Skip to content

util: Port os_generate_formatted_filename to obs-util - #1511

Merged
zackees merged 3 commits into
obs-rust:masterfrom
rdoupe:rust/port-platform-filename
Oct 20, 2026
Merged

zackees merged 3 commits into
obs-rust:masterfrom
rdoupe:rust/port-platform-filename

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator

Refs #262 (slice 5, filenames).

What and why

os_generate_formatted_filename builds recording and replay file names from a format such as %CCYY-%MM-%DD %hh-%mm-%ss. #1510 moved it, with its statics valid_string, replace_text and erase_ch, verbatim into libobs/util/platform-filename.c and pinned it with 17 cases in test_formatted_filename. This PR ports it to obs-util::platform_filename, and libobs-with-rust drops util/platform-filename.c.

  • Safe core (rust/obs-util/src/platform_filename.rs): generate_formatted_filename(extension, space, format, now, video) follows the C step for step. It scans once, left to right. At each position it tries the 23-entry specifier table in the C's order. A strftime result that is empty tries the next entry, and one of only spaces is skipped over without being put in, as in C. Then it tries %FPS, %CRES, %ORES, %VF and %s. A replacement is never scanned again. A % that starts nothing is dropped. After the scan, spaces become _ unless space is set, the extension goes after a ., and the name is cut to 255 bytes on a UTF-8 boundary. An empty name is NULL.
  • Same host calls as C: the date specifiers go through the host C library's localtime and strftime (platform::crt::LocalTime, Unix and Windows; on Windows _time64/_localtime64, which the CRT's inline time/localtime call). %FPS's %.2f goes through the host snprintf (DoubleFormat::F2 in platform/stdio.rs). So names, abbreviations, offsets and rounding are the host's, as in C. These are the only platform/ changes.
  • Video info: the shim calls libobs's own obs_get_video_info C API, once per call and before the format is looked at, as the C does. get_video_format_name is a table in the core, because obs-media-io depends on obs-util; parity checks it against the C inline for every value.
  • Shim (src/ffi/platform_filename.rs): #[repr(C)] struct obs_video_info, NULL extension/format read as empty, and a bmalloced result. The export is in rust-exports.d/obs-util-platform_filename.txt, and util/platform-filename.c moves into libobs.ported-platform. That move was still to do after util: Move os_generate_formatted_filename into platform-filename.c #1510.
  • Kept, characterized, not endorsed (test_formatted_filename is unchanged and passes):
  • Intentional differences (listed in the parity test header and the new rust/README.md row):

Tier 2 cannot see the swap here. test_formatted_filename compiles platform-filename.c into itself, so its time() and obs_get_video_info fixtures replace libc's and libobs's. It tests the C source in both lanes. No other C test calls the function through libobs. So the Tier 3 parity test is what proves the Rust swap matches the C, together with the real-library run below.

