* Add a new file to the archive. * * This requires creating and populating a ZipEntry structure, and copying * the data into the file at the appropriate position. The "appropriate * position" is the current location of the central directory, which we * casually overwrite (we can put it back later). * * If we were concerned about safety, we would want to make all changes * in a temp file a
| 421 | * safely written. Not really a concern for us. |
| 422 | */ |
| 423 | status_t ZipFile::addCommon(const char* fileName, const void* data, size_t size, |
| 424 | const char* storageName, int sourceType, int compressionMethod, |
| 425 | ZipEntry** ppEntry) |
| 426 | { |
| 427 | ZipEntry* pEntry = NULL; |
| 428 | status_t result = NO_ERROR; |
| 429 | long lfhPosn, startPosn, endPosn, uncompressedLen; |
| 430 | FILE* inputFp = NULL; |
| 431 | unsigned long crc; |
| 432 | time_t modWhen; |
| 433 | |
| 434 | if (mReadOnly) |
| 435 | return INVALID_OPERATION; |
| 436 | |
| 437 | assert(compressionMethod == ZipEntry::kCompressDeflated || |
| 438 | compressionMethod == ZipEntry::kCompressStored); |
| 439 | |
| 440 | /* make sure we're in a reasonable state */ |
| 441 | assert(mZipFp != NULL); |
| 442 | assert(mEntries.size() == mEOCD.mTotalNumEntries); |
| 443 | |
| 444 | /* make sure it doesn't already exist */ |
| 445 | if (getEntryByName(storageName) != NULL) |
| 446 | return ALREADY_EXISTS; |
| 447 | |
| 448 | if (!data) { |
| 449 | inputFp = fopen(fileName, FILE_OPEN_RO); |
| 450 | if (inputFp == NULL) |
| 451 | return errnoToStatus(errno); |
| 452 | } |
| 453 | |
| 454 | if (fseek(mZipFp, mEOCD.mCentralDirOffset, SEEK_SET) != 0) { |
| 455 | result = UNKNOWN_ERROR; |
| 456 | goto bail; |
| 457 | } |
| 458 | |
| 459 | pEntry = new ZipEntry; |
| 460 | pEntry->initNew(storageName, NULL); |
| 461 | |
| 462 | /* |
| 463 | * From here on out, failures are more interesting. |
| 464 | */ |
| 465 | mNeedCDRewrite = true; |
| 466 | |
| 467 | /* |
| 468 | * Write the LFH, even though it's still mostly blank. We need it |
| 469 | * as a place-holder. In theory the LFH isn't necessary, but in |
| 470 | * practice some utilities demand it. |
| 471 | */ |
| 472 | lfhPosn = ftell(mZipFp); |
| 473 | pEntry->mLFH.write(mZipFp); |
| 474 | startPosn = ftell(mZipFp); |
| 475 | |
| 476 | /* |
| 477 | * Copy the data in, possibly compressing it as we go. |
| 478 | */ |
| 479 | if (sourceType == ZipEntry::kCompressStored) { |
| 480 | if (compressionMethod == ZipEntry::kCompressDeflated) { |
nothing calls this directly
no test coverage detected