Skip to content

Commit 9051638

Browse files
KarthikNayakgitster
authored andcommitted
reftable: add code to facilitate consistency checks
The `git refs verify` command is used to run consistency checks on the reference backends. This command is also invoked when users run 'git fsck'. While the files-backend has some fsck checks added, the reftable backend lacks such checks. Let's add the required infrastructure and a check to test for the files present in the reftable directory. Since the reftable library is treated as an independent library we should ensure that the library code works independently without knowledge about Git's internals. To do this, add both 'reftable/fsck.c' and 'reftable/reftable-fsck.h'. Which provide an entry point 'reftable_fsck_check' for running fsck checks over a provided reftable stack. The callee provides the function with callbacks to handle issue and information reporting. The added check, goes over all tables in the reftable stack validates that they have a valid name. It not, it raises an error. While here, move 'reftable/error.o' in the Makefile to retain lexicographic ordering. Signed-off-by: Karthik Nayak <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5a71321 commit 9051638

File tree

4 files changed

+143
-1
lines changed

4 files changed

+143
-1
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2729,9 +2729,10 @@ XDIFF_OBJS += xdiff/xutils.o
27292729
xdiff-objs: $(XDIFF_OBJS)
27302730

27312731
REFTABLE_OBJS += reftable/basics.o
2732-
REFTABLE_OBJS += reftable/error.o
27332732
REFTABLE_OBJS += reftable/block.o
27342733
REFTABLE_OBJS += reftable/blocksource.o
2734+
REFTABLE_OBJS += reftable/error.o
2735+
REFTABLE_OBJS += reftable/fsck.o
27352736
REFTABLE_OBJS += reftable/iter.o
27362737
REFTABLE_OBJS += reftable/merged.o
27372738
REFTABLE_OBJS += reftable/pq.o

meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ libgit_sources = [
452452
'reftable/error.c',
453453
'reftable/block.c',
454454
'reftable/blocksource.c',
455+
'reftable/fsck.c',
455456
'reftable/iter.c',
456457
'reftable/merged.c',
457458
'reftable/pq.c',

reftable/fsck.c

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#include "basics.h"
2+
#include "reftable-fsck.h"
3+
#include "reftable-table.h"
4+
#include "stack.h"
5+
6+
static bool table_has_valid_name(const char *name)
7+
{
8+
const char *ptr = name;
9+
char *endptr;
10+
11+
/* strtoull doesn't set errno on success */
12+
errno = 0;
13+
14+
strtoull(ptr, &endptr, 16);
15+
if (errno)
16+
return false;
17+
ptr = endptr;
18+
19+
if (*ptr != '-')
20+
return false;
21+
ptr++;
22+
23+
strtoull(ptr, &endptr, 16);
24+
if (errno)
25+
return false;
26+
ptr = endptr;
27+
28+
if (*ptr != '-')
29+
return false;
30+
ptr++;
31+
32+
strtoul(ptr, &endptr, 16);
33+
if (errno)
34+
return false;
35+
ptr = endptr;
36+
37+
if (strcmp(ptr, ".ref") && strcmp(ptr, ".log"))
38+
return false;
39+
40+
return true;
41+
}
42+
43+
typedef int (*table_check_fn)(struct reftable_table *table,
44+
reftable_fsck_report_fn report_fn,
45+
void *cb_data);
46+
47+
static int table_check_name(struct reftable_table *table,
48+
reftable_fsck_report_fn report_fn,
49+
void *cb_data)
50+
{
51+
if (!table_has_valid_name(table->name)) {
52+
struct reftable_fsck_info info;
53+
54+
info.error = REFTABLE_FSCK_ERROR_TABLE_NAME;
55+
info.msg = "invalid reftable table name";
56+
info.path = table->name;
57+
58+
return report_fn(&info, cb_data);
59+
}
60+
61+
return 0;
62+
}
63+
64+
static int table_checks(struct reftable_table *table,
65+
reftable_fsck_report_fn report_fn,
66+
reftable_fsck_verbose_fn verbose_fn UNUSED,
67+
void *cb_data)
68+
{
69+
table_check_fn table_check_fns[] = {
70+
table_check_name,
71+
NULL,
72+
};
73+
int err = 0;
74+
75+
for (size_t i = 0; table_check_fns[i]; i++)
76+
err |= table_check_fns[i](table, report_fn, cb_data);
77+
78+
return err;
79+
}
80+
81+
int reftable_fsck_check(struct reftable_stack *stack,
82+
reftable_fsck_report_fn report_fn,
83+
reftable_fsck_verbose_fn verbose_fn,
84+
void *cb_data)
85+
{
86+
struct reftable_buf msg = REFTABLE_BUF_INIT;
87+
int err = 0;
88+
89+
for (size_t i = 0; i < stack->tables_len; i++) {
90+
reftable_buf_reset(&msg);
91+
reftable_buf_addstr(&msg, "Checking table: ");
92+
reftable_buf_addstr(&msg, stack->tables[i]->name);
93+
verbose_fn(msg.buf, cb_data);
94+
95+
err |= table_checks(stack->tables[i], report_fn, verbose_fn, cb_data);
96+
}
97+
98+
reftable_buf_release(&msg);
99+
return err;
100+
}

reftable/reftable-fsck.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#ifndef REFTABLE_FSCK_H
2+
#define REFTABLE_FSCK_H
3+
4+
#include "reftable-stack.h"
5+
6+
enum reftable_fsck_error {
7+
/* Invalid table name */
8+
REFTABLE_FSCK_ERROR_TABLE_NAME = 0,
9+
/* Used for bounds checking, must be last */
10+
REFTABLE_FSCK_MAX_VALUE,
11+
};
12+
13+
/* Represents an individual error encountered during the FSCK checks. */
14+
struct reftable_fsck_info {
15+
enum reftable_fsck_error error;
16+
const char *msg;
17+
const char *path;
18+
};
19+
20+
typedef int reftable_fsck_report_fn(struct reftable_fsck_info *info,
21+
void *cb_data);
22+
typedef void reftable_fsck_verbose_fn(const char *msg, void *cb_data);
23+
24+
/*
25+
* Given a reftable stack, perform consistency checks on the stack.
26+
*
27+
* If an issue is encountered, the issue is reported to the callee via the
28+
* provided 'report_fn'. If the issue is non-recoverable the flow will not
29+
* continue. If it is recoverable, the flow will continue and further issues
30+
* will be reported as identified.
31+
*
32+
* The 'verbose_fn' will be invoked to provide verbose information about
33+
* the progress and state of the consistency checks.
34+
*/
35+
int reftable_fsck_check(struct reftable_stack *stack,
36+
reftable_fsck_report_fn report_fn,
37+
reftable_fsck_verbose_fn verbose_fn,
38+
void *cb_data);
39+
40+
#endif /* REFTABLE_FSCK_H */

0 commit comments

Comments
 (0)