| 808 | } |
| 809 | |
| 810 | int odb_source_loose_write_stream(struct odb_source_loose *loose, |
| 811 | struct odb_write_stream *in_stream, size_t len, |
| 812 | struct object_id *oid) |
| 813 | { |
| 814 | const struct git_hash_algo *compat = loose->base.odb->repo->compat_hash_algo; |
| 815 | struct object_id compat_oid; |
| 816 | int fd, ret, err = 0, flush = 0; |
| 817 | unsigned char compressed[4096]; |
| 818 | git_zstream stream; |
| 819 | struct git_hash_ctx c, compat_c; |
| 820 | struct strbuf tmp_file = STRBUF_INIT; |
| 821 | struct strbuf filename = STRBUF_INIT; |
| 822 | unsigned char buf[8192]; |
| 823 | int dirlen; |
| 824 | char hdr[MAX_HEADER_LEN]; |
| 825 | int hdrlen; |
| 826 | |
| 827 | if (batch_fsync_enabled(FSYNC_COMPONENT_LOOSE_OBJECT)) |
| 828 | prepare_loose_object_transaction(loose->base.odb->transaction); |
| 829 | |
| 830 | /* Since oid is not determined, save tmp file to odb path. */ |
| 831 | strbuf_addf(&filename, "%s/", loose->base.path); |
| 832 | hdrlen = format_object_header(hdr, sizeof(hdr), OBJ_BLOB, len); |
| 833 | |
| 834 | /* |
| 835 | * Common steps for write_loose_object and stream_loose_object to |
| 836 | * start writing loose objects: |
| 837 | * |
| 838 | * - Create tmpfile for the loose object. |
| 839 | * - Setup zlib stream for compression. |
| 840 | * - Start to feed header to zlib stream. |
| 841 | */ |
| 842 | fd = start_loose_object_common(loose, &tmp_file, filename.buf, 0, |
| 843 | &stream, compressed, sizeof(compressed), |
| 844 | &c, &compat_c, hdr, hdrlen); |
| 845 | if (fd < 0) { |
| 846 | err = -1; |
| 847 | goto cleanup; |
| 848 | } |
| 849 | |
| 850 | /* Then the data itself.. */ |
| 851 | do { |
| 852 | unsigned char *in0 = stream.next_in; |
| 853 | |
| 854 | if (!stream.avail_in && !in_stream->is_finished) { |
| 855 | ssize_t read_len = odb_write_stream_read(in_stream, buf, |
| 856 | sizeof(buf)); |
| 857 | if (read_len < 0) { |
| 858 | close(fd); |
| 859 | err = -1; |
| 860 | goto cleanup; |
| 861 | } |
| 862 | |
| 863 | stream.avail_in = read_len; |
| 864 | stream.next_in = buf; |
| 865 | in0 = buf; |
| 866 | /* All data has been read. */ |
| 867 | if (in_stream->is_finished) |
no test coverage detected