THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
5.6KB

  1. #include "cache.h"
  2. #include "repository.h"
  3. #include "pack.h"
  4. #include "pack-revindex.h"
  5. #include "progress.h"
  6. #include "packfile.h"
  7. #include "object-store.h"
  8. struct idx_entry {
  9. off_t offset;
  10. union idx_entry_object {
  11. const unsigned char *hash;
  12. struct object_id *oid;
  13. } oid;
  14. unsigned int nr;
  15. };
  16. static int compare_entries(const void *e1, const void *e2)
  17. {
  18. const struct idx_entry *entry1 = e1;
  19. const struct idx_entry *entry2 = e2;
  20. if (entry1->offset < entry2->offset)
  21. return -1;
  22. if (entry1->offset > entry2->offset)
  23. return 1;
  24. return 0;
  25. }
  26. int check_pack_crc(struct packed_git *p, struct pack_window **w_curs,
  27. off_t offset, off_t len, unsigned int nr)
  28. {
  29. const uint32_t *index_crc;
  30. uint32_t data_crc = crc32(0, NULL, 0);
  31. do {
  32. unsigned long avail;
  33. void *data = use_pack(p, w_curs, offset, &avail);
  34. if (avail > len)
  35. avail = len;
  36. data_crc = crc32(data_crc, data, avail);
  37. offset += avail;
  38. len -= avail;
  39. } while (len);
  40. index_crc = p->index_data;
  41. index_crc += 2 + 256 + p->num_objects * (the_hash_algo->rawsz/4) + nr;
  42. return data_crc != ntohl(*index_crc);
  43. }
  44. static int verify_packfile(struct repository *r,
  45. struct packed_git *p,
  46. struct pack_window **w_curs,
  47. verify_fn fn,
  48. struct progress *progress, uint32_t base_count)
  49. {
  50. off_t index_size = p->index_size;
  51. const unsigned char *index_base = p->index_data;
  52. git_hash_ctx ctx;
  53. unsigned char hash[GIT_MAX_RAWSZ], *pack_sig;
  54. off_t offset = 0, pack_sig_ofs = 0;
  55. uint32_t nr_objects, i;
  56. int err = 0;
  57. struct idx_entry *entries;
  58. if (!is_pack_valid(p))
  59. return error("packfile %s cannot be accessed", p->pack_name);
  60. the_hash_algo->init_fn(&ctx);
  61. do {
  62. unsigned long remaining;
  63. unsigned char *in = use_pack(p, w_curs, offset, &remaining);
  64. offset += remaining;
  65. if (!pack_sig_ofs)
  66. pack_sig_ofs = p->pack_size - the_hash_algo->rawsz;
  67. if (offset > pack_sig_ofs)
  68. remaining -= (unsigned int)(offset - pack_sig_ofs);
  69. the_hash_algo->update_fn(&ctx, in, remaining);
  70. } while (offset < pack_sig_ofs);
  71. the_hash_algo->final_fn(hash, &ctx);
  72. pack_sig = use_pack(p, w_curs, pack_sig_ofs, NULL);
  73. if (!hasheq(hash, pack_sig))
  74. err = error("%s pack checksum mismatch",
  75. p->pack_name);
  76. if (!hasheq(index_base + index_size - the_hash_algo->hexsz, pack_sig))
  77. err = error("%s pack checksum does not match its index",
  78. p->pack_name);
  79. unuse_pack(w_curs);
  80. /* Make sure everything reachable from idx is valid. Since we
  81. * have verified that nr_objects matches between idx and pack,
  82. * we do not do scan-streaming check on the pack file.
  83. */
  84. nr_objects = p->num_objects;
  85. ALLOC_ARRAY(entries, nr_objects + 1);
  86. entries[nr_objects].offset = pack_sig_ofs;
  87. /* first sort entries by pack offset, since unpacking them is more efficient that way */
  88. for (i = 0; i < nr_objects; i++) {
  89. entries[i].oid.hash = nth_packed_object_sha1(p, i);
  90. if (!entries[i].oid.hash)
  91. die("internal error pack-check nth-packed-object");
  92. entries[i].offset = nth_packed_object_offset(p, i);
  93. entries[i].nr = i;
  94. }
  95. QSORT(entries, nr_objects, compare_entries);
  96. for (i = 0; i < nr_objects; i++) {
  97. void *data;
  98. enum object_type type;
  99. unsigned long size;
  100. off_t curpos;
  101. int data_valid;
  102. if (p->index_version > 1) {
  103. off_t offset = entries[i].offset;
  104. off_t len = entries[i+1].offset - offset;
  105. unsigned int nr = entries[i].nr;
  106. if (check_pack_crc(p, w_curs, offset, len, nr))
  107. err = error("index CRC mismatch for object %s "
  108. "from %s at offset %"PRIuMAX"",
  109. oid_to_hex(entries[i].oid.oid),
  110. p->pack_name, (uintmax_t)offset);
  111. }
  112. curpos = entries[i].offset;
  113. type = unpack_object_header(p, w_curs, &curpos, &size);
  114. unuse_pack(w_curs);
  115. if (type == OBJ_BLOB && big_file_threshold <= size) {
  116. /*
  117. * Let check_object_signature() check it with
  118. * the streaming interface; no point slurping
  119. * the data in-core only to discard.
  120. */
  121. data = NULL;
  122. data_valid = 0;
  123. } else {
  124. data = unpack_entry(r, p, entries[i].offset, &type, &size);
  125. data_valid = 1;
  126. }
  127. if (data_valid && !data)
  128. err = error("cannot unpack %s from %s at offset %"PRIuMAX"",
  129. oid_to_hex(entries[i].oid.oid), p->pack_name,
  130. (uintmax_t)entries[i].offset);
  131. else if (check_object_signature(entries[i].oid.oid, data, size, type_name(type)))
  132. err = error("packed %s from %s is corrupt",
  133. oid_to_hex(entries[i].oid.oid), p->pack_name);
  134. else if (fn) {
  135. int eaten = 0;
  136. err |= fn(entries[i].oid.oid, type, size, data, &eaten);
  137. if (eaten)
  138. data = NULL;
  139. }
  140. if (((base_count + i) & 1023) == 0)
  141. display_progress(progress, base_count + i);
  142. free(data);
  143. }
  144. display_progress(progress, base_count + i);
  145. free(entries);
  146. return err;
  147. }
  148. int verify_pack_index(struct packed_git *p)
  149. {
  150. off_t index_size;
  151. const unsigned char *index_base;
  152. git_hash_ctx ctx;
  153. unsigned char hash[GIT_MAX_RAWSZ];
  154. int err = 0;
  155. if (open_pack_index(p))
  156. return error("packfile %s index not opened", p->pack_name);
  157. index_size = p->index_size;
  158. index_base = p->index_data;
  159. /* Verify SHA1 sum of the index file */
  160. the_hash_algo->init_fn(&ctx);
  161. the_hash_algo->update_fn(&ctx, index_base, (unsigned int)(index_size - the_hash_algo->rawsz));
  162. the_hash_algo->final_fn(hash, &ctx);
  163. if (!hasheq(hash, index_base + index_size - the_hash_algo->rawsz))
  164. err = error("Packfile index for %s hash mismatch",
  165. p->pack_name);
  166. return err;
  167. }
  168. int verify_pack(struct repository *r, struct packed_git *p, verify_fn fn,
  169. struct progress *progress, uint32_t base_count)
  170. {
  171. int err = 0;
  172. struct pack_window *w_curs = NULL;
  173. err |= verify_pack_index(p);
  174. if (!p->index_data)
  175. return -1;
  176. err |= verify_packfile(r, p, &w_curs, fn, progress, base_count);
  177. unuse_pack(&w_curs);
  178. return err;
  179. }