From 4dd8a831bd5ea32108db837754289ab42a2fa6ca Mon Sep 17 00:00:00 2001 From: wwylele Date: Fri, 14 Oct 2016 15:29:09 +0800 Subject: FileSys: make Archive interfaces return error code and make the mode parameter a reference since it is a BitField union --- src/core/file_sys/disk_archive.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src/core/file_sys/disk_archive.h') diff --git a/src/core/file_sys/disk_archive.h b/src/core/file_sys/disk_archive.h index 59ebb2002..0edd87954 100644 --- a/src/core/file_sys/disk_archive.h +++ b/src/core/file_sys/disk_archive.h @@ -34,15 +34,15 @@ public: } ResultVal> OpenFile(const Path& path, - const Mode mode) const override; + 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; - bool DeleteDirectoryRecursively(const Path& path) const override; + ResultCode RenameFile(const Path& src_path, const Path& dest_path) const override; + ResultCode DeleteDirectory(const Path& path) const override; + ResultCode DeleteDirectoryRecursively(const Path& path) const override; ResultCode CreateFile(const Path& path, u64 size) const override; - bool CreateDirectory(const Path& path) const override; - bool RenameDirectory(const Path& src_path, const Path& dest_path) const override; - std::unique_ptr OpenDirectory(const Path& path) const override; + ResultCode CreateDirectory(const Path& path) const override; + ResultCode RenameDirectory(const Path& src_path, const Path& dest_path) const override; + ResultVal> OpenDirectory(const Path& path) const override; u64 GetFreeBytes() const override; protected: -- cgit v1.2.3 From 0e754875d175bfec393a3f4c4f6ad73f45ed361f Mon Sep 17 00:00:00 2001 From: wwylele Date: Sun, 16 Oct 2016 23:09:36 +0800 Subject: FileSys: remove Open from DirectoryBackend Open should not be an interface exposed by Directory because it is the Archive thats implement the methed to open the directory. The service API of 3DS also implies this - Open is not a function of directory service, but is of FS main service --- src/core/file_sys/directory_backend.h | 6 ------ src/core/file_sys/disk_archive.cpp | 17 ++++------------- src/core/file_sys/disk_archive.h | 4 +--- src/core/file_sys/ivfc_archive.h | 3 --- 4 files changed, 5 insertions(+), 25 deletions(-) (limited to 'src/core/file_sys/disk_archive.h') diff --git a/src/core/file_sys/directory_backend.h b/src/core/file_sys/directory_backend.h index b55e382ef..0c93f2074 100644 --- a/src/core/file_sys/directory_backend.h +++ b/src/core/file_sys/directory_backend.h @@ -40,12 +40,6 @@ public: DirectoryBackend() {} virtual ~DirectoryBackend() {} - /** - * Open the directory - * @return true if the directory opened correctly - */ - virtual bool Open() = 0; - /** * List files contained in the directory * @param count Number of entries to return at once in entries diff --git a/src/core/file_sys/disk_archive.cpp b/src/core/file_sys/disk_archive.cpp index ce6b9360b..fef6e68a2 100644 --- a/src/core/file_sys/disk_archive.cpp +++ b/src/core/file_sys/disk_archive.cpp @@ -112,10 +112,11 @@ ResultCode DiskArchive::RenameDirectory(const Path& src_path, const Path& dest_p ResultVal> DiskArchive::OpenDirectory(const Path& path) const { LOG_DEBUG(Service_FS, "called path=%s", path.DebugStr().c_str()); - auto directory = std::make_unique(*this, path); - if (!directory->Open()) + auto full_path = mount_point + path.AsString(); + if (!FileUtil::IsDirectory(full_path)) return ResultCode(ErrorDescription::FS_NotFound, ErrorModule::FS, ErrorSummary::NotFound, ErrorLevel::Permanent); + auto directory = std::make_unique(full_path); return MakeResult>(std::move(directory)); } @@ -211,21 +212,11 @@ bool DiskFile::Close() const { //////////////////////////////////////////////////////////////////////////////////////////////////// -DiskDirectory::DiskDirectory(const DiskArchive& archive, const Path& path) : directory() { - // 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 /../../usr/bin can give the emulated program your installed programs. - this->path = archive.mount_point + path.AsString(); -} - -bool DiskDirectory::Open() { - if (!FileUtil::IsDirectory(path)) - return false; +DiskDirectory::DiskDirectory(const std::string& path) : directory() { unsigned size = FileUtil::ScanDirectoryTree(path, directory); directory.size = size; directory.isDirectory = true; children_iterator = directory.children.begin(); - return true; } u32 DiskDirectory::Read(const u32 count, Entry* entries) { diff --git a/src/core/file_sys/disk_archive.h b/src/core/file_sys/disk_archive.h index 0edd87954..c93c32eb2 100644 --- a/src/core/file_sys/disk_archive.h +++ b/src/core/file_sys/disk_archive.h @@ -75,13 +75,12 @@ protected: class DiskDirectory : public DirectoryBackend { public: - DiskDirectory(const DiskArchive& archive, const Path& path); + DiskDirectory(const std::string& path); ~DiskDirectory() override { Close(); } - bool Open() override; u32 Read(const u32 count, Entry* entries) override; bool Close() const override { @@ -89,7 +88,6 @@ public: } protected: - std::string path; u32 total_entries_in_directory; FileUtil::FSTEntry directory; diff --git a/src/core/file_sys/ivfc_archive.h b/src/core/file_sys/ivfc_archive.h index af6297e1f..826261441 100644 --- a/src/core/file_sys/ivfc_archive.h +++ b/src/core/file_sys/ivfc_archive.h @@ -75,9 +75,6 @@ private: class IVFCDirectory : public DirectoryBackend { public: - bool Open() override { - return false; - } u32 Read(const u32 count, Entry* entries) override { return 0; } -- cgit v1.2.3 From 9a0405858a5602a5e1e112b131ecdd8ca27e6d10 Mon Sep 17 00:00:00 2001 From: wwylele Date: Mon, 17 Oct 2016 10:10:23 +0800 Subject: FileSys: remove Open from FileBackend Same as directory, file shouldn't expose Open either. --- src/core/file_sys/disk_archive.cpp | 92 +++++++++++++++++--------------------- src/core/file_sys/disk_archive.h | 7 +-- src/core/file_sys/file_backend.h | 6 --- src/core/file_sys/ivfc_archive.h | 3 -- 4 files changed, 44 insertions(+), 64 deletions(-) (limited to 'src/core/file_sys/disk_archive.h') 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> 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(*this, path, mode); - ResultCode result = file->Open(); - if (result.IsError()) - return result; - return MakeResult>(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(std::move(file), mode); + return MakeResult>(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(path, mode_string.c_str()); - if (file->IsOpen()) - return RESULT_SUCCESS; - return ResultCode(ErrorDescription::FS_NotFound, ErrorModule::FS, ErrorSummary::NotFound, - ErrorLevel::Status); -} - ResultVal 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 Read(u64 offset, size_t length, u8* buffer) const override; ResultVal 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 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 @@ -18,12 +18,6 @@ public: FileBackend() {} 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 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 file, u64 offset, u64 size) : romfs_file(file), data_offset(offset), data_size(size) {} - ResultCode Open() override { - return RESULT_SUCCESS; - } ResultVal Read(u64 offset, size_t length, u8* buffer) const override; ResultVal Write(u64 offset, size_t length, bool flush, const u8* buffer) const override; u64 GetSize() const override; -- cgit v1.2.3 From f775a3781bde276707b27ec220f4c7ca18062767 Mon Sep 17 00:00:00 2001 From: wwylele Date: Wed, 19 Oct 2016 21:42:47 +0800 Subject: FileSys: remove unused DiskArchive All "subclasses" of DiskArchive are splitted out. This class is useless --- src/core/file_sys/disk_archive.cpp | 147 ------------------------------------- src/core/file_sys/disk_archive.h | 32 -------- 2 files changed, 179 deletions(-) (limited to 'src/core/file_sys/disk_archive.h') diff --git a/src/core/file_sys/disk_archive.cpp b/src/core/file_sys/disk_archive.cpp index 0e51fd1a6..a243d9a13 100644 --- a/src/core/file_sys/disk_archive.cpp +++ b/src/core/file_sys/disk_archive.cpp @@ -15,153 +15,6 @@ namespace FileSys { -ResultVal> 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 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(std::move(file), mode); - return MakeResult>(std::move(disk_file)); -} - -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); -} - -ResultCode DiskArchive::RenameFile(const Path& src_path, const Path& dest_path) const { - if (FileUtil::Rename(mount_point + src_path.AsString(), mount_point + dest_path.AsString())) - return RESULT_SUCCESS; - - // TODO(yuriks): This code probably isn't right, it'll return a Status even if the file didn't - // exist or similar. Verify. - return ResultCode(ErrorDescription::NoData, ErrorModule::FS, // TODO: verify description - ErrorSummary::NothingHappened, ErrorLevel::Status); -} - -ResultCode DiskArchive::DeleteDirectory(const Path& path) const { - if (FileUtil::DeleteDir(mount_point + path.AsString())) - return RESULT_SUCCESS; - return ResultCode(ErrorDescription::NoData, ErrorModule::FS, // TODO: verify description - ErrorSummary::Canceled, ErrorLevel::Status); -} - -ResultCode DiskArchive::DeleteDirectoryRecursively(const Path& path) const { - if (FileUtil::DeleteDirRecursively(mount_point + path.AsString())) - return RESULT_SUCCESS; - return ResultCode(ErrorDescription::NoData, ErrorModule::FS, // TODO: verify description - ErrorSummary::Canceled, ErrorLevel::Status); -} - -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::FS_AlreadyExists, ErrorModule::FS, - ErrorSummary::NothingHappened, ErrorLevel::Status); - - if (size == 0) { - FileUtil::CreateEmptyFile(full_path); - return RESULT_SUCCESS; - } - - FileUtil::IOFile file(full_path, "wb"); - // Creates a sparse file (or a normal file on filesystems without the concept of sparse files) - // We do this by seeking to the right size, then writing a single null byte. - if (file.Seek(size - 1, SEEK_SET) && file.WriteBytes("", 1) == 1) - return RESULT_SUCCESS; - - return ResultCode(ErrorDescription::TooLarge, ErrorModule::FS, ErrorSummary::OutOfResource, - ErrorLevel::Info); -} - -ResultCode DiskArchive::CreateDirectory(const Path& path) const { - if (FileUtil::CreateDir(mount_point + path.AsString())) - return RESULT_SUCCESS; - return ResultCode(ErrorDescription::NoData, ErrorModule::FS, // TODO: verify description - ErrorSummary::Canceled, ErrorLevel::Status); -} - -ResultCode DiskArchive::RenameDirectory(const Path& src_path, const Path& dest_path) const { - if (FileUtil::Rename(mount_point + src_path.AsString(), mount_point + dest_path.AsString())) - return RESULT_SUCCESS; - - // TODO(yuriks): This code probably isn't right, it'll return a Status even if the file didn't - // exist or similar. Verify. - return ResultCode(ErrorDescription::NoData, ErrorModule::FS, // TODO: verify description - ErrorSummary::NothingHappened, ErrorLevel::Status); -} - -ResultVal> DiskArchive::OpenDirectory(const Path& path) const { - LOG_DEBUG(Service_FS, "called path=%s", path.DebugStr().c_str()); - auto full_path = mount_point + path.AsString(); - if (!FileUtil::IsDirectory(full_path)) - return ResultCode(ErrorDescription::FS_NotFound, ErrorModule::FS, ErrorSummary::NotFound, - ErrorLevel::Permanent); - auto directory = std::make_unique(full_path); - return MakeResult>(std::move(directory)); -} - -u64 DiskArchive::GetFreeBytes() const { - // TODO: Stubbed to return 1GiB - return 1024 * 1024 * 1024; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - ResultVal DiskFile::Read(const u64 offset, const size_t length, u8* buffer) const { if (!mode.read_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 c2c3c3b23..eb9166df6 100644 --- a/src/core/file_sys/disk_archive.h +++ b/src/core/file_sys/disk_archive.h @@ -20,38 +20,6 @@ namespace FileSys { -/** - * Helper which implements a backend accessing the host machine's filesystem. - * This should be subclassed by concrete archive types, which will provide the - * base directory on the host filesystem and override any required functionality. - */ -class DiskArchive : public ArchiveBackend { -public: - DiskArchive(const std::string& mount_point_) : mount_point(mount_point_) {} - - virtual std::string GetName() const override { - return "DiskArchive: " + mount_point; - } - - ResultVal> OpenFile(const Path& path, - const Mode& mode) const override; - ResultCode DeleteFile(const Path& path) const override; - ResultCode RenameFile(const Path& src_path, const Path& dest_path) const override; - ResultCode DeleteDirectory(const Path& path) const override; - ResultCode DeleteDirectoryRecursively(const Path& path) const override; - ResultCode CreateFile(const Path& path, u64 size) const override; - ResultCode CreateDirectory(const Path& path) const override; - ResultCode RenameDirectory(const Path& src_path, const Path& dest_path) const override; - ResultVal> OpenDirectory(const Path& path) const override; - u64 GetFreeBytes() const override; - -protected: - friend class DiskFile; - friend class DiskDirectory; - - std::string mount_point; -}; - class DiskFile : public FileBackend { public: DiskFile(FileUtil::IOFile&& file_, const Mode& mode_) -- cgit v1.2.3