2 * Copyright (c) 2011, Google Inc.
5 #include "bulk-checkin.h"
10 static struct bulk_checkin_state {
16 struct pack_idx_option pack_idx_opts;
18 struct pack_idx_entry **written;
19 uint32_t alloc_written;
23 static void finish_bulk_checkin(struct bulk_checkin_state *state)
26 struct strbuf packname = STRBUF_INIT;
32 if (state->nr_written == 0) {
34 unlink(state->pack_tmp_name);
36 } else if (state->nr_written == 1) {
37 sha1close(state->f, oid.hash, CSUM_FSYNC);
39 int fd = sha1close(state->f, oid.hash, 0);
40 fixup_pack_header_footer(fd, oid.hash, state->pack_tmp_name,
41 state->nr_written, oid.hash,
46 strbuf_addf(&packname, "%s/pack/pack-", get_object_directory());
47 finish_tmp_packfile(&packname, state->pack_tmp_name,
48 state->written, state->nr_written,
49 &state->pack_idx_opts, oid.hash);
50 for (i = 0; i < state->nr_written; i++)
51 free(state->written[i]);
55 memset(state, 0, sizeof(*state));
57 strbuf_release(&packname);
58 /* Make objects we just wrote available to ourselves */
59 reprepare_packed_git();
62 static int already_written(struct bulk_checkin_state *state, unsigned char sha1[])
66 /* The object may already exist in the repository */
67 if (has_sha1_file(sha1))
70 /* Might want to keep the list sorted */
71 for (i = 0; i < state->nr_written; i++)
72 if (!hashcmp(state->written[i]->sha1, sha1))
75 /* This is a new object we need to keep */
80 * Read the contents from fd for size bytes, streaming it to the
81 * packfile in state while updating the hash in ctx. Signal a failure
82 * by returning a negative value when the resulting pack would exceed
83 * the pack size limit and this is not the first object in the pack,
84 * so that the caller can discard what we wrote from the current pack
85 * by truncating it and opening a new one. The caller will then call
86 * us again after rewinding the input fd.
88 * The already_hashed_to pointer is kept untouched by the caller to
89 * make sure we do not hash the same byte when we are called
90 * again. This way, the caller does not have to checkpoint its hash
91 * status before calling us just in case we ask it to call us again
94 static int stream_to_pack(struct bulk_checkin_state *state,
95 git_SHA_CTX *ctx, off_t *already_hashed_to,
96 int fd, size_t size, enum object_type type,
97 const char *path, unsigned flags)
100 unsigned char obuf[16384];
103 int write_object = (flags & HASH_WRITE_OBJECT);
106 git_deflate_init(&s, pack_compression_level);
108 hdrlen = encode_in_pack_object_header(type, size, obuf);
109 s.next_out = obuf + hdrlen;
110 s.avail_out = sizeof(obuf) - hdrlen;
112 while (status != Z_STREAM_END) {
113 unsigned char ibuf[16384];
115 if (size && !s.avail_in) {
116 ssize_t rsize = size < sizeof(ibuf) ? size : sizeof(ibuf);
117 if (read_in_full(fd, ibuf, rsize) != rsize)
118 die("failed to read %d bytes from '%s'",
121 if (*already_hashed_to < offset) {
122 size_t hsize = offset - *already_hashed_to;
126 git_SHA1_Update(ctx, ibuf, hsize);
127 *already_hashed_to = offset;
134 status = git_deflate(&s, size ? 0 : Z_FINISH);
136 if (!s.avail_out || status == Z_STREAM_END) {
138 size_t written = s.next_out - obuf;
140 /* would we bust the size limit? */
141 if (state->nr_written &&
142 pack_size_limit_cfg &&
143 pack_size_limit_cfg < state->offset + written) {
144 git_deflate_abort(&s);
148 sha1write(state->f, obuf, written);
149 state->offset += written;
152 s.avail_out = sizeof(obuf);
161 die("unexpected deflate failure: %d", status);
168 /* Lazily create backing packfile for the state */
169 static void prepare_to_stream(struct bulk_checkin_state *state,
172 if (!(flags & HASH_WRITE_OBJECT) || state->f)
175 state->f = create_tmp_packfile(&state->pack_tmp_name);
176 reset_pack_idx_option(&state->pack_idx_opts);
178 /* Pretend we are going to write only one object */
179 state->offset = write_pack_header(state->f, 1);
181 die_errno("unable to write pack header");
184 static int deflate_to_pack(struct bulk_checkin_state *state,
185 unsigned char result_sha1[],
187 enum object_type type, const char *path,
190 off_t seekback, already_hashed_to;
192 unsigned char obuf[16384];
194 struct sha1file_checkpoint checkpoint;
195 struct pack_idx_entry *idx = NULL;
197 seekback = lseek(fd, 0, SEEK_CUR);
198 if (seekback == (off_t) -1)
199 return error("cannot find the current offset");
201 header_len = xsnprintf((char *)obuf, sizeof(obuf), "%s %" PRIuMAX,
202 typename(type), (uintmax_t)size) + 1;
204 git_SHA1_Update(&ctx, obuf, header_len);
206 /* Note: idx is non-NULL when we are writing */
207 if ((flags & HASH_WRITE_OBJECT) != 0)
208 idx = xcalloc(1, sizeof(*idx));
210 already_hashed_to = 0;
213 prepare_to_stream(state, flags);
215 sha1file_checkpoint(state->f, &checkpoint);
216 idx->offset = state->offset;
217 crc32_begin(state->f);
219 if (!stream_to_pack(state, &ctx, &already_hashed_to,
220 fd, size, type, path, flags))
223 * Writing this object to the current pack will make
224 * it too big; we need to truncate it, start a new
225 * pack, and write into it.
228 die("BUG: should not happen");
229 sha1file_truncate(state->f, &checkpoint);
230 state->offset = checkpoint.offset;
231 finish_bulk_checkin(state);
232 if (lseek(fd, seekback, SEEK_SET) == (off_t) -1)
233 return error("cannot seek back");
235 git_SHA1_Final(result_sha1, &ctx);
239 idx->crc32 = crc32_end(state->f);
240 if (already_written(state, result_sha1)) {
241 sha1file_truncate(state->f, &checkpoint);
242 state->offset = checkpoint.offset;
245 hashcpy(idx->sha1, result_sha1);
246 ALLOC_GROW(state->written,
247 state->nr_written + 1,
248 state->alloc_written);
249 state->written[state->nr_written++] = idx;
254 int index_bulk_checkin(unsigned char *sha1,
255 int fd, size_t size, enum object_type type,
256 const char *path, unsigned flags)
258 int status = deflate_to_pack(&state, sha1, fd, size, type,
261 finish_bulk_checkin(&state);
265 void plug_bulk_checkin(void)
270 void unplug_bulk_checkin(void)
274 finish_bulk_checkin(&state);