HLE/FS: Return the proper error codes when opening files.

This commit is contained in:
Subv 2015-12-28 10:17:06 -05:00 committed by Sean Maas
parent 4be40b7c81
commit 910871cc7c
7 changed files with 43 additions and 28 deletions

View file

@ -76,9 +76,9 @@ public:
* Open a file specified by its path, using the specified mode
* @param path Path relative to the archive
* @param mode Mode to open the file with
* @return Opened file, or nullptr
* @return Opened file, or error code
*/
virtual std::unique_ptr<FileBackend> OpenFile(const Path& path, const Mode mode) const = 0;
virtual ResultVal<std::unique_ptr<FileBackend>> OpenFile(const Path& path, const Mode mode) const = 0;
/**
* Delete a file specified by its path

View file

@ -17,12 +17,13 @@
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));
}
ResultCode DiskArchive::DeleteFile(const Path& path) const {
@ -103,25 +104,38 @@ 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);
}
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
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";
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);
}
ResultVal<size_t> DiskFile::Read(const u64 offset, const size_t length, u8* buffer) const {

View file

@ -33,7 +33,7 @@ public:
virtual std::string GetName() const override { return "DiskArchive: " + mount_point; }
std::unique_ptr<FileBackend> OpenFile(const Path& path, const Mode mode) const override;
ResultVal<std::unique_ptr<FileBackend>> OpenFile(const Path& path, const Mode mode) const override;
ResultCode DeleteFile(const Path& path) const override;
bool RenameFile(const Path& src_path, const Path& dest_path) const override;
bool DeleteDirectory(const Path& path) const override;
@ -54,7 +54,7 @@ class DiskFile : public FileBackend {
public:
DiskFile(const DiskArchive& archive, const Path& path, const Mode mode);
bool Open() override;
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;

View file

@ -21,9 +21,9 @@ public:
/**
* Open the file
* @return true if the file opened correctly
* @return Result of the file operation
*/
virtual bool Open() = 0;
virtual ResultCode Open() = 0;
/**
* Read data from the file

View file

@ -20,8 +20,8 @@ std::string IVFCArchive::GetName() const {
return "IVFC";
}
std::unique_ptr<FileBackend> IVFCArchive::OpenFile(const Path& path, const Mode mode) const {
return Common::make_unique<IVFCFile>(romfs_file, data_offset, data_size);
ResultVal<std::unique_ptr<FileBackend>> IVFCArchive::OpenFile(const Path& path, const Mode mode) const {
return MakeResult<std::unique_ptr<FileBackend>>(Common::make_unique<IVFCFile>(romfs_file, data_offset, data_size));
}
ResultCode IVFCArchive::DeleteFile(const Path& path) const {

View file

@ -34,7 +34,7 @@ public:
std::string GetName() const override;
std::unique_ptr<FileBackend> OpenFile(const Path& path, const Mode mode) const override;
ResultVal<std::unique_ptr<FileBackend>> OpenFile(const Path& path, const Mode mode) const override;
ResultCode DeleteFile(const Path& path) const override;
bool RenameFile(const Path& src_path, const Path& dest_path) const override;
bool DeleteDirectory(const Path& path) const override;
@ -55,7 +55,7 @@ public:
IVFCFile(std::shared_ptr<FileUtil::IOFile> file, u64 offset, u64 size)
: romfs_file(file), data_offset(offset), data_size(size) {}
bool Open() override { return true; }
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;

View file

@ -307,13 +307,14 @@ ResultVal<Kernel::SharedPtr<File>> OpenFileFromArchive(ArchiveHandle archive_han
if (archive == nullptr)
return ERR_INVALID_HANDLE;
std::unique_ptr<FileSys::FileBackend> backend = archive->OpenFile(path, mode);
if (backend == nullptr) {
auto backend = archive->OpenFile(path, mode);
if (backend.Failed()) {
return backend.Code();
return ResultCode(ErrorDescription::FS_NotFound, ErrorModule::FS,
ErrorSummary::NotFound, ErrorLevel::Status);
}
auto file = Kernel::SharedPtr<File>(new File(std::move(backend), path));
auto file = Kernel::SharedPtr<File>(new File(backend.MoveFrom(), path));
return MakeResult<Kernel::SharedPtr<File>>(std::move(file));
}