archive.h 2.1 KB

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