libkernel: fsync and fdatasync fail on read-only and directory descriptors on Windows; fix branch ready #3317

Closed
opened 2026-10-11 07:14:39 +00:00 by DavidSamir · 0 comments
DavidSamir commented 2026-10-11 07:14:39 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-11T07:14:39Z.


On Windows, fsync and fdatasync (and sceKernelFsync, sceKernelFdatasync) fail with EBADF on a descriptor the guest opened read-only and on a directory descriptor. FreeBSD's fsync(2) lists EBADF only for "not a valid descriptor" and has no error for the open mode or for directories; the Linux build returns 0 for both.

Opening a directory and calling fsync on it is the usual way to make a created or renamed file durable, so a title that checks the result sees its save or cache step fail.

Cause. sceKernelFsync and sceKernelFdatasync call _commit. FlushFileBuffers needs write access to the handle, and _commit reports its failure as EBADF. A directory descriptor is a placeholder descriptor on NUL, which cannot be flushed either.

Observed on main 1690db9a2, Windows 11 (10.0.26300), WinLibs GCC 15.2.0 r7, Release:

call Windows FreeBSD, Linux build
sceKernelFsync(fd), file opened O_RDONLY -1, errno 9 0
fsync(fd), file opened O_RDONLY -1, errno 9 0
sceKernelFdatasync(fd), file opened O_RDONLY 0x80020009 (EBADF) 0
sceKernelFsync(fd), directory -1, errno 9 0
sceKernelFdatasync(fd), directory 0x80020009 (EBADF) 0
any of them, file opened O_RDWR 0 0

I have not found a title that stops on this; it was found by probing the file functions, not from a title.

Fix, ready to cherry-pick. DavidSamir:fix/libkernel-fsync-read-only-windows (d9bafbde, one commit on main 1690db9a2, two files, +30/-2). I can't open it as a PR because of the intake check.

Windows only, in a NativeSync helper both functions call:

  • A directory descriptor is flushed through a handle opened on its path with GENERIC_WRITE and FILE_FLAG_BACKUP_SEMANTICS, the way FlushPath already does for sceKernelSync.
  • Otherwise _commit runs first, so a writable descriptor behaves exactly as before. When it fails and the descriptor is an open disk file, the file is flushed through a write handle from ReOpenFile.
  • When that write handle is refused with access denied (the file has the read-only attribute), the call returns 0, as FlushPath does: no handle that could flush it can be opened.
  • A closed or out-of-range descriptor and a descriptor that is not a disk file still return EBADF, and errno is left untouched on success.

guest_filesystem gains checks: sceKernelFsync, fdatasync and sceKernelFdatasync return 0 on a directory descriptor and on a file opened read-only, and sceKernelFsync returns 0 on a read-only descriptor of a file made read-only with sceKernelChmod(0400). The existing checks for a closed descriptor, a socket and descriptor 0x7fffffff are unchanged and still pass.

Checked on Windows (d9bafbde on main 1690db9a2, WinLibs GCC 15.2.0 r7, Release):

  • With the fix reverted and the new checks kept, guest_filesystem fails at the directory sceKernelFsync check (guest errno 9). With the fix it passes 5/5 (--repeat until-fail:5).
  • A Win32 probe of each path on this machine: FlushFileBuffers on a directory handle opened as above succeeds; on a read-only handle it fails with access denied; on the ReOpenFile write handle it succeeds; ReOpenFile for write on a read-only-attribute file fails with access denied.
  • Titles from the homebrew catalog, run for 25 s with the libraries from main 0e90079f and from the branch: DOOM (PPSA99666), OpenRCT2 (PPSA99728), ScreenTester5 (PPSA69420) and Prospero Vibrate (PPSA99920) keep running with the same log line counts; Yamagi Quake II (PPSA99007) exits with code 0 in both; Middle Ages Ping Pong (PPSA28319, Unity) stops at the same multisampling check in both.
  • Full ctest --parallel 4 passes 770/770 (5 skipped, no display in the session). check_conventions.py --base origin/main passes; pr_overlap.py --branch reports no overlap with open pull requests.

Checked on Linux (same commit, WSL2 Ubuntu 24.04, GCC 13.3.0, Release): guest_filesystem passes 3/3, so the new checks match the existing Linux behaviour. Only that test was built and run there.

