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