]> git.ipfire.org Git - thirdparty/git.git/blob - reftable/writer.c
Git 2.35.8
[thirdparty/git.git] / reftable / writer.c
1 /*
2 Copyright 2020 Google LLC
3
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
7 */
8
9 #include "writer.h"
10
11 #include "system.h"
12
13 #include "block.h"
14 #include "constants.h"
15 #include "record.h"
16 #include "tree.h"
17 #include "reftable-error.h"
18
19 /* finishes a block, and writes it to storage */
20 static int writer_flush_block(struct reftable_writer *w);
21
22 /* deallocates memory related to the index */
23 static void writer_clear_index(struct reftable_writer *w);
24
25 /* finishes writing a 'r' (refs) or 'g' (reflogs) section */
26 static int writer_finish_public_section(struct reftable_writer *w);
27
28 static struct reftable_block_stats *
29 writer_reftable_block_stats(struct reftable_writer *w, uint8_t typ)
30 {
31 switch (typ) {
32 case 'r':
33 return &w->stats.ref_stats;
34 case 'o':
35 return &w->stats.obj_stats;
36 case 'i':
37 return &w->stats.idx_stats;
38 case 'g':
39 return &w->stats.log_stats;
40 }
41 abort();
42 return NULL;
43 }
44
45 /* write data, queuing the padding for the next write. Returns negative for
46 * error. */
47 static int padded_write(struct reftable_writer *w, uint8_t *data, size_t len,
48 int padding)
49 {
50 int n = 0;
51 if (w->pending_padding > 0) {
52 uint8_t *zeroed = reftable_calloc(w->pending_padding);
53 int n = w->write(w->write_arg, zeroed, w->pending_padding);
54 if (n < 0)
55 return n;
56
57 w->pending_padding = 0;
58 reftable_free(zeroed);
59 }
60
61 w->pending_padding = padding;
62 n = w->write(w->write_arg, data, len);
63 if (n < 0)
64 return n;
65 n += padding;
66 return 0;
67 }
68
69 static void options_set_defaults(struct reftable_write_options *opts)
70 {
71 if (opts->restart_interval == 0) {
72 opts->restart_interval = 16;
73 }
74
75 if (opts->hash_id == 0) {
76 opts->hash_id = GIT_SHA1_FORMAT_ID;
77 }
78 if (opts->block_size == 0) {
79 opts->block_size = DEFAULT_BLOCK_SIZE;
80 }
81 }
82
83 static int writer_version(struct reftable_writer *w)
84 {
85 return (w->opts.hash_id == 0 || w->opts.hash_id == GIT_SHA1_FORMAT_ID) ?
86 1 :
87 2;
88 }
89
90 static int writer_write_header(struct reftable_writer *w, uint8_t *dest)
91 {
92 memcpy(dest, "REFT", 4);
93
94 dest[4] = writer_version(w);
95
96 put_be24(dest + 5, w->opts.block_size);
97 put_be64(dest + 8, w->min_update_index);
98 put_be64(dest + 16, w->max_update_index);
99 if (writer_version(w) == 2) {
100 put_be32(dest + 24, w->opts.hash_id);
101 }
102 return header_size(writer_version(w));
103 }
104
105 static void writer_reinit_block_writer(struct reftable_writer *w, uint8_t typ)
106 {
107 int block_start = 0;
108 if (w->next == 0) {
109 block_start = header_size(writer_version(w));
110 }
111
112 strbuf_release(&w->last_key);
113 block_writer_init(&w->block_writer_data, typ, w->block,
114 w->opts.block_size, block_start,
115 hash_size(w->opts.hash_id));
116 w->block_writer = &w->block_writer_data;
117 w->block_writer->restart_interval = w->opts.restart_interval;
118 }
119
120 static struct strbuf reftable_empty_strbuf = STRBUF_INIT;
121
122 struct reftable_writer *
123 reftable_new_writer(ssize_t (*writer_func)(void *, const void *, size_t),
124 void *writer_arg, struct reftable_write_options *opts)
125 {
126 struct reftable_writer *wp =
127 reftable_calloc(sizeof(struct reftable_writer));
128 strbuf_init(&wp->block_writer_data.last_key, 0);
129 options_set_defaults(opts);
130 if (opts->block_size >= (1 << 24)) {
131 /* TODO - error return? */
132 abort();
133 }
134 wp->last_key = reftable_empty_strbuf;
135 wp->block = reftable_calloc(opts->block_size);
136 wp->write = writer_func;
137 wp->write_arg = writer_arg;
138 wp->opts = *opts;
139 writer_reinit_block_writer(wp, BLOCK_TYPE_REF);
140
141 return wp;
142 }
143
144 void reftable_writer_set_limits(struct reftable_writer *w, uint64_t min,
145 uint64_t max)
146 {
147 w->min_update_index = min;
148 w->max_update_index = max;
149 }
150
151 void reftable_writer_free(struct reftable_writer *w)
152 {
153 reftable_free(w->block);
154 reftable_free(w);
155 }
156
157 struct obj_index_tree_node {
158 struct strbuf hash;
159 uint64_t *offsets;
160 size_t offset_len;
161 size_t offset_cap;
162 };
163
164 #define OBJ_INDEX_TREE_NODE_INIT \
165 { \
166 .hash = STRBUF_INIT \
167 }
168
169 static int obj_index_tree_node_compare(const void *a, const void *b)
170 {
171 return strbuf_cmp(&((const struct obj_index_tree_node *)a)->hash,
172 &((const struct obj_index_tree_node *)b)->hash);
173 }
174
175 static void writer_index_hash(struct reftable_writer *w, struct strbuf *hash)
176 {
177 uint64_t off = w->next;
178
179 struct obj_index_tree_node want = { .hash = *hash };
180
181 struct tree_node *node = tree_search(&want, &w->obj_index_tree,
182 &obj_index_tree_node_compare, 0);
183 struct obj_index_tree_node *key = NULL;
184 if (node == NULL) {
185 struct obj_index_tree_node empty = OBJ_INDEX_TREE_NODE_INIT;
186 key = reftable_malloc(sizeof(struct obj_index_tree_node));
187 *key = empty;
188
189 strbuf_reset(&key->hash);
190 strbuf_addbuf(&key->hash, hash);
191 tree_search((void *)key, &w->obj_index_tree,
192 &obj_index_tree_node_compare, 1);
193 } else {
194 key = node->key;
195 }
196
197 if (key->offset_len > 0 && key->offsets[key->offset_len - 1] == off) {
198 return;
199 }
200
201 if (key->offset_len == key->offset_cap) {
202 key->offset_cap = 2 * key->offset_cap + 1;
203 key->offsets = reftable_realloc(
204 key->offsets, sizeof(uint64_t) * key->offset_cap);
205 }
206
207 key->offsets[key->offset_len++] = off;
208 }
209
210 static int writer_add_record(struct reftable_writer *w,
211 struct reftable_record *rec)
212 {
213 struct strbuf key = STRBUF_INIT;
214 int err = -1;
215 reftable_record_key(rec, &key);
216 if (strbuf_cmp(&w->last_key, &key) >= 0) {
217 err = REFTABLE_API_ERROR;
218 goto done;
219 }
220
221 strbuf_reset(&w->last_key);
222 strbuf_addbuf(&w->last_key, &key);
223 if (w->block_writer == NULL) {
224 writer_reinit_block_writer(w, reftable_record_type(rec));
225 }
226
227 assert(block_writer_type(w->block_writer) == reftable_record_type(rec));
228
229 if (block_writer_add(w->block_writer, rec) == 0) {
230 err = 0;
231 goto done;
232 }
233
234 err = writer_flush_block(w);
235 if (err < 0) {
236 goto done;
237 }
238
239 writer_reinit_block_writer(w, reftable_record_type(rec));
240 err = block_writer_add(w->block_writer, rec);
241 if (err < 0) {
242 /* we are writing into memory, so an error can only mean it
243 * doesn't fit. */
244 err = REFTABLE_ENTRY_TOO_BIG_ERROR;
245 goto done;
246 }
247
248 err = 0;
249 done:
250 strbuf_release(&key);
251 return err;
252 }
253
254 int reftable_writer_add_ref(struct reftable_writer *w,
255 struct reftable_ref_record *ref)
256 {
257 struct reftable_record rec = { NULL };
258 struct reftable_ref_record copy = *ref;
259 int err = 0;
260
261 if (ref->refname == NULL)
262 return REFTABLE_API_ERROR;
263 if (ref->update_index < w->min_update_index ||
264 ref->update_index > w->max_update_index)
265 return REFTABLE_API_ERROR;
266
267 reftable_record_from_ref(&rec, &copy);
268 copy.update_index -= w->min_update_index;
269
270 err = writer_add_record(w, &rec);
271 if (err < 0)
272 return err;
273
274 if (!w->opts.skip_index_objects && reftable_ref_record_val1(ref)) {
275 struct strbuf h = STRBUF_INIT;
276 strbuf_add(&h, (char *)reftable_ref_record_val1(ref),
277 hash_size(w->opts.hash_id));
278 writer_index_hash(w, &h);
279 strbuf_release(&h);
280 }
281
282 if (!w->opts.skip_index_objects && reftable_ref_record_val2(ref)) {
283 struct strbuf h = STRBUF_INIT;
284 strbuf_add(&h, reftable_ref_record_val2(ref),
285 hash_size(w->opts.hash_id));
286 writer_index_hash(w, &h);
287 strbuf_release(&h);
288 }
289 return 0;
290 }
291
292 int reftable_writer_add_refs(struct reftable_writer *w,
293 struct reftable_ref_record *refs, int n)
294 {
295 int err = 0;
296 int i = 0;
297 QSORT(refs, n, reftable_ref_record_compare_name);
298 for (i = 0; err == 0 && i < n; i++) {
299 err = reftable_writer_add_ref(w, &refs[i]);
300 }
301 return err;
302 }
303
304 static int reftable_writer_add_log_verbatim(struct reftable_writer *w,
305 struct reftable_log_record *log)
306 {
307 struct reftable_record rec = { NULL };
308 if (w->block_writer &&
309 block_writer_type(w->block_writer) == BLOCK_TYPE_REF) {
310 int err = writer_finish_public_section(w);
311 if (err < 0)
312 return err;
313 }
314
315 w->next -= w->pending_padding;
316 w->pending_padding = 0;
317
318 reftable_record_from_log(&rec, log);
319 return writer_add_record(w, &rec);
320 }
321
322 int reftable_writer_add_log(struct reftable_writer *w,
323 struct reftable_log_record *log)
324 {
325 char *input_log_message = NULL;
326 struct strbuf cleaned_message = STRBUF_INIT;
327 int err = 0;
328
329 if (log->value_type == REFTABLE_LOG_DELETION)
330 return reftable_writer_add_log_verbatim(w, log);
331
332 if (log->refname == NULL)
333 return REFTABLE_API_ERROR;
334
335 input_log_message = log->value.update.message;
336 if (!w->opts.exact_log_message && log->value.update.message) {
337 strbuf_addstr(&cleaned_message, log->value.update.message);
338 while (cleaned_message.len &&
339 cleaned_message.buf[cleaned_message.len - 1] == '\n')
340 strbuf_setlen(&cleaned_message,
341 cleaned_message.len - 1);
342 if (strchr(cleaned_message.buf, '\n')) {
343 /* multiple lines not allowed. */
344 err = REFTABLE_API_ERROR;
345 goto done;
346 }
347 strbuf_addstr(&cleaned_message, "\n");
348 log->value.update.message = cleaned_message.buf;
349 }
350
351 err = reftable_writer_add_log_verbatim(w, log);
352 log->value.update.message = input_log_message;
353 done:
354 strbuf_release(&cleaned_message);
355 return err;
356 }
357
358 int reftable_writer_add_logs(struct reftable_writer *w,
359 struct reftable_log_record *logs, int n)
360 {
361 int err = 0;
362 int i = 0;
363 QSORT(logs, n, reftable_log_record_compare_key);
364
365 for (i = 0; err == 0 && i < n; i++) {
366 err = reftable_writer_add_log(w, &logs[i]);
367 }
368 return err;
369 }
370
371 static int writer_finish_section(struct reftable_writer *w)
372 {
373 uint8_t typ = block_writer_type(w->block_writer);
374 uint64_t index_start = 0;
375 int max_level = 0;
376 int threshold = w->opts.unpadded ? 1 : 3;
377 int before_blocks = w->stats.idx_stats.blocks;
378 int err = writer_flush_block(w);
379 int i = 0;
380 struct reftable_block_stats *bstats = NULL;
381 if (err < 0)
382 return err;
383
384 while (w->index_len > threshold) {
385 struct reftable_index_record *idx = NULL;
386 int idx_len = 0;
387
388 max_level++;
389 index_start = w->next;
390 writer_reinit_block_writer(w, BLOCK_TYPE_INDEX);
391
392 idx = w->index;
393 idx_len = w->index_len;
394
395 w->index = NULL;
396 w->index_len = 0;
397 w->index_cap = 0;
398 for (i = 0; i < idx_len; i++) {
399 struct reftable_record rec = { NULL };
400 reftable_record_from_index(&rec, idx + i);
401 if (block_writer_add(w->block_writer, &rec) == 0) {
402 continue;
403 }
404
405 err = writer_flush_block(w);
406 if (err < 0)
407 return err;
408
409 writer_reinit_block_writer(w, BLOCK_TYPE_INDEX);
410
411 err = block_writer_add(w->block_writer, &rec);
412 if (err != 0) {
413 /* write into fresh block should always succeed
414 */
415 abort();
416 }
417 }
418 for (i = 0; i < idx_len; i++) {
419 strbuf_release(&idx[i].last_key);
420 }
421 reftable_free(idx);
422 }
423
424 writer_clear_index(w);
425
426 err = writer_flush_block(w);
427 if (err < 0)
428 return err;
429
430 bstats = writer_reftable_block_stats(w, typ);
431 bstats->index_blocks = w->stats.idx_stats.blocks - before_blocks;
432 bstats->index_offset = index_start;
433 bstats->max_index_level = max_level;
434
435 /* Reinit lastKey, as the next section can start with any key. */
436 w->last_key.len = 0;
437
438 return 0;
439 }
440
441 struct common_prefix_arg {
442 struct strbuf *last;
443 int max;
444 };
445
446 static void update_common(void *void_arg, void *key)
447 {
448 struct common_prefix_arg *arg = void_arg;
449 struct obj_index_tree_node *entry = key;
450 if (arg->last) {
451 int n = common_prefix_size(&entry->hash, arg->last);
452 if (n > arg->max) {
453 arg->max = n;
454 }
455 }
456 arg->last = &entry->hash;
457 }
458
459 struct write_record_arg {
460 struct reftable_writer *w;
461 int err;
462 };
463
464 static void write_object_record(void *void_arg, void *key)
465 {
466 struct write_record_arg *arg = void_arg;
467 struct obj_index_tree_node *entry = key;
468 struct reftable_obj_record obj_rec = {
469 .hash_prefix = (uint8_t *)entry->hash.buf,
470 .hash_prefix_len = arg->w->stats.object_id_len,
471 .offsets = entry->offsets,
472 .offset_len = entry->offset_len,
473 };
474 struct reftable_record rec = { NULL };
475 if (arg->err < 0)
476 goto done;
477
478 reftable_record_from_obj(&rec, &obj_rec);
479 arg->err = block_writer_add(arg->w->block_writer, &rec);
480 if (arg->err == 0)
481 goto done;
482
483 arg->err = writer_flush_block(arg->w);
484 if (arg->err < 0)
485 goto done;
486
487 writer_reinit_block_writer(arg->w, BLOCK_TYPE_OBJ);
488 arg->err = block_writer_add(arg->w->block_writer, &rec);
489 if (arg->err == 0)
490 goto done;
491 obj_rec.offset_len = 0;
492 arg->err = block_writer_add(arg->w->block_writer, &rec);
493
494 /* Should be able to write into a fresh block. */
495 assert(arg->err == 0);
496
497 done:;
498 }
499
500 static void object_record_free(void *void_arg, void *key)
501 {
502 struct obj_index_tree_node *entry = key;
503
504 FREE_AND_NULL(entry->offsets);
505 strbuf_release(&entry->hash);
506 reftable_free(entry);
507 }
508
509 static int writer_dump_object_index(struct reftable_writer *w)
510 {
511 struct write_record_arg closure = { .w = w };
512 struct common_prefix_arg common = { NULL };
513 if (w->obj_index_tree) {
514 infix_walk(w->obj_index_tree, &update_common, &common);
515 }
516 w->stats.object_id_len = common.max + 1;
517
518 writer_reinit_block_writer(w, BLOCK_TYPE_OBJ);
519
520 if (w->obj_index_tree) {
521 infix_walk(w->obj_index_tree, &write_object_record, &closure);
522 }
523
524 if (closure.err < 0)
525 return closure.err;
526 return writer_finish_section(w);
527 }
528
529 static int writer_finish_public_section(struct reftable_writer *w)
530 {
531 uint8_t typ = 0;
532 int err = 0;
533
534 if (w->block_writer == NULL)
535 return 0;
536
537 typ = block_writer_type(w->block_writer);
538 err = writer_finish_section(w);
539 if (err < 0)
540 return err;
541 if (typ == BLOCK_TYPE_REF && !w->opts.skip_index_objects &&
542 w->stats.ref_stats.index_blocks > 0) {
543 err = writer_dump_object_index(w);
544 if (err < 0)
545 return err;
546 }
547
548 if (w->obj_index_tree) {
549 infix_walk(w->obj_index_tree, &object_record_free, NULL);
550 tree_free(w->obj_index_tree);
551 w->obj_index_tree = NULL;
552 }
553
554 w->block_writer = NULL;
555 return 0;
556 }
557
558 int reftable_writer_close(struct reftable_writer *w)
559 {
560 uint8_t footer[72];
561 uint8_t *p = footer;
562 int err = writer_finish_public_section(w);
563 int empty_table = w->next == 0;
564 if (err != 0)
565 goto done;
566 w->pending_padding = 0;
567 if (empty_table) {
568 /* Empty tables need a header anyway. */
569 uint8_t header[28];
570 int n = writer_write_header(w, header);
571 err = padded_write(w, header, n, 0);
572 if (err < 0)
573 goto done;
574 }
575
576 p += writer_write_header(w, footer);
577 put_be64(p, w->stats.ref_stats.index_offset);
578 p += 8;
579 put_be64(p, (w->stats.obj_stats.offset) << 5 | w->stats.object_id_len);
580 p += 8;
581 put_be64(p, w->stats.obj_stats.index_offset);
582 p += 8;
583
584 put_be64(p, w->stats.log_stats.offset);
585 p += 8;
586 put_be64(p, w->stats.log_stats.index_offset);
587 p += 8;
588
589 put_be32(p, crc32(0, footer, p - footer));
590 p += 4;
591
592 err = padded_write(w, footer, footer_size(writer_version(w)), 0);
593 if (err < 0)
594 goto done;
595
596 if (empty_table) {
597 err = REFTABLE_EMPTY_TABLE_ERROR;
598 goto done;
599 }
600
601 done:
602 /* free up memory. */
603 block_writer_release(&w->block_writer_data);
604 writer_clear_index(w);
605 strbuf_release(&w->last_key);
606 return err;
607 }
608
609 static void writer_clear_index(struct reftable_writer *w)
610 {
611 int i = 0;
612 for (i = 0; i < w->index_len; i++) {
613 strbuf_release(&w->index[i].last_key);
614 }
615
616 FREE_AND_NULL(w->index);
617 w->index_len = 0;
618 w->index_cap = 0;
619 }
620
621 static const int debug = 0;
622
623 static int writer_flush_nonempty_block(struct reftable_writer *w)
624 {
625 uint8_t typ = block_writer_type(w->block_writer);
626 struct reftable_block_stats *bstats =
627 writer_reftable_block_stats(w, typ);
628 uint64_t block_typ_off = (bstats->blocks == 0) ? w->next : 0;
629 int raw_bytes = block_writer_finish(w->block_writer);
630 int padding = 0;
631 int err = 0;
632 struct reftable_index_record ir = { .last_key = STRBUF_INIT };
633 if (raw_bytes < 0)
634 return raw_bytes;
635
636 if (!w->opts.unpadded && typ != BLOCK_TYPE_LOG) {
637 padding = w->opts.block_size - raw_bytes;
638 }
639
640 if (block_typ_off > 0) {
641 bstats->offset = block_typ_off;
642 }
643
644 bstats->entries += w->block_writer->entries;
645 bstats->restarts += w->block_writer->restart_len;
646 bstats->blocks++;
647 w->stats.blocks++;
648
649 if (debug) {
650 fprintf(stderr, "block %c off %" PRIu64 " sz %d (%d)\n", typ,
651 w->next, raw_bytes,
652 get_be24(w->block + w->block_writer->header_off + 1));
653 }
654
655 if (w->next == 0) {
656 writer_write_header(w, w->block);
657 }
658
659 err = padded_write(w, w->block, raw_bytes, padding);
660 if (err < 0)
661 return err;
662
663 if (w->index_cap == w->index_len) {
664 w->index_cap = 2 * w->index_cap + 1;
665 w->index = reftable_realloc(
666 w->index,
667 sizeof(struct reftable_index_record) * w->index_cap);
668 }
669
670 ir.offset = w->next;
671 strbuf_reset(&ir.last_key);
672 strbuf_addbuf(&ir.last_key, &w->block_writer->last_key);
673 w->index[w->index_len] = ir;
674
675 w->index_len++;
676 w->next += padding + raw_bytes;
677 w->block_writer = NULL;
678 return 0;
679 }
680
681 static int writer_flush_block(struct reftable_writer *w)
682 {
683 if (w->block_writer == NULL)
684 return 0;
685 if (w->block_writer->entries == 0)
686 return 0;
687 return writer_flush_nonempty_block(w);
688 }
689
690 const struct reftable_stats *writer_stats(struct reftable_writer *w)
691 {
692 return &w->stats;
693 }