[PATCH v3 7/8] reftable: add code to facilitate consistency checks
- From
Karthik Nayak <karthik.188@gmail.com>
- Date
- Sep 18, 2025, 08:11 UTC
- Message-ID
- <20250918-228-reftable-introduce-consistency-checks-v3-7-271af03eb34d@gmail.com>
- In-Reply-To
- <20250918-228-reftable-introduce-consistency-checks-v3-0-271af03eb34d@gmail.com>
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 files in the reftable directory and validates that they have the expected file type and a valid name. It raises specific errors for both.
While here, move 'reftable/error.o' in the Makefile to retain lexicographic ordering.
Signed-off-by: Karthik Nayak <karthik.188@gmail.com> --- Makefile | 3 +- meson.build | 1 + reftable/fsck.c | 112 +++++++++++++++++++++++++++++++++++++++++++++++ reftable/reftable-fsck.h | 42 ++++++++++++++++++ 4 files changed, 157 insertions(+), 1 deletion(-)
diff --git a/Makefile b/Makefile index 4c95affadb..03fbaf2b21 100644 --- a/Makefile +++ b/Makefile @@ -2732,9 +2732,10 @@ XDIFF_OBJS += xdiff/xutils.o xdiff-objs: $(XDIFF_OBJS) REFTABLE_OBJS += reftable/basics.o -REFTABLE_OBJS += reftable/error.o REFTABLE_OBJS += reftable/block.o REFTABLE_OBJS += reftable/blocksource.o +REFTABLE_OBJS += reftable/error.o +REFTABLE_OBJS += reftable/fsck.o REFTABLE_OBJS += reftable/iter.o REFTABLE_OBJS += reftable/merged.o REFTABLE_OBJS += reftable/pq.o diff --git a/meson.build b/meson.build index b3dfcc0497..8914252910 100644 --- a/meson.build +++ b/meson.build @@ -452,6 +452,7 @@ libgit_sources = [ 'reftable/error.c', 'reftable/block.c', 'reftable/blocksource.c', + 'reftable/fsck.c', 'reftable/iter.c', 'reftable/merged.c', 'reftable/pq.c', diff --git a/reftable/fsck.c b/reftable/fsck.c new file mode 100644 index 0000000000..785e4b43e8 --- /dev/null +++ b/reftable/fsck.c @@ -0,0 +1,112 @@ +#include "basics.h" +#include "reftable-fsck.h" +#include "stack.h" + +static bool valid_table_name(const char *name, uint64_t *min_update_index, + uint64_t *max_update_index) +{ + const char *ptr = name; + char *endptr; + + /* strtoull doesn't set errno on success */ + errno = 0; + + *min_update_index = strtoull(ptr, &endptr, 16); + if (errno == EINVAL) + return false; + ptr = endptr; + + if (strncmp(ptr, "-", 1)) + return false; + ptr++; + + *max_update_index = strtoull(ptr, &endptr, 16); + if (errno == EINVAL) + return false; + ptr = endptr; + + if (*ptr != '-') + return false; + ptr++; + + strtoul(ptr, &endptr, 16); + if (errno == EINVAL) + return false; + ptr = endptr; + + if (strcmp(ptr, ".ref") && strcmp(ptr, ".log")) + return false; + + return true; +} + +static int stack_check_all_files_in_dir(struct reftable_stack *stack, + reftable_fsck_report_fn report_fn, + void *cb_data) +{ + DIR *dir = opendir(stack->reftable_dir); + struct reftable_fsck_info info; + struct dirent *d = NULL; + uint64_t min, max; + int err = 0; + + if (!dir) + return 0; + + while ((d = readdir(dir))) { + if (!strcmp(d->d_name, "tables.list")) + continue; + + if ((d->d_name[0] == '.' && + (d->d_name[1] == '\0' || + (d->d_name[1] == '.' && d->d_name[2] == '\0')))) + continue; + + if (d->d_type == DT_REG) { + if (!valid_table_name(d->d_name, &min, &max)) { + info.error = REFTABLE_FSCK_ERROR_TABLE_NAME; + info.msg = "file with invalid table name"; + info.path = d->d_name; + + err |= report_fn(&info, cb_data); + } + } else { + info.error = REFTABLE_FSCK_ERROR_INVALID_FILE_TYPE; + info.msg = "file with unexpected type"; + info.path = d->d_name; + + err |= report_fn(&info, cb_data); + } + } + + closedir(dir); + return err; +} + +static int stack_checks(struct reftable_stack *stack, + reftable_fsck_report_fn report_fn, + void *cb_data) +{ + struct reftable_buf msg = REFTABLE_BUF_INIT; + char **names = NULL; + int err = 0; + + if (stack == NULL) + goto out; + + err |= stack_check_all_files_in_dir(stack, report_fn, cb_data); + +out: + free_names(names); + reftable_buf_release(&msg); + return err; +} + +int reftable_fsck_check(struct reftable_stack *stack, + reftable_fsck_report_fn report_fn, + reftable_fsck_verbose_fn verbose_fn, + void *cb_data) +{ + verbose_fn("Checking reftable: stack checks", cb_data); + return stack_checks(stack, report_fn, cb_data); +} diff --git a/reftable/reftable-fsck.h b/reftable/reftable-fsck.h new file mode 100644 index 0000000000..5e13ac9f02 --- /dev/null +++ b/reftable/reftable-fsck.h @@ -0,0 +1,42 @@ +#ifndef REFTABLE_FSCK_H +#define REFTABLE_FSCK_H + +#include "reftable-stack.h" + +enum reftable_fsck_error { + /* Non regular file in the reftable directory */ + REFTABLE_FSCK_ERROR_INVALID_FILE_TYPE = 0, + /* Invalid table name */ + REFTABLE_FSCK_ERROR_TABLE_NAME, + /* Used for bounds checking, must be last */ + REFTABLE_FSCK_MAX_VALUE +}; + +/* Represents an individual error encountered during the FSCK checks. */ +struct reftable_fsck_info { + enum reftable_fsck_error error; + const char *msg; + const char *path; +}; + +typedef int reftable_fsck_report_fn(struct reftable_fsck_info *info, + void *cb_data); +typedef void reftable_fsck_verbose_fn(const char *msg, void *cb_data); + +/* + * Given a reftable stack, perform consistency checks on the stack. + * + * If an issue is encountered, the issue is reported to the callee via the + * provided 'report_fn'. If the issue is non-recoverable the flow will not + * continue. If it is recoverable, the flow will continue and further issues + * will be reported as identified. + * + * The 'verbose_fn' will be invoked to provide verbose information about + * the progress and state of the consistency checks. + */ +int reftable_fsck_check(struct reftable_stack *stack, + reftable_fsck_report_fn report_fn, + reftable_fsck_verbose_fn verbose_fn, + void *cb_data); + +#endif /* REFTABLE_FSCK_H */
-- 2.51.0