summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Subv2015-12-28 10:17:06 -0500
committerGravatar Subv2016-03-20 14:28:22 -0500
commit95b34f8081e26cfe75d63a853d1626fdd5b636e6 (patch)
treea25b91d70bf84ad71922f63f2f92e37320d2bbfa /src
parentHLE/FS: Fixed the OpenDirectory error code (diff)
downloadyuzu-95b34f8081e26cfe75d63a853d1626fdd5b636e6.tar.gz
yuzu-95b34f8081e26cfe75d63a853d1626fdd5b636e6.tar.xz
yuzu-95b34f8081e26cfe75d63a853d1626fdd5b636e6.zip
HLE/FS: Return the proper error codes when opening files.
Diffstat (limited to 'src')
-rw-r--r--src/core/file_sys/archive_backend.h4
-rw-r--r--src/core/file_sys/disk_archive.cpp44
-rw-r--r--src/core/file_sys/disk_archive.h4
-rw-r--r--src/core/file_sys/file_backend.h4
-rw-r--r--src/core/file_sys/ivfc_archive.cpp4
-rw-r--r--src/core/file_sys/ivfc_archive.h4
-rw-r--r--src/core/hle/service/fs/archive.cpp7
7 files changed, 43 insertions, 28 deletions
diff --git a/src/core/file_sys/archive_backend.h b/src/core/file_sys/archive_backend.h
index c5da9bd6f..60108b4b0 100644
--- a/src/core/file_sys/archive_backend.h
+++ b/src/core/file_sys/archive_backend.h
@@ -76,9 +76,9 @@ public:
76 * Open a file specified by its path, using the specified mode 76 * Open a file specified by its path, using the specified mode
77 * @param path Path relative to the archive 77 * @param path Path relative to the archive
78 * @param mode Mode to open the file with 78 * @param mode Mode to open the file with
79 * @return Opened file, or nullptr 79 * @return Opened file, or error code
80 */ 80 */
81 virtual std::unique_ptr<FileBackend> OpenFile(const Path& path, const Mode mode) const = 0; 81 virtual ResultVal<std::unique_ptr<FileBackend>> OpenFile(const Path& path, const Mode mode) const = 0;
82 82
83 /** 83 /**
84 * Delete a file specified by its path 84 * Delete a file specified by its path
diff --git a/src/core/file_sys/disk_archive.cpp b/src/core/file_sys/disk_archive.cpp
index 2dca895fd..8e4ea01c5 100644
--- a/src/core/file_sys/disk_archive.cpp
+++ b/src/core/file_sys/disk_archive.cpp
@@ -17,12 +17,13 @@
17 17
18namespace FileSys { 18namespace FileSys {
19 19
20std::unique_ptr<FileBackend> DiskArchive::OpenFile(const Path& path, const Mode mode) const { 20ResultVal<std::unique_ptr<FileBackend>> DiskArchive::OpenFile(const Path& path, const Mode mode) const {
21 LOG_DEBUG(Service_FS, "called path=%s mode=%01X", path.DebugStr().c_str(), mode.hex); 21 LOG_DEBUG(Service_FS, "called path=%s mode=%01X", path.DebugStr().c_str(), mode.hex);
22 auto file = Common::make_unique<DiskFile>(*this, path, mode); 22 auto file = Common::make_unique<DiskFile>(*this, path, mode);
23 if (!file->Open()) 23 ResultCode result = file->Open();
24 return nullptr; 24 if (result.IsError())
25 return std::move(file); 25 return result;
26 return MakeResult<std::unique_ptr<FileBackend>>(std::move(file));
26} 27}
27 28
28ResultCode DiskArchive::DeleteFile(const Path& path) const { 29ResultCode DiskArchive::DeleteFile(const Path& path) const {
@@ -103,25 +104,38 @@ DiskFile::DiskFile(const DiskArchive& archive, const Path& path, const Mode mode
103 this->mode.hex = mode.hex; 104 this->mode.hex = mode.hex;
104} 105}
105 106
106bool DiskFile::Open() { 107ResultCode DiskFile::Open() {
107 if (!mode.create_flag && !FileUtil::Exists(path)) { 108 if (FileUtil::IsDirectory(path))
108 LOG_ERROR(Service_FS, "Non-existing file %s can't be open without mode create.", path.c_str()); 109 return ResultCode(ErrorDescription::FS_NotAFile, ErrorModule::FS, ErrorSummary::Canceled, ErrorLevel::Status);
109 return false; 110
111 // Specifying only the Create flag is invalid
112 if (mode.create_flag && !mode.read_flag && !mode.write_flag) {
113 return ResultCode(ErrorDescription::FS_InvalidOpenFlags, ErrorModule::FS, ErrorSummary::Canceled, ErrorLevel::Status);
110 } 114 }
111 115
112 std::string mode_string; 116 if (!FileUtil::Exists(path)) {
113 if (mode.create_flag) 117 if (!mode.create_flag) {
114 mode_string = "w+"; 118 LOG_ERROR(Service_FS, "Non-existing file %s can't be open without mode create.", path.c_str());
115 else if (mode.write_flag) 119 return ResultCode(ErrorDescription::FS_NotFound, ErrorModule::FS, ErrorSummary::NotFound, ErrorLevel::Status);
116 mode_string = "r+"; // Files opened with Write access can be read from 120 } else {
121 // Create the file
122 FileUtil::CreateEmptyFile(path);
123 }
124 }
125
126 std::string mode_string = "";
127 if (mode.write_flag)
128 mode_string += "r+"; // Files opened with Write access can be read from
117 else if (mode.read_flag) 129 else if (mode.read_flag)
118 mode_string = "r"; 130 mode_string += "r";
119 131
120 // Open the file in binary mode, to avoid problems with CR/LF on Windows systems 132 // Open the file in binary mode, to avoid problems with CR/LF on Windows systems
121 mode_string += "b"; 133 mode_string += "b";
122 134
123 file = Common::make_unique<FileUtil::IOFile>(path, mode_string.c_str()); 135 file = Common::make_unique<FileUtil::IOFile>(path, mode_string.c_str());
124 return file->IsOpen(); 136 if (file->IsOpen())
137 return RESULT_SUCCESS;
138 return ResultCode(ErrorDescription::FS_NotFound, ErrorModule::FS, ErrorSummary::NotFound, ErrorLevel::Status);
125} 139}
126 140
127ResultVal<size_t> DiskFile::Read(const u64 offset, const size_t length, u8* buffer) const { 141ResultVal<size_t> DiskFile::Read(const u64 offset, const size_t length, u8* buffer) const {
diff --git a/src/core/file_sys/disk_archive.h b/src/core/file_sys/disk_archive.h
index 96d86ad21..b4cc2f702 100644
--- a/src/core/file_sys/disk_archive.h
+++ b/src/core/file_sys/disk_archive.h
@@ -33,7 +33,7 @@ public:
33 33
34 virtual std::string GetName() const override { return "DiskArchive: " + mount_point; } 34 virtual std::string GetName() const override { return "DiskArchive: " + mount_point; }
35 35
36 std::unique_ptr<FileBackend> OpenFile(const Path& path, const Mode mode) const override; 36 ResultVal<std::unique_ptr<FileBackend>> OpenFile(const Path& path, const Mode mode) const override;
37 ResultCode DeleteFile(const Path& path) const override; 37 ResultCode DeleteFile(const Path& path) const override;
38 bool RenameFile(const Path& src_path, const Path& dest_path) const override; 38 bool RenameFile(const Path& src_path, const Path& dest_path) const override;
39 bool DeleteDirectory(const Path& path) const override; 39 bool DeleteDirectory(const Path& path) const override;
@@ -54,7 +54,7 @@ class DiskFile : public FileBackend {
54public: 54public:
55 DiskFile(const DiskArchive& archive, const Path& path, const Mode mode); 55 DiskFile(const DiskArchive& archive, const Path& path, const Mode mode);
56 56
57 bool Open() override; 57 ResultCode Open() override;
58 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;
59 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;
60 u64 GetSize() const override; 60 u64 GetSize() const override;
diff --git a/src/core/file_sys/file_backend.h b/src/core/file_sys/file_backend.h
index 21864a73c..9137bbbad 100644
--- a/src/core/file_sys/file_backend.h
+++ b/src/core/file_sys/file_backend.h
@@ -21,9 +21,9 @@ public:
21 21
22 /** 22 /**
23 * Open the file 23 * Open the file
24 * @return true if the file opened correctly 24 * @return Result of the file operation
25 */ 25 */
26 virtual bool Open() = 0; 26 virtual ResultCode Open() = 0;
27 27
28 /** 28 /**
29 * Read data from the file 29 * Read data from the file
diff --git a/src/core/file_sys/ivfc_archive.cpp b/src/core/file_sys/ivfc_archive.cpp
index e7b37e09d..a8e9a72ef 100644
--- a/src/core/file_sys/ivfc_archive.cpp
+++ b/src/core/file_sys/ivfc_archive.cpp
@@ -20,8 +20,8 @@ std::string IVFCArchive::GetName() const {
20 return "IVFC"; 20 return "IVFC";
21} 21}
22 22
23std::unique_ptr<FileBackend> IVFCArchive::OpenFile(const Path& path, const Mode mode) const { 23ResultVal<std::unique_ptr<FileBackend>> IVFCArchive::OpenFile(const Path& path, const Mode mode) const {
24 return Common::make_unique<IVFCFile>(romfs_file, data_offset, data_size); 24 return MakeResult<std::unique_ptr<FileBackend>>(Common::make_unique<IVFCFile>(romfs_file, data_offset, data_size));
25} 25}
26 26
27ResultCode IVFCArchive::DeleteFile(const Path& path) const { 27ResultCode IVFCArchive::DeleteFile(const Path& path) const {
diff --git a/src/core/file_sys/ivfc_archive.h b/src/core/file_sys/ivfc_archive.h
index acc7e60f5..19d32dcca 100644
--- a/src/core/file_sys/ivfc_archive.h
+++ b/src/core/file_sys/ivfc_archive.h
@@ -34,7 +34,7 @@ public:
34 34
35 std::string GetName() const override; 35 std::string GetName() const override;
36 36
37 std::unique_ptr<FileBackend> OpenFile(const Path& path, const Mode mode) const override; 37 ResultVal<std::unique_ptr<FileBackend>> OpenFile(const Path& path, const Mode mode) const override;
38 ResultCode DeleteFile(const Path& path) const override; 38 ResultCode DeleteFile(const Path& path) const override;
39 bool RenameFile(const Path& src_path, const Path& dest_path) const override; 39 bool RenameFile(const Path& src_path, const Path& dest_path) const override;
40 bool DeleteDirectory(const Path& path) const override; 40 bool DeleteDirectory(const Path& path) const override;
@@ -55,7 +55,7 @@ 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 bool Open() override { return true; } 58 ResultCode Open() override { return RESULT_SUCCESS; }
59 ResultVal<size_t> Read(u64 offset, size_t length, u8* buffer) const override; 59 ResultVal<size_t> Read(u64 offset, size_t length, u8* buffer) const override;
60 ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) const override; 60 ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) const override;
61 u64 GetSize() const override; 61 u64 GetSize() const override;
diff --git a/src/core/hle/service/fs/archive.cpp b/src/core/hle/service/fs/archive.cpp
index 0c56777cf..2ce5f0fe7 100644
--- a/src/core/hle/service/fs/archive.cpp
+++ b/src/core/hle/service/fs/archive.cpp
@@ -307,13 +307,14 @@ ResultVal<Kernel::SharedPtr<File>> OpenFileFromArchive(ArchiveHandle archive_han
307 if (archive == nullptr) 307 if (archive == nullptr)
308 return ERR_INVALID_HANDLE; 308 return ERR_INVALID_HANDLE;
309 309
310 std::unique_ptr<FileSys::FileBackend> backend = archive->OpenFile(path, mode); 310 auto backend = archive->OpenFile(path, mode);
311 if (backend == nullptr) { 311 if (backend.Failed()) {
312 return backend.Code();
312 return ResultCode(ErrorDescription::FS_NotFound, ErrorModule::FS, 313 return ResultCode(ErrorDescription::FS_NotFound, ErrorModule::FS,
313 ErrorSummary::NotFound, ErrorLevel::Status); 314 ErrorSummary::NotFound, ErrorLevel::Status);
314 } 315 }
315 316
316 auto file = Kernel::SharedPtr<File>(new File(std::move(backend), path)); 317 auto file = Kernel::SharedPtr<File>(new File(backend.MoveFrom(), path));
317 return MakeResult<Kernel::SharedPtr<File>>(std::move(file)); 318 return MakeResult<Kernel::SharedPtr<File>>(std::move(file));
318} 319}
319 320