34
#define MIDX_CHUNK_LARGE_OFFSET_WIDTH (sizeof(uint64_t))
35
#define MIDX_LARGE_OFFSET_NEEDED 0x80000000
36
37
+#define PACK_EXPIRED UINT_MAX
38
+
39
static char *get_midx_filename(const char *object_dir)
40
{
41
return xstrfmt("%s/pack/multi-pack-index", object_dir);
433
uint32_t orig_pack_int_id;
434
char *pack_name;
435
struct packed_git *p;
436
+ unsigned expired : 1;
437
};
438
439
static int pack_info_compare(const void *_a, const void *_b)
481
482
packs->info[packs->nr].pack_name = xstrdup(file_name);
483
packs->info[packs->nr].orig_pack_int_id = packs->nr;
484
+ packs->info[packs->nr].expired = 0;
485
packs->nr++;
486
}
487
}
641
size_t written = 0;
642
643
for (i = 0; i < num_packs; i++) {
640
- size_t writelen = strlen(info[i].pack_name) + 1;
644
+ size_t writelen;
645
+
646
+ if (info[i].expired)
647
+ continue;
648
649
if (i && strcmp(info[i].pack_name, info[i - 1].pack_name) <= 0)
650
BUG("incorrect pack-file order: %s before %s",
651
info[i - 1].pack_name,
652
info[i].pack_name);
653
654
+ writelen = strlen(info[i].pack_name) + 1;
655
hashwrite(f, info[i].pack_name, writelen);
656
written += writelen;
657
}
733
for (i = 0; i < nr_objects; i++) {
734
struct pack_midx_entry *obj = list++;
735
736
+ if (perm[obj->pack_int_id] == PACK_EXPIRED)
737
+ BUG("object %s is in an expired pack with int-id %d",
738
+ oid_to_hex(&obj->oid),
739
+ obj->pack_int_id);
740
+
741
hashwrite_be32(f, perm[obj->pack_int_id]);
742
743
if (large_offset_needed && obj->offset >> 31)
784
return written;
785
}
786
774
-int write_midx_file(const char *object_dir)
787
+static int write_midx_internal(const char *object_dir, struct multi_pack_index *m,
788
+ struct string_list *packs_to_drop)
789
{
790
unsigned char cur_chunk, num_chunks = 0;
791
char *midx_name;
801
struct pack_midx_entry *entries = NULL;
802
int large_offsets_needed = 0;
803
int pack_name_concat_len = 0;
804
+ int dropped_packs = 0;
805
+ int result = 0;
806
807
midx_name = get_midx_filename(object_dir);
808
if (safe_create_leading_directories(midx_name)) {
811
midx_name);
812
}
813
798
- packs.m = load_multi_pack_index(object_dir, 1);
814
+ if (m)
815
+ packs.m = m;
816
+ else
817
+ packs.m = load_multi_pack_index(object_dir, 1);
818
819
packs.nr = 0;
820
packs.alloc = packs.m ? packs.m->num_packs : 16;
828
packs.info[packs.nr].orig_pack_int_id = i;
829
packs.info[packs.nr].pack_name = xstrdup(packs.m->pack_names[i]);
830
packs.info[packs.nr].p = NULL;
831
+ packs.info[packs.nr].expired = 0;
832
packs.nr++;
833
}
834
}
835
836
for_each_file_in_pack_dir(object_dir, add_pack_to_midx, &packs);
837
818
- if (packs.m && packs.nr == packs.m->num_packs)
838
+ if (packs.m && packs.nr == packs.m->num_packs && !packs_to_drop)
839
goto cleanup;
840
841
entries = get_sorted_entries(packs.m, packs.info, packs.nr, &nr_entries);
849
850
QSORT(packs.info, packs.nr, pack_info_compare);
851
852
+ if (packs_to_drop && packs_to_drop->nr) {
853
+ int drop_index = 0;
854
+ int missing_drops = 0;
855
+
856
+ for (i = 0; i < packs.nr && drop_index < packs_to_drop->nr; i++) {
857
+ int cmp = strcmp(packs.info[i].pack_name,
858
+ packs_to_drop->items[drop_index].string);
859
+
860
+ if (!cmp) {
861
+ drop_index++;
862
+ packs.info[i].expired = 1;
863
+ } else if (cmp > 0) {
864
+ error(_("did not see pack-file %s to drop"),
865
+ packs_to_drop->items[drop_index].string);
866
+ drop_index++;
867
+ missing_drops++;
868
+ i--;
869
+ } else {
870
+ packs.info[i].expired = 0;
871
+ }
872
+ }
873
+
874
+ if (missing_drops) {
875
+ result = 1;
876
+ goto cleanup;
877
+ }
878
+ }
879
+
880
/*
881
* pack_perm stores a permutation between pack-int-ids from the
882
* previous multi-pack-index to the new one we are writing:
885
*/
886
ALLOC_ARRAY(pack_perm, packs.nr);
887
for (i = 0; i < packs.nr; i++) {
840
- pack_perm[packs.info[i].orig_pack_int_id] = i;
888
+ if (packs.info[i].expired) {
889
+ dropped_packs++;
890
+ pack_perm[packs.info[i].orig_pack_int_id] = PACK_EXPIRED;
891
+ } else {
892
+ pack_perm[packs.info[i].orig_pack_int_id] = i - dropped_packs;
893
+ }
894
}
895
843
- for (i = 0; i < packs.nr; i++)
844
- pack_name_concat_len += strlen(packs.info[i].pack_name) + 1;
896
+ for (i = 0; i < packs.nr; i++) {
897
+ if (!packs.info[i].expired)
898
+ pack_name_concat_len += strlen(packs.info[i].pack_name) + 1;
899
+ }
900
901
if (pack_name_concat_len % MIDX_CHUNK_ALIGNMENT)
902
pack_name_concat_len += MIDX_CHUNK_ALIGNMENT -
912
cur_chunk = 0;
913
num_chunks = large_offsets_needed ? 5 : 4;
914
860
- written = write_midx_header(f, num_chunks, packs.nr);
915
+ written = write_midx_header(f, num_chunks, packs.nr - dropped_packs);
916
917
chunk_ids[cur_chunk] = MIDX_CHUNKID_PACKNAMES;
918
chunk_offsets[cur_chunk] = written + (num_chunks + 1) * MIDX_CHUNKLOOKUP_WIDTH;
1013
free(entries);
1014
free(pack_perm);
1015
free(midx_name);
961
- return 0;
1016
+ return result;
1017
+}
1018
+
1019
+int write_midx_file(const char *object_dir)
1020
+{
1021
+ return write_midx_internal(object_dir, NULL, NULL);
1022
}
1023
1024
void clear_midx_file(struct repository *r)
1185
1186
int expire_midx_packs(struct repository *r, const char *object_dir)
1187
{
1128
- return 0;
1188
+ uint32_t i, *count, result = 0;
1189
+ struct string_list packs_to_drop = STRING_LIST_INIT_DUP;
1190
+ struct multi_pack_index *m = load_multi_pack_index(object_dir, 1);
1191
+
1192
+ if (!m)
1193
+ return 0;
1194
+
1195
+ count = xcalloc(m->num_packs, sizeof(uint32_t));
1196
+ for (i = 0; i < m->num_objects; i++) {
1197
+ int pack_int_id = nth_midxed_pack_int_id(m, i);
1198
+ count[pack_int_id]++;
1199
+ }
1200
+
1201
+ for (i = 0; i < m->num_packs; i++) {
1202
+ char *pack_name;
1203
+
1204
+ if (count[i])
1205
+ continue;
1206
+
1207
+ if (prepare_midx_pack(r, m, i))
1208
+ continue;
1209
+
1210
+ if (m->packs[i]->pack_keep)
1211
+ continue;
1212
+
1213
+ pack_name = xstrdup(m->packs[i]->pack_name);
1214
+ close_pack(m->packs[i]);
1215
+
1216
+ string_list_insert(&packs_to_drop, m->pack_names[i]);
1217
+ unlink_pack_path(pack_name, 0);
1218
+ free(pack_name);
1219
+ }
1220
+
1221
+ free(count);
1222
+
1223
+ if (packs_to_drop.nr)
1224
+ result = write_midx_internal(object_dir, m, &packs_to_drop);
1225
+
1226
+ string_list_clear(&packs_to_drop, 0);
1227
+ return result;
1228
}