Merge "Add close_file flag to OpenArchiveFd()"

This commit is contained in:
Dmitriy Ivanov 2015-02-02 18:48:28 +00:00 committed by Gerrit Code Review
commit 7cb19579bc
3 changed files with 33 additions and 6 deletions

View file

@ -101,6 +101,9 @@ int32_t OpenArchive(const char* fileName, ZipArchiveHandle* handle);
* Sets handle to the value of the opaque handle for this file descriptor. * Sets handle to the value of the opaque handle for this file descriptor.
* This handle must be released by calling CloseArchive with this handle. * This handle must be released by calling CloseArchive with this handle.
* *
* If assume_ownership parameter is 'true' calling CloseArchive will close
* the file.
*
* This function maps and scans the central directory and builds a table * This function maps and scans the central directory and builds a table
* of entries for future lookups. * of entries for future lookups.
* *
@ -109,7 +112,7 @@ int32_t OpenArchive(const char* fileName, ZipArchiveHandle* handle);
* Returns 0 on success, and negative values on failure. * Returns 0 on success, and negative values on failure.
*/ */
int32_t OpenArchiveFd(const int fd, const char* debugFileName, int32_t OpenArchiveFd(const int fd, const char* debugFileName,
ZipArchiveHandle *handle); ZipArchiveHandle *handle, bool assume_ownership = true);
/* /*
* Close archive, releasing resources associated with it. This will * Close archive, releasing resources associated with it. This will

View file

@ -289,6 +289,7 @@ static const char kTempMappingFileName[] = "zip: ExtractFileToFile";
struct ZipArchive { struct ZipArchive {
/* open Zip archive */ /* open Zip archive */
const int fd; const int fd;
const bool close_file;
/* mapped central directory area */ /* mapped central directory area */
off64_t directory_offset; off64_t directory_offset;
@ -306,8 +307,9 @@ struct ZipArchive {
uint32_t hash_table_size; uint32_t hash_table_size;
ZipEntryName* hash_table; ZipEntryName* hash_table;
ZipArchive(const int fd) : ZipArchive(const int fd, bool assume_ownership) :
fd(fd), fd(fd),
close_file(assume_ownership),
directory_offset(0), directory_offset(0),
directory_map(NULL), directory_map(NULL),
num_entries(0), num_entries(0),
@ -315,7 +317,7 @@ struct ZipArchive {
hash_table(NULL) {} hash_table(NULL) {}
~ZipArchive() { ~ZipArchive() {
if (fd >= 0) { if (close_file && fd >= 0) {
close(fd); close(fd);
} }
@ -690,21 +692,22 @@ static int32_t OpenArchiveInternal(ZipArchive* archive,
} }
int32_t OpenArchiveFd(int fd, const char* debug_file_name, int32_t OpenArchiveFd(int fd, const char* debug_file_name,
ZipArchiveHandle* handle) { ZipArchiveHandle* handle, bool assume_ownership) {
ZipArchive* archive = new ZipArchive(fd); ZipArchive* archive = new ZipArchive(fd, assume_ownership);
*handle = archive; *handle = archive;
return OpenArchiveInternal(archive, debug_file_name); return OpenArchiveInternal(archive, debug_file_name);
} }
int32_t OpenArchive(const char* fileName, ZipArchiveHandle* handle) { int32_t OpenArchive(const char* fileName, ZipArchiveHandle* handle) {
const int fd = open(fileName, O_RDONLY | O_BINARY, 0); const int fd = open(fileName, O_RDONLY | O_BINARY, 0);
ZipArchive* archive = new ZipArchive(fd); ZipArchive* archive = new ZipArchive(fd, true);
*handle = archive; *handle = archive;
if (fd < 0) { if (fd < 0) {
ALOGW("Unable to open '%s': %s", fileName, strerror(errno)); ALOGW("Unable to open '%s': %s", fileName, strerror(errno));
return kIoError; return kIoError;
} }
return OpenArchiveInternal(archive, fileName); return OpenArchiveInternal(archive, fileName);
} }

View file

@ -17,6 +17,7 @@
#include "ziparchive/zip_archive.h" #include "ziparchive/zip_archive.h"
#include <errno.h> #include <errno.h>
#include <fcntl.h>
#include <getopt.h> #include <getopt.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
@ -88,6 +89,26 @@ TEST(ziparchive, OpenMissing) {
ASSERT_EQ(-1, GetFileDescriptor(handle)); ASSERT_EQ(-1, GetFileDescriptor(handle));
} }
TEST(ziparchive, OpenAssumeFdOwnership) {
int fd = open((test_data_dir + "/" + kValidZip).c_str(), O_RDONLY);
ASSERT_NE(-1, fd);
ZipArchiveHandle handle;
ASSERT_EQ(0, OpenArchiveFd(fd, "OpenWithAssumeFdOwnership", &handle));
CloseArchive(handle);
ASSERT_EQ(-1, lseek(fd, 0, SEEK_SET));
ASSERT_EQ(EBADF, errno);
}
TEST(ziparchive, OpenDoNotAssumeFdOwnership) {
int fd = open((test_data_dir + "/" + kValidZip).c_str(), O_RDONLY);
ASSERT_NE(-1, fd);
ZipArchiveHandle handle;
ASSERT_EQ(0, OpenArchiveFd(fd, "OpenWithAssumeFdOwnership", &handle, false));
CloseArchive(handle);
ASSERT_EQ(0, lseek(fd, 0, SEEK_SET));
close(fd);
}
TEST(ziparchive, Iteration) { TEST(ziparchive, Iteration) {
ZipArchiveHandle handle; ZipArchiveHandle handle;
ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle)); ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle));