Recognize asar archive with unpacked files

This commit is contained in:
Cheng Zhao 2015-03-20 20:34:58 +08:00
parent dc82553fc3
commit b5a8cfb704
9 changed files with 84 additions and 55 deletions

View file

@ -41,6 +41,7 @@ class Archive : public mate::Wrappable {
return v8::False(isolate);
mate::Dictionary dict(isolate, v8::Object::New(isolate));
dict.Set("size", info.size);
dict.Set("unpacked", info.unpacked);
dict.Set("offset", info.offset);
return dict.GetHandle();
}

View file

@ -81,18 +81,22 @@ bool GetNodeFromPath(std::string path,
bool FillFileInfoWithNode(Archive::FileInfo* info,
uint32 header_size,
const base::DictionaryValue* node) {
int size;
if (!node->GetInteger("size", &size))
return false;
info->size = static_cast<uint32>(size);
info->unpacked = false;
if (node->GetBoolean("unpacked", &info->unpacked) && info->unpacked)
return true;
std::string offset;
if (!node->GetString("offset", &offset))
return false;
if (!base::StringToUint64(offset, &info->offset))
return false;
int size;
if (!node->GetInteger("size", &size))
return false;
info->offset += header_size;
info->size = static_cast<uint32>(size);
return true;
}
@ -240,6 +244,11 @@ bool Archive::CopyFileOut(const base::FilePath& path, base::FilePath* out) {
if (!GetFileInfo(path, &info))
return false;
if (info.unpacked) {
*out = path_.AddExtension(FILE_PATH_LITERAL("unpacked")).Append(path);
return true;
}
scoped_ptr<ScopedTemporaryFile> temp_file(new ScopedTemporaryFile);
if (!temp_file->InitFromFile(path_, info.offset, info.size))
return false;

View file

@ -25,6 +25,7 @@ class Archive {
public:
struct FileInfo {
FileInfo() : size(0), offset(0) {}
bool unpacked;
uint32 size;
uint64 offset;
};
@ -55,6 +56,7 @@ class Archive {
bool Realpath(const base::FilePath& path, base::FilePath* realpath);
// Copy the file into a temporary file, and return the new path.
// For unpacked file, this method will return its real path.
bool CopyFileOut(const base::FilePath& path, base::FilePath* out);
base::FilePath path() const { return path_; }

View file

@ -71,6 +71,13 @@ bool ReadFileToString(const base::FilePath& path, std::string* contents) {
if (!archive->GetFileInfo(relative_path, &info))
return false;
if (info.unpacked) {
base::FilePath real_path;
// For unpacked file it will return the real path instead of doing the copy.
archive->CopyFileOut(relative_path, &real_path);
return base::ReadFileToString(real_path, contents);
}
base::File src(asar_path, base::File::FLAG_OPEN | base::File::FLAG_READ);
if (!src.IsValid())
return false;

View file

@ -90,7 +90,7 @@ overrideAPI = (module, name, arg = 0) ->
return callback new Error("Invalid package #{asarPath}") unless archive
newPath = archive.copyFileOut filePath
return callback createNotFoundError(asarPath, filePath) unless newPath
return callback createNotFoundError(asarPath, filePath) unless newPath
arguments[arg] = newPath
old.apply this, arguments
@ -218,6 +218,10 @@ exports.wrapFsWithAsar = (fs) ->
info = archive.getFileInfo filePath
return callback createNotFoundError(asarPath, filePath) unless info
if info.unpacked
realPath = archive.copyFileOut filePath
return fs.readFile realPath, options, callback
if not options
options = encoding: null, flag: 'r'
else if util.isString options
@ -247,6 +251,10 @@ exports.wrapFsWithAsar = (fs) ->
info = archive.getFileInfo filePath
throw createNotFoundError(asarPath, filePath) unless info
if info.unpacked
realPath = archive.copyFileOut filePath
return fs.readFileSync realPath, options
if not options
options = encoding: null, flag: 'r'
else if util.isString options