/*-------------------------------------------------------------------------
|
* cfs_filename.c
|
* Compressed File Store (CFS) — frontend-safe on-disk format lexicon.
|
*
|
* Implements the strict sidecar filename parser shared by the server
|
* (incremental base backup) and the frontend tools. Compiled into every
|
* build, with or without CFS support, so that tools built without CFS
|
* can still recognize sidecar files and refuse or skip them.
|
*
|
* Copyright (c) 2026, Kirillov Vyacheslav, QDB Development Group
|
*
|
* IDENTIFICATION
|
* src/common/cfs_filename.c
|
*
|
*-------------------------------------------------------------------------
|
*/
|
#ifndef FRONTEND
|
#include "postgres.h"
|
#else
|
#include "postgres_fe.h"
|
#endif
|
|
#include "common/cfs_filename.h"
|
|
/*
|
* Strict parsing of a putative CFS sidecar filename, mirroring the rules
|
* of parse_filename_for_nontemp_relation(): the name must be
|
* "<relfilenode>.cfp"[.<segno>], "<relfilenode>.cfsmap" or
|
* "<relfilenode>.cfsjrn", with no leading zeroes in the numbers.
|
*
|
* Returns true and sets *relnumber, *kind and *segno on success; returns
|
* false (zeroing the outputs) for anything else, including transient
|
* compaction files (".cfp.tmp", ".cfsswap", ...).
|
*/
|
bool
|
parse_cfs_sidecar_filename(const char *name, RelFileNumber *relnumber,
|
CfsFileKind *kind, unsigned *segno)
|
{
|
unsigned long n,
|
s;
|
char *endp;
|
|
*relnumber = InvalidRelFileNumber;
|
*kind = CFS_FILE_DATA;
|
*segno = 0;
|
|
/* Reject leading zeroes, just like the relation filename parser. */
|
if (name[0] < '1' || name[0] > '9')
|
return false;
|
|
errno = 0;
|
n = strtoul(name, &endp, 10);
|
if (errno || name == endp || n <= 0 || n > PG_UINT32_MAX)
|
return false;
|
name = endp;
|
|
if (strcmp(name, ".cfsmap") == 0)
|
{
|
*kind = CFS_FILE_MAP;
|
}
|
else if (strcmp(name, ".cfsjrn") == 0)
|
{
|
*kind = CFS_FILE_JOURNAL;
|
}
|
else if (strncmp(name, ".cfp", 4) == 0)
|
{
|
name += 4;
|
if (*name != '\0')
|
{
|
/* Numbered segment: ".cfp.N" with N > 0 and no leading zero. */
|
if (*name != '.' || name[1] < '1' || name[1] > '9')
|
return false;
|
|
errno = 0;
|
s = strtoul(name + 1, &endp, 10);
|
if (errno || name + 1 == endp || s <= 0 || s > PG_UINT32_MAX)
|
return false;
|
if (*endp != '\0')
|
return false;
|
*segno = (unsigned) s;
|
}
|
}
|
else
|
return false;
|
|
*relnumber = (RelFileNumber) n;
|
return true;
|
}
|