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"
9#include "packfile.h"
10
11static struct bulk_checkin_state {
12 unsigned plugged:1;
13
14 char *pack_tmp_name;
15 struct hashfile *f;
16 off_t offset;
17 struct pack_idx_option pack_idx_opts;
18
19 struct pack_idx_entry **written;
20 uint32_t alloc_written;
21 uint32_t nr_written;
22} state;
23
24static void finish_bulk_checkin(struct bulk_checkin_state *state)
25{
26 struct object_id oid;
27 struct strbuf packname = STRBUF_INIT;
28 int i;
29
30 if (!state->f)
31 return;
32
33 if (state->nr_written == 0) {
34 close(state->f->fd);
35 unlink(state->pack_tmp_name);
36 goto clear_exit;
37 } else if (state->nr_written == 1) {
38 hashclose(state->f, oid.hash, CSUM_FSYNC);
39 } else {
40 int fd = hashclose(state->f, oid.hash, 0);
41 fixup_pack_header_footer(fd, oid.hash, state->pack_tmp_name,
42 state->nr_written, oid.hash,
43 state->offset);
44 close(fd);
45 }
46
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]);
53
54clear_exit:
55 free(state->written);
56 memset(state, 0, sizeof(*state));
57
58 strbuf_release(&packname);
59 /* Make objects we just wrote available to ourselves */
60 reprepare_packed_git();
61}
62
63static int already_written(struct bulk_checkin_state *state, unsigned char sha1[])
64{
65 int i;
66
67 /* The object may already exist in the repository */
68 if (has_sha1_file(sha1))
69 return 1;
70
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))
74 return 1;
75
76 /* This is a new object we need to keep */
77 return 0;
78}
79
80/*
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.
88 *
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
93 * with a new pack.
94 */
95static int stream_to_pack(struct bulk_checkin_state *state,
96 git_hash_ctx *ctx, off_t *already_hashed_to,
97 int fd, size_t size, enum object_type type,
98 const char *path, unsigned flags)
99{
100 git_zstream s;
101 unsigned char obuf[16384];
102 unsigned hdrlen;
103 int status = Z_OK;
104 int write_object = (flags & HASH_WRITE_OBJECT);
105 off_t offset = 0;
106
107 git_deflate_init(&s, pack_compression_level);
108
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;
112
113 while (status != Z_STREAM_END) {
114 unsigned char ibuf[16384];
115
116 if (size && !s.avail_in) {
117 ssize_t rsize = size < sizeof(ibuf) ? size : sizeof(ibuf);
118 ssize_t read_result = read_in_full(fd, ibuf, rsize);
119 if (read_result < 0)
120 die_errno("failed to read from '%s'", path);
121 if (read_result != rsize)
122 die("failed to read %d bytes from '%s'",
123 (int)rsize, path);
124 offset += rsize;
125 if (*already_hashed_to < offset) {
126 size_t hsize = offset - *already_hashed_to;
127 if (rsize < hsize)
128 hsize = rsize;
129 if (hsize)
130 the_hash_algo->update_fn(ctx, ibuf, hsize);
131 *already_hashed_to = offset;
132 }
133 s.next_in = ibuf;
134 s.avail_in = rsize;
135 size -= rsize;
136 }
137
138 status = git_deflate(&s, size ? 0 : Z_FINISH);
139
140 if (!s.avail_out || status == Z_STREAM_END) {
141 if (write_object) {
142 size_t written = s.next_out - obuf;
143
144 /* would we bust the size limit? */
145 if (state->nr_written &&
146 pack_size_limit_cfg &&
147 pack_size_limit_cfg < state->offset + written) {
148 git_deflate_abort(&s);
149 return -1;
150 }
151
152 hashwrite(state->f, obuf, written);
153 state->offset += written;
154 }
155 s.next_out = obuf;
156 s.avail_out = sizeof(obuf);
157 }
158
159 switch (status) {
160 case Z_OK:
161 case Z_BUF_ERROR:
162 case Z_STREAM_END:
163 continue;
164 default:
165 die("unexpected deflate failure: %d", status);
166 }
167 }
168 git_deflate_end(&s);
169 return 0;
170}
171
172/* Lazily create backing packfile for the state */
173static void prepare_to_stream(struct bulk_checkin_state *state,
174 unsigned flags)
175{
176 if (!(flags & HASH_WRITE_OBJECT) || state->f)
177 return;
178
179 state->f = create_tmp_packfile(&state->pack_tmp_name);
180 reset_pack_idx_option(&state->pack_idx_opts);
181
182 /* Pretend we are going to write only one object */
183 state->offset = write_pack_header(state->f, 1);
184 if (!state->offset)
185 die_errno("unable to write pack header");
186}
187
188static int deflate_to_pack(struct bulk_checkin_state *state,
189 unsigned char result_sha1[],
190 int fd, size_t size,
191 enum object_type type, const char *path,
192 unsigned flags)
193{
194 off_t seekback, already_hashed_to;
195 git_hash_ctx ctx;
196 unsigned char obuf[16384];
197 unsigned header_len;
198 struct hashfile_checkpoint checkpoint;
199 struct pack_idx_entry *idx = NULL;
200
201 seekback = lseek(fd, 0, SEEK_CUR);
202 if (seekback == (off_t) -1)
203 return error("cannot find the current offset");
204
205 header_len = xsnprintf((char *)obuf, sizeof(obuf), "%s %" PRIuMAX,
206 typename(type), (uintmax_t)size) + 1;
207 the_hash_algo->init_fn(&ctx);
208 the_hash_algo->update_fn(&ctx, obuf, header_len);
209
210 /* Note: idx is non-NULL when we are writing */
211 if ((flags & HASH_WRITE_OBJECT) != 0)
212 idx = xcalloc(1, sizeof(*idx));
213
214 already_hashed_to = 0;
215
216 while (1) {
217 prepare_to_stream(state, flags);
218 if (idx) {
219 hashfile_checkpoint(state->f, &checkpoint);
220 idx->offset = state->offset;
221 crc32_begin(state->f);
222 }
223 if (!stream_to_pack(state, &ctx, &already_hashed_to,
224 fd, size, type, path, flags))
225 break;
226 /*
227 * Writing this object to the current pack will make
228 * it too big; we need to truncate it, start a new
229 * pack, and write into it.
230 */
231 if (!idx)
232 die("BUG: should not happen");
233 hashfile_truncate(state->f, &checkpoint);
234 state->offset = checkpoint.offset;
235 finish_bulk_checkin(state);
236 if (lseek(fd, seekback, SEEK_SET) == (off_t) -1)
237 return error("cannot seek back");
238 }
239 the_hash_algo->final_fn(result_sha1, &ctx);
240 if (!idx)
241 return 0;
242
243 idx->crc32 = crc32_end(state->f);
244 if (already_written(state, result_sha1)) {
245 hashfile_truncate(state->f, &checkpoint);
246 state->offset = checkpoint.offset;
247 free(idx);
248 } else {
249 hashcpy(idx->oid.hash, result_sha1);
250 ALLOC_GROW(state->written,
251 state->nr_written + 1,
252 state->alloc_written);
253 state->written[state->nr_written++] = idx;
254 }
255 return 0;
256}
257
258int index_bulk_checkin(unsigned char *sha1,
259 int fd, size_t size, enum object_type type,
260 const char *path, unsigned flags)
261{
262 int status = deflate_to_pack(&state, sha1, fd, size, type,
263 path, flags);
264 if (!state.plugged)
265 finish_bulk_checkin(&state);
266 return status;
267}
268
269void plug_bulk_checkin(void)
270{
271 state.plugged = 1;
272}
273
274void unplug_bulk_checkin(void)
275{
276 state.plugged = 0;
277 if (state.f)
278 finish_bulk_checkin(&state);
279}