diff options
Diffstat (limited to 'src/core/file_sys')
-rw-r--r-- | src/core/file_sys/archive_backend.h | 19 | ||||
-rw-r--r-- | src/core/file_sys/archive_romfs.cpp | 11 | ||||
-rw-r--r-- | src/core/file_sys/archive_romfs.h | 8 | ||||
-rw-r--r-- | src/core/file_sys/disk_archive.cpp | 21 | ||||
-rw-r--r-- | src/core/file_sys/disk_archive.h | 1 |
5 files changed, 57 insertions, 3 deletions
diff --git a/src/core/file_sys/archive_backend.h b/src/core/file_sys/archive_backend.h index 065b22e5d..eb1fdaa1f 100644 --- a/src/core/file_sys/archive_backend.h +++ b/src/core/file_sys/archive_backend.h @@ -143,7 +143,16 @@ public: case Char: return std::vector<u8>(string.begin(), string.end()); case Wchar: - return std::vector<u8>(u16str.begin(), u16str.end()); + { + // use two u8 for each character of u16str + std::vector<u8> to_return(u16str.size() * 2); + for (size_t i = 0; i < u16str.size(); ++i) { + u16 tmp_char = u16str.at(i); + to_return[i*2] = (tmp_char & 0xFF00) >> 8; + to_return[i*2 + 1] = (tmp_char & 0x00FF); + } + return to_return; + } case Empty: return {}; default: @@ -200,6 +209,14 @@ public: virtual bool DeleteDirectory(const FileSys::Path& path) const = 0; /** + * Create a file specified by its path + * @param path Path relative to the Archive + * @param size The size of the new file, filled with zeroes + * @return File creation result code + */ + virtual ResultCode CreateFile(const Path& path, u32 size) const = 0; + + /** * Create a directory specified by its path * @param path Path relative to the archive * @return Whether the directory could be created diff --git a/src/core/file_sys/archive_romfs.cpp b/src/core/file_sys/archive_romfs.cpp index 6ef6ea2fb..ced0794ef 100644 --- a/src/core/file_sys/archive_romfs.cpp +++ b/src/core/file_sys/archive_romfs.cpp @@ -5,6 +5,7 @@ #include <memory> #include "common/common_types.h" +#include "common/make_unique.h" #include "core/file_sys/archive_romfs.h" #include "core/file_sys/directory_romfs.h" @@ -29,7 +30,7 @@ Archive_RomFS::Archive_RomFS(const Loader::AppLoader& app_loader) { * @return Opened file, or nullptr */ std::unique_ptr<FileBackend> Archive_RomFS::OpenFile(const Path& path, const Mode mode) const { - return std::make_unique<File_RomFS>(this); + return Common::make_unique<File_RomFS>(this); } /** @@ -57,6 +58,12 @@ bool Archive_RomFS::DeleteDirectory(const FileSys::Path& path) const { return false; } +ResultCode Archive_RomFS::CreateFile(const Path& path, u32 size) const { + LOG_WARNING(Service_FS, "Attempted to create a file in ROMFS."); + // TODO: Verify error code + return ResultCode(ErrorDescription::NotAuthorized, ErrorModule::FS, ErrorSummary::NotSupported, ErrorLevel::Permanent); +} + /** * Create a directory specified by its path * @param path Path relative to the archive @@ -78,7 +85,7 @@ bool Archive_RomFS::RenameDirectory(const FileSys::Path& src_path, const FileSys * @return Opened directory, or nullptr */ std::unique_ptr<DirectoryBackend> Archive_RomFS::OpenDirectory(const Path& path) const { - return std::make_unique<Directory_RomFS>(); + return Common::make_unique<Directory_RomFS>(); } } // namespace FileSys diff --git a/src/core/file_sys/archive_romfs.h b/src/core/file_sys/archive_romfs.h index 564c23f70..2fafd0d2a 100644 --- a/src/core/file_sys/archive_romfs.h +++ b/src/core/file_sys/archive_romfs.h @@ -54,6 +54,14 @@ public: bool DeleteDirectory(const FileSys::Path& path) const override; /** + * Create a file specified by its path + * @param path Path relative to the Archive + * @param size The size of the new file, filled with zeroes + * @return File creation result code + */ + ResultCode CreateFile(const Path& path, u32 size) const override; + + /** * Create a directory specified by its path * @param path Path relative to the archive * @return Whether the directory could be created diff --git a/src/core/file_sys/disk_archive.cpp b/src/core/file_sys/disk_archive.cpp index fc9ee4acf..1689a1a91 100644 --- a/src/core/file_sys/disk_archive.cpp +++ b/src/core/file_sys/disk_archive.cpp @@ -35,6 +35,27 @@ bool DiskArchive::DeleteDirectory(const FileSys::Path& path) const { return FileUtil::DeleteDir(GetMountPoint() + path.AsString()); } +ResultCode DiskArchive::CreateFile(const FileSys::Path& path, u32 size) const { + std::string full_path = GetMountPoint() + path.AsString(); + + if (FileUtil::Exists(full_path)) + return ResultCode(ErrorDescription::AlreadyExists, ErrorModule::FS, ErrorSummary::NothingHappened, ErrorLevel::Info); + + 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); +} + + bool DiskArchive::CreateDirectory(const Path& path) const { return FileUtil::CreateDir(GetMountPoint() + path.AsString()); } diff --git a/src/core/file_sys/disk_archive.h b/src/core/file_sys/disk_archive.h index 73fce2fce..c1784e870 100644 --- a/src/core/file_sys/disk_archive.h +++ b/src/core/file_sys/disk_archive.h @@ -28,6 +28,7 @@ public: bool DeleteFile(const FileSys::Path& path) const override; bool RenameFile(const FileSys::Path& src_path, const FileSys::Path& dest_path) const override; bool DeleteDirectory(const FileSys::Path& path) const override; + ResultCode CreateFile(const Path& path, u32 size) const override; bool CreateDirectory(const Path& path) const override; bool RenameDirectory(const FileSys::Path& src_path, const FileSys::Path& dest_path) const override; std::unique_ptr<DirectoryBackend> OpenDirectory(const Path& path) const override; |