diff --git a/src/core/kernel/file_operations.cpp b/src/core/kernel/file_operations.cpp index c7837100..972190fa 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"); diff --git a/src/core/kernel/kernel.cpp b/src/core/kernel/kernel.cpp index cd691b48..04af5096 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; }