Add asar.stat method.
This commit is contained in:
parent
0d09143a77
commit
8199ad2ae6
3 changed files with 67 additions and 10 deletions
|
@ -29,6 +29,7 @@ class Archive : public mate::Wrappable {
|
||||||
explicit Archive(scoped_refptr<asar::Archive> archive) : archive_(archive) {}
|
explicit Archive(scoped_refptr<asar::Archive> archive) : archive_(archive) {}
|
||||||
virtual ~Archive() {}
|
virtual ~Archive() {}
|
||||||
|
|
||||||
|
// Reads the offset and size of file.
|
||||||
v8::Handle<v8::Value> GetFileInfo(mate::Arguments* args,
|
v8::Handle<v8::Value> GetFileInfo(mate::Arguments* args,
|
||||||
const base::FilePath& path) {
|
const base::FilePath& path) {
|
||||||
asar::Archive::FileInfo info;
|
asar::Archive::FileInfo info;
|
||||||
|
@ -40,11 +41,27 @@ class Archive : public mate::Wrappable {
|
||||||
return dict.GetHandle();
|
return dict.GetHandle();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns a fake result of fs.stat(path).
|
||||||
|
v8::Handle<v8::Value> Stat(mate::Arguments* args,
|
||||||
|
const base::FilePath& path) {
|
||||||
|
asar::Archive::Stats stats;
|
||||||
|
if (!archive_->Stat(path, &stats))
|
||||||
|
return args->ThrowError("Can not find file");
|
||||||
|
mate::Dictionary dict(args->isolate(), v8::Object::New(args->isolate()));
|
||||||
|
dict.Set("size", stats.size);
|
||||||
|
dict.Set("offset", stats.offset);
|
||||||
|
dict.Set("isFile", stats.is_file);
|
||||||
|
dict.Set("isDirectory", stats.is_directory);
|
||||||
|
dict.Set("isLink", stats.is_link);
|
||||||
|
return dict.GetHandle();
|
||||||
|
}
|
||||||
|
|
||||||
// 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -42,6 +42,24 @@ bool GetNodeFromPath(std::string path,
|
||||||
return GetChildNode(path, root, out);
|
return GetChildNode(path, root, out);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool FillFileInfoWithNode(Archive::FileInfo* info,
|
||||||
|
uint32 header_size,
|
||||||
|
const base::DictionaryValue* node) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
Archive::Archive(const base::FilePath& path)
|
Archive::Archive(const base::FilePath& path)
|
||||||
|
@ -113,19 +131,30 @@ bool Archive::GetFileInfo(const base::FilePath& path, FileInfo* info) {
|
||||||
if (node->GetString("link", &link))
|
if (node->GetString("link", &link))
|
||||||
return GetFileInfo(base::FilePath::FromUTF8Unsafe(link), info);
|
return GetFileInfo(base::FilePath::FromUTF8Unsafe(link), info);
|
||||||
|
|
||||||
std::string offset;
|
return FillFileInfoWithNode(info, header_size_, node);
|
||||||
if (!node->GetString("offset", &offset))
|
}
|
||||||
return false;
|
|
||||||
if (!base::StringToUint64(offset, &info->offset))
|
bool Archive::Stat(const base::FilePath& path, Stats* stats) {
|
||||||
|
if (!header_)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
int size;
|
const base::DictionaryValue* node;
|
||||||
if (!node->GetInteger("size", &size))
|
if (!GetNodeFromPath(path.AsUTF8Unsafe(), header_.get(), &node))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
info->offset += header_size_;
|
if (node->HasKey("link")) {
|
||||||
info->size = static_cast<uint32>(size);
|
stats->is_file = false;
|
||||||
|
stats->is_link = true;
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (node->HasKey("files")) {
|
||||||
|
stats->is_file = false;
|
||||||
|
stats->is_directory = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return FillFileInfoWithNode(stats, header_size_, node);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace asar
|
} // namespace asar
|
||||||
|
|
|
@ -18,10 +18,18 @@ namespace asar {
|
||||||
class Archive : public base::RefCounted<Archive> {
|
class Archive : public base::RefCounted<Archive> {
|
||||||
public:
|
public:
|
||||||
struct FileInfo {
|
struct FileInfo {
|
||||||
|
FileInfo() : size(0), offset(0) {}
|
||||||
uint32 size;
|
uint32 size;
|
||||||
uint64 offset;
|
uint64 offset;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct Stats : public FileInfo {
|
||||||
|
Stats() : is_file(true), is_directory(false), is_link(false) {}
|
||||||
|
bool is_file;
|
||||||
|
bool is_directory;
|
||||||
|
bool is_link;
|
||||||
|
};
|
||||||
|
|
||||||
explicit Archive(const base::FilePath& path);
|
explicit Archive(const base::FilePath& path);
|
||||||
|
|
||||||
// Read and parse the header.
|
// Read and parse the header.
|
||||||
|
@ -30,6 +38,9 @@ class Archive : public base::RefCounted<Archive> {
|
||||||
// Get the info of a file.
|
// Get the info of a file.
|
||||||
bool GetFileInfo(const base::FilePath& path, FileInfo* info);
|
bool GetFileInfo(const base::FilePath& path, FileInfo* info);
|
||||||
|
|
||||||
|
// Fs.stat(path).
|
||||||
|
bool Stat(const base::FilePath& path, Stats* stats);
|
||||||
|
|
||||||
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(); }
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue