diff options
Diffstat (limited to '')
-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 { ResultVal<std::unique_ptr<FileBackend>> DiskArchive::OpenFile(const Path& path, const Mode& mode) const { LOG_DEBUG(Service_FS, "called path=%s mode=%01X", path.DebugStr().c_str(), mode.hex); - auto file = std::make_unique<DiskFile>(*this, path, mode); - ResultCode result = file->Open(); - if (result.IsError()) - return result; - return MakeResult<std::unique_ptr<FileBackend>>(std::move(file)); + + auto full_path = mount_point + path.AsString(); + if (FileUtil::IsDirectory(full_path)) + return ResultCode(ErrorDescription::FS_NotAFile, ErrorModule::FS, ErrorSummary::Canceled, + ErrorLevel::Status); + + // Specifying only the Create flag is invalid + if (mode.create_flag && !mode.read_flag && !mode.write_flag) { + return ResultCode(ErrorDescription::FS_InvalidOpenFlags, ErrorModule::FS, + ErrorSummary::Canceled, ErrorLevel::Status); + } + + if (!FileUtil::Exists(full_path)) { + if (!mode.create_flag) { + LOG_ERROR(Service_FS, "Non-existing file %s can't be open without mode create.", + full_path.c_str()); + return ResultCode(ErrorDescription::FS_NotFound, ErrorModule::FS, + ErrorSummary::NotFound, ErrorLevel::Status); + } else { + // Create the file + FileUtil::CreateEmptyFile(full_path); + } + } + + std::string mode_string = ""; + if (mode.write_flag) + mode_string += "r+"; // Files opened with Write access can be read from + else if (mode.read_flag) + mode_string += "r"; + + // Open the file in binary mode, to avoid problems with CR/LF on Windows systems + mode_string += "b"; + + FileUtil::IOFile file(full_path, mode_string.c_str()); + if (!file.IsOpen()) + return ResultCode(ErrorDescription::FS_NotFound, ErrorModule::FS, ErrorSummary::NotFound, + ErrorLevel::Status); + + auto disk_file = std::make_unique<DiskFile>(std::move(file), mode); + return MakeResult<std::unique_ptr<FileBackend>>(std::move(disk_file)); } ResultCode DiskArchive::DeleteFile(const Path& path) const { @@ -127,53 +162,6 @@ u64 DiskArchive::GetFreeBytes() const { //////////////////////////////////////////////////////////////////////////////////////////////////// -DiskFile::DiskFile(const DiskArchive& archive, const Path& path, const Mode mode) { - // TODO(Link Mauve): normalize path into an absolute path without "..", it can currently bypass - // the root directory we set while opening the archive. - // For example, opening /../../etc/passwd can give the emulated program your users list. - this->path = archive.mount_point + path.AsString(); - this->mode.hex = mode.hex; -} - -ResultCode DiskFile::Open() { - if (FileUtil::IsDirectory(path)) - return ResultCode(ErrorDescription::FS_NotAFile, ErrorModule::FS, ErrorSummary::Canceled, - ErrorLevel::Status); - - // Specifying only the Create flag is invalid - if (mode.create_flag && !mode.read_flag && !mode.write_flag) { - return ResultCode(ErrorDescription::FS_InvalidOpenFlags, ErrorModule::FS, - ErrorSummary::Canceled, ErrorLevel::Status); - } - - if (!FileUtil::Exists(path)) { - if (!mode.create_flag) { - LOG_ERROR(Service_FS, "Non-existing file %s can't be open without mode create.", - path.c_str()); - return ResultCode(ErrorDescription::FS_NotFound, ErrorModule::FS, - ErrorSummary::NotFound, ErrorLevel::Status); - } else { - // Create the file - FileUtil::CreateEmptyFile(path); - } - } - - std::string mode_string = ""; - if (mode.write_flag) - mode_string += "r+"; // Files opened with Write access can be read from - else if (mode.read_flag) - mode_string += "r"; - - // Open the file in binary mode, to avoid problems with CR/LF on Windows systems - mode_string += "b"; - - file = std::make_unique<FileUtil::IOFile>(path, mode_string.c_str()); - if (file->IsOpen()) - return RESULT_SUCCESS; - return ResultCode(ErrorDescription::FS_NotFound, ErrorModule::FS, ErrorSummary::NotFound, - ErrorLevel::Status); -} - ResultVal<size_t> DiskFile::Read(const u64 offset, const size_t length, u8* buffer) const { if (!mode.read_flag && !mode.write_flag) 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: class DiskFile : public FileBackend { public: - DiskFile(const DiskArchive& archive, const Path& path, const Mode mode); + DiskFile(FileUtil::IOFile&& file_, const Mode& mode_) + : file(new FileUtil::IOFile(std::move(file_))) { + mode.hex = mode_.hex; + } - ResultCode Open() override; ResultVal<size_t> Read(u64 offset, size_t length, u8* buffer) const override; ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) const override; u64 GetSize() const override; @@ -68,7 +70,6 @@ public: } protected: - std::string path; Mode mode; std::unique_ptr<FileUtil::IOFile> file; }; 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: virtual ~FileBackend() {} /** - * Open the file - * @return Result of the file operation - */ - virtual ResultCode Open() = 0; - - /** * Read data from the file * @param offset Offset in bytes to start reading data from * @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: IVFCFile(std::shared_ptr<FileUtil::IOFile> file, u64 offset, u64 size) : romfs_file(file), data_offset(offset), data_size(size) {} - ResultCode Open() override { - return RESULT_SUCCESS; - } ResultVal<size_t> Read(u64 offset, size_t length, u8* buffer) const override; ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) const override; u64 GetSize() const override; |