diff --git a/src/core/file_sys/archive.h b/src/core/file_sys/archive.h index f3cb11133..f37ae642c 100644 --- a/src/core/file_sys/archive.h +++ b/src/core/file_sys/archive.h @@ -208,6 +208,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 Whether creation of directory succeeded + */ + virtual bool CreateFile(const Path& path, const u32 size) const = 0; + /** * Create a directory specified by its path * @param path Path relative to the archive diff --git a/src/core/file_sys/archive_romfs.cpp b/src/core/file_sys/archive_romfs.cpp index 8c2dbeda5..93a147a05 100644 --- a/src/core/file_sys/archive_romfs.cpp +++ b/src/core/file_sys/archive_romfs.cpp @@ -58,6 +58,11 @@ bool Archive_RomFS::DeleteDirectory(const FileSys::Path& path) const { return false; } +bool Archive_RomFS::CreateFile(const Path& path, const u32 size) const { + ERROR_LOG(FILESYS, "Attempted to create a file in ROMFS."); + return false; +} + /** * Create a directory specified by its path * @param path Path relative to the archive diff --git a/src/core/file_sys/archive_romfs.h b/src/core/file_sys/archive_romfs.h index 222bdc356..83278644b 100644 --- a/src/core/file_sys/archive_romfs.h +++ b/src/core/file_sys/archive_romfs.h @@ -43,6 +43,14 @@ public: */ bool DeleteFile(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 Whether creation of directory succeeded + */ + bool CreateFile(const Path& path, const u32 size) const override; + /** * Rename a File specified by its path * @param src_path Source path relative to the archive diff --git a/src/core/file_sys/archive_sdmc.cpp b/src/core/file_sys/archive_sdmc.cpp index fc0b9b72d..2b70532b0 100644 --- a/src/core/file_sys/archive_sdmc.cpp +++ b/src/core/file_sys/archive_sdmc.cpp @@ -79,6 +79,11 @@ bool Archive_SDMC::DeleteDirectory(const FileSys::Path& path) const { return FileUtil::DeleteDir(GetMountPoint() + path.AsString()); } +bool Archive_SDMC::CreateFile(const FileSys::Path& path, const u32 size) const { + FileUtil::IOFile file(GetMountPoint() + path.AsString(), "wb"); + return file.Seek(size, SEEK_SET); +} + /** * Create a directory specified by its path * @param path Path relative to the archive diff --git a/src/core/file_sys/archive_sdmc.h b/src/core/file_sys/archive_sdmc.h index 19f563a62..52e1b7977 100644 --- a/src/core/file_sys/archive_sdmc.h +++ b/src/core/file_sys/archive_sdmc.h @@ -62,6 +62,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 Whether creation of directory succeeded + */ + bool CreateFile(const Path& path, const u32 size) const override; + /** * Create a directory specified by its path * @param path Path relative to the archive diff --git a/src/core/hle/kernel/archive.cpp b/src/core/hle/kernel/archive.cpp index a875fa7ff..760799a81 100644 --- a/src/core/hle/kernel/archive.cpp +++ b/src/core/hle/kernel/archive.cpp @@ -377,6 +377,16 @@ ResultCode DeleteDirectoryFromArchive(Handle archive_handle, const FileSys::Path ErrorSummary::Canceled, ErrorLevel::Status); } +ResultCode CreateFileFromArchive(Handle archive_handle, const FileSys::Path& path, const u32 file_size) { + Archive* archive = Kernel::g_object_pool.GetFast(archive_handle); + if (archive == nullptr) + return InvalidHandle(ErrorModule::FS); + if (archive->backend->CreateFile(path, file_size)) + return RESULT_SUCCESS; + return ResultCode(ErrorDescription::NoData, ErrorModule::FS, // TODO: verify description + ErrorSummary::Canceled, ErrorLevel::Status); +} + ResultCode CreateDirectoryFromArchive(Handle archive_handle, const FileSys::Path& path) { Archive* archive = Kernel::g_object_pool.GetFast(archive_handle); if (archive == nullptr) diff --git a/src/core/hle/kernel/archive.h b/src/core/hle/kernel/archive.h index b50833a2b..f24b76edc 100644 --- a/src/core/hle/kernel/archive.h +++ b/src/core/hle/kernel/archive.h @@ -71,6 +71,15 @@ ResultCode RenameFileBetweenArchives(Handle src_archive_handle, const FileSys::P */ ResultCode DeleteDirectoryFromArchive(Handle archive_handle, const FileSys::Path& path); +/** + * Create a File from an Archive + * @param archive_handle Handle to an open Archive object + * @param path Path to the File inside of the Archive + * @param file_size The size of the new file, filled with zeroes + * @return Whether creation of directory succeeded + */ +ResultCode CreateFileFromArchive(Handle archive_handle, const FileSys::Path& path, const u32 file_size); + /** * Create a Directory from an Archive * @param archive_handle Handle to an open Archive object diff --git a/src/core/hle/service/fs_user.cpp b/src/core/hle/service/fs_user.cpp index 51e8b579e..c14b7d99f 100644 --- a/src/core/hle/service/fs_user.cpp +++ b/src/core/hle/service/fs_user.cpp @@ -11,6 +11,8 @@ #include "core/hle/service/fs_user.h" #include "core/settings.h" +#include "common/file_util.h" + //////////////////////////////////////////////////////////////////////////////////////////////////// // Namespace FS_User @@ -237,6 +239,39 @@ void DeleteDirectory(Service::Interface* self) { DEBUG_LOG(KERNEL, "called"); } +/* + * FS_User::CreateFile service function + * Inputs: + * 2 : Archive handle lower word + * 3 : Archive handle upper word + * 4 : File path string type + * 5 : File path string size + * 7 : File size (filled with zeroes) + * 10: File path string data + * Outputs: + * 1 : Result of function, 0 on success, otherwise error code + */ +static void CreateFile(Service::Interface* self) { + u32* cmd_buff = Service::GetCommandBuffer(); + + // TODO: cmd_buff[2], aka archive handle lower word, isn't used according to + // 3dmoo's or ctrulib's implementations. Triple check if it's really the case. + Handle archive_handle = static_cast(cmd_buff[3]); + auto filename_type = static_cast(cmd_buff[4]); + u32 filename_size = cmd_buff[5]; + u32 file_size = cmd_buff[7]; + u32 filename_ptr = cmd_buff[10]; + + FileSys::Path file_path(filename_type, filename_size, filename_ptr); + + DEBUG_LOG(KERNEL, "type=%d size=%d data=%s", filename_type, filename_size, file_path.DebugStr().c_str()); + + + cmd_buff[1] = Kernel::CreateFileFromArchive(archive_handle, file_path, file_size).raw; + + DEBUG_LOG(KERNEL, "called"); +} + /* * FS_User::CreateDirectory service function * Inputs: @@ -401,7 +436,7 @@ const Interface::FunctionInfo FunctionTable[] = { {0x08050244, RenameFile, "RenameFile"}, {0x08060142, DeleteDirectory, "DeleteDirectory"}, {0x08070142, nullptr, "DeleteDirectoryRecursively"}, - {0x08080202, nullptr, "CreateFile"}, + {0x08080202, CreateFile, "CreateFile"}, {0x08090182, CreateDirectory, "CreateDirectory"}, {0x080A0244, RenameDirectory, "RenameDirectory"}, {0x080B0102, OpenDirectory, "OpenDirectory"},