2 Copyright 2020 Google LLC
4 Use of this source code is governed by a BSD-style
5 license that can be found in the LICENSE file or at
6 https://developers.google.com/open-source/licenses/bsd
11 #include "../write-or-die.h"
16 #include "reftable-error.h"
17 #include "reftable-record.h"
18 #include "reftable-merged.h"
22 static int stack_try_add(struct reftable_stack
*st
,
23 int (*write_table
)(struct reftable_writer
*wr
,
26 static int stack_write_compact(struct reftable_stack
*st
,
27 struct reftable_writer
*wr
,
28 size_t first
, size_t last
,
29 struct reftable_log_expiry_config
*config
);
30 static int stack_check_addition(struct reftable_stack
*st
,
31 const char *new_tab_name
);
32 static void reftable_addition_close(struct reftable_addition
*add
);
33 static int reftable_stack_reload_maybe_reuse(struct reftable_stack
*st
,
36 static void stack_filename(struct strbuf
*dest
, struct reftable_stack
*st
,
40 strbuf_addstr(dest
, st
->reftable_dir
);
41 strbuf_addstr(dest
, "/");
42 strbuf_addstr(dest
, name
);
45 static ssize_t
reftable_fd_write(void *arg
, const void *data
, size_t sz
)
47 int *fdp
= (int *)arg
;
48 return write_in_full(*fdp
, data
, sz
);
51 static int reftable_fd_flush(void *arg
)
53 int *fdp
= (int *)arg
;
55 return fsync_component(FSYNC_COMPONENT_REFERENCE
, *fdp
);
58 int reftable_new_stack(struct reftable_stack
**dest
, const char *dir
,
59 struct reftable_write_options config
)
61 struct reftable_stack
*p
= reftable_calloc(1, sizeof(*p
));
62 struct strbuf list_file_name
= STRBUF_INIT
;
65 if (config
.hash_id
== 0) {
66 config
.hash_id
= GIT_SHA1_FORMAT_ID
;
71 strbuf_reset(&list_file_name
);
72 strbuf_addstr(&list_file_name
, dir
);
73 strbuf_addstr(&list_file_name
, "/tables.list");
75 p
->list_file
= strbuf_detach(&list_file_name
, NULL
);
77 p
->reftable_dir
= xstrdup(dir
);
80 err
= reftable_stack_reload_maybe_reuse(p
, 1);
82 reftable_stack_destroy(p
);
89 static int fd_read_lines(int fd
, char ***namesp
)
91 off_t size
= lseek(fd
, 0, SEEK_END
);
95 err
= REFTABLE_IO_ERROR
;
98 err
= lseek(fd
, 0, SEEK_SET
);
100 err
= REFTABLE_IO_ERROR
;
104 REFTABLE_ALLOC_ARRAY(buf
, size
+ 1);
105 if (read_in_full(fd
, buf
, size
) != size
) {
106 err
= REFTABLE_IO_ERROR
;
111 parse_names(buf
, size
, namesp
);
118 int read_lines(const char *filename
, char ***namesp
)
120 int fd
= open(filename
, O_RDONLY
);
123 if (errno
== ENOENT
) {
124 REFTABLE_CALLOC_ARRAY(*namesp
, 1);
128 return REFTABLE_IO_ERROR
;
130 err
= fd_read_lines(fd
, namesp
);
135 struct reftable_merged_table
*
136 reftable_stack_merged_table(struct reftable_stack
*st
)
141 static int has_name(char **names
, const char *name
)
144 if (!strcmp(*names
, name
))
151 /* Close and free the stack */
152 void reftable_stack_destroy(struct reftable_stack
*st
)
157 reftable_merged_table_free(st
->merged
);
161 err
= read_lines(st
->list_file
, &names
);
163 FREE_AND_NULL(names
);
168 struct strbuf filename
= STRBUF_INIT
;
169 for (i
= 0; i
< st
->readers_len
; i
++) {
170 const char *name
= reader_name(st
->readers
[i
]);
171 strbuf_reset(&filename
);
172 if (names
&& !has_name(names
, name
)) {
173 stack_filename(&filename
, st
, name
);
175 reftable_reader_free(st
->readers
[i
]);
178 /* On Windows, can only unlink after closing. */
179 unlink(filename
.buf
);
182 strbuf_release(&filename
);
184 FREE_AND_NULL(st
->readers
);
187 if (st
->list_fd
>= 0) {
192 FREE_AND_NULL(st
->list_file
);
193 FREE_AND_NULL(st
->reftable_dir
);
198 static struct reftable_reader
**stack_copy_readers(struct reftable_stack
*st
,
201 struct reftable_reader
**cur
= reftable_calloc(cur_len
, sizeof(*cur
));
203 for (i
= 0; i
< cur_len
; i
++) {
204 cur
[i
] = st
->readers
[i
];
209 static int reftable_stack_reload_once(struct reftable_stack
*st
, char **names
,
212 size_t cur_len
= !st
->merged
? 0 : st
->merged
->stack_len
;
213 struct reftable_reader
**cur
= stack_copy_readers(st
, cur_len
);
214 size_t names_len
= names_length(names
);
215 struct reftable_reader
**new_readers
=
216 reftable_calloc(names_len
, sizeof(*new_readers
));
217 struct reftable_table
*new_tables
=
218 reftable_calloc(names_len
, sizeof(*new_tables
));
219 size_t new_readers_len
= 0;
220 struct reftable_merged_table
*new_merged
= NULL
;
221 struct strbuf table_path
= STRBUF_INIT
;
226 struct reftable_reader
*rd
= NULL
;
227 char *name
= *names
++;
229 /* this is linear; we assume compaction keeps the number of
230 tables under control so this is not quadratic. */
231 for (i
= 0; reuse_open
&& i
< cur_len
; i
++) {
232 if (cur
[i
] && 0 == strcmp(cur
[i
]->name
, name
)) {
240 struct reftable_block_source src
= { NULL
};
241 stack_filename(&table_path
, st
, name
);
243 err
= reftable_block_source_from_file(&src
,
248 err
= reftable_new_reader(&rd
, &src
, name
);
253 new_readers
[new_readers_len
] = rd
;
254 reftable_table_from_reader(&new_tables
[new_readers_len
], rd
);
259 err
= reftable_new_merged_table(&new_merged
, new_tables
,
260 new_readers_len
, st
->config
.hash_id
);
265 st
->readers_len
= new_readers_len
;
267 merged_table_release(st
->merged
);
268 reftable_merged_table_free(st
->merged
);
271 reftable_free(st
->readers
);
273 st
->readers
= new_readers
;
277 new_merged
->suppress_deletions
= 1;
278 st
->merged
= new_merged
;
279 for (i
= 0; i
< cur_len
; i
++) {
281 const char *name
= reader_name(cur
[i
]);
282 stack_filename(&table_path
, st
, name
);
284 reader_close(cur
[i
]);
285 reftable_reader_free(cur
[i
]);
287 /* On Windows, can only unlink after closing. */
288 unlink(table_path
.buf
);
293 for (i
= 0; i
< new_readers_len
; i
++) {
294 reader_close(new_readers
[i
]);
295 reftable_reader_free(new_readers
[i
]);
297 reftable_free(new_readers
);
298 reftable_free(new_tables
);
300 strbuf_release(&table_path
);
304 /* return negative if a before b. */
305 static int tv_cmp(struct timeval
*a
, struct timeval
*b
)
307 time_t diff
= a
->tv_sec
- b
->tv_sec
;
308 int udiff
= a
->tv_usec
- b
->tv_usec
;
316 static int reftable_stack_reload_maybe_reuse(struct reftable_stack
*st
,
319 char **names
= NULL
, **names_after
= NULL
;
320 struct timeval deadline
;
325 err
= gettimeofday(&deadline
, NULL
);
328 deadline
.tv_sec
+= 3;
333 err
= gettimeofday(&now
, NULL
);
338 * Only look at deadlines after the first few times. This
339 * simplifies debugging in GDB.
342 if (tries
> 3 && tv_cmp(&now
, &deadline
) >= 0)
345 fd
= open(st
->list_file
, O_RDONLY
);
347 if (errno
!= ENOENT
) {
348 err
= REFTABLE_IO_ERROR
;
352 REFTABLE_CALLOC_ARRAY(names
, 1);
354 err
= fd_read_lines(fd
, &names
);
359 err
= reftable_stack_reload_once(st
, names
, reuse_open
);
362 if (err
!= REFTABLE_NOT_EXIST_ERROR
)
366 * REFTABLE_NOT_EXIST_ERROR can be caused by a concurrent
367 * writer. Check if there was one by checking if the name list
370 err
= read_lines(st
->list_file
, &names_after
);
373 if (names_equal(names_after
, names
)) {
374 err
= REFTABLE_NOT_EXIST_ERROR
;
380 free_names(names_after
);
385 delay
= delay
+ (delay
* rand()) / RAND_MAX
+ 1;
386 sleep_millisec(delay
);
391 * Invalidate the stat cache. It is sufficient to only close the file
392 * descriptor and keep the cached stat info because we never use the
393 * latter when the former is negative.
395 if (st
->list_fd
>= 0) {
401 * Cache stat information in case it provides a useful signal to us.
402 * According to POSIX, "The st_ino and st_dev fields taken together
403 * uniquely identify the file within the system." That being said,
404 * Windows is not POSIX compliant and we do not have these fields
405 * available. So the information we have there is insufficient to
406 * determine whether two file descriptors point to the same file.
408 * While we could fall back to using other signals like the file's
409 * mtime, those are not sufficient to avoid races. We thus refrain from
410 * using the stat cache on such systems and fall back to the secondary
411 * caching mechanism, which is to check whether contents of the file
414 * On other systems which are POSIX compliant we must keep the file
415 * descriptor open. This is to avoid a race condition where two
416 * processes access the reftable stack at the same point in time:
418 * 1. A reads the reftable stack and caches its stat info.
420 * 2. B updates the stack, appending a new table to "tables.list".
421 * This will both use a new inode and result in a different file
422 * size, thus invalidating A's cache in theory.
424 * 3. B decides to auto-compact the stack and merges two tables. The
425 * file size now matches what A has cached again. Furthermore, the
426 * filesystem may decide to recycle the inode number of the file
427 * we have replaced in (2) because it is not in use anymore.
429 * 4. A reloads the reftable stack. Neither the inode number nor the
430 * file size changed. If the timestamps did not change either then
431 * we think the cached copy of our stack is up-to-date.
433 * By keeping the file descriptor open the inode number cannot be
434 * recycled, mitigating the race.
436 if (!err
&& fd
>= 0 && !fstat(fd
, &st
->list_st
) &&
437 st
->list_st
.st_dev
&& st
->list_st
.st_ino
) {
445 free_names(names_after
);
452 static int stack_uptodate(struct reftable_stack
*st
)
459 * When we have cached stat information available then we use it to
460 * verify whether the file has been rewritten.
462 * Note that we explicitly do not want to use `stat_validity_check()`
463 * and friends here because they may end up not comparing the `st_dev`
464 * and `st_ino` fields. These functions thus cannot guarantee that we
465 * indeed still have the same file.
467 if (st
->list_fd
>= 0) {
470 if (stat(st
->list_file
, &list_st
) < 0) {
472 * It's fine for "tables.list" to not exist. In that
473 * case, we have to refresh when the loaded stack has
477 return !!st
->readers_len
;
478 return REFTABLE_IO_ERROR
;
482 * When "tables.list" refers to the same file we can assume
483 * that it didn't change. This is because we always use
484 * rename(3P) to update the file and never write to it
487 if (st
->list_st
.st_dev
== list_st
.st_dev
&&
488 st
->list_st
.st_ino
== list_st
.st_ino
)
492 err
= read_lines(st
->list_file
, &names
);
496 for (i
= 0; i
< st
->readers_len
; i
++) {
502 if (strcmp(st
->readers
[i
]->name
, names
[i
])) {
508 if (names
[st
->merged
->stack_len
]) {
518 int reftable_stack_reload(struct reftable_stack
*st
)
520 int err
= stack_uptodate(st
);
522 return reftable_stack_reload_maybe_reuse(st
, 1);
526 int reftable_stack_add(struct reftable_stack
*st
,
527 int (*write
)(struct reftable_writer
*wr
, void *arg
),
530 int err
= stack_try_add(st
, write
, arg
);
532 if (err
== REFTABLE_LOCK_ERROR
) {
533 /* Ignore error return, we want to propagate
536 reftable_stack_reload(st
);
544 static void format_name(struct strbuf
*dest
, uint64_t min
, uint64_t max
)
547 uint32_t rnd
= (uint32_t)git_rand();
548 snprintf(buf
, sizeof(buf
), "0x%012" PRIx64
"-0x%012" PRIx64
"-%08x",
551 strbuf_addstr(dest
, buf
);
554 struct reftable_addition
{
555 struct tempfile
*lock_file
;
556 struct reftable_stack
*stack
;
559 size_t new_tables_len
, new_tables_cap
;
560 uint64_t next_update_index
;
563 #define REFTABLE_ADDITION_INIT {0}
565 static int reftable_stack_init_addition(struct reftable_addition
*add
,
566 struct reftable_stack
*st
)
568 struct strbuf lock_file_name
= STRBUF_INIT
;
572 strbuf_addf(&lock_file_name
, "%s.lock", st
->list_file
);
574 add
->lock_file
= create_tempfile(lock_file_name
.buf
);
575 if (!add
->lock_file
) {
576 if (errno
== EEXIST
) {
577 err
= REFTABLE_LOCK_ERROR
;
579 err
= REFTABLE_IO_ERROR
;
583 if (st
->config
.default_permissions
) {
584 if (chmod(add
->lock_file
->filename
.buf
, st
->config
.default_permissions
) < 0) {
585 err
= REFTABLE_IO_ERROR
;
590 err
= stack_uptodate(st
);
595 err
= REFTABLE_LOCK_ERROR
;
599 add
->next_update_index
= reftable_stack_next_update_index(st
);
602 reftable_addition_close(add
);
604 strbuf_release(&lock_file_name
);
608 static void reftable_addition_close(struct reftable_addition
*add
)
610 struct strbuf nm
= STRBUF_INIT
;
613 for (i
= 0; i
< add
->new_tables_len
; i
++) {
614 stack_filename(&nm
, add
->stack
, add
->new_tables
[i
]);
616 reftable_free(add
->new_tables
[i
]);
617 add
->new_tables
[i
] = NULL
;
619 reftable_free(add
->new_tables
);
620 add
->new_tables
= NULL
;
621 add
->new_tables_len
= 0;
622 add
->new_tables_cap
= 0;
624 delete_tempfile(&add
->lock_file
);
628 void reftable_addition_destroy(struct reftable_addition
*add
)
633 reftable_addition_close(add
);
637 int reftable_addition_commit(struct reftable_addition
*add
)
639 struct strbuf table_list
= STRBUF_INIT
;
640 int lock_file_fd
= get_tempfile_fd(add
->lock_file
);
644 if (add
->new_tables_len
== 0)
647 for (i
= 0; i
< add
->stack
->merged
->stack_len
; i
++) {
648 strbuf_addstr(&table_list
, add
->stack
->readers
[i
]->name
);
649 strbuf_addstr(&table_list
, "\n");
651 for (i
= 0; i
< add
->new_tables_len
; i
++) {
652 strbuf_addstr(&table_list
, add
->new_tables
[i
]);
653 strbuf_addstr(&table_list
, "\n");
656 err
= write_in_full(lock_file_fd
, table_list
.buf
, table_list
.len
);
657 strbuf_release(&table_list
);
659 err
= REFTABLE_IO_ERROR
;
663 fsync_component_or_die(FSYNC_COMPONENT_REFERENCE
, lock_file_fd
,
664 get_tempfile_path(add
->lock_file
));
666 err
= rename_tempfile(&add
->lock_file
, add
->stack
->list_file
);
668 err
= REFTABLE_IO_ERROR
;
672 /* success, no more state to clean up. */
673 for (i
= 0; i
< add
->new_tables_len
; i
++)
674 reftable_free(add
->new_tables
[i
]);
675 reftable_free(add
->new_tables
);
676 add
->new_tables
= NULL
;
677 add
->new_tables_len
= 0;
678 add
->new_tables_cap
= 0;
680 err
= reftable_stack_reload_maybe_reuse(add
->stack
, 1);
684 if (!add
->stack
->disable_auto_compact
)
685 err
= reftable_stack_auto_compact(add
->stack
);
688 reftable_addition_close(add
);
692 int reftable_stack_new_addition(struct reftable_addition
**dest
,
693 struct reftable_stack
*st
)
696 struct reftable_addition empty
= REFTABLE_ADDITION_INIT
;
697 REFTABLE_CALLOC_ARRAY(*dest
, 1);
699 err
= reftable_stack_init_addition(*dest
, st
);
701 reftable_free(*dest
);
707 static int stack_try_add(struct reftable_stack
*st
,
708 int (*write_table
)(struct reftable_writer
*wr
,
712 struct reftable_addition add
= REFTABLE_ADDITION_INIT
;
713 int err
= reftable_stack_init_addition(&add
, st
);
717 err
= REFTABLE_LOCK_ERROR
;
721 err
= reftable_addition_add(&add
, write_table
, arg
);
725 err
= reftable_addition_commit(&add
);
727 reftable_addition_close(&add
);
731 int reftable_addition_add(struct reftable_addition
*add
,
732 int (*write_table
)(struct reftable_writer
*wr
,
736 struct strbuf temp_tab_file_name
= STRBUF_INIT
;
737 struct strbuf tab_file_name
= STRBUF_INIT
;
738 struct strbuf next_name
= STRBUF_INIT
;
739 struct reftable_writer
*wr
= NULL
;
743 strbuf_reset(&next_name
);
744 format_name(&next_name
, add
->next_update_index
, add
->next_update_index
);
746 stack_filename(&temp_tab_file_name
, add
->stack
, next_name
.buf
);
747 strbuf_addstr(&temp_tab_file_name
, ".temp.XXXXXX");
749 tab_fd
= mkstemp(temp_tab_file_name
.buf
);
751 err
= REFTABLE_IO_ERROR
;
754 if (add
->stack
->config
.default_permissions
) {
755 if (chmod(temp_tab_file_name
.buf
, add
->stack
->config
.default_permissions
)) {
756 err
= REFTABLE_IO_ERROR
;
760 wr
= reftable_new_writer(reftable_fd_write
, reftable_fd_flush
, &tab_fd
,
761 &add
->stack
->config
);
762 err
= write_table(wr
, arg
);
766 err
= reftable_writer_close(wr
);
767 if (err
== REFTABLE_EMPTY_TABLE_ERROR
) {
777 err
= REFTABLE_IO_ERROR
;
781 err
= stack_check_addition(add
->stack
, temp_tab_file_name
.buf
);
785 if (wr
->min_update_index
< add
->next_update_index
) {
786 err
= REFTABLE_API_ERROR
;
790 format_name(&next_name
, wr
->min_update_index
, wr
->max_update_index
);
791 strbuf_addstr(&next_name
, ".ref");
793 stack_filename(&tab_file_name
, add
->stack
, next_name
.buf
);
796 On windows, this relies on rand() picking a unique destination name.
797 Maybe we should do retry loop as well?
799 err
= rename(temp_tab_file_name
.buf
, tab_file_name
.buf
);
801 err
= REFTABLE_IO_ERROR
;
805 REFTABLE_ALLOC_GROW(add
->new_tables
, add
->new_tables_len
+ 1,
806 add
->new_tables_cap
);
807 add
->new_tables
[add
->new_tables_len
++] = strbuf_detach(&next_name
, NULL
);
813 if (temp_tab_file_name
.len
> 0) {
814 unlink(temp_tab_file_name
.buf
);
817 strbuf_release(&temp_tab_file_name
);
818 strbuf_release(&tab_file_name
);
819 strbuf_release(&next_name
);
820 reftable_writer_free(wr
);
824 uint64_t reftable_stack_next_update_index(struct reftable_stack
*st
)
826 int sz
= st
->merged
->stack_len
;
828 return reftable_reader_max_update_index(st
->readers
[sz
- 1]) +
833 static int stack_compact_locked(struct reftable_stack
*st
,
834 size_t first
, size_t last
,
835 struct strbuf
*temp_tab
,
836 struct reftable_log_expiry_config
*config
)
838 struct strbuf next_name
= STRBUF_INIT
;
840 struct reftable_writer
*wr
= NULL
;
843 format_name(&next_name
,
844 reftable_reader_min_update_index(st
->readers
[first
]),
845 reftable_reader_max_update_index(st
->readers
[last
]));
847 stack_filename(temp_tab
, st
, next_name
.buf
);
848 strbuf_addstr(temp_tab
, ".temp.XXXXXX");
850 tab_fd
= mkstemp(temp_tab
->buf
);
851 if (st
->config
.default_permissions
&&
852 chmod(temp_tab
->buf
, st
->config
.default_permissions
) < 0) {
853 err
= REFTABLE_IO_ERROR
;
857 wr
= reftable_new_writer(reftable_fd_write
, reftable_fd_flush
, &tab_fd
, &st
->config
);
859 err
= stack_write_compact(st
, wr
, first
, last
, config
);
862 err
= reftable_writer_close(wr
);
870 reftable_writer_free(wr
);
875 if (err
!= 0 && temp_tab
->len
> 0) {
876 unlink(temp_tab
->buf
);
877 strbuf_release(temp_tab
);
879 strbuf_release(&next_name
);
883 static int stack_write_compact(struct reftable_stack
*st
,
884 struct reftable_writer
*wr
,
885 size_t first
, size_t last
,
886 struct reftable_log_expiry_config
*config
)
888 size_t subtabs_len
= last
- first
+ 1;
889 struct reftable_table
*subtabs
= reftable_calloc(
890 last
- first
+ 1, sizeof(*subtabs
));
891 struct reftable_merged_table
*mt
= NULL
;
892 struct reftable_iterator it
= { NULL
};
893 struct reftable_ref_record ref
= { NULL
};
894 struct reftable_log_record log
= { NULL
};
895 uint64_t entries
= 0;
898 for (size_t i
= first
, j
= 0; i
<= last
; i
++) {
899 struct reftable_reader
*t
= st
->readers
[i
];
900 reftable_table_from_reader(&subtabs
[j
++], t
);
901 st
->stats
.bytes
+= t
->size
;
903 reftable_writer_set_limits(wr
, st
->readers
[first
]->min_update_index
,
904 st
->readers
[last
]->max_update_index
);
906 err
= reftable_new_merged_table(&mt
, subtabs
, subtabs_len
,
909 reftable_free(subtabs
);
913 err
= reftable_merged_table_seek_ref(mt
, &it
, "");
918 err
= reftable_iterator_next_ref(&it
, &ref
);
926 if (first
== 0 && reftable_ref_record_is_deletion(&ref
)) {
930 err
= reftable_writer_add_ref(wr
, &ref
);
935 reftable_iterator_destroy(&it
);
937 err
= reftable_merged_table_seek_log(mt
, &it
, "");
942 err
= reftable_iterator_next_log(&it
, &log
);
949 if (first
== 0 && reftable_log_record_is_deletion(&log
)) {
953 if (config
&& config
->min_update_index
> 0 &&
954 log
.update_index
< config
->min_update_index
) {
958 if (config
&& config
->time
> 0 &&
959 log
.value
.update
.time
< config
->time
) {
963 err
= reftable_writer_add_log(wr
, &log
);
970 reftable_iterator_destroy(&it
);
972 merged_table_release(mt
);
973 reftable_merged_table_free(mt
);
975 reftable_ref_record_release(&ref
);
976 reftable_log_record_release(&log
);
977 st
->stats
.entries_written
+= entries
;
981 /* < 0: error. 0 == OK, > 0 attempt failed; could retry. */
982 static int stack_compact_range(struct reftable_stack
*st
,
983 size_t first
, size_t last
,
984 struct reftable_log_expiry_config
*expiry
)
986 char **delete_on_success
= NULL
, **subtable_locks
= NULL
, **listp
= NULL
;
987 struct strbuf temp_tab_file_name
= STRBUF_INIT
;
988 struct strbuf new_table_name
= STRBUF_INIT
;
989 struct strbuf lock_file_name
= STRBUF_INIT
;
990 struct strbuf ref_list_contents
= STRBUF_INIT
;
991 struct strbuf new_table_path
= STRBUF_INIT
;
992 size_t i
, j
, compact_count
;
995 int lock_file_fd
= -1;
996 int is_empty_table
= 0;
998 if (first
> last
|| (!expiry
&& first
== last
)) {
1003 compact_count
= last
- first
+ 1;
1004 REFTABLE_CALLOC_ARRAY(delete_on_success
, compact_count
+ 1);
1005 REFTABLE_CALLOC_ARRAY(subtable_locks
, compact_count
+ 1);
1007 st
->stats
.attempts
++;
1009 strbuf_reset(&lock_file_name
);
1010 strbuf_addstr(&lock_file_name
, st
->list_file
);
1011 strbuf_addstr(&lock_file_name
, ".lock");
1014 open(lock_file_name
.buf
, O_EXCL
| O_CREAT
| O_WRONLY
, 0666);
1015 if (lock_file_fd
< 0) {
1016 if (errno
== EEXIST
) {
1019 err
= REFTABLE_IO_ERROR
;
1023 /* Don't want to write to the lock for now. */
1024 close(lock_file_fd
);
1028 err
= stack_uptodate(st
);
1032 for (i
= first
, j
= 0; i
<= last
; i
++) {
1033 struct strbuf subtab_file_name
= STRBUF_INIT
;
1034 struct strbuf subtab_lock
= STRBUF_INIT
;
1035 int sublock_file_fd
= -1;
1037 stack_filename(&subtab_file_name
, st
,
1038 reader_name(st
->readers
[i
]));
1040 strbuf_reset(&subtab_lock
);
1041 strbuf_addbuf(&subtab_lock
, &subtab_file_name
);
1042 strbuf_addstr(&subtab_lock
, ".lock");
1044 sublock_file_fd
= open(subtab_lock
.buf
,
1045 O_EXCL
| O_CREAT
| O_WRONLY
, 0666);
1046 if (sublock_file_fd
>= 0) {
1047 close(sublock_file_fd
);
1048 } else if (sublock_file_fd
< 0) {
1049 if (errno
== EEXIST
) {
1052 err
= REFTABLE_IO_ERROR
;
1056 subtable_locks
[j
] = subtab_lock
.buf
;
1057 delete_on_success
[j
] = subtab_file_name
.buf
;
1064 err
= unlink(lock_file_name
.buf
);
1069 err
= stack_compact_locked(st
, first
, last
, &temp_tab_file_name
,
1071 /* Compaction + tombstones can create an empty table out of non-empty
1073 is_empty_table
= (err
== REFTABLE_EMPTY_TABLE_ERROR
);
1074 if (is_empty_table
) {
1081 open(lock_file_name
.buf
, O_EXCL
| O_CREAT
| O_WRONLY
, 0666);
1082 if (lock_file_fd
< 0) {
1083 if (errno
== EEXIST
) {
1086 err
= REFTABLE_IO_ERROR
;
1091 if (st
->config
.default_permissions
) {
1092 if (chmod(lock_file_name
.buf
, st
->config
.default_permissions
) < 0) {
1093 err
= REFTABLE_IO_ERROR
;
1098 format_name(&new_table_name
, st
->readers
[first
]->min_update_index
,
1099 st
->readers
[last
]->max_update_index
);
1100 strbuf_addstr(&new_table_name
, ".ref");
1102 stack_filename(&new_table_path
, st
, new_table_name
.buf
);
1104 if (!is_empty_table
) {
1106 err
= rename(temp_tab_file_name
.buf
, new_table_path
.buf
);
1108 err
= REFTABLE_IO_ERROR
;
1113 for (i
= 0; i
< first
; i
++) {
1114 strbuf_addstr(&ref_list_contents
, st
->readers
[i
]->name
);
1115 strbuf_addstr(&ref_list_contents
, "\n");
1117 if (!is_empty_table
) {
1118 strbuf_addbuf(&ref_list_contents
, &new_table_name
);
1119 strbuf_addstr(&ref_list_contents
, "\n");
1121 for (i
= last
+ 1; i
< st
->merged
->stack_len
; i
++) {
1122 strbuf_addstr(&ref_list_contents
, st
->readers
[i
]->name
);
1123 strbuf_addstr(&ref_list_contents
, "\n");
1126 err
= write_in_full(lock_file_fd
, ref_list_contents
.buf
, ref_list_contents
.len
);
1128 err
= REFTABLE_IO_ERROR
;
1129 unlink(new_table_path
.buf
);
1133 err
= fsync_component(FSYNC_COMPONENT_REFERENCE
, lock_file_fd
);
1135 err
= REFTABLE_IO_ERROR
;
1136 unlink(new_table_path
.buf
);
1140 err
= close(lock_file_fd
);
1143 err
= REFTABLE_IO_ERROR
;
1144 unlink(new_table_path
.buf
);
1148 err
= rename(lock_file_name
.buf
, st
->list_file
);
1150 err
= REFTABLE_IO_ERROR
;
1151 unlink(new_table_path
.buf
);
1156 /* Reload the stack before deleting. On windows, we can only delete the
1157 files after we closed them.
1159 err
= reftable_stack_reload_maybe_reuse(st
, first
< last
);
1161 listp
= delete_on_success
;
1163 if (strcmp(*listp
, new_table_path
.buf
)) {
1170 free_names(delete_on_success
);
1172 if (subtable_locks
) {
1173 listp
= subtable_locks
;
1178 free_names(subtable_locks
);
1180 if (lock_file_fd
>= 0) {
1181 close(lock_file_fd
);
1185 unlink(lock_file_name
.buf
);
1187 strbuf_release(&new_table_name
);
1188 strbuf_release(&new_table_path
);
1189 strbuf_release(&ref_list_contents
);
1190 strbuf_release(&temp_tab_file_name
);
1191 strbuf_release(&lock_file_name
);
1195 int reftable_stack_compact_all(struct reftable_stack
*st
,
1196 struct reftable_log_expiry_config
*config
)
1198 return stack_compact_range(st
, 0, st
->merged
->stack_len
?
1199 st
->merged
->stack_len
- 1 : 0, config
);
1202 static int stack_compact_range_stats(struct reftable_stack
*st
,
1203 size_t first
, size_t last
,
1204 struct reftable_log_expiry_config
*config
)
1206 int err
= stack_compact_range(st
, first
, last
, config
);
1208 st
->stats
.failures
++;
1212 static int segment_size(struct segment
*s
)
1214 return s
->end
- s
->start
;
1217 int fastlog2(uint64_t sz
)
1222 for (; sz
; sz
/= 2) {
1228 struct segment
*sizes_to_segments(size_t *seglen
, uint64_t *sizes
, size_t n
)
1230 struct segment
*segs
= reftable_calloc(n
, sizeof(*segs
));
1231 struct segment cur
= { 0 };
1238 for (i
= 0; i
< n
; i
++) {
1239 int log
= fastlog2(sizes
[i
]);
1240 if (cur
.log
!= log
&& cur
.bytes
> 0) {
1241 struct segment fresh
= {
1251 cur
.bytes
+= sizes
[i
];
1258 struct segment
suggest_compaction_segment(uint64_t *sizes
, size_t n
)
1260 struct segment min_seg
= {
1263 struct segment
*segs
;
1264 size_t seglen
= 0, i
;
1266 segs
= sizes_to_segments(&seglen
, sizes
, n
);
1267 for (i
= 0; i
< seglen
; i
++) {
1268 if (segment_size(&segs
[i
]) == 1)
1271 if (segs
[i
].log
< min_seg
.log
)
1275 while (min_seg
.start
> 0) {
1276 size_t prev
= min_seg
.start
- 1;
1277 if (fastlog2(min_seg
.bytes
) < fastlog2(sizes
[prev
]))
1280 min_seg
.start
= prev
;
1281 min_seg
.bytes
+= sizes
[prev
];
1284 reftable_free(segs
);
1288 static uint64_t *stack_table_sizes_for_compaction(struct reftable_stack
*st
)
1291 reftable_calloc(st
->merged
->stack_len
, sizeof(*sizes
));
1292 int version
= (st
->config
.hash_id
== GIT_SHA1_FORMAT_ID
) ? 1 : 2;
1293 int overhead
= header_size(version
) - 1;
1295 for (i
= 0; i
< st
->merged
->stack_len
; i
++) {
1296 sizes
[i
] = st
->readers
[i
]->size
- overhead
;
1301 int reftable_stack_auto_compact(struct reftable_stack
*st
)
1303 uint64_t *sizes
= stack_table_sizes_for_compaction(st
);
1304 struct segment seg
=
1305 suggest_compaction_segment(sizes
, st
->merged
->stack_len
);
1306 reftable_free(sizes
);
1307 if (segment_size(&seg
) > 0)
1308 return stack_compact_range_stats(st
, seg
.start
, seg
.end
- 1,
1314 struct reftable_compaction_stats
*
1315 reftable_stack_compaction_stats(struct reftable_stack
*st
)
1320 int reftable_stack_read_ref(struct reftable_stack
*st
, const char *refname
,
1321 struct reftable_ref_record
*ref
)
1323 struct reftable_table tab
= { NULL
};
1324 reftable_table_from_merged_table(&tab
, reftable_stack_merged_table(st
));
1325 return reftable_table_read_ref(&tab
, refname
, ref
);
1328 int reftable_stack_read_log(struct reftable_stack
*st
, const char *refname
,
1329 struct reftable_log_record
*log
)
1331 struct reftable_iterator it
= { NULL
};
1332 struct reftable_merged_table
*mt
= reftable_stack_merged_table(st
);
1333 int err
= reftable_merged_table_seek_log(mt
, &it
, refname
);
1337 err
= reftable_iterator_next_log(&it
, log
);
1341 if (strcmp(log
->refname
, refname
) ||
1342 reftable_log_record_is_deletion(log
)) {
1349 reftable_log_record_release(log
);
1351 reftable_iterator_destroy(&it
);
1355 static int stack_check_addition(struct reftable_stack
*st
,
1356 const char *new_tab_name
)
1359 struct reftable_block_source src
= { NULL
};
1360 struct reftable_reader
*rd
= NULL
;
1361 struct reftable_table tab
= { NULL
};
1362 struct reftable_ref_record
*refs
= NULL
;
1363 struct reftable_iterator it
= { NULL
};
1368 if (st
->config
.skip_name_check
)
1371 err
= reftable_block_source_from_file(&src
, new_tab_name
);
1375 err
= reftable_new_reader(&rd
, &src
, new_tab_name
);
1379 err
= reftable_reader_seek_ref(rd
, &it
, "");
1388 struct reftable_ref_record ref
= { NULL
};
1389 err
= reftable_iterator_next_ref(&it
, &ref
);
1395 REFTABLE_ALLOC_GROW(refs
, len
+ 1, cap
);
1399 reftable_table_from_merged_table(&tab
, reftable_stack_merged_table(st
));
1401 err
= validate_ref_record_addition(tab
, refs
, len
);
1404 for (i
= 0; i
< len
; i
++) {
1405 reftable_ref_record_release(&refs
[i
]);
1409 reftable_iterator_destroy(&it
);
1410 reftable_reader_free(rd
);
1414 static int is_table_name(const char *s
)
1416 const char *dot
= strrchr(s
, '.');
1417 return dot
&& !strcmp(dot
, ".ref");
1420 static void remove_maybe_stale_table(struct reftable_stack
*st
, uint64_t max
,
1424 uint64_t update_idx
= 0;
1425 struct reftable_block_source src
= { NULL
};
1426 struct reftable_reader
*rd
= NULL
;
1427 struct strbuf table_path
= STRBUF_INIT
;
1428 stack_filename(&table_path
, st
, name
);
1430 err
= reftable_block_source_from_file(&src
, table_path
.buf
);
1434 err
= reftable_new_reader(&rd
, &src
, name
);
1438 update_idx
= reftable_reader_max_update_index(rd
);
1439 reftable_reader_free(rd
);
1441 if (update_idx
<= max
) {
1442 unlink(table_path
.buf
);
1445 strbuf_release(&table_path
);
1448 static int reftable_stack_clean_locked(struct reftable_stack
*st
)
1450 uint64_t max
= reftable_merged_table_max_update_index(
1451 reftable_stack_merged_table(st
));
1452 DIR *dir
= opendir(st
->reftable_dir
);
1453 struct dirent
*d
= NULL
;
1455 return REFTABLE_IO_ERROR
;
1458 while ((d
= readdir(dir
))) {
1461 if (!is_table_name(d
->d_name
))
1464 for (i
= 0; !found
&& i
< st
->readers_len
; i
++) {
1465 found
= !strcmp(reader_name(st
->readers
[i
]), d
->d_name
);
1470 remove_maybe_stale_table(st
, max
, d
->d_name
);
1477 int reftable_stack_clean(struct reftable_stack
*st
)
1479 struct reftable_addition
*add
= NULL
;
1480 int err
= reftable_stack_new_addition(&add
, st
);
1485 err
= reftable_stack_reload(st
);
1490 err
= reftable_stack_clean_locked(st
);
1493 reftable_addition_destroy(add
);
1497 int reftable_stack_print_directory(const char *stackdir
, uint32_t hash_id
)
1499 struct reftable_stack
*stack
= NULL
;
1500 struct reftable_write_options cfg
= { .hash_id
= hash_id
};
1501 struct reftable_merged_table
*merged
= NULL
;
1502 struct reftable_table table
= { NULL
};
1504 int err
= reftable_new_stack(&stack
, stackdir
, cfg
);
1508 merged
= reftable_stack_merged_table(stack
);
1509 reftable_table_from_merged_table(&table
, merged
);
1510 err
= reftable_table_print(&table
);
1513 reftable_stack_destroy(stack
);