Merge branch 'ek/alt-odb-entry-fix'
[git] / bulk-checkin.c
1 /*
2  * Copyright (c) 2011, Google Inc.
3  */
4 #include "bulk-checkin.h"
5 #include "csum-file.h"
6 #include "pack.h"
7 #include "strbuf.h"
8
9 static int pack_compression_level = Z_DEFAULT_COMPRESSION;
10
11 static struct bulk_checkin_state {
12         unsigned plugged:1;
13
14         char *pack_tmp_name;
15         struct sha1file *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
24 static void finish_bulk_checkin(struct bulk_checkin_state *state)
25 {
26         unsigned char sha1[20];
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                 sha1close(state->f, sha1, CSUM_FSYNC);
39         } else {
40                 int fd = sha1close(state->f, sha1, 0);
41                 fixup_pack_header_footer(fd, sha1, state->pack_tmp_name,
42                                          state->nr_written, sha1,
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, sha1);
51         for (i = 0; i < state->nr_written; i++)
52                 free(state->written[i]);
53
54 clear_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
63 static 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]->sha1, 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  */
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)
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         memset(&s, 0, sizeof(s));
108         git_deflate_init(&s, pack_compression_level);
109
110         hdrlen = encode_in_pack_object_header(type, size, obuf);
111         s.next_out = obuf + hdrlen;
112         s.avail_out = sizeof(obuf) - hdrlen;
113
114         while (status != Z_STREAM_END) {
115                 unsigned char ibuf[16384];
116
117                 if (size && !s.avail_in) {
118                         ssize_t rsize = size < sizeof(ibuf) ? size : sizeof(ibuf);
119                         if (read_in_full(fd, ibuf, rsize) != rsize)
120                                 die("failed to read %d bytes from '%s'",
121                                     (int)rsize, path);
122                         offset += rsize;
123                         if (*already_hashed_to < offset) {
124                                 size_t hsize = offset - *already_hashed_to;
125                                 if (rsize < hsize)
126                                         hsize = rsize;
127                                 if (hsize)
128                                         git_SHA1_Update(ctx, ibuf, hsize);
129                                 *already_hashed_to = offset;
130                         }
131                         s.next_in = ibuf;
132                         s.avail_in = rsize;
133                         size -= rsize;
134                 }
135
136                 status = git_deflate(&s, size ? 0 : Z_FINISH);
137
138                 if (!s.avail_out || status == Z_STREAM_END) {
139                         if (write_object) {
140                                 size_t written = s.next_out - obuf;
141
142                                 /* would we bust the size limit? */
143                                 if (state->nr_written &&
144                                     pack_size_limit_cfg &&
145                                     pack_size_limit_cfg < state->offset + written) {
146                                         git_deflate_abort(&s);
147                                         return -1;
148                                 }
149
150                                 sha1write(state->f, obuf, written);
151                                 state->offset += written;
152                         }
153                         s.next_out = obuf;
154                         s.avail_out = sizeof(obuf);
155                 }
156
157                 switch (status) {
158                 case Z_OK:
159                 case Z_BUF_ERROR:
160                 case Z_STREAM_END:
161                         continue;
162                 default:
163                         die("unexpected deflate failure: %d", status);
164                 }
165         }
166         git_deflate_end(&s);
167         return 0;
168 }
169
170 /* Lazily create backing packfile for the state */
171 static void prepare_to_stream(struct bulk_checkin_state *state,
172                               unsigned flags)
173 {
174         if (!(flags & HASH_WRITE_OBJECT) || state->f)
175                 return;
176
177         state->f = create_tmp_packfile(&state->pack_tmp_name);
178         reset_pack_idx_option(&state->pack_idx_opts);
179
180         /* Pretend we are going to write only one object */
181         state->offset = write_pack_header(state->f, 1);
182         if (!state->offset)
183                 die_errno("unable to write pack header");
184 }
185
186 static int deflate_to_pack(struct bulk_checkin_state *state,
187                            unsigned char result_sha1[],
188                            int fd, size_t size,
189                            enum object_type type, const char *path,
190                            unsigned flags)
191 {
192         off_t seekback, already_hashed_to;
193         git_SHA_CTX ctx;
194         unsigned char obuf[16384];
195         unsigned header_len;
196         struct sha1file_checkpoint checkpoint;
197         struct pack_idx_entry *idx = NULL;
198
199         seekback = lseek(fd, 0, SEEK_CUR);
200         if (seekback == (off_t) -1)
201                 return error("cannot find the current offset");
202
203         header_len = sprintf((char *)obuf, "%s %" PRIuMAX,
204                              typename(type), (uintmax_t)size) + 1;
205         git_SHA1_Init(&ctx);
206         git_SHA1_Update(&ctx, obuf, header_len);
207
208         /* Note: idx is non-NULL when we are writing */
209         if ((flags & HASH_WRITE_OBJECT) != 0)
210                 idx = xcalloc(1, sizeof(*idx));
211
212         already_hashed_to = 0;
213
214         while (1) {
215                 prepare_to_stream(state, flags);
216                 if (idx) {
217                         sha1file_checkpoint(state->f, &checkpoint);
218                         idx->offset = state->offset;
219                         crc32_begin(state->f);
220                 }
221                 if (!stream_to_pack(state, &ctx, &already_hashed_to,
222                                     fd, size, type, path, flags))
223                         break;
224                 /*
225                  * Writing this object to the current pack will make
226                  * it too big; we need to truncate it, start a new
227                  * pack, and write into it.
228                  */
229                 if (!idx)
230                         die("BUG: should not happen");
231                 sha1file_truncate(state->f, &checkpoint);
232                 state->offset = checkpoint.offset;
233                 finish_bulk_checkin(state);
234                 if (lseek(fd, seekback, SEEK_SET) == (off_t) -1)
235                         return error("cannot seek back");
236         }
237         git_SHA1_Final(result_sha1, &ctx);
238         if (!idx)
239                 return 0;
240
241         idx->crc32 = crc32_end(state->f);
242         if (already_written(state, result_sha1)) {
243                 sha1file_truncate(state->f, &checkpoint);
244                 state->offset = checkpoint.offset;
245                 free(idx);
246         } else {
247                 hashcpy(idx->sha1, result_sha1);
248                 ALLOC_GROW(state->written,
249                            state->nr_written + 1,
250                            state->alloc_written);
251                 state->written[state->nr_written++] = idx;
252         }
253         return 0;
254 }
255
256 int index_bulk_checkin(unsigned char *sha1,
257                        int fd, size_t size, enum object_type type,
258                        const char *path, unsigned flags)
259 {
260         int status = deflate_to_pack(&state, sha1, fd, size, type,
261                                      path, flags);
262         if (!state.plugged)
263                 finish_bulk_checkin(&state);
264         return status;
265 }
266
267 void plug_bulk_checkin(void)
268 {
269         state.plugged = 1;
270 }
271
272 void unplug_bulk_checkin(void)
273 {
274         state.plugged = 0;
275         if (state.f)
276                 finish_bulk_checkin(&state);
277 }