Skip to content

util: Port platform-file.c helpers to obs-util - #1444

Merged
zackees merged 2 commits into
obs-rust:masterfrom
swilk19:port/platform-file-rust
Oct 18, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
swilk19:port/platform-file-rust

Conversation

@swilk19

@swilk19 swilk19 commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Refs #262 (slice 3, files).

Follows #1443 (the verbatim move into platform-file.c, merged).

RED (util: Test platform-file.c helpers against Rust stubs): rust/obs-util/src/platform_file.rs with todo!() cores and C ABI shims in ffi/platform_file.rs for os_fopen, os_wfopen, os_fgetsize, os_stat (Windows), os_fseeki64/os_ftelli64, os_fread_mbs/os_fread_utf8, os_quick_read_*/os_quick_write_* (incl. _safe) and os_get_file_size. Aborts on todo!("os_quick_write_utf8_file port (#262)").

  • Tier 1 (tests/platform/platform_file.rs): 1:1 ports of test_platform_file.c and the platform-file cases of test_platform.c.
  • Tier 3 (tests/platform/platform_file_parity.rs): reads (BOM edge cases, embedded NULs, high bytes), writes, NULL and missing paths, seek/tell/size incl. past 4 GiB, every safe-write temp/backup extension combination with and without an existing target, wide paths, and os_stat on Windows, all against the real platform-file.c compiled as oracle_*. Streams opened by one side are read by the other, since C callers mix them with their own stdio. Proptests over random contents.
  • Tier 2: ENABLE_RUST_LIBOBS=ON drops util/platform-file.c (new libobs.ported-platform group, same libobs-platform.pch.h); exports in rust-exports.d/obs-util-platform_file.txt.
  • Oracle: the Windows plain-name forwarders for os_fopen, os_fseeki64, os_ftelli64 and os_quick_*_utf8_file leave platform_host.c (and ci/test_oracle_standins.py), since obs-util now defines those symbols; os_stat becomes oracle_os_stat on Windows for the same reason.

GREEN (util: Port platform-file.c helpers to obs-util): the streams are the C library's own FILE *, opened through a CFile type in the platform boundary (platform/{unix,windows}/file.rs): fopen/fseeko/ftello on Unix; _wfopen/_fseeki64/_ftelli64/_wstat64i32 on Windows (MSVC branch), with paths converted by the ported utf8_to_wchar. The cores follow the C step for step.

Design note vs. my claim: I said Windows os_fopen would keep calling the C os_utf8_to_wcs_ptr (slice 1). obs-util's test binaries don't link platform.c, so instead the platform layer calls the ported utf8_to_wchar directly, the function os_utf8_to_wcs_ptr wraps. The _mbs_ helpers likewise do mbstowcs/wcstombs + utf8_to_wchar/wchar_to_utf8 privately, the way os_mbs_to_utf8_ptr/os_utf8_to_mbs_ptr compose them. No slice 1 symbol is exported or changed; slice 1 can later swap these private helpers for its port.

Intentional differences, documented on the shims: where C would crash (a NULL FILE *, mode or pstr, a NULL path to os_quick_write_utf8_file_safe, NULL str with a nonzero length), the shim returns the failure value.

Local checks:

  • ./unittest green: Rust Tiers 1–3, exported symbols C vs Rust IDENTICAL (1766), and 307/307 cmocka tests against both libobs and libobs-with-rust.
  • Windows (xwin, MSVC target): all 34 platform-file tests pass, including os_stat; full obs-util is 533 passed with the same 9 WSL-only failures (UNC working directory, interop process spawns) as master; obs-data 32/32.
  • clippy (host + Windows + macOS targets), rustfmt and the Dylints are clean.
  • Mutation check: dropping the BOM skip, seeking back one byte off in os_fgetsize, or inverting the leading-. extension rule each fails Tier 1 and Tier 3 cases.

