-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpack_index.c
973 lines (875 loc) · 25 KB
/
pack_index.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
/*
* Copyright (c) 2019 Ori Bernstein <[email protected]>
* Copyright (c) 2020, 2022 Stefan Sperling <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "got_compat.h"
#include <sys/queue.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/mman.h>
#include <stdint.h>
#include <errno.h>
#include <imsg.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include <zlib.h>
#include <err.h>
#include <assert.h>
#include <dirent.h>
#include "got_error.h"
#include "got_object.h"
#include "got_lib_hash.h"
#include "got_lib_delta.h"
#include "got_lib_inflate.h"
#include "got_lib_object.h"
#include "got_lib_object_parse.h"
#include "got_lib_object_idset.h"
#include "got_lib_privsep.h"
#include "got_lib_pack.h"
#include "got_lib_ratelimit.h"
#include "got_lib_pack_index.h"
#include "got_lib_delta_cache.h"
struct got_indexed_object {
struct got_object_id id;
/*
* Has this object been fully resolved?
* If so, we know its ID, otherwise we don't and 'id' is invalid.
*/
int valid;
/* Offset of type+size field for this object in pack file. */
off_t off;
/* Type+size values parsed from pack file. */
uint8_t type;
uint64_t size;
/* Length of on-disk type+size data. */
size_t tslen;
/* Length of object data following type+size. */
size_t len;
uint32_t crc;
union {
struct {
/* For ref deltas. */
struct got_object_id ref_id;
} ref;
struct {
/* For offset deltas. */
off_t base_offset;
size_t base_offsetlen;
} ofs;
} delta;
};
static void
putbe32(char *b, uint32_t n)
{
b[0] = n >> 24;
b[1] = n >> 16;
b[2] = n >> 8;
b[3] = n >> 0;
}
static const struct got_error *
read_checksum(uint32_t *crc, struct got_hash *ctx, int fd, size_t len)
{
uint8_t buf[8192];
size_t n;
ssize_t r;
for (n = len; n > 0; n -= r){
r = read(fd, buf, n > sizeof(buf) ? sizeof(buf) : n);
if (r == -1)
return got_error_from_errno("read");
if (r == 0)
break;
if (crc)
*crc = crc32(*crc, buf, r);
if (ctx)
got_hash_update(ctx, buf, r);
}
return NULL;
}
static const struct got_error *
read_file_digest(struct got_hash *ctx, FILE *f, size_t len)
{
uint8_t buf[8192];
size_t n, r;
for (n = len; n > 0; n -= r) {
r = fread(buf, 1, n > sizeof(buf) ? sizeof(buf) : n, f);
if (r == 0) {
if (feof(f))
return NULL;
return got_ferror(f, GOT_ERR_IO);
}
got_hash_update(ctx, buf, r);
}
return NULL;
}
static const struct got_error *
read_packed_object(struct got_pack *pack, struct got_indexed_object *obj,
FILE *tmpfile, struct got_hash *pack_hash_ctx)
{
const struct got_error *err = NULL;
struct got_hash ctx;
uint8_t *data = NULL;
size_t datalen = 0;
ssize_t n;
char *header;
size_t headerlen;
const char *obj_label;
size_t mapoff = obj->off;
struct got_inflate_checksum csum;
size_t digest_len;
memset(&csum, 0, sizeof(csum));
csum.input_ctx = pack_hash_ctx;
csum.input_crc = &obj->crc;
digest_len = got_hash_digest_length(pack->algo);
err = got_pack_parse_object_type_and_size(&obj->type, &obj->size,
&obj->tslen, pack, obj->off);
if (err)
return err;
if (pack->map) {
obj->crc = crc32(obj->crc, pack->map + mapoff, obj->tslen);
got_hash_update(pack_hash_ctx, pack->map + mapoff, obj->tslen);
mapoff += obj->tslen;
} else {
/* XXX Seek back and get the CRC of on-disk type+size bytes. */
if (lseek(pack->fd, obj->off, SEEK_SET) == -1)
return got_error_from_errno("lseek");
err = read_checksum(&obj->crc, pack_hash_ctx,
pack->fd, obj->tslen);
if (err)
return err;
}
switch (obj->type) {
case GOT_OBJ_TYPE_BLOB:
case GOT_OBJ_TYPE_COMMIT:
case GOT_OBJ_TYPE_TREE:
case GOT_OBJ_TYPE_TAG:
if (obj->size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
if (fseek(tmpfile, 0L, SEEK_SET) == -1) {
err = got_error_from_errno("fseek");
break;
}
if (pack->map) {
err = got_inflate_to_file_mmap(&datalen,
&obj->len, &csum, pack->map, mapoff,
pack->filesize - mapoff, tmpfile);
} else {
err = got_inflate_to_file_fd(&datalen,
&obj->len, &csum, pack->fd, tmpfile);
}
} else {
if (pack->map) {
err = got_inflate_to_mem_mmap(&data, &datalen,
&obj->len, &csum, pack->map, mapoff,
pack->filesize - mapoff);
} else {
err = got_inflate_to_mem_fd(&data, &datalen,
&obj->len, &csum, obj->size, pack->fd);
}
}
if (err)
break;
got_hash_init(&ctx, pack->algo);
err = got_object_type_label(&obj_label, obj->type);
if (err) {
free(data);
break;
}
if (asprintf(&header, "%s %lld", obj_label,
(long long)obj->size) == -1) {
err = got_error_from_errno("asprintf");
free(data);
break;
}
headerlen = strlen(header) + 1;
got_hash_update(&ctx, header, headerlen);
if (obj->size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
err = read_file_digest(&ctx, tmpfile, datalen);
if (err) {
free(header);
free(data);
break;
}
} else
got_hash_update(&ctx, data, datalen);
got_hash_final_object_id(&ctx, &obj->id);
free(header);
free(data);
break;
case GOT_OBJ_TYPE_REF_DELTA:
memset(obj->id.hash, 0xff, digest_len);
obj->id.algo = pack->algo;
if (pack->map) {
if (mapoff + digest_len >= pack->filesize) {
err = got_error(GOT_ERR_BAD_PACKFILE);
break;
}
memcpy(obj->delta.ref.ref_id.hash, pack->map + mapoff,
digest_len);
obj->crc = crc32(obj->crc, pack->map + mapoff,
digest_len);
got_hash_update(pack_hash_ctx, pack->map + mapoff,
digest_len);
mapoff += digest_len;
err = got_inflate_to_mem_mmap(NULL, &datalen,
&obj->len, &csum, pack->map, mapoff,
pack->filesize - mapoff);
if (err)
break;
} else {
n = read(pack->fd, obj->delta.ref.ref_id.hash,
digest_len);
if (n == -1) {
err = got_error_from_errno("read");
break;
}
if (n < digest_len) {
err = got_error(GOT_ERR_BAD_PACKFILE);
break;
}
obj->crc = crc32(obj->crc, obj->delta.ref.ref_id.hash,
digest_len);
got_hash_update(pack_hash_ctx,
obj->delta.ref.ref_id.hash, digest_len);
err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
&csum, obj->size, pack->fd);
if (err)
break;
}
obj->len += digest_len;
break;
case GOT_OBJ_TYPE_OFFSET_DELTA:
memset(obj->id.hash, 0xff, digest_len);
obj->id.algo = pack->algo;
err = got_pack_parse_offset_delta(&obj->delta.ofs.base_offset,
&obj->delta.ofs.base_offsetlen, pack, obj->off,
obj->tslen);
if (err)
break;
if (pack->map) {
if (mapoff + obj->delta.ofs.base_offsetlen >=
pack->filesize) {
err = got_error(GOT_ERR_BAD_PACKFILE);
break;
}
if (mapoff + obj->delta.ofs.base_offsetlen >
SIZE_MAX) {
err = got_error_fmt(GOT_ERR_RANGE,
"mapoff %lld would overflow size_t",
(long long)mapoff
+ obj->delta.ofs.base_offsetlen);
break;
}
obj->crc = crc32(obj->crc, pack->map + mapoff,
obj->delta.ofs.base_offsetlen);
got_hash_update(pack_hash_ctx, pack->map + mapoff,
obj->delta.ofs.base_offsetlen);
mapoff += obj->delta.ofs.base_offsetlen;
err = got_inflate_to_mem_mmap(NULL, &datalen,
&obj->len, &csum, pack->map, mapoff,
pack->filesize - mapoff);
if (err)
break;
} else {
/*
* XXX Seek back and get CRC and hash digest
* of on-disk offset bytes.
*/
if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET)
== -1) {
err = got_error_from_errno("lseek");
break;
}
err = read_checksum(&obj->crc, pack_hash_ctx,
pack->fd, obj->delta.ofs.base_offsetlen);
if (err)
break;
err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
&csum, obj->size, pack->fd);
if (err)
break;
}
obj->len += obj->delta.ofs.base_offsetlen;
break;
default:
err = got_error(GOT_ERR_OBJ_TYPE);
break;
}
return err;
}
const struct got_error *
got_pack_hwrite(int fd, void *buf, int len, struct got_hash *ctx)
{
ssize_t w;
got_hash_update(ctx, buf, len);
w = write(fd, buf, len);
if (w == -1)
return got_error_from_errno("write");
if (w != len)
return got_error(GOT_ERR_IO);
return NULL;
}
static const struct got_error *
resolve_deltified_object(struct got_pack *pack, struct got_packidx *packidx,
struct got_indexed_object *obj, FILE *tmpfile, FILE *delta_base_file,
FILE *delta_accum_file)
{
const struct got_error *err = NULL;
struct got_delta_chain deltas;
struct got_delta *delta;
uint8_t *buf = NULL;
size_t len = 0;
struct got_hash ctx;
char *header = NULL;
size_t headerlen;
uint64_t max_size;
int base_obj_type;
const char *obj_label;
deltas.nentries = 0;
STAILQ_INIT(&deltas.entries);
err = got_pack_resolve_delta_chain(&deltas, packidx, pack,
obj->off, obj->tslen, obj->type, obj->size,
GOT_DELTA_CHAIN_RECURSION_MAX);
if (err)
goto done;
err = got_pack_get_delta_chain_max_size(&max_size, &deltas, pack);
if (err)
goto done;
if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
rewind(tmpfile);
rewind(delta_base_file);
rewind(delta_accum_file);
err = got_pack_dump_delta_chain_to_file(&len, &deltas,
pack, tmpfile, delta_base_file, delta_accum_file);
if (err)
goto done;
} else {
err = got_pack_dump_delta_chain_to_mem(&buf, &len,
&deltas, pack);
}
if (err)
goto done;
err = got_delta_chain_get_base_type(&base_obj_type, &deltas);
if (err)
goto done;
err = got_object_type_label(&obj_label, base_obj_type);
if (err)
goto done;
if (asprintf(&header, "%s %zd", obj_label, len) == -1) {
err = got_error_from_errno("asprintf");
goto done;
}
headerlen = strlen(header) + 1;
got_hash_init(&ctx, pack->algo);
got_hash_update(&ctx, header, headerlen);
if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
err = read_file_digest(&ctx, tmpfile, len);
if (err)
goto done;
} else
got_hash_update(&ctx, buf, len);
got_hash_final_object_id(&ctx, &obj->id);
done:
free(buf);
free(header);
while (!STAILQ_EMPTY(&deltas.entries)) {
delta = STAILQ_FIRST(&deltas.entries);
STAILQ_REMOVE_HEAD(&deltas.entries, entry);
free(delta);
}
return err;
}
/* Determine the slot in the pack index a given object ID should use. */
static int
find_object_idx(struct got_packidx *packidx, uint8_t *hash)
{
u_int8_t id0 = hash[0];
uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
int left = 0, right = nindexed - 1;
int cmp = 0, i = 0;
size_t digest_len = got_hash_digest_length(packidx->algo);
if (id0 > 0)
left = be32toh(packidx->hdr.fanout_table[id0 - 1]);
while (left <= right) {
uint8_t *oid;
i = ((left + right) / 2);
oid = packidx->hdr.sorted_ids + i * digest_len;
cmp = memcmp(hash, oid, digest_len);
if (cmp == 0)
return -1; /* object already indexed */
else if (cmp > 0)
left = i + 1;
else if (cmp < 0)
right = i - 1;
}
return left;
}
#if 0
static void
print_packidx(struct got_packidx *packidx)
{
uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
size_t digest_len = got_hash_digest_length(packidx->algo);
int i;
fprintf(stderr, "object IDs:\n");
for (i = 0; i < nindexed; i++) {
char hex[GOT_HASH_DIGEST_STRING_MAXLEN];
got_hash_digest_to_str(packidx->hdr.sorted_ids + i * digest_len,
hex, sizeof(hex), packidx->algo);
fprintf(stderr, "%s\n", hex);
}
fprintf(stderr, "\n");
fprintf(stderr, "object offsets:\n");
for (i = 0; i < nindexed; i++) {
uint32_t offset = be32toh(packidx->hdr.offsets[i]);
if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
int j = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
fprintf(stderr, "%u -> %llu\n", offset,
be64toh(packidx->hdr.large_offsets[j]));
} else
fprintf(stderr, "%u\n", offset);
}
fprintf(stderr, "\n");
fprintf(stderr, "fanout table:");
for (i = 0; i <= 0xff; i++)
fprintf(stderr, " %u", be32toh(packidx->hdr.fanout_table[i]));
fprintf(stderr, "\n");
}
#endif
static void
add_indexed_object(struct got_packidx *packidx, uint32_t idx,
struct got_indexed_object *obj)
{
int i;
uint8_t *oid;
size_t digest_len = got_hash_digest_length(packidx->algo);
oid = packidx->hdr.sorted_ids + idx * digest_len;
memcpy(oid, obj->id.hash, digest_len);
obj->id.algo = packidx->algo;
packidx->hdr.crc32[idx] = htobe32(obj->crc);
if (obj->off < GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX)
packidx->hdr.offsets[idx] = htobe32(obj->off);
else {
packidx->hdr.offsets[idx] = htobe32(packidx->nlargeobj |
GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX);
packidx->hdr.large_offsets[packidx->nlargeobj] =
htobe64(obj->off);
packidx->nlargeobj++;
}
for (i = obj->id.hash[0]; i <= 0xff; i++) {
uint32_t n = be32toh(packidx->hdr.fanout_table[i]);
packidx->hdr.fanout_table[i] = htobe32(n + 1);
}
}
static int
indexed_obj_cmp(const void *pa, const void *pb)
{
struct got_indexed_object *a, *b;
a = (struct got_indexed_object *)pa;
b = (struct got_indexed_object *)pb;
return got_object_id_cmp(&a->id, &b->id);
}
static void
make_packidx(struct got_packidx *packidx, uint32_t nobj,
struct got_indexed_object *objects)
{
struct got_indexed_object *obj;
int i;
uint32_t idx = 0;
qsort(objects, nobj, sizeof(struct got_indexed_object),
indexed_obj_cmp);
memset(packidx->hdr.fanout_table, 0,
GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t));
packidx->nlargeobj = 0;
for (i = 0; i < nobj; i++) {
obj = &objects[i];
if (obj->valid)
add_indexed_object(packidx, idx++, obj);
}
}
static void
update_packidx(struct got_packidx *packidx, uint32_t nobj,
struct got_indexed_object *obj)
{
int idx;
uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
size_t digest_len = got_hash_digest_length(packidx->algo);
uint8_t *from, *to;
idx = find_object_idx(packidx, obj->id.hash);
if (idx == -1)
return; /* object already indexed */
from = packidx->hdr.sorted_ids + idx * digest_len;
to = from + digest_len;
memmove(to, from, digest_len * (nindexed - idx));
memmove(&packidx->hdr.offsets[idx + 1], &packidx->hdr.offsets[idx],
sizeof(uint32_t) * (nindexed - idx));
add_indexed_object(packidx, idx, obj);
}
static const struct got_error *
report_progress(uint32_t nobj_total, uint32_t nobj_indexed, uint32_t nobj_loose,
uint32_t nobj_resolved, struct got_ratelimit *rl,
got_pack_index_progress_cb progress_cb, void *progress_arg)
{
const struct got_error *err;
int elapsed = 0;
if (rl) {
err = got_ratelimit_check(&elapsed, rl);
if (err || !elapsed)
return err;
}
return progress_cb(progress_arg, nobj_total, nobj_indexed, nobj_loose,
nobj_resolved);
}
const struct got_error *
got_pack_index(struct got_pack *pack, int idxfd, FILE *tmpfile,
FILE *delta_base_file, FILE *delta_accum_file,
struct got_object_id *pack_hash_expected,
got_pack_index_progress_cb progress_cb, void *progress_arg,
struct got_ratelimit *rl)
{
const struct got_error *err;
struct got_packfile_hdr hdr;
struct got_packidx packidx;
char buf[8];
struct got_object_id pack_hash;
uint32_t nobj, nvalid, nloose, nresolved = 0, i;
struct got_indexed_object *objects = NULL, *obj;
struct got_hash ctx;
uint8_t packidx_hash[GOT_HASH_DIGEST_MAXLEN];
ssize_t r, w;
int pass, have_ref_deltas = 0, first_delta_idx = -1;
size_t mapoff = 0;
int p_indexed = 0, last_p_indexed = -1;
int p_resolved = 0, last_p_resolved = -1;
ssize_t digest_len;
/* This has to be signed for lseek(2) later */
digest_len = got_hash_digest_length(pack->algo);
/* Require that pack file header and hash trailer are present. */
if (pack->filesize < sizeof(hdr) + digest_len)
return got_error_msg(GOT_ERR_BAD_PACKFILE,
"short pack file");
if (pack->map) {
memcpy(&hdr, pack->map, sizeof(hdr));
mapoff += sizeof(hdr);
} else {
r = read(pack->fd, &hdr, sizeof(hdr));
if (r == -1)
return got_error_from_errno("read");
if (r < sizeof(hdr))
return got_error_msg(GOT_ERR_BAD_PACKFILE,
"short pack file");
}
if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
return got_error_msg(GOT_ERR_BAD_PACKFILE,
"bad packfile signature");
if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
return got_error_msg(GOT_ERR_BAD_PACKFILE,
"bad packfile version");
nobj = be32toh(hdr.nobjects);
if (nobj == 0)
return got_error_msg(GOT_ERR_BAD_PACKFILE,
"bad packfile with zero objects");
/* We compute the hash of pack file contents and verify later on. */
got_hash_init(&ctx, pack->algo);
got_hash_update(&ctx, &hdr, sizeof(hdr));
/*
* Create an in-memory pack index which will grow as objects
* IDs in the pack file are discovered. Only fields used to
* read deltified objects will be needed by the pack.c library
* code, so setting up just a pack index header is sufficient.
*/
memset(&packidx, 0, sizeof(packidx));
packidx.hdr.magic = malloc(sizeof(uint32_t));
if (packidx.hdr.magic == NULL)
return got_error_from_errno("malloc");
*packidx.hdr.magic = htobe32(GOT_PACKIDX_V2_MAGIC);
packidx.hdr.version = malloc(sizeof(uint32_t));
if (packidx.hdr.version == NULL) {
err = got_error_from_errno("malloc");
goto done;
}
*packidx.hdr.version = htobe32(GOT_PACKIDX_VERSION);
packidx.hdr.fanout_table = calloc(GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS,
sizeof(uint32_t));
if (packidx.hdr.fanout_table == NULL) {
err = got_error_from_errno("calloc");
goto done;
}
packidx.hdr.sorted_ids = calloc(nobj, digest_len);
if (packidx.hdr.sorted_ids == NULL) {
err = got_error_from_errno("calloc");
goto done;
}
packidx.hdr.crc32 = calloc(nobj, sizeof(uint32_t));
if (packidx.hdr.crc32 == NULL) {
err = got_error_from_errno("calloc");
goto done;
}
packidx.hdr.offsets = calloc(nobj, sizeof(uint32_t));
if (packidx.hdr.offsets == NULL) {
err = got_error_from_errno("calloc");
goto done;
}
packidx.algo = pack->algo;
/* Large offsets table is empty for pack files < 2 GB. */
if (pack->filesize >= GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
packidx.hdr.large_offsets = calloc(nobj, sizeof(uint64_t));
if (packidx.hdr.large_offsets == NULL) {
err = got_error_from_errno("calloc");
goto done;
}
}
nvalid = 0;
nloose = 0;
objects = calloc(nobj, sizeof(struct got_indexed_object));
if (objects == NULL)
return got_error_from_errno("calloc");
/*
* First pass: locate all objects and identify un-deltified objects.
*
* When this pass has completed we will know offset, type, size, and
* CRC information for all objects in this pack file. We won't know
* any of the actual object IDs of deltified objects yet since we
* will not yet attempt to combine deltas.
*/
pass = 1;
for (i = 0; i < nobj; i++) {
/* Don't send too many progress privsep messages. */
p_indexed = ((i + 1) * 100) / nobj;
if (p_indexed != last_p_indexed) {
err = report_progress(nobj, i + 1, nloose, 0,
rl, progress_cb, progress_arg);
if (err)
goto done;
last_p_indexed = p_indexed;
}
obj = &objects[i];
obj->crc = crc32(0L, NULL, 0);
/* Store offset to type+size information for this object. */
if (pack->map) {
obj->off = mapoff;
} else {
obj->off = lseek(pack->fd, 0, SEEK_CUR);
if (obj->off == -1) {
err = got_error_from_errno("lseek");
goto done;
}
}
err = read_packed_object(pack, obj, tmpfile, &ctx);
if (err)
goto done;
if (pack->map) {
mapoff += obj->tslen + obj->len;
} else {
if (lseek(pack->fd, obj->off + obj->tslen + obj->len,
SEEK_SET) == -1) {
err = got_error_from_errno("lseek");
goto done;
}
}
if (obj->type == GOT_OBJ_TYPE_BLOB ||
obj->type == GOT_OBJ_TYPE_TREE ||
obj->type == GOT_OBJ_TYPE_COMMIT ||
obj->type == GOT_OBJ_TYPE_TAG) {
obj->valid = 1;
nloose++;
} else {
if (first_delta_idx == -1)
first_delta_idx = i;
if (obj->type == GOT_OBJ_TYPE_REF_DELTA)
have_ref_deltas = 1;
}
}
nvalid = nloose;
/*
* Having done a full pass over the pack file and can now
* verify its checksum.
*/
got_hash_final_object_id(&ctx, &pack_hash);
if (got_object_id_cmp(pack_hash_expected, &pack_hash) != 0) {
err = got_error(GOT_ERR_PACKFILE_CSUM);
goto done;
}
/* Verify the hash checksum stored at the end of the pack file. */
if (pack->map) {
if (pack->filesize > SIZE_MAX) {
err = got_error_fmt(GOT_ERR_RANGE,
"filesize %lld overflows size_t",
(long long)pack->filesize);
goto done;
}
memcpy(pack_hash_expected, pack->map +
pack->filesize - digest_len,
digest_len);
} else {
ssize_t n;
if (lseek(pack->fd, -digest_len, SEEK_END) == -1) {
err = got_error_from_errno("lseek");
goto done;
}
n = read(pack->fd, pack_hash_expected, digest_len);
if (n == -1) {
err = got_error_from_errno("read");
goto done;
}
if (n != digest_len) {
err = got_error(GOT_ERR_IO);
goto done;
}
}
if (got_object_id_cmp(pack_hash_expected, &pack_hash) != 0) {
err = got_error_msg(GOT_ERR_BAD_PACKFILE,
"bad checksum in pack file trailer");
goto done;
}
if (first_delta_idx == -1)
first_delta_idx = 0;
/* In order to resolve ref deltas we need an in-progress pack index. */
if (have_ref_deltas)
make_packidx(&packidx, nobj, objects);
/*
* Second pass: We can now resolve deltas to compute the IDs of
* objects which appear in deltified form. Because deltas can be
* chained this pass may require a couple of iterations until all
* IDs of deltified objects have been discovered.
*/
pass++;
while (nvalid != nobj) {
int n = 0;
/*
* This loop will only run once unless the pack file
* contains ref deltas which refer to objects located
* later in the pack file, which is unusual.
* Offset deltas can always be resolved in one pass
* unless the packfile is corrupt.
*/
for (i = first_delta_idx; i < nobj; i++) {
obj = &objects[i];
if (obj->type != GOT_OBJ_TYPE_REF_DELTA &&
obj->type != GOT_OBJ_TYPE_OFFSET_DELTA)
continue;
if (obj->valid)
continue;
if (pack->map == NULL && lseek(pack->fd,
obj->off + obj->tslen, SEEK_SET) == -1) {
err = got_error_from_errno("lseek");
goto done;
}
err = resolve_deltified_object(pack, &packidx, obj,
tmpfile, delta_base_file, delta_accum_file);
if (err) {
if (err->code != GOT_ERR_NO_OBJ)
goto done;
/*
* We cannot resolve this object yet because
* a delta base is unknown. Try again later.
*/
continue;
}
obj->valid = 1;
n++;
if (have_ref_deltas)
update_packidx(&packidx, nobj, obj);
/* Don't send too many progress privsep messages. */
p_resolved = ((nresolved + n) * 100) / nobj;
if (p_resolved != last_p_resolved) {
err = report_progress(nobj, nobj,
nloose, nresolved + n, rl,
progress_cb, progress_arg);
if (err)
goto done;
last_p_resolved = p_resolved;
}
}
if (pass++ > 3 && n == 0) {
err = got_error_msg(GOT_ERR_BAD_PACKFILE,
"could not resolve any of deltas; packfile could "
"be corrupt");
goto done;
}
nresolved += n;
nvalid += n;
}
if (nloose + nresolved != nobj) {
static char msg[64];
snprintf(msg, sizeof(msg), "discovered only %d of %d objects",
nloose + nresolved, nobj);
err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
goto done;
}
err = report_progress(nobj, nobj, nloose, nresolved, NULL,
progress_cb, progress_arg);
if (err)
goto done;
make_packidx(&packidx, nobj, objects);
free(objects);
objects = NULL;
got_hash_init(&ctx, pack->algo);
putbe32(buf, GOT_PACKIDX_V2_MAGIC);
putbe32(buf + 4, GOT_PACKIDX_VERSION);
err = got_pack_hwrite(idxfd, buf, 8, &ctx);
if (err)
goto done;
err = got_pack_hwrite(idxfd, packidx.hdr.fanout_table,
GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t), &ctx);
if (err)
goto done;
err = got_pack_hwrite(idxfd, packidx.hdr.sorted_ids,
nobj * digest_len, &ctx);
if (err)
goto done;
err = got_pack_hwrite(idxfd, packidx.hdr.crc32,
nobj * sizeof(uint32_t), &ctx);
if (err)
goto done;
err = got_pack_hwrite(idxfd, packidx.hdr.offsets,
nobj * sizeof(uint32_t), &ctx);
if (err)
goto done;
if (packidx.nlargeobj > 0) {
err = got_pack_hwrite(idxfd, packidx.hdr.large_offsets,
packidx.nlargeobj * sizeof(uint64_t), &ctx);
if (err)
goto done;
}
err = got_pack_hwrite(idxfd, &pack_hash.hash, digest_len, &ctx);
if (err)
goto done;
got_hash_final(&ctx, packidx_hash);
w = write(idxfd, packidx_hash, digest_len);
if (w == -1) {
err = got_error_from_errno("write");
goto done;
}
if (w != digest_len) {
err = got_error(GOT_ERR_IO);
goto done;
}
done:
free(objects);
free(packidx.hdr.magic);
free(packidx.hdr.version);
free(packidx.hdr.fanout_table);
free(packidx.hdr.sorted_ids);
free(packidx.hdr.offsets);
free(packidx.hdr.large_offsets);
return err;
}