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.
|
|
|
|
|
2021-11-22 07:34:31 +00:00
|
|
|
#ifndef ELECTRON_SHELL_COMMON_ASAR_ARCHIVE_H_
|
|
|
|
#define ELECTRON_SHELL_COMMON_ASAR_ARCHIVE_H_
|
2014-09-23 11:14:30 +00:00
|
|
|
|
2016-07-04 06:08:55 +00:00
|
|
|
#include <memory>
|
2024-01-10 22:23:35 +00:00
|
|
|
#include <optional>
|
2021-09-09 21:49:01 +00:00
|
|
|
#include <string>
|
2017-04-04 04:50:44 +00:00
|
|
|
#include <unordered_map>
|
2014-09-24 10:44:00 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2023-09-25 11:17:24 +00:00
|
|
|
#include <uv.h>
|
|
|
|
|
2015-05-11 02:47:07 +00:00
|
|
|
#include "base/files/file.h"
|
2014-09-23 11:14:30 +00:00
|
|
|
#include "base/files/file_path.h"
|
2021-06-04 01:49:08 +00:00
|
|
|
#include "base/synchronization/lock.h"
|
2022-07-05 16:35:38 +00:00
|
|
|
#include "base/values.h"
|
2014-09-23 11:14:30 +00:00
|
|
|
|
|
|
|
namespace asar {
|
|
|
|
|
2014-09-25 08:56:50 +00:00
|
|
|
class ScopedTemporaryFile;
|
|
|
|
|
2023-05-12 13:23:42 +00:00
|
|
|
enum class HashAlgorithm {
|
|
|
|
kSHA256,
|
|
|
|
kNone,
|
2021-09-09 21:49:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct IntegrityPayload {
|
2021-09-10 02:52:23 +00:00
|
|
|
IntegrityPayload();
|
|
|
|
~IntegrityPayload();
|
|
|
|
IntegrityPayload(const IntegrityPayload& other);
|
2024-09-24 05:37:18 +00:00
|
|
|
HashAlgorithm algorithm = HashAlgorithm::kNone;
|
2021-09-09 21:49:01 +00:00
|
|
|
std::string hash;
|
2024-09-24 05:37:18 +00:00
|
|
|
uint32_t block_size = 0U;
|
2021-09-09 21:49:01 +00:00
|
|
|
std::vector<std::string> blocks;
|
|
|
|
};
|
|
|
|
|
2014-09-25 08:56:50 +00:00
|
|
|
// This class represents an asar package, and provides methods to read
|
2021-06-04 01:49:08 +00:00
|
|
|
// information from it. It is thread-safe after |Init| has been called.
|
2014-09-25 12:38:12 +00:00
|
|
|
class Archive {
|
2014-09-23 11:14:30 +00:00
|
|
|
public:
|
|
|
|
struct FileInfo {
|
2021-09-10 02:52:23 +00:00
|
|
|
FileInfo();
|
|
|
|
~FileInfo();
|
2024-09-24 05:37:18 +00:00
|
|
|
bool unpacked = false;
|
|
|
|
bool executable = false;
|
|
|
|
uint32_t size = 0U;
|
|
|
|
uint64_t offset = 0U;
|
2024-01-10 22:23:35 +00:00
|
|
|
std::optional<IntegrityPayload> integrity;
|
2014-09-23 11:14:30 +00:00
|
|
|
};
|
|
|
|
|
2023-09-25 11:17:24 +00:00
|
|
|
enum class FileType {
|
|
|
|
kFile = UV_DIRENT_FILE,
|
|
|
|
kDirectory = UV_DIRENT_DIR,
|
|
|
|
kLink = UV_DIRENT_LINK,
|
|
|
|
};
|
|
|
|
|
2014-09-24 04:02:33 +00:00
|
|
|
struct Stats : public FileInfo {
|
2023-09-25 11:17:24 +00:00
|
|
|
FileType type = FileType::kFile;
|
2014-09-24 04:02:33 +00:00
|
|
|
};
|
|
|
|
|
2014-09-23 11:14:30 +00:00
|
|
|
explicit Archive(const base::FilePath& path);
|
2014-09-25 12:38:12 +00:00
|
|
|
virtual ~Archive();
|
2014-09-23 11:14:30 +00:00
|
|
|
|
2021-11-03 11:41:45 +00:00
|
|
|
// disable copy
|
|
|
|
Archive(const Archive&) = delete;
|
|
|
|
Archive& operator=(const Archive&) = delete;
|
|
|
|
|
2014-09-23 11:14:30 +00:00
|
|
|
// Read and parse the header.
|
|
|
|
bool Init();
|
|
|
|
|
2024-01-10 22:23:35 +00:00
|
|
|
std::optional<IntegrityPayload> HeaderIntegrity() const;
|
|
|
|
std::optional<base::FilePath> RelativePath() const;
|
2021-09-09 21:49:01 +00:00
|
|
|
|
2014-09-23 11:14:30 +00:00
|
|
|
// Get the info of a file.
|
2021-06-04 01:49:08 +00:00
|
|
|
bool GetFileInfo(const base::FilePath& path, FileInfo* info) const;
|
2014-09-23 11:14:30 +00:00
|
|
|
|
2014-09-24 04:02:33 +00:00
|
|
|
// Fs.stat(path).
|
2021-06-04 01:49:08 +00:00
|
|
|
bool Stat(const base::FilePath& path, Stats* stats) const;
|
2014-09-24 04:02:33 +00:00
|
|
|
|
2014-09-24 10:44:00 +00:00
|
|
|
// Fs.readdir(path).
|
2021-06-04 01:49:08 +00:00
|
|
|
bool Readdir(const base::FilePath& path,
|
|
|
|
std::vector<base::FilePath>* files) const;
|
2014-09-24 10:44:00 +00:00
|
|
|
|
2014-09-30 06:53:41 +00:00
|
|
|
// Fs.realpath(path).
|
2021-06-04 01:49:08 +00:00
|
|
|
bool Realpath(const base::FilePath& path, base::FilePath* realpath) const;
|
2014-09-30 06:53:41 +00:00
|
|
|
|
2014-09-25 08:56:50 +00:00
|
|
|
// Copy the file into a temporary file, and return the new path.
|
2015-03-20 12:34:58 +00:00
|
|
|
// For unpacked file, this method will return its real path.
|
2014-09-25 08:56:50 +00:00
|
|
|
bool CopyFileOut(const base::FilePath& path, base::FilePath* out);
|
|
|
|
|
2021-03-15 18:42:54 +00:00
|
|
|
// Returns the file's fd.
|
2021-09-09 21:49:01 +00:00
|
|
|
// Using this fd will not validate the integrity of any files
|
|
|
|
// you read out of the ASAR manually. Callers are responsible
|
|
|
|
// for integrity validation after this fd is handed over.
|
|
|
|
int GetUnsafeFD() const;
|
2021-03-15 18:42:54 +00:00
|
|
|
|
2014-09-23 11:14:30 +00:00
|
|
|
base::FilePath path() const { return path_; }
|
|
|
|
|
|
|
|
private:
|
2024-09-24 05:37:18 +00:00
|
|
|
bool initialized_ = false;
|
2021-09-09 21:49:01 +00:00
|
|
|
bool header_validated_ = false;
|
2021-06-04 01:49:08 +00:00
|
|
|
const base::FilePath path_;
|
2024-09-24 05:37:18 +00:00
|
|
|
base::File file_{base::File::FILE_OK};
|
2021-03-15 18:42:54 +00:00
|
|
|
int fd_ = -1;
|
2018-05-21 22:18:38 +00:00
|
|
|
uint32_t header_size_ = 0;
|
2024-01-10 22:23:35 +00:00
|
|
|
std::optional<base::Value::Dict> header_;
|
2014-09-23 11:14:30 +00:00
|
|
|
|
2014-09-25 08:56:50 +00:00
|
|
|
// Cached external temporary files.
|
2021-06-04 01:49:08 +00:00
|
|
|
base::Lock external_files_lock_;
|
2017-04-04 04:50:44 +00:00
|
|
|
std::unordered_map<base::FilePath::StringType,
|
2018-04-18 01:44:10 +00:00
|
|
|
std::unique_ptr<ScopedTemporaryFile>>
|
|
|
|
external_files_;
|
2014-09-23 11:14:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace asar
|
|
|
|
|
2021-11-22 07:34:31 +00:00
|
|
|
#endif // ELECTRON_SHELL_COMMON_ASAR_ARCHIVE_H_
|