From f8e1dfbbc637e600c05fbb06bda0ec0cf41f4e02 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 11 May 2015 10:47:07 +0800 Subject: [PATCH] Keep archive's file opened in the archive's whole life time --- atom/common/asar/archive.cc | 10 +++++----- atom/common/asar/archive.h | 2 ++ atom/common/asar/scoped_temporary_file.cc | 7 +++---- atom/common/asar/scoped_temporary_file.h | 6 +++++- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/atom/common/asar/archive.cc b/atom/common/asar/archive.cc index 4ad8c1817a9..e0d2373d9d9 100644 --- a/atom/common/asar/archive.cc +++ b/atom/common/asar/archive.cc @@ -104,6 +104,7 @@ bool FillFileInfoWithNode(Archive::FileInfo* info, Archive::Archive(const base::FilePath& path) : path_(path), + file_(path_, base::File::FLAG_OPEN | base::File::FLAG_READ), header_size_(0) { } @@ -111,15 +112,14 @@ Archive::~Archive() { } bool Archive::Init() { - base::File file(path_, base::File::FLAG_OPEN | base::File::FLAG_READ); - if (!file.IsValid()) + if (!file_.IsValid()) return false; std::vector buf; int len; buf.resize(8); - len = file.ReadAtCurrentPos(buf.data(), buf.size()); + len = file_.ReadAtCurrentPos(buf.data(), buf.size()); if (len != static_cast(buf.size())) { PLOG(ERROR) << "Failed to read header size from " << path_.value(); return false; @@ -132,7 +132,7 @@ bool Archive::Init() { } buf.resize(size); - len = file.ReadAtCurrentPos(buf.data(), buf.size()); + len = file_.ReadAtCurrentPos(buf.data(), buf.size()); if (len != static_cast(buf.size())) { PLOG(ERROR) << "Failed to read header from " << path_.value(); return false; @@ -250,7 +250,7 @@ bool Archive::CopyFileOut(const base::FilePath& path, base::FilePath* out) { } scoped_ptr temp_file(new ScopedTemporaryFile); - if (!temp_file->InitFromFile(path_, info.offset, info.size)) + if (!temp_file->InitFromFile(file_, info.offset, info.size)) return false; *out = temp_file->path(); diff --git a/atom/common/asar/archive.h b/atom/common/asar/archive.h index 53adbfc13c4..e8bd7c5b063 100644 --- a/atom/common/asar/archive.h +++ b/atom/common/asar/archive.h @@ -8,6 +8,7 @@ #include #include "base/containers/scoped_ptr_hash_map.h" +#include "base/files/file.h" #include "base/files/file_path.h" #include "base/memory/scoped_ptr.h" @@ -64,6 +65,7 @@ class Archive { private: base::FilePath path_; + base::File file_; uint32 header_size_; scoped_ptr header_; diff --git a/atom/common/asar/scoped_temporary_file.cc b/atom/common/asar/scoped_temporary_file.cc index 574f178f6f3..de70a333279 100644 --- a/atom/common/asar/scoped_temporary_file.cc +++ b/atom/common/asar/scoped_temporary_file.cc @@ -36,13 +36,12 @@ bool ScopedTemporaryFile::Init() { return base::CreateTemporaryFile(&path_); } -bool ScopedTemporaryFile::InitFromFile(const base::FilePath& path, +bool ScopedTemporaryFile::InitFromFile(base::File& src, uint64 offset, uint64 size) { - if (!Init()) + if (!src.IsValid()) return false; - base::File src(path, base::File::FLAG_OPEN | base::File::FLAG_READ); - if (!src.IsValid()) + if (!Init()) return false; std::vector buf(size); diff --git a/atom/common/asar/scoped_temporary_file.h b/atom/common/asar/scoped_temporary_file.h index 62aafc8f07d..8d822e99d60 100644 --- a/atom/common/asar/scoped_temporary_file.h +++ b/atom/common/asar/scoped_temporary_file.h @@ -7,6 +7,10 @@ #include "base/files/file_path.h" +namespace base { +class File; +} + namespace asar { // An object representing a temporary file that should be cleaned up when this @@ -22,7 +26,7 @@ class ScopedTemporaryFile { bool Init(); // Init an temporary file and fill it with content of |path|. - bool InitFromFile(const base::FilePath& path, uint64 offset, uint64 size); + bool InitFromFile(base::File& src, uint64 offset, uint64 size); base::FilePath path() const { return path_; }