]>
Commit | Line | Data |
---|---|---|
568508e7 JH |
1 | /* |
2 | * Copyright (c) 2011, Google Inc. | |
3 | */ | |
1c4b6604 | 4 | #include "cache.h" |
568508e7 | 5 | #include "bulk-checkin.h" |
a49d2834 | 6 | #include "repository.h" |
568508e7 JH |
7 | #include "csum-file.h" |
8 | #include "pack.h" | |
58892711 | 9 | #include "strbuf.h" |
0abe14f6 | 10 | #include "packfile.h" |
cbd53a21 | 11 | #include "object-store.h" |
568508e7 | 12 | |
568508e7 JH |
13 | static struct bulk_checkin_state { |
14 | unsigned plugged:1; | |
15 | ||
16 | char *pack_tmp_name; | |
98a3beab | 17 | struct hashfile *f; |
568508e7 JH |
18 | off_t offset; |
19 | struct pack_idx_option pack_idx_opts; | |
20 | ||
21 | struct pack_idx_entry **written; | |
22 | uint32_t alloc_written; | |
23 | uint32_t nr_written; | |
24 | } state; | |
25 | ||
2ec02dd5 ÆAB |
26 | static void finish_tmp_packfile(struct strbuf *basename, |
27 | const char *pack_tmp_name, | |
28 | struct pack_idx_entry **written_list, | |
29 | uint32_t nr_written, | |
30 | struct pack_idx_option *pack_idx_opts, | |
31 | unsigned char hash[]) | |
32 | { | |
33 | char *idx_tmp_name = NULL; | |
34 | ||
35 | stage_tmp_packfiles(basename, pack_tmp_name, written_list, nr_written, | |
36 | pack_idx_opts, hash, &idx_tmp_name); | |
37 | rename_tmp_packfile_idx(basename, &idx_tmp_name); | |
38 | ||
39 | free(idx_tmp_name); | |
40 | } | |
41 | ||
568508e7 JH |
42 | static void finish_bulk_checkin(struct bulk_checkin_state *state) |
43 | { | |
ae44b5a4 | 44 | unsigned char hash[GIT_MAX_RAWSZ]; |
58892711 | 45 | struct strbuf packname = STRBUF_INIT; |
568508e7 JH |
46 | int i; |
47 | ||
48 | if (!state->f) | |
49 | return; | |
50 | ||
51 | if (state->nr_written == 0) { | |
52 | close(state->f->fd); | |
53 | unlink(state->pack_tmp_name); | |
54 | goto clear_exit; | |
55 | } else if (state->nr_written == 1) { | |
ae44b5a4 | 56 | finalize_hashfile(state->f, hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE); |
568508e7 | 57 | } else { |
ae44b5a4 TB |
58 | int fd = finalize_hashfile(state->f, hash, 0); |
59 | fixup_pack_header_footer(fd, hash, state->pack_tmp_name, | |
60 | state->nr_written, hash, | |
568508e7 JH |
61 | state->offset); |
62 | close(fd); | |
63 | } | |
64 | ||
66833f0e ÆAB |
65 | strbuf_addf(&packname, "%s/pack/pack-%s.", get_object_directory(), |
66 | hash_to_hex(hash)); | |
58892711 | 67 | finish_tmp_packfile(&packname, state->pack_tmp_name, |
568508e7 | 68 | state->written, state->nr_written, |
ae44b5a4 | 69 | &state->pack_idx_opts, hash); |
568508e7 JH |
70 | for (i = 0; i < state->nr_written; i++) |
71 | free(state->written[i]); | |
72 | ||
73 | clear_exit: | |
74 | free(state->written); | |
75 | memset(state, 0, sizeof(*state)); | |
76 | ||
58892711 | 77 | strbuf_release(&packname); |
568508e7 | 78 | /* Make objects we just wrote available to ourselves */ |
a49d2834 | 79 | reprepare_packed_git(the_repository); |
568508e7 JH |
80 | } |
81 | ||
68ee6dfc | 82 | static int already_written(struct bulk_checkin_state *state, struct object_id *oid) |
568508e7 JH |
83 | { |
84 | int i; | |
85 | ||
86 | /* The object may already exist in the repository */ | |
98374a07 | 87 | if (has_object_file(oid)) |
568508e7 JH |
88 | return 1; |
89 | ||
90 | /* Might want to keep the list sorted */ | |
91 | for (i = 0; i < state->nr_written; i++) | |
4a7e27e9 | 92 | if (oideq(&state->written[i]->oid, oid)) |
568508e7 JH |
93 | return 1; |
94 | ||
95 | /* This is a new object we need to keep */ | |
96 | return 0; | |
97 | } | |
98 | ||
99 | /* | |
100 | * Read the contents from fd for size bytes, streaming it to the | |
101 | * packfile in state while updating the hash in ctx. Signal a failure | |
102 | * by returning a negative value when the resulting pack would exceed | |
103 | * the pack size limit and this is not the first object in the pack, | |
104 | * so that the caller can discard what we wrote from the current pack | |
105 | * by truncating it and opening a new one. The caller will then call | |
106 | * us again after rewinding the input fd. | |
107 | * | |
108 | * The already_hashed_to pointer is kept untouched by the caller to | |
109 | * make sure we do not hash the same byte when we are called | |
110 | * again. This way, the caller does not have to checkpoint its hash | |
111 | * status before calling us just in case we ask it to call us again | |
112 | * with a new pack. | |
113 | */ | |
114 | static int stream_to_pack(struct bulk_checkin_state *state, | |
f87e8137 | 115 | git_hash_ctx *ctx, off_t *already_hashed_to, |
568508e7 JH |
116 | int fd, size_t size, enum object_type type, |
117 | const char *path, unsigned flags) | |
118 | { | |
119 | git_zstream s; | |
f96178c5 | 120 | unsigned char ibuf[16384]; |
568508e7 JH |
121 | unsigned char obuf[16384]; |
122 | unsigned hdrlen; | |
123 | int status = Z_OK; | |
124 | int write_object = (flags & HASH_WRITE_OBJECT); | |
125 | off_t offset = 0; | |
126 | ||
568508e7 JH |
127 | git_deflate_init(&s, pack_compression_level); |
128 | ||
7202a6fa | 129 | hdrlen = encode_in_pack_object_header(obuf, sizeof(obuf), type, size); |
568508e7 JH |
130 | s.next_out = obuf + hdrlen; |
131 | s.avail_out = sizeof(obuf) - hdrlen; | |
132 | ||
133 | while (status != Z_STREAM_END) { | |
568508e7 JH |
134 | if (size && !s.avail_in) { |
135 | ssize_t rsize = size < sizeof(ibuf) ? size : sizeof(ibuf); | |
41dcc4dc JK |
136 | ssize_t read_result = read_in_full(fd, ibuf, rsize); |
137 | if (read_result < 0) | |
138 | die_errno("failed to read from '%s'", path); | |
139 | if (read_result != rsize) | |
568508e7 JH |
140 | die("failed to read %d bytes from '%s'", |
141 | (int)rsize, path); | |
142 | offset += rsize; | |
143 | if (*already_hashed_to < offset) { | |
144 | size_t hsize = offset - *already_hashed_to; | |
145 | if (rsize < hsize) | |
146 | hsize = rsize; | |
147 | if (hsize) | |
f87e8137 | 148 | the_hash_algo->update_fn(ctx, ibuf, hsize); |
568508e7 JH |
149 | *already_hashed_to = offset; |
150 | } | |
151 | s.next_in = ibuf; | |
152 | s.avail_in = rsize; | |
153 | size -= rsize; | |
154 | } | |
155 | ||
156 | status = git_deflate(&s, size ? 0 : Z_FINISH); | |
157 | ||
158 | if (!s.avail_out || status == Z_STREAM_END) { | |
159 | if (write_object) { | |
160 | size_t written = s.next_out - obuf; | |
161 | ||
162 | /* would we bust the size limit? */ | |
163 | if (state->nr_written && | |
164 | pack_size_limit_cfg && | |
165 | pack_size_limit_cfg < state->offset + written) { | |
166 | git_deflate_abort(&s); | |
167 | return -1; | |
168 | } | |
169 | ||
98a3beab | 170 | hashwrite(state->f, obuf, written); |
568508e7 JH |
171 | state->offset += written; |
172 | } | |
173 | s.next_out = obuf; | |
174 | s.avail_out = sizeof(obuf); | |
175 | } | |
176 | ||
177 | switch (status) { | |
178 | case Z_OK: | |
179 | case Z_BUF_ERROR: | |
180 | case Z_STREAM_END: | |
181 | continue; | |
182 | default: | |
183 | die("unexpected deflate failure: %d", status); | |
184 | } | |
185 | } | |
186 | git_deflate_end(&s); | |
187 | return 0; | |
188 | } | |
189 | ||
190 | /* Lazily create backing packfile for the state */ | |
191 | static void prepare_to_stream(struct bulk_checkin_state *state, | |
192 | unsigned flags) | |
193 | { | |
194 | if (!(flags & HASH_WRITE_OBJECT) || state->f) | |
195 | return; | |
196 | ||
197 | state->f = create_tmp_packfile(&state->pack_tmp_name); | |
198 | reset_pack_idx_option(&state->pack_idx_opts); | |
199 | ||
200 | /* Pretend we are going to write only one object */ | |
201 | state->offset = write_pack_header(state->f, 1); | |
202 | if (!state->offset) | |
203 | die_errno("unable to write pack header"); | |
204 | } | |
205 | ||
206 | static int deflate_to_pack(struct bulk_checkin_state *state, | |
68ee6dfc | 207 | struct object_id *result_oid, |
568508e7 JH |
208 | int fd, size_t size, |
209 | enum object_type type, const char *path, | |
210 | unsigned flags) | |
211 | { | |
212 | off_t seekback, already_hashed_to; | |
f87e8137 | 213 | git_hash_ctx ctx; |
568508e7 JH |
214 | unsigned char obuf[16384]; |
215 | unsigned header_len; | |
7140414d | 216 | struct hashfile_checkpoint checkpoint = {0}; |
568508e7 JH |
217 | struct pack_idx_entry *idx = NULL; |
218 | ||
219 | seekback = lseek(fd, 0, SEEK_CUR); | |
220 | if (seekback == (off_t) -1) | |
221 | return error("cannot find the current offset"); | |
222 | ||
b04cdea4 ÆAB |
223 | header_len = format_object_header((char *)obuf, sizeof(obuf), |
224 | type, size); | |
f87e8137 | 225 | the_hash_algo->init_fn(&ctx); |
226 | the_hash_algo->update_fn(&ctx, obuf, header_len); | |
568508e7 JH |
227 | |
228 | /* Note: idx is non-NULL when we are writing */ | |
229 | if ((flags & HASH_WRITE_OBJECT) != 0) | |
ca56dadb | 230 | CALLOC_ARRAY(idx, 1); |
568508e7 JH |
231 | |
232 | already_hashed_to = 0; | |
233 | ||
234 | while (1) { | |
235 | prepare_to_stream(state, flags); | |
236 | if (idx) { | |
98a3beab | 237 | hashfile_checkpoint(state->f, &checkpoint); |
568508e7 JH |
238 | idx->offset = state->offset; |
239 | crc32_begin(state->f); | |
240 | } | |
241 | if (!stream_to_pack(state, &ctx, &already_hashed_to, | |
242 | fd, size, type, path, flags)) | |
243 | break; | |
244 | /* | |
245 | * Writing this object to the current pack will make | |
246 | * it too big; we need to truncate it, start a new | |
247 | * pack, and write into it. | |
248 | */ | |
249 | if (!idx) | |
033abf97 | 250 | BUG("should not happen"); |
98a3beab | 251 | hashfile_truncate(state->f, &checkpoint); |
568508e7 JH |
252 | state->offset = checkpoint.offset; |
253 | finish_bulk_checkin(state); | |
254 | if (lseek(fd, seekback, SEEK_SET) == (off_t) -1) | |
255 | return error("cannot seek back"); | |
256 | } | |
5951bf46 | 257 | the_hash_algo->final_oid_fn(result_oid, &ctx); |
568508e7 JH |
258 | if (!idx) |
259 | return 0; | |
260 | ||
261 | idx->crc32 = crc32_end(state->f); | |
68ee6dfc | 262 | if (already_written(state, result_oid)) { |
98a3beab | 263 | hashfile_truncate(state->f, &checkpoint); |
568508e7 JH |
264 | state->offset = checkpoint.offset; |
265 | free(idx); | |
266 | } else { | |
68ee6dfc | 267 | oidcpy(&idx->oid, result_oid); |
568508e7 JH |
268 | ALLOC_GROW(state->written, |
269 | state->nr_written + 1, | |
270 | state->alloc_written); | |
271 | state->written[state->nr_written++] = idx; | |
272 | } | |
273 | return 0; | |
274 | } | |
275 | ||
68ee6dfc | 276 | int index_bulk_checkin(struct object_id *oid, |
568508e7 JH |
277 | int fd, size_t size, enum object_type type, |
278 | const char *path, unsigned flags) | |
279 | { | |
68ee6dfc | 280 | int status = deflate_to_pack(&state, oid, fd, size, type, |
568508e7 JH |
281 | path, flags); |
282 | if (!state.plugged) | |
283 | finish_bulk_checkin(&state); | |
284 | return status; | |
285 | } | |
286 | ||
287 | void plug_bulk_checkin(void) | |
288 | { | |
289 | state.plugged = 1; | |
290 | } | |
291 | ||
292 | void unplug_bulk_checkin(void) | |
293 | { | |
294 | state.plugged = 0; | |
295 | if (state.f) | |
296 | finish_bulk_checkin(&state); | |
297 | } |