feat(flatfile): warn on missing or mismatched log format-version

This commit is contained in:
2026-05-04 17:17:33 +03:00
parent a9b181beb7
commit 48b89460b7
3 changed files with 65 additions and 2 deletions

View File

@@ -16,6 +16,7 @@
#include <sys/stat.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -338,6 +339,49 @@ ff_skip_bom(FILE* fp)
return 0;
}
// Format-version helper
// =========================================================================
//
// Scan leading '#' comment lines for `# format-version: <N>`. Stops at
// the first non-comment line or after FF_VERSION_SCAN_MAX comments.
// Restores fp to its position at entry, so callers can re-parse the
// header normally afterwards.
#define FF_VERSION_SCAN_MAX 16
int
ff_read_format_version(FILE* fp)
{
long start = ftell(fp);
if (start < 0)
return -1;
int version = 0;
size_t prefix_len = strlen(FF_VERSION_PREFIX);
for (int i = 0; i < FF_VERSION_SCAN_MAX; i++) {
gboolean truncated = FALSE;
char* line = ff_readline(fp, &truncated);
if (!line)
break;
if (line[0] != '#') {
free(line);
break;
}
if (strncmp(line, FF_VERSION_PREFIX, prefix_len) == 0) {
char* endptr = NULL;
long v = strtol(line + prefix_len, &endptr, 10);
if (endptr != line + prefix_len && v > 0 && v <= INT_MAX)
version = (int)v;
free(line);
break;
}
free(line);
}
fseek(fp, start, SEEK_SET);
return version;
}
// Readline helper
// =========================================================================
//