Skip to content

Commit c5b5d5f

Browse files
pks-tgitster
authored andcommitted
reftable/stack: refactor reloading to use file descriptor
We're about to introduce a stat(3P)-based caching mechanism to reload the list of stacks only when it has changed. In order to avoid race conditions this requires us to have a file descriptor available that we can use to call fstat(3P) on. Prepare for this by converting the code to use `fd_read_lines()` so that we have the file descriptor readily available. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3c94bd8 commit c5b5d5f

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

reftable/stack.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ static int reftable_stack_reload_maybe_reuse(struct reftable_stack *st,
308308
struct timeval deadline;
309309
int64_t delay = 0;
310310
int tries = 0, err;
311+
int fd = -1;
311312

312313
err = gettimeofday(&deadline, NULL);
313314
if (err < 0)
@@ -329,9 +330,19 @@ static int reftable_stack_reload_maybe_reuse(struct reftable_stack *st,
329330
if (tries > 3 && tv_cmp(&now, &deadline) >= 0)
330331
goto out;
331332

332-
err = read_lines(st->list_file, &names);
333-
if (err < 0)
334-
goto out;
333+
fd = open(st->list_file, O_RDONLY);
334+
if (fd < 0) {
335+
if (errno != ENOENT) {
336+
err = REFTABLE_IO_ERROR;
337+
goto out;
338+
}
339+
340+
names = reftable_calloc(sizeof(char *));
341+
} else {
342+
err = fd_read_lines(fd, &names);
343+
if (err < 0)
344+
goto out;
345+
}
335346

336347
err = reftable_stack_reload_once(st, names, reuse_open);
337348
if (!err)
@@ -356,12 +367,16 @@ static int reftable_stack_reload_maybe_reuse(struct reftable_stack *st,
356367
names = NULL;
357368
free_names(names_after);
358369
names_after = NULL;
370+
close(fd);
371+
fd = -1;
359372

360373
delay = delay + (delay * rand()) / RAND_MAX + 1;
361374
sleep_millisec(delay);
362375
}
363376

364377
out:
378+
if (fd >= 0)
379+
close(fd);
365380
free_names(names);
366381
free_names(names_after);
367382
return err;

0 commit comments

Comments
 (0)