fix: allow paths to asar archives to contain the .asar extension in directories (#20342)

This commit is contained in:
Milan Burda 2019-10-02 06:33:07 +02:00 committed by Cheng Zhao
parent 4ac4b34ae9
commit bf978e09e4
19 changed files with 204 additions and 183 deletions

View file

@ -12,6 +12,7 @@
#include "base/lazy_instance.h"
#include "base/stl_util.h"
#include "base/threading/thread_local.h"
#include "base/threading/thread_restrictions.h"
#include "shell/common/asar/archive.h"
namespace asar {
@ -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) {
@ -55,11 +67,12 @@ 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;
@ -67,7 +80,8 @@ bool GetAsarArchivePath(const base::FilePath& full_path,
}
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;