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