filesystem.h 6.0 KB

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