Say that pipeline-binaries.bin is cut back also at a malformed record, and check the cut itself.

The review of the torn-file fix (43ac5ae3) found that pb_index also stops at a malformed record in the middle of the
file, so pb_init cuts the file back to it and drops the whole records after it, while its log line and the comment
above the cut spoke only of a record half written. It also found that tests/pb_torn_check.py covered only the index
walk, not the keep-and-cut that is the fix. The decision after the walk moves from pb_init into pb_keep(path, good, len),
which returns the length to go on with (len when the records fill the file, good after cutting it back) or 0 when the
file cannot be cut and starts empty; its log line now reads "<path> ends inside a record or holds a malformed one at
byte <good>; <n> bytes from there dropped", and the comment and the start-empty reason say the same. Behaviour is
unchanged.

pb_torn_check.py now also cuts pb_keep out of vk_pipeline.c and runs it on real files, with plat_file_truncate a
stand-in that cuts with the C library: a torn file is cut to its whole records and logged, a second walk reaches the end
of what is left, a record appended after the cut is read back with the others; a malformed record in the middle is cut
off with what follows; a whole file is left as it is, unlogged; and a file that cannot be cut gives 0 and stays as it
was. The check passes, vk_pipeline.c compiles, and its -Wall -Wextra warnings are the four it had, none in pb_keep or
pb_init. docs/building.md names pb_keep and what the check now covers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-13 17:45:51 -05:00
parent ad0e934fbf
commit 75b1e0b5d9
3 changed files with 109 additions and 21 deletions

View File

@@ -336,6 +336,18 @@ static uint64_t pb_index(FILE *f, uint64_t len) {
return good;
}
// The length of the file at path (len bytes) to go on with after pb_index found its whole records ending at good: len
// when they fill it. When it ends inside a record (the process ended during an append, or a disk filled) or holds a
// malformed one, the records from there on are dropped and the file is cut back to good, so that appends go on after
// the whole records before it. 0 when it cannot be cut, and the file starts empty.
static uint64_t pb_keep(const char *path, uint64_t good, uint64_t len) {
if (good == len) return len;
if (!plat_file_truncate(path, good)) return 0;
rt_log("vk_pipeline: pipeline binaries: %s ends inside a record or holds a malformed one at byte %llu; %llu bytes "
"from there dropped", path, (unsigned long long)good, (unsigned long long)(len - good));
return good;
}
// Opens pipeline-binaries.bin and indexes its records (cache_load, before P.cache).
static void pb_init(VkCtx *c) {
if (!c->has_pipeline_binary || !P.cache_path[0]) return;
@@ -372,17 +384,13 @@ static void pb_init(VkCtx *c) {
else good = pb_index(f, len);
fclose(f);
}
// A file that ends inside a record (the process ended during an append, or a disk filled): the whole records
// before it stay, and the file is cut back to them so that appends go on from there. Only when it cannot be cut
// does it start empty.
if (f && !why && good != len) {
if (plat_file_truncate(PB.path, good)) {
rt_log("vk_pipeline: pipeline binaries: %s was cut short (a record half written); %llu bytes after the last "
"whole record dropped", PB.path, (unsigned long long)(len - good));
len = good;
} else {
why = "cut short (a record half written), and it cannot be cut back to its whole records";
}
// A file that ends inside a record (the process ended during an append, or a disk filled) or holds a malformed
// record: the whole records before it stay, and the file is cut back to them so that appends go on from there.
// Only when it cannot be cut does it start empty.
if (f && !why) {
uint64_t kept = pb_keep(PB.path, good, len);
if (kept) len = kept;
else why = "ends inside a record or holds a malformed one, and it cannot be cut back to its whole records";
}
if (why || !f) { // start empty: the index is dropped and the file rewritten
if (f) rt_log("vk_pipeline: pipeline binaries: %s %s (%.0f MB); starting empty", PB.path, why, len / 1048576.0);