filesystem.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 "common/common_types.h"
  7. #include "core/file_sys/filesystem.h"
  8. #include "core/file_sys/vfs.h"
  9. #include "core/hle/result.h"
  10. namespace FileSys {
  11. class FileSystemBackend;
  12. class FileSystemFactory;
  13. class Path;
  14. } // namespace FileSys
  15. namespace Service {
  16. namespace SM {
  17. class ServiceManager;
  18. } // namespace SM
  19. namespace FileSystem {
  20. /// Supported FileSystem types
  21. enum class Type {
  22. RomFS = 1,
  23. SaveData = 2,
  24. SDMC = 3,
  25. };
  26. // A class that wraps a VfsDirectory with methods that return ResultVal and ResultCode instead of
  27. // pointers and booleans. This makes using a VfsDirectory with switch services much easier and
  28. // avoids repetitive code.
  29. class VfsDirectoryServiceWrapper {
  30. public:
  31. explicit VfsDirectoryServiceWrapper(FileSys::VirtualDir backing);
  32. /**
  33. * Get a descriptive name for the archive (e.g. "RomFS", "SaveData", etc.)
  34. */
  35. std::string GetName() const;
  36. /**
  37. * Create a file specified by its path
  38. * @param path Path relative to the Archive
  39. * @param size The size of the new file, filled with zeroes
  40. * @return Result of the operation
  41. */
  42. ResultCode CreateFile(const std::string& path, u64 size) const;
  43. /**
  44. * Delete a file specified by its path
  45. * @param path Path relative to the archive
  46. * @return Result of the operation
  47. */
  48. ResultCode DeleteFile(const std::string& path) const;
  49. /**
  50. * Create a directory specified by its path
  51. * @param path Path relative to the archive
  52. * @return Result of the operation
  53. */
  54. ResultCode CreateDirectory(const std::string& path) const;
  55. /**
  56. * Delete a directory specified by its path
  57. * @param path Path relative to the archive
  58. * @return Result of the operation
  59. */
  60. ResultCode DeleteDirectory(const std::string& path) const;
  61. /**
  62. * Delete a directory specified by its path and anything under it
  63. * @param path Path relative to the archive
  64. * @return Result of the operation
  65. */
  66. ResultCode DeleteDirectoryRecursively(const std::string& path) const;
  67. /**
  68. * Rename a File specified by its path
  69. * @param src_path Source path relative to the archive
  70. * @param dest_path Destination path relative to the archive
  71. * @return Result of the operation
  72. */
  73. ResultCode RenameFile(const std::string& src_path, const std::string& dest_path) const;
  74. /**
  75. * Rename a Directory specified by its path
  76. * @param src_path Source path relative to the archive
  77. * @param dest_path Destination path relative to the archive
  78. * @return Result of the operation
  79. */
  80. ResultCode RenameDirectory(const std::string& src_path, const std::string& dest_path) const;
  81. /**
  82. * Open a file specified by its path, using the specified mode
  83. * @param path Path relative to the archive
  84. * @param mode Mode to open the file with
  85. * @return Opened file, or error code
  86. */
  87. ResultVal<FileSys::VirtualFile> OpenFile(const std::string& path, FileSys::Mode mode) const;
  88. /**
  89. * Open a directory specified by its path
  90. * @param path Path relative to the archive
  91. * @return Opened directory, or error code
  92. */
  93. ResultVal<FileSys::VirtualDir> OpenDirectory(const std::string& path);
  94. /**
  95. * Get the free space
  96. * @return The number of free bytes in the archive
  97. */
  98. u64 GetFreeSpaceSize() const;
  99. /**
  100. * Get the type of the specified path
  101. * @return The type of the specified path or error code
  102. */
  103. ResultVal<FileSys::EntryType> GetEntryType(const std::string& path) const;
  104. private:
  105. FileSys::VirtualDir backing;
  106. };
  107. // A class that deferres the creation of a filesystem until a later time.
  108. // This is useful if construction depends on a variable not known when the filesystem is registered.
  109. // Construct this with a filesystem (VirtualDir) to avoid the deferrence feature or override the
  110. // CreateFilesystem method which will be called on first use.
  111. struct DeferredFilesystem {
  112. DeferredFilesystem() = default;
  113. explicit DeferredFilesystem(FileSys::VirtualDir vfs_directory) : fs(std::move(vfs_directory)) {}
  114. FileSys::VirtualDir Get() {
  115. if (fs == nullptr)
  116. fs = CreateFilesystem();
  117. return fs;
  118. }
  119. virtual ~DeferredFilesystem() = default;
  120. protected:
  121. virtual FileSys::VirtualDir CreateFilesystem() {
  122. return fs;
  123. }
  124. private:
  125. FileSys::VirtualDir fs;
  126. };
  127. /**
  128. * Registers a FileSystem, instances of which can later be opened using its IdCode.
  129. * @param factory FileSystem backend interface to use
  130. * @param type Type used to access this type of FileSystem
  131. */
  132. ResultCode RegisterFileSystem(std::unique_ptr<DeferredFilesystem>&& fs, Type type);
  133. ResultCode RegisterRomFS(FileSys::VirtualFile fs);
  134. /**
  135. * Opens a file system
  136. * @param type Type of the file system to open
  137. * @param path Path to the file system, used with Binary paths
  138. * @return FileSys::FileSystemBackend interface to the file system
  139. */
  140. ResultVal<FileSys::VirtualDir> OpenFileSystem(Type type);
  141. ResultVal<FileSys::VirtualFile> OpenRomFS();
  142. /**
  143. * Formats a file system
  144. * @param type Type of the file system to format
  145. * @return ResultCode of the operation
  146. */
  147. ResultCode FormatFileSystem(Type type);
  148. /// Registers all Filesystem services with the specified service manager.
  149. void InstallInterfaces(SM::ServiceManager& service_manager);
  150. } // namespace FileSystem
  151. } // namespace Service