diff options
author | bunnei <bunneidev@gmail.com> | 2016-03-20 21:11:49 +0100 |
---|---|---|
committer | bunnei <bunneidev@gmail.com> | 2016-03-20 21:11:49 +0100 |
commit | b83e95727f95fa6fe35d436be3e821605244a6a8 (patch) | |
tree | 64255c7432e882205a2dbea1fe962025863664ee /src/core/file_sys/disk_archive.cpp | |
parent | Merge pull request #1550 from LittleWhite-tb/gdbstub_include_fix (diff) | |
parent | HLE/FS: Change the error code returned when an ExtSaveData archive is not found. (diff) | |
download | yuzu-b83e95727f95fa6fe35d436be3e821605244a6a8.tar yuzu-b83e95727f95fa6fe35d436be3e821605244a6a8.tar.gz yuzu-b83e95727f95fa6fe35d436be3e821605244a6a8.tar.bz2 yuzu-b83e95727f95fa6fe35d436be3e821605244a6a8.tar.lz yuzu-b83e95727f95fa6fe35d436be3e821605244a6a8.tar.xz yuzu-b83e95727f95fa6fe35d436be3e821605244a6a8.tar.zst yuzu-b83e95727f95fa6fe35d436be3e821605244a6a8.zip |
Diffstat (limited to 'src/core/file_sys/disk_archive.cpp')
-rw-r--r-- | src/core/file_sys/disk_archive.cpp | 80 |
1 files changed, 57 insertions, 23 deletions
diff --git a/src/core/file_sys/disk_archive.cpp b/src/core/file_sys/disk_archive.cpp index a51416774..8e4ea01c5 100644 --- a/src/core/file_sys/disk_archive.cpp +++ b/src/core/file_sys/disk_archive.cpp @@ -17,16 +17,28 @@ namespace FileSys { -std::unique_ptr<FileBackend> DiskArchive::OpenFile(const Path& path, const Mode mode) const { +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 = Common::make_unique<DiskFile>(*this, path, mode); - if (!file->Open()) - return nullptr; - return std::move(file); + ResultCode result = file->Open(); + if (result.IsError()) + return result; + return MakeResult<std::unique_ptr<FileBackend>>(std::move(file)); } -bool DiskArchive::DeleteFile(const Path& path) const { - return FileUtil::Delete(mount_point + path.AsString()); +ResultCode DiskArchive::DeleteFile(const Path& path) const { + std::string file_path = mount_point + path.AsString(); + + if (FileUtil::IsDirectory(file_path)) + return ResultCode(ErrorDescription::FS_NotAFile, ErrorModule::FS, ErrorSummary::Canceled, ErrorLevel::Status); + + if (!FileUtil::Exists(file_path)) + return ResultCode(ErrorDescription::FS_NotFound, ErrorModule::FS, ErrorSummary::NotFound, ErrorLevel::Status); + + if (FileUtil::Delete(file_path)) + return RESULT_SUCCESS; + + return ResultCode(ErrorDescription::FS_NotAFile, ErrorModule::FS, ErrorSummary::Canceled, ErrorLevel::Status); } bool DiskArchive::RenameFile(const Path& src_path, const Path& dest_path) const { @@ -37,11 +49,14 @@ bool DiskArchive::DeleteDirectory(const Path& path) const { return FileUtil::DeleteDir(mount_point + path.AsString()); } -ResultCode DiskArchive::CreateFile(const FileSys::Path& path, u32 size) const { +ResultCode DiskArchive::CreateFile(const FileSys::Path& path, u64 size) const { std::string full_path = mount_point + path.AsString(); + if (FileUtil::IsDirectory(full_path)) + return ResultCode(ErrorDescription::FS_NotAFile, ErrorModule::FS, ErrorSummary::Canceled, ErrorLevel::Status); + if (FileUtil::Exists(full_path)) - return ResultCode(ErrorDescription::AlreadyExists, ErrorModule::FS, ErrorSummary::NothingHappened, ErrorLevel::Info); + return ResultCode(ErrorDescription::FS_AlreadyExists, ErrorModule::FS, ErrorSummary::NothingHappened, ErrorLevel::Status); if (size == 0) { FileUtil::CreateEmptyFile(full_path); @@ -89,38 +104,57 @@ DiskFile::DiskFile(const DiskArchive& archive, const Path& path, const Mode mode this->mode.hex = mode.hex; } -bool DiskFile::Open() { - if (!mode.create_flag && !FileUtil::Exists(path)) { - LOG_ERROR(Service_FS, "Non-existing file %s can't be open without mode create.", path.c_str()); - return false; +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.create_flag) - mode_string = "w+"; - else if (mode.write_flag) - mode_string = "r+"; // Files opened with Write access can be read from + 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"; + mode_string += "r"; // Open the file in binary mode, to avoid problems with CR/LF on Windows systems mode_string += "b"; file = Common::make_unique<FileUtil::IOFile>(path, mode_string.c_str()); - return file->IsOpen(); + if (file->IsOpen()) + return RESULT_SUCCESS; + return ResultCode(ErrorDescription::FS_NotFound, ErrorModule::FS, ErrorSummary::NotFound, ErrorLevel::Status); } -size_t DiskFile::Read(const u64 offset, const size_t length, u8* buffer) const { +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, ErrorSummary::Canceled, ErrorLevel::Status); + file->Seek(offset, SEEK_SET); - return file->ReadBytes(buffer, length); + return MakeResult<size_t>(file->ReadBytes(buffer, length)); } -size_t DiskFile::Write(const u64 offset, const size_t length, const bool flush, const u8* buffer) const { +ResultVal<size_t> DiskFile::Write(const u64 offset, const size_t length, const bool flush, const u8* buffer) const { + if (!mode.write_flag) + return ResultCode(ErrorDescription::FS_InvalidOpenFlags, ErrorModule::FS, ErrorSummary::Canceled, ErrorLevel::Status); + file->Seek(offset, SEEK_SET); size_t written = file->WriteBytes(buffer, length); if (flush) file->Flush(); - return written; + return MakeResult<size_t>(written); } u64 DiskFile::GetSize() const { |