archive_extsavedata.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <memory>
  6. #include <vector>
  7. #include "common/common_types.h"
  8. #include "common/file_util.h"
  9. #include "common/logging/log.h"
  10. #include "common/string_util.h"
  11. #include "core/file_sys/archive_extsavedata.h"
  12. #include "core/file_sys/disk_archive.h"
  13. #include "core/file_sys/errors.h"
  14. #include "core/file_sys/path_parser.h"
  15. #include "core/file_sys/savedata_archive.h"
  16. #include "core/hle/service/fs/archive.h"
  17. ////////////////////////////////////////////////////////////////////////////////////////////////////
  18. // FileSys namespace
  19. namespace FileSys {
  20. /**
  21. * A modified version of DiskFile for fixed-size file used by ExtSaveData
  22. * The file size can't be changed by SetSize or Write.
  23. */
  24. class FixSizeDiskFile : public DiskFile {
  25. public:
  26. FixSizeDiskFile(FileUtil::IOFile&& file, const Mode& mode) : DiskFile(std::move(file), mode) {
  27. size = GetSize();
  28. }
  29. bool SetSize(u64 size) const override {
  30. return false;
  31. }
  32. ResultVal<size_t> Write(u64 offset, size_t length, bool flush,
  33. const u8* buffer) const override {
  34. if (offset > size) {
  35. return ResultCode(ErrorDescription::FS_WriteBeyondEnd, ErrorModule::FS,
  36. ErrorSummary::InvalidArgument, ErrorLevel::Usage);
  37. } else if (offset == size) {
  38. return MakeResult<size_t>(0);
  39. }
  40. if (offset + length > size) {
  41. length = size - offset;
  42. }
  43. return DiskFile::Write(offset, length, flush, buffer);
  44. }
  45. private:
  46. u64 size{};
  47. };
  48. /**
  49. * Archive backend for general extsave data archive type.
  50. * The behaviour of ExtSaveDataArchive is almost the same as SaveDataArchive, except for
  51. * - file size can't be changed once created (thus creating zero-size file and openning with create
  52. * flag are prohibited);
  53. * - always open a file with read+write permission.
  54. */
  55. class ExtSaveDataArchive : public SaveDataArchive {
  56. public:
  57. explicit ExtSaveDataArchive(const std::string& mount_point) : SaveDataArchive(mount_point) {}
  58. std::string GetName() const override {
  59. return "ExtSaveDataArchive: " + mount_point;
  60. }
  61. ResultVal<std::unique_ptr<FileBackend>> OpenFile(const Path& path,
  62. const Mode& mode) const override {
  63. LOG_DEBUG(Service_FS, "called path=%s mode=%01X", path.DebugStr().c_str(), mode.hex);
  64. const PathParser path_parser(path);
  65. if (!path_parser.IsValid()) {
  66. LOG_ERROR(Service_FS, "Invalid path %s", path.DebugStr().c_str());
  67. return ERROR_INVALID_PATH;
  68. }
  69. if (mode.hex == 0) {
  70. LOG_ERROR(Service_FS, "Empty open mode");
  71. return ERROR_UNSUPPORTED_OPEN_FLAGS;
  72. }
  73. if (mode.create_flag) {
  74. LOG_ERROR(Service_FS, "Create flag is not supported");
  75. return ERROR_UNSUPPORTED_OPEN_FLAGS;
  76. }
  77. const auto full_path = path_parser.BuildHostPath(mount_point);
  78. switch (path_parser.GetHostStatus(mount_point)) {
  79. case PathParser::InvalidMountPoint:
  80. LOG_CRITICAL(Service_FS, "(unreachable) Invalid mount point %s", mount_point.c_str());
  81. return ERROR_FILE_NOT_FOUND;
  82. case PathParser::PathNotFound:
  83. LOG_ERROR(Service_FS, "Path not found %s", full_path.c_str());
  84. return ERROR_PATH_NOT_FOUND;
  85. case PathParser::FileInPath:
  86. case PathParser::DirectoryFound:
  87. LOG_ERROR(Service_FS, "Unexpected file or directory in %s", full_path.c_str());
  88. return ERROR_UNEXPECTED_FILE_OR_DIRECTORY;
  89. case PathParser::NotFound:
  90. LOG_ERROR(Service_FS, "%s not found", full_path.c_str());
  91. return ERROR_FILE_NOT_FOUND;
  92. case PathParser::FileFound:
  93. break; // Expected 'success' case
  94. }
  95. FileUtil::IOFile file(full_path, "r+b");
  96. if (!file.IsOpen()) {
  97. LOG_CRITICAL(Service_FS, "(unreachable) Unknown error opening %s", full_path.c_str());
  98. return ERROR_FILE_NOT_FOUND;
  99. }
  100. Mode rwmode;
  101. rwmode.write_flag.Assign(1);
  102. rwmode.read_flag.Assign(1);
  103. auto disk_file = std::make_unique<FixSizeDiskFile>(std::move(file), rwmode);
  104. return MakeResult<std::unique_ptr<FileBackend>>(std::move(disk_file));
  105. }
  106. ResultCode CreateFile(const Path& path, u64 size) const override {
  107. if (size == 0) {
  108. LOG_ERROR(Service_FS, "Zero-size file is not supported");
  109. return ERROR_UNSUPPORTED_OPEN_FLAGS;
  110. }
  111. return SaveDataArchive::CreateFile(path, size);
  112. }
  113. };
  114. std::string GetExtSaveDataPath(const std::string& mount_point, const Path& path) {
  115. std::vector<u8> vec_data = path.AsBinary();
  116. const u32* data = reinterpret_cast<const u32*>(vec_data.data());
  117. u32 save_low = data[1];
  118. u32 save_high = data[2];
  119. return Common::StringFromFormat("%s%08X/%08X/", mount_point.c_str(), save_high, save_low);
  120. }
  121. std::string GetExtDataContainerPath(const std::string& mount_point, bool shared) {
  122. if (shared)
  123. return Common::StringFromFormat("%sdata/%s/extdata/", mount_point.c_str(), SYSTEM_ID);
  124. return Common::StringFromFormat("%sNintendo 3DS/%s/%s/extdata/", mount_point.c_str(), SYSTEM_ID,
  125. SDCARD_ID);
  126. }
  127. Path ConstructExtDataBinaryPath(u32 media_type, u32 high, u32 low) {
  128. std::vector<u8> binary_path;
  129. binary_path.reserve(12);
  130. // Append each word byte by byte
  131. // The first word is the media type
  132. for (unsigned i = 0; i < 4; ++i)
  133. binary_path.push_back((media_type >> (8 * i)) & 0xFF);
  134. // Next is the low word
  135. for (unsigned i = 0; i < 4; ++i)
  136. binary_path.push_back((low >> (8 * i)) & 0xFF);
  137. // Next is the high word
  138. for (unsigned i = 0; i < 4; ++i)
  139. binary_path.push_back((high >> (8 * i)) & 0xFF);
  140. return {binary_path};
  141. }
  142. ArchiveFactory_ExtSaveData::ArchiveFactory_ExtSaveData(const std::string& mount_location,
  143. bool shared)
  144. : shared(shared), mount_point(GetExtDataContainerPath(mount_location, shared)) {
  145. LOG_INFO(Service_FS, "Directory %s set as base for ExtSaveData.", mount_point.c_str());
  146. }
  147. bool ArchiveFactory_ExtSaveData::Initialize() {
  148. if (!FileUtil::CreateFullPath(mount_point)) {
  149. LOG_ERROR(Service_FS, "Unable to create ExtSaveData base path.");
  150. return false;
  151. }
  152. return true;
  153. }
  154. ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_ExtSaveData::Open(const Path& path) {
  155. std::string fullpath = GetExtSaveDataPath(mount_point, path) + "user/";
  156. if (!FileUtil::Exists(fullpath)) {
  157. // TODO(Subv): Verify the archive behavior of SharedExtSaveData compared to ExtSaveData.
  158. // ExtSaveData seems to return FS_NotFound (120) when the archive doesn't exist.
  159. if (!shared) {
  160. return ResultCode(ErrorDescription::FS_NotFound, ErrorModule::FS,
  161. ErrorSummary::InvalidState, ErrorLevel::Status);
  162. } else {
  163. return ResultCode(ErrorDescription::FS_NotFormatted, ErrorModule::FS,
  164. ErrorSummary::InvalidState, ErrorLevel::Status);
  165. }
  166. }
  167. auto archive = std::make_unique<ExtSaveDataArchive>(fullpath);
  168. return MakeResult<std::unique_ptr<ArchiveBackend>>(std::move(archive));
  169. }
  170. ResultCode ArchiveFactory_ExtSaveData::Format(const Path& path,
  171. const FileSys::ArchiveFormatInfo& format_info) {
  172. // These folders are always created with the ExtSaveData
  173. std::string user_path = GetExtSaveDataPath(mount_point, path) + "user/";
  174. std::string boss_path = GetExtSaveDataPath(mount_point, path) + "boss/";
  175. FileUtil::CreateFullPath(user_path);
  176. FileUtil::CreateFullPath(boss_path);
  177. // Write the format metadata
  178. std::string metadata_path = GetExtSaveDataPath(mount_point, path) + "metadata";
  179. FileUtil::IOFile file(metadata_path, "wb");
  180. if (!file.IsOpen()) {
  181. // TODO(Subv): Find the correct error code
  182. return ResultCode(-1);
  183. }
  184. file.WriteBytes(&format_info, sizeof(format_info));
  185. return RESULT_SUCCESS;
  186. }
  187. ResultVal<ArchiveFormatInfo> ArchiveFactory_ExtSaveData::GetFormatInfo(const Path& path) const {
  188. std::string metadata_path = GetExtSaveDataPath(mount_point, path) + "metadata";
  189. FileUtil::IOFile file(metadata_path, "rb");
  190. if (!file.IsOpen()) {
  191. LOG_ERROR(Service_FS, "Could not open metadata information for archive");
  192. // TODO(Subv): Verify error code
  193. return ResultCode(ErrorDescription::FS_NotFormatted, ErrorModule::FS,
  194. ErrorSummary::InvalidState, ErrorLevel::Status);
  195. }
  196. ArchiveFormatInfo info = {};
  197. file.ReadBytes(&info, sizeof(info));
  198. return MakeResult<ArchiveFormatInfo>(info);
  199. }
  200. void ArchiveFactory_ExtSaveData::WriteIcon(const Path& path, const u8* icon_data,
  201. size_t icon_size) {
  202. std::string game_path = FileSys::GetExtSaveDataPath(GetMountPoint(), path);
  203. FileUtil::IOFile icon_file(game_path + "icon", "wb");
  204. icon_file.WriteBytes(icon_data, icon_size);
  205. }
  206. } // namespace FileSys