From d8d7e5b9bb11e4959dd4fe04602ed83b6b022e1d Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 11 May 2015 11:02:17 +0800 Subject: [PATCH] Add Archive::GetFD --- atom/common/api/atom_api_asar.cc | 8 ++++++++ atom/common/asar/archive.cc | 20 +++++++++++++++++++- atom/common/asar/archive.h | 3 +++ atom/common/asar/scoped_temporary_file.cc | 6 +++--- atom/common/asar/scoped_temporary_file.h | 2 +- 5 files changed, 34 insertions(+), 5 deletions(-) diff --git a/atom/common/api/atom_api_asar.cc b/atom/common/api/atom_api_asar.cc index d035159380a4..3fd2ca6f86f7 100644 --- a/atom/common/api/atom_api_asar.cc +++ b/atom/common/api/atom_api_asar.cc @@ -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); } diff --git a/atom/common/asar/archive.cc b/atom/common/asar/archive.cc index e0d2373d9d97..98e464650bac 100644 --- a/atom/common/asar/archive.cc +++ b/atom/common/asar/archive.cc @@ -4,6 +4,10 @@ #include "atom/common/asar/archive.h" +#if defined(OS_WIN) +#include +#endif + #include #include @@ -250,7 +254,7 @@ bool Archive::CopyFileOut(const base::FilePath& path, base::FilePath* out) { } scoped_ptr 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(file_.GetPlatformFile()), 0); +#elif defined(OS_POSIX) + return file_.GetPlatformFile(); +#else + return -1; +#endif +} + } // namespace asar diff --git a/atom/common/asar/archive.h b/atom/common/asar/archive.h index e8bd7c5b063a..2acd17fd7ab4 100644 --- a/atom/common/asar/archive.h +++ b/atom/common/asar/archive.h @@ -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(); } diff --git a/atom/common/asar/scoped_temporary_file.cc b/atom/common/asar/scoped_temporary_file.cc index de70a333279f..6fccc9434fdb 100644 --- a/atom/common/asar/scoped_temporary_file.cc +++ b/atom/common/asar/scoped_temporary_file.cc @@ -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 buf(size); - int len = src.Read(offset, buf.data(), buf.size()); + int len = src->Read(offset, buf.data(), buf.size()); if (len != static_cast(size)) return false; diff --git a/atom/common/asar/scoped_temporary_file.h b/atom/common/asar/scoped_temporary_file.h index 8d822e99d60f..ffaee22e514e 100644 --- a/atom/common/asar/scoped_temporary_file.h +++ b/atom/common/asar/scoped_temporary_file.h @@ -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_; }