diff options
author | jorgelo@chromium.org <jorgelo@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-15 19:47:38 +0000 |
---|---|---|
committer | jorgelo@chromium.org <jorgelo@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-15 19:47:38 +0000 |
commit | 4e5a3a54806e79871eac5cfd5b1436ebdd66bd46 (patch) | |
tree | 335bc3a531b0f45565052b4fb0d162d67ddd4625 /chrome/common | |
parent | de98810ae94f84c3929502383bc90f7aae9afb20 (diff) | |
download | chromium_src-4e5a3a54806e79871eac5cfd5b1436ebdd66bd46.zip chromium_src-4e5a3a54806e79871eac5cfd5b1436ebdd66bd46.tar.gz chromium_src-4e5a3a54806e79871eac5cfd5b1436ebdd66bd46.tar.bz2 |
Add an API to unpack Zip files directly from and to file descriptors.
BUG=None
TEST=zip_reader_unittest.cc (compiles into 'unit_tests' binary)
Review URL: http://codereview.chromium.org/8873039
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@114684 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common')
-rw-r--r-- | chrome/common/zip_internal.cc | 50 | ||||
-rw-r--r-- | chrome/common/zip_internal.h | 5 | ||||
-rw-r--r-- | chrome/common/zip_reader.cc | 75 | ||||
-rw-r--r-- | chrome/common/zip_reader.h | 16 | ||||
-rw-r--r-- | chrome/common/zip_reader_unittest.cc | 117 |
5 files changed, 255 insertions, 8 deletions
diff --git a/chrome/common/zip_internal.cc b/chrome/common/zip_internal.cc index ddcfd00..7a15895 100644 --- a/chrome/common/zip_internal.cc +++ b/chrome/common/zip_internal.cc @@ -9,6 +9,8 @@ #include "third_party/zlib/contrib/minizip/zip.h" #if defined(OS_WIN) #include "third_party/zlib/contrib/minizip/iowin32.h" +#elif defined(OS_POSIX) +#include "third_party/zlib/contrib/minizip/ioapi.h" #endif namespace { @@ -65,6 +67,45 @@ void* ZipOpenFunc(void *opaque, const char* filename, int mode) { } #endif +#if defined(OS_POSIX) +// Callback function for zlib that opens a file stream from a file descriptor. +void* FdOpenFileFunc(void* opaque, const char* filename, int mode) { + FILE* file = NULL; + const char* mode_fopen = NULL; + + if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER) == ZLIB_FILEFUNC_MODE_READ) + mode_fopen = "rb"; + else if (mode & ZLIB_FILEFUNC_MODE_EXISTING) + mode_fopen = "r+b"; + else if (mode & ZLIB_FILEFUNC_MODE_CREATE) + mode_fopen = "wb"; + + if ((filename != NULL) && (mode_fopen != NULL)) + file = fdopen(*static_cast<int*>(opaque), mode_fopen); + + return file; +} + +// We don't actually close the file stream since that would close +// the underlying file descriptor, and we don't own it. We do free +// |opaque| since we malloc'ed it in FillFdOpenFileFunc. +int CloseFileFunc(void* opaque, void* stream) { + free(opaque); + return 0; +} + +// Fills |pzlib_filecunc_def| appropriately to handle the zip file +// referred to by |fd|. +void FillFdOpenFileFunc(zlib_filefunc_def* pzlib_filefunc_def, int fd) { + fill_fopen_filefunc(pzlib_filefunc_def); + pzlib_filefunc_def->zopen_file = FdOpenFileFunc; + pzlib_filefunc_def->zclose_file = CloseFileFunc; + int* ptr_fd = static_cast<int*>(malloc(sizeof(fd))); + *ptr_fd = fd; + pzlib_filefunc_def->opaque = ptr_fd; +} +#endif // defined(OS_POSIX) + } // namespace namespace zip { @@ -81,6 +122,15 @@ unzFile OpenForUnzipping(const std::string& file_name_utf8) { return unzOpen2(file_name_utf8.c_str(), zip_func_ptrs); } +#if defined(OS_POSIX) +unzFile OpenFdForUnzipping(int zip_fd) { + zlib_filefunc_def zip_funcs; + FillFdOpenFileFunc(&zip_funcs, zip_fd); + // Passing dummy "fd" filename to zlib. + return unzOpen2("fd", &zip_funcs); +} +#endif + zipFile OpenForZipping(const std::string& file_name_utf8, int append_flag) { zlib_filefunc_def* zip_func_ptrs = NULL; #if defined(OS_WIN) diff --git a/chrome/common/zip_internal.h b/chrome/common/zip_internal.h index 3f750ea..db40a4c 100644 --- a/chrome/common/zip_internal.h +++ b/chrome/common/zip_internal.h @@ -20,6 +20,11 @@ namespace internal { // Windows. unzFile OpenForUnzipping(const std::string& file_name_utf8); +#if defined(OS_POSIX) +// Opens the file referred to by |zip_fd| for unzipping. +unzFile OpenFdForUnzipping(int zip_fd); +#endif + // Opens the given file name in UTF-8 for zipping, with some setup for // Windows. |append_flag| will be passed to zipOpen2(). zipFile OpenForZipping(const std::string& file_name_utf8, int append_flag); diff --git a/chrome/common/zip_reader.cc b/chrome/common/zip_reader.cc index 88d3f8f..461a65a 100644 --- a/chrome/common/zip_reader.cc +++ b/chrome/common/zip_reader.cc @@ -82,18 +82,21 @@ bool ZipReader::Open(const FilePath& zip_file_path) { return false; } - unz_global_info zip_info = {}; // Zero-clear. - if (unzGetGlobalInfo(zip_file_, &zip_info) != UNZ_OK) { + return OpenInternal(); +} + +#if defined(OS_POSIX) +bool ZipReader::OpenFromFd(const int zip_fd) { + DCHECK(!zip_file_); + + zip_file_ = internal::OpenFdForUnzipping(zip_fd); + if (!zip_file_) { return false; } - num_entries_ = zip_info.number_entry; - if (num_entries_ < 0) - return false; - // We are already at the end if the zip file is empty. - reached_end_ = (num_entries_ == 0); - return true; + return OpenInternal(); } +#endif void ZipReader::Close() { if (zip_file_) { @@ -229,6 +232,62 @@ bool ZipReader::ExtractCurrentEntryIntoDirectory( return ExtractCurrentEntryToFilePath(output_file_path); } +#if defined(OS_POSIX) +bool ZipReader::ExtractCurrentEntryToFd(const int fd) { + DCHECK(zip_file_); + + // If this is a directory, there's nothing to extract to the file descriptor, + // so return false. + if (current_entry_info()->is_directory()) + return false; + + const int open_result = unzOpenCurrentFile(zip_file_); + if (open_result != UNZ_OK) + return false; + + bool success = true; // This becomes false when something bad happens. + while (true) { + char buf[internal::kZipBufSize]; + const int num_bytes_read = unzReadCurrentFile(zip_file_, buf, + internal::kZipBufSize); + if (num_bytes_read == 0) { + // Reached the end of the file. + break; + } else if (num_bytes_read < 0) { + // If num_bytes_read < 0, then it's a specific UNZ_* error code. + success = false; + break; + } else if (num_bytes_read > 0) { + // Some data is read. Write it to the output file descriptor. + if (num_bytes_read != + file_util::WriteFileDescriptor(fd, buf, num_bytes_read)) { + success = false; + break; + } + } + } + + unzCloseCurrentFile(zip_file_); + return success; +} +#endif // defined(OS_POSIX) + +bool ZipReader::OpenInternal() { + DCHECK(zip_file_); + + unz_global_info zip_info = {}; // Zero-clear. + if (unzGetGlobalInfo(zip_file_, &zip_info) != UNZ_OK) { + return false; + } + num_entries_ = zip_info.number_entry; + if (num_entries_ < 0) + return false; + + // We are already at the end if the zip file is empty. + reached_end_ = (num_entries_ == 0); + return true; +} + void ZipReader::Reset() { zip_file_ = NULL; num_entries_ = 0; diff --git a/chrome/common/zip_reader.h b/chrome/common/zip_reader.h index f77616b..4d93d0c 100644 --- a/chrome/common/zip_reader.h +++ b/chrome/common/zip_reader.h @@ -10,6 +10,7 @@ #include "base/basictypes.h" #include "base/file_path.h" +#include "base/file_util.h" #include "base/memory/scoped_ptr.h" #include "base/time.h" #include "third_party/zlib/contrib/minizip/unzip.h" @@ -77,6 +78,12 @@ class ZipReader { // success. bool Open(const FilePath& zip_file_path); +#if defined(OS_POSIX) + // Opens the zip file referred to by the file descriptor |zip_fd|. + // Returns true on success. + bool OpenFromFd(int zip_fd); +#endif + // Closes the currently opened zip file. This function is called in the // destructor of the class, so you usually don't need to call this. void Close(); @@ -125,6 +132,12 @@ class ZipReader { // beforehand. bool ExtractCurrentEntryIntoDirectory(const FilePath& output_directory_path); +#if defined(OS_POSIX) + // Extracts the current entry by writing directly to a file descriptor. + // Does not close the file descriptor. Returns true on success. + bool ExtractCurrentEntryToFd(int fd); +#endif + // Returns the current entry info. Returns NULL if the current entry is // not yet opened. OpenCurrentEntryInZip() must be called beforehand. EntryInfo* current_entry_info() const { @@ -136,6 +149,9 @@ class ZipReader { int num_entries() const { return num_entries_; } private: + // Common code used both in Open and OpenFromFd. + bool OpenInternal(); + // Resets the internal state. void Reset(); diff --git a/chrome/common/zip_reader_unittest.cc b/chrome/common/zip_reader_unittest.cc index 82f0b14..85641d1 100644 --- a/chrome/common/zip_reader_unittest.cc +++ b/chrome/common/zip_reader_unittest.cc @@ -4,6 +4,12 @@ #include "chrome/common/zip_reader.h" +#if defined(OS_POSIX) +#include <fcntl.h> +#include <sys/stat.h> +#include <sys/types.h> +#endif + #include <set> #include <string> @@ -18,6 +24,46 @@ #include "testing/gtest/include/gtest/gtest.h" #include "testing/platform_test.h" +namespace { + +#if defined(OS_POSIX) +// Wrap file descriptors in a class so that we don't leak them in tests. +class FdWrapper { + public: + typedef enum { + READ_ONLY, + READ_WRITE + } AccessMode; + + FdWrapper(const FilePath& file, AccessMode mode) : fd_(-1) { + switch (mode) { + case READ_ONLY: + fd_ = open(file.value().c_str(), O_RDONLY); + break; + case READ_WRITE: + fd_ = open(file.value().c_str(), + O_RDWR | O_CREAT, + S_IRUSR | S_IWUSR); + break; + default: + NOTREACHED(); + } + return; + } + + ~FdWrapper() { + close(fd_); + } + + int fd() { return fd_; } + + private: + int fd_; +}; +#endif + +} // namespace + namespace zip { // Make the test a PlatformTest to setup autorelease pools properly on Mac. @@ -74,6 +120,14 @@ TEST_F(ZipReaderTest, Open_ValidZipFile) { ASSERT_TRUE(reader.Open(test_zip_file_)); } +#if defined(OS_POSIX) +TEST_F(ZipReaderTest, Open_ValidZipFd) { + ZipReader reader; + FdWrapper zip_fd_wrapper(test_zip_file_, FdWrapper::READ_ONLY); + ASSERT_TRUE(reader.OpenFromFd(zip_fd_wrapper.fd())); +} +#endif + TEST_F(ZipReaderTest, Open_NonExistentFile) { ZipReader reader; ASSERT_FALSE(reader.Open(test_data_dir_.AppendASCII("nonexistent.zip"))); @@ -102,6 +156,26 @@ TEST_F(ZipReaderTest, Iteration) { EXPECT_EQ(test_zip_contents_, actual_contents); } +#if defined(OS_POSIX) +// Open the test zip file from a file descriptor, iterate through its contents, +// and compare that they match the expected contents. +TEST_F(ZipReaderTest, FdIteration) { + std::set<FilePath> actual_contents; + ZipReader reader; + FdWrapper zip_fd_wrapper(test_zip_file_, FdWrapper::READ_ONLY); + ASSERT_TRUE(reader.OpenFromFd(zip_fd_wrapper.fd())); + while (reader.HasMore()) { + ASSERT_TRUE(reader.OpenCurrentEntryInZip()); + actual_contents.insert(reader.current_entry_info()->file_path()); + ASSERT_TRUE(reader.AdvanceToNextEntry()); + } + EXPECT_FALSE(reader.AdvanceToNextEntry()); // Shouldn't go further. + EXPECT_EQ(test_zip_contents_.size(), + static_cast<size_t>(reader.num_entries())); + EXPECT_EQ(test_zip_contents_.size(), actual_contents.size()); + EXPECT_EQ(test_zip_contents_, actual_contents); +} +#endif TEST_F(ZipReaderTest, LocateAndOpenEntry_ValidFile) { std::set<FilePath> actual_contents; @@ -140,6 +214,49 @@ TEST_F(ZipReaderTest, ExtractCurrentEntryToFilePath_RegularFile) { EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size()); } +#if defined(OS_POSIX) +TEST_F(ZipReaderTest, FdExtractCurrentEntryToFilePath_RegularFile) { + ZipReader reader; + FdWrapper zip_fd_wrapper(test_zip_file_, FdWrapper::READ_ONLY); + ASSERT_TRUE(reader.OpenFromFd(zip_fd_wrapper.fd())); + FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); + ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); + ASSERT_TRUE(reader.ExtractCurrentEntryToFilePath( + test_dir_.AppendASCII("quux.txt"))); + // Read the output file and compute the MD5. + std::string output; + ASSERT_TRUE(file_util::ReadFileToString(test_dir_.AppendASCII("quux.txt"), + &output)); + const std::string md5 = base::MD5String(output); + const std::string kExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6"; + EXPECT_EQ(kExpectedMD5, md5); + // quux.txt should be larger than kZipBufSize so that we can exercise + // the loop in ExtractCurrentEntry(). + EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size()); +} + +TEST_F(ZipReaderTest, FdExtractCurrentEntryToFd_RegularFile) { + ZipReader reader; + FdWrapper zip_fd_wrapper(test_zip_file_, FdWrapper::READ_ONLY); + ASSERT_TRUE(reader.OpenFromFd(zip_fd_wrapper.fd())); + FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); + FilePath out_path = test_dir_.AppendASCII("quux.txt"); + FdWrapper out_fd_w(out_path, FdWrapper::READ_WRITE); + ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); + ASSERT_TRUE(reader.ExtractCurrentEntryToFd(out_fd_w.fd())); + // Read the output file and compute the MD5. + std::string output; + ASSERT_TRUE(file_util::ReadFileToString(test_dir_.AppendASCII("quux.txt"), + &output)); + const std::string md5 = base::MD5String(output); + const std::string kExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6"; + EXPECT_EQ(kExpectedMD5, md5); + // quux.txt should be larger than kZipBufSize so that we can exercise + // the loop in ExtractCurrentEntry(). + EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size()); +} +#endif + TEST_F(ZipReaderTest, ExtractCurrentEntryToFilePath_Directory) { ZipReader reader; ASSERT_TRUE(reader.Open(test_zip_file_)); |