зеркало из https://github.com/electron/electron.git
Make fs.readdir support asar package.
This commit is contained in:
Родитель
9f9d209e3d
Коммит
0cab034dab
|
@ -2,6 +2,8 @@
|
|||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "atom/common/asar/archive.h"
|
||||
#include "atom/common/asar/archive_factory.h"
|
||||
#include "atom/common/native_mate_converters/file_path_converter.h"
|
||||
|
@ -16,13 +18,13 @@ namespace {
|
|||
|
||||
class Archive : public mate::Wrappable {
|
||||
public:
|
||||
static v8::Handle<v8::Value> Create(mate::Arguments* args,
|
||||
static v8::Handle<v8::Value> Create(v8::Isolate* isolate,
|
||||
const base::FilePath& path) {
|
||||
static asar::ArchiveFactory archive_factory;
|
||||
scoped_refptr<asar::Archive> archive = archive_factory.GetOrCreate(path);
|
||||
if (!archive)
|
||||
return v8::False(args->isolate());
|
||||
return (new Archive(archive))->GetWrapper(args->isolate());
|
||||
return v8::False(isolate);
|
||||
return (new Archive(archive))->GetWrapper(isolate);
|
||||
}
|
||||
|
||||
protected:
|
||||
|
@ -30,24 +32,24 @@ class Archive : public mate::Wrappable {
|
|||
virtual ~Archive() {}
|
||||
|
||||
// Reads the offset and size of file.
|
||||
v8::Handle<v8::Value> GetFileInfo(mate::Arguments* args,
|
||||
v8::Handle<v8::Value> GetFileInfo(v8::Isolate* isolate,
|
||||
const base::FilePath& path) {
|
||||
asar::Archive::FileInfo info;
|
||||
if (!archive_->GetFileInfo(path, &info))
|
||||
return v8::False(args->isolate());
|
||||
mate::Dictionary dict(args->isolate(), v8::Object::New(args->isolate()));
|
||||
return v8::False(isolate);
|
||||
mate::Dictionary dict(isolate, v8::Object::New(isolate));
|
||||
dict.Set("size", info.size);
|
||||
dict.Set("offset", info.offset);
|
||||
return dict.GetHandle();
|
||||
}
|
||||
|
||||
// Returns a fake result of fs.stat(path).
|
||||
v8::Handle<v8::Value> Stat(mate::Arguments* args,
|
||||
v8::Handle<v8::Value> Stat(v8::Isolate* isolate,
|
||||
const base::FilePath& path) {
|
||||
asar::Archive::Stats stats;
|
||||
if (!archive_->Stat(path, &stats))
|
||||
return v8::False(args->isolate());
|
||||
mate::Dictionary dict(args->isolate(), v8::Object::New(args->isolate()));
|
||||
return v8::False(isolate);
|
||||
mate::Dictionary dict(isolate, v8::Object::New(isolate));
|
||||
dict.Set("size", stats.size);
|
||||
dict.Set("offset", stats.offset);
|
||||
dict.Set("isFile", stats.is_file);
|
||||
|
@ -56,12 +58,22 @@ class Archive : public mate::Wrappable {
|
|||
return dict.GetHandle();
|
||||
}
|
||||
|
||||
// Returns all files under a directory.
|
||||
v8::Handle<v8::Value> Readdir(v8::Isolate* isolate,
|
||||
const base::FilePath& path) {
|
||||
std::vector<base::FilePath> files;
|
||||
if (!archive_->Readdir(path, &files))
|
||||
return v8::False(isolate);
|
||||
return mate::ConvertToV8(isolate, files);
|
||||
}
|
||||
|
||||
// mate::Wrappable:
|
||||
mate::ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate* isolate) {
|
||||
return mate::ObjectTemplateBuilder(isolate)
|
||||
.SetValue("path", archive_->path())
|
||||
.SetMethod("getFileInfo", &Archive::GetFileInfo)
|
||||
.SetMethod("stat", &Archive::Stat);
|
||||
.SetMethod("stat", &Archive::Stat)
|
||||
.SetMethod("readdir", &Archive::Readdir);
|
||||
}
|
||||
|
||||
private:
|
||||
|
|
|
@ -162,4 +162,25 @@ bool Archive::Stat(const base::FilePath& path, Stats* stats) {
|
|||
return FillFileInfoWithNode(stats, header_size_, node);
|
||||
}
|
||||
|
||||
bool Archive::Readdir(const base::FilePath& path,
|
||||
std::vector<base::FilePath>* list) {
|
||||
if (!header_)
|
||||
return false;
|
||||
|
||||
const base::DictionaryValue* node;
|
||||
if (!GetNodeFromPath(path.AsUTF8Unsafe(), header_.get(), &node))
|
||||
return false;
|
||||
|
||||
const base::DictionaryValue* files;
|
||||
if (!node->GetDictionaryWithoutPathExpansion("files", &files))
|
||||
return false;
|
||||
|
||||
base::DictionaryValue::Iterator iter(*files);
|
||||
while (!iter.IsAtEnd()) {
|
||||
list->push_back(base::FilePath::FromUTF8Unsafe(iter.key()));
|
||||
iter.Advance();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace asar
|
||||
|
|
|
@ -5,6 +5,8 @@
|
|||
#ifndef ATOM_COMMON_ASAR_ARCHIVE_H_
|
||||
#define ATOM_COMMON_ASAR_ARCHIVE_H_
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "base/files/file_path.h"
|
||||
#include "base/memory/ref_counted.h"
|
||||
#include "base/memory/scoped_ptr.h"
|
||||
|
@ -41,6 +43,9 @@ class Archive : public base::RefCounted<Archive> {
|
|||
// Fs.stat(path).
|
||||
bool Stat(const base::FilePath& path, Stats* stats);
|
||||
|
||||
// Fs.readdir(path).
|
||||
bool Readdir(const base::FilePath& path, std::vector<base::FilePath>* files);
|
||||
|
||||
base::FilePath path() const { return path_; }
|
||||
base::DictionaryValue* header() const { return header_.get(); }
|
||||
|
||||
|
|
|
@ -162,6 +162,36 @@ fs.readFileSync = (p, options) ->
|
|||
|
||||
buffer = new Buffer(info.size)
|
||||
fd = fs.openSync archive.path, flag
|
||||
fs.readSync fd, buffer, 0, info.size, info.offset
|
||||
fs.closeSync fd
|
||||
try
|
||||
fs.readSync fd, buffer, 0, info.size, info.offset
|
||||
catch e
|
||||
throw e
|
||||
finally
|
||||
fs.closeSync fd
|
||||
if encoding then buffer.toString encoding else buffer
|
||||
|
||||
readdir = fs.readdir
|
||||
fs.readdir = (p, callback) ->
|
||||
[isAsar, asarPath, filePath] = splitPath p
|
||||
return readdir.apply this, arguments unless isAsar
|
||||
|
||||
archive = asar.createArchive asarPath
|
||||
return callback throw new Error("Invalid package #{asarPath}") unless archive
|
||||
|
||||
files = archive.readdir filePath
|
||||
return callback createNotFoundError(asarPath, filePath) unless files
|
||||
|
||||
callback undefined, files
|
||||
|
||||
readdirSync = fs.readdirSync
|
||||
fs.readdirSync = (p) ->
|
||||
[isAsar, asarPath, filePath] = splitPath p
|
||||
return readdirSync.apply this, arguments unless isAsar
|
||||
|
||||
archive = asar.createArchive asarPath
|
||||
throw new Error("Invalid package #{asarPath}") unless archive
|
||||
|
||||
files = archive.readdir filePath
|
||||
throw createNotFoundError(asarPath, filePath) unless files
|
||||
|
||||
files
|
||||
|
|
Загрузка…
Ссылка в новой задаче