archive.h 3.0 KB

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