diff options
Diffstat (limited to '')
| -rw-r--r-- | src/core/file_sys/disk_archive.cpp | 147 | ||||
| -rw-r--r-- | src/core/file_sys/disk_archive.h | 32 |
2 files changed, 0 insertions, 179 deletions
diff --git a/src/core/file_sys/disk_archive.cpp b/src/core/file_sys/disk_archive.cpp index 0e51fd1a6..a243d9a13 100644 --- a/src/core/file_sys/disk_archive.cpp +++ b/src/core/file_sys/disk_archive.cpp | |||
| @@ -15,153 +15,6 @@ | |||
| 15 | 15 | ||
| 16 | namespace FileSys { | 16 | namespace FileSys { |
| 17 | 17 | ||
| 18 | ResultVal<std::unique_ptr<FileBackend>> DiskArchive::OpenFile(const Path& path, | ||
| 19 | const Mode& mode) const { | ||
| 20 | LOG_DEBUG(Service_FS, "called path=%s mode=%01X", path.DebugStr().c_str(), mode.hex); | ||
| 21 | |||
| 22 | auto full_path = mount_point + path.AsString(); | ||
| 23 | if (FileUtil::IsDirectory(full_path)) | ||
| 24 | return ResultCode(ErrorDescription::FS_NotAFile, ErrorModule::FS, ErrorSummary::Canceled, | ||
| 25 | ErrorLevel::Status); | ||
| 26 | |||
| 27 | // Specifying only the Create flag is invalid | ||
| 28 | if (mode.create_flag && !mode.read_flag && !mode.write_flag) { | ||
| 29 | return ResultCode(ErrorDescription::FS_InvalidOpenFlags, ErrorModule::FS, | ||
| 30 | ErrorSummary::Canceled, ErrorLevel::Status); | ||
| 31 | } | ||
| 32 | |||
| 33 | if (!FileUtil::Exists(full_path)) { | ||
| 34 | if (!mode.create_flag) { | ||
| 35 | LOG_ERROR(Service_FS, "Non-existing file %s can't be open without mode create.", | ||
| 36 | full_path.c_str()); | ||
| 37 | return ResultCode(ErrorDescription::FS_NotFound, ErrorModule::FS, | ||
| 38 | ErrorSummary::NotFound, ErrorLevel::Status); | ||
| 39 | } else { | ||
| 40 | // Create the file | ||
| 41 | FileUtil::CreateEmptyFile(full_path); | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | std::string mode_string = ""; | ||
| 46 | if (mode.write_flag) | ||
| 47 | mode_string += "r+"; // Files opened with Write access can be read from | ||
| 48 | else if (mode.read_flag) | ||
| 49 | mode_string += "r"; | ||
| 50 | |||
| 51 | // Open the file in binary mode, to avoid problems with CR/LF on Windows systems | ||
| 52 | mode_string += "b"; | ||
| 53 | |||
| 54 | FileUtil::IOFile file(full_path, mode_string.c_str()); | ||
| 55 | if (!file.IsOpen()) | ||
| 56 | return ResultCode(ErrorDescription::FS_NotFound, ErrorModule::FS, ErrorSummary::NotFound, | ||
| 57 | ErrorLevel::Status); | ||
| 58 | |||
| 59 | auto disk_file = std::make_unique<DiskFile>(std::move(file), mode); | ||
| 60 | return MakeResult<std::unique_ptr<FileBackend>>(std::move(disk_file)); | ||
| 61 | } | ||
| 62 | |||
| 63 | ResultCode DiskArchive::DeleteFile(const Path& path) const { | ||
| 64 | std::string file_path = mount_point + path.AsString(); | ||
| 65 | |||
| 66 | if (FileUtil::IsDirectory(file_path)) | ||
| 67 | return ResultCode(ErrorDescription::FS_NotAFile, ErrorModule::FS, ErrorSummary::Canceled, | ||
| 68 | ErrorLevel::Status); | ||
| 69 | |||
| 70 | if (!FileUtil::Exists(file_path)) | ||
| 71 | return ResultCode(ErrorDescription::FS_NotFound, ErrorModule::FS, ErrorSummary::NotFound, | ||
| 72 | ErrorLevel::Status); | ||
| 73 | |||
| 74 | if (FileUtil::Delete(file_path)) | ||
| 75 | return RESULT_SUCCESS; | ||
| 76 | |||
| 77 | return ResultCode(ErrorDescription::FS_NotAFile, ErrorModule::FS, ErrorSummary::Canceled, | ||
| 78 | ErrorLevel::Status); | ||
| 79 | } | ||
| 80 | |||
| 81 | ResultCode DiskArchive::RenameFile(const Path& src_path, const Path& dest_path) const { | ||
| 82 | if (FileUtil::Rename(mount_point + src_path.AsString(), mount_point + dest_path.AsString())) | ||
| 83 | return RESULT_SUCCESS; | ||
| 84 | |||
| 85 | // TODO(yuriks): This code probably isn't right, it'll return a Status even if the file didn't | ||
| 86 | // exist or similar. Verify. | ||
| 87 | return ResultCode(ErrorDescription::NoData, ErrorModule::FS, // TODO: verify description | ||
| 88 | ErrorSummary::NothingHappened, ErrorLevel::Status); | ||
| 89 | } | ||
| 90 | |||
| 91 | ResultCode DiskArchive::DeleteDirectory(const Path& path) const { | ||
| 92 | if (FileUtil::DeleteDir(mount_point + path.AsString())) | ||
| 93 | return RESULT_SUCCESS; | ||
| 94 | return ResultCode(ErrorDescription::NoData, ErrorModule::FS, // TODO: verify description | ||
| 95 | ErrorSummary::Canceled, ErrorLevel::Status); | ||
| 96 | } | ||
| 97 | |||
| 98 | ResultCode DiskArchive::DeleteDirectoryRecursively(const Path& path) const { | ||
| 99 | if (FileUtil::DeleteDirRecursively(mount_point + path.AsString())) | ||
| 100 | return RESULT_SUCCESS; | ||
| 101 | return ResultCode(ErrorDescription::NoData, ErrorModule::FS, // TODO: verify description | ||
| 102 | ErrorSummary::Canceled, ErrorLevel::Status); | ||
| 103 | } | ||
| 104 | |||
| 105 | ResultCode DiskArchive::CreateFile(const FileSys::Path& path, u64 size) const { | ||
| 106 | std::string full_path = mount_point + path.AsString(); | ||
| 107 | |||
| 108 | if (FileUtil::IsDirectory(full_path)) | ||
| 109 | return ResultCode(ErrorDescription::FS_NotAFile, ErrorModule::FS, ErrorSummary::Canceled, | ||
| 110 | ErrorLevel::Status); | ||
| 111 | |||
| 112 | if (FileUtil::Exists(full_path)) | ||
| 113 | return ResultCode(ErrorDescription::FS_AlreadyExists, ErrorModule::FS, | ||
| 114 | ErrorSummary::NothingHappened, ErrorLevel::Status); | ||
| 115 | |||
| 116 | if (size == 0) { | ||
| 117 | FileUtil::CreateEmptyFile(full_path); | ||
| 118 | return RESULT_SUCCESS; | ||
| 119 | } | ||
| 120 | |||
| 121 | FileUtil::IOFile file(full_path, "wb"); | ||
| 122 | // Creates a sparse file (or a normal file on filesystems without the concept of sparse files) | ||
| 123 | // We do this by seeking to the right size, then writing a single null byte. | ||
| 124 | if (file.Seek(size - 1, SEEK_SET) && file.WriteBytes("", 1) == 1) | ||
| 125 | return RESULT_SUCCESS; | ||
| 126 | |||
| 127 | return ResultCode(ErrorDescription::TooLarge, ErrorModule::FS, ErrorSummary::OutOfResource, | ||
| 128 | ErrorLevel::Info); | ||
| 129 | } | ||
| 130 | |||
| 131 | ResultCode DiskArchive::CreateDirectory(const Path& path) const { | ||
| 132 | if (FileUtil::CreateDir(mount_point + path.AsString())) | ||
| 133 | return RESULT_SUCCESS; | ||
| 134 | return ResultCode(ErrorDescription::NoData, ErrorModule::FS, // TODO: verify description | ||
| 135 | ErrorSummary::Canceled, ErrorLevel::Status); | ||
| 136 | } | ||
| 137 | |||
| 138 | ResultCode DiskArchive::RenameDirectory(const Path& src_path, const Path& dest_path) const { | ||
| 139 | if (FileUtil::Rename(mount_point + src_path.AsString(), mount_point + dest_path.AsString())) | ||
| 140 | return RESULT_SUCCESS; | ||
| 141 | |||
| 142 | // TODO(yuriks): This code probably isn't right, it'll return a Status even if the file didn't | ||
| 143 | // exist or similar. Verify. | ||
| 144 | return ResultCode(ErrorDescription::NoData, ErrorModule::FS, // TODO: verify description | ||
| 145 | ErrorSummary::NothingHappened, ErrorLevel::Status); | ||
| 146 | } | ||
| 147 | |||
| 148 | ResultVal<std::unique_ptr<DirectoryBackend>> DiskArchive::OpenDirectory(const Path& path) const { | ||
| 149 | LOG_DEBUG(Service_FS, "called path=%s", path.DebugStr().c_str()); | ||
| 150 | auto full_path = mount_point + path.AsString(); | ||
| 151 | if (!FileUtil::IsDirectory(full_path)) | ||
| 152 | return ResultCode(ErrorDescription::FS_NotFound, ErrorModule::FS, ErrorSummary::NotFound, | ||
| 153 | ErrorLevel::Permanent); | ||
| 154 | auto directory = std::make_unique<DiskDirectory>(full_path); | ||
| 155 | return MakeResult<std::unique_ptr<DirectoryBackend>>(std::move(directory)); | ||
| 156 | } | ||
| 157 | |||
| 158 | u64 DiskArchive::GetFreeBytes() const { | ||
| 159 | // TODO: Stubbed to return 1GiB | ||
| 160 | return 1024 * 1024 * 1024; | ||
| 161 | } | ||
| 162 | |||
| 163 | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 164 | |||
| 165 | ResultVal<size_t> DiskFile::Read(const u64 offset, const size_t length, u8* buffer) const { | 18 | ResultVal<size_t> DiskFile::Read(const u64 offset, const size_t length, u8* buffer) const { |
| 166 | if (!mode.read_flag) | 19 | if (!mode.read_flag) |
| 167 | return ResultCode(ErrorDescription::FS_InvalidOpenFlags, ErrorModule::FS, | 20 | return ResultCode(ErrorDescription::FS_InvalidOpenFlags, ErrorModule::FS, |
diff --git a/src/core/file_sys/disk_archive.h b/src/core/file_sys/disk_archive.h index c2c3c3b23..eb9166df6 100644 --- a/src/core/file_sys/disk_archive.h +++ b/src/core/file_sys/disk_archive.h | |||
| @@ -20,38 +20,6 @@ | |||
| 20 | 20 | ||
| 21 | namespace FileSys { | 21 | namespace FileSys { |
| 22 | 22 | ||
| 23 | /** | ||
| 24 | * Helper which implements a backend accessing the host machine's filesystem. | ||
| 25 | * This should be subclassed by concrete archive types, which will provide the | ||
| 26 | * base directory on the host filesystem and override any required functionality. | ||
| 27 | */ | ||
| 28 | class DiskArchive : public ArchiveBackend { | ||
| 29 | public: | ||
| 30 | DiskArchive(const std::string& mount_point_) : mount_point(mount_point_) {} | ||
| 31 | |||
| 32 | virtual std::string GetName() const override { | ||
| 33 | return "DiskArchive: " + mount_point; | ||
| 34 | } | ||
| 35 | |||
| 36 | ResultVal<std::unique_ptr<FileBackend>> OpenFile(const Path& path, | ||
| 37 | const Mode& mode) const override; | ||
| 38 | ResultCode DeleteFile(const Path& path) const override; | ||
| 39 | ResultCode RenameFile(const Path& src_path, const Path& dest_path) const override; | ||
| 40 | ResultCode DeleteDirectory(const Path& path) const override; | ||
| 41 | ResultCode DeleteDirectoryRecursively(const Path& path) const override; | ||
| 42 | ResultCode CreateFile(const Path& path, u64 size) const override; | ||
| 43 | ResultCode CreateDirectory(const Path& path) const override; | ||
| 44 | ResultCode RenameDirectory(const Path& src_path, const Path& dest_path) const override; | ||
| 45 | ResultVal<std::unique_ptr<DirectoryBackend>> OpenDirectory(const Path& path) const override; | ||
| 46 | u64 GetFreeBytes() const override; | ||
| 47 | |||
| 48 | protected: | ||
| 49 | friend class DiskFile; | ||
| 50 | friend class DiskDirectory; | ||
| 51 | |||
| 52 | std::string mount_point; | ||
| 53 | }; | ||
| 54 | |||
| 55 | class DiskFile : public FileBackend { | 23 | class DiskFile : public FileBackend { |
| 56 | public: | 24 | public: |
| 57 | DiskFile(FileUtil::IOFile&& file_, const Mode& mode_) | 25 | DiskFile(FileUtil::IOFile&& file_, const Mode& mode_) |