archive.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 SHELL_COMMON_ASAR_ARCHIVE_H_
  5. #define SHELL_COMMON_ASAR_ARCHIVE_H_
  6. #include <memory>
  7. #include <unordered_map>
  8. #include <vector>
  9. #include "base/files/file.h"
  10. #include "base/files/file_path.h"
  11. #include "base/synchronization/lock.h"
  12. namespace base {
  13. class DictionaryValue;
  14. }
  15. namespace asar {
  16. class ScopedTemporaryFile;
  17. // This class represents an asar package, and provides methods to read
  18. // information from it. It is thread-safe after |Init| has been called.
  19. class Archive {
  20. public:
  21. struct FileInfo {
  22. FileInfo() : unpacked(false), executable(false), size(0), offset(0) {}
  23. bool unpacked;
  24. bool executable;
  25. uint32_t size;
  26. uint64_t offset;
  27. };
  28. struct Stats : public FileInfo {
  29. Stats() : is_file(true), is_directory(false), is_link(false) {}
  30. bool is_file;
  31. bool is_directory;
  32. bool is_link;
  33. };
  34. explicit Archive(const base::FilePath& path);
  35. virtual ~Archive();
  36. // Read and parse the header.
  37. bool Init();
  38. // Get the info of a file.
  39. bool GetFileInfo(const base::FilePath& path, FileInfo* info) const;
  40. // Fs.stat(path).
  41. bool Stat(const base::FilePath& path, Stats* stats) const;
  42. // Fs.readdir(path).
  43. bool Readdir(const base::FilePath& path,
  44. std::vector<base::FilePath>* files) const;
  45. // Fs.realpath(path).
  46. bool Realpath(const base::FilePath& path, base::FilePath* realpath) const;
  47. // Copy the file into a temporary file, and return the new path.
  48. // For unpacked file, this method will return its real path.
  49. bool CopyFileOut(const base::FilePath& path, base::FilePath* out);
  50. // Returns the file's fd.
  51. int GetFD() const;
  52. base::FilePath path() const { return path_; }
  53. private:
  54. bool initialized_;
  55. const base::FilePath path_;
  56. base::File file_;
  57. int fd_ = -1;
  58. uint32_t header_size_ = 0;
  59. std::unique_ptr<base::DictionaryValue> header_;
  60. // Cached external temporary files.
  61. base::Lock external_files_lock_;
  62. std::unordered_map<base::FilePath::StringType,
  63. std::unique_ptr<ScopedTemporaryFile>>
  64. external_files_;
  65. DISALLOW_COPY_AND_ASSIGN(Archive);
  66. };
  67. } // namespace asar
  68. #endif // SHELL_COMMON_ASAR_ARCHIVE_H_