RelNotes/2.2.0.txt: fix minor typos
[git.git] / bulk-checkin.c
blob0c4b8a7cad085fa1c7760e6290f21213830eda3d
1 /*
2 * Copyright (c) 2011, Google Inc.
3 */
4 #include "cache.h"
5 #include "bulk-checkin.h"
6 #include "csum-file.h"
7 #include "pack.h"
8 #include "strbuf.h"
10 static int pack_compression_level = Z_DEFAULT_COMPRESSION;
12 static struct bulk_checkin_state {
13 unsigned plugged:1;
15 char *pack_tmp_name;
16 struct sha1file *f;
17 off_t offset;
18 struct pack_idx_option pack_idx_opts;
20 struct pack_idx_entry **written;
21 uint32_t alloc_written;
22 uint32_t nr_written;
23 } state;
25 static void finish_bulk_checkin(struct bulk_checkin_state *state)
27 unsigned char sha1[20];
28 struct strbuf packname = STRBUF_INIT;
29 int i;
31 if (!state->f)
32 return;
34 if (state->nr_written == 0) {
35 close(state->f->fd);
36 unlink(state->pack_tmp_name);
37 goto clear_exit;
38 } else if (state->nr_written == 1) {
39 sha1close(state->f, sha1, CSUM_FSYNC);
40 } else {
41 int fd = sha1close(state->f, sha1, 0);
42 fixup_pack_header_footer(fd, sha1, state->pack_tmp_name,
43 state->nr_written, sha1,
44 state->offset);
45 close(fd);
48 strbuf_addf(&packname, "%s/pack/pack-", get_object_directory());
49 finish_tmp_packfile(&packname, state->pack_tmp_name,
50 state->written, state->nr_written,
51 &state->pack_idx_opts, sha1);
52 for (i = 0; i < state->nr_written; i++)
53 free(state->written[i]);
55 clear_exit:
56 free(state->written);
57 memset(state, 0, sizeof(*state));
59 strbuf_release(&packname);
60 /* Make objects we just wrote available to ourselves */
61 reprepare_packed_git();
64 static int already_written(struct bulk_checkin_state *state, unsigned char sha1[])
66 int i;
68 /* The object may already exist in the repository */
69 if (has_sha1_file(sha1))
70 return 1;
72 /* Might want to keep the list sorted */
73 for (i = 0; i < state->nr_written; i++)
74 if (!hashcmp(state->written[i]->sha1, sha1))
75 return 1;
77 /* This is a new object we need to keep */
78 return 0;
82 * Read the contents from fd for size bytes, streaming it to the
83 * packfile in state while updating the hash in ctx. Signal a failure
84 * by returning a negative value when the resulting pack would exceed
85 * the pack size limit and this is not the first object in the pack,
86 * so that the caller can discard what we wrote from the current pack
87 * by truncating it and opening a new one. The caller will then call
88 * us again after rewinding the input fd.
90 * The already_hashed_to pointer is kept untouched by the caller to
91 * make sure we do not hash the same byte when we are called
92 * again. This way, the caller does not have to checkpoint its hash
93 * status before calling us just in case we ask it to call us again
94 * with a new pack.
96 static int stream_to_pack(struct bulk_checkin_state *state,
97 git_SHA_CTX *ctx, off_t *already_hashed_to,
98 int fd, size_t size, enum object_type type,
99 const char *path, unsigned flags)
101 git_zstream s;
102 unsigned char obuf[16384];
103 unsigned hdrlen;
104 int status = Z_OK;
105 int write_object = (flags & HASH_WRITE_OBJECT);
106 off_t offset = 0;
108 memset(&s, 0, sizeof(s));
109 git_deflate_init(&s, pack_compression_level);
111 hdrlen = encode_in_pack_object_header(type, size, obuf);
112 s.next_out = obuf + hdrlen;
113 s.avail_out = sizeof(obuf) - hdrlen;
115 while (status != Z_STREAM_END) {
116 unsigned char ibuf[16384];
118 if (size && !s.avail_in) {
119 ssize_t rsize = size < sizeof(ibuf) ? size : sizeof(ibuf);
120 if (read_in_full(fd, ibuf, rsize) != rsize)
121 die("failed to read %d bytes from '%s'",
122 (int)rsize, path);
123 offset += rsize;
124 if (*already_hashed_to < offset) {
125 size_t hsize = offset - *already_hashed_to;
126 if (rsize < hsize)
127 hsize = rsize;
128 if (hsize)
129 git_SHA1_Update(ctx, ibuf, hsize);
130 *already_hashed_to = offset;
132 s.next_in = ibuf;
133 s.avail_in = rsize;
134 size -= rsize;
137 status = git_deflate(&s, size ? 0 : Z_FINISH);
139 if (!s.avail_out || status == Z_STREAM_END) {
140 if (write_object) {
141 size_t written = s.next_out - obuf;
143 /* would we bust the size limit? */
144 if (state->nr_written &&
145 pack_size_limit_cfg &&
146 pack_size_limit_cfg < state->offset + written) {
147 git_deflate_abort(&s);
148 return -1;
151 sha1write(state->f, obuf, written);
152 state->offset += written;
154 s.next_out = obuf;
155 s.avail_out = sizeof(obuf);
158 switch (status) {
159 case Z_OK:
160 case Z_BUF_ERROR:
161 case Z_STREAM_END:
162 continue;
163 default:
164 die("unexpected deflate failure: %d", status);
167 git_deflate_end(&s);
168 return 0;
171 /* Lazily create backing packfile for the state */
172 static void prepare_to_stream(struct bulk_checkin_state *state,
173 unsigned flags)
175 if (!(flags & HASH_WRITE_OBJECT) || state->f)
176 return;
178 state->f = create_tmp_packfile(&state->pack_tmp_name);
179 reset_pack_idx_option(&state->pack_idx_opts);
181 /* Pretend we are going to write only one object */
182 state->offset = write_pack_header(state->f, 1);
183 if (!state->offset)
184 die_errno("unable to write pack header");
187 static int deflate_to_pack(struct bulk_checkin_state *state,
188 unsigned char result_sha1[],
189 int fd, size_t size,
190 enum object_type type, const char *path,
191 unsigned flags)
193 off_t seekback, already_hashed_to;
194 git_SHA_CTX ctx;
195 unsigned char obuf[16384];
196 unsigned header_len;
197 struct sha1file_checkpoint checkpoint;
198 struct pack_idx_entry *idx = NULL;
200 seekback = lseek(fd, 0, SEEK_CUR);
201 if (seekback == (off_t) -1)
202 return error("cannot find the current offset");
204 header_len = sprintf((char *)obuf, "%s %" PRIuMAX,
205 typename(type), (uintmax_t)size) + 1;
206 git_SHA1_Init(&ctx);
207 git_SHA1_Update(&ctx, obuf, header_len);
209 /* Note: idx is non-NULL when we are writing */
210 if ((flags & HASH_WRITE_OBJECT) != 0)
211 idx = xcalloc(1, sizeof(*idx));
213 already_hashed_to = 0;
215 while (1) {
216 prepare_to_stream(state, flags);
217 if (idx) {
218 sha1file_checkpoint(state->f, &checkpoint);
219 idx->offset = state->offset;
220 crc32_begin(state->f);
222 if (!stream_to_pack(state, &ctx, &already_hashed_to,
223 fd, size, type, path, flags))
224 break;
226 * Writing this object to the current pack will make
227 * it too big; we need to truncate it, start a new
228 * pack, and write into it.
230 if (!idx)
231 die("BUG: should not happen");
232 sha1file_truncate(state->f, &checkpoint);
233 state->offset = checkpoint.offset;
234 finish_bulk_checkin(state);
235 if (lseek(fd, seekback, SEEK_SET) == (off_t) -1)
236 return error("cannot seek back");
238 git_SHA1_Final(result_sha1, &ctx);
239 if (!idx)
240 return 0;
242 idx->crc32 = crc32_end(state->f);
243 if (already_written(state, result_sha1)) {
244 sha1file_truncate(state->f, &checkpoint);
245 state->offset = checkpoint.offset;
246 free(idx);
247 } else {
248 hashcpy(idx->sha1, result_sha1);
249 ALLOC_GROW(state->written,
250 state->nr_written + 1,
251 state->alloc_written);
252 state->written[state->nr_written++] = idx;
254 return 0;
257 int index_bulk_checkin(unsigned char *sha1,
258 int fd, size_t size, enum object_type type,
259 const char *path, unsigned flags)
261 int status = deflate_to_pack(&state, sha1, fd, size, type,
262 path, flags);
263 if (!state.plugged)
264 finish_bulk_checkin(&state);
265 return status;
268 void plug_bulk_checkin(void)
270 state.plugged = 1;
273 void unplug_bulk_checkin(void)
275 state.plugged = 0;
276 if (state.f)
277 finish_bulk_checkin(&state);