|
11 | 11 | #include "trace2.h"
|
12 | 12 | #include "run-command.h"
|
13 | 13 | #include "repository.h"
|
| 14 | +#include "chunk-format.h" |
14 | 15 |
|
15 | 16 | #define MIDX_SIGNATURE 0x4d494458 /* "MIDX" */
|
16 | 17 | #define MIDX_VERSION 1
|
|
21 | 22 | #define MIDX_HEADER_SIZE 12
|
22 | 23 | #define MIDX_MIN_SIZE (MIDX_HEADER_SIZE + the_hash_algo->rawsz)
|
23 | 24 |
|
24 |
| -#define MIDX_MAX_CHUNKS 5 |
25 | 25 | #define MIDX_CHUNK_ALIGNMENT 4
|
26 | 26 | #define MIDX_CHUNKID_PACKNAMES 0x504e414d /* "PNAM" */
|
27 | 27 | #define MIDX_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
|
@@ -799,18 +799,15 @@ static int write_midx_large_offsets(struct hashfile *f,
|
799 | 799 | static int write_midx_internal(const char *object_dir, struct multi_pack_index *m,
|
800 | 800 | struct string_list *packs_to_drop, unsigned flags)
|
801 | 801 | {
|
802 |
| - unsigned char cur_chunk, num_chunks = 0; |
803 | 802 | char *midx_name;
|
804 | 803 | uint32_t i;
|
805 | 804 | struct hashfile *f = NULL;
|
806 | 805 | struct lock_file lk;
|
807 | 806 | struct write_midx_context ctx = { 0 };
|
808 |
| - uint64_t header_size = 0; |
809 |
| - uint32_t chunk_ids[MIDX_MAX_CHUNKS + 1]; |
810 |
| - uint64_t chunk_offsets[MIDX_MAX_CHUNKS + 1]; |
811 | 807 | int pack_name_concat_len = 0;
|
812 | 808 | int dropped_packs = 0;
|
813 | 809 | int result = 0;
|
| 810 | + struct chunkfile *cf; |
814 | 811 |
|
815 | 812 | midx_name = get_midx_filename(object_dir);
|
816 | 813 | if (safe_create_leading_directories(midx_name))
|
@@ -923,98 +920,35 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *
|
923 | 920 | if (ctx.m)
|
924 | 921 | close_midx(ctx.m);
|
925 | 922 |
|
926 |
| - cur_chunk = 0; |
927 |
| - num_chunks = ctx.large_offsets_needed ? 5 : 4; |
928 |
| - |
929 | 923 | if (ctx.nr - dropped_packs == 0) {
|
930 | 924 | error(_("no pack files to index."));
|
931 | 925 | result = 1;
|
932 | 926 | goto cleanup;
|
933 | 927 | }
|
934 | 928 |
|
935 |
| - header_size = write_midx_header(f, num_chunks, ctx.nr - dropped_packs); |
936 |
| - |
937 |
| - chunk_ids[cur_chunk] = MIDX_CHUNKID_PACKNAMES; |
938 |
| - chunk_offsets[cur_chunk] = header_size + (num_chunks + 1) * MIDX_CHUNKLOOKUP_WIDTH; |
939 |
| - |
940 |
| - cur_chunk++; |
941 |
| - chunk_ids[cur_chunk] = MIDX_CHUNKID_OIDFANOUT; |
942 |
| - chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + pack_name_concat_len; |
943 |
| - |
944 |
| - cur_chunk++; |
945 |
| - chunk_ids[cur_chunk] = MIDX_CHUNKID_OIDLOOKUP; |
946 |
| - chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + MIDX_CHUNK_FANOUT_SIZE; |
947 |
| - |
948 |
| - cur_chunk++; |
949 |
| - chunk_ids[cur_chunk] = MIDX_CHUNKID_OBJECTOFFSETS; |
950 |
| - chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + ctx.entries_nr * the_hash_algo->rawsz; |
951 |
| - |
952 |
| - cur_chunk++; |
953 |
| - chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + ctx.entries_nr * MIDX_CHUNK_OFFSET_WIDTH; |
954 |
| - if (ctx.large_offsets_needed) { |
955 |
| - chunk_ids[cur_chunk] = MIDX_CHUNKID_LARGEOFFSETS; |
956 |
| - |
957 |
| - cur_chunk++; |
958 |
| - chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + |
959 |
| - ctx.num_large_offsets * MIDX_CHUNK_LARGE_OFFSET_WIDTH; |
960 |
| - } |
961 |
| - |
962 |
| - chunk_ids[cur_chunk] = 0; |
963 |
| - |
964 |
| - for (i = 0; i <= num_chunks; i++) { |
965 |
| - if (i && chunk_offsets[i] < chunk_offsets[i - 1]) |
966 |
| - BUG("incorrect chunk offsets: %"PRIu64" before %"PRIu64, |
967 |
| - chunk_offsets[i - 1], |
968 |
| - chunk_offsets[i]); |
969 |
| - |
970 |
| - if (chunk_offsets[i] % MIDX_CHUNK_ALIGNMENT) |
971 |
| - BUG("chunk offset %"PRIu64" is not properly aligned", |
972 |
| - chunk_offsets[i]); |
973 |
| - |
974 |
| - hashwrite_be32(f, chunk_ids[i]); |
975 |
| - hashwrite_be64(f, chunk_offsets[i]); |
976 |
| - } |
977 |
| - |
978 |
| - for (i = 0; i < num_chunks; i++) { |
979 |
| - if (f->total + f->offset != chunk_offsets[i]) |
980 |
| - BUG("incorrect chunk offset (%"PRIu64" != %"PRIu64") for chunk id %"PRIx32, |
981 |
| - chunk_offsets[i], |
982 |
| - f->total + f->offset, |
983 |
| - chunk_ids[i]); |
| 929 | + cf = init_chunkfile(f); |
984 | 930 |
|
985 |
| - switch (chunk_ids[i]) { |
986 |
| - case MIDX_CHUNKID_PACKNAMES: |
987 |
| - write_midx_pack_names(f, &ctx); |
988 |
| - break; |
| 931 | + add_chunk(cf, MIDX_CHUNKID_PACKNAMES, pack_name_concat_len, |
| 932 | + write_midx_pack_names); |
| 933 | + add_chunk(cf, MIDX_CHUNKID_OIDFANOUT, MIDX_CHUNK_FANOUT_SIZE, |
| 934 | + write_midx_oid_fanout); |
| 935 | + add_chunk(cf, MIDX_CHUNKID_OIDLOOKUP, |
| 936 | + ctx.entries_nr * the_hash_algo->rawsz, |
| 937 | + write_midx_oid_lookup); |
| 938 | + add_chunk(cf, MIDX_CHUNKID_OBJECTOFFSETS, |
| 939 | + ctx.entries_nr * MIDX_CHUNK_OFFSET_WIDTH, |
| 940 | + write_midx_object_offsets); |
989 | 941 |
|
990 |
| - case MIDX_CHUNKID_OIDFANOUT: |
991 |
| - write_midx_oid_fanout(f, &ctx); |
992 |
| - break; |
993 |
| - |
994 |
| - case MIDX_CHUNKID_OIDLOOKUP: |
995 |
| - write_midx_oid_lookup(f, &ctx); |
996 |
| - break; |
997 |
| - |
998 |
| - case MIDX_CHUNKID_OBJECTOFFSETS: |
999 |
| - write_midx_object_offsets(f, &ctx); |
1000 |
| - break; |
1001 |
| - |
1002 |
| - case MIDX_CHUNKID_LARGEOFFSETS: |
1003 |
| - write_midx_large_offsets(f, &ctx); |
1004 |
| - break; |
1005 |
| - |
1006 |
| - default: |
1007 |
| - BUG("trying to write unknown chunk id %"PRIx32, |
1008 |
| - chunk_ids[i]); |
1009 |
| - } |
1010 |
| - } |
| 942 | + if (ctx.large_offsets_needed) |
| 943 | + add_chunk(cf, MIDX_CHUNKID_LARGEOFFSETS, |
| 944 | + ctx.num_large_offsets * MIDX_CHUNK_LARGE_OFFSET_WIDTH, |
| 945 | + write_midx_large_offsets); |
1011 | 946 |
|
1012 |
| - if (hashfile_total(f) != chunk_offsets[num_chunks]) |
1013 |
| - BUG("incorrect final offset %"PRIu64" != %"PRIu64, |
1014 |
| - hashfile_total(f), |
1015 |
| - chunk_offsets[num_chunks]); |
| 947 | + write_midx_header(f, get_num_chunks(cf), ctx.nr - dropped_packs); |
| 948 | + write_chunkfile(cf, &ctx); |
1016 | 949 |
|
1017 | 950 | finalize_hashfile(f, NULL, CSUM_FSYNC | CSUM_HASH_IN_STREAM);
|
| 951 | + free_chunkfile(cf); |
1018 | 952 | commit_lock_file(&lk);
|
1019 | 953 |
|
1020 | 954 | cleanup:
|
|
0 commit comments