archive_backend.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 "common/common_types.h"
  7. #include "common/string_util.h"
  8. #include "common/bit_field.h"
  9. #include "core/file_sys/file_backend.h"
  10. #include "core/file_sys/directory_backend.h"
  11. #include "core/mem_map.h"
  12. #include "core/hle/kernel/kernel.h"
  13. ////////////////////////////////////////////////////////////////////////////////////////////////////
  14. // FileSys namespace
  15. namespace FileSys {
  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():
  33. type(Invalid)
  34. {
  35. }
  36. Path(LowPathType type, u32 size, u32 pointer):
  37. type(type)
  38. {
  39. switch (type) {
  40. case Binary:
  41. {
  42. u8* data = Memory::GetPointer(pointer);
  43. binary = std::vector<u8>(data, data + size);
  44. break;
  45. }
  46. case Char:
  47. {
  48. const char* data = reinterpret_cast<const char*>(Memory::GetPointer(pointer));
  49. string = std::string(data, size - 1); // Data is always null-terminated.
  50. break;
  51. }
  52. case Wchar:
  53. {
  54. const char16_t* data = reinterpret_cast<const char16_t*>(Memory::GetPointer(pointer));
  55. u16str = std::u16string(data, size/2 - 1); // Data is always null-terminated.
  56. break;
  57. }
  58. default:
  59. break;
  60. }
  61. }
  62. LowPathType GetType() const {
  63. return type;
  64. }
  65. /**
  66. * Gets the string representation of the path for debugging
  67. * @return String representation of the path for debugging
  68. */
  69. const std::string DebugStr() const {
  70. switch (GetType()) {
  71. case Invalid:
  72. return "[Invalid]";
  73. case Empty:
  74. return "[Empty]";
  75. case Binary:
  76. {
  77. std::stringstream res;
  78. res << "[Binary: ";
  79. for (unsigned byte : binary)
  80. res << std::hex << std::setw(2) << std::setfill('0') << byte;
  81. res << ']';
  82. return res.str();
  83. }
  84. case Char:
  85. return "[Char: " + AsString() + ']';
  86. case Wchar:
  87. return "[Wchar: " + AsString() + ']';
  88. default:
  89. // TODO(yuriks): Add assert
  90. LOG_ERROR(Service_FS, "LowPathType cannot be converted to string!");
  91. return {};
  92. }
  93. }
  94. const std::string AsString() const {
  95. switch (GetType()) {
  96. case Char:
  97. return string;
  98. case Wchar:
  99. return Common::UTF16ToUTF8(u16str);
  100. case Empty:
  101. return {};
  102. default:
  103. // TODO(yuriks): Add assert
  104. LOG_ERROR(Service_FS, "LowPathType cannot be converted to string!");
  105. return {};
  106. }
  107. }
  108. const std::u16string AsU16Str() const {
  109. switch (GetType()) {
  110. case Char:
  111. return Common::UTF8ToUTF16(string);
  112. case Wchar:
  113. return u16str;
  114. case Empty:
  115. return {};
  116. default:
  117. // TODO(yuriks): Add assert
  118. LOG_ERROR(Service_FS, "LowPathType cannot be converted to u16string!");
  119. return {};
  120. }
  121. }
  122. const std::vector<u8> AsBinary() const {
  123. switch (GetType()) {
  124. case Binary:
  125. return binary;
  126. case Char:
  127. return std::vector<u8>(string.begin(), string.end());
  128. case Wchar:
  129. return std::vector<u8>(u16str.begin(), u16str.end());
  130. case Empty:
  131. return {};
  132. default:
  133. // TODO(yuriks): Add assert
  134. LOG_ERROR(Service_FS, "LowPathType cannot be converted to binary!");
  135. return {};
  136. }
  137. }
  138. private:
  139. LowPathType type;
  140. std::vector<u8> binary;
  141. std::string string;
  142. std::u16string u16str;
  143. };
  144. class ArchiveBackend : NonCopyable {
  145. public:
  146. virtual ~ArchiveBackend() { }
  147. /**
  148. * Get a descriptive name for the archive (e.g. "RomFS", "SaveData", etc.)
  149. */
  150. virtual std::string GetName() const = 0;
  151. /**
  152. * Open a file specified by its path, using the specified mode
  153. * @param path Path relative to the archive
  154. * @param mode Mode to open the file with
  155. * @return Opened file, or nullptr
  156. */
  157. virtual std::unique_ptr<FileBackend> OpenFile(const Path& path, const Mode mode) const = 0;
  158. /**
  159. * Delete a file specified by its path
  160. * @param path Path relative to the archive
  161. * @return Whether the file could be deleted
  162. */
  163. virtual bool DeleteFile(const FileSys::Path& path) const = 0;
  164. /**
  165. * Rename a File specified by its path
  166. * @param src_path Source path relative to the archive
  167. * @param dest_path Destination path relative to the archive
  168. * @return Whether rename succeeded
  169. */
  170. virtual bool RenameFile(const FileSys::Path& src_path, const FileSys::Path& dest_path) const = 0;
  171. /**
  172. * Delete a directory specified by its path
  173. * @param path Path relative to the archive
  174. * @return Whether the directory could be deleted
  175. */
  176. virtual bool DeleteDirectory(const FileSys::Path& path) const = 0;
  177. /**
  178. * Create a directory specified by its path
  179. * @param path Path relative to the archive
  180. * @return Whether the directory could be created
  181. */
  182. virtual bool CreateDirectory(const Path& path) const = 0;
  183. /**
  184. * Rename a Directory specified by its path
  185. * @param src_path Source path relative to the archive
  186. * @param dest_path Destination path relative to the archive
  187. * @return Whether rename succeeded
  188. */
  189. virtual bool RenameDirectory(const FileSys::Path& src_path, const FileSys::Path& dest_path) const = 0;
  190. /**
  191. * Open a directory specified by its path
  192. * @param path Path relative to the archive
  193. * @return Opened directory, or nullptr
  194. */
  195. virtual std::unique_ptr<DirectoryBackend> OpenDirectory(const Path& path) const = 0;
  196. };
  197. } // namespace FileSys