Derrick Stolee | a340773 | 2018-07-12 15:39:21 -0400 | [diff] [blame] | 1 | #include "cache.h" |
Derrick Stolee | c4d2522 | 2018-07-12 15:39:33 -0400 | [diff] [blame] | 2 | #include "config.h" |
Derrick Stolee | fc59e74 | 2018-07-12 15:39:22 -0400 | [diff] [blame] | 3 | #include "csum-file.h" |
Derrick Stolee | 396f257 | 2018-07-12 15:39:26 -0400 | [diff] [blame] | 4 | #include "dir.h" |
Derrick Stolee | fc59e74 | 2018-07-12 15:39:22 -0400 | [diff] [blame] | 5 | #include "lockfile.h" |
Derrick Stolee | 396f257 | 2018-07-12 15:39:26 -0400 | [diff] [blame] | 6 | #include "packfile.h" |
Derrick Stolee | 4d80560 | 2018-07-12 15:39:23 -0400 | [diff] [blame] | 7 | #include "object-store.h" |
Martin Ågren | bc62692 | 2020-12-31 12:56:23 +0100 | [diff] [blame] | 8 | #include "hash-lookup.h" |
Derrick Stolee | a340773 | 2018-07-12 15:39:21 -0400 | [diff] [blame] | 9 | #include "midx.h" |
Derrick Stolee | 144d703 | 2018-09-13 11:02:26 -0700 | [diff] [blame] | 10 | #include "progress.h" |
Jeff Hostetler | d829223 | 2019-03-21 12:36:13 -0700 | [diff] [blame] | 11 | #include "trace2.h" |
Derrick Stolee | ce1e4a1 | 2019-06-10 16:35:27 -0700 | [diff] [blame] | 12 | #include "run-command.h" |
Derrick Stolee | 18e449f | 2020-09-25 12:33:34 +0000 | [diff] [blame] | 13 | #include "repository.h" |
Derrick Stolee | 63a8f0e | 2021-02-18 14:07:33 +0000 | [diff] [blame] | 14 | #include "chunk-format.h" |
Taylor Blau | 38ff7ca | 2021-03-30 11:04:32 -0400 | [diff] [blame] | 15 | #include "pack.h" |
Derrick Stolee | a340773 | 2018-07-12 15:39:21 -0400 | [diff] [blame] | 16 | |
Derrick Stolee | fc59e74 | 2018-07-12 15:39:22 -0400 | [diff] [blame] | 17 | #define MIDX_SIGNATURE 0x4d494458 /* "MIDX" */ |
| 18 | #define MIDX_VERSION 1 |
Derrick Stolee | 4d80560 | 2018-07-12 15:39:23 -0400 | [diff] [blame] | 19 | #define MIDX_BYTE_FILE_VERSION 4 |
| 20 | #define MIDX_BYTE_HASH_VERSION 5 |
| 21 | #define MIDX_BYTE_NUM_CHUNKS 6 |
| 22 | #define MIDX_BYTE_NUM_PACKS 8 |
Derrick Stolee | fc59e74 | 2018-07-12 15:39:22 -0400 | [diff] [blame] | 23 | #define MIDX_HEADER_SIZE 12 |
brian m. carlson | aaa95df | 2019-08-18 20:04:27 +0000 | [diff] [blame] | 24 | #define MIDX_MIN_SIZE (MIDX_HEADER_SIZE + the_hash_algo->rawsz) |
Derrick Stolee | fc59e74 | 2018-07-12 15:39:22 -0400 | [diff] [blame] | 25 | |
Derrick Stolee | 32f3c54 | 2018-07-12 15:39:27 -0400 | [diff] [blame] | 26 | #define MIDX_CHUNK_ALIGNMENT 4 |
| 27 | #define MIDX_CHUNKID_PACKNAMES 0x504e414d /* "PNAM" */ |
Derrick Stolee | d7cacf2 | 2018-07-12 15:39:31 -0400 | [diff] [blame] | 28 | #define MIDX_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */ |
Derrick Stolee | 0d5b3a5 | 2018-07-12 15:39:30 -0400 | [diff] [blame] | 29 | #define MIDX_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */ |
Derrick Stolee | 662148c | 2018-07-12 15:39:32 -0400 | [diff] [blame] | 30 | #define MIDX_CHUNKID_OBJECTOFFSETS 0x4f4f4646 /* "OOFF" */ |
| 31 | #define MIDX_CHUNKID_LARGEOFFSETS 0x4c4f4646 /* "LOFF" */ |
Derrick Stolee | d7cacf2 | 2018-07-12 15:39:31 -0400 | [diff] [blame] | 32 | #define MIDX_CHUNK_FANOUT_SIZE (sizeof(uint32_t) * 256) |
Derrick Stolee | 662148c | 2018-07-12 15:39:32 -0400 | [diff] [blame] | 33 | #define MIDX_CHUNK_OFFSET_WIDTH (2 * sizeof(uint32_t)) |
| 34 | #define MIDX_CHUNK_LARGE_OFFSET_WIDTH (sizeof(uint64_t)) |
| 35 | #define MIDX_LARGE_OFFSET_NEEDED 0x80000000 |
Derrick Stolee | 32f3c54 | 2018-07-12 15:39:27 -0400 | [diff] [blame] | 36 | |
Derrick Stolee | 19575c7 | 2019-06-10 16:35:25 -0700 | [diff] [blame] | 37 | #define PACK_EXPIRED UINT_MAX |
| 38 | |
Derrick Stolee | d960754 | 2020-08-17 14:04:48 +0000 | [diff] [blame] | 39 | static uint8_t oid_version(void) |
| 40 | { |
| 41 | switch (hash_algo_by_ptr(the_hash_algo)) { |
| 42 | case GIT_HASH_SHA1: |
| 43 | return 1; |
| 44 | case GIT_HASH_SHA256: |
| 45 | return 2; |
| 46 | default: |
| 47 | die(_("invalid hash version")); |
| 48 | } |
| 49 | } |
| 50 | |
Taylor Blau | f894081 | 2021-03-30 11:04:26 -0400 | [diff] [blame] | 51 | static const unsigned char *get_midx_checksum(struct multi_pack_index *m) |
| 52 | { |
| 53 | return m->data + m->data_len - the_hash_algo->rawsz; |
| 54 | } |
| 55 | |
Derrick Stolee | fc59e74 | 2018-07-12 15:39:22 -0400 | [diff] [blame] | 56 | static char *get_midx_filename(const char *object_dir) |
| 57 | { |
| 58 | return xstrfmt("%s/pack/multi-pack-index", object_dir); |
| 59 | } |
| 60 | |
Taylor Blau | f894081 | 2021-03-30 11:04:26 -0400 | [diff] [blame] | 61 | char *get_midx_rev_filename(struct multi_pack_index *m) |
| 62 | { |
| 63 | return xstrfmt("%s/pack/multi-pack-index-%s.rev", |
| 64 | m->object_dir, hash_to_hex(get_midx_checksum(m))); |
| 65 | } |
| 66 | |
Derrick Stolee | 6ab3b8b | 2021-02-18 14:07:36 +0000 | [diff] [blame] | 67 | static int midx_read_oid_fanout(const unsigned char *chunk_start, |
| 68 | size_t chunk_size, void *data) |
| 69 | { |
| 70 | struct multi_pack_index *m = data; |
| 71 | m->chunk_oid_fanout = (uint32_t *)chunk_start; |
| 72 | |
| 73 | if (chunk_size != 4 * 256) { |
| 74 | error(_("multi-pack-index OID fanout is of the wrong size")); |
| 75 | return 1; |
| 76 | } |
| 77 | return 0; |
| 78 | } |
| 79 | |
Derrick Stolee | 2cf489a | 2018-08-20 16:51:55 +0000 | [diff] [blame] | 80 | struct multi_pack_index *load_multi_pack_index(const char *object_dir, int local) |
Derrick Stolee | 4d80560 | 2018-07-12 15:39:23 -0400 | [diff] [blame] | 81 | { |
| 82 | struct multi_pack_index *m = NULL; |
| 83 | int fd; |
| 84 | struct stat st; |
| 85 | size_t midx_size; |
| 86 | void *midx_map = NULL; |
| 87 | uint32_t hash_version; |
| 88 | char *midx_name = get_midx_filename(object_dir); |
Derrick Stolee | 32f3c54 | 2018-07-12 15:39:27 -0400 | [diff] [blame] | 89 | uint32_t i; |
Derrick Stolee | 3227565 | 2018-07-12 15:39:28 -0400 | [diff] [blame] | 90 | const char *cur_pack_name; |
Derrick Stolee | 6ab3b8b | 2021-02-18 14:07:36 +0000 | [diff] [blame] | 91 | struct chunkfile *cf = NULL; |
Derrick Stolee | 4d80560 | 2018-07-12 15:39:23 -0400 | [diff] [blame] | 92 | |
| 93 | fd = git_open(midx_name); |
| 94 | |
| 95 | if (fd < 0) |
| 96 | goto cleanup_fail; |
| 97 | if (fstat(fd, &st)) { |
| 98 | error_errno(_("failed to read %s"), midx_name); |
| 99 | goto cleanup_fail; |
| 100 | } |
| 101 | |
| 102 | midx_size = xsize_t(st.st_size); |
| 103 | |
| 104 | if (midx_size < MIDX_MIN_SIZE) { |
| 105 | error(_("multi-pack-index file %s is too small"), midx_name); |
| 106 | goto cleanup_fail; |
| 107 | } |
| 108 | |
| 109 | FREE_AND_NULL(midx_name); |
| 110 | |
| 111 | midx_map = xmmap(NULL, midx_size, PROT_READ, MAP_PRIVATE, fd, 0); |
Derrick Stolee | 6c7ff7c | 2020-04-24 09:17:16 -0400 | [diff] [blame] | 112 | close(fd); |
Derrick Stolee | 4d80560 | 2018-07-12 15:39:23 -0400 | [diff] [blame] | 113 | |
Denton Liu | 577314c | 2019-04-03 15:00:05 -0700 | [diff] [blame] | 114 | FLEX_ALLOC_STR(m, object_dir, object_dir); |
Derrick Stolee | 4d80560 | 2018-07-12 15:39:23 -0400 | [diff] [blame] | 115 | m->data = midx_map; |
| 116 | m->data_len = midx_size; |
Derrick Stolee | 2cf489a | 2018-08-20 16:51:55 +0000 | [diff] [blame] | 117 | m->local = local; |
Derrick Stolee | 4d80560 | 2018-07-12 15:39:23 -0400 | [diff] [blame] | 118 | |
| 119 | m->signature = get_be32(m->data); |
Derrick Stolee | 53ad040 | 2018-09-13 11:02:15 -0700 | [diff] [blame] | 120 | if (m->signature != MIDX_SIGNATURE) |
| 121 | die(_("multi-pack-index signature 0x%08x does not match signature 0x%08x"), |
Derrick Stolee | 4d80560 | 2018-07-12 15:39:23 -0400 | [diff] [blame] | 122 | m->signature, MIDX_SIGNATURE); |
Derrick Stolee | 4d80560 | 2018-07-12 15:39:23 -0400 | [diff] [blame] | 123 | |
| 124 | m->version = m->data[MIDX_BYTE_FILE_VERSION]; |
Derrick Stolee | 53ad040 | 2018-09-13 11:02:15 -0700 | [diff] [blame] | 125 | if (m->version != MIDX_VERSION) |
| 126 | die(_("multi-pack-index version %d not recognized"), |
Derrick Stolee | 4d80560 | 2018-07-12 15:39:23 -0400 | [diff] [blame] | 127 | m->version); |
Derrick Stolee | 4d80560 | 2018-07-12 15:39:23 -0400 | [diff] [blame] | 128 | |
| 129 | hash_version = m->data[MIDX_BYTE_HASH_VERSION]; |
Derrick Stolee | d960754 | 2020-08-17 14:04:48 +0000 | [diff] [blame] | 130 | if (hash_version != oid_version()) { |
| 131 | error(_("multi-pack-index hash version %u does not match version %u"), |
| 132 | hash_version, oid_version()); |
| 133 | goto cleanup_fail; |
| 134 | } |
brian m. carlson | aaa95df | 2019-08-18 20:04:27 +0000 | [diff] [blame] | 135 | m->hash_len = the_hash_algo->rawsz; |
Derrick Stolee | 4d80560 | 2018-07-12 15:39:23 -0400 | [diff] [blame] | 136 | |
| 137 | m->num_chunks = m->data[MIDX_BYTE_NUM_CHUNKS]; |
| 138 | |
| 139 | m->num_packs = get_be32(m->data + MIDX_BYTE_NUM_PACKS); |
| 140 | |
Derrick Stolee | 6ab3b8b | 2021-02-18 14:07:36 +0000 | [diff] [blame] | 141 | cf = init_chunkfile(NULL); |
Derrick Stolee | 32f3c54 | 2018-07-12 15:39:27 -0400 | [diff] [blame] | 142 | |
Derrick Stolee | 6ab3b8b | 2021-02-18 14:07:36 +0000 | [diff] [blame] | 143 | if (read_table_of_contents(cf, m->data, midx_size, |
| 144 | MIDX_HEADER_SIZE, m->num_chunks)) |
| 145 | goto cleanup_fail; |
Derrick Stolee | d3f8e21 | 2018-09-13 11:02:16 -0700 | [diff] [blame] | 146 | |
Derrick Stolee | 6ab3b8b | 2021-02-18 14:07:36 +0000 | [diff] [blame] | 147 | if (pair_chunk(cf, MIDX_CHUNKID_PACKNAMES, &m->chunk_pack_names) == CHUNK_NOT_FOUND) |
Derrick Stolee | 32f3c54 | 2018-07-12 15:39:27 -0400 | [diff] [blame] | 148 | die(_("multi-pack-index missing required pack-name chunk")); |
Derrick Stolee | 6ab3b8b | 2021-02-18 14:07:36 +0000 | [diff] [blame] | 149 | if (read_chunk(cf, MIDX_CHUNKID_OIDFANOUT, midx_read_oid_fanout, m) == CHUNK_NOT_FOUND) |
Derrick Stolee | d7cacf2 | 2018-07-12 15:39:31 -0400 | [diff] [blame] | 150 | die(_("multi-pack-index missing required OID fanout chunk")); |
Derrick Stolee | 6ab3b8b | 2021-02-18 14:07:36 +0000 | [diff] [blame] | 151 | if (pair_chunk(cf, MIDX_CHUNKID_OIDLOOKUP, &m->chunk_oid_lookup) == CHUNK_NOT_FOUND) |
Derrick Stolee | 0d5b3a5 | 2018-07-12 15:39:30 -0400 | [diff] [blame] | 152 | die(_("multi-pack-index missing required OID lookup chunk")); |
Derrick Stolee | 6ab3b8b | 2021-02-18 14:07:36 +0000 | [diff] [blame] | 153 | if (pair_chunk(cf, MIDX_CHUNKID_OBJECTOFFSETS, &m->chunk_object_offsets) == CHUNK_NOT_FOUND) |
Derrick Stolee | 662148c | 2018-07-12 15:39:32 -0400 | [diff] [blame] | 154 | die(_("multi-pack-index missing required object offsets chunk")); |
Derrick Stolee | 32f3c54 | 2018-07-12 15:39:27 -0400 | [diff] [blame] | 155 | |
Derrick Stolee | 6ab3b8b | 2021-02-18 14:07:36 +0000 | [diff] [blame] | 156 | pair_chunk(cf, MIDX_CHUNKID_LARGEOFFSETS, &m->chunk_large_offsets); |
| 157 | |
Derrick Stolee | d7cacf2 | 2018-07-12 15:39:31 -0400 | [diff] [blame] | 158 | m->num_objects = ntohl(m->chunk_oid_fanout[255]); |
| 159 | |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 160 | CALLOC_ARRAY(m->pack_names, m->num_packs); |
| 161 | CALLOC_ARRAY(m->packs, m->num_packs); |
Derrick Stolee | 3227565 | 2018-07-12 15:39:28 -0400 | [diff] [blame] | 162 | |
| 163 | cur_pack_name = (const char *)m->chunk_pack_names; |
| 164 | for (i = 0; i < m->num_packs; i++) { |
| 165 | m->pack_names[i] = cur_pack_name; |
| 166 | |
| 167 | cur_pack_name += strlen(cur_pack_name) + 1; |
| 168 | |
Derrick Stolee | 8e72a3c | 2018-09-13 11:02:18 -0700 | [diff] [blame] | 169 | if (i && strcmp(m->pack_names[i], m->pack_names[i - 1]) <= 0) |
| 170 | die(_("multi-pack-index pack names out of order: '%s' before '%s'"), |
Derrick Stolee | 3227565 | 2018-07-12 15:39:28 -0400 | [diff] [blame] | 171 | m->pack_names[i - 1], |
| 172 | m->pack_names[i]); |
Derrick Stolee | 3227565 | 2018-07-12 15:39:28 -0400 | [diff] [blame] | 173 | } |
| 174 | |
Jeff Hostetler | d829223 | 2019-03-21 12:36:13 -0700 | [diff] [blame] | 175 | trace2_data_intmax("midx", the_repository, "load/num_packs", m->num_packs); |
| 176 | trace2_data_intmax("midx", the_repository, "load/num_objects", m->num_objects); |
| 177 | |
Derrick Stolee | 4d80560 | 2018-07-12 15:39:23 -0400 | [diff] [blame] | 178 | return m; |
| 179 | |
| 180 | cleanup_fail: |
| 181 | free(m); |
| 182 | free(midx_name); |
Derrick Stolee | 6ab3b8b | 2021-02-18 14:07:36 +0000 | [diff] [blame] | 183 | free(cf); |
Derrick Stolee | 4d80560 | 2018-07-12 15:39:23 -0400 | [diff] [blame] | 184 | if (midx_map) |
| 185 | munmap(midx_map, midx_size); |
| 186 | if (0 <= fd) |
| 187 | close(fd); |
| 188 | return NULL; |
| 189 | } |
| 190 | |
Derrick Stolee | 1dcd9f2 | 2018-10-12 10:34:19 -0700 | [diff] [blame] | 191 | void close_midx(struct multi_pack_index *m) |
Derrick Stolee | a40498a | 2018-07-12 15:39:36 -0400 | [diff] [blame] | 192 | { |
| 193 | uint32_t i; |
Derrick Stolee | 1dcd9f2 | 2018-10-12 10:34:19 -0700 | [diff] [blame] | 194 | |
| 195 | if (!m) |
| 196 | return; |
| 197 | |
Derrick Stolee | a40498a | 2018-07-12 15:39:36 -0400 | [diff] [blame] | 198 | munmap((unsigned char *)m->data, m->data_len); |
Derrick Stolee | a40498a | 2018-07-12 15:39:36 -0400 | [diff] [blame] | 199 | |
| 200 | for (i = 0; i < m->num_packs; i++) { |
Derrick Stolee | af96fe3 | 2019-04-29 09:18:56 -0700 | [diff] [blame] | 201 | if (m->packs[i]) |
| 202 | m->packs[i]->multi_pack_index = 0; |
Derrick Stolee | a40498a | 2018-07-12 15:39:36 -0400 | [diff] [blame] | 203 | } |
| 204 | FREE_AND_NULL(m->packs); |
| 205 | FREE_AND_NULL(m->pack_names); |
| 206 | } |
| 207 | |
Derrick Stolee | 64404a2 | 2019-04-29 09:18:55 -0700 | [diff] [blame] | 208 | int prepare_midx_pack(struct repository *r, struct multi_pack_index *m, uint32_t pack_int_id) |
Derrick Stolee | 3715a63 | 2018-07-12 15:39:34 -0400 | [diff] [blame] | 209 | { |
| 210 | struct strbuf pack_name = STRBUF_INIT; |
Derrick Stolee | af96fe3 | 2019-04-29 09:18:56 -0700 | [diff] [blame] | 211 | struct packed_git *p; |
Derrick Stolee | 3715a63 | 2018-07-12 15:39:34 -0400 | [diff] [blame] | 212 | |
| 213 | if (pack_int_id >= m->num_packs) |
Jean-Noël Avila | d355e46 | 2018-11-28 22:43:09 +0100 | [diff] [blame] | 214 | die(_("bad pack-int-id: %u (%u total packs)"), |
Derrick Stolee | cc6af73 | 2018-09-13 11:02:25 -0700 | [diff] [blame] | 215 | pack_int_id, m->num_packs); |
Derrick Stolee | 3715a63 | 2018-07-12 15:39:34 -0400 | [diff] [blame] | 216 | |
| 217 | if (m->packs[pack_int_id]) |
| 218 | return 0; |
| 219 | |
| 220 | strbuf_addf(&pack_name, "%s/pack/%s", m->object_dir, |
| 221 | m->pack_names[pack_int_id]); |
| 222 | |
Derrick Stolee | af96fe3 | 2019-04-29 09:18:56 -0700 | [diff] [blame] | 223 | p = add_packed_git(pack_name.buf, pack_name.len, m->local); |
Derrick Stolee | 3715a63 | 2018-07-12 15:39:34 -0400 | [diff] [blame] | 224 | strbuf_release(&pack_name); |
Derrick Stolee | af96fe3 | 2019-04-29 09:18:56 -0700 | [diff] [blame] | 225 | |
| 226 | if (!p) |
| 227 | return 1; |
| 228 | |
| 229 | p->multi_pack_index = 1; |
| 230 | m->packs[pack_int_id] = p; |
| 231 | install_packed_git(r, p); |
| 232 | list_add_tail(&p->mru, &r->objects->packed_git_mru); |
| 233 | |
| 234 | return 0; |
Derrick Stolee | 3715a63 | 2018-07-12 15:39:34 -0400 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | int bsearch_midx(const struct object_id *oid, struct multi_pack_index *m, uint32_t *result) |
| 238 | { |
| 239 | return bsearch_hash(oid->hash, m->chunk_oid_fanout, m->chunk_oid_lookup, |
brian m. carlson | aaa95df | 2019-08-18 20:04:27 +0000 | [diff] [blame] | 240 | the_hash_algo->rawsz, result); |
Derrick Stolee | 3715a63 | 2018-07-12 15:39:34 -0400 | [diff] [blame] | 241 | } |
| 242 | |
Derrick Stolee | 8aac67a | 2018-07-12 15:39:35 -0400 | [diff] [blame] | 243 | struct object_id *nth_midxed_object_oid(struct object_id *oid, |
| 244 | struct multi_pack_index *m, |
| 245 | uint32_t n) |
| 246 | { |
| 247 | if (n >= m->num_objects) |
| 248 | return NULL; |
| 249 | |
| 250 | hashcpy(oid->hash, m->chunk_oid_lookup + m->hash_len * n); |
| 251 | return oid; |
| 252 | } |
| 253 | |
Taylor Blau | 62f2c1b | 2021-03-30 11:04:20 -0400 | [diff] [blame] | 254 | off_t nth_midxed_offset(struct multi_pack_index *m, uint32_t pos) |
Derrick Stolee | 3715a63 | 2018-07-12 15:39:34 -0400 | [diff] [blame] | 255 | { |
| 256 | const unsigned char *offset_data; |
| 257 | uint32_t offset32; |
| 258 | |
Derrick Stolee | 329fac3 | 2021-02-18 14:07:37 +0000 | [diff] [blame] | 259 | offset_data = m->chunk_object_offsets + (off_t)pos * MIDX_CHUNK_OFFSET_WIDTH; |
Derrick Stolee | 3715a63 | 2018-07-12 15:39:34 -0400 | [diff] [blame] | 260 | offset32 = get_be32(offset_data + sizeof(uint32_t)); |
| 261 | |
| 262 | if (m->chunk_large_offsets && offset32 & MIDX_LARGE_OFFSET_NEEDED) { |
Derrick Stolee | d8ac9ee | 2018-09-13 11:02:23 -0700 | [diff] [blame] | 263 | if (sizeof(off_t) < sizeof(uint64_t)) |
Derrick Stolee | 3715a63 | 2018-07-12 15:39:34 -0400 | [diff] [blame] | 264 | die(_("multi-pack-index stores a 64-bit offset, but off_t is too small")); |
| 265 | |
| 266 | offset32 ^= MIDX_LARGE_OFFSET_NEEDED; |
| 267 | return get_be64(m->chunk_large_offsets + sizeof(uint64_t) * offset32); |
| 268 | } |
| 269 | |
| 270 | return offset32; |
| 271 | } |
| 272 | |
Taylor Blau | 62f2c1b | 2021-03-30 11:04:20 -0400 | [diff] [blame] | 273 | uint32_t nth_midxed_pack_int_id(struct multi_pack_index *m, uint32_t pos) |
Derrick Stolee | 3715a63 | 2018-07-12 15:39:34 -0400 | [diff] [blame] | 274 | { |
Derrick Stolee | 329fac3 | 2021-02-18 14:07:37 +0000 | [diff] [blame] | 275 | return get_be32(m->chunk_object_offsets + |
| 276 | (off_t)pos * MIDX_CHUNK_OFFSET_WIDTH); |
Derrick Stolee | 3715a63 | 2018-07-12 15:39:34 -0400 | [diff] [blame] | 277 | } |
| 278 | |
Derrick Stolee | 64404a2 | 2019-04-29 09:18:55 -0700 | [diff] [blame] | 279 | static int nth_midxed_pack_entry(struct repository *r, |
| 280 | struct multi_pack_index *m, |
| 281 | struct pack_entry *e, |
| 282 | uint32_t pos) |
Derrick Stolee | 3715a63 | 2018-07-12 15:39:34 -0400 | [diff] [blame] | 283 | { |
| 284 | uint32_t pack_int_id; |
| 285 | struct packed_git *p; |
| 286 | |
| 287 | if (pos >= m->num_objects) |
| 288 | return 0; |
| 289 | |
| 290 | pack_int_id = nth_midxed_pack_int_id(m, pos); |
| 291 | |
Derrick Stolee | 64404a2 | 2019-04-29 09:18:55 -0700 | [diff] [blame] | 292 | if (prepare_midx_pack(r, m, pack_int_id)) |
Taylor Blau | 506ec2f | 2020-11-25 12:17:33 -0500 | [diff] [blame] | 293 | return 0; |
Derrick Stolee | 3715a63 | 2018-07-12 15:39:34 -0400 | [diff] [blame] | 294 | p = m->packs[pack_int_id]; |
| 295 | |
| 296 | /* |
| 297 | * We are about to tell the caller where they can locate the |
| 298 | * requested object. We better make sure the packfile is |
| 299 | * still here and can be accessed before supplying that |
| 300 | * answer, as it may have been deleted since the MIDX was |
| 301 | * loaded! |
| 302 | */ |
| 303 | if (!is_pack_valid(p)) |
| 304 | return 0; |
| 305 | |
Derrick Stolee | c39b02a | 2018-08-20 16:51:57 +0000 | [diff] [blame] | 306 | if (p->num_bad_objects) { |
| 307 | uint32_t i; |
| 308 | struct object_id oid; |
| 309 | nth_midxed_object_oid(&oid, m, pos); |
| 310 | for (i = 0; i < p->num_bad_objects; i++) |
Jeff King | e43d2dc | 2018-10-02 17:19:21 -0400 | [diff] [blame] | 311 | if (hasheq(oid.hash, |
| 312 | p->bad_object_sha1 + the_hash_algo->rawsz * i)) |
Derrick Stolee | c39b02a | 2018-08-20 16:51:57 +0000 | [diff] [blame] | 313 | return 0; |
| 314 | } |
| 315 | |
Derrick Stolee | 3715a63 | 2018-07-12 15:39:34 -0400 | [diff] [blame] | 316 | e->offset = nth_midxed_offset(m, pos); |
| 317 | e->p = p; |
| 318 | |
| 319 | return 1; |
| 320 | } |
| 321 | |
Derrick Stolee | 64404a2 | 2019-04-29 09:18:55 -0700 | [diff] [blame] | 322 | int fill_midx_entry(struct repository * r, |
| 323 | const struct object_id *oid, |
| 324 | struct pack_entry *e, |
| 325 | struct multi_pack_index *m) |
Derrick Stolee | 3715a63 | 2018-07-12 15:39:34 -0400 | [diff] [blame] | 326 | { |
| 327 | uint32_t pos; |
| 328 | |
| 329 | if (!bsearch_midx(oid, m, &pos)) |
| 330 | return 0; |
| 331 | |
Derrick Stolee | 64404a2 | 2019-04-29 09:18:55 -0700 | [diff] [blame] | 332 | return nth_midxed_pack_entry(r, m, e, pos); |
Derrick Stolee | 3715a63 | 2018-07-12 15:39:34 -0400 | [diff] [blame] | 333 | } |
| 334 | |
Jeff King | 013fd7a | 2019-04-05 14:06:04 -0400 | [diff] [blame] | 335 | /* Match "foo.idx" against either "foo.pack" _or_ "foo.idx". */ |
| 336 | static int cmp_idx_or_pack_name(const char *idx_or_pack_name, |
| 337 | const char *idx_name) |
| 338 | { |
| 339 | /* Skip past any initial matching prefix. */ |
| 340 | while (*idx_name && *idx_name == *idx_or_pack_name) { |
| 341 | idx_name++; |
| 342 | idx_or_pack_name++; |
| 343 | } |
| 344 | |
| 345 | /* |
| 346 | * If we didn't match completely, we may have matched "pack-1234." and |
| 347 | * be left with "idx" and "pack" respectively, which is also OK. We do |
| 348 | * not have to check for "idx" and "idx", because that would have been |
| 349 | * a complete match (and in that case these strcmps will be false, but |
| 350 | * we'll correctly return 0 from the final strcmp() below. |
| 351 | * |
| 352 | * Technically this matches "fooidx" and "foopack", but we'd never have |
| 353 | * such names in the first place. |
| 354 | */ |
| 355 | if (!strcmp(idx_name, "idx") && !strcmp(idx_or_pack_name, "pack")) |
| 356 | return 0; |
| 357 | |
| 358 | /* |
| 359 | * This not only checks for a complete match, but also orders based on |
| 360 | * the first non-identical character, which means our ordering will |
| 361 | * match a raw strcmp(). That makes it OK to use this to binary search |
| 362 | * a naively-sorted list. |
| 363 | */ |
| 364 | return strcmp(idx_or_pack_name, idx_name); |
| 365 | } |
| 366 | |
| 367 | int midx_contains_pack(struct multi_pack_index *m, const char *idx_or_pack_name) |
Derrick Stolee | a40498a | 2018-07-12 15:39:36 -0400 | [diff] [blame] | 368 | { |
| 369 | uint32_t first = 0, last = m->num_packs; |
| 370 | |
| 371 | while (first < last) { |
| 372 | uint32_t mid = first + (last - first) / 2; |
| 373 | const char *current; |
| 374 | int cmp; |
| 375 | |
| 376 | current = m->pack_names[mid]; |
Jeff King | 013fd7a | 2019-04-05 14:06:04 -0400 | [diff] [blame] | 377 | cmp = cmp_idx_or_pack_name(idx_or_pack_name, current); |
Derrick Stolee | a40498a | 2018-07-12 15:39:36 -0400 | [diff] [blame] | 378 | if (!cmp) |
| 379 | return 1; |
| 380 | if (cmp > 0) { |
| 381 | first = mid + 1; |
| 382 | continue; |
| 383 | } |
| 384 | last = mid; |
| 385 | } |
| 386 | |
| 387 | return 0; |
| 388 | } |
| 389 | |
Derrick Stolee | 2cf489a | 2018-08-20 16:51:55 +0000 | [diff] [blame] | 390 | int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, int local) |
Derrick Stolee | c4d2522 | 2018-07-12 15:39:33 -0400 | [diff] [blame] | 391 | { |
Derrick Stolee | 29e2016 | 2018-08-20 16:52:00 +0000 | [diff] [blame] | 392 | struct multi_pack_index *m; |
Derrick Stolee | c4d2522 | 2018-07-12 15:39:33 -0400 | [diff] [blame] | 393 | struct multi_pack_index *m_search; |
Derrick Stolee | c4d2522 | 2018-07-12 15:39:33 -0400 | [diff] [blame] | 394 | |
Derrick Stolee | 18e449f | 2020-09-25 12:33:34 +0000 | [diff] [blame] | 395 | prepare_repo_settings(r); |
| 396 | if (!r->settings.core_multi_pack_index) |
Derrick Stolee | c4d2522 | 2018-07-12 15:39:33 -0400 | [diff] [blame] | 397 | return 0; |
| 398 | |
Derrick Stolee | 29e2016 | 2018-08-20 16:52:00 +0000 | [diff] [blame] | 399 | for (m_search = r->objects->multi_pack_index; m_search; m_search = m_search->next) |
Derrick Stolee | c4d2522 | 2018-07-12 15:39:33 -0400 | [diff] [blame] | 400 | if (!strcmp(object_dir, m_search->object_dir)) |
| 401 | return 1; |
| 402 | |
Derrick Stolee | 29e2016 | 2018-08-20 16:52:00 +0000 | [diff] [blame] | 403 | m = load_multi_pack_index(object_dir, local); |
Derrick Stolee | c4d2522 | 2018-07-12 15:39:33 -0400 | [diff] [blame] | 404 | |
Derrick Stolee | 29e2016 | 2018-08-20 16:52:00 +0000 | [diff] [blame] | 405 | if (m) { |
Taylor Blau | 59552fb | 2020-08-28 16:22:13 -0400 | [diff] [blame] | 406 | struct multi_pack_index *mp = r->objects->multi_pack_index; |
| 407 | if (mp) { |
| 408 | m->next = mp->next; |
| 409 | mp->next = m; |
| 410 | } else |
| 411 | r->objects->multi_pack_index = m; |
Derrick Stolee | c4d2522 | 2018-07-12 15:39:33 -0400 | [diff] [blame] | 412 | return 1; |
| 413 | } |
| 414 | |
| 415 | return 0; |
| 416 | } |
| 417 | |
Derrick Stolee | fc59e74 | 2018-07-12 15:39:22 -0400 | [diff] [blame] | 418 | static size_t write_midx_header(struct hashfile *f, |
| 419 | unsigned char num_chunks, |
| 420 | uint32_t num_packs) |
| 421 | { |
Derrick Stolee | fc59e74 | 2018-07-12 15:39:22 -0400 | [diff] [blame] | 422 | hashwrite_be32(f, MIDX_SIGNATURE); |
René Scharfe | 014f144 | 2020-09-06 10:59:02 +0200 | [diff] [blame] | 423 | hashwrite_u8(f, MIDX_VERSION); |
| 424 | hashwrite_u8(f, oid_version()); |
| 425 | hashwrite_u8(f, num_chunks); |
| 426 | hashwrite_u8(f, 0); /* unused */ |
Derrick Stolee | fc59e74 | 2018-07-12 15:39:22 -0400 | [diff] [blame] | 427 | hashwrite_be32(f, num_packs); |
| 428 | |
| 429 | return MIDX_HEADER_SIZE; |
| 430 | } |
| 431 | |
Derrick Stolee | d01bf2e | 2019-06-10 16:35:24 -0700 | [diff] [blame] | 432 | struct pack_info { |
| 433 | uint32_t orig_pack_int_id; |
| 434 | char *pack_name; |
| 435 | struct packed_git *p; |
Derrick Stolee | 19575c7 | 2019-06-10 16:35:25 -0700 | [diff] [blame] | 436 | unsigned expired : 1; |
Derrick Stolee | d01bf2e | 2019-06-10 16:35:24 -0700 | [diff] [blame] | 437 | }; |
| 438 | |
| 439 | static int pack_info_compare(const void *_a, const void *_b) |
| 440 | { |
| 441 | struct pack_info *a = (struct pack_info *)_a; |
| 442 | struct pack_info *b = (struct pack_info *)_b; |
| 443 | return strcmp(a->pack_name, b->pack_name); |
| 444 | } |
| 445 | |
Taylor Blau | 9218c6a | 2021-03-30 11:04:11 -0400 | [diff] [blame] | 446 | static int idx_or_pack_name_cmp(const void *_va, const void *_vb) |
| 447 | { |
| 448 | const char *pack_name = _va; |
| 449 | const struct pack_info *compar = _vb; |
| 450 | |
| 451 | return cmp_idx_or_pack_name(pack_name, compar->pack_name); |
| 452 | } |
| 453 | |
Derrick Stolee | 577dc49 | 2021-02-18 14:07:26 +0000 | [diff] [blame] | 454 | struct write_midx_context { |
Derrick Stolee | d01bf2e | 2019-06-10 16:35:24 -0700 | [diff] [blame] | 455 | struct pack_info *info; |
Derrick Stolee | 396f257 | 2018-07-12 15:39:26 -0400 | [diff] [blame] | 456 | uint32_t nr; |
Derrick Stolee | d01bf2e | 2019-06-10 16:35:24 -0700 | [diff] [blame] | 457 | uint32_t alloc; |
Derrick Stolee | a40498a | 2018-07-12 15:39:36 -0400 | [diff] [blame] | 458 | struct multi_pack_index *m; |
William Baker | 840cef0 | 2019-10-21 18:39:59 +0000 | [diff] [blame] | 459 | struct progress *progress; |
| 460 | unsigned pack_paths_checked; |
Derrick Stolee | 31bda9a | 2021-02-18 14:07:28 +0000 | [diff] [blame] | 461 | |
| 462 | struct pack_midx_entry *entries; |
| 463 | uint32_t entries_nr; |
Derrick Stolee | 7a3ada1 | 2021-02-18 14:07:29 +0000 | [diff] [blame] | 464 | |
| 465 | uint32_t *pack_perm; |
Taylor Blau | 38ff7ca | 2021-03-30 11:04:32 -0400 | [diff] [blame] | 466 | uint32_t *pack_order; |
Derrick Stolee | 7a3ada1 | 2021-02-18 14:07:29 +0000 | [diff] [blame] | 467 | unsigned large_offsets_needed:1; |
Derrick Stolee | 980f525 | 2021-02-18 14:07:30 +0000 | [diff] [blame] | 468 | uint32_t num_large_offsets; |
Taylor Blau | 9218c6a | 2021-03-30 11:04:11 -0400 | [diff] [blame] | 469 | |
| 470 | int preferred_pack_idx; |
Derrick Stolee | 396f257 | 2018-07-12 15:39:26 -0400 | [diff] [blame] | 471 | }; |
| 472 | |
| 473 | static void add_pack_to_midx(const char *full_path, size_t full_path_len, |
| 474 | const char *file_name, void *data) |
| 475 | { |
Derrick Stolee | 577dc49 | 2021-02-18 14:07:26 +0000 | [diff] [blame] | 476 | struct write_midx_context *ctx = data; |
Derrick Stolee | 396f257 | 2018-07-12 15:39:26 -0400 | [diff] [blame] | 477 | |
| 478 | if (ends_with(file_name, ".idx")) { |
Derrick Stolee | 577dc49 | 2021-02-18 14:07:26 +0000 | [diff] [blame] | 479 | display_progress(ctx->progress, ++ctx->pack_paths_checked); |
| 480 | if (ctx->m && midx_contains_pack(ctx->m, file_name)) |
Derrick Stolee | a40498a | 2018-07-12 15:39:36 -0400 | [diff] [blame] | 481 | return; |
| 482 | |
Derrick Stolee | 577dc49 | 2021-02-18 14:07:26 +0000 | [diff] [blame] | 483 | ALLOC_GROW(ctx->info, ctx->nr + 1, ctx->alloc); |
Derrick Stolee | 396f257 | 2018-07-12 15:39:26 -0400 | [diff] [blame] | 484 | |
Derrick Stolee | 577dc49 | 2021-02-18 14:07:26 +0000 | [diff] [blame] | 485 | ctx->info[ctx->nr].p = add_packed_git(full_path, |
| 486 | full_path_len, |
| 487 | 0); |
Derrick Stolee | fe1ed56 | 2018-07-12 15:39:29 -0400 | [diff] [blame] | 488 | |
Derrick Stolee | 577dc49 | 2021-02-18 14:07:26 +0000 | [diff] [blame] | 489 | if (!ctx->info[ctx->nr].p) { |
Derrick Stolee | 396f257 | 2018-07-12 15:39:26 -0400 | [diff] [blame] | 490 | warning(_("failed to add packfile '%s'"), |
| 491 | full_path); |
| 492 | return; |
| 493 | } |
| 494 | |
Derrick Stolee | 577dc49 | 2021-02-18 14:07:26 +0000 | [diff] [blame] | 495 | if (open_pack_index(ctx->info[ctx->nr].p)) { |
Derrick Stolee | fe1ed56 | 2018-07-12 15:39:29 -0400 | [diff] [blame] | 496 | warning(_("failed to open pack-index '%s'"), |
| 497 | full_path); |
Derrick Stolee | 577dc49 | 2021-02-18 14:07:26 +0000 | [diff] [blame] | 498 | close_pack(ctx->info[ctx->nr].p); |
| 499 | FREE_AND_NULL(ctx->info[ctx->nr].p); |
Derrick Stolee | fe1ed56 | 2018-07-12 15:39:29 -0400 | [diff] [blame] | 500 | return; |
| 501 | } |
| 502 | |
Derrick Stolee | 577dc49 | 2021-02-18 14:07:26 +0000 | [diff] [blame] | 503 | ctx->info[ctx->nr].pack_name = xstrdup(file_name); |
| 504 | ctx->info[ctx->nr].orig_pack_int_id = ctx->nr; |
| 505 | ctx->info[ctx->nr].expired = 0; |
| 506 | ctx->nr++; |
Derrick Stolee | 396f257 | 2018-07-12 15:39:26 -0400 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | |
Derrick Stolee | fe1ed56 | 2018-07-12 15:39:29 -0400 | [diff] [blame] | 510 | struct pack_midx_entry { |
| 511 | struct object_id oid; |
| 512 | uint32_t pack_int_id; |
| 513 | time_t pack_mtime; |
| 514 | uint64_t offset; |
Taylor Blau | 9218c6a | 2021-03-30 11:04:11 -0400 | [diff] [blame] | 515 | unsigned preferred : 1; |
Derrick Stolee | fe1ed56 | 2018-07-12 15:39:29 -0400 | [diff] [blame] | 516 | }; |
| 517 | |
| 518 | static int midx_oid_compare(const void *_a, const void *_b) |
| 519 | { |
| 520 | const struct pack_midx_entry *a = (const struct pack_midx_entry *)_a; |
| 521 | const struct pack_midx_entry *b = (const struct pack_midx_entry *)_b; |
| 522 | int cmp = oidcmp(&a->oid, &b->oid); |
| 523 | |
| 524 | if (cmp) |
| 525 | return cmp; |
| 526 | |
Taylor Blau | 9218c6a | 2021-03-30 11:04:11 -0400 | [diff] [blame] | 527 | /* Sort objects in a preferred pack first when multiple copies exist. */ |
| 528 | if (a->preferred > b->preferred) |
| 529 | return -1; |
| 530 | if (a->preferred < b->preferred) |
| 531 | return 1; |
| 532 | |
Derrick Stolee | fe1ed56 | 2018-07-12 15:39:29 -0400 | [diff] [blame] | 533 | if (a->pack_mtime > b->pack_mtime) |
| 534 | return -1; |
| 535 | else if (a->pack_mtime < b->pack_mtime) |
| 536 | return 1; |
| 537 | |
| 538 | return a->pack_int_id - b->pack_int_id; |
| 539 | } |
| 540 | |
Derrick Stolee | a40498a | 2018-07-12 15:39:36 -0400 | [diff] [blame] | 541 | static int nth_midxed_pack_midx_entry(struct multi_pack_index *m, |
Derrick Stolee | a40498a | 2018-07-12 15:39:36 -0400 | [diff] [blame] | 542 | struct pack_midx_entry *e, |
| 543 | uint32_t pos) |
| 544 | { |
| 545 | if (pos >= m->num_objects) |
| 546 | return 1; |
| 547 | |
| 548 | nth_midxed_object_oid(&e->oid, m, pos); |
Derrick Stolee | d01bf2e | 2019-06-10 16:35:24 -0700 | [diff] [blame] | 549 | e->pack_int_id = nth_midxed_pack_int_id(m, pos); |
Derrick Stolee | a40498a | 2018-07-12 15:39:36 -0400 | [diff] [blame] | 550 | e->offset = nth_midxed_offset(m, pos); |
| 551 | |
| 552 | /* consider objects in midx to be from "old" packs */ |
| 553 | e->pack_mtime = 0; |
| 554 | return 0; |
| 555 | } |
| 556 | |
Derrick Stolee | fe1ed56 | 2018-07-12 15:39:29 -0400 | [diff] [blame] | 557 | static void fill_pack_entry(uint32_t pack_int_id, |
| 558 | struct packed_git *p, |
| 559 | uint32_t cur_object, |
Taylor Blau | 9218c6a | 2021-03-30 11:04:11 -0400 | [diff] [blame] | 560 | struct pack_midx_entry *entry, |
| 561 | int preferred) |
Derrick Stolee | fe1ed56 | 2018-07-12 15:39:29 -0400 | [diff] [blame] | 562 | { |
Jeff King | 0763671 | 2020-02-23 23:27:36 -0500 | [diff] [blame] | 563 | if (nth_packed_object_id(&entry->oid, p, cur_object) < 0) |
Derrick Stolee | fe1ed56 | 2018-07-12 15:39:29 -0400 | [diff] [blame] | 564 | die(_("failed to locate object %d in packfile"), cur_object); |
| 565 | |
| 566 | entry->pack_int_id = pack_int_id; |
| 567 | entry->pack_mtime = p->mtime; |
| 568 | |
| 569 | entry->offset = nth_packed_object_offset(p, cur_object); |
Taylor Blau | 9218c6a | 2021-03-30 11:04:11 -0400 | [diff] [blame] | 570 | entry->preferred = !!preferred; |
Derrick Stolee | fe1ed56 | 2018-07-12 15:39:29 -0400 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | /* |
| 574 | * It is possible to artificially get into a state where there are many |
| 575 | * duplicate copies of objects. That can create high memory pressure if |
| 576 | * we are to create a list of all objects before de-duplication. To reduce |
| 577 | * this memory pressure without a significant performance drop, automatically |
| 578 | * group objects by the first byte of their object id. Use the IDX fanout |
| 579 | * tables to group the data, copy to a local array, then sort. |
| 580 | * |
| 581 | * Copy only the de-duplicated entries (selected by most-recent modified time |
| 582 | * of a packfile containing the object). |
| 583 | */ |
Derrick Stolee | a40498a | 2018-07-12 15:39:36 -0400 | [diff] [blame] | 584 | static struct pack_midx_entry *get_sorted_entries(struct multi_pack_index *m, |
Derrick Stolee | d01bf2e | 2019-06-10 16:35:24 -0700 | [diff] [blame] | 585 | struct pack_info *info, |
Derrick Stolee | fe1ed56 | 2018-07-12 15:39:29 -0400 | [diff] [blame] | 586 | uint32_t nr_packs, |
Taylor Blau | 9218c6a | 2021-03-30 11:04:11 -0400 | [diff] [blame] | 587 | uint32_t *nr_objects, |
| 588 | int preferred_pack) |
Derrick Stolee | fe1ed56 | 2018-07-12 15:39:29 -0400 | [diff] [blame] | 589 | { |
| 590 | uint32_t cur_fanout, cur_pack, cur_object; |
| 591 | uint32_t alloc_fanout, alloc_objects, total_objects = 0; |
| 592 | struct pack_midx_entry *entries_by_fanout = NULL; |
| 593 | struct pack_midx_entry *deduplicated_entries = NULL; |
Derrick Stolee | a40498a | 2018-07-12 15:39:36 -0400 | [diff] [blame] | 594 | uint32_t start_pack = m ? m->num_packs : 0; |
Derrick Stolee | fe1ed56 | 2018-07-12 15:39:29 -0400 | [diff] [blame] | 595 | |
Derrick Stolee | a40498a | 2018-07-12 15:39:36 -0400 | [diff] [blame] | 596 | for (cur_pack = start_pack; cur_pack < nr_packs; cur_pack++) |
Derrick Stolee | d01bf2e | 2019-06-10 16:35:24 -0700 | [diff] [blame] | 597 | total_objects += info[cur_pack].p->num_objects; |
Derrick Stolee | fe1ed56 | 2018-07-12 15:39:29 -0400 | [diff] [blame] | 598 | |
| 599 | /* |
| 600 | * As we de-duplicate by fanout value, we expect the fanout |
| 601 | * slices to be evenly distributed, with some noise. Hence, |
| 602 | * allocate slightly more than one 256th. |
| 603 | */ |
| 604 | alloc_objects = alloc_fanout = total_objects > 3200 ? total_objects / 200 : 16; |
| 605 | |
| 606 | ALLOC_ARRAY(entries_by_fanout, alloc_fanout); |
| 607 | ALLOC_ARRAY(deduplicated_entries, alloc_objects); |
| 608 | *nr_objects = 0; |
| 609 | |
| 610 | for (cur_fanout = 0; cur_fanout < 256; cur_fanout++) { |
| 611 | uint32_t nr_fanout = 0; |
| 612 | |
Derrick Stolee | a40498a | 2018-07-12 15:39:36 -0400 | [diff] [blame] | 613 | if (m) { |
| 614 | uint32_t start = 0, end; |
| 615 | |
| 616 | if (cur_fanout) |
| 617 | start = ntohl(m->chunk_oid_fanout[cur_fanout - 1]); |
| 618 | end = ntohl(m->chunk_oid_fanout[cur_fanout]); |
| 619 | |
| 620 | for (cur_object = start; cur_object < end; cur_object++) { |
| 621 | ALLOC_GROW(entries_by_fanout, nr_fanout + 1, alloc_fanout); |
Derrick Stolee | d01bf2e | 2019-06-10 16:35:24 -0700 | [diff] [blame] | 622 | nth_midxed_pack_midx_entry(m, |
Derrick Stolee | a40498a | 2018-07-12 15:39:36 -0400 | [diff] [blame] | 623 | &entries_by_fanout[nr_fanout], |
| 624 | cur_object); |
Taylor Blau | 9218c6a | 2021-03-30 11:04:11 -0400 | [diff] [blame] | 625 | if (nth_midxed_pack_int_id(m, cur_object) == preferred_pack) |
| 626 | entries_by_fanout[nr_fanout].preferred = 1; |
| 627 | else |
| 628 | entries_by_fanout[nr_fanout].preferred = 0; |
Derrick Stolee | a40498a | 2018-07-12 15:39:36 -0400 | [diff] [blame] | 629 | nr_fanout++; |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | for (cur_pack = start_pack; cur_pack < nr_packs; cur_pack++) { |
Derrick Stolee | fe1ed56 | 2018-07-12 15:39:29 -0400 | [diff] [blame] | 634 | uint32_t start = 0, end; |
Taylor Blau | 9218c6a | 2021-03-30 11:04:11 -0400 | [diff] [blame] | 635 | int preferred = cur_pack == preferred_pack; |
Derrick Stolee | fe1ed56 | 2018-07-12 15:39:29 -0400 | [diff] [blame] | 636 | |
| 637 | if (cur_fanout) |
Derrick Stolee | d01bf2e | 2019-06-10 16:35:24 -0700 | [diff] [blame] | 638 | start = get_pack_fanout(info[cur_pack].p, cur_fanout - 1); |
| 639 | end = get_pack_fanout(info[cur_pack].p, cur_fanout); |
Derrick Stolee | fe1ed56 | 2018-07-12 15:39:29 -0400 | [diff] [blame] | 640 | |
| 641 | for (cur_object = start; cur_object < end; cur_object++) { |
| 642 | ALLOC_GROW(entries_by_fanout, nr_fanout + 1, alloc_fanout); |
Taylor Blau | 9218c6a | 2021-03-30 11:04:11 -0400 | [diff] [blame] | 643 | fill_pack_entry(cur_pack, |
| 644 | info[cur_pack].p, |
| 645 | cur_object, |
| 646 | &entries_by_fanout[nr_fanout], |
| 647 | preferred); |
Derrick Stolee | fe1ed56 | 2018-07-12 15:39:29 -0400 | [diff] [blame] | 648 | nr_fanout++; |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | QSORT(entries_by_fanout, nr_fanout, midx_oid_compare); |
| 653 | |
| 654 | /* |
| 655 | * The batch is now sorted by OID and then mtime (descending). |
| 656 | * Take only the first duplicate. |
| 657 | */ |
| 658 | for (cur_object = 0; cur_object < nr_fanout; cur_object++) { |
Jeff King | e43d2dc | 2018-10-02 17:19:21 -0400 | [diff] [blame] | 659 | if (cur_object && oideq(&entries_by_fanout[cur_object - 1].oid, |
| 660 | &entries_by_fanout[cur_object].oid)) |
Derrick Stolee | fe1ed56 | 2018-07-12 15:39:29 -0400 | [diff] [blame] | 661 | continue; |
| 662 | |
| 663 | ALLOC_GROW(deduplicated_entries, *nr_objects + 1, alloc_objects); |
| 664 | memcpy(&deduplicated_entries[*nr_objects], |
| 665 | &entries_by_fanout[cur_object], |
| 666 | sizeof(struct pack_midx_entry)); |
| 667 | (*nr_objects)++; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | free(entries_by_fanout); |
| 672 | return deduplicated_entries; |
| 673 | } |
| 674 | |
Derrick Stolee | 0ccd713 | 2021-02-18 14:07:31 +0000 | [diff] [blame] | 675 | static int write_midx_pack_names(struct hashfile *f, void *data) |
Derrick Stolee | 32f3c54 | 2018-07-12 15:39:27 -0400 | [diff] [blame] | 676 | { |
Derrick Stolee | b4d9414 | 2021-02-18 14:07:27 +0000 | [diff] [blame] | 677 | struct write_midx_context *ctx = data; |
Derrick Stolee | 32f3c54 | 2018-07-12 15:39:27 -0400 | [diff] [blame] | 678 | uint32_t i; |
| 679 | unsigned char padding[MIDX_CHUNK_ALIGNMENT]; |
| 680 | size_t written = 0; |
| 681 | |
Derrick Stolee | b4d9414 | 2021-02-18 14:07:27 +0000 | [diff] [blame] | 682 | for (i = 0; i < ctx->nr; i++) { |
Derrick Stolee | 19575c7 | 2019-06-10 16:35:25 -0700 | [diff] [blame] | 683 | size_t writelen; |
| 684 | |
Derrick Stolee | b4d9414 | 2021-02-18 14:07:27 +0000 | [diff] [blame] | 685 | if (ctx->info[i].expired) |
Derrick Stolee | 19575c7 | 2019-06-10 16:35:25 -0700 | [diff] [blame] | 686 | continue; |
Derrick Stolee | 32f3c54 | 2018-07-12 15:39:27 -0400 | [diff] [blame] | 687 | |
Derrick Stolee | b4d9414 | 2021-02-18 14:07:27 +0000 | [diff] [blame] | 688 | if (i && strcmp(ctx->info[i].pack_name, ctx->info[i - 1].pack_name) <= 0) |
Derrick Stolee | 32f3c54 | 2018-07-12 15:39:27 -0400 | [diff] [blame] | 689 | BUG("incorrect pack-file order: %s before %s", |
Derrick Stolee | b4d9414 | 2021-02-18 14:07:27 +0000 | [diff] [blame] | 690 | ctx->info[i - 1].pack_name, |
| 691 | ctx->info[i].pack_name); |
Derrick Stolee | 32f3c54 | 2018-07-12 15:39:27 -0400 | [diff] [blame] | 692 | |
Derrick Stolee | b4d9414 | 2021-02-18 14:07:27 +0000 | [diff] [blame] | 693 | writelen = strlen(ctx->info[i].pack_name) + 1; |
| 694 | hashwrite(f, ctx->info[i].pack_name, writelen); |
Derrick Stolee | 32f3c54 | 2018-07-12 15:39:27 -0400 | [diff] [blame] | 695 | written += writelen; |
| 696 | } |
| 697 | |
| 698 | /* add padding to be aligned */ |
| 699 | i = MIDX_CHUNK_ALIGNMENT - (written % MIDX_CHUNK_ALIGNMENT); |
| 700 | if (i < MIDX_CHUNK_ALIGNMENT) { |
| 701 | memset(padding, 0, sizeof(padding)); |
| 702 | hashwrite(f, padding, i); |
Derrick Stolee | 32f3c54 | 2018-07-12 15:39:27 -0400 | [diff] [blame] | 703 | } |
| 704 | |
Derrick Stolee | 0ccd713 | 2021-02-18 14:07:31 +0000 | [diff] [blame] | 705 | return 0; |
Derrick Stolee | 32f3c54 | 2018-07-12 15:39:27 -0400 | [diff] [blame] | 706 | } |
| 707 | |
Derrick Stolee | 0ccd713 | 2021-02-18 14:07:31 +0000 | [diff] [blame] | 708 | static int write_midx_oid_fanout(struct hashfile *f, |
| 709 | void *data) |
Derrick Stolee | d7cacf2 | 2018-07-12 15:39:31 -0400 | [diff] [blame] | 710 | { |
Derrick Stolee | 31bda9a | 2021-02-18 14:07:28 +0000 | [diff] [blame] | 711 | struct write_midx_context *ctx = data; |
| 712 | struct pack_midx_entry *list = ctx->entries; |
| 713 | struct pack_midx_entry *last = ctx->entries + ctx->entries_nr; |
Derrick Stolee | d7cacf2 | 2018-07-12 15:39:31 -0400 | [diff] [blame] | 714 | uint32_t count = 0; |
| 715 | uint32_t i; |
| 716 | |
| 717 | /* |
| 718 | * Write the first-level table (the list is sorted, |
| 719 | * but we use a 256-entry lookup to be able to avoid |
| 720 | * having to do eight extra binary search iterations). |
| 721 | */ |
| 722 | for (i = 0; i < 256; i++) { |
| 723 | struct pack_midx_entry *next = list; |
| 724 | |
| 725 | while (next < last && next->oid.hash[0] == i) { |
| 726 | count++; |
| 727 | next++; |
| 728 | } |
| 729 | |
| 730 | hashwrite_be32(f, count); |
| 731 | list = next; |
| 732 | } |
| 733 | |
Derrick Stolee | 0ccd713 | 2021-02-18 14:07:31 +0000 | [diff] [blame] | 734 | return 0; |
Derrick Stolee | d7cacf2 | 2018-07-12 15:39:31 -0400 | [diff] [blame] | 735 | } |
| 736 | |
Derrick Stolee | 0ccd713 | 2021-02-18 14:07:31 +0000 | [diff] [blame] | 737 | static int write_midx_oid_lookup(struct hashfile *f, |
| 738 | void *data) |
Derrick Stolee | 0d5b3a5 | 2018-07-12 15:39:30 -0400 | [diff] [blame] | 739 | { |
Derrick Stolee | 31bda9a | 2021-02-18 14:07:28 +0000 | [diff] [blame] | 740 | struct write_midx_context *ctx = data; |
| 741 | unsigned char hash_len = the_hash_algo->rawsz; |
| 742 | struct pack_midx_entry *list = ctx->entries; |
Derrick Stolee | 0d5b3a5 | 2018-07-12 15:39:30 -0400 | [diff] [blame] | 743 | uint32_t i; |
Derrick Stolee | 0d5b3a5 | 2018-07-12 15:39:30 -0400 | [diff] [blame] | 744 | |
Derrick Stolee | 31bda9a | 2021-02-18 14:07:28 +0000 | [diff] [blame] | 745 | for (i = 0; i < ctx->entries_nr; i++) { |
Derrick Stolee | 0d5b3a5 | 2018-07-12 15:39:30 -0400 | [diff] [blame] | 746 | struct pack_midx_entry *obj = list++; |
| 747 | |
Derrick Stolee | 31bda9a | 2021-02-18 14:07:28 +0000 | [diff] [blame] | 748 | if (i < ctx->entries_nr - 1) { |
Derrick Stolee | 0d5b3a5 | 2018-07-12 15:39:30 -0400 | [diff] [blame] | 749 | struct pack_midx_entry *next = list; |
| 750 | if (oidcmp(&obj->oid, &next->oid) >= 0) |
| 751 | BUG("OIDs not in order: %s >= %s", |
| 752 | oid_to_hex(&obj->oid), |
| 753 | oid_to_hex(&next->oid)); |
| 754 | } |
| 755 | |
| 756 | hashwrite(f, obj->oid.hash, (int)hash_len); |
Derrick Stolee | 0d5b3a5 | 2018-07-12 15:39:30 -0400 | [diff] [blame] | 757 | } |
| 758 | |
Derrick Stolee | 0ccd713 | 2021-02-18 14:07:31 +0000 | [diff] [blame] | 759 | return 0; |
Derrick Stolee | 0d5b3a5 | 2018-07-12 15:39:30 -0400 | [diff] [blame] | 760 | } |
| 761 | |
Derrick Stolee | 0ccd713 | 2021-02-18 14:07:31 +0000 | [diff] [blame] | 762 | static int write_midx_object_offsets(struct hashfile *f, |
| 763 | void *data) |
Derrick Stolee | 662148c | 2018-07-12 15:39:32 -0400 | [diff] [blame] | 764 | { |
Derrick Stolee | 7a3ada1 | 2021-02-18 14:07:29 +0000 | [diff] [blame] | 765 | struct write_midx_context *ctx = data; |
| 766 | struct pack_midx_entry *list = ctx->entries; |
Derrick Stolee | 662148c | 2018-07-12 15:39:32 -0400 | [diff] [blame] | 767 | uint32_t i, nr_large_offset = 0; |
Derrick Stolee | 662148c | 2018-07-12 15:39:32 -0400 | [diff] [blame] | 768 | |
Derrick Stolee | 7a3ada1 | 2021-02-18 14:07:29 +0000 | [diff] [blame] | 769 | for (i = 0; i < ctx->entries_nr; i++) { |
Derrick Stolee | 662148c | 2018-07-12 15:39:32 -0400 | [diff] [blame] | 770 | struct pack_midx_entry *obj = list++; |
| 771 | |
Derrick Stolee | 7a3ada1 | 2021-02-18 14:07:29 +0000 | [diff] [blame] | 772 | if (ctx->pack_perm[obj->pack_int_id] == PACK_EXPIRED) |
Derrick Stolee | 19575c7 | 2019-06-10 16:35:25 -0700 | [diff] [blame] | 773 | BUG("object %s is in an expired pack with int-id %d", |
| 774 | oid_to_hex(&obj->oid), |
| 775 | obj->pack_int_id); |
| 776 | |
Derrick Stolee | 7a3ada1 | 2021-02-18 14:07:29 +0000 | [diff] [blame] | 777 | hashwrite_be32(f, ctx->pack_perm[obj->pack_int_id]); |
Derrick Stolee | 662148c | 2018-07-12 15:39:32 -0400 | [diff] [blame] | 778 | |
Derrick Stolee | 7a3ada1 | 2021-02-18 14:07:29 +0000 | [diff] [blame] | 779 | if (ctx->large_offsets_needed && obj->offset >> 31) |
Derrick Stolee | 662148c | 2018-07-12 15:39:32 -0400 | [diff] [blame] | 780 | hashwrite_be32(f, MIDX_LARGE_OFFSET_NEEDED | nr_large_offset++); |
Derrick Stolee | 7a3ada1 | 2021-02-18 14:07:29 +0000 | [diff] [blame] | 781 | else if (!ctx->large_offsets_needed && obj->offset >> 32) |
Derrick Stolee | 662148c | 2018-07-12 15:39:32 -0400 | [diff] [blame] | 782 | BUG("object %s requires a large offset (%"PRIx64") but the MIDX is not writing large offsets!", |
| 783 | oid_to_hex(&obj->oid), |
| 784 | obj->offset); |
| 785 | else |
| 786 | hashwrite_be32(f, (uint32_t)obj->offset); |
Derrick Stolee | 662148c | 2018-07-12 15:39:32 -0400 | [diff] [blame] | 787 | } |
| 788 | |
Derrick Stolee | 0ccd713 | 2021-02-18 14:07:31 +0000 | [diff] [blame] | 789 | return 0; |
Derrick Stolee | 662148c | 2018-07-12 15:39:32 -0400 | [diff] [blame] | 790 | } |
| 791 | |
Derrick Stolee | 0ccd713 | 2021-02-18 14:07:31 +0000 | [diff] [blame] | 792 | static int write_midx_large_offsets(struct hashfile *f, |
| 793 | void *data) |
Derrick Stolee | 662148c | 2018-07-12 15:39:32 -0400 | [diff] [blame] | 794 | { |
Derrick Stolee | 980f525 | 2021-02-18 14:07:30 +0000 | [diff] [blame] | 795 | struct write_midx_context *ctx = data; |
| 796 | struct pack_midx_entry *list = ctx->entries; |
| 797 | struct pack_midx_entry *end = ctx->entries + ctx->entries_nr; |
Derrick Stolee | 980f525 | 2021-02-18 14:07:30 +0000 | [diff] [blame] | 798 | uint32_t nr_large_offset = ctx->num_large_offsets; |
Derrick Stolee | 662148c | 2018-07-12 15:39:32 -0400 | [diff] [blame] | 799 | |
| 800 | while (nr_large_offset) { |
Jeff King | 61b0fcb | 2018-11-03 22:27:46 -0400 | [diff] [blame] | 801 | struct pack_midx_entry *obj; |
| 802 | uint64_t offset; |
| 803 | |
| 804 | if (list >= end) |
| 805 | BUG("too many large-offset objects"); |
| 806 | |
| 807 | obj = list++; |
| 808 | offset = obj->offset; |
Derrick Stolee | 662148c | 2018-07-12 15:39:32 -0400 | [diff] [blame] | 809 | |
| 810 | if (!(offset >> 31)) |
| 811 | continue; |
| 812 | |
Derrick Stolee | 0ccd713 | 2021-02-18 14:07:31 +0000 | [diff] [blame] | 813 | hashwrite_be64(f, offset); |
Derrick Stolee | 662148c | 2018-07-12 15:39:32 -0400 | [diff] [blame] | 814 | |
| 815 | nr_large_offset--; |
| 816 | } |
| 817 | |
Derrick Stolee | 0ccd713 | 2021-02-18 14:07:31 +0000 | [diff] [blame] | 818 | return 0; |
Derrick Stolee | 662148c | 2018-07-12 15:39:32 -0400 | [diff] [blame] | 819 | } |
| 820 | |
Jeff King | 3007752 | 2021-03-30 11:04:36 -0400 | [diff] [blame] | 821 | struct midx_pack_order_data { |
| 822 | uint32_t nr; |
| 823 | uint32_t pack; |
| 824 | off_t offset; |
| 825 | }; |
| 826 | |
| 827 | static int midx_pack_order_cmp(const void *va, const void *vb) |
Taylor Blau | 38ff7ca | 2021-03-30 11:04:32 -0400 | [diff] [blame] | 828 | { |
Jeff King | 3007752 | 2021-03-30 11:04:36 -0400 | [diff] [blame] | 829 | const struct midx_pack_order_data *a = va, *b = vb; |
| 830 | if (a->pack < b->pack) |
Taylor Blau | 38ff7ca | 2021-03-30 11:04:32 -0400 | [diff] [blame] | 831 | return -1; |
Jeff King | 3007752 | 2021-03-30 11:04:36 -0400 | [diff] [blame] | 832 | else if (a->pack > b->pack) |
Taylor Blau | 38ff7ca | 2021-03-30 11:04:32 -0400 | [diff] [blame] | 833 | return 1; |
Jeff King | 3007752 | 2021-03-30 11:04:36 -0400 | [diff] [blame] | 834 | else if (a->offset < b->offset) |
Taylor Blau | 38ff7ca | 2021-03-30 11:04:32 -0400 | [diff] [blame] | 835 | return -1; |
Jeff King | 3007752 | 2021-03-30 11:04:36 -0400 | [diff] [blame] | 836 | else if (a->offset > b->offset) |
Taylor Blau | 38ff7ca | 2021-03-30 11:04:32 -0400 | [diff] [blame] | 837 | return 1; |
Jeff King | 3007752 | 2021-03-30 11:04:36 -0400 | [diff] [blame] | 838 | else |
| 839 | return 0; |
Taylor Blau | 38ff7ca | 2021-03-30 11:04:32 -0400 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | static uint32_t *midx_pack_order(struct write_midx_context *ctx) |
| 843 | { |
Jeff King | 3007752 | 2021-03-30 11:04:36 -0400 | [diff] [blame] | 844 | struct midx_pack_order_data *data; |
Taylor Blau | 38ff7ca | 2021-03-30 11:04:32 -0400 | [diff] [blame] | 845 | uint32_t *pack_order; |
| 846 | uint32_t i; |
| 847 | |
Jeff King | 3007752 | 2021-03-30 11:04:36 -0400 | [diff] [blame] | 848 | ALLOC_ARRAY(data, ctx->entries_nr); |
| 849 | for (i = 0; i < ctx->entries_nr; i++) { |
| 850 | struct pack_midx_entry *e = &ctx->entries[i]; |
| 851 | data[i].nr = i; |
| 852 | data[i].pack = ctx->pack_perm[e->pack_int_id]; |
| 853 | if (!e->preferred) |
| 854 | data[i].pack |= (1U << 31); |
| 855 | data[i].offset = e->offset; |
| 856 | } |
| 857 | |
| 858 | QSORT(data, ctx->entries_nr, midx_pack_order_cmp); |
| 859 | |
Taylor Blau | 38ff7ca | 2021-03-30 11:04:32 -0400 | [diff] [blame] | 860 | ALLOC_ARRAY(pack_order, ctx->entries_nr); |
| 861 | for (i = 0; i < ctx->entries_nr; i++) |
Jeff King | 3007752 | 2021-03-30 11:04:36 -0400 | [diff] [blame] | 862 | pack_order[i] = data[i].nr; |
| 863 | free(data); |
Taylor Blau | 38ff7ca | 2021-03-30 11:04:32 -0400 | [diff] [blame] | 864 | |
| 865 | return pack_order; |
| 866 | } |
| 867 | |
| 868 | static void write_midx_reverse_index(char *midx_name, unsigned char *midx_hash, |
| 869 | struct write_midx_context *ctx) |
| 870 | { |
| 871 | struct strbuf buf = STRBUF_INIT; |
| 872 | const char *tmp_file; |
| 873 | |
| 874 | strbuf_addf(&buf, "%s-%s.rev", midx_name, hash_to_hex(midx_hash)); |
| 875 | |
| 876 | tmp_file = write_rev_file_order(NULL, ctx->pack_order, ctx->entries_nr, |
| 877 | midx_hash, WRITE_REV); |
| 878 | |
| 879 | if (finalize_object_file(tmp_file, buf.buf)) |
| 880 | die(_("cannot store reverse index file")); |
| 881 | |
| 882 | strbuf_release(&buf); |
| 883 | } |
| 884 | |
| 885 | static void clear_midx_files_ext(struct repository *r, const char *ext, |
| 886 | unsigned char *keep_hash); |
| 887 | |
Derrick Stolee | 19575c7 | 2019-06-10 16:35:25 -0700 | [diff] [blame] | 888 | static int write_midx_internal(const char *object_dir, struct multi_pack_index *m, |
Taylor Blau | 9218c6a | 2021-03-30 11:04:11 -0400 | [diff] [blame] | 889 | struct string_list *packs_to_drop, |
| 890 | const char *preferred_pack_name, |
| 891 | unsigned flags) |
Derrick Stolee | a340773 | 2018-07-12 15:39:21 -0400 | [diff] [blame] | 892 | { |
Derrick Stolee | fc59e74 | 2018-07-12 15:39:22 -0400 | [diff] [blame] | 893 | char *midx_name; |
Taylor Blau | 9f19161 | 2021-03-30 11:04:17 -0400 | [diff] [blame] | 894 | unsigned char midx_hash[GIT_MAX_RAWSZ]; |
Derrick Stolee | 396f257 | 2018-07-12 15:39:26 -0400 | [diff] [blame] | 895 | uint32_t i; |
Derrick Stolee | fc59e74 | 2018-07-12 15:39:22 -0400 | [diff] [blame] | 896 | struct hashfile *f = NULL; |
| 897 | struct lock_file lk; |
Derrick Stolee | 577dc49 | 2021-02-18 14:07:26 +0000 | [diff] [blame] | 898 | struct write_midx_context ctx = { 0 }; |
Derrick Stolee | dba6175 | 2019-06-10 16:35:24 -0700 | [diff] [blame] | 899 | int pack_name_concat_len = 0; |
Derrick Stolee | 19575c7 | 2019-06-10 16:35:25 -0700 | [diff] [blame] | 900 | int dropped_packs = 0; |
| 901 | int result = 0; |
Derrick Stolee | 63a8f0e | 2021-02-18 14:07:33 +0000 | [diff] [blame] | 902 | struct chunkfile *cf; |
Derrick Stolee | fc59e74 | 2018-07-12 15:39:22 -0400 | [diff] [blame] | 903 | |
| 904 | midx_name = get_midx_filename(object_dir); |
Jeff King | d5e1961 | 2020-08-13 11:55:00 -0400 | [diff] [blame] | 905 | if (safe_create_leading_directories(midx_name)) |
Derrick Stolee | fc59e74 | 2018-07-12 15:39:22 -0400 | [diff] [blame] | 906 | die_errno(_("unable to create leading directories of %s"), |
| 907 | midx_name); |
Derrick Stolee | fc59e74 | 2018-07-12 15:39:22 -0400 | [diff] [blame] | 908 | |
Derrick Stolee | 19575c7 | 2019-06-10 16:35:25 -0700 | [diff] [blame] | 909 | if (m) |
Derrick Stolee | 577dc49 | 2021-02-18 14:07:26 +0000 | [diff] [blame] | 910 | ctx.m = m; |
Derrick Stolee | 19575c7 | 2019-06-10 16:35:25 -0700 | [diff] [blame] | 911 | else |
Derrick Stolee | 577dc49 | 2021-02-18 14:07:26 +0000 | [diff] [blame] | 912 | ctx.m = load_multi_pack_index(object_dir, 1); |
Derrick Stolee | a40498a | 2018-07-12 15:39:36 -0400 | [diff] [blame] | 913 | |
Derrick Stolee | 577dc49 | 2021-02-18 14:07:26 +0000 | [diff] [blame] | 914 | ctx.nr = 0; |
| 915 | ctx.alloc = ctx.m ? ctx.m->num_packs : 16; |
| 916 | ctx.info = NULL; |
| 917 | ALLOC_ARRAY(ctx.info, ctx.alloc); |
Derrick Stolee | 396f257 | 2018-07-12 15:39:26 -0400 | [diff] [blame] | 918 | |
Derrick Stolee | 577dc49 | 2021-02-18 14:07:26 +0000 | [diff] [blame] | 919 | if (ctx.m) { |
| 920 | for (i = 0; i < ctx.m->num_packs; i++) { |
| 921 | ALLOC_GROW(ctx.info, ctx.nr + 1, ctx.alloc); |
Derrick Stolee | a40498a | 2018-07-12 15:39:36 -0400 | [diff] [blame] | 922 | |
Derrick Stolee | 577dc49 | 2021-02-18 14:07:26 +0000 | [diff] [blame] | 923 | ctx.info[ctx.nr].orig_pack_int_id = i; |
| 924 | ctx.info[ctx.nr].pack_name = xstrdup(ctx.m->pack_names[i]); |
| 925 | ctx.info[ctx.nr].p = NULL; |
| 926 | ctx.info[ctx.nr].expired = 0; |
| 927 | ctx.nr++; |
Derrick Stolee | a40498a | 2018-07-12 15:39:36 -0400 | [diff] [blame] | 928 | } |
| 929 | } |
| 930 | |
Derrick Stolee | 577dc49 | 2021-02-18 14:07:26 +0000 | [diff] [blame] | 931 | ctx.pack_paths_checked = 0; |
William Baker | 840cef0 | 2019-10-21 18:39:59 +0000 | [diff] [blame] | 932 | if (flags & MIDX_PROGRESS) |
Derrick Stolee | 577dc49 | 2021-02-18 14:07:26 +0000 | [diff] [blame] | 933 | ctx.progress = start_delayed_progress(_("Adding packfiles to multi-pack-index"), 0); |
William Baker | 840cef0 | 2019-10-21 18:39:59 +0000 | [diff] [blame] | 934 | else |
Derrick Stolee | 577dc49 | 2021-02-18 14:07:26 +0000 | [diff] [blame] | 935 | ctx.progress = NULL; |
William Baker | 840cef0 | 2019-10-21 18:39:59 +0000 | [diff] [blame] | 936 | |
Derrick Stolee | 577dc49 | 2021-02-18 14:07:26 +0000 | [diff] [blame] | 937 | for_each_file_in_pack_dir(object_dir, add_pack_to_midx, &ctx); |
| 938 | stop_progress(&ctx.progress); |
Derrick Stolee | 396f257 | 2018-07-12 15:39:26 -0400 | [diff] [blame] | 939 | |
Derrick Stolee | 577dc49 | 2021-02-18 14:07:26 +0000 | [diff] [blame] | 940 | if (ctx.m && ctx.nr == ctx.m->num_packs && !packs_to_drop) |
Derrick Stolee | a40498a | 2018-07-12 15:39:36 -0400 | [diff] [blame] | 941 | goto cleanup; |
| 942 | |
Taylor Blau | 9218c6a | 2021-03-30 11:04:11 -0400 | [diff] [blame] | 943 | ctx.preferred_pack_idx = -1; |
| 944 | if (preferred_pack_name) { |
| 945 | for (i = 0; i < ctx.nr; i++) { |
| 946 | if (!cmp_idx_or_pack_name(preferred_pack_name, |
| 947 | ctx.info[i].pack_name)) { |
| 948 | ctx.preferred_pack_idx = i; |
| 949 | break; |
| 950 | } |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | ctx.entries = get_sorted_entries(ctx.m, ctx.info, ctx.nr, &ctx.entries_nr, |
| 955 | ctx.preferred_pack_idx); |
Derrick Stolee | a40498a | 2018-07-12 15:39:36 -0400 | [diff] [blame] | 956 | |
Derrick Stolee | 7a3ada1 | 2021-02-18 14:07:29 +0000 | [diff] [blame] | 957 | ctx.large_offsets_needed = 0; |
Derrick Stolee | 31bda9a | 2021-02-18 14:07:28 +0000 | [diff] [blame] | 958 | for (i = 0; i < ctx.entries_nr; i++) { |
| 959 | if (ctx.entries[i].offset > 0x7fffffff) |
Derrick Stolee | 980f525 | 2021-02-18 14:07:30 +0000 | [diff] [blame] | 960 | ctx.num_large_offsets++; |
Derrick Stolee | 31bda9a | 2021-02-18 14:07:28 +0000 | [diff] [blame] | 961 | if (ctx.entries[i].offset > 0xffffffff) |
Derrick Stolee | 7a3ada1 | 2021-02-18 14:07:29 +0000 | [diff] [blame] | 962 | ctx.large_offsets_needed = 1; |
Derrick Stolee | 662148c | 2018-07-12 15:39:32 -0400 | [diff] [blame] | 963 | } |
Derrick Stolee | fe1ed56 | 2018-07-12 15:39:29 -0400 | [diff] [blame] | 964 | |
Derrick Stolee | 577dc49 | 2021-02-18 14:07:26 +0000 | [diff] [blame] | 965 | QSORT(ctx.info, ctx.nr, pack_info_compare); |
Derrick Stolee | d01bf2e | 2019-06-10 16:35:24 -0700 | [diff] [blame] | 966 | |
Derrick Stolee | 19575c7 | 2019-06-10 16:35:25 -0700 | [diff] [blame] | 967 | if (packs_to_drop && packs_to_drop->nr) { |
| 968 | int drop_index = 0; |
| 969 | int missing_drops = 0; |
| 970 | |
Derrick Stolee | 577dc49 | 2021-02-18 14:07:26 +0000 | [diff] [blame] | 971 | for (i = 0; i < ctx.nr && drop_index < packs_to_drop->nr; i++) { |
| 972 | int cmp = strcmp(ctx.info[i].pack_name, |
Derrick Stolee | 19575c7 | 2019-06-10 16:35:25 -0700 | [diff] [blame] | 973 | packs_to_drop->items[drop_index].string); |
| 974 | |
| 975 | if (!cmp) { |
| 976 | drop_index++; |
Derrick Stolee | 577dc49 | 2021-02-18 14:07:26 +0000 | [diff] [blame] | 977 | ctx.info[i].expired = 1; |
Derrick Stolee | 19575c7 | 2019-06-10 16:35:25 -0700 | [diff] [blame] | 978 | } else if (cmp > 0) { |
| 979 | error(_("did not see pack-file %s to drop"), |
| 980 | packs_to_drop->items[drop_index].string); |
| 981 | drop_index++; |
| 982 | missing_drops++; |
| 983 | i--; |
| 984 | } else { |
Derrick Stolee | 577dc49 | 2021-02-18 14:07:26 +0000 | [diff] [blame] | 985 | ctx.info[i].expired = 0; |
Derrick Stolee | 19575c7 | 2019-06-10 16:35:25 -0700 | [diff] [blame] | 986 | } |
| 987 | } |
| 988 | |
| 989 | if (missing_drops) { |
| 990 | result = 1; |
| 991 | goto cleanup; |
| 992 | } |
| 993 | } |
| 994 | |
Derrick Stolee | d01bf2e | 2019-06-10 16:35:24 -0700 | [diff] [blame] | 995 | /* |
| 996 | * pack_perm stores a permutation between pack-int-ids from the |
| 997 | * previous multi-pack-index to the new one we are writing: |
| 998 | * |
| 999 | * pack_perm[old_id] = new_id |
| 1000 | */ |
Derrick Stolee | 7a3ada1 | 2021-02-18 14:07:29 +0000 | [diff] [blame] | 1001 | ALLOC_ARRAY(ctx.pack_perm, ctx.nr); |
Derrick Stolee | 577dc49 | 2021-02-18 14:07:26 +0000 | [diff] [blame] | 1002 | for (i = 0; i < ctx.nr; i++) { |
| 1003 | if (ctx.info[i].expired) { |
Derrick Stolee | 19575c7 | 2019-06-10 16:35:25 -0700 | [diff] [blame] | 1004 | dropped_packs++; |
Derrick Stolee | 7a3ada1 | 2021-02-18 14:07:29 +0000 | [diff] [blame] | 1005 | ctx.pack_perm[ctx.info[i].orig_pack_int_id] = PACK_EXPIRED; |
Derrick Stolee | 19575c7 | 2019-06-10 16:35:25 -0700 | [diff] [blame] | 1006 | } else { |
Derrick Stolee | 7a3ada1 | 2021-02-18 14:07:29 +0000 | [diff] [blame] | 1007 | ctx.pack_perm[ctx.info[i].orig_pack_int_id] = i - dropped_packs; |
Derrick Stolee | 19575c7 | 2019-06-10 16:35:25 -0700 | [diff] [blame] | 1008 | } |
Derrick Stolee | d01bf2e | 2019-06-10 16:35:24 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
Derrick Stolee | 577dc49 | 2021-02-18 14:07:26 +0000 | [diff] [blame] | 1011 | for (i = 0; i < ctx.nr; i++) { |
| 1012 | if (!ctx.info[i].expired) |
| 1013 | pack_name_concat_len += strlen(ctx.info[i].pack_name) + 1; |
Derrick Stolee | 19575c7 | 2019-06-10 16:35:25 -0700 | [diff] [blame] | 1014 | } |
Derrick Stolee | dba6175 | 2019-06-10 16:35:24 -0700 | [diff] [blame] | 1015 | |
Taylor Blau | 9218c6a | 2021-03-30 11:04:11 -0400 | [diff] [blame] | 1016 | /* Check that the preferred pack wasn't expired (if given). */ |
| 1017 | if (preferred_pack_name) { |
| 1018 | struct pack_info *preferred = bsearch(preferred_pack_name, |
| 1019 | ctx.info, ctx.nr, |
| 1020 | sizeof(*ctx.info), |
| 1021 | idx_or_pack_name_cmp); |
| 1022 | |
| 1023 | if (!preferred) |
| 1024 | warning(_("unknown preferred pack: '%s'"), |
| 1025 | preferred_pack_name); |
| 1026 | else { |
| 1027 | uint32_t perm = ctx.pack_perm[preferred->orig_pack_int_id]; |
| 1028 | if (perm == PACK_EXPIRED) |
| 1029 | warning(_("preferred pack '%s' is expired"), |
| 1030 | preferred_pack_name); |
| 1031 | } |
| 1032 | } |
| 1033 | |
Derrick Stolee | dba6175 | 2019-06-10 16:35:24 -0700 | [diff] [blame] | 1034 | if (pack_name_concat_len % MIDX_CHUNK_ALIGNMENT) |
| 1035 | pack_name_concat_len += MIDX_CHUNK_ALIGNMENT - |
| 1036 | (pack_name_concat_len % MIDX_CHUNK_ALIGNMENT); |
| 1037 | |
Derrick Stolee | fc59e74 | 2018-07-12 15:39:22 -0400 | [diff] [blame] | 1038 | hold_lock_file_for_update(&lk, midx_name, LOCK_DIE_ON_ERROR); |
Martin Ågren | acd7160 | 2021-01-05 20:23:48 +0100 | [diff] [blame] | 1039 | f = hashfd(get_lock_file_fd(&lk), get_lock_file_path(&lk)); |
Derrick Stolee | fc59e74 | 2018-07-12 15:39:22 -0400 | [diff] [blame] | 1040 | |
Derrick Stolee | 577dc49 | 2021-02-18 14:07:26 +0000 | [diff] [blame] | 1041 | if (ctx.m) |
| 1042 | close_midx(ctx.m); |
Derrick Stolee | a40498a | 2018-07-12 15:39:36 -0400 | [diff] [blame] | 1043 | |
Derrick Stolee | 577dc49 | 2021-02-18 14:07:26 +0000 | [diff] [blame] | 1044 | if (ctx.nr - dropped_packs == 0) { |
Damien Robert | 796d61c | 2020-03-28 23:18:22 +0100 | [diff] [blame] | 1045 | error(_("no pack files to index.")); |
| 1046 | result = 1; |
| 1047 | goto cleanup; |
| 1048 | } |
| 1049 | |
Derrick Stolee | 63a8f0e | 2021-02-18 14:07:33 +0000 | [diff] [blame] | 1050 | cf = init_chunkfile(f); |
Derrick Stolee | 32f3c54 | 2018-07-12 15:39:27 -0400 | [diff] [blame] | 1051 | |
Derrick Stolee | 63a8f0e | 2021-02-18 14:07:33 +0000 | [diff] [blame] | 1052 | add_chunk(cf, MIDX_CHUNKID_PACKNAMES, pack_name_concat_len, |
| 1053 | write_midx_pack_names); |
| 1054 | add_chunk(cf, MIDX_CHUNKID_OIDFANOUT, MIDX_CHUNK_FANOUT_SIZE, |
| 1055 | write_midx_oid_fanout); |
| 1056 | add_chunk(cf, MIDX_CHUNKID_OIDLOOKUP, |
Derrick Stolee | 329fac3 | 2021-02-18 14:07:37 +0000 | [diff] [blame] | 1057 | (size_t)ctx.entries_nr * the_hash_algo->rawsz, |
Derrick Stolee | 63a8f0e | 2021-02-18 14:07:33 +0000 | [diff] [blame] | 1058 | write_midx_oid_lookup); |
| 1059 | add_chunk(cf, MIDX_CHUNKID_OBJECTOFFSETS, |
Derrick Stolee | 329fac3 | 2021-02-18 14:07:37 +0000 | [diff] [blame] | 1060 | (size_t)ctx.entries_nr * MIDX_CHUNK_OFFSET_WIDTH, |
Derrick Stolee | 63a8f0e | 2021-02-18 14:07:33 +0000 | [diff] [blame] | 1061 | write_midx_object_offsets); |
Derrick Stolee | 32f3c54 | 2018-07-12 15:39:27 -0400 | [diff] [blame] | 1062 | |
Derrick Stolee | 63a8f0e | 2021-02-18 14:07:33 +0000 | [diff] [blame] | 1063 | if (ctx.large_offsets_needed) |
| 1064 | add_chunk(cf, MIDX_CHUNKID_LARGEOFFSETS, |
Derrick Stolee | 329fac3 | 2021-02-18 14:07:37 +0000 | [diff] [blame] | 1065 | (size_t)ctx.num_large_offsets * MIDX_CHUNK_LARGE_OFFSET_WIDTH, |
Derrick Stolee | 63a8f0e | 2021-02-18 14:07:33 +0000 | [diff] [blame] | 1066 | write_midx_large_offsets); |
Derrick Stolee | 32f3c54 | 2018-07-12 15:39:27 -0400 | [diff] [blame] | 1067 | |
Derrick Stolee | 63a8f0e | 2021-02-18 14:07:33 +0000 | [diff] [blame] | 1068 | write_midx_header(f, get_num_chunks(cf), ctx.nr - dropped_packs); |
| 1069 | write_chunkfile(cf, &ctx); |
Derrick Stolee | fc59e74 | 2018-07-12 15:39:22 -0400 | [diff] [blame] | 1070 | |
Taylor Blau | 9f19161 | 2021-03-30 11:04:17 -0400 | [diff] [blame] | 1071 | finalize_hashfile(f, midx_hash, CSUM_FSYNC | CSUM_HASH_IN_STREAM); |
Derrick Stolee | 63a8f0e | 2021-02-18 14:07:33 +0000 | [diff] [blame] | 1072 | free_chunkfile(cf); |
Taylor Blau | 38ff7ca | 2021-03-30 11:04:32 -0400 | [diff] [blame] | 1073 | |
| 1074 | if (flags & MIDX_WRITE_REV_INDEX) |
| 1075 | ctx.pack_order = midx_pack_order(&ctx); |
| 1076 | |
| 1077 | if (flags & MIDX_WRITE_REV_INDEX) |
| 1078 | write_midx_reverse_index(midx_name, midx_hash, &ctx); |
| 1079 | clear_midx_files_ext(the_repository, ".rev", midx_hash); |
| 1080 | |
Derrick Stolee | fc59e74 | 2018-07-12 15:39:22 -0400 | [diff] [blame] | 1081 | commit_lock_file(&lk); |
| 1082 | |
Derrick Stolee | a40498a | 2018-07-12 15:39:36 -0400 | [diff] [blame] | 1083 | cleanup: |
Derrick Stolee | 577dc49 | 2021-02-18 14:07:26 +0000 | [diff] [blame] | 1084 | for (i = 0; i < ctx.nr; i++) { |
| 1085 | if (ctx.info[i].p) { |
| 1086 | close_pack(ctx.info[i].p); |
| 1087 | free(ctx.info[i].p); |
Derrick Stolee | 396f257 | 2018-07-12 15:39:26 -0400 | [diff] [blame] | 1088 | } |
Derrick Stolee | 577dc49 | 2021-02-18 14:07:26 +0000 | [diff] [blame] | 1089 | free(ctx.info[i].pack_name); |
Derrick Stolee | 396f257 | 2018-07-12 15:39:26 -0400 | [diff] [blame] | 1090 | } |
| 1091 | |
Derrick Stolee | 577dc49 | 2021-02-18 14:07:26 +0000 | [diff] [blame] | 1092 | free(ctx.info); |
Derrick Stolee | 31bda9a | 2021-02-18 14:07:28 +0000 | [diff] [blame] | 1093 | free(ctx.entries); |
Derrick Stolee | 7a3ada1 | 2021-02-18 14:07:29 +0000 | [diff] [blame] | 1094 | free(ctx.pack_perm); |
Taylor Blau | 38ff7ca | 2021-03-30 11:04:32 -0400 | [diff] [blame] | 1095 | free(ctx.pack_order); |
Derrick Stolee | a40498a | 2018-07-12 15:39:36 -0400 | [diff] [blame] | 1096 | free(midx_name); |
Derrick Stolee | 19575c7 | 2019-06-10 16:35:25 -0700 | [diff] [blame] | 1097 | return result; |
| 1098 | } |
| 1099 | |
Taylor Blau | 9218c6a | 2021-03-30 11:04:11 -0400 | [diff] [blame] | 1100 | int write_midx_file(const char *object_dir, |
| 1101 | const char *preferred_pack_name, |
| 1102 | unsigned flags) |
Derrick Stolee | 19575c7 | 2019-06-10 16:35:25 -0700 | [diff] [blame] | 1103 | { |
Taylor Blau | 9218c6a | 2021-03-30 11:04:11 -0400 | [diff] [blame] | 1104 | return write_midx_internal(object_dir, NULL, NULL, preferred_pack_name, |
| 1105 | flags); |
Derrick Stolee | a340773 | 2018-07-12 15:39:21 -0400 | [diff] [blame] | 1106 | } |
Derrick Stolee | 525e18c | 2018-07-12 15:39:40 -0400 | [diff] [blame] | 1107 | |
Taylor Blau | 38ff7ca | 2021-03-30 11:04:32 -0400 | [diff] [blame] | 1108 | struct clear_midx_data { |
| 1109 | char *keep; |
| 1110 | const char *ext; |
| 1111 | }; |
| 1112 | |
| 1113 | static void clear_midx_file_ext(const char *full_path, size_t full_path_len, |
| 1114 | const char *file_name, void *_data) |
| 1115 | { |
| 1116 | struct clear_midx_data *data = _data; |
| 1117 | |
| 1118 | if (!(starts_with(file_name, "multi-pack-index-") && |
| 1119 | ends_with(file_name, data->ext))) |
| 1120 | return; |
| 1121 | if (data->keep && !strcmp(data->keep, file_name)) |
| 1122 | return; |
| 1123 | |
| 1124 | if (unlink(full_path)) |
| 1125 | die_errno(_("failed to remove %s"), full_path); |
| 1126 | } |
| 1127 | |
| 1128 | static void clear_midx_files_ext(struct repository *r, const char *ext, |
| 1129 | unsigned char *keep_hash) |
| 1130 | { |
| 1131 | struct clear_midx_data data; |
| 1132 | memset(&data, 0, sizeof(struct clear_midx_data)); |
| 1133 | |
| 1134 | if (keep_hash) |
| 1135 | data.keep = xstrfmt("multi-pack-index-%s%s", |
| 1136 | hash_to_hex(keep_hash), ext); |
| 1137 | data.ext = ext; |
| 1138 | |
| 1139 | for_each_file_in_pack_dir(r->objects->odb->path, |
| 1140 | clear_midx_file_ext, |
| 1141 | &data); |
| 1142 | |
| 1143 | free(data.keep); |
Derrick Stolee | a340773 | 2018-07-12 15:39:21 -0400 | [diff] [blame] | 1144 | } |
Derrick Stolee | 525e18c | 2018-07-12 15:39:40 -0400 | [diff] [blame] | 1145 | |
Derrick Stolee | 1dcd9f2 | 2018-10-12 10:34:19 -0700 | [diff] [blame] | 1146 | void clear_midx_file(struct repository *r) |
Derrick Stolee | 525e18c | 2018-07-12 15:39:40 -0400 | [diff] [blame] | 1147 | { |
Junio C Hamano | 3b2f8a0 | 2019-01-04 13:33:32 -0800 | [diff] [blame] | 1148 | char *midx = get_midx_filename(r->objects->odb->path); |
Derrick Stolee | 1dcd9f2 | 2018-10-12 10:34:19 -0700 | [diff] [blame] | 1149 | |
| 1150 | if (r->objects && r->objects->multi_pack_index) { |
| 1151 | close_midx(r->objects->multi_pack_index); |
| 1152 | r->objects->multi_pack_index = NULL; |
| 1153 | } |
Derrick Stolee | 525e18c | 2018-07-12 15:39:40 -0400 | [diff] [blame] | 1154 | |
Jeff King | d5e1961 | 2020-08-13 11:55:00 -0400 | [diff] [blame] | 1155 | if (remove_path(midx)) |
Derrick Stolee | 525e18c | 2018-07-12 15:39:40 -0400 | [diff] [blame] | 1156 | die(_("failed to clear multi-pack-index at %s"), midx); |
Derrick Stolee | 525e18c | 2018-07-12 15:39:40 -0400 | [diff] [blame] | 1157 | |
Taylor Blau | 38ff7ca | 2021-03-30 11:04:32 -0400 | [diff] [blame] | 1158 | clear_midx_files_ext(r, ".rev", NULL); |
| 1159 | |
Derrick Stolee | 525e18c | 2018-07-12 15:39:40 -0400 | [diff] [blame] | 1160 | free(midx); |
| 1161 | } |
Derrick Stolee | 56ee7ff | 2018-09-13 11:02:13 -0700 | [diff] [blame] | 1162 | |
| 1163 | static int verify_midx_error; |
| 1164 | |
Derrick Stolee | d4bf1d8 | 2018-09-13 11:02:19 -0700 | [diff] [blame] | 1165 | static void midx_report(const char *fmt, ...) |
| 1166 | { |
| 1167 | va_list ap; |
| 1168 | verify_midx_error = 1; |
| 1169 | va_start(ap, fmt); |
| 1170 | vfprintf(stderr, fmt, ap); |
| 1171 | fprintf(stderr, "\n"); |
| 1172 | va_end(ap); |
| 1173 | } |
| 1174 | |
Jeff Hostetler | 5ae18df | 2019-03-21 12:36:15 -0700 | [diff] [blame] | 1175 | struct pair_pos_vs_id |
| 1176 | { |
| 1177 | uint32_t pos; |
| 1178 | uint32_t pack_int_id; |
| 1179 | }; |
| 1180 | |
| 1181 | static int compare_pair_pos_vs_id(const void *_a, const void *_b) |
| 1182 | { |
| 1183 | struct pair_pos_vs_id *a = (struct pair_pos_vs_id *)_a; |
| 1184 | struct pair_pos_vs_id *b = (struct pair_pos_vs_id *)_b; |
| 1185 | |
| 1186 | return b->pack_int_id - a->pack_int_id; |
| 1187 | } |
| 1188 | |
Jeff Hostetler | 430efb8 | 2019-03-21 12:36:14 -0700 | [diff] [blame] | 1189 | /* |
| 1190 | * Limit calls to display_progress() for performance reasons. |
| 1191 | * The interval here was arbitrarily chosen. |
| 1192 | */ |
| 1193 | #define SPARSE_PROGRESS_INTERVAL (1 << 12) |
| 1194 | #define midx_display_sparse_progress(progress, n) \ |
| 1195 | do { \ |
| 1196 | uint64_t _n = (n); \ |
| 1197 | if ((_n & (SPARSE_PROGRESS_INTERVAL - 1)) == 0) \ |
| 1198 | display_progress(progress, _n); \ |
| 1199 | } while (0) |
| 1200 | |
William Baker | efbc3ae | 2019-10-21 18:39:58 +0000 | [diff] [blame] | 1201 | int verify_midx_file(struct repository *r, const char *object_dir, unsigned flags) |
Derrick Stolee | 56ee7ff | 2018-09-13 11:02:13 -0700 | [diff] [blame] | 1202 | { |
Jeff Hostetler | 5ae18df | 2019-03-21 12:36:15 -0700 | [diff] [blame] | 1203 | struct pair_pos_vs_id *pairs = NULL; |
Derrick Stolee | d4bf1d8 | 2018-09-13 11:02:19 -0700 | [diff] [blame] | 1204 | uint32_t i; |
William Baker | ad60096 | 2019-10-21 18:40:01 +0000 | [diff] [blame] | 1205 | struct progress *progress = NULL; |
Derrick Stolee | 56ee7ff | 2018-09-13 11:02:13 -0700 | [diff] [blame] | 1206 | struct multi_pack_index *m = load_multi_pack_index(object_dir, 1); |
| 1207 | verify_midx_error = 0; |
| 1208 | |
Derrick Stolee | d960754 | 2020-08-17 14:04:48 +0000 | [diff] [blame] | 1209 | if (!m) { |
| 1210 | int result = 0; |
| 1211 | struct stat sb; |
| 1212 | char *filename = get_midx_filename(object_dir); |
| 1213 | if (!stat(filename, &sb)) { |
| 1214 | error(_("multi-pack-index file exists, but failed to parse")); |
| 1215 | result = 1; |
| 1216 | } |
| 1217 | free(filename); |
| 1218 | return result; |
| 1219 | } |
Derrick Stolee | 56ee7ff | 2018-09-13 11:02:13 -0700 | [diff] [blame] | 1220 | |
William Baker | ad60096 | 2019-10-21 18:40:01 +0000 | [diff] [blame] | 1221 | if (flags & MIDX_PROGRESS) |
Derrick Stolee | efdd2f0 | 2020-09-25 12:33:35 +0000 | [diff] [blame] | 1222 | progress = start_delayed_progress(_("Looking for referenced packfiles"), |
William Baker | ad60096 | 2019-10-21 18:40:01 +0000 | [diff] [blame] | 1223 | m->num_packs); |
Derrick Stolee | d4bf1d8 | 2018-09-13 11:02:19 -0700 | [diff] [blame] | 1224 | for (i = 0; i < m->num_packs; i++) { |
Derrick Stolee | 64404a2 | 2019-04-29 09:18:55 -0700 | [diff] [blame] | 1225 | if (prepare_midx_pack(r, m, i)) |
Derrick Stolee | d4bf1d8 | 2018-09-13 11:02:19 -0700 | [diff] [blame] | 1226 | midx_report("failed to load pack in position %d", i); |
Jeff Hostetler | 430efb8 | 2019-03-21 12:36:14 -0700 | [diff] [blame] | 1227 | |
| 1228 | display_progress(progress, i + 1); |
Derrick Stolee | d4bf1d8 | 2018-09-13 11:02:19 -0700 | [diff] [blame] | 1229 | } |
Jeff Hostetler | 430efb8 | 2019-03-21 12:36:14 -0700 | [diff] [blame] | 1230 | stop_progress(&progress); |
Derrick Stolee | d4bf1d8 | 2018-09-13 11:02:19 -0700 | [diff] [blame] | 1231 | |
Derrick Stolee | 2f23d3f | 2018-09-13 11:02:20 -0700 | [diff] [blame] | 1232 | for (i = 0; i < 255; i++) { |
| 1233 | uint32_t oid_fanout1 = ntohl(m->chunk_oid_fanout[i]); |
| 1234 | uint32_t oid_fanout2 = ntohl(m->chunk_oid_fanout[i + 1]); |
| 1235 | |
| 1236 | if (oid_fanout1 > oid_fanout2) |
| 1237 | midx_report(_("oid fanout out of order: fanout[%d] = %"PRIx32" > %"PRIx32" = fanout[%d]"), |
| 1238 | i, oid_fanout1, oid_fanout2, i + 1); |
| 1239 | } |
| 1240 | |
Damien Robert | 796d61c | 2020-03-28 23:18:22 +0100 | [diff] [blame] | 1241 | if (m->num_objects == 0) { |
| 1242 | midx_report(_("the midx contains no oid")); |
| 1243 | /* |
| 1244 | * Remaining tests assume that we have objects, so we can |
| 1245 | * return here. |
| 1246 | */ |
| 1247 | return verify_midx_error; |
| 1248 | } |
| 1249 | |
William Baker | ad60096 | 2019-10-21 18:40:01 +0000 | [diff] [blame] | 1250 | if (flags & MIDX_PROGRESS) |
| 1251 | progress = start_sparse_progress(_("Verifying OID order in multi-pack-index"), |
| 1252 | m->num_objects - 1); |
Derrick Stolee | 55c5648 | 2018-09-13 11:02:22 -0700 | [diff] [blame] | 1253 | for (i = 0; i < m->num_objects - 1; i++) { |
| 1254 | struct object_id oid1, oid2; |
| 1255 | |
| 1256 | nth_midxed_object_oid(&oid1, m, i); |
| 1257 | nth_midxed_object_oid(&oid2, m, i + 1); |
| 1258 | |
| 1259 | if (oidcmp(&oid1, &oid2) >= 0) |
| 1260 | midx_report(_("oid lookup out of order: oid[%d] = %s >= %s = oid[%d]"), |
| 1261 | i, oid_to_hex(&oid1), oid_to_hex(&oid2), i + 1); |
Derrick Stolee | 55c5648 | 2018-09-13 11:02:22 -0700 | [diff] [blame] | 1262 | |
Jeff Hostetler | 430efb8 | 2019-03-21 12:36:14 -0700 | [diff] [blame] | 1263 | midx_display_sparse_progress(progress, i + 1); |
| 1264 | } |
| 1265 | stop_progress(&progress); |
| 1266 | |
Jeff Hostetler | 5ae18df | 2019-03-21 12:36:15 -0700 | [diff] [blame] | 1267 | /* |
| 1268 | * Create an array mapping each object to its packfile id. Sort it |
| 1269 | * to group the objects by packfile. Use this permutation to visit |
| 1270 | * each of the objects and only require 1 packfile to be open at a |
| 1271 | * time. |
| 1272 | */ |
| 1273 | ALLOC_ARRAY(pairs, m->num_objects); |
| 1274 | for (i = 0; i < m->num_objects; i++) { |
| 1275 | pairs[i].pos = i; |
| 1276 | pairs[i].pack_int_id = nth_midxed_pack_int_id(m, i); |
| 1277 | } |
| 1278 | |
William Baker | ad60096 | 2019-10-21 18:40:01 +0000 | [diff] [blame] | 1279 | if (flags & MIDX_PROGRESS) |
| 1280 | progress = start_sparse_progress(_("Sorting objects by packfile"), |
| 1281 | m->num_objects); |
Jeff Hostetler | 5ae18df | 2019-03-21 12:36:15 -0700 | [diff] [blame] | 1282 | display_progress(progress, 0); /* TODO: Measure QSORT() progress */ |
| 1283 | QSORT(pairs, m->num_objects, compare_pair_pos_vs_id); |
| 1284 | stop_progress(&progress); |
| 1285 | |
William Baker | ad60096 | 2019-10-21 18:40:01 +0000 | [diff] [blame] | 1286 | if (flags & MIDX_PROGRESS) |
| 1287 | progress = start_sparse_progress(_("Verifying object offsets"), m->num_objects); |
Derrick Stolee | cc6af73 | 2018-09-13 11:02:25 -0700 | [diff] [blame] | 1288 | for (i = 0; i < m->num_objects; i++) { |
| 1289 | struct object_id oid; |
| 1290 | struct pack_entry e; |
| 1291 | off_t m_offset, p_offset; |
| 1292 | |
Jeff Hostetler | 5ae18df | 2019-03-21 12:36:15 -0700 | [diff] [blame] | 1293 | if (i > 0 && pairs[i-1].pack_int_id != pairs[i].pack_int_id && |
| 1294 | m->packs[pairs[i-1].pack_int_id]) |
| 1295 | { |
| 1296 | close_pack_fd(m->packs[pairs[i-1].pack_int_id]); |
| 1297 | close_pack_index(m->packs[pairs[i-1].pack_int_id]); |
| 1298 | } |
| 1299 | |
| 1300 | nth_midxed_object_oid(&oid, m, pairs[i].pos); |
| 1301 | |
Derrick Stolee | 64404a2 | 2019-04-29 09:18:55 -0700 | [diff] [blame] | 1302 | if (!fill_midx_entry(r, &oid, &e, m)) { |
Derrick Stolee | cc6af73 | 2018-09-13 11:02:25 -0700 | [diff] [blame] | 1303 | midx_report(_("failed to load pack entry for oid[%d] = %s"), |
Jeff Hostetler | 5ae18df | 2019-03-21 12:36:15 -0700 | [diff] [blame] | 1304 | pairs[i].pos, oid_to_hex(&oid)); |
Derrick Stolee | cc6af73 | 2018-09-13 11:02:25 -0700 | [diff] [blame] | 1305 | continue; |
| 1306 | } |
| 1307 | |
| 1308 | if (open_pack_index(e.p)) { |
| 1309 | midx_report(_("failed to load pack-index for packfile %s"), |
| 1310 | e.p->pack_name); |
| 1311 | break; |
| 1312 | } |
| 1313 | |
| 1314 | m_offset = e.offset; |
| 1315 | p_offset = find_pack_entry_one(oid.hash, e.p); |
| 1316 | |
| 1317 | if (m_offset != p_offset) |
| 1318 | midx_report(_("incorrect object offset for oid[%d] = %s: %"PRIx64" != %"PRIx64), |
Jeff Hostetler | 5ae18df | 2019-03-21 12:36:15 -0700 | [diff] [blame] | 1319 | pairs[i].pos, oid_to_hex(&oid), m_offset, p_offset); |
Derrick Stolee | 144d703 | 2018-09-13 11:02:26 -0700 | [diff] [blame] | 1320 | |
Jeff Hostetler | 430efb8 | 2019-03-21 12:36:14 -0700 | [diff] [blame] | 1321 | midx_display_sparse_progress(progress, i + 1); |
Derrick Stolee | cc6af73 | 2018-09-13 11:02:25 -0700 | [diff] [blame] | 1322 | } |
Derrick Stolee | 144d703 | 2018-09-13 11:02:26 -0700 | [diff] [blame] | 1323 | stop_progress(&progress); |
Derrick Stolee | cc6af73 | 2018-09-13 11:02:25 -0700 | [diff] [blame] | 1324 | |
Jeff Hostetler | 5ae18df | 2019-03-21 12:36:15 -0700 | [diff] [blame] | 1325 | free(pairs); |
| 1326 | |
Derrick Stolee | 56ee7ff | 2018-09-13 11:02:13 -0700 | [diff] [blame] | 1327 | return verify_midx_error; |
| 1328 | } |
Derrick Stolee | cff9711 | 2019-06-10 16:35:23 -0700 | [diff] [blame] | 1329 | |
William Baker | efbc3ae | 2019-10-21 18:39:58 +0000 | [diff] [blame] | 1330 | int expire_midx_packs(struct repository *r, const char *object_dir, unsigned flags) |
Derrick Stolee | cff9711 | 2019-06-10 16:35:23 -0700 | [diff] [blame] | 1331 | { |
Derrick Stolee | 19575c7 | 2019-06-10 16:35:25 -0700 | [diff] [blame] | 1332 | uint32_t i, *count, result = 0; |
| 1333 | struct string_list packs_to_drop = STRING_LIST_INIT_DUP; |
| 1334 | struct multi_pack_index *m = load_multi_pack_index(object_dir, 1); |
William Baker | 8dc18f8 | 2019-10-21 18:40:00 +0000 | [diff] [blame] | 1335 | struct progress *progress = NULL; |
Derrick Stolee | 19575c7 | 2019-06-10 16:35:25 -0700 | [diff] [blame] | 1336 | |
| 1337 | if (!m) |
| 1338 | return 0; |
| 1339 | |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 1340 | CALLOC_ARRAY(count, m->num_packs); |
William Baker | 8dc18f8 | 2019-10-21 18:40:00 +0000 | [diff] [blame] | 1341 | |
| 1342 | if (flags & MIDX_PROGRESS) |
Derrick Stolee | efdd2f0 | 2020-09-25 12:33:35 +0000 | [diff] [blame] | 1343 | progress = start_delayed_progress(_("Counting referenced objects"), |
William Baker | 8dc18f8 | 2019-10-21 18:40:00 +0000 | [diff] [blame] | 1344 | m->num_objects); |
Derrick Stolee | 19575c7 | 2019-06-10 16:35:25 -0700 | [diff] [blame] | 1345 | for (i = 0; i < m->num_objects; i++) { |
| 1346 | int pack_int_id = nth_midxed_pack_int_id(m, i); |
| 1347 | count[pack_int_id]++; |
William Baker | 8dc18f8 | 2019-10-21 18:40:00 +0000 | [diff] [blame] | 1348 | display_progress(progress, i + 1); |
Derrick Stolee | 19575c7 | 2019-06-10 16:35:25 -0700 | [diff] [blame] | 1349 | } |
William Baker | 8dc18f8 | 2019-10-21 18:40:00 +0000 | [diff] [blame] | 1350 | stop_progress(&progress); |
Derrick Stolee | 19575c7 | 2019-06-10 16:35:25 -0700 | [diff] [blame] | 1351 | |
William Baker | 8dc18f8 | 2019-10-21 18:40:00 +0000 | [diff] [blame] | 1352 | if (flags & MIDX_PROGRESS) |
Derrick Stolee | efdd2f0 | 2020-09-25 12:33:35 +0000 | [diff] [blame] | 1353 | progress = start_delayed_progress(_("Finding and deleting unreferenced packfiles"), |
William Baker | 8dc18f8 | 2019-10-21 18:40:00 +0000 | [diff] [blame] | 1354 | m->num_packs); |
Derrick Stolee | 19575c7 | 2019-06-10 16:35:25 -0700 | [diff] [blame] | 1355 | for (i = 0; i < m->num_packs; i++) { |
| 1356 | char *pack_name; |
William Baker | 8dc18f8 | 2019-10-21 18:40:00 +0000 | [diff] [blame] | 1357 | display_progress(progress, i + 1); |
Derrick Stolee | 19575c7 | 2019-06-10 16:35:25 -0700 | [diff] [blame] | 1358 | |
| 1359 | if (count[i]) |
| 1360 | continue; |
| 1361 | |
| 1362 | if (prepare_midx_pack(r, m, i)) |
| 1363 | continue; |
| 1364 | |
| 1365 | if (m->packs[i]->pack_keep) |
| 1366 | continue; |
| 1367 | |
| 1368 | pack_name = xstrdup(m->packs[i]->pack_name); |
| 1369 | close_pack(m->packs[i]); |
| 1370 | |
| 1371 | string_list_insert(&packs_to_drop, m->pack_names[i]); |
| 1372 | unlink_pack_path(pack_name, 0); |
| 1373 | free(pack_name); |
| 1374 | } |
William Baker | 8dc18f8 | 2019-10-21 18:40:00 +0000 | [diff] [blame] | 1375 | stop_progress(&progress); |
Derrick Stolee | 19575c7 | 2019-06-10 16:35:25 -0700 | [diff] [blame] | 1376 | |
| 1377 | free(count); |
| 1378 | |
| 1379 | if (packs_to_drop.nr) |
Taylor Blau | 9218c6a | 2021-03-30 11:04:11 -0400 | [diff] [blame] | 1380 | result = write_midx_internal(object_dir, m, &packs_to_drop, NULL, flags); |
Derrick Stolee | 19575c7 | 2019-06-10 16:35:25 -0700 | [diff] [blame] | 1381 | |
| 1382 | string_list_clear(&packs_to_drop, 0); |
| 1383 | return result; |
Derrick Stolee | cff9711 | 2019-06-10 16:35:23 -0700 | [diff] [blame] | 1384 | } |
Derrick Stolee | 2af890b | 2019-06-10 16:35:26 -0700 | [diff] [blame] | 1385 | |
Derrick Stolee | ce1e4a1 | 2019-06-10 16:35:27 -0700 | [diff] [blame] | 1386 | struct repack_info { |
| 1387 | timestamp_t mtime; |
| 1388 | uint32_t referenced_objects; |
| 1389 | uint32_t pack_int_id; |
| 1390 | }; |
| 1391 | |
| 1392 | static int compare_by_mtime(const void *a_, const void *b_) |
| 1393 | { |
| 1394 | const struct repack_info *a, *b; |
| 1395 | |
| 1396 | a = (const struct repack_info *)a_; |
| 1397 | b = (const struct repack_info *)b_; |
| 1398 | |
| 1399 | if (a->mtime < b->mtime) |
| 1400 | return -1; |
| 1401 | if (a->mtime > b->mtime) |
| 1402 | return 1; |
| 1403 | return 0; |
| 1404 | } |
| 1405 | |
Derrick Stolee | 3ce4ca0 | 2020-05-10 16:07:34 +0000 | [diff] [blame] | 1406 | static int fill_included_packs_all(struct repository *r, |
| 1407 | struct multi_pack_index *m, |
Derrick Stolee | ce1e4a1 | 2019-06-10 16:35:27 -0700 | [diff] [blame] | 1408 | unsigned char *include_pack) |
| 1409 | { |
Derrick Stolee | 3ce4ca0 | 2020-05-10 16:07:34 +0000 | [diff] [blame] | 1410 | uint32_t i, count = 0; |
| 1411 | int pack_kept_objects = 0; |
Derrick Stolee | ce1e4a1 | 2019-06-10 16:35:27 -0700 | [diff] [blame] | 1412 | |
Derrick Stolee | 3ce4ca0 | 2020-05-10 16:07:34 +0000 | [diff] [blame] | 1413 | repo_config_get_bool(r, "repack.packkeptobjects", &pack_kept_objects); |
| 1414 | |
| 1415 | for (i = 0; i < m->num_packs; i++) { |
| 1416 | if (prepare_midx_pack(r, m, i)) |
| 1417 | continue; |
| 1418 | if (!pack_kept_objects && m->packs[i]->pack_keep) |
| 1419 | continue; |
| 1420 | |
Derrick Stolee | ce1e4a1 | 2019-06-10 16:35:27 -0700 | [diff] [blame] | 1421 | include_pack[i] = 1; |
Derrick Stolee | 3ce4ca0 | 2020-05-10 16:07:34 +0000 | [diff] [blame] | 1422 | count++; |
| 1423 | } |
Derrick Stolee | ce1e4a1 | 2019-06-10 16:35:27 -0700 | [diff] [blame] | 1424 | |
Derrick Stolee | 3ce4ca0 | 2020-05-10 16:07:34 +0000 | [diff] [blame] | 1425 | return count < 2; |
Derrick Stolee | ce1e4a1 | 2019-06-10 16:35:27 -0700 | [diff] [blame] | 1426 | } |
| 1427 | |
| 1428 | static int fill_included_packs_batch(struct repository *r, |
| 1429 | struct multi_pack_index *m, |
| 1430 | unsigned char *include_pack, |
| 1431 | size_t batch_size) |
| 1432 | { |
| 1433 | uint32_t i, packs_to_repack; |
| 1434 | size_t total_size; |
| 1435 | struct repack_info *pack_info = xcalloc(m->num_packs, sizeof(struct repack_info)); |
Derrick Stolee | 3ce4ca0 | 2020-05-10 16:07:34 +0000 | [diff] [blame] | 1436 | int pack_kept_objects = 0; |
| 1437 | |
| 1438 | repo_config_get_bool(r, "repack.packkeptobjects", &pack_kept_objects); |
Derrick Stolee | ce1e4a1 | 2019-06-10 16:35:27 -0700 | [diff] [blame] | 1439 | |
| 1440 | for (i = 0; i < m->num_packs; i++) { |
| 1441 | pack_info[i].pack_int_id = i; |
| 1442 | |
| 1443 | if (prepare_midx_pack(r, m, i)) |
| 1444 | continue; |
| 1445 | |
| 1446 | pack_info[i].mtime = m->packs[i]->mtime; |
| 1447 | } |
| 1448 | |
| 1449 | for (i = 0; batch_size && i < m->num_objects; i++) { |
| 1450 | uint32_t pack_int_id = nth_midxed_pack_int_id(m, i); |
| 1451 | pack_info[pack_int_id].referenced_objects++; |
| 1452 | } |
| 1453 | |
| 1454 | QSORT(pack_info, m->num_packs, compare_by_mtime); |
| 1455 | |
| 1456 | total_size = 0; |
| 1457 | packs_to_repack = 0; |
| 1458 | for (i = 0; total_size < batch_size && i < m->num_packs; i++) { |
| 1459 | int pack_int_id = pack_info[i].pack_int_id; |
| 1460 | struct packed_git *p = m->packs[pack_int_id]; |
| 1461 | size_t expected_size; |
| 1462 | |
| 1463 | if (!p) |
| 1464 | continue; |
Derrick Stolee | 3ce4ca0 | 2020-05-10 16:07:34 +0000 | [diff] [blame] | 1465 | if (!pack_kept_objects && p->pack_keep) |
| 1466 | continue; |
Derrick Stolee | ce1e4a1 | 2019-06-10 16:35:27 -0700 | [diff] [blame] | 1467 | if (open_pack_index(p) || !p->num_objects) |
| 1468 | continue; |
| 1469 | |
| 1470 | expected_size = (size_t)(p->pack_size |
| 1471 | * pack_info[i].referenced_objects); |
| 1472 | expected_size /= p->num_objects; |
| 1473 | |
| 1474 | if (expected_size >= batch_size) |
| 1475 | continue; |
| 1476 | |
| 1477 | packs_to_repack++; |
| 1478 | total_size += expected_size; |
| 1479 | include_pack[pack_int_id] = 1; |
| 1480 | } |
| 1481 | |
| 1482 | free(pack_info); |
| 1483 | |
Derrick Stolee | 1eb22c7 | 2020-08-11 15:30:18 +0000 | [diff] [blame] | 1484 | if (packs_to_repack < 2) |
Derrick Stolee | ce1e4a1 | 2019-06-10 16:35:27 -0700 | [diff] [blame] | 1485 | return 1; |
| 1486 | |
| 1487 | return 0; |
| 1488 | } |
| 1489 | |
William Baker | efbc3ae | 2019-10-21 18:39:58 +0000 | [diff] [blame] | 1490 | int midx_repack(struct repository *r, const char *object_dir, size_t batch_size, unsigned flags) |
Derrick Stolee | 2af890b | 2019-06-10 16:35:26 -0700 | [diff] [blame] | 1491 | { |
Derrick Stolee | ce1e4a1 | 2019-06-10 16:35:27 -0700 | [diff] [blame] | 1492 | int result = 0; |
| 1493 | uint32_t i; |
| 1494 | unsigned char *include_pack; |
| 1495 | struct child_process cmd = CHILD_PROCESS_INIT; |
René Scharfe | 6af3b00 | 2020-08-12 18:52:54 +0200 | [diff] [blame] | 1496 | FILE *cmd_in; |
Derrick Stolee | ce1e4a1 | 2019-06-10 16:35:27 -0700 | [diff] [blame] | 1497 | struct strbuf base_name = STRBUF_INIT; |
| 1498 | struct multi_pack_index *m = load_multi_pack_index(object_dir, 1); |
| 1499 | |
Son Luong Ngoc | e11d86d | 2020-05-10 16:07:33 +0000 | [diff] [blame] | 1500 | /* |
| 1501 | * When updating the default for these configuration |
| 1502 | * variables in builtin/repack.c, these must be adjusted |
| 1503 | * to match. |
| 1504 | */ |
| 1505 | int delta_base_offset = 1; |
| 1506 | int use_delta_islands = 0; |
| 1507 | |
Derrick Stolee | ce1e4a1 | 2019-06-10 16:35:27 -0700 | [diff] [blame] | 1508 | if (!m) |
| 1509 | return 0; |
| 1510 | |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 1511 | CALLOC_ARRAY(include_pack, m->num_packs); |
Derrick Stolee | ce1e4a1 | 2019-06-10 16:35:27 -0700 | [diff] [blame] | 1512 | |
| 1513 | if (batch_size) { |
| 1514 | if (fill_included_packs_batch(r, m, include_pack, batch_size)) |
| 1515 | goto cleanup; |
Derrick Stolee | 3ce4ca0 | 2020-05-10 16:07:34 +0000 | [diff] [blame] | 1516 | } else if (fill_included_packs_all(r, m, include_pack)) |
Derrick Stolee | ce1e4a1 | 2019-06-10 16:35:27 -0700 | [diff] [blame] | 1517 | goto cleanup; |
| 1518 | |
Son Luong Ngoc | e11d86d | 2020-05-10 16:07:33 +0000 | [diff] [blame] | 1519 | repo_config_get_bool(r, "repack.usedeltabaseoffset", &delta_base_offset); |
| 1520 | repo_config_get_bool(r, "repack.usedeltaislands", &use_delta_islands); |
| 1521 | |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 1522 | strvec_push(&cmd.args, "pack-objects"); |
Derrick Stolee | ce1e4a1 | 2019-06-10 16:35:27 -0700 | [diff] [blame] | 1523 | |
| 1524 | strbuf_addstr(&base_name, object_dir); |
| 1525 | strbuf_addstr(&base_name, "/pack/pack"); |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 1526 | strvec_push(&cmd.args, base_name.buf); |
William Baker | 64d80e7 | 2019-10-21 18:40:02 +0000 | [diff] [blame] | 1527 | |
Son Luong Ngoc | e11d86d | 2020-05-10 16:07:33 +0000 | [diff] [blame] | 1528 | if (delta_base_offset) |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 1529 | strvec_push(&cmd.args, "--delta-base-offset"); |
Son Luong Ngoc | e11d86d | 2020-05-10 16:07:33 +0000 | [diff] [blame] | 1530 | if (use_delta_islands) |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 1531 | strvec_push(&cmd.args, "--delta-islands"); |
Son Luong Ngoc | e11d86d | 2020-05-10 16:07:33 +0000 | [diff] [blame] | 1532 | |
William Baker | 64d80e7 | 2019-10-21 18:40:02 +0000 | [diff] [blame] | 1533 | if (flags & MIDX_PROGRESS) |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 1534 | strvec_push(&cmd.args, "--progress"); |
William Baker | 64d80e7 | 2019-10-21 18:40:02 +0000 | [diff] [blame] | 1535 | else |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 1536 | strvec_push(&cmd.args, "-q"); |
William Baker | 64d80e7 | 2019-10-21 18:40:02 +0000 | [diff] [blame] | 1537 | |
Derrick Stolee | ce1e4a1 | 2019-06-10 16:35:27 -0700 | [diff] [blame] | 1538 | strbuf_release(&base_name); |
| 1539 | |
| 1540 | cmd.git_cmd = 1; |
| 1541 | cmd.in = cmd.out = -1; |
| 1542 | |
| 1543 | if (start_command(&cmd)) { |
| 1544 | error(_("could not start pack-objects")); |
| 1545 | result = 1; |
| 1546 | goto cleanup; |
| 1547 | } |
| 1548 | |
René Scharfe | 6af3b00 | 2020-08-12 18:52:54 +0200 | [diff] [blame] | 1549 | cmd_in = xfdopen(cmd.in, "w"); |
| 1550 | |
Derrick Stolee | ce1e4a1 | 2019-06-10 16:35:27 -0700 | [diff] [blame] | 1551 | for (i = 0; i < m->num_objects; i++) { |
| 1552 | struct object_id oid; |
| 1553 | uint32_t pack_int_id = nth_midxed_pack_int_id(m, i); |
| 1554 | |
| 1555 | if (!include_pack[pack_int_id]) |
| 1556 | continue; |
| 1557 | |
| 1558 | nth_midxed_object_oid(&oid, m, i); |
René Scharfe | 6af3b00 | 2020-08-12 18:52:54 +0200 | [diff] [blame] | 1559 | fprintf(cmd_in, "%s\n", oid_to_hex(&oid)); |
Derrick Stolee | ce1e4a1 | 2019-06-10 16:35:27 -0700 | [diff] [blame] | 1560 | } |
René Scharfe | 6af3b00 | 2020-08-12 18:52:54 +0200 | [diff] [blame] | 1561 | fclose(cmd_in); |
Derrick Stolee | ce1e4a1 | 2019-06-10 16:35:27 -0700 | [diff] [blame] | 1562 | |
| 1563 | if (finish_command(&cmd)) { |
| 1564 | error(_("could not finish pack-objects")); |
| 1565 | result = 1; |
| 1566 | goto cleanup; |
| 1567 | } |
| 1568 | |
Taylor Blau | 9218c6a | 2021-03-30 11:04:11 -0400 | [diff] [blame] | 1569 | result = write_midx_internal(object_dir, m, NULL, NULL, flags); |
Derrick Stolee | ce1e4a1 | 2019-06-10 16:35:27 -0700 | [diff] [blame] | 1570 | m = NULL; |
| 1571 | |
| 1572 | cleanup: |
| 1573 | if (m) |
| 1574 | close_midx(m); |
| 1575 | free(include_pack); |
| 1576 | return result; |
Derrick Stolee | 2af890b | 2019-06-10 16:35:26 -0700 | [diff] [blame] | 1577 | } |