2015-02-12 09:23:49 +00:00
|
|
|
// Copyright (c) 2015 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/common/asar/asar_util.h"
|
2015-02-12 09:23:49 +00:00
|
|
|
|
|
|
|
#include <map>
|
2015-02-12 11:50:28 +00:00
|
|
|
#include <string>
|
2021-06-04 01:49:08 +00:00
|
|
|
#include <utility>
|
2015-02-12 09:23:49 +00:00
|
|
|
|
|
|
|
#include "base/files/file_path.h"
|
2015-02-12 11:34:21 +00:00
|
|
|
#include "base/files/file_util.h"
|
2015-02-12 09:23:49 +00:00
|
|
|
#include "base/lazy_instance.h"
|
2021-06-04 01:49:08 +00:00
|
|
|
#include "base/no_destructor.h"
|
2015-02-12 09:23:49 +00:00
|
|
|
#include "base/stl_util.h"
|
2021-06-04 01:49:08 +00:00
|
|
|
#include "base/synchronization/lock.h"
|
2017-03-10 08:33:27 +00:00
|
|
|
#include "base/threading/thread_local.h"
|
2019-10-02 04:33:07 +00:00
|
|
|
#include "base/threading/thread_restrictions.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/common/asar/archive.h"
|
2015-02-12 09:23:49 +00:00
|
|
|
|
|
|
|
namespace asar {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2015-02-12 11:50:28 +00:00
|
|
|
typedef std::map<base::FilePath, std::shared_ptr<Archive>> ArchiveMap;
|
2015-02-12 09:23:49 +00:00
|
|
|
|
2015-02-12 09:31:14 +00:00
|
|
|
const base::FilePath::CharType kAsarExtension[] = FILE_PATH_LITERAL(".asar");
|
|
|
|
|
2019-10-02 04:33:07 +00:00
|
|
|
bool IsDirectoryCached(const base::FilePath& path) {
|
2021-06-04 01:49:08 +00:00
|
|
|
static base::NoDestructor<std::map<base::FilePath, bool>>
|
|
|
|
s_is_directory_cache;
|
|
|
|
static base::NoDestructor<base::Lock> lock;
|
|
|
|
|
|
|
|
base::AutoLock auto_lock(*lock);
|
|
|
|
auto& is_directory_cache = *s_is_directory_cache;
|
|
|
|
|
|
|
|
auto it = is_directory_cache.find(path);
|
|
|
|
if (it != is_directory_cache.end()) {
|
2019-10-02 04:33:07 +00:00
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
base::ThreadRestrictions::ScopedAllowIO allow_io;
|
2021-06-04 01:49:08 +00:00
|
|
|
return is_directory_cache[path] = base::DirectoryExists(path);
|
2019-10-02 04:33:07 +00:00
|
|
|
}
|
|
|
|
|
2015-02-12 09:23:49 +00:00
|
|
|
} // namespace
|
|
|
|
|
2021-06-04 01:49:08 +00:00
|
|
|
ArchiveMap& GetArchiveCache() {
|
|
|
|
static base::NoDestructor<ArchiveMap> s_archive_map;
|
|
|
|
return *s_archive_map;
|
|
|
|
}
|
|
|
|
|
|
|
|
base::Lock& GetArchiveCacheLock() {
|
|
|
|
static base::NoDestructor<base::Lock> lock;
|
|
|
|
return *lock;
|
|
|
|
}
|
|
|
|
|
2015-02-12 09:23:49 +00:00
|
|
|
std::shared_ptr<Archive> GetOrCreateAsarArchive(const base::FilePath& path) {
|
2021-06-04 01:49:08 +00:00
|
|
|
base::AutoLock auto_lock(GetArchiveCacheLock());
|
|
|
|
ArchiveMap& map = GetArchiveCache();
|
2019-08-28 14:39:21 +00:00
|
|
|
|
|
|
|
// if we have it, return it
|
|
|
|
const auto lower = map.lower_bound(path);
|
|
|
|
if (lower != std::end(map) && !map.key_comp()(path, lower->first))
|
|
|
|
return lower->second;
|
|
|
|
|
|
|
|
// if we can create it, return it
|
|
|
|
auto archive = std::make_shared<Archive>(path);
|
|
|
|
if (archive->Init()) {
|
|
|
|
base::TryEmplace(map, lower, path, archive);
|
|
|
|
return archive;
|
2015-02-12 09:23:49 +00:00
|
|
|
}
|
2019-08-28 14:39:21 +00:00
|
|
|
|
|
|
|
// didn't have it, couldn't create it
|
|
|
|
return nullptr;
|
2015-02-12 09:23:49 +00:00
|
|
|
}
|
|
|
|
|
2017-03-10 08:33:27 +00:00
|
|
|
void ClearArchives() {
|
2021-06-04 01:49:08 +00:00
|
|
|
base::AutoLock auto_lock(GetArchiveCacheLock());
|
|
|
|
ArchiveMap& map = GetArchiveCache();
|
|
|
|
|
|
|
|
map.clear();
|
2017-03-10 08:33:27 +00:00
|
|
|
}
|
|
|
|
|
2015-02-12 09:31:14 +00:00
|
|
|
bool GetAsarArchivePath(const base::FilePath& full_path,
|
|
|
|
base::FilePath* asar_path,
|
2019-10-02 04:33:07 +00:00
|
|
|
base::FilePath* relative_path,
|
|
|
|
bool allow_root) {
|
2015-02-12 09:31:14 +00:00
|
|
|
base::FilePath iter = full_path;
|
|
|
|
while (true) {
|
|
|
|
base::FilePath dirname = iter.DirName();
|
2019-10-02 04:33:07 +00:00
|
|
|
if (iter.MatchesExtension(kAsarExtension) && !IsDirectoryCached(iter))
|
2015-02-12 09:31:14 +00:00
|
|
|
break;
|
|
|
|
else if (iter == dirname)
|
|
|
|
return false;
|
|
|
|
iter = dirname;
|
|
|
|
}
|
|
|
|
|
|
|
|
base::FilePath tail;
|
2019-10-02 04:33:07 +00:00
|
|
|
if (!((allow_root && iter == full_path) ||
|
|
|
|
iter.AppendRelativePath(full_path, &tail)))
|
2015-02-12 09:31:14 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
*asar_path = iter;
|
|
|
|
*relative_path = tail;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-02-12 11:34:21 +00:00
|
|
|
bool ReadFileToString(const base::FilePath& path, std::string* contents) {
|
|
|
|
base::FilePath asar_path, relative_path;
|
|
|
|
if (!GetAsarArchivePath(path, &asar_path, &relative_path))
|
|
|
|
return base::ReadFileToString(path, contents);
|
|
|
|
|
|
|
|
std::shared_ptr<Archive> archive = GetOrCreateAsarArchive(asar_path);
|
|
|
|
if (!archive)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Archive::FileInfo info;
|
|
|
|
if (!archive->GetFileInfo(relative_path, &info))
|
|
|
|
return false;
|
|
|
|
|
2015-03-20 12:34:58 +00:00
|
|
|
if (info.unpacked) {
|
|
|
|
base::FilePath real_path;
|
|
|
|
// For unpacked file it will return the real path instead of doing the copy.
|
|
|
|
archive->CopyFileOut(relative_path, &real_path);
|
|
|
|
return base::ReadFileToString(real_path, contents);
|
|
|
|
}
|
|
|
|
|
2015-02-12 11:34:21 +00:00
|
|
|
base::File src(asar_path, base::File::FLAG_OPEN | base::File::FLAG_READ);
|
|
|
|
if (!src.IsValid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
contents->resize(info.size);
|
2018-04-18 01:55:30 +00:00
|
|
|
return static_cast<int>(info.size) ==
|
|
|
|
src.Read(info.offset, const_cast<char*>(contents->data()),
|
|
|
|
contents->size());
|
2015-02-12 11:34:21 +00:00
|
|
|
}
|
|
|
|
|
2015-02-12 09:23:49 +00:00
|
|
|
} // namespace asar
|