diff options
| author | 2016-10-17 10:10:23 +0800 | |
|---|---|---|
| committer | 2016-11-19 17:17:19 +0200 | |
| commit | 9a0405858a5602a5e1e112b131ecdd8ca27e6d10 (patch) | |
| tree | 287e47a4425fe9dd2c04bbfd92b36d0240a767b9 /src/core | |
| parent | FileSys: remove Open from DirectoryBackend (diff) | |
| download | yuzu-9a0405858a5602a5e1e112b131ecdd8ca27e6d10.tar.gz yuzu-9a0405858a5602a5e1e112b131ecdd8ca27e6d10.tar.xz yuzu-9a0405858a5602a5e1e112b131ecdd8ca27e6d10.zip | |
FileSys: remove Open from FileBackend
Same as directory, file shouldn't expose Open either.
Diffstat (limited to 'src/core')
| -rw-r--r-- | src/core/file_sys/disk_archive.cpp | 92 | ||||
| -rw-r--r-- | src/core/file_sys/disk_archive.h | 7 | ||||
| -rw-r--r-- | src/core/file_sys/file_backend.h | 6 | ||||
| -rw-r--r-- | src/core/file_sys/ivfc_archive.h | 3 |
4 files changed, 44 insertions, 64 deletions
diff --git a/src/core/file_sys/disk_archive.cpp b/src/core/file_sys/disk_archive.cpp index fef6e68a2..43d199434 100644 --- a/src/core/file_sys/disk_archive.cpp +++ b/src/core/file_sys/disk_archive.cpp | |||
| @@ -18,11 +18,46 @@ namespace FileSys { | |||
| 18 | ResultVal<std::unique_ptr<FileBackend>> DiskArchive::OpenFile(const Path& path, | 18 | ResultVal<std::unique_ptr<FileBackend>> DiskArchive::OpenFile(const Path& path, |
| 19 | const Mode& mode) const { | 19 | const Mode& mode) const { |
| 20 | LOG_DEBUG(Service_FS, "called path=%s mode=%01X", path.DebugStr().c_str(), mode.hex); | 20 | LOG_DEBUG(Service_FS, "called path=%s mode=%01X", path.DebugStr().c_str(), mode.hex); |
| 21 | auto file = std::make_unique<DiskFile>(*this, path, mode); | 21 | |
| 22 | ResultCode result = file->Open(); | 22 | auto full_path = mount_point + path.AsString(); |
| 23 | if (result.IsError()) | 23 | if (FileUtil::IsDirectory(full_path)) |
| 24 | return result; | 24 | return ResultCode(ErrorDescription::FS_NotAFile, ErrorModule::FS, ErrorSummary::Canceled, |
| 25 | return MakeResult<std::unique_ptr<FileBackend>>(std::move(file)); | 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)); | ||
| 26 | } | 61 | } |
| 27 | 62 | ||
| 28 | ResultCode DiskArchive::DeleteFile(const Path& path) const { | 63 | ResultCode DiskArchive::DeleteFile(const Path& path) const { |
| @@ -127,53 +162,6 @@ u64 DiskArchive::GetFreeBytes() const { | |||
| 127 | 162 | ||
| 128 | //////////////////////////////////////////////////////////////////////////////////////////////////// | 163 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 129 | 164 | ||
| 130 | DiskFile::DiskFile(const DiskArchive& archive, const Path& path, const Mode mode) { | ||
| 131 | // TODO(Link Mauve): normalize path into an absolute path without "..", it can currently bypass | ||
| 132 | // the root directory we set while opening the archive. | ||
| 133 | // For example, opening /../../etc/passwd can give the emulated program your users list. | ||
| 134 | this->path = archive.mount_point + path.AsString(); | ||
| 135 | this->mode.hex = mode.hex; | ||
| 136 | } | ||
| 137 | |||
| 138 | ResultCode DiskFile::Open() { | ||
| 139 | if (FileUtil::IsDirectory(path)) | ||
| 140 | return ResultCode(ErrorDescription::FS_NotAFile, ErrorModule::FS, ErrorSummary::Canceled, | ||
| 141 | ErrorLevel::Status); | ||
| 142 | |||
| 143 | // Specifying only the Create flag is invalid | ||
| 144 | if (mode.create_flag && !mode.read_flag && !mode.write_flag) { | ||
| 145 | return ResultCode(ErrorDescription::FS_InvalidOpenFlags, ErrorModule::FS, | ||
| 146 | ErrorSummary::Canceled, ErrorLevel::Status); | ||
| 147 | } | ||
| 148 | |||
| 149 | if (!FileUtil::Exists(path)) { | ||
| 150 | if (!mode.create_flag) { | ||
| 151 | LOG_ERROR(Service_FS, "Non-existing file %s can't be open without mode create.", | ||
| 152 | path.c_str()); | ||
| 153 | return ResultCode(ErrorDescription::FS_NotFound, ErrorModule::FS, | ||
| 154 | ErrorSummary::NotFound, ErrorLevel::Status); | ||
| 155 | } else { | ||
| 156 | // Create the file | ||
| 157 | FileUtil::CreateEmptyFile(path); | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | std::string mode_string = ""; | ||
| 162 | if (mode.write_flag) | ||
| 163 | mode_string += "r+"; // Files opened with Write access can be read from | ||
| 164 | else if (mode.read_flag) | ||
| 165 | mode_string += "r"; | ||
| 166 | |||
| 167 | // Open the file in binary mode, to avoid problems with CR/LF on Windows systems | ||
| 168 | mode_string += "b"; | ||
| 169 | |||
| 170 | file = std::make_unique<FileUtil::IOFile>(path, mode_string.c_str()); | ||
| 171 | if (file->IsOpen()) | ||
| 172 | return RESULT_SUCCESS; | ||
| 173 | return ResultCode(ErrorDescription::FS_NotFound, ErrorModule::FS, ErrorSummary::NotFound, | ||
| 174 | ErrorLevel::Status); | ||
| 175 | } | ||
| 176 | |||
| 177 | ResultVal<size_t> DiskFile::Read(const u64 offset, const size_t length, u8* buffer) const { | 165 | ResultVal<size_t> DiskFile::Read(const u64 offset, const size_t length, u8* buffer) const { |
| 178 | if (!mode.read_flag && !mode.write_flag) | 166 | if (!mode.read_flag && !mode.write_flag) |
| 179 | return ResultCode(ErrorDescription::FS_InvalidOpenFlags, ErrorModule::FS, | 167 | 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 c93c32eb2..c2c3c3b23 100644 --- a/src/core/file_sys/disk_archive.h +++ b/src/core/file_sys/disk_archive.h | |||
| @@ -54,9 +54,11 @@ protected: | |||
| 54 | 54 | ||
| 55 | class DiskFile : public FileBackend { | 55 | class DiskFile : public FileBackend { |
| 56 | public: | 56 | public: |
| 57 | DiskFile(const DiskArchive& archive, const Path& path, const Mode mode); | 57 | DiskFile(FileUtil::IOFile&& file_, const Mode& mode_) |
| 58 | : file(new FileUtil::IOFile(std::move(file_))) { | ||
| 59 | mode.hex = mode_.hex; | ||
| 60 | } | ||
| 58 | 61 | ||
| 59 | ResultCode Open() override; | ||
| 60 | ResultVal<size_t> Read(u64 offset, size_t length, u8* buffer) const override; | 62 | ResultVal<size_t> Read(u64 offset, size_t length, u8* buffer) const override; |
| 61 | ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) const override; | 63 | ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) const override; |
| 62 | u64 GetSize() const override; | 64 | u64 GetSize() const override; |
| @@ -68,7 +70,6 @@ public: | |||
| 68 | } | 70 | } |
| 69 | 71 | ||
| 70 | protected: | 72 | protected: |
| 71 | std::string path; | ||
| 72 | Mode mode; | 73 | Mode mode; |
| 73 | std::unique_ptr<FileUtil::IOFile> file; | 74 | std::unique_ptr<FileUtil::IOFile> file; |
| 74 | }; | 75 | }; |
diff --git a/src/core/file_sys/file_backend.h b/src/core/file_sys/file_backend.h index ed997537f..5e7c2bab4 100644 --- a/src/core/file_sys/file_backend.h +++ b/src/core/file_sys/file_backend.h | |||
| @@ -19,12 +19,6 @@ public: | |||
| 19 | virtual ~FileBackend() {} | 19 | virtual ~FileBackend() {} |
| 20 | 20 | ||
| 21 | /** | 21 | /** |
| 22 | * Open the file | ||
| 23 | * @return Result of the file operation | ||
| 24 | */ | ||
| 25 | virtual ResultCode Open() = 0; | ||
| 26 | |||
| 27 | /** | ||
| 28 | * Read data from the file | 22 | * Read data from the file |
| 29 | * @param offset Offset in bytes to start reading data from | 23 | * @param offset Offset in bytes to start reading data from |
| 30 | * @param length Length in bytes of data to read from file | 24 | * @param length Length in bytes of data to read from file |
diff --git a/src/core/file_sys/ivfc_archive.h b/src/core/file_sys/ivfc_archive.h index 826261441..e6fbdfb1f 100644 --- a/src/core/file_sys/ivfc_archive.h +++ b/src/core/file_sys/ivfc_archive.h | |||
| @@ -55,9 +55,6 @@ public: | |||
| 55 | IVFCFile(std::shared_ptr<FileUtil::IOFile> file, u64 offset, u64 size) | 55 | IVFCFile(std::shared_ptr<FileUtil::IOFile> file, u64 offset, u64 size) |
| 56 | : romfs_file(file), data_offset(offset), data_size(size) {} | 56 | : romfs_file(file), data_offset(offset), data_size(size) {} |
| 57 | 57 | ||
| 58 | ResultCode Open() override { | ||
| 59 | return RESULT_SUCCESS; | ||
| 60 | } | ||
| 61 | ResultVal<size_t> Read(u64 offset, size_t length, u8* buffer) const override; | 58 | ResultVal<size_t> Read(u64 offset, size_t length, u8* buffer) const override; |
| 62 | ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) const override; | 59 | ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) const override; |
| 63 | u64 GetSize() const override; | 60 | u64 GetSize() const override; |