2 * Copyright (c) 2011, Google Inc.
5 #include "bulk-checkin.h"
11 static struct bulk_checkin_state {
17 struct pack_idx_option pack_idx_opts;
19 struct pack_idx_entry **written;
20 uint32_t alloc_written;
24 static void finish_bulk_checkin(struct bulk_checkin_state *state)
27 struct strbuf packname = STRBUF_INIT;
33 if (state->nr_written == 0) {
35 unlink(state->pack_tmp_name);
37 } else if (state->nr_written == 1) {
38 sha1close(state->f, oid.hash, CSUM_FSYNC);
40 int fd = sha1close(state->f, oid.hash, 0);
41 fixup_pack_header_footer(fd, oid.hash, state->pack_tmp_name,
42 state->nr_written, oid.hash,
47 strbuf_addf(&packname, "%s/pack/pack-", get_object_directory());
48 finish_tmp_packfile(&packname, state->pack_tmp_name,
49 state->written, state->nr_written,
50 &state->pack_idx_opts, oid.hash);
51 for (i = 0; i < state->nr_written; i++)
52 free(state->written[i]);
56 memset(state, 0, sizeof(*state));
58 strbuf_release(&packname);
59 /* Make objects we just wrote available to ourselves */
60 reprepare_packed_git();
63 static int already_written(struct bulk_checkin_state *state, unsigned char sha1[])
67 /* The object may already exist in the repository */
68 if (has_sha1_file(sha1))
71 /* Might want to keep the list sorted */
72 for (i = 0; i < state->nr_written; i++)
73 if (!hashcmp(state->written[i]->oid.hash, sha1))
76 /* This is a new object we need to keep */
81 * Read the contents from fd for size bytes, streaming it to the
82 * packfile in state while updating the hash in ctx. Signal a failure
83 * by returning a negative value when the resulting pack would exceed
84 * the pack size limit and this is not the first object in the pack,
85 * so that the caller can discard what we wrote from the current pack
86 * by truncating it and opening a new one. The caller will then call
87 * us again after rewinding the input fd.
89 * The already_hashed_to pointer is kept untouched by the caller to
90 * make sure we do not hash the same byte when we are called
91 * again. This way, the caller does not have to checkpoint its hash
92 * status before calling us just in case we ask it to call us again
95 static int stream_to_pack(struct bulk_checkin_state *state,
96 git_SHA_CTX *ctx, off_t *already_hashed_to,
97 int fd, size_t size, enum object_type type,
98 const char *path, unsigned flags)
101 unsigned char obuf[16384];
104 int write_object = (flags & HASH_WRITE_OBJECT);
107 git_deflate_init(&s, pack_compression_level);
109 hdrlen = encode_in_pack_object_header(obuf, sizeof(obuf), type, size);
110 s.next_out = obuf + hdrlen;
111 s.avail_out = sizeof(obuf) - hdrlen;
113 while (status != Z_STREAM_END) {
114 unsigned char ibuf[16384];
116 if (size && !s.avail_in) {
117 ssize_t rsize = size < sizeof(ibuf) ? size : sizeof(ibuf);
118 if (read_in_full(fd, ibuf, rsize) != rsize)
119 die("failed to read %d bytes from '%s'",
122 if (*already_hashed_to < offset) {
123 size_t hsize = offset - *already_hashed_to;
127 git_SHA1_Update(ctx, ibuf, hsize);
128 *already_hashed_to = offset;
135 status = git_deflate(&s, size ? 0 : Z_FINISH);
137 if (!s.avail_out || status == Z_STREAM_END) {
139 size_t written = s.next_out - obuf;
141 /* would we bust the size limit? */
142 if (state->nr_written &&
143 pack_size_limit_cfg &&
144 pack_size_limit_cfg < state->offset + written) {
145 git_deflate_abort(&s);
149 sha1write(state->f, obuf, written);
150 state->offset += written;
153 s.avail_out = sizeof(obuf);
162 die("unexpected deflate failure: %d", status);
169 /* Lazily create backing packfile for the state */
170 static void prepare_to_stream(struct bulk_checkin_state *state,
173 if (!(flags & HASH_WRITE_OBJECT) || state->f)
176 state->f = create_tmp_packfile(&state->pack_tmp_name);
177 reset_pack_idx_option(&state->pack_idx_opts);
179 /* Pretend we are going to write only one object */
180 state->offset = write_pack_header(state->f, 1);
182 die_errno("unable to write pack header");
185 static int deflate_to_pack(struct bulk_checkin_state *state,
186 unsigned char result_sha1[],
188 enum object_type type, const char *path,
191 off_t seekback, already_hashed_to;
193 unsigned char obuf[16384];
195 struct sha1file_checkpoint checkpoint;
196 struct pack_idx_entry *idx = NULL;
198 seekback = lseek(fd, 0, SEEK_CUR);
199 if (seekback == (off_t) -1)
200 return error("cannot find the current offset");
202 header_len = xsnprintf((char *)obuf, sizeof(obuf), "%s %" PRIuMAX,
203 typename(type), (uintmax_t)size) + 1;
205 git_SHA1_Update(&ctx, obuf, header_len);
207 /* Note: idx is non-NULL when we are writing */
208 if ((flags & HASH_WRITE_OBJECT) != 0)
209 idx = xcalloc(1, sizeof(*idx));
211 already_hashed_to = 0;
214 prepare_to_stream(state, flags);
216 sha1file_checkpoint(state->f, &checkpoint);
217 idx->offset = state->offset;
218 crc32_begin(state->f);
220 if (!stream_to_pack(state, &ctx, &already_hashed_to,
221 fd, size, type, path, flags))
224 * Writing this object to the current pack will make
225 * it too big; we need to truncate it, start a new
226 * pack, and write into it.
229 die("BUG: should not happen");
230 sha1file_truncate(state->f, &checkpoint);
231 state->offset = checkpoint.offset;
232 finish_bulk_checkin(state);
233 if (lseek(fd, seekback, SEEK_SET) == (off_t) -1)
234 return error("cannot seek back");
236 git_SHA1_Final(result_sha1, &ctx);
240 idx->crc32 = crc32_end(state->f);
241 if (already_written(state, result_sha1)) {
242 sha1file_truncate(state->f, &checkpoint);
243 state->offset = checkpoint.offset;
246 hashcpy(idx->oid.hash, result_sha1);
247 ALLOC_GROW(state->written,
248 state->nr_written + 1,
249 state->alloc_written);
250 state->written[state->nr_written++] = idx;
255 int index_bulk_checkin(unsigned char *sha1,
256 int fd, size_t size, enum object_type type,
257 const char *path, unsigned flags)
259 int status = deflate_to_pack(&state, sha1, fd, size, type,
262 finish_bulk_checkin(&state);
266 void plug_bulk_checkin(void)
271 void unplug_bulk_checkin(void)
275 finish_bulk_checkin(&state);