* Open a file and parse its guts. */
| 57 | * Open a file and parse its guts. |
| 58 | */ |
| 59 | status_t ZipFile::open(const char* zipFileName, int flags) |
| 60 | { |
| 61 | bool newArchive = false; |
| 62 | |
| 63 | assert(mZipFp == NULL); // no reopen |
| 64 | |
| 65 | if ((flags & kOpenTruncate)) |
| 66 | flags |= kOpenCreate; // trunc implies create |
| 67 | |
| 68 | if ((flags & kOpenReadOnly) && (flags & kOpenReadWrite)) |
| 69 | return INVALID_OPERATION; // not both |
| 70 | if (!((flags & kOpenReadOnly) || (flags & kOpenReadWrite))) |
| 71 | return INVALID_OPERATION; // not neither |
| 72 | if ((flags & kOpenCreate) && !(flags & kOpenReadWrite)) |
| 73 | return INVALID_OPERATION; // create requires write |
| 74 | |
| 75 | if (flags & kOpenTruncate) { |
| 76 | newArchive = true; |
| 77 | } else { |
| 78 | newArchive = (access(zipFileName, F_OK) != 0); |
| 79 | if (!(flags & kOpenCreate) && newArchive) { |
| 80 | /* not creating, must already exist */ |
| 81 | ALOGD("File %s does not exist", zipFileName); |
| 82 | return NAME_NOT_FOUND; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /* open the file */ |
| 87 | const char* openflags; |
| 88 | if (flags & kOpenReadWrite) { |
| 89 | if (newArchive) |
| 90 | openflags = FILE_OPEN_RW_CREATE; |
| 91 | else |
| 92 | openflags = FILE_OPEN_RW; |
| 93 | } else { |
| 94 | openflags = FILE_OPEN_RO; |
| 95 | } |
| 96 | mZipFp = fopen(zipFileName, openflags); |
| 97 | if (mZipFp == NULL) { |
| 98 | int err = errno; |
| 99 | ALOGD("fopen failed: %d\n", err); |
| 100 | return errnoToStatus(err); |
| 101 | } |
| 102 | |
| 103 | status_t result; |
| 104 | if (!newArchive) { |
| 105 | /* |
| 106 | * Load the central directory. If that fails, then this probably |
| 107 | * isn't a Zip archive. |
| 108 | */ |
| 109 | result = readCentralDir(); |
| 110 | } else { |
| 111 | /* |
| 112 | * Newly-created. The EndOfCentralDir constructor actually |
| 113 | * sets everything to be the way we want it (all zeroes). We |
| 114 | * set mNeedCDRewrite so that we create *something* if the |
| 115 | * caller doesn't add any files. (We could also just unlink |
| 116 | * the file if it's brand new and nothing was added, but that's |