]> git.proxmox.com Git - libgit2.git/blob - src/indexer.c
Merge pull request #3325 from libgit2/cmn/filebuf-rename-error
[libgit2.git] / src / indexer.c
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7
8 #include "git2/indexer.h"
9 #include "git2/object.h"
10
11 #include "common.h"
12 #include "pack.h"
13 #include "mwindow.h"
14 #include "posix.h"
15 #include "pack.h"
16 #include "filebuf.h"
17 #include "oid.h"
18 #include "oidmap.h"
19 #include "zstream.h"
20
21 GIT__USE_OIDMAP
22
23 extern git_mutex git__mwindow_mutex;
24
25 #define UINT31_MAX (0x7FFFFFFF)
26
27 struct entry {
28 git_oid oid;
29 uint32_t crc;
30 uint32_t offset;
31 uint64_t offset_long;
32 };
33
34 struct git_indexer {
35 unsigned int parsed_header :1,
36 opened_pack :1,
37 have_stream :1,
38 have_delta :1;
39 struct git_pack_header hdr;
40 struct git_pack_file *pack;
41 unsigned int mode;
42 git_off_t off;
43 git_off_t entry_start;
44 git_packfile_stream stream;
45 size_t nr_objects;
46 git_vector objects;
47 git_vector deltas;
48 unsigned int fanout[256];
49 git_hash_ctx hash_ctx;
50 git_oid hash;
51 git_transfer_progress_cb progress_cb;
52 void *progress_payload;
53 char objbuf[8*1024];
54
55 /* Needed to look up objects which we want to inject to fix a thin pack */
56 git_odb *odb;
57
58 /* Fields for calculating the packfile trailer (hash of everything before it) */
59 char inbuf[GIT_OID_RAWSZ];
60 size_t inbuf_len;
61 git_hash_ctx trailer;
62 };
63
64 struct delta_info {
65 git_off_t delta_off;
66 };
67
68 const git_oid *git_indexer_hash(const git_indexer *idx)
69 {
70 return &idx->hash;
71 }
72
73 static int parse_header(struct git_pack_header *hdr, struct git_pack_file *pack)
74 {
75 int error;
76 git_map map;
77
78 if ((error = p_mmap(&map, sizeof(*hdr), GIT_PROT_READ, GIT_MAP_SHARED, pack->mwf.fd, 0)) < 0)
79 return error;
80
81 memcpy(hdr, map.data, sizeof(*hdr));
82 p_munmap(&map);
83
84 /* Verify we recognize this pack file format. */
85 if (hdr->hdr_signature != ntohl(PACK_SIGNATURE)) {
86 giterr_set(GITERR_INDEXER, "Wrong pack signature");
87 return -1;
88 }
89
90 if (!pack_version_ok(hdr->hdr_version)) {
91 giterr_set(GITERR_INDEXER, "Wrong pack version");
92 return -1;
93 }
94
95 return 0;
96 }
97
98 static int objects_cmp(const void *a, const void *b)
99 {
100 const struct entry *entrya = a;
101 const struct entry *entryb = b;
102
103 return git_oid__cmp(&entrya->oid, &entryb->oid);
104 }
105
106 int git_indexer_new(
107 git_indexer **out,
108 const char *prefix,
109 unsigned int mode,
110 git_odb *odb,
111 git_transfer_progress_cb progress_cb,
112 void *progress_payload)
113 {
114 git_indexer *idx;
115 git_buf path = GIT_BUF_INIT, tmp_path = GIT_BUF_INIT;
116 static const char suff[] = "/pack";
117 int error, fd = -1;
118
119 idx = git__calloc(1, sizeof(git_indexer));
120 GITERR_CHECK_ALLOC(idx);
121 idx->odb = odb;
122 idx->progress_cb = progress_cb;
123 idx->progress_payload = progress_payload;
124 idx->mode = mode ? mode : GIT_PACK_FILE_MODE;
125 git_hash_ctx_init(&idx->hash_ctx);
126 git_hash_ctx_init(&idx->trailer);
127
128 error = git_buf_joinpath(&path, prefix, suff);
129 if (error < 0)
130 goto cleanup;
131
132 fd = git_futils_mktmp(&tmp_path, git_buf_cstr(&path), idx->mode);
133 git_buf_free(&path);
134 if (fd < 0)
135 goto cleanup;
136
137 error = git_packfile_alloc(&idx->pack, git_buf_cstr(&tmp_path));
138 git_buf_free(&tmp_path);
139
140 if (error < 0)
141 goto cleanup;
142
143 idx->pack->mwf.fd = fd;
144 if ((error = git_mwindow_file_register(&idx->pack->mwf)) < 0)
145 goto cleanup;
146
147 *out = idx;
148 return 0;
149
150 cleanup:
151 if (fd != -1)
152 p_close(fd);
153
154 git_buf_free(&path);
155 git_buf_free(&tmp_path);
156 git__free(idx);
157 return -1;
158 }
159
160 /* Try to store the delta so we can try to resolve it later */
161 static int store_delta(git_indexer *idx)
162 {
163 struct delta_info *delta;
164
165 delta = git__calloc(1, sizeof(struct delta_info));
166 GITERR_CHECK_ALLOC(delta);
167 delta->delta_off = idx->entry_start;
168
169 if (git_vector_insert(&idx->deltas, delta) < 0)
170 return -1;
171
172 return 0;
173 }
174
175 static void hash_header(git_hash_ctx *ctx, git_off_t len, git_otype type)
176 {
177 char buffer[64];
178 size_t hdrlen;
179
180 hdrlen = git_odb__format_object_header(buffer, sizeof(buffer), (size_t)len, type);
181 git_hash_update(ctx, buffer, hdrlen);
182 }
183
184 static int hash_object_stream(git_indexer*idx, git_packfile_stream *stream)
185 {
186 ssize_t read;
187
188 assert(idx && stream);
189
190 do {
191 if ((read = git_packfile_stream_read(stream, idx->objbuf, sizeof(idx->objbuf))) < 0)
192 break;
193
194 git_hash_update(&idx->hash_ctx, idx->objbuf, read);
195 } while (read > 0);
196
197 if (read < 0)
198 return (int)read;
199
200 return 0;
201 }
202
203 /* In order to create the packfile stream, we need to skip over the delta base description */
204 static int advance_delta_offset(git_indexer *idx, git_otype type)
205 {
206 git_mwindow *w = NULL;
207
208 assert(type == GIT_OBJ_REF_DELTA || type == GIT_OBJ_OFS_DELTA);
209
210 if (type == GIT_OBJ_REF_DELTA) {
211 idx->off += GIT_OID_RAWSZ;
212 } else {
213 git_off_t base_off = get_delta_base(idx->pack, &w, &idx->off, type, idx->entry_start);
214 git_mwindow_close(&w);
215 if (base_off < 0)
216 return (int)base_off;
217 }
218
219 return 0;
220 }
221
222 /* Read from the stream and discard any output */
223 static int read_object_stream(git_indexer *idx, git_packfile_stream *stream)
224 {
225 ssize_t read;
226
227 assert(stream);
228
229 do {
230 read = git_packfile_stream_read(stream, idx->objbuf, sizeof(idx->objbuf));
231 } while (read > 0);
232
233 if (read < 0)
234 return (int)read;
235
236 return 0;
237 }
238
239 static int crc_object(uint32_t *crc_out, git_mwindow_file *mwf, git_off_t start, git_off_t size)
240 {
241 void *ptr;
242 uint32_t crc;
243 unsigned int left, len;
244 git_mwindow *w = NULL;
245
246 crc = crc32(0L, Z_NULL, 0);
247 while (size) {
248 ptr = git_mwindow_open(mwf, &w, start, (size_t)size, &left);
249 if (ptr == NULL)
250 return -1;
251
252 len = min(left, (unsigned int)size);
253 crc = crc32(crc, ptr, len);
254 size -= len;
255 start += len;
256 git_mwindow_close(&w);
257 }
258
259 *crc_out = htonl(crc);
260 return 0;
261 }
262
263 static int store_object(git_indexer *idx)
264 {
265 int i, error;
266 khiter_t k;
267 git_oid oid;
268 struct entry *entry;
269 git_off_t entry_size;
270 struct git_pack_entry *pentry;
271 git_off_t entry_start = idx->entry_start;
272
273 entry = git__calloc(1, sizeof(*entry));
274 GITERR_CHECK_ALLOC(entry);
275
276 pentry = git__calloc(1, sizeof(struct git_pack_entry));
277 GITERR_CHECK_ALLOC(pentry);
278
279 git_hash_final(&oid, &idx->hash_ctx);
280 entry_size = idx->off - entry_start;
281 if (entry_start > UINT31_MAX) {
282 entry->offset = UINT32_MAX;
283 entry->offset_long = entry_start;
284 } else {
285 entry->offset = (uint32_t)entry_start;
286 }
287
288 git_oid_cpy(&pentry->sha1, &oid);
289 pentry->offset = entry_start;
290
291 k = kh_put(oid, idx->pack->idx_cache, &pentry->sha1, &error);
292 if (error == -1) {
293 git__free(pentry);
294 giterr_set_oom();
295 goto on_error;
296 }
297
298 if (error == 0) {
299 giterr_set(GITERR_INDEXER, "duplicate object %s found in pack", git_oid_tostr_s(&pentry->sha1));
300 git__free(pentry);
301 goto on_error;
302 }
303
304
305 kh_value(idx->pack->idx_cache, k) = pentry;
306
307 git_oid_cpy(&entry->oid, &oid);
308
309 if (crc_object(&entry->crc, &idx->pack->mwf, entry_start, entry_size) < 0)
310 goto on_error;
311
312 /* Add the object to the list */
313 if (git_vector_insert(&idx->objects, entry) < 0)
314 goto on_error;
315
316 for (i = oid.id[0]; i < 256; ++i) {
317 idx->fanout[i]++;
318 }
319
320 return 0;
321
322 on_error:
323 git__free(entry);
324
325 return -1;
326 }
327
328 GIT_INLINE(bool) has_entry(git_indexer *idx, git_oid *id)
329 {
330 khiter_t k;
331 k = kh_get(oid, idx->pack->idx_cache, id);
332 return (k != kh_end(idx->pack->idx_cache));
333 }
334
335 static int save_entry(git_indexer *idx, struct entry *entry, struct git_pack_entry *pentry, git_off_t entry_start)
336 {
337 int i, error;
338 khiter_t k;
339
340 if (entry_start > UINT31_MAX) {
341 entry->offset = UINT32_MAX;
342 entry->offset_long = entry_start;
343 } else {
344 entry->offset = (uint32_t)entry_start;
345 }
346
347 pentry->offset = entry_start;
348 k = kh_put(oid, idx->pack->idx_cache, &pentry->sha1, &error);
349
350 if (error <= 0) {
351 giterr_set(GITERR_INDEXER, "cannot insert object into pack");
352 return -1;
353 }
354
355 kh_value(idx->pack->idx_cache, k) = pentry;
356
357 /* Add the object to the list */
358 if (git_vector_insert(&idx->objects, entry) < 0)
359 return -1;
360
361 for (i = entry->oid.id[0]; i < 256; ++i) {
362 idx->fanout[i]++;
363 }
364
365 return 0;
366 }
367
368 static int hash_and_save(git_indexer *idx, git_rawobj *obj, git_off_t entry_start)
369 {
370 git_oid oid;
371 size_t entry_size;
372 struct entry *entry;
373 struct git_pack_entry *pentry = NULL;
374
375 entry = git__calloc(1, sizeof(*entry));
376 GITERR_CHECK_ALLOC(entry);
377
378 if (git_odb__hashobj(&oid, obj) < 0) {
379 giterr_set(GITERR_INDEXER, "Failed to hash object");
380 goto on_error;
381 }
382
383 pentry = git__calloc(1, sizeof(struct git_pack_entry));
384 GITERR_CHECK_ALLOC(pentry);
385
386 git_oid_cpy(&pentry->sha1, &oid);
387 git_oid_cpy(&entry->oid, &oid);
388 entry->crc = crc32(0L, Z_NULL, 0);
389
390 entry_size = (size_t)(idx->off - entry_start);
391 if (crc_object(&entry->crc, &idx->pack->mwf, entry_start, entry_size) < 0)
392 goto on_error;
393
394 return save_entry(idx, entry, pentry, entry_start);
395
396 on_error:
397 git__free(pentry);
398 git__free(entry);
399 git__free(obj->data);
400 return -1;
401 }
402
403 static int do_progress_callback(git_indexer *idx, git_transfer_progress *stats)
404 {
405 if (idx->progress_cb)
406 return giterr_set_after_callback_function(
407 idx->progress_cb(stats, idx->progress_payload),
408 "indexer progress");
409 return 0;
410 }
411
412 /* Hash everything but the last 20B of input */
413 static void hash_partially(git_indexer *idx, const uint8_t *data, size_t size)
414 {
415 size_t to_expell, to_keep;
416
417 if (size == 0)
418 return;
419
420 /* Easy case, dump the buffer and the data minus the last 20 bytes */
421 if (size >= GIT_OID_RAWSZ) {
422 git_hash_update(&idx->trailer, idx->inbuf, idx->inbuf_len);
423 git_hash_update(&idx->trailer, data, size - GIT_OID_RAWSZ);
424
425 data += size - GIT_OID_RAWSZ;
426 memcpy(idx->inbuf, data, GIT_OID_RAWSZ);
427 idx->inbuf_len = GIT_OID_RAWSZ;
428 return;
429 }
430
431 /* We can just append */
432 if (idx->inbuf_len + size <= GIT_OID_RAWSZ) {
433 memcpy(idx->inbuf + idx->inbuf_len, data, size);
434 idx->inbuf_len += size;
435 return;
436 }
437
438 /* We need to partially drain the buffer and then append */
439 to_keep = GIT_OID_RAWSZ - size;
440 to_expell = idx->inbuf_len - to_keep;
441
442 git_hash_update(&idx->trailer, idx->inbuf, to_expell);
443
444 memmove(idx->inbuf, idx->inbuf + to_expell, to_keep);
445 memcpy(idx->inbuf + to_keep, data, size);
446 idx->inbuf_len += size - to_expell;
447 }
448
449 static int write_at(git_indexer *idx, const void *data, git_off_t offset, size_t size)
450 {
451 git_file fd = idx->pack->mwf.fd;
452 size_t page_size;
453 size_t page_offset;
454 git_off_t page_start;
455 unsigned char *map_data;
456 git_map map;
457 int error;
458
459 assert(data && size);
460
461 if ((error = git__page_size(&page_size)) < 0)
462 return error;
463
464 /* the offset needs to be at the beginning of the a page boundary */
465 page_offset = offset % page_size;
466 page_start = offset - page_offset;
467
468 if ((error = p_mmap(&map, page_offset + size, GIT_PROT_WRITE, GIT_MAP_SHARED, fd, page_start)) < 0)
469 return error;
470
471 map_data = (unsigned char *)map.data;
472 memcpy(map_data + page_offset, data, size);
473 p_munmap(&map);
474
475 return 0;
476 }
477
478 static int append_to_pack(git_indexer *idx, const void *data, size_t size)
479 {
480 git_off_t current_size = idx->pack->mwf.size;
481 int fd = idx->pack->mwf.fd;
482
483 if (!size)
484 return 0;
485
486 if (p_lseek(fd, current_size + size - 1, SEEK_SET) < 0 ||
487 p_write(idx->pack->mwf.fd, data, 1) < 0) {
488 giterr_set(GITERR_OS, "cannot extend packfile '%s'", idx->pack->pack_name);
489 return -1;
490 }
491
492 return write_at(idx, data, idx->pack->mwf.size, size);
493 }
494
495 int git_indexer_append(git_indexer *idx, const void *data, size_t size, git_transfer_progress *stats)
496 {
497 int error = -1;
498 size_t processed;
499 struct git_pack_header *hdr = &idx->hdr;
500 git_mwindow_file *mwf = &idx->pack->mwf;
501
502 assert(idx && data && stats);
503
504 processed = stats->indexed_objects;
505
506 if ((error = append_to_pack(idx, data, size)) < 0)
507 return error;
508
509 hash_partially(idx, data, (int)size);
510
511 /* Make sure we set the new size of the pack */
512 idx->pack->mwf.size += size;
513
514 if (!idx->parsed_header) {
515 unsigned int total_objects;
516
517 if ((unsigned)idx->pack->mwf.size < sizeof(struct git_pack_header))
518 return 0;
519
520 if ((error = parse_header(&idx->hdr, idx->pack)) < 0)
521 return error;
522
523 idx->parsed_header = 1;
524 idx->nr_objects = ntohl(hdr->hdr_entries);
525 idx->off = sizeof(struct git_pack_header);
526
527 /* for now, limit to 2^32 objects */
528 assert(idx->nr_objects == (size_t)((unsigned int)idx->nr_objects));
529 if (idx->nr_objects == (size_t)((unsigned int)idx->nr_objects))
530 total_objects = (unsigned int)idx->nr_objects;
531 else
532 total_objects = UINT_MAX;
533
534 idx->pack->idx_cache = git_oidmap_alloc();
535 GITERR_CHECK_ALLOC(idx->pack->idx_cache);
536
537 idx->pack->has_cache = 1;
538 if (git_vector_init(&idx->objects, total_objects, objects_cmp) < 0)
539 return -1;
540
541 if (git_vector_init(&idx->deltas, total_objects / 2, NULL) < 0)
542 return -1;
543
544 stats->received_objects = 0;
545 stats->local_objects = 0;
546 stats->total_deltas = 0;
547 stats->indexed_deltas = 0;
548 processed = stats->indexed_objects = 0;
549 stats->total_objects = total_objects;
550
551 if ((error = do_progress_callback(idx, stats)) != 0)
552 return error;
553 }
554
555 /* Now that we have data in the pack, let's try to parse it */
556
557 /* As the file grows any windows we try to use will be out of date */
558 git_mwindow_free_all(mwf);
559
560 while (processed < idx->nr_objects) {
561 git_packfile_stream *stream = &idx->stream;
562 git_off_t entry_start = idx->off;
563 size_t entry_size;
564 git_otype type;
565 git_mwindow *w = NULL;
566
567 if (idx->pack->mwf.size <= idx->off + 20)
568 return 0;
569
570 if (!idx->have_stream) {
571 error = git_packfile_unpack_header(&entry_size, &type, mwf, &w, &idx->off);
572 if (error == GIT_EBUFS) {
573 idx->off = entry_start;
574 return 0;
575 }
576 if (error < 0)
577 goto on_error;
578
579 git_mwindow_close(&w);
580 idx->entry_start = entry_start;
581 git_hash_init(&idx->hash_ctx);
582
583 if (type == GIT_OBJ_REF_DELTA || type == GIT_OBJ_OFS_DELTA) {
584 error = advance_delta_offset(idx, type);
585 if (error == GIT_EBUFS) {
586 idx->off = entry_start;
587 return 0;
588 }
589 if (error < 0)
590 goto on_error;
591
592 idx->have_delta = 1;
593 } else {
594 idx->have_delta = 0;
595 hash_header(&idx->hash_ctx, entry_size, type);
596 }
597
598 idx->have_stream = 1;
599
600 error = git_packfile_stream_open(stream, idx->pack, idx->off);
601 if (error < 0)
602 goto on_error;
603 }
604
605 if (idx->have_delta) {
606 error = read_object_stream(idx, stream);
607 } else {
608 error = hash_object_stream(idx, stream);
609 }
610
611 idx->off = stream->curpos;
612 if (error == GIT_EBUFS)
613 return 0;
614
615 /* We want to free the stream reasorces no matter what here */
616 idx->have_stream = 0;
617 git_packfile_stream_free(stream);
618
619 if (error < 0)
620 goto on_error;
621
622 if (idx->have_delta) {
623 error = store_delta(idx);
624 } else {
625 error = store_object(idx);
626 }
627
628 if (error < 0)
629 goto on_error;
630
631 if (!idx->have_delta) {
632 stats->indexed_objects = (unsigned int)++processed;
633 }
634 stats->received_objects++;
635
636 if ((error = do_progress_callback(idx, stats)) != 0)
637 goto on_error;
638 }
639
640 return 0;
641
642 on_error:
643 git_mwindow_free_all(mwf);
644 return error;
645 }
646
647 static int index_path(git_buf *path, git_indexer *idx, const char *suffix)
648 {
649 const char prefix[] = "pack-";
650 size_t slash = (size_t)path->size;
651
652 /* search backwards for '/' */
653 while (slash > 0 && path->ptr[slash - 1] != '/')
654 slash--;
655
656 if (git_buf_grow(path, slash + 1 + strlen(prefix) +
657 GIT_OID_HEXSZ + strlen(suffix) + 1) < 0)
658 return -1;
659
660 git_buf_truncate(path, slash);
661 git_buf_puts(path, prefix);
662 git_oid_fmt(path->ptr + git_buf_len(path), &idx->hash);
663 path->size += GIT_OID_HEXSZ;
664 git_buf_puts(path, suffix);
665
666 return git_buf_oom(path) ? -1 : 0;
667 }
668
669 /**
670 * Rewind the packfile by the trailer, as we might need to fix the
671 * packfile by injecting objects at the tail and must overwrite it.
672 */
673 static void seek_back_trailer(git_indexer *idx)
674 {
675 idx->pack->mwf.size -= GIT_OID_RAWSZ;
676 git_mwindow_free_all(&idx->pack->mwf);
677 }
678
679 static int inject_object(git_indexer *idx, git_oid *id)
680 {
681 git_odb_object *obj;
682 struct entry *entry;
683 struct git_pack_entry *pentry = NULL;
684 git_oid foo = {{0}};
685 unsigned char hdr[64];
686 git_buf buf = GIT_BUF_INIT;
687 git_off_t entry_start;
688 const void *data;
689 size_t len, hdr_len;
690 int error;
691
692 seek_back_trailer(idx);
693 entry_start = idx->pack->mwf.size;
694
695 if (git_odb_read(&obj, idx->odb, id) < 0) {
696 giterr_set(GITERR_INDEXER, "missing delta bases");
697 return -1;
698 }
699
700 data = git_odb_object_data(obj);
701 len = git_odb_object_size(obj);
702
703 entry = git__calloc(1, sizeof(*entry));
704 GITERR_CHECK_ALLOC(entry);
705
706 entry->crc = crc32(0L, Z_NULL, 0);
707
708 /* Write out the object header */
709 hdr_len = git_packfile__object_header(hdr, len, git_odb_object_type(obj));
710 if ((error = append_to_pack(idx, hdr, hdr_len)) < 0)
711 goto cleanup;
712
713 idx->pack->mwf.size += hdr_len;
714 entry->crc = crc32(entry->crc, hdr, (uInt)hdr_len);
715
716 if ((error = git_zstream_deflatebuf(&buf, data, len)) < 0)
717 goto cleanup;
718
719 /* And then the compressed object */
720 if ((error = append_to_pack(idx, buf.ptr, buf.size)) < 0)
721 goto cleanup;
722
723 idx->pack->mwf.size += buf.size;
724 entry->crc = htonl(crc32(entry->crc, (unsigned char *)buf.ptr, (uInt)buf.size));
725 git_buf_free(&buf);
726
727 /* Write a fake trailer so the pack functions play ball */
728
729 if ((error = append_to_pack(idx, &foo, GIT_OID_RAWSZ)) < 0)
730 goto cleanup;
731
732 idx->pack->mwf.size += GIT_OID_RAWSZ;
733
734 pentry = git__calloc(1, sizeof(struct git_pack_entry));
735 GITERR_CHECK_ALLOC(pentry);
736
737 git_oid_cpy(&pentry->sha1, id);
738 git_oid_cpy(&entry->oid, id);
739 idx->off = entry_start + hdr_len + len;
740
741 error = save_entry(idx, entry, pentry, entry_start);
742
743 cleanup:
744 if (error) {
745 git__free(entry);
746 git__free(pentry);
747 }
748
749 git_odb_object_free(obj);
750 return error;
751 }
752
753 static int fix_thin_pack(git_indexer *idx, git_transfer_progress *stats)
754 {
755 int error, found_ref_delta = 0;
756 unsigned int i;
757 struct delta_info *delta;
758 size_t size;
759 git_otype type;
760 git_mwindow *w = NULL;
761 git_off_t curpos = 0;
762 unsigned char *base_info;
763 unsigned int left = 0;
764 git_oid base;
765
766 assert(git_vector_length(&idx->deltas) > 0);
767
768 if (idx->odb == NULL) {
769 giterr_set(GITERR_INDEXER, "cannot fix a thin pack without an ODB");
770 return -1;
771 }
772
773 /* Loop until we find the first REF delta */
774 git_vector_foreach(&idx->deltas, i, delta) {
775 if (!delta)
776 continue;
777
778 curpos = delta->delta_off;
779 error = git_packfile_unpack_header(&size, &type, &idx->pack->mwf, &w, &curpos);
780 git_mwindow_close(&w);
781 if (error < 0)
782 return error;
783
784 if (type == GIT_OBJ_REF_DELTA) {
785 found_ref_delta = 1;
786 break;
787 }
788 }
789
790 if (!found_ref_delta) {
791 giterr_set(GITERR_INDEXER, "no REF_DELTA found, cannot inject object");
792 return -1;
793 }
794
795 /* curpos now points to the base information, which is an OID */
796 base_info = git_mwindow_open(&idx->pack->mwf, &w, curpos, GIT_OID_RAWSZ, &left);
797 if (base_info == NULL) {
798 giterr_set(GITERR_INDEXER, "failed to map delta information");
799 return -1;
800 }
801
802 git_oid_fromraw(&base, base_info);
803 git_mwindow_close(&w);
804
805 if (has_entry(idx, &base))
806 return 0;
807
808 if (inject_object(idx, &base) < 0)
809 return -1;
810
811 stats->local_objects++;
812
813 return 0;
814 }
815
816 static int resolve_deltas(git_indexer *idx, git_transfer_progress *stats)
817 {
818 unsigned int i;
819 struct delta_info *delta;
820 int progressed = 0, non_null = 0, progress_cb_result;
821
822 while (idx->deltas.length > 0) {
823 progressed = 0;
824 non_null = 0;
825 git_vector_foreach(&idx->deltas, i, delta) {
826 git_rawobj obj = {NULL};
827
828 if (!delta)
829 continue;
830
831 non_null = 1;
832 idx->off = delta->delta_off;
833 if (git_packfile_unpack(&obj, idx->pack, &idx->off) < 0)
834 continue;
835
836 if (hash_and_save(idx, &obj, delta->delta_off) < 0)
837 continue;
838
839 git__free(obj.data);
840 stats->indexed_objects++;
841 stats->indexed_deltas++;
842 progressed = 1;
843 if ((progress_cb_result = do_progress_callback(idx, stats)) < 0)
844 return progress_cb_result;
845
846 /* remove from the list */
847 git_vector_set(NULL, &idx->deltas, i, NULL);
848 git__free(delta);
849 }
850
851 /* if none were actually set, we're done */
852 if (!non_null)
853 break;
854
855 if (!progressed && (fix_thin_pack(idx, stats) < 0)) {
856 return -1;
857 }
858 }
859
860 return 0;
861 }
862
863 static int update_header_and_rehash(git_indexer *idx, git_transfer_progress *stats)
864 {
865 void *ptr;
866 size_t chunk = 1024*1024;
867 git_off_t hashed = 0;
868 git_mwindow *w = NULL;
869 git_mwindow_file *mwf;
870 unsigned int left;
871
872 mwf = &idx->pack->mwf;
873
874 git_hash_init(&idx->trailer);
875
876
877 /* Update the header to include the numer of local objects we injected */
878 idx->hdr.hdr_entries = htonl(stats->total_objects + stats->local_objects);
879 if (write_at(idx, &idx->hdr, 0, sizeof(struct git_pack_header)) < 0)
880 return -1;
881
882 /*
883 * We now use the same technique as before to determine the
884 * hash. We keep reading up to the end and let
885 * hash_partially() keep the existing trailer out of the
886 * calculation.
887 */
888 git_mwindow_free_all(mwf);
889 idx->inbuf_len = 0;
890 while (hashed < mwf->size) {
891 ptr = git_mwindow_open(mwf, &w, hashed, chunk, &left);
892 if (ptr == NULL)
893 return -1;
894
895 hash_partially(idx, ptr, left);
896 hashed += left;
897
898 git_mwindow_close(&w);
899 }
900
901 return 0;
902 }
903
904 int git_indexer_commit(git_indexer *idx, git_transfer_progress *stats)
905 {
906 git_mwindow *w = NULL;
907 unsigned int i, long_offsets = 0, left;
908 int error;
909 struct git_pack_idx_header hdr;
910 git_buf filename = GIT_BUF_INIT;
911 struct entry *entry;
912 git_oid trailer_hash, file_hash;
913 git_hash_ctx ctx;
914 git_filebuf index_file = {0};
915 void *packfile_trailer;
916
917 if (git_hash_ctx_init(&ctx) < 0)
918 return -1;
919
920 /* Test for this before resolve_deltas(), as it plays with idx->off */
921 if (idx->off < idx->pack->mwf.size - 20) {
922 giterr_set(GITERR_INDEXER, "Unexpected data at the end of the pack");
923 return -1;
924 }
925
926 packfile_trailer = git_mwindow_open(&idx->pack->mwf, &w, idx->pack->mwf.size - GIT_OID_RAWSZ, GIT_OID_RAWSZ, &left);
927 if (packfile_trailer == NULL) {
928 git_mwindow_close(&w);
929 goto on_error;
930 }
931
932 /* Compare the packfile trailer as it was sent to us and what we calculated */
933 git_oid_fromraw(&file_hash, packfile_trailer);
934 git_mwindow_close(&w);
935
936 git_hash_final(&trailer_hash, &idx->trailer);
937 if (git_oid_cmp(&file_hash, &trailer_hash)) {
938 giterr_set(GITERR_INDEXER, "packfile trailer mismatch");
939 return -1;
940 }
941
942 /* Freeze the number of deltas */
943 stats->total_deltas = stats->total_objects - stats->indexed_objects;
944
945 if ((error = resolve_deltas(idx, stats)) < 0)
946 return error;
947
948 if (stats->indexed_objects != stats->total_objects) {
949 giterr_set(GITERR_INDEXER, "early EOF");
950 return -1;
951 }
952
953 if (stats->local_objects > 0) {
954 if (update_header_and_rehash(idx, stats) < 0)
955 return -1;
956
957 git_hash_final(&trailer_hash, &idx->trailer);
958 write_at(idx, &trailer_hash, idx->pack->mwf.size - GIT_OID_RAWSZ, GIT_OID_RAWSZ);
959 }
960
961 git_vector_sort(&idx->objects);
962
963 git_buf_sets(&filename, idx->pack->pack_name);
964 git_buf_shorten(&filename, strlen("pack"));
965 git_buf_puts(&filename, "idx");
966 if (git_buf_oom(&filename))
967 return -1;
968
969 if (git_filebuf_open(&index_file, filename.ptr,
970 GIT_FILEBUF_HASH_CONTENTS, idx->mode) < 0)
971 goto on_error;
972
973 /* Write out the header */
974 hdr.idx_signature = htonl(PACK_IDX_SIGNATURE);
975 hdr.idx_version = htonl(2);
976 git_filebuf_write(&index_file, &hdr, sizeof(hdr));
977
978 /* Write out the fanout table */
979 for (i = 0; i < 256; ++i) {
980 uint32_t n = htonl(idx->fanout[i]);
981 git_filebuf_write(&index_file, &n, sizeof(n));
982 }
983
984 /* Write out the object names (SHA-1 hashes) */
985 git_vector_foreach(&idx->objects, i, entry) {
986 git_filebuf_write(&index_file, &entry->oid, sizeof(git_oid));
987 git_hash_update(&ctx, &entry->oid, GIT_OID_RAWSZ);
988 }
989 git_hash_final(&idx->hash, &ctx);
990
991 /* Write out the CRC32 values */
992 git_vector_foreach(&idx->objects, i, entry) {
993 git_filebuf_write(&index_file, &entry->crc, sizeof(uint32_t));
994 }
995
996 /* Write out the offsets */
997 git_vector_foreach(&idx->objects, i, entry) {
998 uint32_t n;
999
1000 if (entry->offset == UINT32_MAX)
1001 n = htonl(0x80000000 | long_offsets++);
1002 else
1003 n = htonl(entry->offset);
1004
1005 git_filebuf_write(&index_file, &n, sizeof(uint32_t));
1006 }
1007
1008 /* Write out the long offsets */
1009 git_vector_foreach(&idx->objects, i, entry) {
1010 uint32_t split[2];
1011
1012 if (entry->offset != UINT32_MAX)
1013 continue;
1014
1015 split[0] = htonl(entry->offset_long >> 32);
1016 split[1] = htonl(entry->offset_long & 0xffffffff);
1017
1018 git_filebuf_write(&index_file, &split, sizeof(uint32_t) * 2);
1019 }
1020
1021 /* Write out the packfile trailer to the index */
1022 if (git_filebuf_write(&index_file, &trailer_hash, GIT_OID_RAWSZ) < 0)
1023 goto on_error;
1024
1025 /* Write out the hash of the idx */
1026 if (git_filebuf_hash(&trailer_hash, &index_file) < 0)
1027 goto on_error;
1028
1029 git_filebuf_write(&index_file, &trailer_hash, sizeof(git_oid));
1030
1031 /* Figure out what the final name should be */
1032 if (index_path(&filename, idx, ".idx") < 0)
1033 goto on_error;
1034
1035 /* Commit file */
1036 if (git_filebuf_commit_at(&index_file, filename.ptr) < 0)
1037 goto on_error;
1038
1039 git_mwindow_free_all(&idx->pack->mwf);
1040 /* We need to close the descriptor here so Windows doesn't choke on commit_at */
1041 if (p_close(idx->pack->mwf.fd) < 0) {
1042 giterr_set(GITERR_OS, "failed to close packfile");
1043 goto on_error;
1044 }
1045
1046 idx->pack->mwf.fd = -1;
1047
1048 if (index_path(&filename, idx, ".pack") < 0)
1049 goto on_error;
1050
1051 /* And don't forget to rename the packfile to its new place. */
1052 p_rename(idx->pack->pack_name, git_buf_cstr(&filename));
1053
1054 git_buf_free(&filename);
1055 git_hash_ctx_cleanup(&ctx);
1056 return 0;
1057
1058 on_error:
1059 git_mwindow_free_all(&idx->pack->mwf);
1060 git_filebuf_cleanup(&index_file);
1061 git_buf_free(&filename);
1062 git_hash_ctx_cleanup(&ctx);
1063 return -1;
1064 }
1065
1066 void git_indexer_free(git_indexer *idx)
1067 {
1068 if (idx == NULL)
1069 return;
1070
1071 git_vector_free_deep(&idx->objects);
1072
1073 if (idx->pack && idx->pack->idx_cache) {
1074 struct git_pack_entry *pentry;
1075 kh_foreach_value(
1076 idx->pack->idx_cache, pentry, { git__free(pentry); });
1077
1078 git_oidmap_free(idx->pack->idx_cache);
1079 }
1080
1081 git_vector_free_deep(&idx->deltas);
1082
1083 if (!git_mutex_lock(&git__mwindow_mutex)) {
1084 git_packfile_free(idx->pack);
1085 git_mutex_unlock(&git__mwindow_mutex);
1086 }
1087
1088 git_hash_ctx_cleanup(&idx->trailer);
1089 git_hash_ctx_cleanup(&idx->hash_ctx);
1090 git__free(idx);
1091 }