Skip to content

Commit

Permalink
fix: allow paths to asar archives to contain the .asar extension in d…
Browse files Browse the repository at this point in the history
…irectories (#20342) (#20403)
  • Loading branch information
miniak authored and zcbenz committed Oct 2, 2019
1 parent d1ae575 commit 945d025
Show file tree
Hide file tree
Showing 19 changed files with 196 additions and 181 deletions.
16 changes: 16 additions & 0 deletions atom/common/api/atom_api_asar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <vector>

#include "atom/common/asar/archive.h"
#include "atom/common/asar/asar_util.h"
#include "atom/common/native_mate_converters/callback.h"
#include "atom/common/native_mate_converters/file_path_converter.h"
#include "native_mate/arguments.h"
Expand Down Expand Up @@ -128,12 +129,27 @@ void InitAsarSupport(v8::Isolate* isolate, v8::Local<v8::Value> require) {
&asar_init_params, &asar_init_args, nullptr);
}

v8::Local<v8::Value> SplitPath(v8::Isolate* isolate,
const base::FilePath& path) {
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
base::FilePath asar_path, file_path;
if (asar::GetAsarArchivePath(path, &asar_path, &file_path, true)) {
dict.Set("isAsar", true);
dict.Set("asarPath", asar_path);
dict.Set("filePath", file_path);
} else {
dict.Set("isAsar", false);
}
return dict.GetHandle();
}

void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Value> unused,
v8::Local<v8::Context> context,
void* priv) {
mate::Dictionary dict(context->GetIsolate(), exports);
dict.SetMethod("createArchive", &Archive::Create);
dict.SetMethod("splitPath", &SplitPath);
dict.SetMethod("initAsarSupport", &InitAsarSupport);
}

Expand Down
20 changes: 17 additions & 3 deletions atom/common/asar/asar_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "base/lazy_instance.h"
#include "base/stl_util.h"
#include "base/threading/thread_local.h"
#include "base/threading/thread_restrictions.h"

namespace asar {

Expand All @@ -25,6 +26,17 @@ base::LazyInstance<base::ThreadLocalPointer<ArchiveMap>>::Leaky

const base::FilePath::CharType kAsarExtension[] = FILE_PATH_LITERAL(".asar");

std::map<base::FilePath, bool> g_is_directory_cache;

bool IsDirectoryCached(const base::FilePath& path) {
auto it = g_is_directory_cache.find(path);
if (it != g_is_directory_cache.end()) {
return it->second;
}
base::ThreadRestrictions::ScopedAllowIO allow_io;
return g_is_directory_cache[path] = base::DirectoryExists(path);
}

} // namespace

std::shared_ptr<Archive> GetOrCreateAsarArchive(const base::FilePath& path) {
Expand All @@ -47,19 +59,21 @@ void ClearArchives() {

bool GetAsarArchivePath(const base::FilePath& full_path,
base::FilePath* asar_path,
base::FilePath* relative_path) {
base::FilePath* relative_path,
bool allow_root) {
base::FilePath iter = full_path;
while (true) {
base::FilePath dirname = iter.DirName();
if (iter.MatchesExtension(kAsarExtension))
if (iter.MatchesExtension(kAsarExtension) && !IsDirectoryCached(iter))
break;
else if (iter == dirname)
return false;
iter = dirname;
}

base::FilePath tail;
if (!iter.AppendRelativePath(full_path, &tail))
if (!((allow_root && iter == full_path) ||
iter.AppendRelativePath(full_path, &tail)))
return false;

*asar_path = iter;
Expand Down
3 changes: 2 additions & 1 deletion atom/common/asar/asar_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ void ClearArchives();
// Separates the path to Archive out.
bool GetAsarArchivePath(const base::FilePath& full_path,
base::FilePath* asar_path,
base::FilePath* relative_path);
base::FilePath* relative_path,
bool allow_root = false);

// Same with base::ReadFileToString but supports asar Archive.
bool ReadFileToString(const base::FilePath& path, std::string* contents);
Expand Down
19 changes: 1 addition & 18 deletions lib/common/asar.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
return newArchive
}

const ASAR_EXTENSION = '.asar'

// Separate asar package's path from full path.
const splitPath = archivePathOrBuffer => {
// Shortcut for disabled asar.
Expand All @@ -54,22 +52,7 @@
}
if (typeof archivePath !== 'string') return { isAsar: false }

if (archivePath.endsWith(ASAR_EXTENSION)) {
return { isAsar: true, asarPath: archivePath, filePath: '' }
}

archivePath = path.normalize(archivePath)
const index = archivePath.lastIndexOf(`${ASAR_EXTENSION}${path.sep}`)
if (index === -1) return { isAsar: false }

// E.g. for "//some/path/to/archive.asar/then/internal.file"...
return {
isAsar: true,
// "//some/path/to/archive.asar"
asarPath: archivePath.substr(0, index + ASAR_EXTENSION.length),
// "then/internal.file" (with a path separator excluded)
filePath: archivePath.substr(index + ASAR_EXTENSION.length + 1)
}
return asar.splitPath(path.normalize(archivePath))
}

// Convert asar archive's Stats object to fs's Stats object.
Expand Down
4 changes: 2 additions & 2 deletions spec/api-protocol-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ describe('protocol module', () => {
})

describe('protocol.registerFileProtocol', () => {
const filePath = path.join(__dirname, 'fixtures', 'asar', 'a.asar', 'file1')
const filePath = path.join(__dirname, 'fixtures', 'test.asar', 'a.asar', 'file1')
const fileContent = require('fs').readFileSync(filePath)
const normalPath = path.join(__dirname, 'fixtures', 'pages', 'a.html')
const normalContent = require('fs').readFileSync(normalPath)
Expand Down Expand Up @@ -394,7 +394,7 @@ describe('protocol module', () => {
})

it('fails when sending unexist-file', (done) => {
const fakeFilePath = path.join(__dirname, 'fixtures', 'asar', 'a.asar', 'not-exist')
const fakeFilePath = path.join(__dirname, 'fixtures', 'test.asar', 'a.asar', 'not-exist')
const handler = (request, callback) => callback(fakeFilePath)
protocol.registerFileProtocol(protocolName, handler, (error) => {
if (error) return done(error)
Expand Down

0 comments on commit 945d025

Please sign in to comment.