2119
return 1;
2120
}
2121
2122
+int stream_loose_object(struct input_stream *in_stream, size_t len,
2123
+ struct object_id *oid)
2124
+{
2125
+ int fd, ret, err = 0, flush = 0;
2126
+ unsigned char compressed[4096];
2127
+ git_zstream stream;
2128
+ git_hash_ctx c;
2129
+ struct strbuf tmp_file = STRBUF_INIT;
2130
+ struct strbuf filename = STRBUF_INIT;
2131
+ int dirlen;
2132
+ char hdr[MAX_HEADER_LEN];
2133
+ int hdrlen;
2134
+
2135
+ if (batch_fsync_enabled(FSYNC_COMPONENT_LOOSE_OBJECT))
2136
+ prepare_loose_object_bulk_checkin();
2137
+
2138
+ /* Since oid is not determined, save tmp file to odb path. */
2139
+ strbuf_addf(&filename, "%s/", get_object_directory());
2140
+ hdrlen = format_object_header(hdr, sizeof(hdr), OBJ_BLOB, len);
2141
+
2142
+ /*
2143
+ * Common steps for write_loose_object and stream_loose_object to
2144
+ * start writing loose objects:
2145
+ *
2146
+ * - Create tmpfile for the loose object.
2147
+ * - Setup zlib stream for compression.
2148
+ * - Start to feed header to zlib stream.
2149
+ */
2150
+ fd = start_loose_object_common(&tmp_file, filename.buf, 0,
2151
+ &stream, compressed, sizeof(compressed),
2152
+ &c, hdr, hdrlen);
2153
+ if (fd < 0) {
2154
+ err = -1;
2155
+ goto cleanup;
2156
+ }
2157
+
2158
+ /* Then the data itself.. */
2159
+ do {
2160
+ unsigned char *in0 = stream.next_in;
2161
+
2162
+ if (!stream.avail_in && !in_stream->is_finished) {
2163
+ const void *in = in_stream->read(in_stream, &stream.avail_in);
2164
+ stream.next_in = (void *)in;
2165
+ in0 = (unsigned char *)in;
2166
+ /* All data has been read. */
2167
+ if (in_stream->is_finished)
2168
+ flush = 1;
2169
+ }
2170
+ ret = write_loose_object_common(&c, &stream, flush, in0, fd,
2171
+ compressed, sizeof(compressed));
2172
+ /*
2173
+ * Unlike write_loose_object(), we do not have the entire
2174
+ * buffer. If we get Z_BUF_ERROR due to too few input bytes,
2175
+ * then we'll replenish them in the next input_stream->read()
2176
+ * call when we loop.
2177
+ */
2178
+ } while (ret == Z_OK || ret == Z_BUF_ERROR);
2179
+
2180
+ if (stream.total_in != len + hdrlen)
2181
+ die(_("write stream object %ld != %"PRIuMAX), stream.total_in,
2182
+ (uintmax_t)len + hdrlen);
2183
+
2184
+ /*
2185
+ * Common steps for write_loose_object and stream_loose_object to
2186
+ * end writing loose oject:
2187
+ *
2188
+ * - End the compression of zlib stream.
2189
+ * - Get the calculated oid.
2190
+ */
2191
+ if (ret != Z_STREAM_END)
2192
+ die(_("unable to stream deflate new object (%d)"), ret);
2193
+ ret = end_loose_object_common(&c, &stream, oid);
2194
+ if (ret != Z_OK)
2195
+ die(_("deflateEnd on stream object failed (%d)"), ret);
2196
+ close_loose_object(fd, tmp_file.buf);
2197
+
2198
+ if (freshen_packed_object(oid) || freshen_loose_object(oid)) {
2199
+ unlink_or_warn(tmp_file.buf);
2200
+ goto cleanup;
2201
+ }
2202
+
2203
+ loose_object_path(the_repository, &filename, oid);
2204
+
2205
+ /* We finally know the object path, and create the missing dir. */
2206
+ dirlen = directory_size(filename.buf);
2207
+ if (dirlen) {
2208
+ struct strbuf dir = STRBUF_INIT;
2209
+ strbuf_add(&dir, filename.buf, dirlen);
2210
+
2211
+ if (mkdir_in_gitdir(dir.buf) && errno != EEXIST) {
2212
+ err = error_errno(_("unable to create directory %s"), dir.buf);
2213
+ strbuf_release(&dir);
2214
+ goto cleanup;
2215
+ }
2216
+ strbuf_release(&dir);
2217
+ }
2218
+
2219
+ err = finalize_object_file(tmp_file.buf, filename.buf);
2220
+cleanup:
2221
+ strbuf_release(&tmp_file);
2222
+ strbuf_release(&filename);
2223
+ return err;
2224
+}
2225
+
2226
int write_object_file_flags(const void *buf, unsigned long len,
2227
enum object_type type, struct object_id *oid,
2228
unsigned flags)