RED → GREEN

  • RED commit: 6e15927 util: Test os_generate_formatted_filename against a Rust stub (RED). 22 of the 23 Tier 1 cases fail on todo!(). now_at_reads_localtime (platform boundary only) and the layout test pass. The parity run aborts when the panic crosses the extern "C" shim.
    • Tier 1 (tests/platform/platform_filename.rs): a 1:1 port of the 17 cmocka cases, on a fixed struct tm for the same instant (2025-11-07 16:09:03 at UTC+05:30, zone OBS), so nothing depends on the process's time zone. The cmocka date case is split in two. %z, %Z and the other date specifiers are also checked in zone_specifiers, which runs in a child of the test binary started with TZ=OBS-5:30 and goes through localtime of the fixed instant (the shim's path). That covers every host: glibc reads %z/%Z from the struct tm, but macOS's %z and the Windows CRT's both read the C library's zone. A follow-up commit, d72e264, fixed this after macOS CI showed +0000 for a hand-built struct tm. New cases: the format-name table and unknown values; an empty strftime result (no struct tm); blank conversions (a zone of spaces is skipped over and the specifier stays, so %Z%Zx stays %Z%Zx); adjacent specifiers.
    • Tier 2 layout (platform_filename_layout.rs): the size and alignment of struct obs_video_info, and all 13 offsets, against the C header.
    • Tier 3 (platform_filename_parity.rs): oracle/platform.c gives the real platform-filename.c a settable time() (a #define around its #include only) and an obs_get_video_info fixture. The old "no video" stub in platform_host.c is replaced by that fixture. The plain obs_get_video_info that the Rust shim calls reads the same fixture, and both sides count their calls. The test compares:
      • the core against the C, given the same time and video info: every characterization format × space × 4 extensions; %FPS for 16 rates; %VF and the name table for -5..40; 240 to 260 byte names with multibyte, 4-byte and invalid tails; times from 1970 to 2999; and a proptest (1024 cases) of random formats built from every specifier, near-misses, stray %, spaces, multibyte characters and arbitrary bytes, random extensions, space, times and video infos;
      • the C ABI shim with the real clock against the C set to the second the shim read (retried if the second changes), with fixed formats, NULL format and extension, the bug: util: os_generate_formatted_filename uses video info that obs_get_video_info did not fill #1509 "no video" fixture, and a proptest (128 cases). Each side must read the video info exactly once per call.
  • GREEN: 78ef98f util: Port os_generate_formatted_filename to obs-util (GREEN). tests::platform::platform_filename* 31/31 (also at d72e264). obs-util lib harness 668/668. The filename tests also pass under TZ=UTC, OBS-5:30, America/New_York and Australia/Lord_Howe (a 30-minute DST shift), and with LC_ALL=de_DE.UTF-8 (the test process stays in the C locale, as C does without setlocale).
  • Mutation check. Each mutant fails these tests:
    • fps_den <= 1 changed to == 1: 5 failures (Tier 1 fps_formatting, 4 parity tests);
    • an unknown % kept instead of dropped: 8 (4 Tier 1, 4 parity);
    • the UTF-8 back-up limit moved from 252 to 251: 2 (Tier 1 invalid_continuation_bytes, parity long_names_and_utf8_cut);
    • a blank strftime result put in (no valid_string): 1 (Tier 1 blank_conversions). The C locale has no blank conversion, so parity cannot catch this one. Tier 1 makes one with a zone of spaces.
  • Local, two build trees (ENABLE_RUST_LIBOBS=OFF and ON), each running the cmocka tests against libobs and against libobs-with-rust:
Lane libobs libobs-with-rust Exported symbols
C (ENABLE_RUST_LIBOBS=OFF) 322/322 322/322 IDENTICAL (1766)
Rust (ENABLE_RUST_LIBOBS=ON) 322/322 322/322 IDENTICAL (1766)
  • 7 frontend tests (test_config_migration_*, test_virtual_cam_shutdown) were not built in these trees, because I built only the cmocka-tests, libobs and libobs-with-rust targets. Nothing was skipped. test_formatted_filename passes in all four runs, but it tests the C source each time (see above).
  • In libobs-with-rust, the C statics (replace_text and the others) are gone and os_generate_formatted_filename is the Rust one. libobs keeps the C.
  • Real library, headless (private Xvfb, private D-Bus, fresh HOME): a small program starts libobs without obs_reset_video, dirties the stack and formats three names. It was run against the Rust lane's libobs and libobs-with-rust:
Library Format Name
libobs (C) %CCYY-%MM-%DD %hh-%mm-%ss %a %b %p %z %Z %s, TZ=OBS-5:30 2026-10-20 14-21-43 Tue Oct PM +0530 OBS 1792486303.mkv
libobs-with-rust same 2026-10-20 14-21-46 Tue Oct PM +0530 OBS 1792486306.mkv (run 3 s later)
libobs (C) %%x%q%, space false %xq.mkv
libobs-with-rust same %xq.mkv
libobs (C) %FPS %CRES %ORES %VF, no video (#1509) 1.00 2880154539x2880154539 2880154539x2880154539 None.mkv (the stack's 0xABABABAB)
libobs-with-rust same 0 0x0 0x0 None.mkv (zeroed struct, the intentional difference)
  • ci/local_gate.py --lane lint (clang-format, gersemi, rustfmt, clippy -D warnings, Dylints, build groups, test-binary cap, ifdef ratchet): passed. --lane cross-release (clippy -D warnings and Dylints for aarch64-pc-windows-msvc, x86_64-apple-darwin and aarch64-unknown-linux-gnu; the time_t/c_long casts are where ci: release lint fails on aarch64: unnecessary_cast on wchar_t/c_char in obs-util #1484 broke before): passed. --lane cross (clippy and Dylints for x86_64-pc-windows-msvc and aarch64-apple-darwin, plus a link of every test binary): passed. Every workspace test binary links on Linux too (cargo test --workspace --no-run).
  • Real app: not applicable (no behavior change).

The rust/obs-util/src/platform/ change starts the Windows and macOS Rust and Tier 2 lanes here. They have not run locally: the Windows _localtime64/strftime path and the macOS test_formatted_filename run are first exercised in PR CI.

Checklist

  • Base is master (or the description starts with a Stacked: note)
  • I claimed the issue, or no one else has an open PR for it
  • Characterization test (if the unit had none), then RED, then GREEN commits
  • User-visible change: its line in docs/features.md is added or updated (not applicable: no user-visible change)
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite
  • Warning or flag changes (-W..., ratchets): not applicable
  • Intentional differences from C listed in the parity test header and rust/README.md (ports)

rust/obs-util/src/platform_filename.rs holds the safe core with todo!()
bodies, and ffi/platform_filename.rs the C ABI shim with its #[repr(C)]
struct obs_video_info. As in C, the shim reads time(), localtime() and
then libobs's obs_get_video_info, once per call and without checking
its result. localtime and strftime come from the host C library through
the platform boundary (platform::crt::LocalTime, Unix and Windows), and
%FPS's "%.2f" from the host snprintf (DoubleFormat::F2).

- Tier 1 (tests/platform/platform_filename.rs): 1:1 port of the 17
  cases of test_formatted_filename.c on a fixed struct tm for the same
  instant, plus the format-name table, an empty strftime result, blank
  (all-space) conversions and adjacent specifiers.
- Tier 2 layout (platform_filename_layout.rs): struct obs_video_info
  against the C header.
- Tier 3 (platform_filename_parity.rs): oracle/platform.c gives the
  real platform-filename.c a settable time() and an obs_get_video_info
  fixture that the Rust shim's plain obs_get_video_info reads too. The
  core is compared on fixed and random formats, extensions, times and
  video infos; the shim on the real clock, retried on a second change,
  and both must read the video info once per call.
- ENABLE_RUST_LIBOBS=ON drops util/platform-filename.c (moved into
  libobs.ported-platform); export in
  rust-exports.d/obs-util-platform_filename.txt.

22 of the 23 Tier 1 cases fail on todo!(); now_at_reads_localtime (the
platform boundary only) and the layout test pass. The parity run aborts
when the panic crosses the extern "C" shim.

Refs obs-rust#262
The safe core follows platform-filename.c step for step: one left to
right scan; at each position the specifier table in order (a strftime
result that is empty tries the next entry, one of only spaces is
skipped over without being put in), then %FPS, %CRES, %ORES, %VF and
%s; a replacement is never scanned again; a % that starts nothing is
dropped. Then spaces become underscores unless kept, the extension is
added after a dot, and the name is cut to 255 bytes on a UTF-8
boundary. An empty name is NULL.

Kept, characterized, not endorsed: %FPS keeps two decimals for a whole
rate with fps_den > 1, and the obs_get_video_info result is not checked
(obs-rust#1509). Rust cannot read the fields the C leaves uninitialized then,
so its struct starts zeroed; the rust/README.md row lists this and the
other differences. localtime() is called as in C, not localtime_r.

Refs obs-rust#262
@rdoupe
rdoupe requested a review from zackees as a code owner October 20, 2026 09:11
@zackees
zackees enabled auto-merge October 20, 2026 09:11
zone_specifiers built a struct tm by hand with tm_gmtoff and tm_zone
and expected strftime to read them. glibc does, but macOS's %z reads
the C library's time zone (+0000 on the runner), and the Windows CRT's
struct tm has neither field. The test now runs in a child of the test
binary started with TZ=OBS-5:30, as the cmocka test's setup has it
before any call, and checks %z, %Z and the other date specifiers through
localtime of the fixed instant (Now::at, the shim's real path) on every
host, Windows included. common::isolated_env passes the environment.

The shim is unaffected: it calls localtime and strftime as the C does,
so it reads the zone from the same place on each host.

Refs obs-rust#262
@zackees
zackees added this pull request to the merge queue Oct 20, 2026
Merged via the queue into obs-rust:master with commit df53fc7 Oct 20, 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