disk_archive.cpp 8.5 KB

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