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/odb/source-loose.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 / odb / source-loose.c` 1136 lines ยท 30396 bytes Raw Blame History 1 #include "git-compat-util.h" 2 #include "abspath.h" 3 #include "chdir-notify.h" 4 #include "gettext.h" 5 #include "hex.h" 6 #include "loose.h" 7 #include "object-file.h" 8 #include "object-file-convert.h" 9 #include "odb.h" 10 #include "odb/source-files.h" 11 #include "odb/source-loose.h" 12 #include "odb/streaming.h" 13 #include "oidtree.h" 14 #include "path.h" 15 #include "progress.h" 16 #include "repository.h" 17 #include "strbuf.h" 18 #include "tempfile.h" 19 #include "write-or-die.h" 20 21 static int append_loose_object(const struct object_id *oid, 22 const char *path UNUSED, 23 void *data) 24 { 25 oidtree_insert(data, oid, NULL); 26 return 0; 27 } 28 29 static struct oidtree *odb_source_loose_cache(struct odb_source_loose *loose, 30 const struct object_id *oid) 31 { 32 int subdir_nr = oid->hash[0]; 33 struct strbuf buf = STRBUF_INIT; 34 size_t word_bits = bitsizeof(loose->subdir_seen[0]); 35 size_t word_index = subdir_nr / word_bits; 36 size_t mask = (size_t)1u << (subdir_nr % word_bits); 37 uint32_t *bitmap; 38 39 if (subdir_nr < 0 || 40 (size_t) subdir_nr >= bitsizeof(loose->subdir_seen)) 41 BUG("subdir_nr out of range"); 42 43 bitmap = &loose->subdir_seen[word_index]; 44 if (*bitmap & mask) 45 return loose->cache; 46 if (!loose->cache) { 47 ALLOC_ARRAY(loose->cache, 1); 48 oidtree_init(loose->cache); 49 } 50 strbuf_addstr(&buf, loose->base.path); 51 for_each_file_in_obj_subdir(subdir_nr, &buf, 52 loose->base.odb->repo->hash_algo, 53 append_loose_object, 54 NULL, NULL, 55 loose->cache); 56 *bitmap |= mask; 57 strbuf_release(&buf); 58 return loose->cache; 59 } 60 61 static int quick_has_loose(struct odb_source_loose *loose, 62 const struct object_id *oid) 63 { 64 return !!oidtree_contains(odb_source_loose_cache(loose, oid), oid); 65 } 66 67 static int read_object_info_from_path(struct odb_source_loose *loose, 68 const char *path, 69 const struct object_id *oid, 70 struct object_info *oi, 71 enum object_info_flags flags, 72 struct strbuf *errmsg) 73 { 74 int ret; 75 int fd; 76 unsigned long mapsize; 77 void *map = NULL; 78 git_zstream stream, *stream_to_end = NULL; 79 char hdr[MAX_HEADER_LEN]; 80 size_t size_scratch; 81 enum object_type type_scratch; 82 struct stat st; 83 84 /* 85 * If we don't care about type or size, then we don't 86 * need to look inside the object at all. Note that we 87 * do not optimize out the stat call, even if the 88 * caller doesn't care about the disk-size, since our 89 * return value implicitly indicates whether the 90 * object even exists. 91 */ 92 if (!oi || (!oi->typep && !oi->sizep && !oi->contentp)) { 93 struct stat st; 94 95 if ((!oi || (!oi->disk_sizep && !oi->mtimep)) && (flags & OBJECT_INFO_QUICK)) { 96 ret = quick_has_loose(loose, oid) ? 0 : ODB_READ_NOT_FOUND; 97 goto out; 98 } 99 100 if (lstat(path, &st) < 0) { 101 if (errno == ENOENT) { 102 ret = ODB_READ_NOT_FOUND; 103 goto out; 104 } 105 106 ret = -1; 107 goto out; 108 } 109 110 if (oi) { 111 if (oi->disk_sizep) 112 *oi->disk_sizep = st.st_size; 113 if (oi->mtimep) 114 *oi->mtimep = st.st_mtime; 115 } 116 117 ret = 0; 118 goto out; 119 } 120 121 fd = git_open(path); 122 if (fd < 0) { 123 if (errno == ENOENT) { 124 ret = ODB_READ_NOT_FOUND; 125 goto out; 126 } 127 128 ret = error_errno(_("unable to open loose object %s"), oid_to_hex(oid)); 129 goto out; 130 } 131 132 if (fstat(fd, &st)) { 133 close(fd); 134 ret = -1; 135 goto out; 136 } 137 138 mapsize = xsize_t(st.st_size); 139 if (!mapsize) { 140 close(fd); 141 ret = error(_("object file %s is empty"), path); 142 goto out; 143 } 144 145 map = xmmap(NULL, mapsize, PROT_READ, MAP_PRIVATE, fd, 0); 146 close(fd); 147 if (!map) { 148 ret = -1; 149 goto out; 150 } 151 152 if (oi->disk_sizep) 153 *oi->disk_sizep = mapsize; 154 if (oi->mtimep) 155 *oi->mtimep = st.st_mtime; 156 157 stream_to_end = &stream; 158 159 switch (unpack_loose_header(&stream, map, mapsize, hdr, sizeof(hdr))) { 160 case ULHR_OK: 161 if (!oi->sizep) 162 oi->sizep = &size_scratch; 163 if (!oi->typep) 164 oi->typep = &type_scratch; 165 166 if (parse_loose_header(hdr, oi) < 0) { 167 ret = error(_("unable to parse %s header"), oid_to_hex(oid)); 168 goto out; 169 } 170 171 if (*oi->typep < 0) 172 die(_("invalid object type")); 173 174 if (oi->contentp) { 175 *oi->contentp = unpack_loose_rest(&stream, hdr, *oi->sizep, oid); 176 if (!*oi->contentp) { 177 ret = -1; 178 goto out; 179 } 180 } 181 182 break; 183 case ULHR_BAD: 184 ret = error(_("unable to unpack %s header"), 185 oid_to_hex(oid)); 186 goto out; 187 case ULHR_TOO_LONG: 188 ret = error(_("header for %s too long, exceeds %d bytes"), 189 oid_to_hex(oid), MAX_HEADER_LEN); 190 goto out; 191 } 192 193 ret = 0; 194 195 out: 196 if (ret && ret != ODB_READ_NOT_FOUND && errmsg) 197 strbuf_addf(errmsg, _("loose object %s (stored in %s) is corrupt"), 198 oid_to_hex(oid), path); 199 if (stream_to_end) 200 git_inflate_end(stream_to_end); 201 if (map) 202 munmap(map, mapsize); 203 if (oi) { 204 if (oi->sizep == &size_scratch) 205 oi->sizep = NULL; 206 if (oi->typep == &type_scratch) 207 oi->typep = NULL; 208 if (oi->delta_base_oid) 209 oidclr(oi->delta_base_oid, loose->base.odb->repo->hash_algo); 210 if (oi->source_infop && !ret) 211 oi->source_infop->source = &loose->base; 212 } 213 214 return ret; 215 } 216 217 static enum odb_read_status odb_source_loose_read_object_info(struct odb_source *source, 218 const struct object_id *oid, 219 struct object_info *oi, 220 enum object_info_flags flags, 221 struct strbuf *errmsg) 222 { 223 struct odb_source_loose *loose = odb_source_loose_downcast(source); 224 static struct strbuf buf = STRBUF_INIT; 225 odb_loose_path(loose, &buf, oid); 226 return read_object_info_from_path(loose, buf.buf, oid, oi, flags, errmsg); 227 } 228 229 /* 230 * Find "oid" as a loose object in given source, open the object and return its 231 * file descriptor. Returns the file descriptor on success, negative on failure. 232 * 233 * The "path" out-parameter will give the path of the object we found (if any). 234 * Note that it may point to static storage and is only valid until another 235 * call to open_loose_object(). 236 */ 237 static int open_loose_object(struct odb_source_loose *loose, 238 const struct object_id *oid, const char **path) 239 { 240 static struct strbuf buf = STRBUF_INIT; 241 int fd; 242 243 *path = odb_loose_path(loose, &buf, oid); 244 fd = git_open(*path); 245 if (fd >= 0) 246 return fd; 247 248 return -1; 249 } 250 251 static void *odb_source_loose_map_object(struct odb_source_loose *loose, 252 const struct object_id *oid, 253 unsigned long *size) 254 { 255 const char *p; 256 int fd = open_loose_object(loose, oid, &p); 257 void *map = NULL; 258 struct stat st; 259 260 if (fd < 0) 261 return NULL; 262 263 if (!fstat(fd, &st)) { 264 *size = xsize_t(st.st_size); 265 if (!*size) { 266 /* mmap() is forbidden on empty files */ 267 error(_("object file %s is empty"), p); 268 goto out; 269 } 270 271 map = xmmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0); 272 } 273 274 out: 275 close(fd); 276 return map; 277 } 278 279 struct odb_loose_read_stream { 280 struct odb_stream base; 281 git_zstream z; 282 enum { 283 ODB_LOOSE_READ_STREAM_INUSE, 284 ODB_LOOSE_READ_STREAM_DONE, 285 ODB_LOOSE_READ_STREAM_ERROR, 286 } z_state; 287 void *mapped; 288 unsigned long mapsize; 289 char hdr[32]; 290 int hdr_avail; 291 int hdr_used; 292 }; 293 294 static ssize_t read_istream_loose(struct odb_stream *_st, char *buf, size_t sz) 295 { 296 struct odb_loose_read_stream *st = 297 container_of(_st, struct odb_loose_read_stream, base); 298 size_t total_read = 0; 299 300 switch (st->z_state) { 301 case ODB_LOOSE_READ_STREAM_DONE: 302 return 0; 303 case ODB_LOOSE_READ_STREAM_ERROR: 304 return -1; 305 default: 306 break; 307 } 308 309 if (st->hdr_used < st->hdr_avail) { 310 size_t to_copy = st->hdr_avail - st->hdr_used; 311 if (sz < to_copy) 312 to_copy = sz; 313 memcpy(buf, st->hdr + st->hdr_used, to_copy); 314 st->hdr_used += to_copy; 315 total_read += to_copy; 316 } 317 318 while (total_read < sz) { 319 int status; 320 321 st->z.next_out = (unsigned char *)buf + total_read; 322 st->z.avail_out = sz - total_read; 323 status = git_inflate(&st->z, Z_FINISH); 324 325 total_read = st->z.next_out - (unsigned char *)buf; 326 327 if (status == Z_STREAM_END) { 328 git_inflate_end(&st->z); 329 st->z_state = ODB_LOOSE_READ_STREAM_DONE; 330 break; 331 } 332 if (status != Z_OK && (status != Z_BUF_ERROR || total_read < sz)) { 333 git_inflate_end(&st->z); 334 st->z_state = ODB_LOOSE_READ_STREAM_ERROR; 335 return -1; 336 } 337 } 338 return total_read; 339 } 340 341 static int close_istream_loose(struct odb_stream *_st) 342 { 343 struct odb_loose_read_stream *st = 344 container_of(_st, struct odb_loose_read_stream, base); 345 346 if (st->z_state == ODB_LOOSE_READ_STREAM_INUSE) 347 git_inflate_end(&st->z); 348 munmap(st->mapped, st->mapsize); 349 return 0; 350 } 351 352 static int odb_source_loose_read_object_stream(struct odb_stream **out, 353 struct odb_source *source, 354 const struct object_id *oid) 355 { 356 struct odb_source_loose *loose = odb_source_loose_downcast(source); 357 struct object_info oi = OBJECT_INFO_INIT; 358 struct odb_loose_read_stream *st; 359 unsigned long mapsize; 360 void *mapped; 361 362 mapped = odb_source_loose_map_object(loose, oid, &mapsize); 363 if (!mapped) 364 return -1; 365 366 /* 367 * Note: we must allocate this structure early even though we may still 368 * fail. This is because we need to initialize the zlib stream, and it 369 * is not possible to copy the stream around after the fact because it 370 * has self-referencing pointers. 371 */ 372 CALLOC_ARRAY(st, 1); 373 374 switch (unpack_loose_header(&st->z, mapped, mapsize, st->hdr, 375 sizeof(st->hdr))) { 376 case ULHR_OK: 377 break; 378 case ULHR_BAD: 379 case ULHR_TOO_LONG: 380 goto error; 381 } 382 383 oi.sizep = &st->base.size; 384 oi.typep = &st->base.type; 385 386 if (parse_loose_header(st->hdr, &oi) < 0 || st->base.type < 0) 387 goto error; 388 389 st->mapped = mapped; 390 st->mapsize = mapsize; 391 st->hdr_used = strlen(st->hdr) + 1; 392 st->hdr_avail = st->z.total_out; 393 st->z_state = ODB_LOOSE_READ_STREAM_INUSE; 394 st->base.close = close_istream_loose; 395 st->base.read = read_istream_loose; 396 397 *out = &st->base; 398 399 return 0; 400 error: 401 git_inflate_end(&st->z); 402 munmap(mapped, mapsize); 403 free(st); 404 return -1; 405 } 406 407 struct for_each_object_wrapper_data { 408 struct odb_source_loose *loose; 409 const struct object_info *request; 410 odb_for_each_object_cb cb; 411 void *cb_data; 412 }; 413 414 static int for_each_object_wrapper_cb(const struct object_id *oid, 415 const char *path, 416 void *cb_data) 417 { 418 struct for_each_object_wrapper_data *data = cb_data; 419 420 if (data->request) { 421 struct object_info oi = *data->request; 422 423 if (read_object_info_from_path(data->loose, path, oid, &oi, 0, NULL) < 0) 424 return -1; 425 426 return data->cb(oid, &oi, data->cb_data); 427 } else { 428 return data->cb(oid, NULL, data->cb_data); 429 } 430 } 431 432 static int for_each_prefixed_object_wrapper_cb(const struct object_id *oid, 433 void *node_data UNUSED, 434 void *cb_data) 435 { 436 struct for_each_object_wrapper_data *data = cb_data; 437 if (data->request) { 438 struct object_info oi = *data->request; 439 440 if (odb_source_read_object_info(&data->loose->base, 441 oid, &oi, 0, NULL) < 0) 442 return -1; 443 444 return data->cb(oid, &oi, data->cb_data); 445 } else { 446 return data->cb(oid, NULL, data->cb_data); 447 } 448 } 449 450 static int odb_source_loose_for_each_object(struct odb_source *source, 451 const struct object_info *request, 452 odb_for_each_object_cb cb, 453 void *cb_data, 454 const struct odb_for_each_object_options *opts) 455 { 456 struct odb_source_loose *loose = odb_source_loose_downcast(source); 457 struct for_each_object_wrapper_data data = { 458 .loose = loose, 459 .request = request, 460 .cb = cb, 461 .cb_data = cb_data, 462 }; 463 464 /* There are no loose promisor objects, so we can return immediately. */ 465 if ((opts->flags & ODB_FOR_EACH_OBJECT_PROMISOR_ONLY)) 466 return 0; 467 468 if (opts->prefix) 469 return oidtree_each(odb_source_loose_cache(loose, opts->prefix), 470 opts->prefix, opts->prefix_hex_len, 471 for_each_prefixed_object_wrapper_cb, &data); 472 473 return for_each_loose_file_in_source(source, for_each_object_wrapper_cb, 474 NULL, NULL, &data); 475 } 476 477 struct find_abbrev_len_data { 478 const struct object_id *oid; 479 unsigned len; 480 }; 481 482 static int find_abbrev_len_cb(const struct object_id *oid, 483 struct object_info *oi UNUSED, 484 void *cb_data) 485 { 486 struct find_abbrev_len_data *data = cb_data; 487 unsigned len = oid_common_prefix_hexlen(oid, data->oid); 488 if (len != hash_algos[oid->algo].hexsz && len >= data->len) 489 data->len = len + 1; 490 return 0; 491 } 492 493 static int odb_source_loose_find_abbrev_len(struct odb_source *source, 494 const struct object_id *oid, 495 unsigned min_len, 496 unsigned *out) 497 { 498 struct odb_source_loose *loose = odb_source_loose_downcast(source); 499 struct odb_for_each_object_options opts = { 500 .prefix = oid, 501 .prefix_hex_len = min_len, 502 }; 503 struct find_abbrev_len_data data = { 504 .oid = oid, 505 .len = min_len, 506 }; 507 int ret; 508 509 ret = odb_source_for_each_object(&loose->base, NULL, find_abbrev_len_cb, 510 &data, &opts); 511 *out = data.len; 512 513 return ret; 514 } 515 516 static int count_loose_object(const struct object_id *oid UNUSED, 517 struct object_info *oi UNUSED, 518 void *payload) 519 { 520 unsigned long *count = payload; 521 (*count)++; 522 return 0; 523 } 524 525 static int odb_source_loose_count_objects(struct odb_source *source, 526 enum odb_count_objects_flags flags, 527 unsigned long *out) 528 { 529 struct odb_source_loose *loose = odb_source_loose_downcast(source); 530 const unsigned hexsz = source->odb->repo->hash_algo->hexsz - 2; 531 char *path = NULL; 532 DIR *dir = NULL; 533 int ret; 534 535 if (flags & ODB_COUNT_OBJECTS_APPROXIMATE) { 536 unsigned long count = 0; 537 struct dirent *ent; 538 539 path = xstrfmt("%s/17", source->path); 540 541 dir = opendir(path); 542 if (!dir) { 543 if (errno == ENOENT) { 544 *out = 0; 545 ret = 0; 546 goto out; 547 } 548 549 ret = error_errno("cannot open object shard '%s'", path); 550 goto out; 551 } 552 553 while ((ent = readdir(dir)) != NULL) { 554 if (strspn(ent->d_name, "`0123456789abcdef`") != hexsz || 555 ent->d_name[hexsz] != '\0') 556 continue; 557 count++; 558 } 559 560 *out = count * 256; 561 ret = 0; 562 } else { 563 struct odb_for_each_object_options opts = { 0 }; 564 *out = 0; 565 ret = odb_source_for_each_object(&loose->base, NULL, count_loose_object, 566 out, &opts); 567 } 568 569 out: 570 if (dir) 571 closedir(dir); 572 free(path); 573 return ret; 574 } 575 576 static int odb_source_loose_freshen_object(struct odb_source *source, 577 const struct object_id *oid, 578 const time_t *mtime) 579 { 580 struct odb_source_loose *loose = odb_source_loose_downcast(source); 581 static struct strbuf path = STRBUF_INIT; 582 odb_loose_path(loose, &path, oid); 583 return !!check_and_freshen_file(path.buf, 1, mtime); 584 } 585 586 /* Finalize a file on disk, and close it. */ 587 static void close_loose_object(struct odb_source_loose *loose, 588 int fd, const char *filename) 589 { 590 if (loose->will_destroy) 591 goto out; 592 593 if (batch_fsync_enabled(FSYNC_COMPONENT_LOOSE_OBJECT)) 594 odb_transaction_files_fsync(loose->base.odb->transaction, fd, filename); 595 else if (fsync_object_files > 0) 596 fsync_or_die(fd, filename); 597 else 598 fsync_component_or_die(FSYNC_COMPONENT_LOOSE_OBJECT, fd, 599 filename); 600 601 out: 602 if (close(fd) != 0) 603 die_errno(_("error when closing loose object file")); 604 } 605 606 /* Size of directory component, including the ending '/' */ 607 static inline int directory_size(const char *filename) 608 { 609 const char *s = strrchr(filename, '/'); 610 if (!s) 611 return 0; 612 return s - filename + 1; 613 } 614 615 /* 616 * This creates a temporary file in the same directory as the final 617 * 'filename' 618 * 619 * We want to avoid cross-directory filename renames, because those 620 * can have problems on various filesystems (FAT, NFS, Coda). 621 */ 622 static int create_tmpfile(struct repository *repo, 623 struct strbuf *tmp, const char *filename) 624 { 625 int fd, dirlen = directory_size(filename); 626 627 strbuf_reset(tmp); 628 strbuf_add(tmp, filename, dirlen); 629 strbuf_addstr(tmp, "tmp_obj_XXXXXX"); 630 fd = git_mkstemp_mode(tmp->buf, 0444); 631 if (fd < 0 && dirlen && errno == ENOENT) { 632 /* 633 * Make sure the directory exists; note that the contents 634 * of the buffer are undefined after mkstemp returns an 635 * error, so we have to rewrite the whole buffer from 636 * scratch. 637 */ 638 strbuf_reset(tmp); 639 strbuf_add(tmp, filename, dirlen - 1); 640 if (mkdir(tmp->buf, 0777) && errno != EEXIST) 641 return -1; 642 if (adjust_shared_perm(repo, tmp->buf)) 643 return -1; 644 645 /* Try again */ 646 strbuf_addstr(tmp, "/tmp_obj_XXXXXX"); 647 fd = git_mkstemp_mode(tmp->buf, 0444); 648 } 649 return fd; 650 } 651 652 /** 653 * Common steps for loose object writers to start writing loose 654 * objects: 655 * 656 * - Create tmpfile for the loose object. 657 * - Setup zlib stream for compression. 658 * - Start to feed header to zlib stream. 659 * 660 * Returns a "fd", which should later be provided to 661 * end_loose_object_common(). 662 */ 663 static int start_loose_object_common(struct odb_source_loose *loose, 664 struct strbuf *tmp_file, 665 const char *filename, unsigned flags, 666 git_zstream *stream, 667 unsigned char *buf, size_t buflen, 668 struct git_hash_ctx *c, struct git_hash_ctx *compat_c, 669 char *hdr, int hdrlen) 670 { 671 const struct git_hash_algo *algo = loose->base.odb->repo->hash_algo; 672 const struct git_hash_algo *compat = loose->base.odb->repo->compat_hash_algo; 673 int fd; 674 struct repo_config_values *cfg = repo_config_values(loose->base.odb->repo); 675 676 fd = create_tmpfile(loose->base.odb->repo, tmp_file, filename); 677 if (fd < 0) { 678 if (flags & ODB_WRITE_OBJECT_SILENT) 679 return -1; 680 else if (errno == EACCES) 681 return error(_("insufficient permission for adding " 682 "an object to repository database %s"), 683 loose->base.path); 684 else 685 return error_errno( 686 _("unable to create temporary file")); 687 } 688 689 /* Setup zlib stream for compression */ 690 git_deflate_init(stream, cfg->zlib_compression_level); 691 stream->next_out = buf; 692 stream->avail_out = buflen; 693 git_hash_init(c, algo); 694 if (compat && compat_c) 695 git_hash_init(compat_c, compat); 696 697 /* Start to feed header to zlib stream */ 698 stream->next_in = (unsigned char *)hdr; 699 stream->avail_in = hdrlen; 700 while (git_deflate(stream, 0) == Z_OK) 701 ; /* nothing */ 702 git_hash_update(c, hdr, hdrlen); 703 if (compat && compat_c) 704 git_hash_update(compat_c, hdr, hdrlen); 705 706 return fd; 707 } 708 709 /** 710 * Common steps for the inner git_deflate() loop for writing loose 711 * objects. Returns what git_deflate() returns. 712 */ 713 static int write_loose_object_common(struct odb_source_loose *loose, 714 struct git_hash_ctx *c, struct git_hash_ctx *compat_c, 715 git_zstream *stream, const int flush, 716 unsigned char *in0, const int fd, 717 unsigned char *compressed, 718 const size_t compressed_len) 719 { 720 const struct git_hash_algo *compat = loose->base.odb->repo->compat_hash_algo; 721 int ret; 722 723 ret = git_deflate(stream, flush ? Z_FINISH : 0); 724 git_hash_update(c, in0, stream->next_in - in0); 725 if (compat && compat_c) 726 git_hash_update(compat_c, in0, stream->next_in - in0); 727 if (write_in_full(fd, compressed, stream->next_out - compressed) < 0) 728 die_errno(_("unable to write loose object file")); 729 stream->next_out = compressed; 730 stream->avail_out = compressed_len; 731 732 return ret; 733 } 734 735 /** 736 * Common steps for loose object writers to end writing loose objects: 737 * 738 * - End the compression of zlib stream. 739 * - Get the calculated oid to "oid". 740 */ 741 static int end_loose_object_common(struct odb_source_loose *loose, 742 struct git_hash_ctx *c, struct git_hash_ctx *compat_c, 743 git_zstream *stream, struct object_id *oid, 744 struct object_id *compat_oid) 745 { 746 const struct git_hash_algo *compat = loose->base.odb->repo->compat_hash_algo; 747 int ret; 748 749 ret = git_deflate_end_gently(stream); 750 if (ret != Z_OK) 751 return ret; 752 git_hash_final_oid(oid, c); 753 if (compat && compat_c) 754 git_hash_final_oid(compat_oid, compat_c); 755 756 return Z_OK; 757 } 758 759 static int write_loose_object(struct odb_source_loose *loose, 760 const struct object_id *oid, char *hdr, 761 int hdrlen, const void *buf, unsigned long len, 762 const time_t *mtime, unsigned flags) 763 { 764 int fd, ret; 765 unsigned char compressed[4096]; 766 git_zstream stream; 767 struct git_hash_ctx c; 768 struct object_id parano_oid; 769 static struct strbuf tmp_file = STRBUF_INIT; 770 static struct strbuf filename = STRBUF_INIT; 771 772 if (batch_fsync_enabled(FSYNC_COMPONENT_LOOSE_OBJECT)) 773 odb_transaction_files_prepare(loose->base.odb->transaction); 774 775 odb_loose_path(loose, &filename, oid); 776 777 fd = start_loose_object_common(loose, &tmp_file, filename.buf, flags, 778 &stream, compressed, sizeof(compressed), 779 &c, NULL, hdr, hdrlen); 780 if (fd < 0) 781 return -1; 782 783 /* Then the data itself.. */ 784 stream.next_in = (void *)buf; 785 stream.avail_in = len; 786 do { 787 unsigned char *in0 = stream.next_in; 788 789 ret = write_loose_object_common(loose, &c, NULL, &stream, 1, in0, fd, 790 compressed, sizeof(compressed)); 791 } while (ret == Z_OK); 792 793 if (ret != Z_STREAM_END) 794 die(_("unable to deflate new object %s (%d)"), oid_to_hex(oid), 795 ret); 796 ret = end_loose_object_common(loose, &c, NULL, &stream, ¶no_oid, NULL); 797 if (ret != Z_OK) 798 die(_("deflateEnd on object %s failed (%d)"), oid_to_hex(oid), 799 ret); 800 if (!oideq(oid, ¶no_oid)) 801 die(_("confused by unstable object source data for %s"), 802 oid_to_hex(oid)); 803 804 close_loose_object(loose, fd, tmp_file.buf); 805 806 if (mtime) { 807 struct utimbuf utb = { 808 .actime = *mtime, 809 .modtime = *mtime, 810 }; 811 812 if (utime(tmp_file.buf, &utb) < 0 && 813 !(flags & ODB_WRITE_OBJECT_SILENT)) 814 warning_errno(_("failed utime() on %s"), tmp_file.buf); 815 } 816 817 return finalize_object_file_flags(loose->base.odb->repo, tmp_file.buf, filename.buf, 818 FOF_SKIP_COLLISION_CHECK); 819 } 820 821 static int odb_source_loose_write_object(struct odb_source *source, 822 const void *buf, size_t len, 823 enum object_type type, 824 const struct object_id *oid, 825 const struct object_id *compat_oid, 826 const time_t *mtime, 827 enum odb_write_object_flags flags) 828 { 829 struct odb_source_loose *loose = odb_source_loose_downcast(source); 830 char hdr[MAX_HEADER_LEN]; 831 int hdrlen; 832 833 hdrlen = format_object_header(hdr, sizeof(hdr), type, len); 834 835 if (write_loose_object(loose, oid, hdr, hdrlen, buf, len, mtime, flags)) 836 return -1; 837 838 if (compat_oid) 839 return repo_add_loose_object_map(loose, oid, compat_oid); 840 841 return 0; 842 } 843 844 static int odb_source_loose_write_object_stream(struct odb_source *source, 845 struct odb_stream *in_stream, 846 struct object_id *oid) 847 { 848 struct odb_source_loose *loose = odb_source_loose_downcast(source); 849 const struct git_hash_algo *compat = loose->base.odb->repo->compat_hash_algo; 850 struct object_id compat_oid; 851 int fd, ret, err = 0, flush = 0; 852 unsigned char compressed[4096]; 853 git_zstream stream; 854 struct git_hash_ctx c, compat_c; 855 struct strbuf tmp_file = STRBUF_INIT; 856 struct strbuf filename = STRBUF_INIT; 857 unsigned char buf[8192]; 858 int dirlen; 859 bool is_finished = false; 860 char hdr[MAX_HEADER_LEN]; 861 int hdrlen; 862 863 if (batch_fsync_enabled(FSYNC_COMPONENT_LOOSE_OBJECT)) 864 odb_transaction_files_prepare(loose->base.odb->transaction); 865 866 /* Since oid is not determined, save tmp file to odb path. */ 867 strbuf_addf(&filename, "%s/", loose->base.path); 868 hdrlen = format_object_header(hdr, sizeof(hdr), in_stream->type, in_stream->size); 869 870 /* 871 * Common steps for write_loose_object and stream_loose_object to 872 * start writing loose objects: 873 * 874 * - Create tmpfile for the loose object. 875 * - Setup zlib stream for compression. 876 * - Start to feed header to zlib stream. 877 */ 878 fd = start_loose_object_common(loose, &tmp_file, filename.buf, 0, 879 &stream, compressed, sizeof(compressed), 880 &c, &compat_c, hdr, hdrlen); 881 if (fd < 0) { 882 err = -1; 883 goto cleanup; 884 } 885 886 /* Then the data itself.. */ 887 do { 888 unsigned char *in0 = stream.next_in; 889 890 if (!stream.avail_in && !is_finished) { 891 ssize_t read_len = odb_stream_read(in_stream, buf, 892 sizeof(buf)); 893 if (read_len < 0) { 894 close(fd); 895 err = -1; 896 goto cleanup; 897 } 898 899 /* All data has been read. */ 900 if (!read_len) { 901 is_finished = true; 902 flush = 1; 903 } 904 905 stream.avail_in = read_len; 906 stream.next_in = buf; 907 in0 = buf; 908 } 909 ret = write_loose_object_common(loose, &c, &compat_c, &stream, flush, in0, fd, 910 compressed, sizeof(compressed)); 911 /* 912 * Unlike write_loose_object(), we do not have the entire 913 * buffer. If we get Z_BUF_ERROR due to too few input bytes, 914 * then we'll replenish them in the next input_stream->read() 915 * call when we loop. 916 */ 917 } while (ret == Z_OK || ret == Z_BUF_ERROR); 918 919 if (stream.total_in != in_stream->size + hdrlen) 920 die(_("write stream object %"PRIuMAX" != %"PRIuMAX), (uintmax_t)stream.total_in, 921 (uintmax_t)in_stream->size + hdrlen); 922 923 /* 924 * Common steps for write_loose_object and stream_loose_object to 925 * end writing loose object: 926 * 927 * - End the compression of zlib stream. 928 * - Get the calculated oid. 929 */ 930 if (ret != Z_STREAM_END) 931 die(_("unable to stream deflate new object (%d)"), ret); 932 ret = end_loose_object_common(loose, &c, &compat_c, &stream, oid, &compat_oid); 933 if (ret != Z_OK) 934 die(_("deflateEnd on stream object failed (%d)"), ret); 935 close_loose_object(loose, fd, tmp_file.buf); 936 937 if (odb_freshen_object(loose->base.odb, oid)) { 938 unlink_or_warn(tmp_file.buf); 939 goto cleanup; 940 } 941 odb_loose_path(loose, &filename, oid); 942 943 /* We finally know the object path, and create the missing dir. */ 944 dirlen = directory_size(filename.buf); 945 if (dirlen) { 946 struct strbuf dir = STRBUF_INIT; 947 strbuf_add(&dir, filename.buf, dirlen); 948 949 if (safe_create_dir_in_gitdir(loose->base.odb->repo, dir.buf) && 950 errno != EEXIST) { 951 err = error_errno(_("unable to create directory %s"), dir.buf); 952 strbuf_release(&dir); 953 goto cleanup; 954 } 955 strbuf_release(&dir); 956 } 957 958 err = finalize_object_file_flags(loose->base.odb->repo, tmp_file.buf, filename.buf, 959 FOF_SKIP_COLLISION_CHECK); 960 if (!err && compat) 961 err = repo_add_loose_object_map(loose, oid, &compat_oid); 962 cleanup: 963 strbuf_release(&tmp_file); 964 strbuf_release(&filename); 965 return err; 966 } 967 968 static int odb_source_loose_begin_transaction(struct odb_source *source UNUSED, 969 struct odb_transaction **out UNUSED, 970 enum odb_transaction_flags flags UNUSED) 971 { 972 /* TODO: this is a known omission that we'll want to address eventually. */ 973 return error("loose source does not support transactions"); 974 } 975 976 static void odb_source_loose_clear_cache(struct odb_source_loose *loose) 977 { 978 oidtree_clear(loose->cache); 979 FREE_AND_NULL(loose->cache); 980 memset(&loose->subdir_seen, 0, 981 sizeof(loose->subdir_seen)); 982 } 983 984 static void odb_source_loose_prepare(struct odb_source *source, 985 enum odb_prepare_flags flags) 986 { 987 struct odb_source_loose *loose = odb_source_loose_downcast(source); 988 if (flags & ODB_PREPARE_FLUSH_CACHES) 989 odb_source_loose_clear_cache(loose); 990 } 991 992 static void odb_source_loose_close(struct odb_source *source UNUSED) 993 { 994 /* Nothing to do. */ 995 } 996 997 static void odb_source_loose_reparent(const char *old_cwd, 998 const char *new_cwd, 999 void *cb_data) 1000 { 1001 struct odb_source_loose *loose = cb_data; 1002 char *path = reparent_relative_path(old_cwd, new_cwd, 1003 loose->base.path); 1004 free(loose->base.path); 1005 loose->base.path = path; 1006 } 1007 1008 static void odb_source_loose_free(struct odb_source *source) 1009 { 1010 struct odb_source_loose *loose = odb_source_loose_downcast(source); 1011 odb_source_loose_clear_cache(loose); 1012 loose_object_map_clear(&loose->map); 1013 chdir_notify_unregister(odb_source_loose_reparent, loose); 1014 odb_source_release(&loose->base); 1015 free(loose); 1016 } 1017 1018 struct fsck_loose_data { 1019 struct odb_source_loose *source; 1020 struct odb_fsck_options *opts; 1021 struct progress *progress; 1022 bool error_found; 1023 }; 1024 1025 static int fsck_loose(const struct object_id *oid, const char *path, 1026 void *cb_data) 1027 { 1028 struct fsck_loose_data *data = cb_data; 1029 enum object_type type = OBJ_NONE; 1030 size_t size; 1031 void *contents = NULL; 1032 int eaten = 0; 1033 struct object_info oi = OBJECT_INFO_INIT; 1034 struct object_id real_oid = *null_oid(data->source->base.odb->repo->hash_algo); 1035 int err = 0; 1036 1037 oi.sizep = &size; 1038 oi.typep = &type; 1039 1040 if (read_loose_object(data->source->base.odb->repo, 1041 path, oid, &real_oid, &contents, &oi) < 0) { 1042 if (contents && !oideq(&real_oid, oid)) 1043 err = error(_("%s: hash-path mismatch, found at: %s"), 1044 oid_to_hex(&real_oid), path); 1045 else 1046 err = error(_("%s: object corrupt or missing: %s"), 1047 oid_to_hex(oid), path); 1048 } 1049 if (err < 0) 1050 goto out; 1051 1052 if (!contents && type != OBJ_BLOB) 1053 BUG("read_loose_object streamed a non-blob"); 1054 1055 if (data->opts->object_cb(oid, type, size, contents, &eaten, 1056 data->opts->object_payload)) { 1057 err = -1; 1058 goto out; 1059 } 1060 1061 out: 1062 if (err) 1063 data->error_found = true; 1064 if (!eaten) 1065 free(contents); 1066 return 0; /* keep checking other objects, even if we saw an error */ 1067 } 1068 1069 static int fsck_cruft(const char *basename, const char *path, 1070 void *data UNUSED) 1071 { 1072 if (!starts_with(basename, "tmp_obj_")) 1073 fprintf_ln(stderr, _("bad sha1 file: %s"), path); 1074 return 0; 1075 } 1076 1077 static int fsck_subdir(unsigned int nr, const char *path UNUSED, void *cb_data) 1078 { 1079 struct fsck_loose_data *data = cb_data; 1080 display_progress(data->progress, nr + 1); 1081 return 0; 1082 } 1083 1084 static int odb_source_loose_fsck(struct odb_source *source, 1085 struct odb_fsck_options *opts) 1086 { 1087 struct odb_source_loose *loose = odb_source_loose_downcast(source); 1088 struct fsck_loose_data data = { 1089 .source = loose, 1090 .opts = opts, 1091 }; 1092 1093 if (opts->flags & ODB_FSCK_VERBOSE) 1094 fprintf_ln(stderr, _("Checking object directory")); 1095 1096 if (opts->flags & ODB_FSCK_PROGRESS) 1097 data.progress = start_progress(source->odb->repo, 1098 _("Checking object directories"), 256); 1099 1100 for_each_loose_file_in_source(source, fsck_loose, 1101 fsck_cruft, fsck_subdir, &data); 1102 display_progress(data.progress, 256); 1103 stop_progress(&data.progress); 1104 1105 return data.error_found ? -1 : 0; 1106 } 1107 1108 struct odb_source_loose *odb_source_loose_new(struct object_database *odb, 1109 const char *path) 1110 { 1111 struct odb_source_loose *loose; 1112 1113 CALLOC_ARRAY(loose, 1); 1114 odb_source_init(&loose->base, odb, ODB_SOURCE_LOOSE, path); 1115 1116 loose->base.free = odb_source_loose_free; 1117 loose->base.close = odb_source_loose_close; 1118 loose->base.prepare = odb_source_loose_prepare; 1119 loose->base.fsck = odb_source_loose_fsck; 1120 loose->base.read_object_info = odb_source_loose_read_object_info; 1121 loose->base.read_object_stream = odb_source_loose_read_object_stream; 1122 loose->base.for_each_object = odb_source_loose_for_each_object; 1123 loose->base.find_abbrev_len = odb_source_loose_find_abbrev_len; 1124 loose->base.count_objects = odb_source_loose_count_objects; 1125 loose->base.freshen_object = odb_source_loose_freshen_object; 1126 loose->base.write_object = odb_source_loose_write_object; 1127 loose->base.write_object_stream = odb_source_loose_write_object_stream; 1128 loose->base.begin_transaction = odb_source_loose_begin_transaction; 1129 1130 if (!is_absolute_path(loose->base.path)) 1131 chdir_notify_register(odb_source_loose_reparent, loose); 1132 1133 loose_object_map_load(loose); 1134 1135 return loose; 1136 }