Not checked on a console. That the data reaches the disk was not measured; only the return values and the Win32 calls above were.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5798902826 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/3317). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-11T07:14:39Z. --- On Windows, `fsync` and `fdatasync` (and `sceKernelFsync`, `sceKernelFdatasync`) fail with `EBADF` on a descriptor the guest opened read-only and on a directory descriptor. FreeBSD's [fsync(2)](https://man.freebsd.org/cgi/man.cgi?query=fsync&sektion=2) lists `EBADF` only for "not a valid descriptor" and has no error for the open mode or for directories; the Linux build returns 0 for both. Opening a directory and calling `fsync` on it is the usual way to make a created or renamed file durable, so a title that checks the result sees its save or cache step fail. **Cause.** [`sceKernelFsync` and `sceKernelFdatasync`](https://github.com/boykopovar/AnyPS5/blob/1690db9a2/core/libs/prx/libkernel/File/src/Stdio.cpp#L655-L672) call `_commit`. `FlushFileBuffers` needs write access to the handle, and `_commit` reports its failure as `EBADF`. A directory descriptor is a placeholder descriptor on `NUL`, which cannot be flushed either. Observed on `main` 1690db9a2, Windows 11 (10.0.26300), WinLibs GCC 15.2.0 r7, Release: | call | Windows | FreeBSD, Linux build | |---|---|---| | `sceKernelFsync(fd)`, file opened `O_RDONLY` | -1, errno 9 | 0 | | `fsync(fd)`, file opened `O_RDONLY` | -1, errno 9 | 0 | | `sceKernelFdatasync(fd)`, file opened `O_RDONLY` | `0x80020009` (EBADF) | 0 | | `sceKernelFsync(fd)`, directory | -1, errno 9 | 0 | | `sceKernelFdatasync(fd)`, directory | `0x80020009` (EBADF) | 0 | | any of them, file opened `O_RDWR` | 0 | 0 | I have not found a title that stops on this; it was found by probing the file functions, not from a title. **Fix, ready to cherry-pick.** [`DavidSamir:fix/libkernel-fsync-read-only-windows`](https://github.com/boykopovar/AnyPS5/compare/main...DavidSamir:AnyPS5:fix/libkernel-fsync-read-only-windows) (d9bafbde, one commit on `main` 1690db9a2, two files, +30/-2). I can't open it as a PR because of the intake check. Windows only, in a `NativeSync` helper both functions call: - A directory descriptor is flushed through a handle opened on its path with `GENERIC_WRITE` and `FILE_FLAG_BACKUP_SEMANTICS`, the way `FlushPath` already does for `sceKernelSync`. - Otherwise `_commit` runs first, so a writable descriptor behaves exactly as before. When it fails and the descriptor is an open disk file, the file is flushed through a write handle from `ReOpenFile`. - When that write handle is refused with access denied (the file has the read-only attribute), the call returns 0, as `FlushPath` does: no handle that could flush it can be opened. - A closed or out-of-range descriptor and a descriptor that is not a disk file still return `EBADF`, and `errno` is left untouched on success. `guest_filesystem` gains checks: `sceKernelFsync`, `fdatasync` and `sceKernelFdatasync` return 0 on a directory descriptor and on a file opened read-only, and `sceKernelFsync` returns 0 on a read-only descriptor of a file made read-only with `sceKernelChmod(0400)`. The existing checks for a closed descriptor, a socket and descriptor `0x7fffffff` are unchanged and still pass. **Checked on Windows** (d9bafbde on `main` 1690db9a2, WinLibs GCC 15.2.0 r7, Release): - With the fix reverted and the new checks kept, `guest_filesystem` fails at the directory `sceKernelFsync` check (guest errno 9). With the fix it passes 5/5 (`--repeat until-fail:5`). - A Win32 probe of each path on this machine: `FlushFileBuffers` on a directory handle opened as above succeeds; on a read-only handle it fails with access denied; on the `ReOpenFile` write handle it succeeds; `ReOpenFile` for write on a read-only-attribute file fails with access denied. - Titles from the homebrew catalog, run for 25 s with the libraries from `main` 0e90079f and from the branch: DOOM (PPSA99666), OpenRCT2 (PPSA99728), ScreenTester5 (PPSA69420) and Prospero Vibrate (PPSA99920) keep running with the same log line counts; Yamagi Quake II (PPSA99007) exits with code 0 in both; Middle Ages Ping Pong (PPSA28319, Unity) stops at the same multisampling check in both. - Full `ctest --parallel 4` passes 770/770 (5 skipped, no display in the session). `check_conventions.py --base origin/main` passes; `pr_overlap.py --branch` reports no overlap with open pull requests. **Checked on Linux** (same commit, WSL2 Ubuntu 24.04, GCC 13.3.0, Release): `guest_filesystem` passes 3/3, so the new checks match the existing Linux behaviour. Only that test was built and run there. Not checked on a console. That the data reaches the disk was not measured; only the return values and the Win32 calls above were. AI-assisted: yes
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5#3317
No description provided.