forked from libgit2/libgit2
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindexer.c
More file actions
1480 lines (1169 loc) · 34.8 KB
/
indexer.c
File metadata and controls
1480 lines (1169 loc) · 34.8 KB
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
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#include "indexer.h"
#include "git2/indexer.h"
#include "git2/object.h"
#include "commit.h"
#include "tree.h"
#include "tag.h"
#include "pack.h"
#include "mwindow.h"
#include "posix.h"
#include "pack.h"
#include "filebuf.h"
#include "oid.h"
#include "oidarray.h"
#include "zstream.h"
#include "object.h"
#include "hashmap_oid.h"
size_t git_indexer__max_objects = UINT32_MAX;
#define UINT31_MAX (0x7FFFFFFF)
GIT_HASHMAP_OID_SETUP(git_indexer_oidmap, git_oid *);
struct entry {
git_oid oid;
uint32_t crc;
uint32_t offset;
uint64_t offset_long;
};
struct git_indexer {
unsigned int parsed_header :1,
pack_committed :1,
have_stream :1,
have_delta :1,
do_fsync :1,
do_verify :1;
git_oid_t oid_type;
struct git_pack_header hdr;
struct git_pack_file *pack;
unsigned int mode;
off64_t off;
off64_t entry_start;
git_object_t entry_type;
git_str entry_data;
git_packfile_stream stream;
size_t nr_objects;
git_vector objects;
git_vector deltas;
unsigned int fanout[256];
git_hash_ctx hash_ctx;
unsigned char checksum[GIT_HASH_MAX_SIZE];
char name[(GIT_HASH_MAX_SIZE * 2) + 1];
git_indexer_progress_cb progress_cb;
void *progress_payload;
char objbuf[8*1024];
/* OIDs referenced from pack objects. Used for verification. */
git_indexer_oidmap expected_oids;
/* Needed to look up objects which we want to inject to fix a thin pack */
git_odb *odb;
/* Fields for calculating the packfile trailer (hash of everything before it) */
char inbuf[GIT_HASH_MAX_SIZE];
size_t inbuf_len;
git_hash_ctx trailer;
};
struct delta_info {
off64_t delta_off;
};
#ifndef GIT_DEPRECATE_HARD
const git_oid *git_indexer_hash(const git_indexer *idx)
{
return (git_oid *)idx->checksum;
}
#endif
const char *git_indexer_name(const git_indexer *idx)
{
return idx->name;
}
static int parse_header(struct git_pack_header *hdr, struct git_pack_file *pack)
{
int error;
git_map map;
if ((error = p_mmap(&map, sizeof(*hdr), GIT_PROT_READ, GIT_MAP_SHARED, pack->mwf.fd, 0)) < 0)
return error;
memcpy(hdr, map.data, sizeof(*hdr));
p_munmap(&map);
/* Verify we recognize this pack file format. */
if (hdr->hdr_signature != ntohl(PACK_SIGNATURE)) {
git_error_set(GIT_ERROR_INDEXER, "wrong pack signature");
return -1;
}
if (!pack_version_ok(hdr->hdr_version)) {
git_error_set(GIT_ERROR_INDEXER, "wrong pack version");
return -1;
}
return 0;
}
static int objects_cmp(const void *a, const void *b)
{
const struct entry *entrya = a;
const struct entry *entryb = b;
return git_oid__cmp(&entrya->oid, &entryb->oid);
}
int git_indexer_options_init(git_indexer_options *opts, unsigned int version)
{
GIT_INIT_STRUCTURE_FROM_TEMPLATE(
opts, version, git_indexer_options, GIT_INDEXER_OPTIONS_INIT);
return 0;
}
#ifndef GIT_DEPRECATE_HARD
int git_indexer_init_options(git_indexer_options *opts, unsigned int version)
{
return git_indexer_options_init(opts, version);
}
#endif
GIT_INLINE(git_hash_algorithm_t) indexer_hash_algorithm(git_indexer *idx)
{
switch (idx->oid_type) {
case GIT_OID_SHA1:
return GIT_HASH_ALGORITHM_SHA1;
#ifdef GIT_EXPERIMENTAL_SHA256
case GIT_OID_SHA256:
return GIT_HASH_ALGORITHM_SHA256;
#endif
}
return GIT_HASH_ALGORITHM_NONE;
}
static int indexer_new(
git_indexer **out,
const char *prefix,
git_oid_t oid_type,
unsigned int mode,
git_odb *odb,
git_indexer_options *in_opts)
{
git_indexer_options opts = GIT_INDEXER_OPTIONS_INIT;
git_indexer *idx;
git_str path = GIT_STR_INIT, tmp_path = GIT_STR_INIT;
static const char suff[] = "/pack";
git_hash_algorithm_t checksum_type;
int error, fd = -1;
if (in_opts)
memcpy(&opts, in_opts, sizeof(opts));
if (oid_type)
GIT_ASSERT_ARG(git_oid_type_is_valid(oid_type));
idx = git__calloc(1, sizeof(git_indexer));
GIT_ERROR_CHECK_ALLOC(idx);
idx->oid_type = oid_type ? oid_type : GIT_OID_DEFAULT;
idx->odb = odb;
idx->progress_cb = opts.progress_cb;
idx->progress_payload = opts.progress_cb_payload;
idx->mode = mode ? mode : GIT_PACK_FILE_MODE;
git_str_init(&idx->entry_data, 0);
checksum_type = indexer_hash_algorithm(idx);
if ((error = git_hash_ctx_init(&idx->hash_ctx, checksum_type)) < 0 ||
(error = git_hash_ctx_init(&idx->trailer, checksum_type)) < 0)
goto cleanup;
idx->do_verify = opts.verify;
if (git_repository__fsync_gitdir)
idx->do_fsync = 1;
error = git_str_joinpath(&path, prefix, suff);
if (error < 0)
goto cleanup;
fd = git_futils_mktmp(&tmp_path, git_str_cstr(&path), idx->mode);
git_str_dispose(&path);
if (fd < 0)
goto cleanup;
error = git_packfile_alloc(&idx->pack, git_str_cstr(&tmp_path), oid_type);
git_str_dispose(&tmp_path);
if (error < 0)
goto cleanup;
idx->pack->mwf.fd = fd;
if ((error = git_mwindow_file_register(&idx->pack->mwf)) < 0)
goto cleanup;
*out = idx;
return 0;
cleanup:
if (fd != -1)
p_close(fd);
if (git_str_len(&tmp_path) > 0)
p_unlink(git_str_cstr(&tmp_path));
if (idx->pack != NULL)
p_unlink(idx->pack->pack_name);
git_str_dispose(&path);
git_str_dispose(&tmp_path);
git__free(idx);
return -1;
}
#ifdef GIT_EXPERIMENTAL_SHA256
int git_indexer_new(
git_indexer **out,
const char *prefix,
git_indexer_options *opts)
{
return indexer_new(
out,
prefix,
opts ? opts->oid_type : 0,
opts ? opts->mode : 0,
opts ? opts->odb : NULL,
opts);
}
#else
int git_indexer_new(
git_indexer **out,
const char *prefix,
unsigned int mode,
git_odb *odb,
git_indexer_options *opts)
{
return indexer_new(out, prefix, GIT_OID_SHA1, mode, odb, opts);
}
#endif
void git_indexer__set_fsync(git_indexer *idx, int do_fsync)
{
idx->do_fsync = !!do_fsync;
}
/* Try to store the delta so we can try to resolve it later */
static int store_delta(git_indexer *idx)
{
struct delta_info *delta;
delta = git__calloc(1, sizeof(struct delta_info));
GIT_ERROR_CHECK_ALLOC(delta);
delta->delta_off = idx->entry_start;
if (git_vector_insert(&idx->deltas, delta) < 0)
return -1;
return 0;
}
static int hash_header(git_hash_ctx *ctx, off64_t len, git_object_t type)
{
char buffer[64];
size_t hdrlen;
int error;
if ((error = git_odb__format_object_header(&hdrlen,
buffer, sizeof(buffer), (size_t)len, type)) < 0)
return error;
return git_hash_update(ctx, buffer, hdrlen);
}
static int hash_object_stream(git_indexer*idx, git_packfile_stream *stream)
{
ssize_t read;
GIT_ASSERT_ARG(idx);
GIT_ASSERT_ARG(stream);
do {
if ((read = git_packfile_stream_read(stream, idx->objbuf, sizeof(idx->objbuf))) < 0)
break;
if (idx->do_verify)
git_str_put(&idx->entry_data, idx->objbuf, read);
git_hash_update(&idx->hash_ctx, idx->objbuf, read);
} while (read > 0);
if (read < 0)
return (int)read;
return 0;
}
/* In order to create the packfile stream, we need to skip over the delta base description */
static int advance_delta_offset(git_indexer *idx, git_object_t type)
{
git_mwindow *w = NULL;
GIT_ASSERT_ARG(type == GIT_PACKFILE_REF_DELTA || type == GIT_PACKFILE_OFS_DELTA);
if (type == GIT_PACKFILE_REF_DELTA) {
idx->off += git_oid_size(idx->oid_type);
} else {
off64_t base_off;
int error = get_delta_base(&base_off, idx->pack, &w, &idx->off, type, idx->entry_start);
git_mwindow_close(&w);
if (error < 0)
return error;
}
return 0;
}
/* Read from the stream and discard any output */
static int read_object_stream(git_indexer *idx, git_packfile_stream *stream)
{
ssize_t read;
GIT_ASSERT_ARG(stream);
do {
read = git_packfile_stream_read(stream, idx->objbuf, sizeof(idx->objbuf));
} while (read > 0);
if (read < 0)
return (int)read;
return 0;
}
static int crc_object(uint32_t *crc_out, git_mwindow_file *mwf, off64_t start, off64_t size)
{
void *ptr;
uint32_t crc;
unsigned int left, len;
git_mwindow *w = NULL;
crc = crc32(0L, Z_NULL, 0);
while (size) {
ptr = git_mwindow_open(mwf, &w, start, (size_t)size, &left);
if (ptr == NULL)
return -1;
len = min(left, (unsigned int)size);
crc = crc32(crc, ptr, len);
size -= len;
start += len;
git_mwindow_close(&w);
}
*crc_out = htonl(crc);
return 0;
}
static int add_expected_oid(git_indexer *idx, const git_oid *oid)
{
/*
* If we know about that object because it is stored in our ODB or
* because we have already processed it as part of our pack file, we do
* not have to expect it.
*/
if ((!idx->odb || !git_odb_exists(idx->odb, oid)) &&
!git_pack_oidmap_contains(&idx->pack->idx_cache, oid) &&
!git_indexer_oidmap_contains(&idx->expected_oids, oid)) {
git_oid *dup = git__malloc(sizeof(*oid));
GIT_ERROR_CHECK_ALLOC(dup);
git_oid_cpy(dup, oid);
return git_indexer_oidmap_put(&idx->expected_oids, dup, dup);
}
return 0;
}
static int check_object_connectivity(git_indexer *idx, const git_rawobj *obj)
{
git_object *object;
git_oid *expected;
int error = 0;
if (obj->type != GIT_OBJECT_BLOB &&
obj->type != GIT_OBJECT_TREE &&
obj->type != GIT_OBJECT_COMMIT &&
obj->type != GIT_OBJECT_TAG)
return 0;
if (git_object__from_raw(&object, obj->data, obj->len, obj->type, idx->oid_type) < 0) {
/*
* parse_raw returns EINVALID on invalid data; downgrade
* that to a normal -1 error code.
*/
error = -1;
goto out;
}
if (git_indexer_oidmap_get(&expected, &idx->expected_oids, &object->cached.oid) == 0) {
git_indexer_oidmap_remove(&idx->expected_oids, &object->cached.oid);
git__free(expected);
}
/*
* Check whether this is a known object. If so, we can just continue as
* we assume that the ODB has a complete graph.
*/
if (idx->odb && git_odb_exists(idx->odb, &object->cached.oid))
return 0;
switch (obj->type) {
case GIT_OBJECT_TREE:
{
git_tree *tree = (git_tree *) object;
git_tree_entry *entry;
size_t i;
git_array_foreach(tree->entries, i, entry)
if (add_expected_oid(idx, &entry->oid) < 0)
goto out;
break;
}
case GIT_OBJECT_COMMIT:
{
git_commit *commit = (git_commit *) object;
git_oid *parent_oid;
size_t i;
git_array_foreach(commit->parent_ids, i, parent_oid)
if (add_expected_oid(idx, parent_oid) < 0)
goto out;
if (add_expected_oid(idx, &commit->tree_id) < 0)
goto out;
break;
}
case GIT_OBJECT_TAG:
{
git_tag *tag = (git_tag *) object;
if (add_expected_oid(idx, &tag->target) < 0)
goto out;
break;
}
case GIT_OBJECT_BLOB:
default:
break;
}
out:
git_object_free(object);
return error;
}
static int store_object(git_indexer *idx)
{
int i, error;
git_oid oid;
struct entry *entry;
off64_t entry_size;
struct git_pack_entry *pentry;
off64_t entry_start = idx->entry_start;
entry = git__calloc(1, sizeof(*entry));
GIT_ERROR_CHECK_ALLOC(entry);
pentry = git__calloc(1, sizeof(struct git_pack_entry));
GIT_ERROR_CHECK_ALLOC(pentry);
if (git_hash_final(oid.id, &idx->hash_ctx)) {
git__free(pentry);
goto on_error;
}
#ifdef GIT_EXPERIMENTAL_SHA256
oid.type = idx->oid_type;
#endif
entry_size = idx->off - entry_start;
if (entry_start > UINT31_MAX) {
entry->offset = UINT32_MAX;
entry->offset_long = entry_start;
} else {
entry->offset = (uint32_t)entry_start;
}
if (idx->do_verify) {
git_rawobj rawobj = {
idx->entry_data.ptr,
idx->entry_data.size,
idx->entry_type
};
if ((error = check_object_connectivity(idx, &rawobj)) < 0)
goto on_error;
}
git_oid_cpy(&pentry->id, &oid);
pentry->offset = entry_start;
if (git_pack_oidmap_contains(&idx->pack->idx_cache, &pentry->id)) {
const char *idstr = git_oid_tostr_s(&pentry->id);
if (!idstr)
git_error_set(GIT_ERROR_INDEXER, "failed to parse object id");
else
git_error_set(GIT_ERROR_INDEXER, "duplicate object %s found in pack", idstr);
git__free(pentry);
goto on_error;
}
if ((error = git_pack_oidmap_put(&idx->pack->idx_cache, &pentry->id, pentry)) < 0) {
git__free(pentry);
git_error_set_oom();
goto on_error;
}
git_oid_cpy(&entry->oid, &oid);
if (crc_object(&entry->crc, &idx->pack->mwf, entry_start, entry_size) < 0)
goto on_error;
/* Add the object to the list */
if (git_vector_insert(&idx->objects, entry) < 0)
goto on_error;
for (i = oid.id[0]; i < 256; ++i) {
idx->fanout[i]++;
}
return 0;
on_error:
git__free(entry);
return -1;
}
GIT_INLINE(bool) has_entry(git_indexer *idx, git_oid *id)
{
return git_pack_oidmap_contains(&idx->pack->idx_cache, id);
}
static int save_entry(git_indexer *idx, struct entry *entry, struct git_pack_entry *pentry, off64_t entry_start)
{
int i;
if (entry_start > UINT31_MAX) {
entry->offset = UINT32_MAX;
entry->offset_long = entry_start;
} else {
entry->offset = (uint32_t)entry_start;
}
pentry->offset = entry_start;
if (git_pack_oidmap_contains(&idx->pack->idx_cache, &pentry->id) ||
git_pack_oidmap_put(&idx->pack->idx_cache, &pentry->id, pentry) < 0) {
git_error_set(GIT_ERROR_INDEXER, "cannot insert object into pack");
return -1;
}
/* Add the object to the list */
if (git_vector_insert(&idx->objects, entry) < 0)
return -1;
for (i = entry->oid.id[0]; i < 256; ++i) {
idx->fanout[i]++;
}
return 0;
}
static int hash_and_save(git_indexer *idx, git_rawobj *obj, off64_t entry_start)
{
git_oid oid;
size_t entry_size;
struct entry *entry;
struct git_pack_entry *pentry = NULL;
entry = git__calloc(1, sizeof(*entry));
GIT_ERROR_CHECK_ALLOC(entry);
if (git_odb__hashobj(&oid, obj, idx->oid_type) < 0) {
git_error_set(GIT_ERROR_INDEXER, "failed to hash object");
goto on_error;
}
pentry = git__calloc(1, sizeof(struct git_pack_entry));
GIT_ERROR_CHECK_ALLOC(pentry);
git_oid_cpy(&pentry->id, &oid);
git_oid_cpy(&entry->oid, &oid);
entry->crc = crc32(0L, Z_NULL, 0);
entry_size = (size_t)(idx->off - entry_start);
if (crc_object(&entry->crc, &idx->pack->mwf, entry_start, entry_size) < 0)
goto on_error;
return save_entry(idx, entry, pentry, entry_start);
on_error:
git__free(pentry);
git__free(entry);
git__free(obj->data);
return -1;
}
static int do_progress_callback(git_indexer *idx, git_indexer_progress *stats)
{
if (idx->progress_cb)
return git_error_set_after_callback_function(
idx->progress_cb(stats, idx->progress_payload),
"indexer progress");
return 0;
}
/* Hash everything but the checksum trailer */
static void hash_partially(git_indexer *idx, const uint8_t *data, size_t size)
{
size_t to_expell, to_keep;
size_t oid_size = git_oid_size(idx->oid_type);
if (size == 0)
return;
/*
* Easy case, dump the buffer and the data minus the trailing
* checksum (SHA1 or SHA256).
*/
if (size >= oid_size) {
git_hash_update(&idx->trailer, idx->inbuf, idx->inbuf_len);
git_hash_update(&idx->trailer, data, size - oid_size);
data += size - oid_size;
memcpy(idx->inbuf, data, oid_size);
idx->inbuf_len = oid_size;
return;
}
/* We can just append */
if (idx->inbuf_len + size <= oid_size) {
memcpy(idx->inbuf + idx->inbuf_len, data, size);
idx->inbuf_len += size;
return;
}
/* We need to partially drain the buffer and then append */
to_keep = oid_size - size;
to_expell = idx->inbuf_len - to_keep;
git_hash_update(&idx->trailer, idx->inbuf, to_expell);
memmove(idx->inbuf, idx->inbuf + to_expell, to_keep);
memcpy(idx->inbuf + to_keep, data, size);
idx->inbuf_len += size - to_expell;
}
#if defined(NO_MMAP) || !defined(GIT_WIN32)
static int write_at(git_indexer *idx, const void *data, off64_t offset, size_t size)
{
size_t remaining_size = size;
const char *ptr = (const char *)data;
/* Handle data size larger that ssize_t */
while (remaining_size > 0) {
ssize_t nb;
HANDLE_EINTR(nb, p_pwrite(idx->pack->mwf.fd, (void *)ptr,
remaining_size, offset));
if (nb <= 0)
return -1;
ptr += nb;
offset += nb;
remaining_size -= nb;
}
return 0;
}
static int append_to_pack(git_indexer *idx, const void *data, size_t size)
{
if (write_at(idx, data, idx->pack->mwf.size, size) < 0) {
git_error_set(GIT_ERROR_OS, "cannot extend packfile '%s'", idx->pack->pack_name);
return -1;
}
return 0;
}
#else
/*
* Windows may keep different views to a networked file for the mmap- and
* open-accessed versions of a file, so any writes done through
* `write(2)`/`pwrite(2)` may not be reflected on the data that `mmap(2)` is
* able to read.
*/
static int write_at(git_indexer *idx, const void *data, off64_t offset, size_t size)
{
git_file fd = idx->pack->mwf.fd;
size_t mmap_alignment;
size_t page_offset;
off64_t page_start;
unsigned char *map_data;
git_map map;
int error;
GIT_ASSERT_ARG(data);
GIT_ASSERT_ARG(size);
if ((error = git__mmap_alignment(&mmap_alignment)) < 0)
return error;
/* the offset needs to be at the mmap boundary for the platform */
page_offset = offset % mmap_alignment;
page_start = offset - page_offset;
if ((error = p_mmap(&map, page_offset + size, GIT_PROT_WRITE, GIT_MAP_SHARED, fd, page_start)) < 0)
return error;
map_data = (unsigned char *)map.data;
memcpy(map_data + page_offset, data, size);
p_munmap(&map);
return 0;
}
static int append_to_pack(git_indexer *idx, const void *data, size_t size)
{
off64_t new_size;
size_t mmap_alignment;
size_t page_offset;
off64_t page_start;
off64_t current_size = idx->pack->mwf.size;
int error;
if (!size)
return 0;
if ((error = git__mmap_alignment(&mmap_alignment)) < 0)
return error;
/* Write a single byte to force the file system to allocate space now or
* report an error, since we can't report errors when writing using mmap.
* Round the size up to the nearest page so that we only need to perform file
* I/O when we add a page, instead of whenever we write even a single byte. */
new_size = current_size + size;
page_offset = new_size % mmap_alignment;
page_start = new_size - page_offset;
if (p_pwrite(idx->pack->mwf.fd, data, 1, page_start + mmap_alignment - 1) < 0) {
git_error_set(GIT_ERROR_OS, "cannot extend packfile '%s'", idx->pack->pack_name);
return -1;
}
return write_at(idx, data, idx->pack->mwf.size, size);
}
#endif
static int read_stream_object(git_indexer *idx, git_indexer_progress *stats)
{
git_packfile_stream *stream = &idx->stream;
off64_t entry_start = idx->off;
size_t oid_size, entry_size;
git_object_t type;
git_mwindow *w = NULL;
int error;
oid_size = git_oid_size(idx->oid_type);
if (idx->pack->mwf.size <= idx->off + (long long)oid_size)
return GIT_EBUFS;
if (!idx->have_stream) {
error = git_packfile_unpack_header(&entry_size, &type, idx->pack, &w, &idx->off);
if (error == GIT_EBUFS) {
idx->off = entry_start;
return error;
}
if (error < 0)
return error;
git_mwindow_close(&w);
idx->entry_start = entry_start;
git_hash_init(&idx->hash_ctx);
git_str_clear(&idx->entry_data);
if (type == GIT_PACKFILE_REF_DELTA || type == GIT_PACKFILE_OFS_DELTA) {
error = advance_delta_offset(idx, type);
if (error == GIT_EBUFS) {
idx->off = entry_start;
return error;
}
if (error < 0)
return error;
idx->have_delta = 1;
} else {
idx->have_delta = 0;
error = hash_header(&idx->hash_ctx, entry_size, type);
if (error < 0)
return error;
}
idx->have_stream = 1;
idx->entry_type = type;
error = git_packfile_stream_open(stream, idx->pack, idx->off);
if (error < 0)
return error;
}
if (idx->have_delta) {
error = read_object_stream(idx, stream);
} else {
error = hash_object_stream(idx, stream);
}
idx->off = stream->curpos;
if (error == GIT_EBUFS)
return error;
/* We want to free the stream reasorces no matter what here */
idx->have_stream = 0;
git_packfile_stream_dispose(stream);
if (error < 0)
return error;
if (idx->have_delta) {
error = store_delta(idx);
} else {
error = store_object(idx);
}
if (error < 0)
return error;
if (!idx->have_delta) {
stats->indexed_objects++;
}
stats->received_objects++;
if ((error = do_progress_callback(idx, stats)) != 0)
return error;
return 0;
}
int git_indexer_append(git_indexer *idx, const void *data, size_t size, git_indexer_progress *stats)
{
int error = -1;
struct git_pack_header *hdr = &idx->hdr;
git_mwindow_file *mwf = &idx->pack->mwf;
GIT_ASSERT_ARG(idx);
GIT_ASSERT_ARG(data);
GIT_ASSERT_ARG(stats);
if ((error = append_to_pack(idx, data, size)) < 0)
return error;
hash_partially(idx, data, (int)size);
/* Make sure we set the new size of the pack */
idx->pack->mwf.size += size;
if (!idx->parsed_header) {
unsigned int total_objects;
if ((unsigned)idx->pack->mwf.size < sizeof(struct git_pack_header))
return 0;
if ((error = parse_header(&idx->hdr, idx->pack)) < 0)
return error;
idx->parsed_header = 1;
idx->nr_objects = ntohl(hdr->hdr_entries);
idx->off = sizeof(struct git_pack_header);
if (idx->nr_objects <= git_indexer__max_objects) {
total_objects = (unsigned int)idx->nr_objects;
} else {
git_error_set(GIT_ERROR_INDEXER, "too many objects");
return -1;
}
idx->pack->has_cache = 1;
if (git_vector_init(&idx->objects, total_objects, objects_cmp) < 0)
return -1;
if (git_vector_init(&idx->deltas, total_objects / 2, NULL) < 0)
return -1;
stats->total_objects = total_objects;
stats->indexed_objects = 0;
stats->received_objects = 0;
stats->local_objects = 0;
stats->total_deltas = 0;
stats->indexed_deltas = 0;
stats->received_bytes = 0;
if ((error = do_progress_callback(idx, stats)) != 0)
return error;
}
/* Now that we have data in the pack, let's try to parse it */
/* As the file grows any windows we try to use will be out of date */
if ((error = git_mwindow_free_all(mwf)) < 0)
goto on_error;
while (stats->indexed_objects < idx->nr_objects) {
if ((error = read_stream_object(idx, stats)) != 0) {
if (error == GIT_EBUFS)
break;
else
goto on_error;
}
}
return 0;
on_error:
git_mwindow_free_all(mwf);
return error;
}
static int index_path(git_str *path, git_indexer *idx, const char *suffix)
{
const char prefix[] = "pack-";
size_t slash = (size_t)path->size;
/* search backwards for '/' */
while (slash > 0 && path->ptr[slash - 1] != '/')
slash--;
if (git_str_grow(path, slash + 1 + strlen(prefix) +
git_oid_hexsize(idx->oid_type) + strlen(suffix) + 1) < 0)
return -1;
git_str_truncate(path, slash);
git_str_puts(path, prefix);
git_str_puts(path, idx->name);
git_str_puts(path, suffix);
return git_str_oom(path) ? -1 : 0;
}
/**
* Rewind the packfile by the trailer, as we might need to fix the
* packfile by injecting objects at the tail and must overwrite it.
*/
static int seek_back_trailer(git_indexer *idx)
{
idx->pack->mwf.size -= git_oid_size(idx->oid_type);
return git_mwindow_free_all(&idx->pack->mwf);
}
static int inject_object(git_indexer *idx, git_oid *id)
{
git_odb_object *obj = NULL;
struct entry *entry = NULL;
struct git_pack_entry *pentry = NULL;
unsigned char empty_checksum[GIT_HASH_MAX_SIZE] = {0};
unsigned char hdr[64];
git_str buf = GIT_STR_INIT;
off64_t entry_start;
const void *data;
size_t len, hdr_len;
size_t checksum_size;