disk_archive.cpp 6.5 KB

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