Merge branch 'sb/unpack-trees-grammofix' into next
[git] / bulk-checkin.c
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
10 static struct bulk_checkin_state {
11         unsigned plugged:1;
12
13         char *pack_tmp_name;
14         struct sha1file *f;
15         off_t offset;
16         struct pack_idx_option pack_idx_opts;
17
18         struct pack_idx_entry **written;
19         uint32_t alloc_written;
20         uint32_t nr_written;
21 } state;
22
23 static void finish_bulk_checkin(struct bulk_checkin_state *state)
24 {
25         struct object_id oid;
26         struct strbuf packname = STRBUF_INIT;
27         int i;
28
29         if (!state->f)
30                 return;
31
32         if (state->nr_written == 0) {
33                 close(state->f->fd);
34                 unlink(state->pack_tmp_name);
35                 goto clear_exit;
36         } else if (state->nr_written == 1) {
37                 sha1close(state->f, oid.hash, CSUM_FSYNC);
38         } else {
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,
42                                          state->offset);
43                 close(fd);
44         }
45
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]);
52
53 clear_exit:
54         free(state->written);
55         memset(state, 0, sizeof(*state));
56
57         strbuf_release(&packname);
58         /* Make objects we just wrote available to ourselves */
59         reprepare_packed_git();
60 }
61
62 static int already_written(struct bulk_checkin_state *state, unsigned char sha1[])
63 {
64         int i;
65
66         /* The object may already exist in the repository */
67         if (has_sha1_file(sha1))
68                 return 1;
69
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))
73                         return 1;
74
75         /* This is a new object we need to keep */
76         return 0;
77 }
78
79 /*
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.
87  *
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
92  * with a new pack.
93  */
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)
98 {
99         git_zstream s;
100         unsigned char obuf[16384];
101         unsigned hdrlen;
102         int status = Z_OK;
103         int write_object = (flags & HASH_WRITE_OBJECT);
104         off_t offset = 0;
105
106         git_deflate_init(&s, pack_compression_level);
107
108         hdrlen = encode_in_pack_object_header(type, size, obuf);
109         s.next_out = obuf + hdrlen;
110         s.avail_out = sizeof(obuf) - hdrlen;
111
112         while (status != Z_STREAM_END) {
113                 unsigned char ibuf[16384];
114
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'",
119                                     (int)rsize, path);
120                         offset += rsize;
121                         if (*already_hashed_to < offset) {
122                                 size_t hsize = offset - *already_hashed_to;
123                                 if (rsize < hsize)
124                                         hsize = rsize;
125                                 if (hsize)
126                                         git_SHA1_Update(ctx, ibuf, hsize);
127                                 *already_hashed_to = offset;
128                         }
129                         s.next_in = ibuf;
130                         s.avail_in = rsize;
131                         size -= rsize;
132                 }
133
134                 status = git_deflate(&s, size ? 0 : Z_FINISH);
135
136                 if (!s.avail_out || status == Z_STREAM_END) {
137                         if (write_object) {
138                                 size_t written = s.next_out - obuf;
139
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);
145                                         return -1;
146                                 }
147
148                                 sha1write(state->f, obuf, written);
149                                 state->offset += written;
150                         }
151                         s.next_out = obuf;
152                         s.avail_out = sizeof(obuf);
153                 }
154
155                 switch (status) {
156                 case Z_OK:
157                 case Z_BUF_ERROR:
158                 case Z_STREAM_END:
159                         continue;
160                 default:
161                         die("unexpected deflate failure: %d", status);
162                 }
163         }
164         git_deflate_end(&s);
165         return 0;
166 }
167
168 /* Lazily create backing packfile for the state */
169 static void prepare_to_stream(struct bulk_checkin_state *state,
170                               unsigned flags)
171 {
172         if (!(flags & HASH_WRITE_OBJECT) || state->f)
173                 return;
174
175         state->f = create_tmp_packfile(&state->pack_tmp_name);
176         reset_pack_idx_option(&state->pack_idx_opts);
177
178         /* Pretend we are going to write only one object */
179         state->offset = write_pack_header(state->f, 1);
180         if (!state->offset)
181                 die_errno("unable to write pack header");
182 }
183
184 static int deflate_to_pack(struct bulk_checkin_state *state,
185                            unsigned char result_sha1[],
186                            int fd, size_t size,
187                            enum object_type type, const char *path,
188                            unsigned flags)
189 {
190         off_t seekback, already_hashed_to;
191         git_SHA_CTX ctx;
192         unsigned char obuf[16384];
193         unsigned header_len;
194         struct sha1file_checkpoint checkpoint;
195         struct pack_idx_entry *idx = NULL;
196
197         seekback = lseek(fd, 0, SEEK_CUR);
198         if (seekback == (off_t) -1)
199                 return error("cannot find the current offset");
200
201         header_len = xsnprintf((char *)obuf, sizeof(obuf), "%s %" PRIuMAX,
202                                typename(type), (uintmax_t)size) + 1;
203         git_SHA1_Init(&ctx);
204         git_SHA1_Update(&ctx, obuf, header_len);
205
206         /* Note: idx is non-NULL when we are writing */
207         if ((flags & HASH_WRITE_OBJECT) != 0)
208                 idx = xcalloc(1, sizeof(*idx));
209
210         already_hashed_to = 0;
211
212         while (1) {
213                 prepare_to_stream(state, flags);
214                 if (idx) {
215                         sha1file_checkpoint(state->f, &checkpoint);
216                         idx->offset = state->offset;
217                         crc32_begin(state->f);
218                 }
219                 if (!stream_to_pack(state, &ctx, &already_hashed_to,
220                                     fd, size, type, path, flags))
221                         break;
222                 /*
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.
226                  */
227                 if (!idx)
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");
234         }
235         git_SHA1_Final(result_sha1, &ctx);
236         if (!idx)
237                 return 0;
238
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;
243                 free(idx);
244         } else {
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;
250         }
251         return 0;
252 }
253
254 int index_bulk_checkin(unsigned char *sha1,
255                        int fd, size_t size, enum object_type type,
256                        const char *path, unsigned flags)
257 {
258         int status = deflate_to_pack(&state, sha1, fd, size, type,
259                                      path, flags);
260         if (!state.plugged)
261                 finish_bulk_checkin(&state);
262         return status;
263 }
264
265 void plug_bulk_checkin(void)
266 {
267         state.plugged = 1;
268 }
269
270 void unplug_bulk_checkin(void)
271 {
272         state.plugged = 0;
273         if (state.f)
274                 finish_bulk_checkin(&state);
275 }