archive_extsavedata.cpp 8.7 KB

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