git-cvsserver: protect against NULL in crypt(3)
[git.git] / bulk-checkin.c
blobb023d9959aae4360cd42414a7f33ee11c550c61a
1 /*
2 * Copyright (c) 2011, Google Inc.
3 */
4 #include "cache.h"
5 #include "bulk-checkin.h"
6 #include "repository.h"
7 #include "csum-file.h"
8 #include "pack.h"
9 #include "strbuf.h"
10 #include "packfile.h"
11 #include "object-store.h"
13 static struct bulk_checkin_state {
14 unsigned plugged:1;
16 char *pack_tmp_name;
17 struct hashfile *f;
18 off_t offset;
19 struct pack_idx_option pack_idx_opts;
21 struct pack_idx_entry **written;
22 uint32_t alloc_written;
23 uint32_t nr_written;
24 } state;
26 static void finish_bulk_checkin(struct bulk_checkin_state *state)
28 struct object_id oid;
29 struct strbuf packname = STRBUF_INIT;
30 int i;
32 if (!state->f)
33 return;
35 if (state->nr_written == 0) {
36 close(state->f->fd);
37 unlink(state->pack_tmp_name);
38 goto clear_exit;
39 } else if (state->nr_written == 1) {
40 finalize_hashfile(state->f, oid.hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE);
41 } else {
42 int fd = finalize_hashfile(state->f, oid.hash, 0);
43 fixup_pack_header_footer(fd, oid.hash, state->pack_tmp_name,
44 state->nr_written, oid.hash,
45 state->offset);
46 close(fd);
49 strbuf_addf(&packname, "%s/pack/pack-", get_object_directory());
50 finish_tmp_packfile(&packname, state->pack_tmp_name,
51 state->written, state->nr_written,
52 &state->pack_idx_opts, oid.hash);
53 for (i = 0; i < state->nr_written; i++)
54 free(state->written[i]);
56 clear_exit:
57 free(state->written);
58 memset(state, 0, sizeof(*state));
60 strbuf_release(&packname);
61 /* Make objects we just wrote available to ourselves */
62 reprepare_packed_git(the_repository);
65 static int already_written(struct bulk_checkin_state *state, struct object_id *oid)
67 int i;
69 /* The object may already exist in the repository */
70 if (has_object_file(oid))
71 return 1;
73 /* Might want to keep the list sorted */
74 for (i = 0; i < state->nr_written; i++)
75 if (oideq(&state->written[i]->oid, oid))
76 return 1;
78 /* This is a new object we need to keep */
79 return 0;
83 * Read the contents from fd for size bytes, streaming it to the
84 * packfile in state while updating the hash in ctx. Signal a failure
85 * by returning a negative value when the resulting pack would exceed
86 * the pack size limit and this is not the first object in the pack,
87 * so that the caller can discard what we wrote from the current pack
88 * by truncating it and opening a new one. The caller will then call
89 * us again after rewinding the input fd.
91 * The already_hashed_to pointer is kept untouched by the caller to
92 * make sure we do not hash the same byte when we are called
93 * again. This way, the caller does not have to checkpoint its hash
94 * status before calling us just in case we ask it to call us again
95 * with a new pack.
97 static int stream_to_pack(struct bulk_checkin_state *state,
98 git_hash_ctx *ctx, off_t *already_hashed_to,
99 int fd, size_t size, enum object_type type,
100 const char *path, unsigned flags)
102 git_zstream s;
103 unsigned char ibuf[16384];
104 unsigned char obuf[16384];
105 unsigned hdrlen;
106 int status = Z_OK;
107 int write_object = (flags & HASH_WRITE_OBJECT);
108 off_t offset = 0;
110 git_deflate_init(&s, pack_compression_level);
112 hdrlen = encode_in_pack_object_header(obuf, sizeof(obuf), type, size);
113 s.next_out = obuf + hdrlen;
114 s.avail_out = sizeof(obuf) - hdrlen;
116 while (status != Z_STREAM_END) {
117 if (size && !s.avail_in) {
118 ssize_t rsize = size < sizeof(ibuf) ? size : sizeof(ibuf);
119 ssize_t read_result = read_in_full(fd, ibuf, rsize);
120 if (read_result < 0)
121 die_errno("failed to read from '%s'", path);
122 if (read_result != rsize)
123 die("failed to read %d bytes from '%s'",
124 (int)rsize, path);
125 offset += rsize;
126 if (*already_hashed_to < offset) {
127 size_t hsize = offset - *already_hashed_to;
128 if (rsize < hsize)
129 hsize = rsize;
130 if (hsize)
131 the_hash_algo->update_fn(ctx, ibuf, hsize);
132 *already_hashed_to = offset;
134 s.next_in = ibuf;
135 s.avail_in = rsize;
136 size -= rsize;
139 status = git_deflate(&s, size ? 0 : Z_FINISH);
141 if (!s.avail_out || status == Z_STREAM_END) {
142 if (write_object) {
143 size_t written = s.next_out - obuf;
145 /* would we bust the size limit? */
146 if (state->nr_written &&
147 pack_size_limit_cfg &&
148 pack_size_limit_cfg < state->offset + written) {
149 git_deflate_abort(&s);
150 return -1;
153 hashwrite(state->f, obuf, written);
154 state->offset += written;
156 s.next_out = obuf;
157 s.avail_out = sizeof(obuf);
160 switch (status) {
161 case Z_OK:
162 case Z_BUF_ERROR:
163 case Z_STREAM_END:
164 continue;
165 default:
166 die("unexpected deflate failure: %d", status);
169 git_deflate_end(&s);
170 return 0;
173 /* Lazily create backing packfile for the state */
174 static void prepare_to_stream(struct bulk_checkin_state *state,
175 unsigned flags)
177 if (!(flags & HASH_WRITE_OBJECT) || state->f)
178 return;
180 state->f = create_tmp_packfile(&state->pack_tmp_name);
181 reset_pack_idx_option(&state->pack_idx_opts);
183 /* Pretend we are going to write only one object */
184 state->offset = write_pack_header(state->f, 1);
185 if (!state->offset)
186 die_errno("unable to write pack header");
189 static int deflate_to_pack(struct bulk_checkin_state *state,
190 struct object_id *result_oid,
191 int fd, size_t size,
192 enum object_type type, const char *path,
193 unsigned flags)
195 off_t seekback, already_hashed_to;
196 git_hash_ctx ctx;
197 unsigned char obuf[16384];
198 unsigned header_len;
199 struct hashfile_checkpoint checkpoint = {0};
200 struct pack_idx_entry *idx = NULL;
202 seekback = lseek(fd, 0, SEEK_CUR);
203 if (seekback == (off_t) -1)
204 return error("cannot find the current offset");
206 header_len = xsnprintf((char *)obuf, sizeof(obuf), "%s %" PRIuMAX,
207 type_name(type), (uintmax_t)size) + 1;
208 the_hash_algo->init_fn(&ctx);
209 the_hash_algo->update_fn(&ctx, obuf, header_len);
211 /* Note: idx is non-NULL when we are writing */
212 if ((flags & HASH_WRITE_OBJECT) != 0)
213 CALLOC_ARRAY(idx, 1);
215 already_hashed_to = 0;
217 while (1) {
218 prepare_to_stream(state, flags);
219 if (idx) {
220 hashfile_checkpoint(state->f, &checkpoint);
221 idx->offset = state->offset;
222 crc32_begin(state->f);
224 if (!stream_to_pack(state, &ctx, &already_hashed_to,
225 fd, size, type, path, flags))
226 break;
228 * Writing this object to the current pack will make
229 * it too big; we need to truncate it, start a new
230 * pack, and write into it.
232 if (!idx)
233 BUG("should not happen");
234 hashfile_truncate(state->f, &checkpoint);
235 state->offset = checkpoint.offset;
236 finish_bulk_checkin(state);
237 if (lseek(fd, seekback, SEEK_SET) == (off_t) -1)
238 return error("cannot seek back");
240 the_hash_algo->final_oid_fn(result_oid, &ctx);
241 if (!idx)
242 return 0;
244 idx->crc32 = crc32_end(state->f);
245 if (already_written(state, result_oid)) {
246 hashfile_truncate(state->f, &checkpoint);
247 state->offset = checkpoint.offset;
248 free(idx);
249 } else {
250 oidcpy(&idx->oid, result_oid);
251 ALLOC_GROW(state->written,
252 state->nr_written + 1,
253 state->alloc_written);
254 state->written[state->nr_written++] = idx;
256 return 0;
259 int index_bulk_checkin(struct object_id *oid,
260 int fd, size_t size, enum object_type type,
261 const char *path, unsigned flags)
263 int status = deflate_to_pack(&state, oid, fd, size, type,
264 path, flags);
265 if (!state.plugged)
266 finish_bulk_checkin(&state);
267 return status;
270 void plug_bulk_checkin(void)
272 state.plugged = 1;
275 void unplug_bulk_checkin(void)
277 state.plugged = 0;
278 if (state.f)
279 finish_bulk_checkin(&state);