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>
|
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"
|
|
|
|
#include "base/stl_util.h"
|
2017-03-10 08:33:27 +00:00
|
|
|
#include "base/threading/thread_local.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 09:31:14 +00:00
|
|
|
// The global instance of ArchiveMap, will be destroyed on exit.
|
2015-02-12 11:50:28 +00:00
|
|
|
typedef std::map<base::FilePath, std::shared_ptr<Archive>> ArchiveMap;
|
2017-03-10 08:33:27 +00:00
|
|
|
base::LazyInstance<base::ThreadLocalPointer<ArchiveMap>>::Leaky
|
|
|
|
g_archive_map_tls = LAZY_INSTANCE_INITIALIZER;
|
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");
|
|
|
|
|
2015-02-12 09:23:49 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
std::shared_ptr<Archive> GetOrCreateAsarArchive(const base::FilePath& path) {
|
2017-03-10 08:33:27 +00:00
|
|
|
if (!g_archive_map_tls.Pointer()->Get())
|
|
|
|
g_archive_map_tls.Pointer()->Set(new ArchiveMap);
|
|
|
|
ArchiveMap& archive_map = *g_archive_map_tls.Pointer()->Get();
|
2019-07-03 01:22:09 +00:00
|
|
|
if (!base::Contains(archive_map, path)) {
|
2015-02-12 09:23:49 +00:00
|
|
|
std::shared_ptr<Archive> archive(new Archive(path));
|
|
|
|
if (!archive->Init())
|
|
|
|
return nullptr;
|
|
|
|
archive_map[path] = archive;
|
|
|
|
}
|
|
|
|
return archive_map[path];
|
|
|
|
}
|
|
|
|
|
2017-03-10 08:33:27 +00:00
|
|
|
void ClearArchives() {
|
|
|
|
if (g_archive_map_tls.Pointer()->Get())
|
|
|
|
delete g_archive_map_tls.Pointer()->Get();
|
|
|
|
}
|
|
|
|
|
2015-02-12 09:31:14 +00:00
|
|
|
bool GetAsarArchivePath(const base::FilePath& full_path,
|
|
|
|
base::FilePath* asar_path,
|
|
|
|
base::FilePath* relative_path) {
|
|
|
|
base::FilePath iter = full_path;
|
|
|
|
while (true) {
|
|
|
|
base::FilePath dirname = iter.DirName();
|
|
|
|
if (iter.MatchesExtension(kAsarExtension))
|
|
|
|
break;
|
|
|
|
else if (iter == dirname)
|
|
|
|
return false;
|
|
|
|
iter = dirname;
|
|
|
|
}
|
|
|
|
|
|
|
|
base::FilePath tail;
|
|
|
|
if (!iter.AppendRelativePath(full_path, &tail))
|
|
|
|
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
|