From ae24bd0701863977f099cbbc2324b869dc6862ed Mon Sep 17 00:00:00 2001 From: wheremyfoodat <44909372+wheremyfoodat@users.noreply.github.com> Date: Sun, 17 Dec 2023 20:35:07 +0200 Subject: [PATCH 1/2] Optionally flush file in File::Write --- src/core/kernel/file_operations.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/core/kernel/file_operations.cpp b/src/core/kernel/file_operations.cpp index c78371005..972190fa6 100644 --- a/src/core/kernel/file_operations.cpp +++ b/src/core/kernel/file_operations.cpp @@ -148,6 +148,11 @@ void Kernel::writeFile(u32 messagePointer, Handle fileHandle) { IOFile f(file->fd); auto [success, bytesWritten] = f.writeBytes(data.get(), size); + // TODO: Should this check only the byte? + if (writeOption) { + f.flush(); + } + mem.write32(messagePointer, IPC::responseHeader(0x0803, 2, 2)); if (!success) { Helpers::panic("Kernel::WriteFile failed"); From a82c03312ad040f912d30835f580bb85b6f3e38d Mon Sep 17 00:00:00 2001 From: wheremyfoodat <44909372+wheremyfoodat@users.noreply.github.com> Date: Sun, 17 Dec 2023 20:47:50 +0200 Subject: [PATCH 2/2] Handle svcClose for file objects --- src/core/kernel/kernel.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/core/kernel/kernel.cpp b/src/core/kernel/kernel.cpp index cd691b485..04af5096e 100644 --- a/src/core/kernel/kernel.cpp +++ b/src/core/kernel/kernel.cpp @@ -179,6 +179,30 @@ u32 Kernel::getTLSPointer() { // Result CloseHandle(Handle handle) void Kernel::svcCloseHandle() { logSVC("CloseHandle(handle = %d) (Unimplemented)\n", regs[0]); + const Handle handle = regs[0]; + + KernelObject* object = getObject(handle); + if (object != nullptr) { + switch (object->type) { + // Close file descriptor when closing a file to prevent leaks and properly flush file contents + case KernelObjectType::File: { + FileSession* file = object->getData(); + if (file->isOpen) { + file->isOpen = false; + + if (file->fd != nullptr) { + fclose(file->fd); + file->fd = nullptr; + } + } + break; + } + + default: break; + } + } + + // Stub to always succeed for now regs[0] = Result::Success; }