libc: a file held by a stream reopened with freopen cannot be renamed or removed on Windows; fix branch ready #5031
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-13T08:55:18Z.
On Windows, a file held by a stream that went through
freopencannot be renamed or removed while the stream is open:renamefails withEIOandremovewithEBUSY. With a stream fromfopen, or a descriptor fromsceKernelOpen, both work, as they do on the console and in the Linux build. A title that reopens its log or save file withfreopenand then rotates or replaces it gets the error.This is the gap docs/dev/debt/libc.md records: "A stream reopened with
freopenstill goes through_wfreopen, which does not share delete access".Cause.
fopenopens the file itself withFILE_SHARE_DELETE(#4090).freopenhands the name to the C runtime's_wfreopen, which opens it without that share mode, so every later rename or delete of the name is a sharing violation.Observed on
mainc2565f014, Windows 11 (10.0.26300), WinLibs GCC 15.2.0 r7, Release. The stream is opened on one file and then reopened withfreopenon a second one, in each of the modesr,rb,r+,w,w+,a,a+:mainon Windowsrename(file, other)remove(file)sceKernelRename, with a descriptor fromsceKernelOpenon the same file as well0x80020005I have not found a title that stops on this. It is the part of the delete-sharing work (#3316 for descriptors, #4090 for
fopen) that the debt file still lists.Fix, ready to cherry-pick.
DavidSamir:fix/libc-freopen-delete-sharing-windows(9203b42ba, three commits onmainc2565f014, three files, +49/-2). I can't open it as a PR while #4532 is open (one open PR without a merged one).fix(libc): share delete access on a stream reopened with freopen on Windows:FileAndHeap.cpp.test(libc): rename and remove a file a reopened stream holds:file_streams.docs(debt): describe how a reopened stream shares delete access.Windows only.
_wfreopenstill does the reopening, so the stream object, its buffer, its mode and its descriptor number are what they are today. After it succeeds,ShareReopenedFileopens the same file once more with read, write and delete sharing and the access the mode asks for, and moves the stream's descriptor onto that handle with_dup2, which closes the handle_wfreopenmade. The append flag is carried over for theamodes. For descriptors 0 to 2 the Win32 standard handle is kept as it was, the wayfreopenon them already keeps it (#4640).If the second open fails, the stream is closed and
freopenreturns null with that error, as it does when the reopening itself fails.Not changed: between
_wfreopenand the_dup2the file is held without delete sharing, so a rename from another thread in that moment still fails. The debt entry now says how the reopened stream gets its handle.file_streamsgains a sequence:fopeninwb, write,freopeninab, write,freopeninr+b; thenrenameandremoveon the name must succeed while the stream is open, and the stream must still read back both writes.Checked on Windows (9203b42ba on
mainc2565f014, Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release, full builds):FileAndHeap.cppas onmainand the test kept,file_streamsfails (exit 0xc0000409,File stream check failed). With the fix it passes 5/5 (--repeat until-fail:5), andguest_standard_streams,guest_filesystem,file_position,guest_file_lock_streams,guest_dup,guest_kernel_fcntlandguest_file_vectorpass 3/3.main's libraries, the branch's and on the Linux build. For each of eight modes (the seven above andwx): same stream object, same descriptor number,ftell,fread,fseek,fwrite,fflush, rename and remove while held, and a read through the stream after the name is gone. Then file contents afterw,aandr+reopens,wxon a missing and on an existing file, a missing file, an invalid mode, a file a descriptor also holds (withflockbetween the two,sceKernelRenameandsceKernelUnlink), and 3000 reopens in a row. 48 lines change againstmain, all of them the renames and removes, and 125 of the 127 equal the Linux build's. The two that do not are the same withmain's libraries:fwriteon a stream reopened inrorrbdoes not set the stream's error indicator on Windows. The 3000 reopens leave the process handle count unchanged.flock, 44 over pipe streams, 15 overfcntlstatus flags and 313 overfdopen, run with both sets of libraries: no line differs, apart from two counters in the last that change from run to run with either set.freopenitself, 4000 reopens per pass: 77 to 106 µs withmain's libraries and 109 to 156 µs with the branch's, in three alternating runs of three passes each, so about 40 µs more perfreopen.freopennow opens the file twice. No other function changes.poll: POLLPRI is not supported by WSAPollin both (#4478); SwanStationPS5 was run four times with each set.ctest --parallel 4: 1037 of 1039 pass. The two that fail,agc_driver_bda_span_readsandagc_driver_pixel_interlock, fail the same way withmainc2565f014built without this change on this machine (RTX 3070); they do not touch this code. A first run of the suite also failedagc_driver_flat_apertures_1,agc_driver_flat_apertures_2andtls_function_coverage_register_address_loads(a 30 s timeout); the three then passed 5/5 on their own and in the second full run, which is the one counted here.file_streams,guest_standard_streams,guest_filesystemandfile_positionpass 3/3.tools/check_conventions.py --base origin/mainpasses.tools/pr_overlap.py --branchreports no overlap with open pull requests.Checked on Linux (same commit, WSL2 Ubuntu 24.04, GCC 13.3.0, Release):
libsbuilds, andfile_streamsandguest_standard_streamspass 3/3. The changed code is Windows only; the new test sequence runs on both and passes on Linux as it is.Not checked on a console or with a commercial title.
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-13T09:05:00Z.
@DavidSamir I've opened your branch as #5033 with your three commits and authorship kept. The fix and the test cherry-picked onto current
main(2a2a3dc65) without changes. The debt commit had one conflict, because #4918 added its_Unwind_Contextentry right after the line you edit, and I kept both entries as they are.I added a Linux run there: Ubuntu 24.04, GCC 13.3, Release. The build is clean,
file_streamspasses 20/20 and all 334guest_*tests pass in two runs. A MinGW-w64-fsyntax-only -Wall -Wextracompile ofFileAndHeap.cppandFileStreams.cppis clean. The Windows results in the PR are yours from this issue; I couldn't run Windows here. I'll keep it up to date withmainand answer review there.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T06:53:58Z.
Closing: no title is known to reach this, and functions are only changed when a title needs them. The catalog titles in the report run the same with and without the change. If a title stops on or misbehaves because of it on current main, comment here with the title ID and the log line.