| 748 | } |
| 749 | |
| 750 | int write_loose_object(struct odb_source_loose *loose, |
| 751 | const struct object_id *oid, char *hdr, |
| 752 | int hdrlen, const void *buf, unsigned long len, |
| 753 | time_t mtime, unsigned flags) |
| 754 | { |
| 755 | int fd, ret; |
| 756 | unsigned char compressed[4096]; |
| 757 | git_zstream stream; |
| 758 | struct git_hash_ctx c; |
| 759 | struct object_id parano_oid; |
| 760 | static struct strbuf tmp_file = STRBUF_INIT; |
| 761 | static struct strbuf filename = STRBUF_INIT; |
| 762 | |
| 763 | if (batch_fsync_enabled(FSYNC_COMPONENT_LOOSE_OBJECT)) |
| 764 | prepare_loose_object_transaction(loose->base.odb->transaction); |
| 765 | |
| 766 | odb_loose_path(loose, &filename, oid); |
| 767 | |
| 768 | fd = start_loose_object_common(loose, &tmp_file, filename.buf, flags, |
| 769 | &stream, compressed, sizeof(compressed), |
| 770 | &c, NULL, hdr, hdrlen); |
| 771 | if (fd < 0) |
| 772 | return -1; |
| 773 | |
| 774 | /* Then the data itself.. */ |
| 775 | stream.next_in = (void *)buf; |
| 776 | stream.avail_in = len; |
| 777 | do { |
| 778 | unsigned char *in0 = stream.next_in; |
| 779 | |
| 780 | ret = write_loose_object_common(loose, &c, NULL, &stream, 1, in0, fd, |
| 781 | compressed, sizeof(compressed)); |
| 782 | } while (ret == Z_OK); |
| 783 | |
| 784 | if (ret != Z_STREAM_END) |
| 785 | die(_("unable to deflate new object %s (%d)"), oid_to_hex(oid), |
| 786 | ret); |
| 787 | ret = end_loose_object_common(loose, &c, NULL, &stream, ¶no_oid, NULL); |
| 788 | if (ret != Z_OK) |
| 789 | die(_("deflateEnd on object %s failed (%d)"), oid_to_hex(oid), |
| 790 | ret); |
| 791 | if (!oideq(oid, ¶no_oid)) |
| 792 | die(_("confused by unstable object source data for %s"), |
| 793 | oid_to_hex(oid)); |
| 794 | |
| 795 | close_loose_object(loose, fd, tmp_file.buf); |
| 796 | |
| 797 | if (mtime) { |
| 798 | struct utimbuf utb; |
| 799 | utb.actime = mtime; |
| 800 | utb.modtime = mtime; |
| 801 | if (utime(tmp_file.buf, &utb) < 0 && |
| 802 | !(flags & ODB_WRITE_OBJECT_SILENT)) |
| 803 | warning_errno(_("failed utime() on %s"), tmp_file.buf); |
| 804 | } |
| 805 | |
| 806 | return finalize_object_file_flags(loose->base.odb->repo, tmp_file.buf, filename.buf, |
| 807 | FOF_SKIP_COLLISION_CHECK); |
no test coverage detected