]> git.ipfire.org Git - thirdparty/git.git/blame - bulk-checkin.c
treewide: remove unnecessary includes for wrapper.h
[thirdparty/git.git] / bulk-checkin.c
CommitLineData
568508e7
JH
1/*
2 * Copyright (c) 2011, Google Inc.
3 */
b6fdc44c 4#include "git-compat-util.h"
36bf1958 5#include "alloc.h"
568508e7 6#include "bulk-checkin.h"
32a8f510 7#include "environment.h"
f394e093 8#include "gettext.h"
41771fa4 9#include "hex.h"
c0f4752e 10#include "lockfile.h"
a49d2834 11#include "repository.h"
568508e7
JH
12#include "csum-file.h"
13#include "pack.h"
58892711 14#include "strbuf.h"
c0f4752e
NS
15#include "string-list.h"
16#include "tmp-objdir.h"
0abe14f6 17#include "packfile.h"
87bed179 18#include "object-file.h"
a034e910 19#include "object-store-ll.h"
568508e7 20
2c23d1b4 21static int odb_transaction_nesting;
568508e7 22
c0f4752e
NS
23static struct tmp_objdir *bulk_fsync_objdir;
24
897c9e25 25static struct bulk_checkin_packfile {
568508e7 26 char *pack_tmp_name;
98a3beab 27 struct hashfile *f;
568508e7
JH
28 off_t offset;
29 struct pack_idx_option pack_idx_opts;
30
31 struct pack_idx_entry **written;
32 uint32_t alloc_written;
33 uint32_t nr_written;
897c9e25 34} bulk_checkin_packfile;
568508e7 35
2ec02dd5
ÆAB
36static void finish_tmp_packfile(struct strbuf *basename,
37 const char *pack_tmp_name,
38 struct pack_idx_entry **written_list,
39 uint32_t nr_written,
40 struct pack_idx_option *pack_idx_opts,
41 unsigned char hash[])
42{
43 char *idx_tmp_name = NULL;
44
45 stage_tmp_packfiles(basename, pack_tmp_name, written_list, nr_written,
1c573cdd 46 NULL, pack_idx_opts, hash, &idx_tmp_name);
2ec02dd5
ÆAB
47 rename_tmp_packfile_idx(basename, &idx_tmp_name);
48
49 free(idx_tmp_name);
50}
51
897c9e25 52static void flush_bulk_checkin_packfile(struct bulk_checkin_packfile *state)
568508e7 53{
ae44b5a4 54 unsigned char hash[GIT_MAX_RAWSZ];
58892711 55 struct strbuf packname = STRBUF_INIT;
568508e7
JH
56 int i;
57
58 if (!state->f)
59 return;
60
61 if (state->nr_written == 0) {
62 close(state->f->fd);
63 unlink(state->pack_tmp_name);
64 goto clear_exit;
65 } else if (state->nr_written == 1) {
020406ea
NS
66 finalize_hashfile(state->f, hash, FSYNC_COMPONENT_PACK,
67 CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE);
568508e7 68 } else {
020406ea 69 int fd = finalize_hashfile(state->f, hash, FSYNC_COMPONENT_PACK, 0);
ae44b5a4
TB
70 fixup_pack_header_footer(fd, hash, state->pack_tmp_name,
71 state->nr_written, hash,
568508e7
JH
72 state->offset);
73 close(fd);
74 }
75
66833f0e
ÆAB
76 strbuf_addf(&packname, "%s/pack/pack-%s.", get_object_directory(),
77 hash_to_hex(hash));
58892711 78 finish_tmp_packfile(&packname, state->pack_tmp_name,
568508e7 79 state->written, state->nr_written,
ae44b5a4 80 &state->pack_idx_opts, hash);
568508e7
JH
81 for (i = 0; i < state->nr_written; i++)
82 free(state->written[i]);
83
84clear_exit:
85 free(state->written);
86 memset(state, 0, sizeof(*state));
87
58892711 88 strbuf_release(&packname);
568508e7 89 /* Make objects we just wrote available to ourselves */
a49d2834 90 reprepare_packed_git(the_repository);
568508e7
JH
91}
92
c0f4752e
NS
93/*
94 * Cleanup after batch-mode fsync_object_files.
95 */
96static void flush_batch_fsync(void)
97{
98 struct strbuf temp_path = STRBUF_INIT;
99 struct tempfile *temp;
100
101 if (!bulk_fsync_objdir)
102 return;
103
104 /*
105 * Issue a full hardware flush against a temporary file to ensure
106 * that all objects are durable before any renames occur. The code in
107 * fsync_loose_object_bulk_checkin has already issued a writeout
108 * request, but it has not flushed any writeback cache in the storage
109 * hardware or any filesystem logs. This fsync call acts as a barrier
110 * to ensure that the data in each new object file is durable before
111 * the final name is visible.
112 */
113 strbuf_addf(&temp_path, "%s/bulk_fsync_XXXXXX", get_object_directory());
114 temp = xmks_tempfile(temp_path.buf);
115 fsync_or_die(get_tempfile_fd(temp), get_tempfile_path(temp));
116 delete_tempfile(&temp);
117 strbuf_release(&temp_path);
118
119 /*
120 * Make the object files visible in the primary ODB after their data is
121 * fully durable.
122 */
123 tmp_objdir_migrate(bulk_fsync_objdir);
124 bulk_fsync_objdir = NULL;
125}
126
897c9e25 127static int already_written(struct bulk_checkin_packfile *state, struct object_id *oid)
568508e7
JH
128{
129 int i;
130
131 /* The object may already exist in the repository */
bc726bd0 132 if (repo_has_object_file(the_repository, oid))
568508e7
JH
133 return 1;
134
135 /* Might want to keep the list sorted */
136 for (i = 0; i < state->nr_written; i++)
4a7e27e9 137 if (oideq(&state->written[i]->oid, oid))
568508e7
JH
138 return 1;
139
140 /* This is a new object we need to keep */
141 return 0;
142}
143
144/*
145 * Read the contents from fd for size bytes, streaming it to the
146 * packfile in state while updating the hash in ctx. Signal a failure
147 * by returning a negative value when the resulting pack would exceed
148 * the pack size limit and this is not the first object in the pack,
149 * so that the caller can discard what we wrote from the current pack
150 * by truncating it and opening a new one. The caller will then call
151 * us again after rewinding the input fd.
152 *
153 * The already_hashed_to pointer is kept untouched by the caller to
154 * make sure we do not hash the same byte when we are called
155 * again. This way, the caller does not have to checkpoint its hash
156 * status before calling us just in case we ask it to call us again
157 * with a new pack.
158 */
897c9e25 159static int stream_to_pack(struct bulk_checkin_packfile *state,
f87e8137 160 git_hash_ctx *ctx, off_t *already_hashed_to,
568508e7
JH
161 int fd, size_t size, enum object_type type,
162 const char *path, unsigned flags)
163{
164 git_zstream s;
f96178c5 165 unsigned char ibuf[16384];
568508e7
JH
166 unsigned char obuf[16384];
167 unsigned hdrlen;
168 int status = Z_OK;
169 int write_object = (flags & HASH_WRITE_OBJECT);
170 off_t offset = 0;
171
568508e7
JH
172 git_deflate_init(&s, pack_compression_level);
173
7202a6fa 174 hdrlen = encode_in_pack_object_header(obuf, sizeof(obuf), type, size);
568508e7
JH
175 s.next_out = obuf + hdrlen;
176 s.avail_out = sizeof(obuf) - hdrlen;
177
178 while (status != Z_STREAM_END) {
568508e7
JH
179 if (size && !s.avail_in) {
180 ssize_t rsize = size < sizeof(ibuf) ? size : sizeof(ibuf);
41dcc4dc
JK
181 ssize_t read_result = read_in_full(fd, ibuf, rsize);
182 if (read_result < 0)
183 die_errno("failed to read from '%s'", path);
184 if (read_result != rsize)
568508e7
JH
185 die("failed to read %d bytes from '%s'",
186 (int)rsize, path);
187 offset += rsize;
188 if (*already_hashed_to < offset) {
189 size_t hsize = offset - *already_hashed_to;
190 if (rsize < hsize)
191 hsize = rsize;
192 if (hsize)
f87e8137 193 the_hash_algo->update_fn(ctx, ibuf, hsize);
568508e7
JH
194 *already_hashed_to = offset;
195 }
196 s.next_in = ibuf;
197 s.avail_in = rsize;
198 size -= rsize;
199 }
200
201 status = git_deflate(&s, size ? 0 : Z_FINISH);
202
203 if (!s.avail_out || status == Z_STREAM_END) {
204 if (write_object) {
205 size_t written = s.next_out - obuf;
206
207 /* would we bust the size limit? */
208 if (state->nr_written &&
209 pack_size_limit_cfg &&
210 pack_size_limit_cfg < state->offset + written) {
211 git_deflate_abort(&s);
212 return -1;
213 }
214
98a3beab 215 hashwrite(state->f, obuf, written);
568508e7
JH
216 state->offset += written;
217 }
218 s.next_out = obuf;
219 s.avail_out = sizeof(obuf);
220 }
221
222 switch (status) {
223 case Z_OK:
224 case Z_BUF_ERROR:
225 case Z_STREAM_END:
226 continue;
227 default:
228 die("unexpected deflate failure: %d", status);
229 }
230 }
231 git_deflate_end(&s);
232 return 0;
233}
234
235/* Lazily create backing packfile for the state */
897c9e25 236static void prepare_to_stream(struct bulk_checkin_packfile *state,
568508e7
JH
237 unsigned flags)
238{
239 if (!(flags & HASH_WRITE_OBJECT) || state->f)
240 return;
241
242 state->f = create_tmp_packfile(&state->pack_tmp_name);
243 reset_pack_idx_option(&state->pack_idx_opts);
244
245 /* Pretend we are going to write only one object */
246 state->offset = write_pack_header(state->f, 1);
247 if (!state->offset)
248 die_errno("unable to write pack header");
249}
250
897c9e25 251static int deflate_to_pack(struct bulk_checkin_packfile *state,
68ee6dfc 252 struct object_id *result_oid,
568508e7
JH
253 int fd, size_t size,
254 enum object_type type, const char *path,
255 unsigned flags)
256{
257 off_t seekback, already_hashed_to;
f87e8137 258 git_hash_ctx ctx;
568508e7
JH
259 unsigned char obuf[16384];
260 unsigned header_len;
7140414d 261 struct hashfile_checkpoint checkpoint = {0};
568508e7
JH
262 struct pack_idx_entry *idx = NULL;
263
264 seekback = lseek(fd, 0, SEEK_CUR);
265 if (seekback == (off_t) -1)
266 return error("cannot find the current offset");
267
b04cdea4
ÆAB
268 header_len = format_object_header((char *)obuf, sizeof(obuf),
269 type, size);
f87e8137 270 the_hash_algo->init_fn(&ctx);
271 the_hash_algo->update_fn(&ctx, obuf, header_len);
568508e7
JH
272
273 /* Note: idx is non-NULL when we are writing */
274 if ((flags & HASH_WRITE_OBJECT) != 0)
ca56dadb 275 CALLOC_ARRAY(idx, 1);
568508e7
JH
276
277 already_hashed_to = 0;
278
279 while (1) {
280 prepare_to_stream(state, flags);
281 if (idx) {
98a3beab 282 hashfile_checkpoint(state->f, &checkpoint);
568508e7
JH
283 idx->offset = state->offset;
284 crc32_begin(state->f);
285 }
286 if (!stream_to_pack(state, &ctx, &already_hashed_to,
287 fd, size, type, path, flags))
288 break;
289 /*
290 * Writing this object to the current pack will make
291 * it too big; we need to truncate it, start a new
292 * pack, and write into it.
293 */
294 if (!idx)
033abf97 295 BUG("should not happen");
98a3beab 296 hashfile_truncate(state->f, &checkpoint);
568508e7 297 state->offset = checkpoint.offset;
897c9e25 298 flush_bulk_checkin_packfile(state);
568508e7
JH
299 if (lseek(fd, seekback, SEEK_SET) == (off_t) -1)
300 return error("cannot seek back");
301 }
5951bf46 302 the_hash_algo->final_oid_fn(result_oid, &ctx);
568508e7
JH
303 if (!idx)
304 return 0;
305
306 idx->crc32 = crc32_end(state->f);
68ee6dfc 307 if (already_written(state, result_oid)) {
98a3beab 308 hashfile_truncate(state->f, &checkpoint);
568508e7
JH
309 state->offset = checkpoint.offset;
310 free(idx);
311 } else {
68ee6dfc 312 oidcpy(&idx->oid, result_oid);
568508e7
JH
313 ALLOC_GROW(state->written,
314 state->nr_written + 1,
315 state->alloc_written);
316 state->written[state->nr_written++] = idx;
317 }
318 return 0;
319}
320
c0f4752e
NS
321void prepare_loose_object_bulk_checkin(void)
322{
323 /*
324 * We lazily create the temporary object directory
325 * the first time an object might be added, since
326 * callers may not know whether any objects will be
327 * added at the time they call begin_odb_transaction.
328 */
329 if (!odb_transaction_nesting || bulk_fsync_objdir)
330 return;
331
332 bulk_fsync_objdir = tmp_objdir_create("bulk-fsync");
333 if (bulk_fsync_objdir)
334 tmp_objdir_replace_primary_odb(bulk_fsync_objdir, 0);
335}
336
337void fsync_loose_object_bulk_checkin(int fd, const char *filename)
338{
339 /*
340 * If we have an active ODB transaction, we issue a call that
341 * cleans the filesystem page cache but avoids a hardware flush
342 * command. Later on we will issue a single hardware flush
343 * before renaming the objects to their final names as part of
344 * flush_batch_fsync.
345 */
346 if (!bulk_fsync_objdir ||
347 git_fsync(fd, FSYNC_WRITEOUT_ONLY) < 0) {
ce50f1f3
JS
348 if (errno == ENOSYS)
349 warning(_("core.fsyncMethod = batch is unsupported on this platform"));
c0f4752e
NS
350 fsync_or_die(fd, filename);
351 }
352}
353
68ee6dfc 354int index_bulk_checkin(struct object_id *oid,
568508e7
JH
355 int fd, size_t size, enum object_type type,
356 const char *path, unsigned flags)
357{
897c9e25 358 int status = deflate_to_pack(&bulk_checkin_packfile, oid, fd, size, type,
568508e7 359 path, flags);
2c23d1b4 360 if (!odb_transaction_nesting)
897c9e25 361 flush_bulk_checkin_packfile(&bulk_checkin_packfile);
568508e7
JH
362 return status;
363}
364
2c23d1b4 365void begin_odb_transaction(void)
568508e7 366{
2c23d1b4 367 odb_transaction_nesting += 1;
568508e7
JH
368}
369
2c23d1b4 370void flush_odb_transaction(void)
568508e7 371{
c0f4752e 372 flush_batch_fsync();
897c9e25 373 flush_bulk_checkin_packfile(&bulk_checkin_packfile);
568508e7 374}
2c23d1b4
NS
375
376void end_odb_transaction(void)
377{
378 odb_transaction_nesting -= 1;
379 if (odb_transaction_nesting < 0)
380 BUG("Unbalanced ODB transaction nesting");
381
382 if (odb_transaction_nesting)
383 return;
384
385 flush_odb_transaction();
568508e7 386}