disk_archive.cpp 9.6 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. #include <algorithm>
  5. #include <cstdio>
  6. #include <memory>
  7. #include "common/common_types.h"
  8. #include "common/file_util.h"
  9. #include "common/logging/log.h"
  10. #include "core/file_sys/disk_archive.h"
  11. ////////////////////////////////////////////////////////////////////////////////////////////////////
  12. // FileSys namespace
  13. namespace FileSys {
  14. ResultVal<std::unique_ptr<FileBackend>> DiskArchive::OpenFile(const Path& path,
  15. const Mode& mode) const {
  16. LOG_DEBUG(Service_FS, "called path=%s mode=%01X", path.DebugStr().c_str(), mode.hex);
  17. auto full_path = mount_point + path.AsString();
  18. if (FileUtil::IsDirectory(full_path))
  19. return ResultCode(ErrorDescription::FS_NotAFile, ErrorModule::FS, ErrorSummary::Canceled,
  20. ErrorLevel::Status);
  21. // Specifying only the Create flag is invalid
  22. if (mode.create_flag && !mode.read_flag && !mode.write_flag) {
  23. return ResultCode(ErrorDescription::FS_InvalidOpenFlags, ErrorModule::FS,
  24. ErrorSummary::Canceled, ErrorLevel::Status);
  25. }
  26. if (!FileUtil::Exists(full_path)) {
  27. if (!mode.create_flag) {
  28. LOG_ERROR(Service_FS, "Non-existing file %s can't be open without mode create.",
  29. full_path.c_str());
  30. return ResultCode(ErrorDescription::FS_NotFound, ErrorModule::FS,
  31. ErrorSummary::NotFound, ErrorLevel::Status);
  32. } else {
  33. // Create the file
  34. FileUtil::CreateEmptyFile(full_path);
  35. }
  36. }
  37. std::string mode_string = "";
  38. if (mode.write_flag)
  39. mode_string += "r+"; // Files opened with Write access can be read from
  40. else if (mode.read_flag)
  41. mode_string += "r";
  42. // Open the file in binary mode, to avoid problems with CR/LF on Windows systems
  43. mode_string += "b";
  44. FileUtil::IOFile file(full_path, mode_string.c_str());
  45. if (!file.IsOpen())
  46. return ResultCode(ErrorDescription::FS_NotFound, ErrorModule::FS, ErrorSummary::NotFound,
  47. ErrorLevel::Status);
  48. auto disk_file = std::make_unique<DiskFile>(std::move(file), mode);
  49. return MakeResult<std::unique_ptr<FileBackend>>(std::move(disk_file));
  50. }
  51. ResultCode DiskArchive::DeleteFile(const Path& path) const {
  52. std::string file_path = mount_point + path.AsString();
  53. if (FileUtil::IsDirectory(file_path))
  54. return ResultCode(ErrorDescription::FS_NotAFile, ErrorModule::FS, ErrorSummary::Canceled,
  55. ErrorLevel::Status);
  56. if (!FileUtil::Exists(file_path))
  57. return ResultCode(ErrorDescription::FS_NotFound, ErrorModule::FS, ErrorSummary::NotFound,
  58. ErrorLevel::Status);
  59. if (FileUtil::Delete(file_path))
  60. return RESULT_SUCCESS;
  61. return ResultCode(ErrorDescription::FS_NotAFile, ErrorModule::FS, ErrorSummary::Canceled,
  62. ErrorLevel::Status);
  63. }
  64. ResultCode DiskArchive::RenameFile(const Path& src_path, const Path& dest_path) const {
  65. if (FileUtil::Rename(mount_point + src_path.AsString(), mount_point + dest_path.AsString()))
  66. return RESULT_SUCCESS;
  67. // TODO(yuriks): This code probably isn't right, it'll return a Status even if the file didn't
  68. // exist or similar. Verify.
  69. return ResultCode(ErrorDescription::NoData, ErrorModule::FS, // TODO: verify description
  70. ErrorSummary::NothingHappened, ErrorLevel::Status);
  71. }
  72. ResultCode DiskArchive::DeleteDirectory(const Path& path) const {
  73. if (FileUtil::DeleteDir(mount_point + path.AsString()))
  74. return RESULT_SUCCESS;
  75. return ResultCode(ErrorDescription::NoData, ErrorModule::FS, // TODO: verify description
  76. ErrorSummary::Canceled, ErrorLevel::Status);
  77. }
  78. ResultCode DiskArchive::DeleteDirectoryRecursively(const Path& path) const {
  79. if (FileUtil::DeleteDirRecursively(mount_point + path.AsString()))
  80. return RESULT_SUCCESS;
  81. return ResultCode(ErrorDescription::NoData, ErrorModule::FS, // TODO: verify description
  82. ErrorSummary::Canceled, ErrorLevel::Status);
  83. }
  84. ResultCode DiskArchive::CreateFile(const FileSys::Path& path, u64 size) const {
  85. std::string full_path = mount_point + path.AsString();
  86. if (FileUtil::IsDirectory(full_path))
  87. return ResultCode(ErrorDescription::FS_NotAFile, ErrorModule::FS, ErrorSummary::Canceled,
  88. ErrorLevel::Status);
  89. if (FileUtil::Exists(full_path))
  90. return ResultCode(ErrorDescription::FS_AlreadyExists, ErrorModule::FS,
  91. ErrorSummary::NothingHappened, ErrorLevel::Status);
  92. if (size == 0) {
  93. FileUtil::CreateEmptyFile(full_path);
  94. return RESULT_SUCCESS;
  95. }
  96. FileUtil::IOFile file(full_path, "wb");
  97. // Creates a sparse file (or a normal file on filesystems without the concept of sparse files)
  98. // We do this by seeking to the right size, then writing a single null byte.
  99. if (file.Seek(size - 1, SEEK_SET) && file.WriteBytes("", 1) == 1)
  100. return RESULT_SUCCESS;
  101. return ResultCode(ErrorDescription::TooLarge, ErrorModule::FS, ErrorSummary::OutOfResource,
  102. ErrorLevel::Info);
  103. }
  104. ResultCode DiskArchive::CreateDirectory(const Path& path) const {
  105. if (FileUtil::CreateDir(mount_point + path.AsString()))
  106. return RESULT_SUCCESS;
  107. return ResultCode(ErrorDescription::NoData, ErrorModule::FS, // TODO: verify description
  108. ErrorSummary::Canceled, ErrorLevel::Status);
  109. }
  110. ResultCode DiskArchive::RenameDirectory(const Path& src_path, const Path& dest_path) const {
  111. if (FileUtil::Rename(mount_point + src_path.AsString(), mount_point + dest_path.AsString()))
  112. return RESULT_SUCCESS;
  113. // TODO(yuriks): This code probably isn't right, it'll return a Status even if the file didn't
  114. // exist or similar. Verify.
  115. return ResultCode(ErrorDescription::NoData, ErrorModule::FS, // TODO: verify description
  116. ErrorSummary::NothingHappened, ErrorLevel::Status);
  117. }
  118. ResultVal<std::unique_ptr<DirectoryBackend>> DiskArchive::OpenDirectory(const Path& path) const {
  119. LOG_DEBUG(Service_FS, "called path=%s", path.DebugStr().c_str());
  120. auto full_path = mount_point + path.AsString();
  121. if (!FileUtil::IsDirectory(full_path))
  122. return ResultCode(ErrorDescription::FS_NotFound, ErrorModule::FS, ErrorSummary::NotFound,
  123. ErrorLevel::Permanent);
  124. auto directory = std::make_unique<DiskDirectory>(full_path);
  125. return MakeResult<std::unique_ptr<DirectoryBackend>>(std::move(directory));
  126. }
  127. u64 DiskArchive::GetFreeBytes() const {
  128. // TODO: Stubbed to return 1GiB
  129. return 1024 * 1024 * 1024;
  130. }
  131. ////////////////////////////////////////////////////////////////////////////////////////////////////
  132. ResultVal<size_t> DiskFile::Read(const u64 offset, const size_t length, u8* buffer) const {
  133. if (!mode.read_flag && !mode.write_flag)
  134. return ResultCode(ErrorDescription::FS_InvalidOpenFlags, ErrorModule::FS,
  135. ErrorSummary::Canceled, ErrorLevel::Status);
  136. file->Seek(offset, SEEK_SET);
  137. return MakeResult<size_t>(file->ReadBytes(buffer, length));
  138. }
  139. ResultVal<size_t> DiskFile::Write(const u64 offset, const size_t length, const bool flush,
  140. const u8* buffer) const {
  141. if (!mode.write_flag)
  142. return ResultCode(ErrorDescription::FS_InvalidOpenFlags, ErrorModule::FS,
  143. ErrorSummary::Canceled, ErrorLevel::Status);
  144. file->Seek(offset, SEEK_SET);
  145. size_t written = file->WriteBytes(buffer, length);
  146. if (flush)
  147. file->Flush();
  148. return MakeResult<size_t>(written);
  149. }
  150. u64 DiskFile::GetSize() const {
  151. return file->GetSize();
  152. }
  153. bool DiskFile::SetSize(const u64 size) const {
  154. file->Resize(size);
  155. file->Flush();
  156. return true;
  157. }
  158. bool DiskFile::Close() const {
  159. return file->Close();
  160. }
  161. ////////////////////////////////////////////////////////////////////////////////////////////////////
  162. DiskDirectory::DiskDirectory(const std::string& path) : directory() {
  163. unsigned size = FileUtil::ScanDirectoryTree(path, directory);
  164. directory.size = size;
  165. directory.isDirectory = true;
  166. children_iterator = directory.children.begin();
  167. }
  168. u32 DiskDirectory::Read(const u32 count, Entry* entries) {
  169. u32 entries_read = 0;
  170. while (entries_read < count && children_iterator != directory.children.cend()) {
  171. const FileUtil::FSTEntry& file = *children_iterator;
  172. const std::string& filename = file.virtualName;
  173. Entry& entry = entries[entries_read];
  174. LOG_TRACE(Service_FS, "File %s: size=%llu dir=%d", filename.c_str(), file.size,
  175. file.isDirectory);
  176. // TODO(Link Mauve): use a proper conversion to UTF-16.
  177. for (size_t j = 0; j < FILENAME_LENGTH; ++j) {
  178. entry.filename[j] = filename[j];
  179. if (!filename[j])
  180. break;
  181. }
  182. FileUtil::SplitFilename83(filename, entry.short_name, entry.extension);
  183. entry.is_directory = file.isDirectory;
  184. entry.is_hidden = (filename[0] == '.');
  185. entry.is_read_only = 0;
  186. entry.file_size = file.size;
  187. // We emulate a SD card where the archive bit has never been cleared, as it would be on
  188. // most user SD cards.
  189. // Some homebrews (blargSNES for instance) are known to mistakenly use the archive bit as a
  190. // file bit.
  191. entry.is_archive = !file.isDirectory;
  192. ++entries_read;
  193. ++children_iterator;
  194. }
  195. return entries_read;
  196. }
  197. } // namespace FileSys