summaryrefslogtreecommitdiff
path: root/libziparchive
diff options
context:
space:
mode:
authorDmitriy Ivanov <dimitry@google.com>2014-07-15 19:33:00 -0700
committerDmitriy Ivanov <dimitry@google.com>2015-01-30 17:57:13 -0800
commit40b52b2c884e2a0abf9c4af8b785433286b7e84b (patch)
tree3fcbfee057ce1576507ea59fda4c06e8c9c3b647 /libziparchive
parentd170bb035dc652b7e350ab7ccca0b1ffa332054e (diff)
Add close_file flag to OpenArchiveFd()
* We should be able to keep fd alive after CloseArchive() Change-Id: I1aa2c039bb2a590ae72f256acc9ba5401c2c59b1
Diffstat (limited to 'libziparchive')
-rw-r--r--libziparchive/zip_archive.cc13
-rw-r--r--libziparchive/zip_archive_test.cc21
2 files changed, 29 insertions, 5 deletions
diff --git a/libziparchive/zip_archive.cc b/libziparchive/zip_archive.cc
index b6fd0d25f..afc122dc4 100644
--- a/libziparchive/zip_archive.cc
+++ b/libziparchive/zip_archive.cc
@@ -289,6 +289,7 @@ static const char kTempMappingFileName[] = "zip: ExtractFileToFile";
struct ZipArchive {
/* open Zip archive */
const int fd;
+ const bool close_file;
/* mapped central directory area */
off64_t directory_offset;
@@ -306,8 +307,9 @@ struct ZipArchive {
uint32_t hash_table_size;
ZipEntryName* hash_table;
- ZipArchive(const int fd) :
+ ZipArchive(const int fd, bool assume_ownership) :
fd(fd),
+ close_file(assume_ownership),
directory_offset(0),
directory_map(NULL),
num_entries(0),
@@ -315,7 +317,7 @@ struct ZipArchive {
hash_table(NULL) {}
~ZipArchive() {
- if (fd >= 0) {
+ if (close_file && fd >= 0) {
close(fd);
}
@@ -690,21 +692,22 @@ static int32_t OpenArchiveInternal(ZipArchive* archive,
}
int32_t OpenArchiveFd(int fd, const char* debug_file_name,
- ZipArchiveHandle* handle) {
- ZipArchive* archive = new ZipArchive(fd);
+ ZipArchiveHandle* handle, bool assume_ownership) {
+ ZipArchive* archive = new ZipArchive(fd, assume_ownership);
*handle = archive;
return OpenArchiveInternal(archive, debug_file_name);
}
int32_t OpenArchive(const char* fileName, ZipArchiveHandle* handle) {
const int fd = open(fileName, O_RDONLY | O_BINARY, 0);
- ZipArchive* archive = new ZipArchive(fd);
+ ZipArchive* archive = new ZipArchive(fd, true);
*handle = archive;
if (fd < 0) {
ALOGW("Unable to open '%s': %s", fileName, strerror(errno));
return kIoError;
}
+
return OpenArchiveInternal(archive, fileName);
}
diff --git a/libziparchive/zip_archive_test.cc b/libziparchive/zip_archive_test.cc
index 4775de00c..c8dafa9f6 100644
--- a/libziparchive/zip_archive_test.cc
+++ b/libziparchive/zip_archive_test.cc
@@ -17,6 +17,7 @@
#include "ziparchive/zip_archive.h"
#include <errno.h>
+#include <fcntl.h>
#include <getopt.h>
#include <stdio.h>
#include <unistd.h>
@@ -88,6 +89,26 @@ TEST(ziparchive, OpenMissing) {
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) {
ZipArchiveHandle handle;
ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle));