CheckPathInZipArchive 95/56795/3
authorTomasz Iwanek <t.iwanek@samsung.com>
Tue, 12 Jan 2016 12:20:47 +0000 (13:20 +0100)
committerPawel Sikorski <p.sikorski@samsung.com>
Thu, 14 Jan 2016 12:00:38 +0000 (04:00 -0800)
Change-Id: I3ef523eee112202eb306e48cf247c0f05d59508e

src/common/utils/file_util.cc
src/common/utils/file_util.h

index 4924ce6..bfc9379 100644 (file)
@@ -389,6 +389,56 @@ bool ExtractToTmpDir(const char* zip_path, const bf::path& tmp_dir,
   return true;
 }
 
+bool CheckPathInZipArchive(const char* zip_archive_path,
+                           const boost::filesystem::path& relative_zip_path,
+                           bool* found) {
+  *found = false;
+  UnzFilePointer zip_file;
+  if (!zip_file.Open(zip_archive_path)) {
+    LOG(ERROR) << "Failed to open the source dir: " << zip_archive_path;
+    return false;
+  }
+
+  unz_global_info info;
+  if (unzGetGlobalInfo(zip_file.Get(), &info) != UNZ_OK) {
+    LOG(ERROR) << "Failed to read global info";
+    return false;
+  }
+
+  char raw_file_name_in_zip[kZipMaxPath];
+  unz_file_info raw_file_info;
+  for (uLong i = 0; i < info.number_entry; i++) {
+    if (unzGetCurrentFileInfo(zip_file.Get(),
+                              &raw_file_info,
+                              raw_file_name_in_zip,
+                              sizeof(raw_file_name_in_zip),
+                              nullptr,
+                              0,
+                              nullptr,
+                              0) != UNZ_OK) {
+      LOG(ERROR) << "Failed to read file info";
+      return false;
+    }
+
+    if (raw_file_name_in_zip[0] == '\0')
+      return false;
+
+    if (relative_zip_path.string() == raw_file_name_in_zip) {
+      *found = true;
+      return true;
+    }
+
+    if ((i + 1) < info.number_entry) {
+      if (unzGoToNextFile(zip_file.Get()) != UNZ_OK) {
+        LOG(ERROR) << "Failed to read next file";
+        return false;
+      }
+    }
+  }
+
+  return true;
+}
+
 bool HasDirectoryClimbing(const boost::filesystem::path& path) {
   std::vector<std::string> segments;
   ba::split(segments, path.string(), ba::is_any_of("/\\"));
index 183da45..e546c29 100644 (file)
@@ -40,6 +40,10 @@ bool ExtractToTmpDir(const char* zip_path,
                      const boost::filesystem::path& tmp_dir,
                      const std::string& filter_prefix);
 
+bool CheckPathInZipArchive(const char* zip_archive_path,
+                           const boost::filesystem::path& relative_zip_path,
+                           bool* found);
+
 bool HasDirectoryClimbing(const boost::filesystem::path& path);
 
 boost::filesystem::path MakeRelativePath(const boost::filesystem::path& input,