The rust/obs-util/src/platform/windows/ paths start the Windows and macOS Rust and Tier 2 lanes here.

🤖 Generated with Claude Code

swilk19 and others added 2 commits October 18, 2026 18:56
Refs obs-rust#262 (slice 3, files).

rust/obs-util/src/platform_file.rs with todo!() cores and C ABI shims
in ffi/platform_file.rs for os_fopen, os_wfopen, os_fgetsize, os_stat
(Windows), os_fseeki64, os_ftelli64, os_fread_mbs, os_fread_utf8, the
os_quick_read_*/os_quick_write_* helpers and os_get_file_size. The
streams are the C library's own FILE *, opened through CFile in the
platform boundary (platform/{unix,windows}/file.rs).

- Tier 1 (tests/platform/platform_file.rs): 1:1 ports of
  test_platform_file.c and of the platform-file cases in
  test_platform.c. Aborts on todo!("os_quick_write_utf8_file port").
- Tier 3 (tests/platform/platform_file_parity.rs): reads, writes, NULL
  and missing paths, seek/tell/size, every safe-write extension
  combination, wide paths and (Windows) os_stat against the real
  platform-file.c compiled as oracle_*, with streams crossed between the
  sides, plus proptests over random contents.
- Tier 2: ENABLE_RUST_LIBOBS=ON drops util/platform-file.c (new
  libobs.ported-platform group); exports in
  rust-exports.d/obs-util-platform_file.txt.
- Oracle: the Windows forwarders for os_fopen, os_fseeki64,
  os_ftelli64 and os_quick_*_utf8_file leave platform_host.c, since
  obs-util now defines those names; os_stat is renamed oracle_os_stat on
  Windows for the same reason.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Refs obs-rust#262 (slice 3, files).

The cores follow platform-file.c step for step over the C library's
stdio: fopen/fseeko/ftello on Unix, _wfopen/_fseeki64/_ftelli64/_wstat
on Windows with paths converted by the ported utf8_to_wchar, and
mbstowcs/wcstombs for the _mbs_ helpers (the locale conversions of
platform.c's os_mbs_to_utf8_ptr/os_utf8_to_mbs_ptr, which stay C).
os_quick_write_utf8_file_safe replaces through the ported
os_safe_replace and logs the same two errors.

Mutation check: dropping the BOM skip, seeking back one byte off in
os_fgetsize, or inverting the leading-'.' extension rule each fails
Tier 1 and Tier 3 cases.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@swilk19
swilk19 force-pushed the port/platform-file-rust branch from ba93944 to a3da6d0 Compare October 18, 2026 22:56
@swilk19
swilk19 marked this pull request as ready for review October 18, 2026 23:09
@swilk19
swilk19 requested a review from zackees as a code owner October 18, 2026 23:09

@zackees zackees left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coordinator review: LGTM.

Checked the core against platform-file.c.h on master:

  • quick_write_utf8_file: the BOM and data writes have the same fwrite(…, n, 1) shapes. Early failures close the stream through CFile's Drop, as C's fclose before return false does, and success is fclose == 0.
  • quick_write_mbs_file: a NULL or empty input writes nothing and succeeds, and an fclose failure makes the call fail, as in C.
  • quick_write_utf8_file_safe: rejects a NULL or empty temp extension, builds path[.]ext the way the dstr code does, skips an empty backup extension, and calls os_safe_replace with the same three arguments.
  • fread_utf8 returns nothing for an empty or BOM-only file; fgetsize restores the position and keeps errno.

The test coverage is strong: Tier 1 ports of both C tests, and Tier 3 parity against the real platform-file.c (cross-side stream use, past-4 GiB seek, every temp/backup combination, proptests). Tier 2 drops the C file through the libobs.ported-platform group.

@zackees
zackees added this pull request to the merge queue Oct 18, 2026
Merged via the queue into obs-rust:master with commit ae897cf Oct 18, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants