Repository navigation
util: Port os_generate_formatted_filename to obs-util - #1511
Merged
Merged
Conversation
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
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
This was referenced Oct 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs #262 (slice 5, filenames).
What and why
os_generate_formatted_filenamebuilds recording and replay file names from a format such as%CCYY-%MM-%DD %hh-%mm-%ss. #1510 moved it, with its staticsvalid_string,replace_textanderase_ch, verbatim intolibobs/util/platform-filename.cand pinned it with 17 cases intest_formatted_filename. This PR ports it toobs-util::platform_filename, andlibobs-with-rustdropsutil/platform-filename.c.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. Astrftimeresult 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,%VFand%s. A replacement is never scanned again. A%that starts nothing is dropped. After the scan, spaces become_unlessspaceis set, the extension goes after a., and the name is cut to 255 bytes on a UTF-8 boundary. An empty name is NULL.localtimeandstrftime(platform::crt::LocalTime, Unix and Windows; on Windows_time64/_localtime64, which the CRT's inlinetime/localtimecall).%FPS's%.2fgoes through the hostsnprintf(DoubleFormat::F2inplatform/stdio.rs). So names, abbreviations, offsets and rounding are the host's, as in C. These are the onlyplatform/changes.obs_get_video_infoC API, once per call and before the format is looked at, as the C does.get_video_format_nameis a table in the core, becauseobs-media-iodepends onobs-util; parity checks it against the C inline for every value.src/ffi/platform_filename.rs):#[repr(C)] struct obs_video_info, NULLextension/formatread as empty, and abmalloced result. The export is inrust-exports.d/obs-util-platform_filename.txt, andutil/platform-filename.cmoves intolibobs.ported-platform. That move was still to do after util: Move os_generate_formatted_filename into platform-filename.c #1510.test_formatted_filenameis unchanged and passes):%FPSkeeps two decimals for a whole rate whenfps_den > 1(50/2is25.00), and rounds as the hostprintfdoes.obs_get_video_infois not checked (bug: util: os_generate_formatted_filename uses video info that obs_get_video_info did not fill #1509, still open; not fixed here). Beforeobs_startupboth the C and the Rust crash inside it.localtime()is called as in C, notlocaltime_r. It re-readsTZand shares the C library's buffer with other callers. The thread-safety question on port: util/platform.c to obs-util #262 is pinned here, not fixed.rust/README.mdrow):obs_get_video_infodid not write from its uninitializedstruct obs_video_info. Rust cannot read uninitialized memory, so its struct starts zeroed and the name gets0,0x0andNone. That is the value bug: util: os_generate_formatted_filename uses video info that obs_get_video_info did not fill #1509 gives the C as well.localtimethat returns NULL gives empty date conversions. The C would pass NULL tostrftime. This cannot happen for the current time.Tier 2 cannot see the swap here.
test_formatted_filenamecompilesplatform-filename.cinto itself, so itstime()andobs_get_video_infofixtures 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
util: Test os_generate_formatted_filename against a Rust stub (RED). 22 of the 23 Tier 1 cases fail ontodo!().now_at_reads_localtime(platform boundary only) and the layout test pass. The parity run aborts when the panic crosses theextern "C"shim.tests/platform/platform_filename.rs): a 1:1 port of the 17 cmocka cases, on a fixedstruct tmfor the same instant (2025-11-07 16:09:03 at UTC+05:30, zoneOBS), so nothing depends on the process's time zone. The cmocka date case is split in two.%z,%Zand the other date specifiers are also checked inzone_specifiers, which runs in a child of the test binary started withTZ=OBS-5:30and goes throughlocaltimeof the fixed instant (the shim's path). That covers every host: glibc reads%z/%Zfrom thestruct tm, but macOS's%zand the Windows CRT's both read the C library's zone. A follow-up commit, d72e264, fixed this after macOS CI showed+0000for a hand-builtstruct tm. New cases: the format-name table and unknown values; an emptystrftimeresult (nostruct tm); blank conversions (a zone of spaces is skipped over and the specifier stays, so%Z%Zxstays%Z%Zx); adjacent specifiers.platform_filename_layout.rs): the size and alignment ofstruct obs_video_info, and all 13 offsets, against the C header.platform_filename_parity.rs):oracle/platform.cgives the realplatform-filename.ca settabletime()(a#definearound its#includeonly) and anobs_get_video_infofixture. The old "no video" stub inplatform_host.cis replaced by that fixture. The plainobs_get_video_infothat the Rust shim calls reads the same fixture, and both sides count their calls. The test compares:%FPSfor 16 rates;%VFand 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;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 underTZ=UTC,OBS-5:30,America/New_YorkandAustralia/Lord_Howe(a 30-minute DST shift), and withLC_ALL=de_DE.UTF-8(the test process stays in the C locale, as C does withoutsetlocale).fps_den <= 1changed to== 1: 5 failures (Tier 1fps_formatting, 4 parity tests);%kept instead of dropped: 8 (4 Tier 1, 4 parity);invalid_continuation_bytes, paritylong_names_and_utf8_cut);strftimeresult put in (novalid_string): 1 (Tier 1blank_conversions). The C locale has no blank conversion, so parity cannot catch this one. Tier 1 makes one with a zone of spaces.ENABLE_RUST_LIBOBS=OFFandON), each running the cmocka tests againstlibobsand againstlibobs-with-rust:ENABLE_RUST_LIBOBS=OFF)ENABLE_RUST_LIBOBS=ON)test_config_migration_*,test_virtual_cam_shutdown) were not built in these trees, because I built only thecmocka-tests,libobsandlibobs-with-rusttargets. Nothing was skipped.test_formatted_filenamepasses in all four runs, but it tests the C source each time (see above).libobs-with-rust, the C statics (replace_textand the others) are gone andos_generate_formatted_filenameis the Rust one.libobskeeps the C.obs_reset_video, dirties the stack and formats three names. It was run against the Rust lane'slibobsandlibobs-with-rust:libobs(C)%CCYY-%MM-%DD %hh-%mm-%ss %a %b %p %z %Z %s,TZ=OBS-5:302026-10-20 14-21-43 Tue Oct PM +0530 OBS 1792486303.mkvlibobs-with-rust2026-10-20 14-21-46 Tue Oct PM +0530 OBS 1792486306.mkv(run 3 s later)libobs(C)%%x%q%,spacefalse%xq.mkvlibobs-with-rust%xq.mkvlibobs(C)%FPS %CRES %ORES %VF, no video (#1509)1.00 2880154539x2880154539 2880154539x2880154539 None.mkv(the stack's0xABABABAB)libobs-with-rust0 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 warningsand Dylints foraarch64-pc-windows-msvc,x86_64-apple-darwinandaarch64-unknown-linux-gnu; thetime_t/c_longcasts 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 forx86_64-pc-windows-msvcandaarch64-apple-darwin, plus a link of every test binary): passed. Every workspace test binary links on Linux too (cargo test --workspace --no-run).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/strftimepath and the macOStest_formatted_filenamerun are first exercised in PR CI.Checklist
master(or the description starts with a Stacked: note)docs/features.mdis added or updated (not applicable: no user-visible change)-W..., ratchets): not applicable