archive_backend.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <memory>
  6. #include <string>
  7. #include <utility>
  8. #include <vector>
  9. #include "common/bit_field.h"
  10. #include "common/common_types.h"
  11. #include "core/hle/result.h"
  12. namespace FileSys {
  13. class FileBackend;
  14. class DirectoryBackend;
  15. // Path string type
  16. enum LowPathType : u32 {
  17. Invalid = 0,
  18. Empty = 1,
  19. Binary = 2,
  20. Char = 3,
  21. Wchar = 4
  22. };
  23. union Mode {
  24. u32 hex;
  25. BitField<0, 1, u32> read_flag;
  26. BitField<1, 1, u32> write_flag;
  27. BitField<2, 1, u32> create_flag;
  28. };
  29. class Path {
  30. public:
  31. Path() : type(Invalid) {}
  32. Path(const char* path) : type(Char), string(path) {}
  33. Path(std::vector<u8> binary_data) : type(Binary), binary(std::move(binary_data)) {}
  34. Path(LowPathType type, u32 size, u32 pointer);
  35. LowPathType GetType() const { return type; }
  36. /**
  37. * Gets the string representation of the path for debugging
  38. * @return String representation of the path for debugging
  39. */
  40. const std::string DebugStr() const;
  41. const std::string AsString() const;
  42. const std::u16string AsU16Str() const;
  43. const std::vector<u8> AsBinary() const;
  44. private:
  45. LowPathType type;
  46. std::vector<u8> binary;
  47. std::string string;
  48. std::u16string u16str;
  49. };
  50. class ArchiveBackend : NonCopyable {
  51. public:
  52. virtual ~ArchiveBackend() {
  53. }
  54. /**
  55. * Get a descriptive name for the archive (e.g. "RomFS", "SaveData", etc.)
  56. */
  57. virtual std::string GetName() const = 0;
  58. /**
  59. * Open a file specified by its path, using the specified mode
  60. * @param path Path relative to the archive
  61. * @param mode Mode to open the file with
  62. * @return Opened file, or nullptr
  63. */
  64. virtual std::unique_ptr<FileBackend> OpenFile(const Path& path, const Mode mode) const = 0;
  65. /**
  66. * Delete a file specified by its path
  67. * @param path Path relative to the archive
  68. * @return Whether the file could be deleted
  69. */
  70. virtual bool DeleteFile(const Path& path) const = 0;
  71. /**
  72. * Rename a File specified by its path
  73. * @param src_path Source path relative to the archive
  74. * @param dest_path Destination path relative to the archive
  75. * @return Whether rename succeeded
  76. */
  77. virtual bool RenameFile(const Path& src_path, const Path& dest_path) const = 0;
  78. /**
  79. * Delete a directory specified by its path
  80. * @param path Path relative to the archive
  81. * @return Whether the directory could be deleted
  82. */
  83. virtual bool DeleteDirectory(const Path& path) const = 0;
  84. /**
  85. * Create a file specified by its path
  86. * @param path Path relative to the Archive
  87. * @param size The size of the new file, filled with zeroes
  88. * @return File creation result code
  89. */
  90. virtual ResultCode CreateFile(const Path& path, u32 size) const = 0;
  91. /**
  92. * Create a directory specified by its path
  93. * @param path Path relative to the archive
  94. * @return Whether the directory could be created
  95. */
  96. virtual bool CreateDirectory(const Path& path) const = 0;
  97. /**
  98. * Rename a Directory specified by its path
  99. * @param src_path Source path relative to the archive
  100. * @param dest_path Destination path relative to the archive
  101. * @return Whether rename succeeded
  102. */
  103. virtual bool RenameDirectory(const Path& src_path, const Path& dest_path) const = 0;
  104. /**
  105. * Open a directory specified by its path
  106. * @param path Path relative to the archive
  107. * @return Opened directory, or nullptr
  108. */
  109. virtual std::unique_ptr<DirectoryBackend> OpenDirectory(const Path& path) const = 0;
  110. };
  111. class ArchiveFactory : NonCopyable {
  112. public:
  113. virtual ~ArchiveFactory() {
  114. }
  115. /**
  116. * Get a descriptive name for the archive (e.g. "RomFS", "SaveData", etc.)
  117. */
  118. virtual std::string GetName() const = 0;
  119. /**
  120. * Tries to open the archive of this type with the specified path
  121. * @param path Path to the archive
  122. * @return An ArchiveBackend corresponding operating specified archive path.
  123. */
  124. virtual ResultVal<std::unique_ptr<ArchiveBackend>> Open(const Path& path) = 0;
  125. /**
  126. * Deletes the archive contents and then re-creates the base folder
  127. * @param path Path to the archive
  128. * @return ResultCode of the operation, 0 on success
  129. */
  130. virtual ResultCode Format(const Path& path) = 0;
  131. };
  132. } // namespace FileSys