Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/pack-check.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / pack-check.c` 211 lines ยท 5873 bytes Raw Blame History 1 #define DISABLE_SIGN_COMPARE_WARNINGS 2 3 #include "git-compat-util.h" 4 #include "environment.h" 5 #include "hex.h" 6 #include "repository.h" 7 #include "pack.h" 8 #include "progress.h" 9 #include "packfile.h" 10 #include "object-file.h" 11 #include "odb.h" 12 #include "odb/streaming.h" 13 14 struct idx_entry { 15 off_t offset; 16 unsigned int nr; 17 }; 18 19 static int compare_entries(const void *e1, const void *e2) 20 { 21 const struct idx_entry *entry1 = e1; 22 const struct idx_entry *entry2 = e2; 23 if (entry1->offset < entry2->offset) 24 return -1; 25 if (entry1->offset > entry2->offset) 26 return 1; 27 return 0; 28 } 29 30 int check_pack_crc(struct packed_git *p, struct pack_window **w_curs, 31 off_t offset, off_t len, unsigned int nr) 32 { 33 const uint32_t *index_crc; 34 uint32_t data_crc = crc32(0, NULL, 0); 35 36 do { 37 size_t avail; 38 void *data = use_pack(p, w_curs, offset, &avail); 39 if (avail > len) 40 avail = len; 41 data_crc = crc32(data_crc, data, avail); 42 offset += avail; 43 len -= avail; 44 } while (len); 45 46 index_crc = p->index_data; 47 index_crc += 2 + 256 + (size_t)p->num_objects * (p->repo->hash_algo->rawsz/4) + nr; 48 49 return data_crc != ntohl(*index_crc); 50 } 51 52 static int verify_packfile(struct repository *r, 53 struct packed_git *p, 54 struct pack_window **w_curs, 55 verify_fn fn, 56 void *fn_data, 57 struct progress *progress, uint32_t base_count) 58 59 { 60 off_t index_size = p->index_size; 61 const unsigned char *index_base = p->index_data; 62 struct git_hash_ctx ctx; 63 unsigned char hash[GIT_MAX_RAWSZ], *pack_sig; 64 off_t offset = 0, pack_sig_ofs = 0; 65 uint32_t nr_objects, i; 66 int err = 0; 67 struct idx_entry *entries; 68 69 if (!is_pack_valid(p)) 70 return error("packfile %s cannot be accessed", p->pack_name); 71 72 git_hash_init(&ctx, r->hash_algo); 73 do { 74 size_t remaining; 75 unsigned char *in = use_pack(p, w_curs, offset, &remaining); 76 offset += remaining; 77 if (!pack_sig_ofs) 78 pack_sig_ofs = p->pack_size - r->hash_algo->rawsz; 79 if (offset > pack_sig_ofs) 80 remaining -= (unsigned int)(offset - pack_sig_ofs); 81 git_hash_update(&ctx, in, remaining); 82 } while (offset < pack_sig_ofs); 83 git_hash_final(hash, &ctx); 84 pack_sig = use_pack(p, w_curs, pack_sig_ofs, NULL); 85 if (!hasheq(hash, pack_sig, r->hash_algo)) 86 err = error("%s pack checksum mismatch", 87 p->pack_name); 88 if (!hasheq(index_base + index_size - r->hash_algo->hexsz, pack_sig, 89 r->hash_algo)) 90 err = error("%s pack checksum does not match its index", 91 p->pack_name); 92 unuse_pack(w_curs); 93 94 /* Make sure everything reachable from idx is valid. Since we 95 * have verified that nr_objects matches between idx and pack, 96 * we do not do scan-streaming check on the pack file. 97 */ 98 nr_objects = p->num_objects; 99 ALLOC_ARRAY(entries, nr_objects + 1); 100 entries[nr_objects].offset = pack_sig_ofs; 101 /* first sort entries by pack offset, since unpacking them is more efficient that way */ 102 for (i = 0; i < nr_objects; i++) { 103 entries[i].offset = nth_packed_object_offset(p, i); 104 entries[i].nr = i; 105 } 106 QSORT(entries, nr_objects, compare_entries); 107 108 for (i = 0; i < nr_objects; i++) { 109 struct odb_stream *stream = NULL; 110 void *data; 111 struct object_id oid; 112 enum object_type type; 113 size_t size; 114 off_t curpos; 115 int data_valid; 116 117 if (nth_packed_object_id(&oid, p, entries[i].nr) < 0) 118 BUG("unable to get oid of object %lu from %s", 119 (unsigned long)entries[i].nr, p->pack_name); 120 121 if (p->index_version > 1) { 122 off_t offset = entries[i].offset; 123 off_t len = entries[i+1].offset - offset; 124 unsigned int nr = entries[i].nr; 125 if (check_pack_crc(p, w_curs, offset, len, nr)) 126 err = error("index CRC mismatch for object %s " 127 "from %s at offset %"PRIuMAX"", 128 oid_to_hex(&oid), 129 p->pack_name, (uintmax_t)offset); 130 } 131 132 curpos = entries[i].offset; 133 type = unpack_object_header(p, w_curs, &curpos, &size); 134 unuse_pack(w_curs); 135 136 if (type == OBJ_BLOB && 137 repo_settings_get_big_file_threshold(r) < size) { 138 /* 139 * Let stream_object_signature() check it with 140 * the streaming interface; no point slurping 141 * the data in-core only to discard. 142 */ 143 data = NULL; 144 data_valid = 0; 145 } else { 146 data = unpack_entry(r, p, entries[i].offset, &type, 147 &size); 148 data_valid = 1; 149 } 150 151 if (data_valid && !data) 152 err = error("cannot unpack %s from %s at offset %"PRIuMAX"", 153 oid_to_hex(&oid), p->pack_name, 154 (uintmax_t)entries[i].offset); 155 else if (data && check_object_signature(r, &oid, data, size, 156 type) < 0) 157 err = error("packed %s from %s is corrupt", 158 oid_to_hex(&oid), p->pack_name); 159 else if (!data && 160 (packfile_read_object_stream(&stream, &oid, p, entries[i].offset) < 0 || 161 stream_object_signature(r, stream, &oid) < 0)) 162 err = error("packed %s from %s is corrupt", 163 oid_to_hex(&oid), p->pack_name); 164 else if (fn) { 165 int eaten = 0; 166 err |= fn(&oid, type, size, data, &eaten, fn_data); 167 if (eaten) 168 data = NULL; 169 } 170 if (((base_count + i) & 1023) == 0) 171 display_progress(progress, base_count + i); 172 173 if (stream) 174 odb_stream_close(stream); 175 free(data); 176 } 177 178 display_progress(progress, base_count + i); 179 free(entries); 180 return err; 181 } 182 183 int verify_pack_index(struct packed_git *p) 184 { 185 int err = 0; 186 187 if (open_pack_index(p)) 188 return error("packfile %s index not opened", p->pack_name); 189 190 /* Verify SHA1 sum of the index file */ 191 if (!hashfile_checksum_valid(p->repo->hash_algo, p->index_data, p->index_size)) 192 err = error("Packfile index for %s hash mismatch", 193 p->pack_name); 194 return err; 195 } 196 197 int verify_pack(struct repository *r, struct packed_git *p, verify_fn fn, void *fn_data, 198 struct progress *progress, uint32_t base_count) 199 { 200 int err = 0; 201 struct pack_window *w_curs = NULL; 202 203 err |= verify_pack_index(p); 204 if (!p->index_data) 205 return -1; 206 207 err |= verify_packfile(r, p, &w_curs, fn, fn_data, progress, base_count); 208 unuse_pack(&w_curs); 209 210 return err; 211 }