Make fs.readdir support asar package.
This commit is contained in:
parent
9f9d209e3d
commit
0cab034dab
4 changed files with 80 additions and 12 deletions
|
@ -2,6 +2,8 @@
|
||||||
// Use of this source code is governed by the MIT license that can be
|
// Use of this source code is governed by the MIT license that can be
|
||||||
// found in the LICENSE file.
|
// found in the LICENSE file.
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "atom/common/asar/archive.h"
|
#include "atom/common/asar/archive.h"
|
||||||
#include "atom/common/asar/archive_factory.h"
|
#include "atom/common/asar/archive_factory.h"
|
||||||
#include "atom/common/native_mate_converters/file_path_converter.h"
|
#include "atom/common/native_mate_converters/file_path_converter.h"
|
||||||
|
@ -16,13 +18,13 @@ namespace {
|
||||||
|
|
||||||
class Archive : public mate::Wrappable {
|
class Archive : public mate::Wrappable {
|
||||||
public:
|
public:
|
||||||
static v8::Handle<v8::Value> Create(mate::Arguments* args,
|
static v8::Handle<v8::Value> Create(v8::Isolate* isolate,
|
||||||
const base::FilePath& path) {
|
const base::FilePath& path) {
|
||||||
static asar::ArchiveFactory archive_factory;
|
static asar::ArchiveFactory archive_factory;
|
||||||
scoped_refptr<asar::Archive> archive = archive_factory.GetOrCreate(path);
|
scoped_refptr<asar::Archive> archive = archive_factory.GetOrCreate(path);
|
||||||
if (!archive)
|
if (!archive)
|
||||||
return v8::False(args->isolate());
|
return v8::False(isolate);
|
||||||
return (new Archive(archive))->GetWrapper(args->isolate());
|
return (new Archive(archive))->GetWrapper(isolate);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
@ -30,24 +32,24 @@ class Archive : public mate::Wrappable {
|
||||||
virtual ~Archive() {}
|
virtual ~Archive() {}
|
||||||
|
|
||||||
// Reads the offset and size of file.
|
// 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) {
|
const base::FilePath& path) {
|
||||||
asar::Archive::FileInfo info;
|
asar::Archive::FileInfo info;
|
||||||
if (!archive_->GetFileInfo(path, &info))
|
if (!archive_->GetFileInfo(path, &info))
|
||||||
return v8::False(args->isolate());
|
return v8::False(isolate);
|
||||||
mate::Dictionary dict(args->isolate(), v8::Object::New(args->isolate()));
|
mate::Dictionary dict(isolate, v8::Object::New(isolate));
|
||||||
dict.Set("size", info.size);
|
dict.Set("size", info.size);
|
||||||
dict.Set("offset", info.offset);
|
dict.Set("offset", info.offset);
|
||||||
return dict.GetHandle();
|
return dict.GetHandle();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns a fake result of fs.stat(path).
|
// 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) {
|
const base::FilePath& path) {
|
||||||
asar::Archive::Stats stats;
|
asar::Archive::Stats stats;
|
||||||
if (!archive_->Stat(path, &stats))
|
if (!archive_->Stat(path, &stats))
|
||||||
return v8::False(args->isolate());
|
return v8::False(isolate);
|
||||||
mate::Dictionary dict(args->isolate(), v8::Object::New(args->isolate()));
|
mate::Dictionary dict(isolate, v8::Object::New(isolate));
|
||||||
dict.Set("size", stats.size);
|
dict.Set("size", stats.size);
|
||||||
dict.Set("offset", stats.offset);
|
dict.Set("offset", stats.offset);
|
||||||
dict.Set("isFile", stats.is_file);
|
dict.Set("isFile", stats.is_file);
|
||||||
|
@ -56,12 +58,22 @@ class Archive : public mate::Wrappable {
|
||||||
return dict.GetHandle();
|
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::Wrappable:
|
||||||
mate::ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate* isolate) {
|
mate::ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate* isolate) {
|
||||||
return mate::ObjectTemplateBuilder(isolate)
|
return mate::ObjectTemplateBuilder(isolate)
|
||||||
.SetValue("path", archive_->path())
|
.SetValue("path", archive_->path())
|
||||||
.SetMethod("getFileInfo", &Archive::GetFileInfo)
|
.SetMethod("getFileInfo", &Archive::GetFileInfo)
|
||||||
.SetMethod("stat", &Archive::Stat);
|
.SetMethod("stat", &Archive::Stat)
|
||||||
|
.SetMethod("readdir", &Archive::Readdir);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -162,4 +162,25 @@ bool Archive::Stat(const base::FilePath& path, Stats* stats) {
|
||||||
return FillFileInfoWithNode(stats, header_size_, node);
|
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
|
} // namespace asar
|
||||||
|
|
|
@ -5,6 +5,8 @@
|
||||||
#ifndef ATOM_COMMON_ASAR_ARCHIVE_H_
|
#ifndef ATOM_COMMON_ASAR_ARCHIVE_H_
|
||||||
#define ATOM_COMMON_ASAR_ARCHIVE_H_
|
#define ATOM_COMMON_ASAR_ARCHIVE_H_
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "base/files/file_path.h"
|
#include "base/files/file_path.h"
|
||||||
#include "base/memory/ref_counted.h"
|
#include "base/memory/ref_counted.h"
|
||||||
#include "base/memory/scoped_ptr.h"
|
#include "base/memory/scoped_ptr.h"
|
||||||
|
@ -41,6 +43,9 @@ class Archive : public base::RefCounted<Archive> {
|
||||||
// Fs.stat(path).
|
// Fs.stat(path).
|
||||||
bool Stat(const base::FilePath& path, Stats* stats);
|
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::FilePath path() const { return path_; }
|
||||||
base::DictionaryValue* header() const { return header_.get(); }
|
base::DictionaryValue* header() const { return header_.get(); }
|
||||||
|
|
||||||
|
|
|
@ -162,6 +162,36 @@ fs.readFileSync = (p, options) ->
|
||||||
|
|
||||||
buffer = new Buffer(info.size)
|
buffer = new Buffer(info.size)
|
||||||
fd = fs.openSync archive.path, flag
|
fd = fs.openSync archive.path, flag
|
||||||
fs.readSync fd, buffer, 0, info.size, info.offset
|
try
|
||||||
fs.closeSync fd
|
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
|
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
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue