Add Archive::GetFD
authorCheng Zhao <zcbenz@gmail.com>
Mon, 11 May 2015 03:02:17 +0000 (11:02 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Mon, 11 May 2015 03:02:17 +0000 (11:02 +0800)
atom/common/api/atom_api_asar.cc
atom/common/asar/archive.cc
atom/common/asar/archive.h
atom/common/asar/scoped_temporary_file.cc
atom/common/asar/scoped_temporary_file.h

index d035159..3fd2ca6 100644 (file)
@@ -88,6 +88,13 @@ class Archive : public mate::Wrappable {
     return mate::ConvertToV8(isolate, new_path);
   }
 
+  // Return the file descriptor.
+  int GetFD() const {
+    if (!archive_)
+      return -1;
+    return archive_->GetFD();
+  }
+
   // Free the resources used by archive.
   void Destroy() {
     archive_.reset();
@@ -102,6 +109,7 @@ class Archive : public mate::Wrappable {
         .SetMethod("readdir", &Archive::Readdir)
         .SetMethod("realpath", &Archive::Realpath)
         .SetMethod("copyFileOut", &Archive::CopyFileOut)
+        .SetMethod("getFd", &Archive::GetFD)
         .SetMethod("destroy", &Archive::Destroy);
   }
 
index e0d2373..98e4646 100644 (file)
@@ -4,6 +4,10 @@
 
 #include "atom/common/asar/archive.h"
 
+#if defined(OS_WIN)
+#include <io.h>
+#endif
+
 #include <string>
 #include <vector>
 
@@ -250,7 +254,7 @@ bool Archive::CopyFileOut(const base::FilePath& path, base::FilePath* out) {
   }
 
   scoped_ptr<ScopedTemporaryFile> temp_file(new ScopedTemporaryFile);
-  if (!temp_file->InitFromFile(file_, info.offset, info.size))
+  if (!temp_file->InitFromFile(&file_, info.offset, info.size))
     return false;
 
   *out = temp_file->path();
@@ -258,4 +262,18 @@ bool Archive::CopyFileOut(const base::FilePath& path, base::FilePath* out) {
   return true;
 }
 
+int Archive::GetFD() const {
+  if (!file_.IsValid())
+    return -1;
+
+#if defined(OS_WIN)
+  return
+    _open_osfhandle(reinterpret_cast<intptr_t>(file_.GetPlatformFile()), 0);
+#elif defined(OS_POSIX)
+  return file_.GetPlatformFile();
+#else
+  return -1;
+#endif
+}
+
 }  // namespace asar
index e8bd7c5..2acd17f 100644 (file)
@@ -60,6 +60,9 @@ class Archive {
   // For unpacked file, this method will return its real path.
   bool CopyFileOut(const base::FilePath& path, base::FilePath* out);
 
+  // Returns the file's fd.
+  int GetFD() const;
+
   base::FilePath path() const { return path_; }
   base::DictionaryValue* header() const { return header_.get(); }
 
index de70a33..6fccc94 100644 (file)
@@ -36,16 +36,16 @@ bool ScopedTemporaryFile::Init() {
   return base::CreateTemporaryFile(&path_);
 }
 
-bool ScopedTemporaryFile::InitFromFile(base::File& src,
+bool ScopedTemporaryFile::InitFromFile(base::File* src,
                                        uint64 offset, uint64 size) {
-  if (!src.IsValid())
+  if (!src->IsValid())
     return false;
 
   if (!Init())
     return false;
 
   std::vector<char> buf(size);
-  int len = src.Read(offset, buf.data(), buf.size());
+  int len = src->Read(offset, buf.data(), buf.size());
   if (len != static_cast<int>(size))
     return false;
 
index 8d822e9..ffaee22 100644 (file)
@@ -26,7 +26,7 @@ class ScopedTemporaryFile {
   bool Init();
 
   // Init an temporary file and fill it with content of |path|.
-  bool InitFromFile(base::File& src, uint64 offset, uint64 size);
+  bool InitFromFile(base::File* src, uint64 offset, uint64 size);
 
   base::FilePath path() const { return path_; }