* Open a file and parse its guts. */
| 133 | * Open a file and parse its guts. |
| 134 | */ |
| 135 | status_t ZipFile::openfd(int fd, int flags) |
| 136 | { |
| 137 | bool newArchive = true; |
| 138 | |
| 139 | assert(mZipFp == NULL); // no reopen |
| 140 | |
| 141 | if ((flags & kOpenTruncate)) |
| 142 | flags |= kOpenCreate; // trunc implies create |
| 143 | |
| 144 | if ((flags & kOpenReadOnly) && (flags & kOpenReadWrite)) |
| 145 | return INVALID_OPERATION; // not both |
| 146 | if (!((flags & kOpenReadOnly) || (flags & kOpenReadWrite))) |
| 147 | return INVALID_OPERATION; // not neither |
| 148 | if ((flags & kOpenCreate) && !(flags & kOpenReadWrite)) |
| 149 | return INVALID_OPERATION; // create requires write |
| 150 | |
| 151 | /* open the file */ |
| 152 | const char* openflags; |
| 153 | if (flags & kOpenReadWrite) { |
| 154 | if (newArchive) |
| 155 | openflags = FILE_OPEN_RW_CREATE; |
| 156 | else |
| 157 | openflags = FILE_OPEN_RW; |
| 158 | } else { |
| 159 | openflags = FILE_OPEN_RO; |
| 160 | } |
| 161 | mZipFp = fdopen(fd, openflags); |
| 162 | if (mZipFp == NULL) { |
| 163 | int err = errno; |
| 164 | ALOGD("fdopen failed: %s\n", strerror(err)); |
| 165 | return errnoToStatus(err); |
| 166 | } |
| 167 | |
| 168 | status_t result; |
| 169 | if (!newArchive) { |
| 170 | /* |
| 171 | * Load the central directory. If that fails, then this probably |
| 172 | * isn't a Zip archive. |
| 173 | */ |
| 174 | result = readCentralDir(); |
| 175 | } else { |
| 176 | /* |
| 177 | * Newly-created. The EndOfCentralDir constructor actually |
| 178 | * sets everything to be the way we want it (all zeroes). We |
| 179 | * set mNeedCDRewrite so that we create *something* if the |
| 180 | * caller doesn't add any files. (We could also just unlink |
| 181 | * the file if it's brand new and nothing was added, but that's |
| 182 | * probably doing more than we really should -- the user might |
| 183 | * have a need for empty zip files.) |
| 184 | */ |
| 185 | mNeedCDRewrite = true; |
| 186 | result = NO_ERROR; |
| 187 | } |
| 188 | |
| 189 | if (flags & kOpenReadOnly) |
| 190 | mReadOnly = true; |
| 191 | else |
| 192 | assert(!mReadOnly); |
no test coverage detected