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