asar: Add support in fs.realpathSync
This commit is contained in:
parent
d77bf0440c
commit
885ac53a48
4 changed files with 48 additions and 4 deletions
|
@ -65,6 +65,15 @@ class Archive : public mate::Wrappable {
|
||||||
return mate::ConvertToV8(isolate, files);
|
return mate::ConvertToV8(isolate, files);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns the path of file with symbol link resolved.
|
||||||
|
v8::Handle<v8::Value> Realpath(v8::Isolate* isolate,
|
||||||
|
const base::FilePath& path) {
|
||||||
|
base::FilePath realpath;
|
||||||
|
if (!archive_ || !archive_->Realpath(path, &realpath))
|
||||||
|
return v8::False(isolate);
|
||||||
|
return mate::ConvertToV8(isolate, realpath);
|
||||||
|
}
|
||||||
|
|
||||||
// Copy the file out into a temporary file and returns the new path.
|
// Copy the file out into a temporary file and returns the new path.
|
||||||
v8::Handle<v8::Value> CopyFileOut(v8::Isolate* isolate,
|
v8::Handle<v8::Value> CopyFileOut(v8::Isolate* isolate,
|
||||||
const base::FilePath& path) {
|
const base::FilePath& path) {
|
||||||
|
@ -86,6 +95,7 @@ class Archive : public mate::Wrappable {
|
||||||
.SetMethod("getFileInfo", &Archive::GetFileInfo)
|
.SetMethod("getFileInfo", &Archive::GetFileInfo)
|
||||||
.SetMethod("stat", &Archive::Stat)
|
.SetMethod("stat", &Archive::Stat)
|
||||||
.SetMethod("readdir", &Archive::Readdir)
|
.SetMethod("readdir", &Archive::Readdir)
|
||||||
|
.SetMethod("realpath", &Archive::Realpath)
|
||||||
.SetMethod("copyFileOut", &Archive::CopyFileOut)
|
.SetMethod("copyFileOut", &Archive::CopyFileOut)
|
||||||
.SetMethod("destroy", &Archive::Destroy);
|
.SetMethod("destroy", &Archive::Destroy);
|
||||||
}
|
}
|
||||||
|
|
|
@ -206,6 +206,24 @@ bool Archive::Readdir(const base::FilePath& path,
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Archive::Realpath(const base::FilePath& path, base::FilePath* realpath) {
|
||||||
|
if (!header_)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
const base::DictionaryValue* node;
|
||||||
|
if (!GetNodeFromPath(path.AsUTF8Unsafe(), header_.get(), &node))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
std::string link;
|
||||||
|
if (node->GetString("link", &link)) {
|
||||||
|
*realpath = base::FilePath::FromUTF8Unsafe(link);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
*realpath = path;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool Archive::CopyFileOut(const base::FilePath& path, base::FilePath* out) {
|
bool Archive::CopyFileOut(const base::FilePath& path, base::FilePath* out) {
|
||||||
if (external_files_.contains(path)) {
|
if (external_files_.contains(path)) {
|
||||||
*out = external_files_.get(path)->path();
|
*out = external_files_.get(path)->path();
|
||||||
|
|
|
@ -51,6 +51,9 @@ class Archive {
|
||||||
// Fs.readdir(path).
|
// Fs.readdir(path).
|
||||||
bool Readdir(const base::FilePath& path, std::vector<base::FilePath>* files);
|
bool Readdir(const base::FilePath& path, std::vector<base::FilePath>* files);
|
||||||
|
|
||||||
|
// Fs.realpath(path).
|
||||||
|
bool Realpath(const base::FilePath& path, base::FilePath* realpath);
|
||||||
|
|
||||||
// Copy the file into a temporary file, and return the new path.
|
// Copy the file into a temporary file, and return the new path.
|
||||||
bool CopyFileOut(const base::FilePath& path, base::FilePath* out);
|
bool CopyFileOut(const base::FilePath& path, base::FilePath* out);
|
||||||
|
|
||||||
|
|
|
@ -75,7 +75,7 @@ fs.lstat = (p, callback) ->
|
||||||
return lstat p, callback unless isAsar
|
return lstat p, callback unless isAsar
|
||||||
|
|
||||||
archive = getOrCreateArchive asarPath
|
archive = getOrCreateArchive asarPath
|
||||||
return callback throw new Error("Invalid package #{asarPath}") unless archive
|
return callback new Error("Invalid package #{asarPath}") unless archive
|
||||||
|
|
||||||
stats = getOrCreateArchive(asarPath).stat filePath
|
stats = getOrCreateArchive(asarPath).stat filePath
|
||||||
return callback createNotFoundError(asarPath, filePath) unless stats
|
return callback createNotFoundError(asarPath, filePath) unless stats
|
||||||
|
@ -109,13 +109,26 @@ fs.statSyncNoException = (p) ->
|
||||||
return false unless stats
|
return false unless stats
|
||||||
asarStatsToFsStats stats
|
asarStatsToFsStats stats
|
||||||
|
|
||||||
|
realpathSync = fs.realpathSync
|
||||||
|
fs.realpathSync = (p) ->
|
||||||
|
[isAsar, asarPath, filePath] = splitPath p
|
||||||
|
return realpathSync.apply this, arguments unless isAsar
|
||||||
|
|
||||||
|
archive = getOrCreateArchive asarPath
|
||||||
|
throw new Error("Invalid package #{asarPath}") unless archive
|
||||||
|
|
||||||
|
real = archive.realpath filePath
|
||||||
|
throw createNotFoundError(asarPath, filePath) unless real
|
||||||
|
|
||||||
|
path.join realpathSync(asarPath), real
|
||||||
|
|
||||||
exists = fs.exists
|
exists = fs.exists
|
||||||
fs.exists = (p, callback) ->
|
fs.exists = (p, callback) ->
|
||||||
[isAsar, asarPath, filePath] = splitPath p
|
[isAsar, asarPath, filePath] = splitPath p
|
||||||
return exists p, callback unless isAsar
|
return exists p, callback unless isAsar
|
||||||
|
|
||||||
archive = getOrCreateArchive asarPath
|
archive = getOrCreateArchive asarPath
|
||||||
return callback throw new Error("Invalid package #{asarPath}") unless archive
|
return callback new Error("Invalid package #{asarPath}") unless archive
|
||||||
|
|
||||||
process.nextTick -> callback archive.stat(filePath) isnt false
|
process.nextTick -> callback archive.stat(filePath) isnt false
|
||||||
|
|
||||||
|
@ -140,7 +153,7 @@ fs.readFile = (p, options, callback) ->
|
||||||
options = undefined
|
options = undefined
|
||||||
|
|
||||||
archive = getOrCreateArchive asarPath
|
archive = getOrCreateArchive asarPath
|
||||||
return callback throw new Error("Invalid package #{asarPath}") unless archive
|
return callback new Error("Invalid package #{asarPath}") unless archive
|
||||||
|
|
||||||
info = archive.getFileInfo filePath
|
info = archive.getFileInfo filePath
|
||||||
return callback createNotFoundError(asarPath, filePath) unless info
|
return callback createNotFoundError(asarPath, filePath) unless info
|
||||||
|
@ -200,7 +213,7 @@ fs.readdir = (p, callback) ->
|
||||||
return readdir.apply this, arguments unless isAsar
|
return readdir.apply this, arguments unless isAsar
|
||||||
|
|
||||||
archive = getOrCreateArchive asarPath
|
archive = getOrCreateArchive asarPath
|
||||||
return callback throw new Error("Invalid package #{asarPath}") unless archive
|
return callback new Error("Invalid package #{asarPath}") unless archive
|
||||||
|
|
||||||
files = archive.readdir filePath
|
files = archive.readdir filePath
|
||||||
return callback createNotFoundError(asarPath, filePath) unless files
|
return callback createNotFoundError(asarPath, filePath) unless files
|
||||||
|
|
Loading…
Reference in a new issue