archive_backend.h 7.0 KB

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