unpack-objects.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. #include "builtin.h"
  2. #include "cache.h"
  3. #include "config.h"
  4. #include "object-store.h"
  5. #include "object.h"
  6. #include "delta.h"
  7. #include "pack.h"
  8. #include "blob.h"
  9. #include "commit.h"
  10. #include "tag.h"
  11. #include "tree.h"
  12. #include "tree-walk.h"
  13. #include "progress.h"
  14. #include "decorate.h"
  15. #include "fsck.h"
  16. static int dry_run, quiet, recover, has_errors, strict;
  17. static const char unpack_usage[] = "git unpack-objects [-n] [-q] [-r] [--strict]";
  18. /* We always read in 4kB chunks. */
  19. static unsigned char buffer[4096];
  20. static unsigned int offset, len;
  21. static off_t consumed_bytes;
  22. static off_t max_input_size;
  23. static git_hash_ctx ctx;
  24. static struct fsck_options fsck_options = FSCK_OPTIONS_STRICT;
  25. static struct progress *progress;
  26. /*
  27. * When running under --strict mode, objects whose reachability are
  28. * suspect are kept in core without getting written in the object
  29. * store.
  30. */
  31. struct obj_buffer {
  32. char *buffer;
  33. unsigned long size;
  34. };
  35. static struct decoration obj_decorate;
  36. static struct obj_buffer *lookup_object_buffer(struct object *base)
  37. {
  38. return lookup_decoration(&obj_decorate, base);
  39. }
  40. static void add_object_buffer(struct object *object, char *buffer, unsigned long size)
  41. {
  42. struct obj_buffer *obj;
  43. obj = xcalloc(1, sizeof(struct obj_buffer));
  44. obj->buffer = buffer;
  45. obj->size = size;
  46. if (add_decoration(&obj_decorate, object, obj))
  47. die("object %s tried to add buffer twice!", oid_to_hex(&object->oid));
  48. }
  49. /*
  50. * Make sure at least "min" bytes are available in the buffer, and
  51. * return the pointer to the buffer.
  52. */
  53. static void *fill(int min)
  54. {
  55. if (min <= len)
  56. return buffer + offset;
  57. if (min > sizeof(buffer))
  58. die("cannot fill %d bytes", min);
  59. if (offset) {
  60. the_hash_algo->update_fn(&ctx, buffer, offset);
  61. memmove(buffer, buffer + offset, len);
  62. offset = 0;
  63. }
  64. do {
  65. ssize_t ret = xread(0, buffer + len, sizeof(buffer) - len);
  66. if (ret <= 0) {
  67. if (!ret)
  68. die("early EOF");
  69. die_errno("read error on input");
  70. }
  71. len += ret;
  72. } while (len < min);
  73. return buffer;
  74. }
  75. static void use(int bytes)
  76. {
  77. if (bytes > len)
  78. die("used more bytes than were available");
  79. len -= bytes;
  80. offset += bytes;
  81. /* make sure off_t is sufficiently large not to wrap */
  82. if (signed_add_overflows(consumed_bytes, bytes))
  83. die("pack too large for current definition of off_t");
  84. consumed_bytes += bytes;
  85. if (max_input_size && consumed_bytes > max_input_size)
  86. die(_("pack exceeds maximum allowed size"));
  87. display_throughput(progress, consumed_bytes);
  88. }
  89. static void *get_data(unsigned long size)
  90. {
  91. git_zstream stream;
  92. void *buf = xmallocz(size);
  93. memset(&stream, 0, sizeof(stream));
  94. stream.next_out = buf;
  95. stream.avail_out = size;
  96. stream.next_in = fill(1);
  97. stream.avail_in = len;
  98. git_inflate_init(&stream);
  99. for (;;) {
  100. int ret = git_inflate(&stream, 0);
  101. use(len - stream.avail_in);
  102. if (stream.total_out == size && ret == Z_STREAM_END)
  103. break;
  104. if (ret != Z_OK) {
  105. error("inflate returned %d", ret);
  106. FREE_AND_NULL(buf);
  107. if (!recover)
  108. exit(1);
  109. has_errors = 1;
  110. break;
  111. }
  112. stream.next_in = fill(1);
  113. stream.avail_in = len;
  114. }
  115. git_inflate_end(&stream);
  116. return buf;
  117. }
  118. struct delta_info {
  119. struct object_id base_oid;
  120. unsigned nr;
  121. off_t base_offset;
  122. unsigned long size;
  123. void *delta;
  124. struct delta_info *next;
  125. };
  126. static struct delta_info *delta_list;
  127. static void add_delta_to_list(unsigned nr, const struct object_id *base_oid,
  128. off_t base_offset,
  129. void *delta, unsigned long size)
  130. {
  131. struct delta_info *info = xmalloc(sizeof(*info));
  132. oidcpy(&info->base_oid, base_oid);
  133. info->base_offset = base_offset;
  134. info->size = size;
  135. info->delta = delta;
  136. info->nr = nr;
  137. info->next = delta_list;
  138. delta_list = info;
  139. }
  140. struct obj_info {
  141. off_t offset;
  142. struct object_id oid;
  143. struct object *obj;
  144. };
  145. /* Remember to update object flag allocation in object.h */
  146. #define FLAG_OPEN (1u<<20)
  147. #define FLAG_WRITTEN (1u<<21)
  148. static struct obj_info *obj_list;
  149. static unsigned nr_objects;
  150. /*
  151. * Called only from check_object() after it verified this object
  152. * is Ok.
  153. */
  154. static void write_cached_object(struct object *obj, struct obj_buffer *obj_buf)
  155. {
  156. struct object_id oid;
  157. if (write_object_file(obj_buf->buffer, obj_buf->size,
  158. type_name(obj->type), &oid) < 0)
  159. die("failed to write object %s", oid_to_hex(&obj->oid));
  160. obj->flags |= FLAG_WRITTEN;
  161. }
  162. /*
  163. * At the very end of the processing, write_rest() scans the objects
  164. * that have reachability requirements and calls this function.
  165. * Verify its reachability and validity recursively and write it out.
  166. */
  167. static int check_object(struct object *obj, int type, void *data, struct fsck_options *options)
  168. {
  169. struct obj_buffer *obj_buf;
  170. if (!obj)
  171. return 1;
  172. if (obj->flags & FLAG_WRITTEN)
  173. return 0;
  174. if (type != OBJ_ANY && obj->type != type)
  175. die("object type mismatch");
  176. if (!(obj->flags & FLAG_OPEN)) {
  177. unsigned long size;
  178. int type = oid_object_info(the_repository, &obj->oid, &size);
  179. if (type != obj->type || type <= 0)
  180. die("object of unexpected type");
  181. obj->flags |= FLAG_WRITTEN;
  182. return 0;
  183. }
  184. obj_buf = lookup_object_buffer(obj);
  185. if (!obj_buf)
  186. die("Whoops! Cannot find object '%s'", oid_to_hex(&obj->oid));
  187. if (fsck_object(obj, obj_buf->buffer, obj_buf->size, &fsck_options))
  188. die("fsck error in packed object");
  189. fsck_options.walk = check_object;
  190. if (fsck_walk(obj, NULL, &fsck_options))
  191. die("Error on reachable objects of %s", oid_to_hex(&obj->oid));
  192. write_cached_object(obj, obj_buf);
  193. return 0;
  194. }
  195. static void write_rest(void)
  196. {
  197. unsigned i;
  198. for (i = 0; i < nr_objects; i++) {
  199. if (obj_list[i].obj)
  200. check_object(obj_list[i].obj, OBJ_ANY, NULL, NULL);
  201. }
  202. }
  203. static void added_object(unsigned nr, enum object_type type,
  204. void *data, unsigned long size);
  205. /*
  206. * Write out nr-th object from the list, now we know the contents
  207. * of it. Under --strict, this buffers structured objects in-core,
  208. * to be checked at the end.
  209. */
  210. static void write_object(unsigned nr, enum object_type type,
  211. void *buf, unsigned long size)
  212. {
  213. if (!strict) {
  214. if (write_object_file(buf, size, type_name(type),
  215. &obj_list[nr].oid) < 0)
  216. die("failed to write object");
  217. added_object(nr, type, buf, size);
  218. free(buf);
  219. obj_list[nr].obj = NULL;
  220. } else if (type == OBJ_BLOB) {
  221. struct blob *blob;
  222. if (write_object_file(buf, size, type_name(type),
  223. &obj_list[nr].oid) < 0)
  224. die("failed to write object");
  225. added_object(nr, type, buf, size);
  226. free(buf);
  227. blob = lookup_blob(the_repository, &obj_list[nr].oid);
  228. if (blob)
  229. blob->object.flags |= FLAG_WRITTEN;
  230. else
  231. die("invalid blob object");
  232. obj_list[nr].obj = NULL;
  233. } else {
  234. struct object *obj;
  235. int eaten;
  236. hash_object_file(the_hash_algo, buf, size, type_name(type),
  237. &obj_list[nr].oid);
  238. added_object(nr, type, buf, size);
  239. obj = parse_object_buffer(the_repository, &obj_list[nr].oid,
  240. type, size, buf,
  241. &eaten);
  242. if (!obj)
  243. die("invalid %s", type_name(type));
  244. add_object_buffer(obj, buf, size);
  245. obj->flags |= FLAG_OPEN;
  246. obj_list[nr].obj = obj;
  247. }
  248. }
  249. static void resolve_delta(unsigned nr, enum object_type type,
  250. void *base, unsigned long base_size,
  251. void *delta, unsigned long delta_size)
  252. {
  253. void *result;
  254. unsigned long result_size;
  255. result = patch_delta(base, base_size,
  256. delta, delta_size,
  257. &result_size);
  258. if (!result)
  259. die("failed to apply delta");
  260. free(delta);
  261. write_object(nr, type, result, result_size);
  262. }
  263. /*
  264. * We now know the contents of an object (which is nr-th in the pack);
  265. * resolve all the deltified objects that are based on it.
  266. */
  267. static void added_object(unsigned nr, enum object_type type,
  268. void *data, unsigned long size)
  269. {
  270. struct delta_info **p = &delta_list;
  271. struct delta_info *info;
  272. while ((info = *p) != NULL) {
  273. if (oideq(&info->base_oid, &obj_list[nr].oid) ||
  274. info->base_offset == obj_list[nr].offset) {
  275. *p = info->next;
  276. p = &delta_list;
  277. resolve_delta(info->nr, type, data, size,
  278. info->delta, info->size);
  279. free(info);
  280. continue;
  281. }
  282. p = &info->next;
  283. }
  284. }
  285. static void unpack_non_delta_entry(enum object_type type, unsigned long size,
  286. unsigned nr)
  287. {
  288. void *buf = get_data(size);
  289. if (!dry_run && buf)
  290. write_object(nr, type, buf, size);
  291. else
  292. free(buf);
  293. }
  294. static int resolve_against_held(unsigned nr, const struct object_id *base,
  295. void *delta_data, unsigned long delta_size)
  296. {
  297. struct object *obj;
  298. struct obj_buffer *obj_buffer;
  299. obj = lookup_object(the_repository, base);
  300. if (!obj)
  301. return 0;
  302. obj_buffer = lookup_object_buffer(obj);
  303. if (!obj_buffer)
  304. return 0;
  305. resolve_delta(nr, obj->type, obj_buffer->buffer,
  306. obj_buffer->size, delta_data, delta_size);
  307. return 1;
  308. }
  309. static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
  310. unsigned nr)
  311. {
  312. void *delta_data, *base;
  313. unsigned long base_size;
  314. struct object_id base_oid;
  315. if (type == OBJ_REF_DELTA) {
  316. hashcpy(base_oid.hash, fill(the_hash_algo->rawsz));
  317. use(the_hash_algo->rawsz);
  318. delta_data = get_data(delta_size);
  319. if (dry_run || !delta_data) {
  320. free(delta_data);
  321. return;
  322. }
  323. if (has_object_file(&base_oid))
  324. ; /* Ok we have this one */
  325. else if (resolve_against_held(nr, &base_oid,
  326. delta_data, delta_size))
  327. return; /* we are done */
  328. else {
  329. /* cannot resolve yet --- queue it */
  330. oidclr(&obj_list[nr].oid);
  331. add_delta_to_list(nr, &base_oid, 0, delta_data, delta_size);
  332. return;
  333. }
  334. } else {
  335. unsigned base_found = 0;
  336. unsigned char *pack, c;
  337. off_t base_offset;
  338. unsigned lo, mid, hi;
  339. pack = fill(1);
  340. c = *pack;
  341. use(1);
  342. base_offset = c & 127;
  343. while (c & 128) {
  344. base_offset += 1;
  345. if (!base_offset || MSB(base_offset, 7))
  346. die("offset value overflow for delta base object");
  347. pack = fill(1);
  348. c = *pack;
  349. use(1);
  350. base_offset = (base_offset << 7) + (c & 127);
  351. }
  352. base_offset = obj_list[nr].offset - base_offset;
  353. if (base_offset <= 0 || base_offset >= obj_list[nr].offset)
  354. die("offset value out of bound for delta base object");
  355. delta_data = get_data(delta_size);
  356. if (dry_run || !delta_data) {
  357. free(delta_data);
  358. return;
  359. }
  360. lo = 0;
  361. hi = nr;
  362. while (lo < hi) {
  363. mid = lo + (hi - lo) / 2;
  364. if (base_offset < obj_list[mid].offset) {
  365. hi = mid;
  366. } else if (base_offset > obj_list[mid].offset) {
  367. lo = mid + 1;
  368. } else {
  369. oidcpy(&base_oid, &obj_list[mid].oid);
  370. base_found = !is_null_oid(&base_oid);
  371. break;
  372. }
  373. }
  374. if (!base_found) {
  375. /*
  376. * The delta base object is itself a delta that
  377. * has not been resolved yet.
  378. */
  379. oidclr(&obj_list[nr].oid);
  380. add_delta_to_list(nr, &null_oid, base_offset, delta_data, delta_size);
  381. return;
  382. }
  383. }
  384. if (resolve_against_held(nr, &base_oid, delta_data, delta_size))
  385. return;
  386. base = read_object_file(&base_oid, &type, &base_size);
  387. if (!base) {
  388. error("failed to read delta-pack base object %s",
  389. oid_to_hex(&base_oid));
  390. if (!recover)
  391. exit(1);
  392. has_errors = 1;
  393. return;
  394. }
  395. resolve_delta(nr, type, base, base_size, delta_data, delta_size);
  396. free(base);
  397. }
  398. static void unpack_one(unsigned nr)
  399. {
  400. unsigned shift;
  401. unsigned char *pack;
  402. unsigned long size, c;
  403. enum object_type type;
  404. obj_list[nr].offset = consumed_bytes;
  405. pack = fill(1);
  406. c = *pack;
  407. use(1);
  408. type = (c >> 4) & 7;
  409. size = (c & 15);
  410. shift = 4;
  411. while (c & 0x80) {
  412. pack = fill(1);
  413. c = *pack;
  414. use(1);
  415. size += (c & 0x7f) << shift;
  416. shift += 7;
  417. }
  418. switch (type) {
  419. case OBJ_COMMIT:
  420. case OBJ_TREE:
  421. case OBJ_BLOB:
  422. case OBJ_TAG:
  423. unpack_non_delta_entry(type, size, nr);
  424. return;
  425. case OBJ_REF_DELTA:
  426. case OBJ_OFS_DELTA:
  427. unpack_delta_entry(type, size, nr);
  428. return;
  429. default:
  430. error("bad object type %d", type);
  431. has_errors = 1;
  432. if (recover)
  433. return;
  434. exit(1);
  435. }
  436. }
  437. static void unpack_all(void)
  438. {
  439. int i;
  440. struct pack_header *hdr = fill(sizeof(struct pack_header));
  441. nr_objects = ntohl(hdr->hdr_entries);
  442. if (ntohl(hdr->hdr_signature) != PACK_SIGNATURE)
  443. die("bad pack file");
  444. if (!pack_version_ok(hdr->hdr_version))
  445. die("unknown pack file version %"PRIu32,
  446. ntohl(hdr->hdr_version));
  447. use(sizeof(struct pack_header));
  448. if (!quiet)
  449. progress = start_progress(_("Unpacking objects"), nr_objects);
  450. obj_list = xcalloc(nr_objects, sizeof(*obj_list));
  451. for (i = 0; i < nr_objects; i++) {
  452. unpack_one(i);
  453. display_progress(progress, i + 1);
  454. }
  455. stop_progress(&progress);
  456. if (delta_list)
  457. die("unresolved deltas left after unpacking");
  458. }
  459. int cmd_unpack_objects(int argc, const char **argv, const char *prefix)
  460. {
  461. int i;
  462. struct object_id oid;
  463. read_replace_refs = 0;
  464. git_config(git_default_config, NULL);
  465. quiet = !isatty(2);
  466. for (i = 1 ; i < argc; i++) {
  467. const char *arg = argv[i];
  468. if (*arg == '-') {
  469. if (!strcmp(arg, "-n")) {
  470. dry_run = 1;
  471. continue;
  472. }
  473. if (!strcmp(arg, "-q")) {
  474. quiet = 1;
  475. continue;
  476. }
  477. if (!strcmp(arg, "-r")) {
  478. recover = 1;
  479. continue;
  480. }
  481. if (!strcmp(arg, "--strict")) {
  482. strict = 1;
  483. continue;
  484. }
  485. if (skip_prefix(arg, "--strict=", &arg)) {
  486. strict = 1;
  487. fsck_set_msg_types(&fsck_options, arg);
  488. continue;
  489. }
  490. if (starts_with(arg, "--pack_header=")) {
  491. struct pack_header *hdr;
  492. char *c;
  493. hdr = (struct pack_header *)buffer;
  494. hdr->hdr_signature = htonl(PACK_SIGNATURE);
  495. hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10));
  496. if (*c != ',')
  497. die("bad %s", arg);
  498. hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));
  499. if (*c)
  500. die("bad %s", arg);
  501. len = sizeof(*hdr);
  502. continue;
  503. }
  504. if (skip_prefix(arg, "--max-input-size=", &arg)) {
  505. max_input_size = strtoumax(arg, NULL, 10);
  506. continue;
  507. }
  508. usage(unpack_usage);
  509. }
  510. /* We don't take any non-flag arguments now.. Maybe some day */
  511. usage(unpack_usage);
  512. }
  513. the_hash_algo->init_fn(&ctx);
  514. unpack_all();
  515. the_hash_algo->update_fn(&ctx, buffer, offset);
  516. the_hash_algo->final_fn(oid.hash, &ctx);
  517. if (strict) {
  518. write_rest();
  519. if (fsck_finish(&fsck_options))
  520. die(_("fsck error in pack objects"));
  521. }
  522. if (!hasheq(fill(the_hash_algo->rawsz), oid.hash))
  523. die("final sha1 did not match");
  524. use(the_hash_algo->rawsz);
  525. /* Write the last part of the buffer to stdout */
  526. while (len) {
  527. int ret = xwrite(1, buffer + offset, len);
  528. if (ret <= 0)
  529. break;
  530. len -= ret;
  531. offset += ret;
  532. }
  533. /* All done */
  534. return has_errors;
  535. }