filesystem.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <utility>
  5. #include "common/assert.h"
  6. #include "common/file_util.h"
  7. #include "core/core.h"
  8. #include "core/file_sys/errors.h"
  9. #include "core/file_sys/savedata_factory.h"
  10. #include "core/file_sys/sdmc_factory.h"
  11. #include "core/file_sys/vfs.h"
  12. #include "core/file_sys/vfs_offset.h"
  13. #include "core/file_sys/vfs_real.h"
  14. #include "core/hle/service/filesystem/filesystem.h"
  15. #include "core/hle/service/filesystem/fsp_srv.h"
  16. namespace Service::FileSystem {
  17. // Size of emulated sd card free space, reported in bytes.
  18. // Just using 32GB because thats reasonable
  19. // TODO(DarkLordZach): Eventually make this configurable in settings.
  20. constexpr u64 EMULATED_SD_REPORTED_SIZE = 32000000000;
  21. static FileSys::VirtualDir GetDirectoryRelativeWrapped(FileSys::VirtualDir base,
  22. const std::string& dir_name) {
  23. if (dir_name.empty() || dir_name == "." || dir_name == "/" || dir_name == "\\")
  24. return base;
  25. return base->GetDirectoryRelative(dir_name);
  26. }
  27. VfsDirectoryServiceWrapper::VfsDirectoryServiceWrapper(FileSys::VirtualDir backing_)
  28. : backing(std::move(backing_)) {}
  29. std::string VfsDirectoryServiceWrapper::GetName() const {
  30. return backing->GetName();
  31. }
  32. ResultCode VfsDirectoryServiceWrapper::CreateFile(const std::string& path, u64 size) const {
  33. auto dir = GetDirectoryRelativeWrapped(backing, FileUtil::GetParentPath(path));
  34. auto file = dir->CreateFile(FileUtil::GetFilename(path));
  35. if (file == nullptr) {
  36. // TODO(DarkLordZach): Find a better error code for this
  37. return ResultCode(-1);
  38. }
  39. if (!file->Resize(size)) {
  40. // TODO(DarkLordZach): Find a better error code for this
  41. return ResultCode(-1);
  42. }
  43. return RESULT_SUCCESS;
  44. }
  45. ResultCode VfsDirectoryServiceWrapper::DeleteFile(const std::string& path) const {
  46. auto dir = GetDirectoryRelativeWrapped(backing, FileUtil::GetParentPath(path));
  47. if (path == "/" || path == "\\") {
  48. // TODO(DarkLordZach): Why do games call this and what should it do? Works as is but...
  49. return RESULT_SUCCESS;
  50. }
  51. if (dir->GetFile(FileUtil::GetFilename(path)) == nullptr)
  52. return FileSys::ERROR_PATH_NOT_FOUND;
  53. if (!backing->DeleteFile(FileUtil::GetFilename(path))) {
  54. // TODO(DarkLordZach): Find a better error code for this
  55. return ResultCode(-1);
  56. }
  57. return RESULT_SUCCESS;
  58. }
  59. ResultCode VfsDirectoryServiceWrapper::CreateDirectory(const std::string& path) const {
  60. auto dir = GetDirectoryRelativeWrapped(backing, FileUtil::GetParentPath(path));
  61. if (dir == nullptr && FileUtil::GetFilename(FileUtil::GetParentPath(path)).empty())
  62. dir = backing;
  63. auto new_dir = dir->CreateSubdirectory(FileUtil::GetFilename(path));
  64. if (new_dir == nullptr) {
  65. // TODO(DarkLordZach): Find a better error code for this
  66. return ResultCode(-1);
  67. }
  68. return RESULT_SUCCESS;
  69. }
  70. ResultCode VfsDirectoryServiceWrapper::DeleteDirectory(const std::string& path) const {
  71. auto dir = GetDirectoryRelativeWrapped(backing, FileUtil::GetParentPath(path));
  72. if (!dir->DeleteSubdirectory(FileUtil::GetFilename(path))) {
  73. // TODO(DarkLordZach): Find a better error code for this
  74. return ResultCode(-1);
  75. }
  76. return RESULT_SUCCESS;
  77. }
  78. ResultCode VfsDirectoryServiceWrapper::DeleteDirectoryRecursively(const std::string& path) const {
  79. auto dir = GetDirectoryRelativeWrapped(backing, FileUtil::GetParentPath(path));
  80. if (!dir->DeleteSubdirectoryRecursive(FileUtil::GetFilename(path))) {
  81. // TODO(DarkLordZach): Find a better error code for this
  82. return ResultCode(-1);
  83. }
  84. return RESULT_SUCCESS;
  85. }
  86. ResultCode VfsDirectoryServiceWrapper::RenameFile(const std::string& src_path,
  87. const std::string& dest_path) const {
  88. auto src = backing->GetFileRelative(src_path);
  89. if (FileUtil::GetParentPath(src_path) == FileUtil::GetParentPath(dest_path)) {
  90. // Use more-optimized vfs implementation rename.
  91. if (src == nullptr)
  92. return FileSys::ERROR_PATH_NOT_FOUND;
  93. if (!src->Rename(FileUtil::GetFilename(dest_path))) {
  94. // TODO(DarkLordZach): Find a better error code for this
  95. return ResultCode(-1);
  96. }
  97. return RESULT_SUCCESS;
  98. }
  99. // Move by hand -- TODO(DarkLordZach): Optimize
  100. auto c_res = CreateFile(dest_path, src->GetSize());
  101. if (c_res != RESULT_SUCCESS)
  102. return c_res;
  103. auto dest = backing->GetFileRelative(dest_path);
  104. ASSERT_MSG(dest != nullptr, "Newly created file with success cannot be found.");
  105. ASSERT_MSG(dest->WriteBytes(src->ReadAllBytes()) == src->GetSize(),
  106. "Could not write all of the bytes but everything else has succeded.");
  107. if (!src->GetContainingDirectory()->DeleteFile(FileUtil::GetFilename(src_path))) {
  108. // TODO(DarkLordZach): Find a better error code for this
  109. return ResultCode(-1);
  110. }
  111. return RESULT_SUCCESS;
  112. }
  113. ResultCode VfsDirectoryServiceWrapper::RenameDirectory(const std::string& src_path,
  114. const std::string& dest_path) const {
  115. auto src = GetDirectoryRelativeWrapped(backing, src_path);
  116. if (FileUtil::GetParentPath(src_path) == FileUtil::GetParentPath(dest_path)) {
  117. // Use more-optimized vfs implementation rename.
  118. if (src == nullptr)
  119. return FileSys::ERROR_PATH_NOT_FOUND;
  120. if (!src->Rename(FileUtil::GetFilename(dest_path))) {
  121. // TODO(DarkLordZach): Find a better error code for this
  122. return ResultCode(-1);
  123. }
  124. return RESULT_SUCCESS;
  125. }
  126. // TODO(DarkLordZach): Implement renaming across the tree (move).
  127. ASSERT_MSG(false,
  128. "Could not rename directory with path \"{}\" to new path \"{}\" because parent dirs "
  129. "don't match -- UNIMPLEMENTED",
  130. src_path, dest_path);
  131. // TODO(DarkLordZach): Find a better error code for this
  132. return ResultCode(-1);
  133. }
  134. ResultVal<FileSys::VirtualFile> VfsDirectoryServiceWrapper::OpenFile(const std::string& path,
  135. FileSys::Mode mode) const {
  136. auto npath = path;
  137. while (npath.size() > 0 && (npath[0] == '/' || npath[0] == '\\'))
  138. npath = npath.substr(1);
  139. auto file = backing->GetFileRelative(npath);
  140. if (file == nullptr)
  141. return FileSys::ERROR_PATH_NOT_FOUND;
  142. if (mode == FileSys::Mode::Append) {
  143. return MakeResult<FileSys::VirtualFile>(
  144. std::make_shared<FileSys::OffsetVfsFile>(file, 0, file->GetSize()));
  145. }
  146. return MakeResult<FileSys::VirtualFile>(file);
  147. }
  148. ResultVal<FileSys::VirtualDir> VfsDirectoryServiceWrapper::OpenDirectory(const std::string& path) {
  149. auto dir = GetDirectoryRelativeWrapped(backing, path);
  150. if (dir == nullptr) {
  151. // TODO(DarkLordZach): Find a better error code for this
  152. return ResultCode(-1);
  153. }
  154. return MakeResult(dir);
  155. }
  156. u64 VfsDirectoryServiceWrapper::GetFreeSpaceSize() const {
  157. if (backing->IsWritable())
  158. return EMULATED_SD_REPORTED_SIZE;
  159. return 0;
  160. }
  161. ResultVal<FileSys::EntryType> VfsDirectoryServiceWrapper::GetEntryType(
  162. const std::string& path) const {
  163. auto dir = GetDirectoryRelativeWrapped(backing, FileUtil::GetParentPath(path));
  164. if (dir == nullptr)
  165. return FileSys::ERROR_PATH_NOT_FOUND;
  166. auto filename = FileUtil::GetFilename(path);
  167. // TODO(Subv): Some games use the '/' path, find out what this means.
  168. if (filename.empty())
  169. return MakeResult(FileSys::EntryType::Directory);
  170. if (dir->GetFile(filename) != nullptr)
  171. return MakeResult(FileSys::EntryType::File);
  172. if (dir->GetSubdirectory(filename) != nullptr)
  173. return MakeResult(FileSys::EntryType::Directory);
  174. return FileSys::ERROR_PATH_NOT_FOUND;
  175. }
  176. /**
  177. * Map of registered file systems, identified by type. Once an file system is registered here, it
  178. * is never removed until UnregisterFileSystems is called.
  179. */
  180. static std::unique_ptr<FileSys::RomFSFactory> romfs_factory;
  181. static std::unique_ptr<FileSys::SaveDataFactory> save_data_factory;
  182. static std::unique_ptr<FileSys::SDMCFactory> sdmc_factory;
  183. ResultCode RegisterRomFS(std::unique_ptr<FileSys::RomFSFactory>&& factory) {
  184. ASSERT_MSG(romfs_factory == nullptr, "Tried to register a second RomFS");
  185. romfs_factory = std::move(factory);
  186. LOG_DEBUG(Service_FS, "Registered RomFS");
  187. return RESULT_SUCCESS;
  188. }
  189. ResultCode RegisterSaveData(std::unique_ptr<FileSys::SaveDataFactory>&& factory) {
  190. ASSERT_MSG(romfs_factory == nullptr, "Tried to register a second save data");
  191. save_data_factory = std::move(factory);
  192. LOG_DEBUG(Service_FS, "Registered save data");
  193. return RESULT_SUCCESS;
  194. }
  195. ResultCode RegisterSDMC(std::unique_ptr<FileSys::SDMCFactory>&& factory) {
  196. ASSERT_MSG(sdmc_factory == nullptr, "Tried to register a second SDMC");
  197. sdmc_factory = std::move(factory);
  198. LOG_DEBUG(Service_FS, "Registered SDMC");
  199. return RESULT_SUCCESS;
  200. }
  201. ResultVal<FileSys::VirtualFile> OpenRomFS(u64 title_id) {
  202. LOG_TRACE(Service_FS, "Opening RomFS for title_id={:016X}", title_id);
  203. if (romfs_factory == nullptr) {
  204. // TODO(bunnei): Find a better error code for this
  205. return ResultCode(-1);
  206. }
  207. return romfs_factory->Open(title_id);
  208. }
  209. ResultVal<FileSys::VirtualDir> OpenSaveData(FileSys::SaveDataSpaceId space,
  210. FileSys::SaveDataDescriptor save_struct) {
  211. LOG_TRACE(Service_FS, "Opening Save Data for space_id={:01X}, save_struct={}",
  212. static_cast<u8>(space), save_struct.DebugInfo());
  213. if (save_data_factory == nullptr) {
  214. return ResultCode(ErrorModule::FS, FileSys::ErrCodes::TitleNotFound);
  215. }
  216. return save_data_factory->Open(space, save_struct);
  217. }
  218. ResultVal<FileSys::VirtualDir> OpenSDMC() {
  219. LOG_TRACE(Service_FS, "Opening SDMC");
  220. if (sdmc_factory == nullptr) {
  221. return ResultCode(ErrorModule::FS, FileSys::ErrCodes::SdCardNotFound);
  222. }
  223. return sdmc_factory->Open();
  224. }
  225. void RegisterFileSystems() {
  226. romfs_factory = nullptr;
  227. save_data_factory = nullptr;
  228. sdmc_factory = nullptr;
  229. auto nand_directory = std::make_shared<FileSys::RealVfsDirectory>(
  230. FileUtil::GetUserPath(D_NAND_IDX), FileSys::Mode::Write);
  231. auto sd_directory = std::make_shared<FileSys::RealVfsDirectory>(
  232. FileUtil::GetUserPath(D_SDMC_IDX), FileSys::Mode::Write);
  233. auto savedata = std::make_unique<FileSys::SaveDataFactory>(std::move(nand_directory));
  234. save_data_factory = std::move(savedata);
  235. auto sdcard = std::make_unique<FileSys::SDMCFactory>(std::move(sd_directory));
  236. sdmc_factory = std::move(sdcard);
  237. }
  238. void InstallInterfaces(SM::ServiceManager& service_manager) {
  239. RegisterFileSystems();
  240. std::make_shared<FSP_SRV>()->InstallAsService(service_manager);
  241. }
  242. } // namespace Service::FileSystem