2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2014 GitHub, Inc.
|
2014-09-23 11:14:30 +00:00
|
|
|
// 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/archive.h"
|
2014-09-23 11:14:30 +00:00
|
|
|
|
|
|
|
#include <string>
|
2024-01-11 01:00:37 +00:00
|
|
|
#include <string_view>
|
2018-09-13 00:25:56 +00:00
|
|
|
#include <utility>
|
2014-09-23 11:14:30 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2021-06-04 01:49:08 +00:00
|
|
|
#include "base/check.h"
|
2024-04-15 22:10:32 +00:00
|
|
|
#include "base/containers/span.h"
|
2014-09-23 11:14:30 +00:00
|
|
|
#include "base/files/file.h"
|
2015-11-27 14:06:37 +00:00
|
|
|
#include "base/files/file_util.h"
|
2016-08-26 22:30:02 +00:00
|
|
|
#include "base/json/json_reader.h"
|
2014-09-23 11:14:30 +00:00
|
|
|
#include "base/logging.h"
|
|
|
|
#include "base/pickle.h"
|
2015-05-22 07:24:34 +00:00
|
|
|
#include "base/values.h"
|
2021-09-09 21:49:01 +00:00
|
|
|
#include "electron/fuses.h"
|
|
|
|
#include "shell/common/asar/asar_util.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/common/asar/scoped_temporary_file.h"
|
2022-11-17 19:59:23 +00:00
|
|
|
#include "shell/common/thread_restrictions.h"
|
2014-09-23 11:14:30 +00:00
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2017-08-30 08:28:45 +00:00
|
|
|
#include <io.h>
|
2016-05-19 07:38:37 +00:00
|
|
|
#endif
|
|
|
|
|
2014-09-23 11:14:30 +00:00
|
|
|
namespace asar {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2014-09-30 12:12:18 +00:00
|
|
|
const char kSeparators[] = "\\/";
|
|
|
|
#else
|
|
|
|
const char kSeparators[] = "/";
|
|
|
|
#endif
|
|
|
|
|
2022-07-05 16:35:38 +00:00
|
|
|
const base::Value::Dict* GetNodeFromPath(std::string path,
|
|
|
|
const base::Value::Dict& root);
|
2014-09-28 14:46:29 +00:00
|
|
|
|
2014-09-29 07:23:28 +00:00
|
|
|
// Gets the "files" from "dir".
|
2022-07-05 16:35:38 +00:00
|
|
|
const base::Value::Dict* GetFilesNode(const base::Value::Dict& root,
|
|
|
|
const base::Value::Dict& dir) {
|
2014-09-28 14:46:29 +00:00
|
|
|
// Test for symbol linked directory.
|
2022-07-05 16:35:38 +00:00
|
|
|
const std::string* link = dir.FindString("link");
|
2021-08-24 00:52:17 +00:00
|
|
|
if (link != nullptr) {
|
2022-07-05 16:35:38 +00:00
|
|
|
const base::Value::Dict* linked_node = GetNodeFromPath(*link, root);
|
|
|
|
if (!linked_node)
|
|
|
|
return nullptr;
|
|
|
|
return linked_node->FindDict("files");
|
2014-09-28 14:46:29 +00:00
|
|
|
}
|
|
|
|
|
2022-07-05 16:35:38 +00:00
|
|
|
return dir.FindDict("files");
|
2014-09-29 07:23:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Gets sub-file "name" from "dir".
|
2022-07-05 16:35:38 +00:00
|
|
|
const base::Value::Dict* GetChildNode(const base::Value::Dict& root,
|
|
|
|
const std::string& name,
|
|
|
|
const base::Value::Dict& dir) {
|
|
|
|
if (name.empty())
|
|
|
|
return &root;
|
|
|
|
|
|
|
|
const base::Value::Dict* files = GetFilesNode(root, dir);
|
|
|
|
return files ? files->FindDict(name) : nullptr;
|
2014-09-23 11:14:30 +00:00
|
|
|
}
|
|
|
|
|
2014-09-28 14:46:29 +00:00
|
|
|
// Gets the node of "path" from "root".
|
2022-07-05 16:35:38 +00:00
|
|
|
const base::Value::Dict* GetNodeFromPath(std::string path,
|
|
|
|
const base::Value::Dict& root) {
|
|
|
|
if (path.empty())
|
|
|
|
return &root;
|
2014-09-24 05:42:04 +00:00
|
|
|
|
2022-07-05 16:35:38 +00:00
|
|
|
const base::Value::Dict* dir = &root;
|
2014-09-30 12:12:18 +00:00
|
|
|
for (size_t delimiter_position = path.find_first_of(kSeparators);
|
2014-09-23 11:14:30 +00:00
|
|
|
delimiter_position != std::string::npos;
|
2014-09-30 12:12:18 +00:00
|
|
|
delimiter_position = path.find_first_of(kSeparators)) {
|
2022-07-05 16:35:38 +00:00
|
|
|
const base::Value::Dict* child =
|
|
|
|
GetChildNode(root, path.substr(0, delimiter_position), *dir);
|
|
|
|
if (!child)
|
|
|
|
return nullptr;
|
2014-09-23 11:14:30 +00:00
|
|
|
|
2014-09-28 14:46:29 +00:00
|
|
|
dir = child;
|
2014-09-23 11:14:30 +00:00
|
|
|
path.erase(0, delimiter_position + 1);
|
|
|
|
}
|
|
|
|
|
2022-07-05 16:35:38 +00:00
|
|
|
return GetChildNode(root, path, *dir);
|
2014-09-23 11:14:30 +00:00
|
|
|
}
|
|
|
|
|
2014-09-24 04:02:33 +00:00
|
|
|
bool FillFileInfoWithNode(Archive::FileInfo* info,
|
2016-03-08 04:40:10 +00:00
|
|
|
uint32_t header_size,
|
2021-09-09 21:49:01 +00:00
|
|
|
bool load_integrity,
|
2022-07-05 16:35:38 +00:00
|
|
|
const base::Value::Dict* node) {
|
2024-01-10 22:23:35 +00:00
|
|
|
if (std::optional<int> size = node->FindInt("size")) {
|
2022-07-05 16:35:38 +00:00
|
|
|
info->size = static_cast<uint32_t>(*size);
|
2022-01-10 22:31:39 +00:00
|
|
|
} else {
|
2015-03-20 12:34:58 +00:00
|
|
|
return false;
|
2022-01-10 22:31:39 +00:00
|
|
|
}
|
2015-03-20 12:34:58 +00:00
|
|
|
|
2024-01-10 22:23:35 +00:00
|
|
|
if (std::optional<bool> unpacked = node->FindBool("unpacked")) {
|
2022-07-05 16:35:38 +00:00
|
|
|
info->unpacked = *unpacked;
|
2022-01-10 22:31:39 +00:00
|
|
|
if (info->unpacked) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2015-03-20 12:34:58 +00:00
|
|
|
|
2022-07-05 16:35:38 +00:00
|
|
|
const std::string* offset = node->FindString("offset");
|
2022-01-10 22:31:39 +00:00
|
|
|
if (offset &&
|
2024-01-11 01:00:37 +00:00
|
|
|
base::StringToUint64(std::string_view{*offset}, &info->offset)) {
|
2022-01-10 22:31:39 +00:00
|
|
|
info->offset += header_size;
|
|
|
|
} else {
|
2014-09-24 04:02:33 +00:00
|
|
|
return false;
|
2022-01-10 22:31:39 +00:00
|
|
|
}
|
2015-03-20 12:34:58 +00:00
|
|
|
|
2024-01-10 22:23:35 +00:00
|
|
|
if (std::optional<bool> executable = node->FindBool("executable")) {
|
2022-07-05 16:35:38 +00:00
|
|
|
info->executable = *executable;
|
2022-01-10 22:31:39 +00:00
|
|
|
}
|
2015-11-26 12:48:47 +00:00
|
|
|
|
2024-05-13 17:48:26 +00:00
|
|
|
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
|
2021-09-09 21:49:01 +00:00
|
|
|
if (load_integrity &&
|
|
|
|
electron::fuses::IsEmbeddedAsarIntegrityValidationEnabled()) {
|
2022-07-05 16:35:38 +00:00
|
|
|
if (const base::Value::Dict* integrity = node->FindDict("integrity")) {
|
|
|
|
const std::string* algorithm = integrity->FindString("algorithm");
|
|
|
|
const std::string* hash = integrity->FindString("hash");
|
2024-01-10 22:23:35 +00:00
|
|
|
std::optional<int> block_size = integrity->FindInt("blockSize");
|
2022-07-05 16:35:38 +00:00
|
|
|
const base::Value::List* blocks = integrity->FindList("blocks");
|
2022-01-10 22:31:39 +00:00
|
|
|
|
|
|
|
if (algorithm && hash && block_size && block_size > 0 && blocks) {
|
|
|
|
IntegrityPayload integrity_payload;
|
|
|
|
integrity_payload.hash = *hash;
|
|
|
|
integrity_payload.block_size =
|
|
|
|
static_cast<uint32_t>(block_size.value());
|
2022-07-05 16:35:38 +00:00
|
|
|
for (auto& value : *blocks) {
|
|
|
|
if (const std::string* block = value.GetIfString()) {
|
2022-01-10 22:31:39 +00:00
|
|
|
integrity_payload.blocks.push_back(*block);
|
|
|
|
} else {
|
2021-09-09 21:49:01 +00:00
|
|
|
LOG(FATAL)
|
|
|
|
<< "Invalid block integrity value for file in ASAR archive";
|
|
|
|
}
|
|
|
|
}
|
2022-01-10 22:31:39 +00:00
|
|
|
if (*algorithm == "SHA256") {
|
2023-05-12 13:23:42 +00:00
|
|
|
integrity_payload.algorithm = HashAlgorithm::kSHA256;
|
2021-09-09 21:49:01 +00:00
|
|
|
info->integrity = std::move(integrity_payload);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!info->integrity.has_value()) {
|
|
|
|
LOG(FATAL) << "Failed to read integrity for file in ASAR archive";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-09-24 04:02:33 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-23 11:14:30 +00:00
|
|
|
} // namespace
|
|
|
|
|
2021-09-10 02:52:23 +00:00
|
|
|
IntegrityPayload::IntegrityPayload()
|
2023-05-12 13:23:42 +00:00
|
|
|
: algorithm(HashAlgorithm::kNone), block_size(0) {}
|
2021-09-10 02:52:23 +00:00
|
|
|
IntegrityPayload::~IntegrityPayload() = default;
|
|
|
|
IntegrityPayload::IntegrityPayload(const IntegrityPayload& other) = default;
|
|
|
|
|
|
|
|
Archive::FileInfo::FileInfo()
|
|
|
|
: unpacked(false), executable(false), size(0), offset(0) {}
|
|
|
|
Archive::FileInfo::~FileInfo() = default;
|
|
|
|
|
2021-03-15 18:42:54 +00:00
|
|
|
Archive::Archive(const base::FilePath& path)
|
2021-06-04 01:49:08 +00:00
|
|
|
: initialized_(false), path_(path), file_(base::File::FILE_OK) {
|
2022-11-17 19:59:23 +00:00
|
|
|
electron::ScopedAllowBlockingForElectron allow_blocking;
|
2021-03-15 18:42:54 +00:00
|
|
|
file_.Initialize(path_, base::File::FLAG_OPEN | base::File::FLAG_READ);
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2021-03-15 18:42:54 +00:00
|
|
|
fd_ = _open_osfhandle(reinterpret_cast<intptr_t>(file_.GetPlatformFile()), 0);
|
2022-02-10 02:58:52 +00:00
|
|
|
#elif BUILDFLAG(IS_POSIX)
|
2021-03-15 18:42:54 +00:00
|
|
|
fd_ = file_.GetPlatformFile();
|
|
|
|
#endif
|
2014-09-23 11:14:30 +00:00
|
|
|
}
|
|
|
|
|
2021-03-15 18:42:54 +00:00
|
|
|
Archive::~Archive() {
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2021-03-15 18:42:54 +00:00
|
|
|
if (fd_ != -1) {
|
|
|
|
_close(fd_);
|
|
|
|
// Don't close the handle since we already closed the fd.
|
|
|
|
file_.TakePlatformFile();
|
|
|
|
}
|
|
|
|
#endif
|
2022-11-17 19:59:23 +00:00
|
|
|
electron::ScopedAllowBlockingForElectron allow_blocking;
|
2021-03-15 18:42:54 +00:00
|
|
|
file_.Close();
|
|
|
|
}
|
2020-08-04 18:48:04 +00:00
|
|
|
|
2014-09-23 11:14:30 +00:00
|
|
|
bool Archive::Init() {
|
2021-06-04 01:49:08 +00:00
|
|
|
// Should only be initialized once
|
|
|
|
CHECK(!initialized_);
|
|
|
|
initialized_ = true;
|
|
|
|
|
2016-01-28 14:38:53 +00:00
|
|
|
if (!file_.IsValid()) {
|
2021-03-15 18:42:54 +00:00
|
|
|
if (file_.error_details() != base::File::FILE_ERROR_NOT_FOUND) {
|
|
|
|
LOG(WARNING) << "Opening " << path_.value() << ": "
|
|
|
|
<< base::File::ErrorToString(file_.error_details());
|
|
|
|
}
|
2014-09-23 11:14:30 +00:00
|
|
|
return false;
|
2016-01-28 14:38:53 +00:00
|
|
|
}
|
2014-09-23 11:14:30 +00:00
|
|
|
|
2024-09-09 20:34:42 +00:00
|
|
|
std::vector<uint8_t> buf;
|
2021-03-15 18:42:54 +00:00
|
|
|
|
|
|
|
buf.resize(8);
|
|
|
|
{
|
2022-11-17 19:59:23 +00:00
|
|
|
electron::ScopedAllowBlockingForElectron allow_blocking;
|
2024-09-09 20:34:42 +00:00
|
|
|
if (!file_.ReadAtCurrentPosAndCheck(buf)) {
|
|
|
|
PLOG(ERROR) << "Failed to read header size from " << path_.value();
|
|
|
|
return false;
|
|
|
|
}
|
2014-09-23 11:14:30 +00:00
|
|
|
}
|
|
|
|
|
2016-03-08 04:40:10 +00:00
|
|
|
uint32_t size;
|
2024-09-09 20:34:42 +00:00
|
|
|
if (!base::PickleIterator(base::Pickle::WithData(buf)).ReadUInt32(&size)) {
|
2021-03-15 18:42:54 +00:00
|
|
|
LOG(ERROR) << "Failed to parse header size from " << path_.value();
|
2014-09-23 11:14:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-15 18:42:54 +00:00
|
|
|
buf.resize(size);
|
|
|
|
{
|
2022-11-17 19:59:23 +00:00
|
|
|
electron::ScopedAllowBlockingForElectron allow_blocking;
|
2024-09-09 20:34:42 +00:00
|
|
|
if (!file_.ReadAtCurrentPosAndCheck(buf)) {
|
|
|
|
PLOG(ERROR) << "Failed to read header from " << path_.value();
|
|
|
|
return false;
|
|
|
|
}
|
2014-09-23 11:14:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string header;
|
2024-09-09 20:34:42 +00:00
|
|
|
if (!base::PickleIterator(base::Pickle::WithData(buf)).ReadString(&header)) {
|
2021-03-15 18:42:54 +00:00
|
|
|
LOG(ERROR) << "Failed to parse header from " << path_.value();
|
2014-09-23 11:14:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-01-18 20:00:49 +00:00
|
|
|
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
|
2021-09-09 21:49:01 +00:00
|
|
|
// Validate header signature if required and possible
|
|
|
|
if (electron::fuses::IsEmbeddedAsarIntegrityValidationEnabled() &&
|
|
|
|
RelativePath().has_value()) {
|
2024-01-10 22:23:35 +00:00
|
|
|
std::optional<IntegrityPayload> integrity = HeaderIntegrity();
|
2021-09-09 21:49:01 +00:00
|
|
|
if (!integrity.has_value()) {
|
|
|
|
LOG(FATAL) << "Failed to get integrity for validatable asar archive: "
|
|
|
|
<< RelativePath().value();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Currently we only support the sha256 algorithm, we can add support for
|
|
|
|
// more below ensure we read them in preference order from most secure to
|
|
|
|
// least
|
2024-09-07 01:22:44 +00:00
|
|
|
if (integrity->algorithm != HashAlgorithm::kNone) {
|
|
|
|
ValidateIntegrityOrDie(base::as_byte_span(header), *integrity);
|
2021-09-09 21:49:01 +00:00
|
|
|
} else {
|
|
|
|
LOG(FATAL) << "No eligible hash for validatable asar archive: "
|
|
|
|
<< RelativePath().value();
|
|
|
|
}
|
|
|
|
|
|
|
|
header_validated_ = true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2024-01-10 22:23:35 +00:00
|
|
|
std::optional<base::Value> value = base::JSONReader::Read(header);
|
2018-04-10 14:05:36 +00:00
|
|
|
if (!value || !value->is_dict()) {
|
2021-03-15 18:42:54 +00:00
|
|
|
LOG(ERROR) << "Failed to parse header";
|
2014-09-23 11:14:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-23 12:30:07 +00:00
|
|
|
header_size_ = 8 + size;
|
2024-05-13 13:58:52 +00:00
|
|
|
header_ = std::move(*value).TakeDict();
|
2014-09-23 11:14:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-01-18 20:00:49 +00:00
|
|
|
#if !BUILDFLAG(IS_MAC) && !BUILDFLAG(IS_WIN)
|
2024-01-10 22:23:35 +00:00
|
|
|
std::optional<IntegrityPayload> Archive::HeaderIntegrity() const {
|
|
|
|
return std::nullopt;
|
2021-09-09 21:49:01 +00:00
|
|
|
}
|
|
|
|
|
2024-01-10 22:23:35 +00:00
|
|
|
std::optional<base::FilePath> Archive::RelativePath() const {
|
|
|
|
return std::nullopt;
|
2021-09-09 21:49:01 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-06-04 01:49:08 +00:00
|
|
|
bool Archive::GetFileInfo(const base::FilePath& path, FileInfo* info) const {
|
2014-09-23 11:14:30 +00:00
|
|
|
if (!header_)
|
|
|
|
return false;
|
|
|
|
|
2022-07-05 16:35:38 +00:00
|
|
|
const base::Value::Dict* node =
|
|
|
|
GetNodeFromPath(path.AsUTF8Unsafe(), *header_);
|
|
|
|
if (!node)
|
2014-09-23 11:14:30 +00:00
|
|
|
return false;
|
|
|
|
|
2022-07-05 16:35:38 +00:00
|
|
|
const std::string* link = node->FindString("link");
|
|
|
|
if (link)
|
|
|
|
return GetFileInfo(base::FilePath::FromUTF8Unsafe(*link), info);
|
2014-09-23 11:14:30 +00:00
|
|
|
|
2021-09-09 21:49:01 +00:00
|
|
|
return FillFileInfoWithNode(info, header_size_, header_validated_, node);
|
2014-09-24 04:02:33 +00:00
|
|
|
}
|
|
|
|
|
2021-06-04 01:49:08 +00:00
|
|
|
bool Archive::Stat(const base::FilePath& path, Stats* stats) const {
|
2014-09-24 04:02:33 +00:00
|
|
|
if (!header_)
|
2014-09-23 11:14:30 +00:00
|
|
|
return false;
|
|
|
|
|
2022-07-05 16:35:38 +00:00
|
|
|
const base::Value::Dict* node =
|
|
|
|
GetNodeFromPath(path.AsUTF8Unsafe(), *header_);
|
|
|
|
if (!node)
|
2014-09-23 11:14:30 +00:00
|
|
|
return false;
|
|
|
|
|
2022-07-05 16:35:38 +00:00
|
|
|
if (node->Find("link")) {
|
2023-09-25 11:17:24 +00:00
|
|
|
stats->type = FileType::kLink;
|
2014-09-24 04:02:33 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-07-05 16:35:38 +00:00
|
|
|
if (node->Find("files")) {
|
2023-09-25 11:17:24 +00:00
|
|
|
stats->type = FileType::kDirectory;
|
2014-09-24 04:02:33 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-09 21:49:01 +00:00
|
|
|
return FillFileInfoWithNode(stats, header_size_, header_validated_, node);
|
2014-09-23 11:14:30 +00:00
|
|
|
}
|
|
|
|
|
2014-09-24 10:44:00 +00:00
|
|
|
bool Archive::Readdir(const base::FilePath& path,
|
2021-06-04 01:49:08 +00:00
|
|
|
std::vector<base::FilePath>* files) const {
|
2014-09-24 10:44:00 +00:00
|
|
|
if (!header_)
|
|
|
|
return false;
|
|
|
|
|
2022-07-05 16:35:38 +00:00
|
|
|
const base::Value::Dict* node =
|
|
|
|
GetNodeFromPath(path.AsUTF8Unsafe(), *header_);
|
|
|
|
if (!node)
|
2014-09-24 10:44:00 +00:00
|
|
|
return false;
|
|
|
|
|
2022-07-05 16:35:38 +00:00
|
|
|
const base::Value::Dict* files_node = GetFilesNode(*header_, *node);
|
|
|
|
if (!files_node)
|
2014-09-24 10:44:00 +00:00
|
|
|
return false;
|
|
|
|
|
2022-07-05 16:35:38 +00:00
|
|
|
for (const auto iter : *files_node)
|
|
|
|
files->push_back(base::FilePath::FromUTF8Unsafe(iter.first));
|
2014-09-24 10:44:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-06-04 01:49:08 +00:00
|
|
|
bool Archive::Realpath(const base::FilePath& path,
|
|
|
|
base::FilePath* realpath) const {
|
2014-09-30 06:53:41 +00:00
|
|
|
if (!header_)
|
|
|
|
return false;
|
|
|
|
|
2022-07-05 16:35:38 +00:00
|
|
|
const base::Value::Dict* node =
|
|
|
|
GetNodeFromPath(path.AsUTF8Unsafe(), *header_);
|
|
|
|
if (!node)
|
2014-09-30 06:53:41 +00:00
|
|
|
return false;
|
|
|
|
|
2022-07-05 16:35:38 +00:00
|
|
|
const std::string* link = node->FindString("link");
|
|
|
|
if (link) {
|
|
|
|
*realpath = base::FilePath::FromUTF8Unsafe(*link);
|
2014-09-30 06:53:41 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
*realpath = path;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-25 08:56:50 +00:00
|
|
|
bool Archive::CopyFileOut(const base::FilePath& path, base::FilePath* out) {
|
2021-06-04 01:49:08 +00:00
|
|
|
if (!header_)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
base::AutoLock auto_lock(external_files_lock_);
|
|
|
|
|
2017-04-04 04:50:44 +00:00
|
|
|
auto it = external_files_.find(path.value());
|
|
|
|
if (it != external_files_.end()) {
|
|
|
|
*out = it->second->path();
|
2014-09-25 08:56:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileInfo info;
|
|
|
|
if (!GetFileInfo(path, &info))
|
|
|
|
return false;
|
|
|
|
|
2015-03-20 12:34:58 +00:00
|
|
|
if (info.unpacked) {
|
|
|
|
*out = path_.AddExtension(FILE_PATH_LITERAL("unpacked")).Append(path);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-18 07:32:55 +00:00
|
|
|
auto temp_file = std::make_unique<ScopedTemporaryFile>();
|
2015-12-01 15:57:32 +00:00
|
|
|
base::FilePath::StringType ext = path.Extension();
|
2021-09-09 21:49:01 +00:00
|
|
|
if (!temp_file->InitFromFile(&file_, ext, info.offset, info.size,
|
|
|
|
info.integrity))
|
2014-09-25 08:56:50 +00:00
|
|
|
return false;
|
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_POSIX)
|
2015-11-27 14:06:37 +00:00
|
|
|
if (info.executable) {
|
|
|
|
// chmod a+x temp_file;
|
|
|
|
base::SetPosixFilePermissions(temp_file->path(), 0755);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-09-25 08:56:50 +00:00
|
|
|
*out = temp_file->path();
|
2017-04-04 04:50:44 +00:00
|
|
|
external_files_[path.value()] = std::move(temp_file);
|
2014-09-25 08:56:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-09 21:49:01 +00:00
|
|
|
int Archive::GetUnsafeFD() const {
|
2021-03-15 18:42:54 +00:00
|
|
|
return fd_;
|
|
|
|
}
|
|
|
|
|
2014-09-23 11:14:30 +00:00
|
|
|
} // namespace asar
|