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
15 #include "reftable-error.h"
16 #include "reftable-record.h"
17 #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
, int first
, int last
,
28 struct reftable_log_expiry_config
*config
);
29 static int stack_check_addition(struct reftable_stack
*st
,
30 const char *new_tab_name
);
31 static void reftable_addition_close(struct reftable_addition
*add
);
32 static int reftable_stack_reload_maybe_reuse(struct reftable_stack
*st
,
35 static void stack_filename(struct strbuf
*dest
, struct reftable_stack
*st
,
39 strbuf_addstr(dest
, st
->reftable_dir
);
40 strbuf_addstr(dest
, "/");
41 strbuf_addstr(dest
, name
);
44 static ssize_t
reftable_fd_write(void *arg
, const void *data
, size_t sz
)
46 int *fdp
= (int *)arg
;
47 return write_in_full(*fdp
, data
, sz
);
50 int reftable_new_stack(struct reftable_stack
**dest
, const char *dir
,
51 struct reftable_write_options config
)
53 struct reftable_stack
*p
= reftable_calloc(1, sizeof(*p
));
54 struct strbuf list_file_name
= STRBUF_INIT
;
57 if (config
.hash_id
== 0) {
58 config
.hash_id
= GIT_SHA1_FORMAT_ID
;
63 strbuf_reset(&list_file_name
);
64 strbuf_addstr(&list_file_name
, dir
);
65 strbuf_addstr(&list_file_name
, "/tables.list");
67 p
->list_file
= strbuf_detach(&list_file_name
, NULL
);
69 p
->reftable_dir
= xstrdup(dir
);
72 err
= reftable_stack_reload_maybe_reuse(p
, 1);
74 reftable_stack_destroy(p
);
81 static int fd_read_lines(int fd
, char ***namesp
)
83 off_t size
= lseek(fd
, 0, SEEK_END
);
87 err
= REFTABLE_IO_ERROR
;
90 err
= lseek(fd
, 0, SEEK_SET
);
92 err
= REFTABLE_IO_ERROR
;
96 REFTABLE_ALLOC_ARRAY(buf
, size
+ 1);
97 if (read_in_full(fd
, buf
, size
) != size
) {
98 err
= REFTABLE_IO_ERROR
;
103 parse_names(buf
, size
, namesp
);
110 int read_lines(const char *filename
, char ***namesp
)
112 int fd
= open(filename
, O_RDONLY
);
115 if (errno
== ENOENT
) {
116 REFTABLE_CALLOC_ARRAY(*namesp
, 1);
120 return REFTABLE_IO_ERROR
;
122 err
= fd_read_lines(fd
, namesp
);
127 struct reftable_merged_table
*
128 reftable_stack_merged_table(struct reftable_stack
*st
)
133 static int has_name(char **names
, const char *name
)
136 if (!strcmp(*names
, name
))
143 /* Close and free the stack */
144 void reftable_stack_destroy(struct reftable_stack
*st
)
149 reftable_merged_table_free(st
->merged
);
153 err
= read_lines(st
->list_file
, &names
);
155 FREE_AND_NULL(names
);
160 struct strbuf filename
= STRBUF_INIT
;
161 for (i
= 0; i
< st
->readers_len
; i
++) {
162 const char *name
= reader_name(st
->readers
[i
]);
163 strbuf_reset(&filename
);
164 if (names
&& !has_name(names
, name
)) {
165 stack_filename(&filename
, st
, name
);
167 reftable_reader_free(st
->readers
[i
]);
170 /* On Windows, can only unlink after closing. */
171 unlink(filename
.buf
);
174 strbuf_release(&filename
);
176 FREE_AND_NULL(st
->readers
);
179 if (st
->list_fd
>= 0) {
184 FREE_AND_NULL(st
->list_file
);
185 FREE_AND_NULL(st
->reftable_dir
);
190 static struct reftable_reader
**stack_copy_readers(struct reftable_stack
*st
,
193 struct reftable_reader
**cur
= reftable_calloc(cur_len
, sizeof(*cur
));
195 for (i
= 0; i
< cur_len
; i
++) {
196 cur
[i
] = st
->readers
[i
];
201 static int reftable_stack_reload_once(struct reftable_stack
*st
, char **names
,
204 int cur_len
= !st
->merged
? 0 : st
->merged
->stack_len
;
205 struct reftable_reader
**cur
= stack_copy_readers(st
, cur_len
);
207 int names_len
= names_length(names
);
208 struct reftable_reader
**new_readers
=
209 reftable_calloc(names_len
, sizeof(*new_readers
));
210 struct reftable_table
*new_tables
=
211 reftable_calloc(names_len
, sizeof(*new_tables
));
212 int new_readers_len
= 0;
213 struct reftable_merged_table
*new_merged
= NULL
;
214 struct strbuf table_path
= STRBUF_INIT
;
218 struct reftable_reader
*rd
= NULL
;
219 char *name
= *names
++;
221 /* this is linear; we assume compaction keeps the number of
222 tables under control so this is not quadratic. */
224 for (j
= 0; reuse_open
&& j
< cur_len
; j
++) {
225 if (cur
[j
] && 0 == strcmp(cur
[j
]->name
, name
)) {
233 struct reftable_block_source src
= { NULL
};
234 stack_filename(&table_path
, st
, name
);
236 err
= reftable_block_source_from_file(&src
,
241 err
= reftable_new_reader(&rd
, &src
, name
);
246 new_readers
[new_readers_len
] = rd
;
247 reftable_table_from_reader(&new_tables
[new_readers_len
], rd
);
252 err
= reftable_new_merged_table(&new_merged
, new_tables
,
253 new_readers_len
, st
->config
.hash_id
);
258 st
->readers_len
= new_readers_len
;
260 merged_table_release(st
->merged
);
261 reftable_merged_table_free(st
->merged
);
264 reftable_free(st
->readers
);
266 st
->readers
= new_readers
;
270 new_merged
->suppress_deletions
= 1;
271 st
->merged
= new_merged
;
272 for (i
= 0; i
< cur_len
; i
++) {
274 const char *name
= reader_name(cur
[i
]);
275 stack_filename(&table_path
, st
, name
);
277 reader_close(cur
[i
]);
278 reftable_reader_free(cur
[i
]);
280 /* On Windows, can only unlink after closing. */
281 unlink(table_path
.buf
);
286 for (i
= 0; i
< new_readers_len
; i
++) {
287 reader_close(new_readers
[i
]);
288 reftable_reader_free(new_readers
[i
]);
290 reftable_free(new_readers
);
291 reftable_free(new_tables
);
293 strbuf_release(&table_path
);
297 /* return negative if a before b. */
298 static int tv_cmp(struct timeval
*a
, struct timeval
*b
)
300 time_t diff
= a
->tv_sec
- b
->tv_sec
;
301 int udiff
= a
->tv_usec
- b
->tv_usec
;
309 static int reftable_stack_reload_maybe_reuse(struct reftable_stack
*st
,
312 char **names
= NULL
, **names_after
= NULL
;
313 struct timeval deadline
;
318 err
= gettimeofday(&deadline
, NULL
);
321 deadline
.tv_sec
+= 3;
326 err
= gettimeofday(&now
, NULL
);
331 * Only look at deadlines after the first few times. This
332 * simplifies debugging in GDB.
335 if (tries
> 3 && tv_cmp(&now
, &deadline
) >= 0)
338 fd
= open(st
->list_file
, O_RDONLY
);
340 if (errno
!= ENOENT
) {
341 err
= REFTABLE_IO_ERROR
;
345 REFTABLE_CALLOC_ARRAY(names
, 1);
347 err
= fd_read_lines(fd
, &names
);
352 err
= reftable_stack_reload_once(st
, names
, reuse_open
);
355 if (err
!= REFTABLE_NOT_EXIST_ERROR
)
359 * REFTABLE_NOT_EXIST_ERROR can be caused by a concurrent
360 * writer. Check if there was one by checking if the name list
363 err
= read_lines(st
->list_file
, &names_after
);
366 if (names_equal(names_after
, names
)) {
367 err
= REFTABLE_NOT_EXIST_ERROR
;
373 free_names(names_after
);
378 delay
= delay
+ (delay
* rand()) / RAND_MAX
+ 1;
379 sleep_millisec(delay
);
384 * Invalidate the stat cache. It is sufficient to only close the file
385 * descriptor and keep the cached stat info because we never use the
386 * latter when the former is negative.
388 if (st
->list_fd
>= 0) {
394 * Cache stat information in case it provides a useful signal to us.
395 * According to POSIX, "The st_ino and st_dev fields taken together
396 * uniquely identify the file within the system." That being said,
397 * Windows is not POSIX compliant and we do not have these fields
398 * available. So the information we have there is insufficient to
399 * determine whether two file descriptors point to the same file.
401 * While we could fall back to using other signals like the file's
402 * mtime, those are not sufficient to avoid races. We thus refrain from
403 * using the stat cache on such systems and fall back to the secondary
404 * caching mechanism, which is to check whether contents of the file
407 * On other systems which are POSIX compliant we must keep the file
408 * descriptor open. This is to avoid a race condition where two
409 * processes access the reftable stack at the same point in time:
411 * 1. A reads the reftable stack and caches its stat info.
413 * 2. B updates the stack, appending a new table to "tables.list".
414 * This will both use a new inode and result in a different file
415 * size, thus invalidating A's cache in theory.
417 * 3. B decides to auto-compact the stack and merges two tables. The
418 * file size now matches what A has cached again. Furthermore, the
419 * filesystem may decide to recycle the inode number of the file
420 * we have replaced in (2) because it is not in use anymore.
422 * 4. A reloads the reftable stack. Neither the inode number nor the
423 * file size changed. If the timestamps did not change either then
424 * we think the cached copy of our stack is up-to-date.
426 * By keeping the file descriptor open the inode number cannot be
427 * recycled, mitigating the race.
429 if (!err
&& fd
>= 0 && !fstat(fd
, &st
->list_st
) &&
430 st
->list_st
.st_dev
&& st
->list_st
.st_ino
) {
438 free_names(names_after
);
445 static int stack_uptodate(struct reftable_stack
*st
)
452 * When we have cached stat information available then we use it to
453 * verify whether the file has been rewritten.
455 * Note that we explicitly do not want to use `stat_validity_check()`
456 * and friends here because they may end up not comparing the `st_dev`
457 * and `st_ino` fields. These functions thus cannot guarantee that we
458 * indeed still have the same file.
460 if (st
->list_fd
>= 0) {
463 if (stat(st
->list_file
, &list_st
) < 0) {
465 * It's fine for "tables.list" to not exist. In that
466 * case, we have to refresh when the loaded stack has
470 return !!st
->readers_len
;
471 return REFTABLE_IO_ERROR
;
475 * When "tables.list" refers to the same file we can assume
476 * that it didn't change. This is because we always use
477 * rename(3P) to update the file and never write to it
480 if (st
->list_st
.st_dev
== list_st
.st_dev
&&
481 st
->list_st
.st_ino
== list_st
.st_ino
)
485 err
= read_lines(st
->list_file
, &names
);
489 for (i
= 0; i
< st
->readers_len
; i
++) {
495 if (strcmp(st
->readers
[i
]->name
, names
[i
])) {
501 if (names
[st
->merged
->stack_len
]) {
511 int reftable_stack_reload(struct reftable_stack
*st
)
513 int err
= stack_uptodate(st
);
515 return reftable_stack_reload_maybe_reuse(st
, 1);
519 int reftable_stack_add(struct reftable_stack
*st
,
520 int (*write
)(struct reftable_writer
*wr
, void *arg
),
523 int err
= stack_try_add(st
, write
, arg
);
525 if (err
== REFTABLE_LOCK_ERROR
) {
526 /* Ignore error return, we want to propagate
529 reftable_stack_reload(st
);
537 static void format_name(struct strbuf
*dest
, uint64_t min
, uint64_t max
)
540 uint32_t rnd
= (uint32_t)git_rand();
541 snprintf(buf
, sizeof(buf
), "0x%012" PRIx64
"-0x%012" PRIx64
"-%08x",
544 strbuf_addstr(dest
, buf
);
547 struct reftable_addition
{
548 struct tempfile
*lock_file
;
549 struct reftable_stack
*stack
;
552 size_t new_tables_len
, new_tables_cap
;
553 uint64_t next_update_index
;
556 #define REFTABLE_ADDITION_INIT {0}
558 static int reftable_stack_init_addition(struct reftable_addition
*add
,
559 struct reftable_stack
*st
)
561 struct strbuf lock_file_name
= STRBUF_INIT
;
565 strbuf_addf(&lock_file_name
, "%s.lock", st
->list_file
);
567 add
->lock_file
= create_tempfile(lock_file_name
.buf
);
568 if (!add
->lock_file
) {
569 if (errno
== EEXIST
) {
570 err
= REFTABLE_LOCK_ERROR
;
572 err
= REFTABLE_IO_ERROR
;
576 if (st
->config
.default_permissions
) {
577 if (chmod(add
->lock_file
->filename
.buf
, st
->config
.default_permissions
) < 0) {
578 err
= REFTABLE_IO_ERROR
;
583 err
= stack_uptodate(st
);
588 err
= REFTABLE_LOCK_ERROR
;
592 add
->next_update_index
= reftable_stack_next_update_index(st
);
595 reftable_addition_close(add
);
597 strbuf_release(&lock_file_name
);
601 static void reftable_addition_close(struct reftable_addition
*add
)
603 struct strbuf nm
= STRBUF_INIT
;
606 for (i
= 0; i
< add
->new_tables_len
; i
++) {
607 stack_filename(&nm
, add
->stack
, add
->new_tables
[i
]);
609 reftable_free(add
->new_tables
[i
]);
610 add
->new_tables
[i
] = NULL
;
612 reftable_free(add
->new_tables
);
613 add
->new_tables
= NULL
;
614 add
->new_tables_len
= 0;
615 add
->new_tables_cap
= 0;
617 delete_tempfile(&add
->lock_file
);
621 void reftable_addition_destroy(struct reftable_addition
*add
)
626 reftable_addition_close(add
);
630 int reftable_addition_commit(struct reftable_addition
*add
)
632 struct strbuf table_list
= STRBUF_INIT
;
633 int lock_file_fd
= get_tempfile_fd(add
->lock_file
);
637 if (add
->new_tables_len
== 0)
640 for (i
= 0; i
< add
->stack
->merged
->stack_len
; i
++) {
641 strbuf_addstr(&table_list
, add
->stack
->readers
[i
]->name
);
642 strbuf_addstr(&table_list
, "\n");
644 for (i
= 0; i
< add
->new_tables_len
; i
++) {
645 strbuf_addstr(&table_list
, add
->new_tables
[i
]);
646 strbuf_addstr(&table_list
, "\n");
649 err
= write_in_full(lock_file_fd
, table_list
.buf
, table_list
.len
);
650 strbuf_release(&table_list
);
652 err
= REFTABLE_IO_ERROR
;
656 err
= rename_tempfile(&add
->lock_file
, add
->stack
->list_file
);
658 err
= REFTABLE_IO_ERROR
;
662 /* success, no more state to clean up. */
663 for (i
= 0; i
< add
->new_tables_len
; i
++)
664 reftable_free(add
->new_tables
[i
]);
665 reftable_free(add
->new_tables
);
666 add
->new_tables
= NULL
;
667 add
->new_tables_len
= 0;
668 add
->new_tables_cap
= 0;
670 err
= reftable_stack_reload_maybe_reuse(add
->stack
, 1);
674 if (!add
->stack
->disable_auto_compact
)
675 err
= reftable_stack_auto_compact(add
->stack
);
678 reftable_addition_close(add
);
682 int reftable_stack_new_addition(struct reftable_addition
**dest
,
683 struct reftable_stack
*st
)
686 struct reftable_addition empty
= REFTABLE_ADDITION_INIT
;
687 REFTABLE_CALLOC_ARRAY(*dest
, 1);
689 err
= reftable_stack_init_addition(*dest
, st
);
691 reftable_free(*dest
);
697 static int stack_try_add(struct reftable_stack
*st
,
698 int (*write_table
)(struct reftable_writer
*wr
,
702 struct reftable_addition add
= REFTABLE_ADDITION_INIT
;
703 int err
= reftable_stack_init_addition(&add
, st
);
707 err
= REFTABLE_LOCK_ERROR
;
711 err
= reftable_addition_add(&add
, write_table
, arg
);
715 err
= reftable_addition_commit(&add
);
717 reftable_addition_close(&add
);
721 int reftable_addition_add(struct reftable_addition
*add
,
722 int (*write_table
)(struct reftable_writer
*wr
,
726 struct strbuf temp_tab_file_name
= STRBUF_INIT
;
727 struct strbuf tab_file_name
= STRBUF_INIT
;
728 struct strbuf next_name
= STRBUF_INIT
;
729 struct reftable_writer
*wr
= NULL
;
733 strbuf_reset(&next_name
);
734 format_name(&next_name
, add
->next_update_index
, add
->next_update_index
);
736 stack_filename(&temp_tab_file_name
, add
->stack
, next_name
.buf
);
737 strbuf_addstr(&temp_tab_file_name
, ".temp.XXXXXX");
739 tab_fd
= mkstemp(temp_tab_file_name
.buf
);
741 err
= REFTABLE_IO_ERROR
;
744 if (add
->stack
->config
.default_permissions
) {
745 if (chmod(temp_tab_file_name
.buf
, add
->stack
->config
.default_permissions
)) {
746 err
= REFTABLE_IO_ERROR
;
750 wr
= reftable_new_writer(reftable_fd_write
, &tab_fd
,
751 &add
->stack
->config
);
752 err
= write_table(wr
, arg
);
756 err
= reftable_writer_close(wr
);
757 if (err
== REFTABLE_EMPTY_TABLE_ERROR
) {
767 err
= REFTABLE_IO_ERROR
;
771 err
= stack_check_addition(add
->stack
, temp_tab_file_name
.buf
);
775 if (wr
->min_update_index
< add
->next_update_index
) {
776 err
= REFTABLE_API_ERROR
;
780 format_name(&next_name
, wr
->min_update_index
, wr
->max_update_index
);
781 strbuf_addstr(&next_name
, ".ref");
783 stack_filename(&tab_file_name
, add
->stack
, next_name
.buf
);
786 On windows, this relies on rand() picking a unique destination name.
787 Maybe we should do retry loop as well?
789 err
= rename(temp_tab_file_name
.buf
, tab_file_name
.buf
);
791 err
= REFTABLE_IO_ERROR
;
795 REFTABLE_ALLOC_GROW(add
->new_tables
, add
->new_tables_len
+ 1,
796 add
->new_tables_cap
);
797 add
->new_tables
[add
->new_tables_len
++] = strbuf_detach(&next_name
, NULL
);
803 if (temp_tab_file_name
.len
> 0) {
804 unlink(temp_tab_file_name
.buf
);
807 strbuf_release(&temp_tab_file_name
);
808 strbuf_release(&tab_file_name
);
809 strbuf_release(&next_name
);
810 reftable_writer_free(wr
);
814 uint64_t reftable_stack_next_update_index(struct reftable_stack
*st
)
816 int sz
= st
->merged
->stack_len
;
818 return reftable_reader_max_update_index(st
->readers
[sz
- 1]) +
823 static int stack_compact_locked(struct reftable_stack
*st
, int first
, int last
,
824 struct strbuf
*temp_tab
,
825 struct reftable_log_expiry_config
*config
)
827 struct strbuf next_name
= STRBUF_INIT
;
829 struct reftable_writer
*wr
= NULL
;
832 format_name(&next_name
,
833 reftable_reader_min_update_index(st
->readers
[first
]),
834 reftable_reader_max_update_index(st
->readers
[last
]));
836 stack_filename(temp_tab
, st
, next_name
.buf
);
837 strbuf_addstr(temp_tab
, ".temp.XXXXXX");
839 tab_fd
= mkstemp(temp_tab
->buf
);
840 wr
= reftable_new_writer(reftable_fd_write
, &tab_fd
, &st
->config
);
842 err
= stack_write_compact(st
, wr
, first
, last
, config
);
845 err
= reftable_writer_close(wr
);
853 reftable_writer_free(wr
);
858 if (err
!= 0 && temp_tab
->len
> 0) {
859 unlink(temp_tab
->buf
);
860 strbuf_release(temp_tab
);
862 strbuf_release(&next_name
);
866 static int stack_write_compact(struct reftable_stack
*st
,
867 struct reftable_writer
*wr
, int first
, int last
,
868 struct reftable_log_expiry_config
*config
)
870 int subtabs_len
= last
- first
+ 1;
871 struct reftable_table
*subtabs
= reftable_calloc(
872 last
- first
+ 1, sizeof(*subtabs
));
873 struct reftable_merged_table
*mt
= NULL
;
875 struct reftable_iterator it
= { NULL
};
876 struct reftable_ref_record ref
= { NULL
};
877 struct reftable_log_record log
= { NULL
};
879 uint64_t entries
= 0;
882 for (i
= first
, j
= 0; i
<= last
; i
++) {
883 struct reftable_reader
*t
= st
->readers
[i
];
884 reftable_table_from_reader(&subtabs
[j
++], t
);
885 st
->stats
.bytes
+= t
->size
;
887 reftable_writer_set_limits(wr
, st
->readers
[first
]->min_update_index
,
888 st
->readers
[last
]->max_update_index
);
890 err
= reftable_new_merged_table(&mt
, subtabs
, subtabs_len
,
893 reftable_free(subtabs
);
897 err
= reftable_merged_table_seek_ref(mt
, &it
, "");
902 err
= reftable_iterator_next_ref(&it
, &ref
);
910 if (first
== 0 && reftable_ref_record_is_deletion(&ref
)) {
914 err
= reftable_writer_add_ref(wr
, &ref
);
919 reftable_iterator_destroy(&it
);
921 err
= reftable_merged_table_seek_log(mt
, &it
, "");
926 err
= reftable_iterator_next_log(&it
, &log
);
933 if (first
== 0 && reftable_log_record_is_deletion(&log
)) {
937 if (config
&& config
->min_update_index
> 0 &&
938 log
.update_index
< config
->min_update_index
) {
942 if (config
&& config
->time
> 0 &&
943 log
.value
.update
.time
< config
->time
) {
947 err
= reftable_writer_add_log(wr
, &log
);
954 reftable_iterator_destroy(&it
);
956 merged_table_release(mt
);
957 reftable_merged_table_free(mt
);
959 reftable_ref_record_release(&ref
);
960 reftable_log_record_release(&log
);
961 st
->stats
.entries_written
+= entries
;
965 /* < 0: error. 0 == OK, > 0 attempt failed; could retry. */
966 static int stack_compact_range(struct reftable_stack
*st
, int first
, int last
,
967 struct reftable_log_expiry_config
*expiry
)
969 struct strbuf temp_tab_file_name
= STRBUF_INIT
;
970 struct strbuf new_table_name
= STRBUF_INIT
;
971 struct strbuf lock_file_name
= STRBUF_INIT
;
972 struct strbuf ref_list_contents
= STRBUF_INIT
;
973 struct strbuf new_table_path
= STRBUF_INIT
;
976 int lock_file_fd
= -1;
977 int compact_count
= last
- first
+ 1;
979 char **delete_on_success
=
980 reftable_calloc(compact_count
+ 1, sizeof(*delete_on_success
));
981 char **subtable_locks
=
982 reftable_calloc(compact_count
+ 1, sizeof(*subtable_locks
));
985 int is_empty_table
= 0;
987 if (first
> last
|| (!expiry
&& first
== last
)) {
992 st
->stats
.attempts
++;
994 strbuf_reset(&lock_file_name
);
995 strbuf_addstr(&lock_file_name
, st
->list_file
);
996 strbuf_addstr(&lock_file_name
, ".lock");
999 open(lock_file_name
.buf
, O_EXCL
| O_CREAT
| O_WRONLY
, 0666);
1000 if (lock_file_fd
< 0) {
1001 if (errno
== EEXIST
) {
1004 err
= REFTABLE_IO_ERROR
;
1008 /* Don't want to write to the lock for now. */
1009 close(lock_file_fd
);
1013 err
= stack_uptodate(st
);
1017 for (i
= first
, j
= 0; i
<= last
; i
++) {
1018 struct strbuf subtab_file_name
= STRBUF_INIT
;
1019 struct strbuf subtab_lock
= STRBUF_INIT
;
1020 int sublock_file_fd
= -1;
1022 stack_filename(&subtab_file_name
, st
,
1023 reader_name(st
->readers
[i
]));
1025 strbuf_reset(&subtab_lock
);
1026 strbuf_addbuf(&subtab_lock
, &subtab_file_name
);
1027 strbuf_addstr(&subtab_lock
, ".lock");
1029 sublock_file_fd
= open(subtab_lock
.buf
,
1030 O_EXCL
| O_CREAT
| O_WRONLY
, 0666);
1031 if (sublock_file_fd
>= 0) {
1032 close(sublock_file_fd
);
1033 } else if (sublock_file_fd
< 0) {
1034 if (errno
== EEXIST
) {
1037 err
= REFTABLE_IO_ERROR
;
1041 subtable_locks
[j
] = subtab_lock
.buf
;
1042 delete_on_success
[j
] = subtab_file_name
.buf
;
1049 err
= unlink(lock_file_name
.buf
);
1054 err
= stack_compact_locked(st
, first
, last
, &temp_tab_file_name
,
1056 /* Compaction + tombstones can create an empty table out of non-empty
1058 is_empty_table
= (err
== REFTABLE_EMPTY_TABLE_ERROR
);
1059 if (is_empty_table
) {
1066 open(lock_file_name
.buf
, O_EXCL
| O_CREAT
| O_WRONLY
, 0666);
1067 if (lock_file_fd
< 0) {
1068 if (errno
== EEXIST
) {
1071 err
= REFTABLE_IO_ERROR
;
1076 if (st
->config
.default_permissions
) {
1077 if (chmod(lock_file_name
.buf
, st
->config
.default_permissions
) < 0) {
1078 err
= REFTABLE_IO_ERROR
;
1083 format_name(&new_table_name
, st
->readers
[first
]->min_update_index
,
1084 st
->readers
[last
]->max_update_index
);
1085 strbuf_addstr(&new_table_name
, ".ref");
1087 stack_filename(&new_table_path
, st
, new_table_name
.buf
);
1089 if (!is_empty_table
) {
1091 err
= rename(temp_tab_file_name
.buf
, new_table_path
.buf
);
1093 err
= REFTABLE_IO_ERROR
;
1098 for (i
= 0; i
< first
; i
++) {
1099 strbuf_addstr(&ref_list_contents
, st
->readers
[i
]->name
);
1100 strbuf_addstr(&ref_list_contents
, "\n");
1102 if (!is_empty_table
) {
1103 strbuf_addbuf(&ref_list_contents
, &new_table_name
);
1104 strbuf_addstr(&ref_list_contents
, "\n");
1106 for (i
= last
+ 1; i
< st
->merged
->stack_len
; i
++) {
1107 strbuf_addstr(&ref_list_contents
, st
->readers
[i
]->name
);
1108 strbuf_addstr(&ref_list_contents
, "\n");
1111 err
= write_in_full(lock_file_fd
, ref_list_contents
.buf
, ref_list_contents
.len
);
1113 err
= REFTABLE_IO_ERROR
;
1114 unlink(new_table_path
.buf
);
1117 err
= close(lock_file_fd
);
1120 err
= REFTABLE_IO_ERROR
;
1121 unlink(new_table_path
.buf
);
1125 err
= rename(lock_file_name
.buf
, st
->list_file
);
1127 err
= REFTABLE_IO_ERROR
;
1128 unlink(new_table_path
.buf
);
1133 /* Reload the stack before deleting. On windows, we can only delete the
1134 files after we closed them.
1136 err
= reftable_stack_reload_maybe_reuse(st
, first
< last
);
1138 listp
= delete_on_success
;
1140 if (strcmp(*listp
, new_table_path
.buf
)) {
1147 free_names(delete_on_success
);
1149 listp
= subtable_locks
;
1154 free_names(subtable_locks
);
1155 if (lock_file_fd
>= 0) {
1156 close(lock_file_fd
);
1160 unlink(lock_file_name
.buf
);
1162 strbuf_release(&new_table_name
);
1163 strbuf_release(&new_table_path
);
1164 strbuf_release(&ref_list_contents
);
1165 strbuf_release(&temp_tab_file_name
);
1166 strbuf_release(&lock_file_name
);
1170 int reftable_stack_compact_all(struct reftable_stack
*st
,
1171 struct reftable_log_expiry_config
*config
)
1173 return stack_compact_range(st
, 0, st
->merged
->stack_len
- 1, config
);
1176 static int stack_compact_range_stats(struct reftable_stack
*st
, int first
,
1178 struct reftable_log_expiry_config
*config
)
1180 int err
= stack_compact_range(st
, first
, last
, config
);
1182 st
->stats
.failures
++;
1187 static int segment_size(struct segment
*s
)
1189 return s
->end
- s
->start
;
1192 int fastlog2(uint64_t sz
)
1197 for (; sz
; sz
/= 2) {
1203 struct segment
*sizes_to_segments(int *seglen
, uint64_t *sizes
, int n
)
1205 struct segment
*segs
= reftable_calloc(n
, sizeof(*segs
));
1207 struct segment cur
= { 0 };
1214 for (i
= 0; i
< n
; i
++) {
1215 int log
= fastlog2(sizes
[i
]);
1216 if (cur
.log
!= log
&& cur
.bytes
> 0) {
1217 struct segment fresh
= {
1227 cur
.bytes
+= sizes
[i
];
1234 struct segment
suggest_compaction_segment(uint64_t *sizes
, int n
)
1237 struct segment
*segs
= sizes_to_segments(&seglen
, sizes
, n
);
1238 struct segment min_seg
= {
1242 for (i
= 0; i
< seglen
; i
++) {
1243 if (segment_size(&segs
[i
]) == 1) {
1247 if (segs
[i
].log
< min_seg
.log
) {
1252 while (min_seg
.start
> 0) {
1253 int prev
= min_seg
.start
- 1;
1254 if (fastlog2(min_seg
.bytes
) < fastlog2(sizes
[prev
])) {
1258 min_seg
.start
= prev
;
1259 min_seg
.bytes
+= sizes
[prev
];
1262 reftable_free(segs
);
1266 static uint64_t *stack_table_sizes_for_compaction(struct reftable_stack
*st
)
1269 reftable_calloc(st
->merged
->stack_len
, sizeof(*sizes
));
1270 int version
= (st
->config
.hash_id
== GIT_SHA1_FORMAT_ID
) ? 1 : 2;
1271 int overhead
= header_size(version
) - 1;
1273 for (i
= 0; i
< st
->merged
->stack_len
; i
++) {
1274 sizes
[i
] = st
->readers
[i
]->size
- overhead
;
1279 int reftable_stack_auto_compact(struct reftable_stack
*st
)
1281 uint64_t *sizes
= stack_table_sizes_for_compaction(st
);
1282 struct segment seg
=
1283 suggest_compaction_segment(sizes
, st
->merged
->stack_len
);
1284 reftable_free(sizes
);
1285 if (segment_size(&seg
) > 0)
1286 return stack_compact_range_stats(st
, seg
.start
, seg
.end
- 1,
1292 struct reftable_compaction_stats
*
1293 reftable_stack_compaction_stats(struct reftable_stack
*st
)
1298 int reftable_stack_read_ref(struct reftable_stack
*st
, const char *refname
,
1299 struct reftable_ref_record
*ref
)
1301 struct reftable_table tab
= { NULL
};
1302 reftable_table_from_merged_table(&tab
, reftable_stack_merged_table(st
));
1303 return reftable_table_read_ref(&tab
, refname
, ref
);
1306 int reftable_stack_read_log(struct reftable_stack
*st
, const char *refname
,
1307 struct reftable_log_record
*log
)
1309 struct reftable_iterator it
= { NULL
};
1310 struct reftable_merged_table
*mt
= reftable_stack_merged_table(st
);
1311 int err
= reftable_merged_table_seek_log(mt
, &it
, refname
);
1315 err
= reftable_iterator_next_log(&it
, log
);
1319 if (strcmp(log
->refname
, refname
) ||
1320 reftable_log_record_is_deletion(log
)) {
1327 reftable_log_record_release(log
);
1329 reftable_iterator_destroy(&it
);
1333 static int stack_check_addition(struct reftable_stack
*st
,
1334 const char *new_tab_name
)
1337 struct reftable_block_source src
= { NULL
};
1338 struct reftable_reader
*rd
= NULL
;
1339 struct reftable_table tab
= { NULL
};
1340 struct reftable_ref_record
*refs
= NULL
;
1341 struct reftable_iterator it
= { NULL
};
1346 if (st
->config
.skip_name_check
)
1349 err
= reftable_block_source_from_file(&src
, new_tab_name
);
1353 err
= reftable_new_reader(&rd
, &src
, new_tab_name
);
1357 err
= reftable_reader_seek_ref(rd
, &it
, "");
1366 struct reftable_ref_record ref
= { NULL
};
1367 err
= reftable_iterator_next_ref(&it
, &ref
);
1373 REFTABLE_ALLOC_GROW(refs
, len
+ 1, cap
);
1377 reftable_table_from_merged_table(&tab
, reftable_stack_merged_table(st
));
1379 err
= validate_ref_record_addition(tab
, refs
, len
);
1382 for (i
= 0; i
< len
; i
++) {
1383 reftable_ref_record_release(&refs
[i
]);
1387 reftable_iterator_destroy(&it
);
1388 reftable_reader_free(rd
);
1392 static int is_table_name(const char *s
)
1394 const char *dot
= strrchr(s
, '.');
1395 return dot
&& !strcmp(dot
, ".ref");
1398 static void remove_maybe_stale_table(struct reftable_stack
*st
, uint64_t max
,
1402 uint64_t update_idx
= 0;
1403 struct reftable_block_source src
= { NULL
};
1404 struct reftable_reader
*rd
= NULL
;
1405 struct strbuf table_path
= STRBUF_INIT
;
1406 stack_filename(&table_path
, st
, name
);
1408 err
= reftable_block_source_from_file(&src
, table_path
.buf
);
1412 err
= reftable_new_reader(&rd
, &src
, name
);
1416 update_idx
= reftable_reader_max_update_index(rd
);
1417 reftable_reader_free(rd
);
1419 if (update_idx
<= max
) {
1420 unlink(table_path
.buf
);
1423 strbuf_release(&table_path
);
1426 static int reftable_stack_clean_locked(struct reftable_stack
*st
)
1428 uint64_t max
= reftable_merged_table_max_update_index(
1429 reftable_stack_merged_table(st
));
1430 DIR *dir
= opendir(st
->reftable_dir
);
1431 struct dirent
*d
= NULL
;
1433 return REFTABLE_IO_ERROR
;
1436 while ((d
= readdir(dir
))) {
1439 if (!is_table_name(d
->d_name
))
1442 for (i
= 0; !found
&& i
< st
->readers_len
; i
++) {
1443 found
= !strcmp(reader_name(st
->readers
[i
]), d
->d_name
);
1448 remove_maybe_stale_table(st
, max
, d
->d_name
);
1455 int reftable_stack_clean(struct reftable_stack
*st
)
1457 struct reftable_addition
*add
= NULL
;
1458 int err
= reftable_stack_new_addition(&add
, st
);
1463 err
= reftable_stack_reload(st
);
1468 err
= reftable_stack_clean_locked(st
);
1471 reftable_addition_destroy(add
);
1475 int reftable_stack_print_directory(const char *stackdir
, uint32_t hash_id
)
1477 struct reftable_stack
*stack
= NULL
;
1478 struct reftable_write_options cfg
= { .hash_id
= hash_id
};
1479 struct reftable_merged_table
*merged
= NULL
;
1480 struct reftable_table table
= { NULL
};
1482 int err
= reftable_new_stack(&stack
, stackdir
, cfg
);
1486 merged
= reftable_stack_merged_table(stack
);
1487 reftable_table_from_merged_table(&table
, merged
);
1488 err
= reftable_table_print(&table
);
1491 reftable_stack_destroy(stack
);