Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*-------------------------------------------------------------------------
 * 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;
}