archive_backend.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 "common/swap.h"
  12. #include "core/hle/result.h"
  13. namespace FileSys {
  14. class FileBackend;
  15. class DirectoryBackend;
  16. // Path string type
  17. enum LowPathType : u32 {
  18. Invalid = 0,
  19. Empty = 1,
  20. Binary = 2,
  21. Char = 3,
  22. Wchar = 4,
  23. };
  24. union Mode {
  25. u32 hex;
  26. BitField<0, 1, u32> read_flag;
  27. BitField<1, 1, u32> write_flag;
  28. BitField<2, 1, u32> create_flag;
  29. };
  30. class Path {
  31. public:
  32. Path() : type(Invalid) {}
  33. Path(const char* path) : type(Char), string(path) {}
  34. Path(std::vector<u8> binary_data) : type(Binary), binary(std::move(binary_data)) {}
  35. Path(LowPathType type, u32 size, u32 pointer);
  36. LowPathType GetType() const {
  37. return type;
  38. }
  39. /**
  40. * Gets the string representation of the path for debugging
  41. * @return String representation of the path for debugging
  42. */
  43. std::string DebugStr() const;
  44. std::string AsString() const;
  45. std::u16string AsU16Str() const;
  46. std::vector<u8> AsBinary() const;
  47. private:
  48. LowPathType type;
  49. std::vector<u8> binary;
  50. std::string string;
  51. std::u16string u16str;
  52. };
  53. /// Parameters of the archive, as specified in the Create or Format call.
  54. struct ArchiveFormatInfo {
  55. u32_le total_size; ///< The pre-defined size of the archive.
  56. u32_le number_directories; ///< The pre-defined number of directories in the archive.
  57. u32_le number_files; ///< The pre-defined number of files in the archive.
  58. u8 duplicate_data; ///< Whether the archive should duplicate the data.
  59. };
  60. static_assert(std::is_pod<ArchiveFormatInfo>::value, "ArchiveFormatInfo is not POD");
  61. class ArchiveBackend : NonCopyable {
  62. public:
  63. virtual ~ArchiveBackend() {}
  64. /**
  65. * Get a descriptive name for the archive (e.g. "RomFS", "SaveData", etc.)
  66. */
  67. virtual std::string GetName() const = 0;
  68. /**
  69. * Open a file specified by its path, using the specified mode
  70. * @param path Path relative to the archive
  71. * @param mode Mode to open the file with
  72. * @return Opened file, or error code
  73. */
  74. virtual ResultVal<std::unique_ptr<FileBackend>> OpenFile(const Path& path,
  75. const Mode& mode) const = 0;
  76. /**
  77. * Delete a file specified by its path
  78. * @param path Path relative to the archive
  79. * @return Result of the operation
  80. */
  81. virtual ResultCode DeleteFile(const Path& path) const = 0;
  82. /**
  83. * Rename a File specified by its path
  84. * @param src_path Source path relative to the archive
  85. * @param dest_path Destination path relative to the archive
  86. * @return Result of the operation
  87. */
  88. virtual ResultCode RenameFile(const Path& src_path, const Path& dest_path) const = 0;
  89. /**
  90. * Delete a directory specified by its path
  91. * @param path Path relative to the archive
  92. * @return Result of the operation
  93. */
  94. virtual ResultCode DeleteDirectory(const Path& path) const = 0;
  95. /**
  96. * Delete a directory specified by its path and anything under it
  97. * @param path Path relative to the archive
  98. * @return Result of the operation
  99. */
  100. virtual ResultCode DeleteDirectoryRecursively(const Path& path) const = 0;
  101. /**
  102. * Create a file specified by its path
  103. * @param path Path relative to the Archive
  104. * @param size The size of the new file, filled with zeroes
  105. * @return Result of the operation
  106. */
  107. virtual ResultCode CreateFile(const Path& path, u64 size) const = 0;
  108. /**
  109. * Create a directory specified by its path
  110. * @param path Path relative to the archive
  111. * @return Result of the operation
  112. */
  113. virtual ResultCode CreateDirectory(const Path& path) const = 0;
  114. /**
  115. * Rename a Directory specified by its path
  116. * @param src_path Source path relative to the archive
  117. * @param dest_path Destination path relative to the archive
  118. * @return Result of the operation
  119. */
  120. virtual ResultCode RenameDirectory(const Path& src_path, const Path& dest_path) const = 0;
  121. /**
  122. * Open a directory specified by its path
  123. * @param path Path relative to the archive
  124. * @return Opened directory, or error code
  125. */
  126. virtual ResultVal<std::unique_ptr<DirectoryBackend>> OpenDirectory(const Path& path) const = 0;
  127. /**
  128. * Get the free space
  129. * @return The number of free bytes in the archive
  130. */
  131. virtual u64 GetFreeBytes() const = 0;
  132. };
  133. class ArchiveFactory : NonCopyable {
  134. public:
  135. virtual ~ArchiveFactory() {}
  136. /**
  137. * Get a descriptive name for the archive (e.g. "RomFS", "SaveData", etc.)
  138. */
  139. virtual std::string GetName() const = 0;
  140. /**
  141. * Tries to open the archive of this type with the specified path
  142. * @param path Path to the archive
  143. * @return An ArchiveBackend corresponding operating specified archive path.
  144. */
  145. virtual ResultVal<std::unique_ptr<ArchiveBackend>> Open(const Path& path) = 0;
  146. /**
  147. * Deletes the archive contents and then re-creates the base folder
  148. * @param path Path to the archive
  149. * @param format_info Format information for the new archive
  150. * @return ResultCode of the operation, 0 on success
  151. */
  152. virtual ResultCode Format(const Path& path, const FileSys::ArchiveFormatInfo& format_info) = 0;
  153. /**
  154. * Retrieves the format info about the archive with the specified path
  155. * @param path Path to the archive
  156. * @return Format information about the archive or error code
  157. */
  158. virtual ResultVal<ArchiveFormatInfo> GetFormatInfo(const Path& path) const = 0;
  159. };
  160. } // namespace FileSys