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