Keep archive's file opened in the archive's whole life time

This commit is contained in:
Cheng Zhao 2015-05-11 10:47:07 +08:00
parent f02cae1b0a
commit f8e1dfbbc6
4 changed files with 15 additions and 10 deletions

View file

@ -104,6 +104,7 @@ bool FillFileInfoWithNode(Archive::FileInfo* info,
Archive::Archive(const base::FilePath& path) Archive::Archive(const base::FilePath& path)
: path_(path), : path_(path),
file_(path_, base::File::FLAG_OPEN | base::File::FLAG_READ),
header_size_(0) { header_size_(0) {
} }
@ -111,15 +112,14 @@ Archive::~Archive() {
} }
bool Archive::Init() { bool Archive::Init() {
base::File file(path_, base::File::FLAG_OPEN | base::File::FLAG_READ); if (!file_.IsValid())
if (!file.IsValid())
return false; return false;
std::vector<char> buf; std::vector<char> buf;
int len; int len;
buf.resize(8); buf.resize(8);
len = file.ReadAtCurrentPos(buf.data(), buf.size()); len = file_.ReadAtCurrentPos(buf.data(), buf.size());
if (len != static_cast<int>(buf.size())) { if (len != static_cast<int>(buf.size())) {
PLOG(ERROR) << "Failed to read header size from " << path_.value(); PLOG(ERROR) << "Failed to read header size from " << path_.value();
return false; return false;
@ -132,7 +132,7 @@ bool Archive::Init() {
} }
buf.resize(size); buf.resize(size);
len = file.ReadAtCurrentPos(buf.data(), buf.size()); len = file_.ReadAtCurrentPos(buf.data(), buf.size());
if (len != static_cast<int>(buf.size())) { if (len != static_cast<int>(buf.size())) {
PLOG(ERROR) << "Failed to read header from " << path_.value(); PLOG(ERROR) << "Failed to read header from " << path_.value();
return false; return false;
@ -250,7 +250,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(path_, 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();

View file

@ -8,6 +8,7 @@
#include <vector> #include <vector>
#include "base/containers/scoped_ptr_hash_map.h" #include "base/containers/scoped_ptr_hash_map.h"
#include "base/files/file.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
@ -64,6 +65,7 @@ class Archive {
private: private:
base::FilePath path_; base::FilePath path_;
base::File file_;
uint32 header_size_; uint32 header_size_;
scoped_ptr<base::DictionaryValue> header_; scoped_ptr<base::DictionaryValue> header_;

View file

@ -36,13 +36,12 @@ bool ScopedTemporaryFile::Init() {
return base::CreateTemporaryFile(&path_); return base::CreateTemporaryFile(&path_);
} }
bool ScopedTemporaryFile::InitFromFile(const base::FilePath& path, bool ScopedTemporaryFile::InitFromFile(base::File& src,
uint64 offset, uint64 size) { uint64 offset, uint64 size) {
if (!Init()) if (!src.IsValid())
return false; return false;
base::File src(path, base::File::FLAG_OPEN | base::File::FLAG_READ); if (!Init())
if (!src.IsValid())
return false; return false;
std::vector<char> buf(size); std::vector<char> buf(size);

View file

@ -7,6 +7,10 @@
#include "base/files/file_path.h" #include "base/files/file_path.h"
namespace base {
class File;
}
namespace asar { namespace asar {
// An object representing a temporary file that should be cleaned up when this // An object representing a temporary file that should be cleaned up when this
@ -22,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(const base::FilePath& path, 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_; }