Add Archive::GetFD

This commit is contained in:
Cheng Zhao 2015-05-11 11:02:17 +08:00
parent f8e1dfbbc6
commit d8d7e5b9bb
5 changed files with 34 additions and 5 deletions

View file

@ -88,6 +88,13 @@ class Archive : public mate::Wrappable {
return mate::ConvertToV8(isolate, new_path); 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. // Free the resources used by archive.
void Destroy() { void Destroy() {
archive_.reset(); archive_.reset();
@ -102,6 +109,7 @@ class Archive : public mate::Wrappable {
.SetMethod("readdir", &Archive::Readdir) .SetMethod("readdir", &Archive::Readdir)
.SetMethod("realpath", &Archive::Realpath) .SetMethod("realpath", &Archive::Realpath)
.SetMethod("copyFileOut", &Archive::CopyFileOut) .SetMethod("copyFileOut", &Archive::CopyFileOut)
.SetMethod("getFd", &Archive::GetFD)
.SetMethod("destroy", &Archive::Destroy); .SetMethod("destroy", &Archive::Destroy);
} }

View file

@ -4,6 +4,10 @@
#include "atom/common/asar/archive.h" #include "atom/common/asar/archive.h"
#if defined(OS_WIN)
#include <io.h>
#endif
#include <string> #include <string>
#include <vector> #include <vector>
@ -250,7 +254,7 @@ bool Archive::CopyFileOut(const base::FilePath& path, base::FilePath* out) {
} }
scoped_ptr<ScopedTemporaryFile> temp_file(new ScopedTemporaryFile); 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; return false;
*out = temp_file->path(); *out = temp_file->path();
@ -258,4 +262,18 @@ bool Archive::CopyFileOut(const base::FilePath& path, base::FilePath* out) {
return true; 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 } // namespace asar

View file

@ -60,6 +60,9 @@ class Archive {
// For unpacked file, this method will return its real path. // For unpacked file, this method will return its real path.
bool CopyFileOut(const base::FilePath& path, base::FilePath* out); bool CopyFileOut(const base::FilePath& path, base::FilePath* out);
// Returns the file's fd.
int GetFD() const;
base::FilePath path() const { return path_; } base::FilePath path() const { return path_; }
base::DictionaryValue* header() const { return header_.get(); } base::DictionaryValue* header() const { return header_.get(); }

View file

@ -36,16 +36,16 @@ bool ScopedTemporaryFile::Init() {
return base::CreateTemporaryFile(&path_); return base::CreateTemporaryFile(&path_);
} }
bool ScopedTemporaryFile::InitFromFile(base::File& src, bool ScopedTemporaryFile::InitFromFile(base::File* src,
uint64 offset, uint64 size) { uint64 offset, uint64 size) {
if (!src.IsValid()) if (!src->IsValid())
return false; return false;
if (!Init()) if (!Init())
return false; return false;
std::vector<char> buf(size); 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)) if (len != static_cast<int>(size))
return false; return false;

View file

@ -26,7 +26,7 @@ class ScopedTemporaryFile {
bool Init(); bool Init();
// Init an temporary file and fill it with content of |path|. // 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_; } base::FilePath path() const { return path_; }