archive.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright (c) 2014 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #ifndef ELECTRON_SHELL_COMMON_ASAR_ARCHIVE_H_
  5. #define ELECTRON_SHELL_COMMON_ASAR_ARCHIVE_H_
  6. #include <memory>
  7. #include <string>
  8. #include <unordered_map>
  9. #include <vector>
  10. #include <uv.h>
  11. #include "base/files/file.h"
  12. #include "base/files/file_path.h"
  13. #include "base/synchronization/lock.h"
  14. #include "base/values.h"
  15. #include "third_party/abseil-cpp/absl/types/optional.h"
  16. namespace asar {
  17. class ScopedTemporaryFile;
  18. enum class HashAlgorithm {
  19. kSHA256,
  20. kNone,
  21. };
  22. struct IntegrityPayload {
  23. IntegrityPayload();
  24. ~IntegrityPayload();
  25. IntegrityPayload(const IntegrityPayload& other);
  26. HashAlgorithm algorithm;
  27. std::string hash;
  28. uint32_t block_size;
  29. std::vector<std::string> blocks;
  30. };
  31. // This class represents an asar package, and provides methods to read
  32. // information from it. It is thread-safe after |Init| has been called.
  33. class Archive {
  34. public:
  35. struct FileInfo {
  36. FileInfo();
  37. ~FileInfo();
  38. bool unpacked;
  39. bool executable;
  40. uint32_t size;
  41. uint64_t offset;
  42. absl::optional<IntegrityPayload> integrity;
  43. };
  44. enum class FileType {
  45. kFile = UV_DIRENT_FILE,
  46. kDirectory = UV_DIRENT_DIR,
  47. kLink = UV_DIRENT_LINK,
  48. };
  49. struct Stats : public FileInfo {
  50. FileType type = FileType::kFile;
  51. };
  52. explicit Archive(const base::FilePath& path);
  53. virtual ~Archive();
  54. // disable copy
  55. Archive(const Archive&) = delete;
  56. Archive& operator=(const Archive&) = delete;
  57. // Read and parse the header.
  58. bool Init();
  59. absl::optional<IntegrityPayload> HeaderIntegrity() const;
  60. absl::optional<base::FilePath> RelativePath() const;
  61. // Get the info of a file.
  62. bool GetFileInfo(const base::FilePath& path, FileInfo* info) const;
  63. // Fs.stat(path).
  64. bool Stat(const base::FilePath& path, Stats* stats) const;
  65. // Fs.readdir(path).
  66. bool Readdir(const base::FilePath& path,
  67. std::vector<base::FilePath>* files) const;
  68. // Fs.realpath(path).
  69. bool Realpath(const base::FilePath& path, base::FilePath* realpath) const;
  70. // Copy the file into a temporary file, and return the new path.
  71. // For unpacked file, this method will return its real path.
  72. bool CopyFileOut(const base::FilePath& path, base::FilePath* out);
  73. // Returns the file's fd.
  74. // Using this fd will not validate the integrity of any files
  75. // you read out of the ASAR manually. Callers are responsible
  76. // for integrity validation after this fd is handed over.
  77. int GetUnsafeFD() const;
  78. base::FilePath path() const { return path_; }
  79. private:
  80. bool initialized_;
  81. bool header_validated_ = false;
  82. const base::FilePath path_;
  83. base::File file_;
  84. int fd_ = -1;
  85. uint32_t header_size_ = 0;
  86. absl::optional<base::Value::Dict> header_;
  87. // Cached external temporary files.
  88. base::Lock external_files_lock_;
  89. std::unordered_map<base::FilePath::StringType,
  90. std::unique_ptr<ScopedTemporaryFile>>
  91. external_files_;
  92. };
  93. } // namespace asar
  94. #endif // ELECTRON_SHELL_COMMON_ASAR_ARCHIVE_H_