interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / object-file.c

1592 lines · 42370 bytes Raw Blame History
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 *
6 * This handles basic git object files - packing, unpacking,
7 * creation etc.
8 */
9
10#define USE_THE_REPOSITORY_VARIABLE
11
12#include "git-compat-util.h"
13#include "config.h"
14#include "convert.h"
15#include "dir.h"
16#include "environment.h"
17#include "fsck.h"
18#include "gettext.h"
19#include "hex.h"
20#include "loose.h"
21#include "object-file-convert.h"
22#include "object-file.h"
23#include "odb.h"
24#include "odb/streaming.h"
25#include "odb/transaction.h"
26#include "pack.h"
27#include "packfile.h"
28#include "path.h"
29#include "read-cache-ll.h"
30#include "run-command.h"
31#include "setup.h"
32#include "strvec.h"
33#include "tempfile.h"
34#include "tmp-objdir.h"
35
36static int get_conv_flags(unsigned flags)
37{
38 if (flags & INDEX_RENORMALIZE)
39 return CONV_EOL_RENORMALIZE;
40 else if (flags & INDEX_WRITE_OBJECT)
41 return global_conv_flags_eol | CONV_WRITE_OBJECT;
42 else
43 return 0;
44}
45
46static void fill_loose_path(struct strbuf *buf,
47 const struct object_id *oid,
48 const struct git_hash_algo *algop)
49{
50 for (size_t i = 0; i < algop->rawsz; i++) {
51 static char hex[] = "0123456789abcdef";
52 unsigned int val = oid->hash[i];
53 strbuf_addch(buf, hex[val >> 4]);
54 strbuf_addch(buf, hex[val & 0xf]);
55 if (!i)
56 strbuf_addch(buf, '/');
57 }
58}
59
60const char *odb_loose_path(struct odb_source_loose *loose,
61 struct strbuf *buf,
62 const struct object_id *oid)
63{
64 strbuf_reset(buf);
65 strbuf_addstr(buf, loose->base.path);
66 strbuf_addch(buf, '/');
67 fill_loose_path(buf, oid, loose->base.odb->repo->hash_algo);
68 return buf->buf;
69}
70
71/* Returns 1 if we have successfully freshened the file, 0 otherwise. */
72static int freshen_file(const char *fn, const time_t *mtime)
73{
74 struct utimbuf times, *timesp = NULL;
75
76 if (mtime) {
77 times.actime = *mtime;
78 times.modtime = *mtime;
79 timesp = &times;
80 }
81
82 return !utime(fn, timesp);
83}
84
85/*
86 * All of the check_and_freshen functions return 1 if the file exists and was
87 * freshened (if freshening was requested), 0 otherwise. If they return
88 * 0, you should not assume that it is safe to skip a write of the object (it
89 * either does not exist on disk, or has a stale mtime and may be subject to
90 * pruning).
91 */
92int check_and_freshen_file(const char *fn, int freshen,
93 const time_t *mtime)
94{
95 if (access(fn, F_OK))
96 return 0;
97 if (freshen && !freshen_file(fn, mtime))
98 return 0;
99 return 1;
100}
101
102int format_object_header(char *str, size_t size, enum object_type type,
103 size_t objsize)
104{
105 const char *name = type_name(type);
106
107 if (!name)
108 BUG("could not get a type name for 'enum object_type' value %d", type);
109
110 return xsnprintf(str, size, "%s %"PRIuMAX, name, (uintmax_t)objsize) + 1;
111}
112
113int check_object_signature(struct repository *r, const struct object_id *oid,
114 void *buf, unsigned long size,
115 enum object_type type)
116{
117 const struct git_hash_algo *algo =
118 oid->algo ? &hash_algos[oid->algo] : r->hash_algo;
119 struct object_id real_oid;
120
121 hash_object_file(algo, buf, size, type, &real_oid);
122
123 return !oideq(oid, &real_oid) ? -1 : 0;
124}
125
126int stream_object_signature(struct repository *r,
127 struct odb_stream *st,
128 const struct object_id *oid)
129{
130 struct object_id real_oid;
131 struct git_hash_ctx c;
132 char hdr[MAX_HEADER_LEN];
133 int hdrlen;
134
135 /* Generate the header */
136 hdrlen = format_object_header(hdr, sizeof(hdr), st->type, st->size);
137
138 /* Sha1.. */
139 git_hash_init(&c, r->hash_algo);
140 git_hash_update(&c, hdr, hdrlen);
141 for (;;) {
142 char buf[1024 * 16];
143 ssize_t readlen = odb_stream_read(st, buf, sizeof(buf));
144 if (readlen < 0)
145 return -1;
146 if (!readlen)
147 break;
148 git_hash_update(&c, buf, readlen);
149 }
150 git_hash_final_oid(&real_oid, &c);
151 return !oideq(oid, &real_oid) ? -1 : 0;
152}
153
154/*
155 * Map and close the given loose object fd. The path argument is used for
156 * error reporting.
157 */
158static void *map_fd(int fd, const char *path, unsigned long *size)
159{
160 void *map = NULL;
161 struct stat st;
162
163 if (!fstat(fd, &st)) {
164 *size = xsize_t(st.st_size);
165 if (!*size) {
166 /* mmap() is forbidden on empty files */
167 error(_("object file %s is empty"), path);
168 close(fd);
169 return NULL;
170 }
171 map = xmmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0);
172 }
173 close(fd);
174 return map;
175}
176
177enum unpack_loose_header_result unpack_loose_header(git_zstream *stream,
178 unsigned char *map,
179 unsigned long mapsize,
180 void *buffer,
181 unsigned long bufsiz)
182{
183 int status;
184
185 /* Get the data stream */
186 memset(stream, 0, sizeof(*stream));
187 stream->next_in = map;
188 stream->avail_in = mapsize;
189 stream->next_out = buffer;
190 stream->avail_out = bufsiz;
191
192 git_inflate_init(stream);
193 obj_read_unlock();
194 status = git_inflate(stream, 0);
195 obj_read_lock();
196 if (status != Z_OK && status != Z_STREAM_END)
197 return ULHR_BAD;
198
199 /*
200 * Check if entire header is unpacked in the first iteration.
201 */
202 if (memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer))
203 return ULHR_OK;
204
205 /*
206 * We have a header longer than MAX_HEADER_LEN.
207 */
208 return ULHR_TOO_LONG;
209}
210
211void *unpack_loose_rest(git_zstream *stream,
212 void *buffer, unsigned long size,
213 const struct object_id *oid)
214{
215 size_t bytes = strlen(buffer) + 1, n;
216 unsigned char *buf = xmallocz(size);
217 int status = Z_OK;
218
219 n = stream->total_out - bytes;
220 if (n > size)
221 n = size;
222 memcpy(buf, (char *) buffer + bytes, n);
223 bytes = n;
224 if (bytes <= size) {
225 /*
226 * The above condition must be (bytes <= size), not
227 * (bytes < size). In other words, even though we
228 * expect no more output and set avail_out to zero,
229 * the input zlib stream may have bytes that express
230 * "this concludes the stream", and we *do* want to
231 * eat that input.
232 *
233 * Otherwise we would not be able to test that we
234 * consumed all the input to reach the expected size;
235 * we also want to check that zlib tells us that all
236 * went well with status == Z_STREAM_END at the end.
237 */
238 stream->next_out = buf + bytes;
239 stream->avail_out = size - bytes;
240 while (status == Z_OK) {
241 obj_read_unlock();
242 status = git_inflate(stream, Z_FINISH);
243 obj_read_lock();
244 }
245 }
246
247 if (status != Z_STREAM_END) {
248 error(_("corrupt loose object '%s'"), oid_to_hex(oid));
249 FREE_AND_NULL(buf);
250 } else if (stream->avail_in) {
251 error(_("garbage at end of loose object '%s'"),
252 oid_to_hex(oid));
253 FREE_AND_NULL(buf);
254 }
255
256 return buf;
257}
258
259/*
260 * parse_loose_header() parses the starting "<type> <len>\0" of an
261 * object. If it doesn't follow that format -1 is returned. To check
262 * the validity of the <type> populate the "typep" in the "struct
263 * object_info". It will be OBJ_BAD if the object type is unknown. The
264 * parsed <len> can be retrieved via "oi->sizep", and from there
265 * passed to unpack_loose_rest().
266 *
267 * We used to just use "sscanf()", but that's actually way
268 * too permissive for what we want to check. So do an anal
269 * object header parse by hand.
270 */
271int parse_loose_header(const char *hdr, struct object_info *oi)
272{
273 const char *type_buf = hdr;
274 size_t size;
275 int type, type_len = 0;
276
277 /*
278 * The type can be of any size but is followed by
279 * a space.
280 */
281 for (;;) {
282 char c = *hdr++;
283 if (!c)
284 return -1;
285 if (c == ' ')
286 break;
287 type_len++;
288 }
289
290 type = type_from_string_gently(type_buf, type_len, 1);
291 if (oi->typep)
292 *oi->typep = type;
293
294 /*
295 * The length must follow immediately, and be in canonical
296 * decimal format (ie "010" is not valid).
297 */
298 size = *hdr++ - '0';
299 if (size > 9)
300 return -1;
301 if (size) {
302 for (;;) {
303 unsigned long c = *hdr - '0';
304 if (c > 9)
305 break;
306 hdr++;
307 size = st_add(st_mult(size, 10), c);
308 }
309 }
310
311 if (oi->sizep)
312 *oi->sizep = size;
313
314 /*
315 * The length must be followed by a zero byte
316 */
317 if (*hdr)
318 return -1;
319
320 /*
321 * The format is valid, but the type may still be bogus. The
322 * Caller needs to check its oi->typep.
323 */
324 return 0;
325}
326
327#define CHECK_COLLISION_DEST_VANISHED -2
328
329static int check_collision(const char *source, const char *dest)
330{
331 char buf_source[4096], buf_dest[4096];
332 int fd_source = -1, fd_dest = -1;
333 int ret = 0;
334
335 fd_source = open(source, O_RDONLY);
336 if (fd_source < 0) {
337 ret = error_errno(_("unable to open %s"), source);
338 goto out;
339 }
340
341 fd_dest = open(dest, O_RDONLY);
342 if (fd_dest < 0) {
343 if (errno != ENOENT)
344 ret = error_errno(_("unable to open %s"), dest);
345 else
346 ret = CHECK_COLLISION_DEST_VANISHED;
347 goto out;
348 }
349
350 while (1) {
351 ssize_t sz_a, sz_b;
352
353 sz_a = read_in_full(fd_source, buf_source, sizeof(buf_source));
354 if (sz_a < 0) {
355 ret = error_errno(_("unable to read %s"), source);
356 goto out;
357 }
358
359 sz_b = read_in_full(fd_dest, buf_dest, sizeof(buf_dest));
360 if (sz_b < 0) {
361 ret = error_errno(_("unable to read %s"), dest);
362 goto out;
363 }
364
365 if (sz_a != sz_b || memcmp(buf_source, buf_dest, sz_a)) {
366 ret = error(_("files '%s' and '%s' differ in contents"),
367 source, dest);
368 goto out;
369 }
370
371 if ((size_t) sz_a < sizeof(buf_source))
372 break;
373 }
374
375out:
376 if (fd_source > -1)
377 close(fd_source);
378 if (fd_dest > -1)
379 close(fd_dest);
380 return ret;
381}
382
383/*
384 * Move the just written object into its final resting place.
385 */
386int finalize_object_file(struct repository *repo,
387 const char *tmpfile, const char *filename)
388{
389 return finalize_object_file_flags(repo, tmpfile, filename, 0);
390}
391
392int finalize_object_file_flags(struct repository *repo,
393 const char *tmpfile, const char *filename,
394 enum finalize_object_file_flags flags)
395{
396 unsigned retries = 0;
397 int ret;
398 struct repo_config_values *cfg = repo_config_values(repo);
399
400retry:
401 ret = 0;
402
403 if (cfg->object_creation_mode == OBJECT_CREATION_USES_RENAMES)
404 goto try_rename;
405 else if (link(tmpfile, filename))
406 ret = errno;
407 else
408 unlink_or_warn(tmpfile);
409
410 /*
411 * Coda hack - coda doesn't like cross-directory links,
412 * so we fall back to a rename, which will mean that it
413 * won't be able to check collisions, but that's not a
414 * big deal.
415 *
416 * The same holds for FAT formatted media.
417 *
418 * When this succeeds, we just return. We have nothing
419 * left to unlink.
420 */
421 if (ret && ret != EEXIST) {
422 struct stat st;
423
424 try_rename:
425 if (!stat(filename, &st))
426 ret = EEXIST;
427 else if (!rename(tmpfile, filename))
428 goto out;
429 else
430 ret = errno;
431 }
432 if (ret) {
433 if (ret != EEXIST) {
434 int saved_errno = errno;
435 unlink_or_warn(tmpfile);
436 errno = saved_errno;
437 return error_errno(_("unable to write file %s"), filename);
438 }
439 if (!(flags & FOF_SKIP_COLLISION_CHECK)) {
440 ret = check_collision(tmpfile, filename);
441 if (ret == CHECK_COLLISION_DEST_VANISHED) {
442 if (retries++ > 5)
443 return error(_("unable to write repeatedly vanishing file %s"),
444 filename);
445 goto retry;
446 }
447 else if (ret)
448 return -1;
449 }
450 unlink_or_warn(tmpfile);
451 }
452
453out:
454 if (adjust_shared_perm(repo, filename))
455 return error(_("unable to set permission to '%s'"), filename);
456 return 0;
457}
458
459void hash_object_file(const struct git_hash_algo *algo, const void *buf,
460 size_t len, enum object_type type,
461 struct object_id *oid)
462{
463 struct git_hash_ctx c;
464 char hdr[MAX_HEADER_LEN];
465 int hdrlen;
466
467 hdrlen = format_object_header(hdr, sizeof(hdr), type, len);
468
469 git_hash_init(&c, algo);
470 git_hash_update(&c, hdr, hdrlen);
471 git_hash_update(&c, buf, len);
472 git_hash_final_oid(oid, &c);
473}
474
475struct transaction_packfile {
476 char *pack_tmp_name;
477 struct hashfile *f;
478 off_t offset;
479 struct pack_idx_option pack_idx_opts;
480
481 struct pack_idx_entry **written;
482 uint32_t alloc_written;
483 uint32_t nr_written;
484};
485
486struct odb_transaction_files {
487 struct odb_transaction base;
488 enum odb_transaction_flags flags;
489
490 struct tmp_objdir *objdir;
491 struct transaction_packfile packfile;
492 const char *prefix;
493
494 struct strvec pack_names;
495 struct tempfile **pack_lockfiles;
496 size_t pack_lockfiles_nr;
497 size_t pack_lockfiles_alloc;
498};
499
500int odb_transaction_files_prepare(struct odb_transaction *base)
501{
502 struct odb_transaction_files *transaction =
503 container_of_or_null(base, struct odb_transaction_files, base);
504
505 /*
506 * We lazily create the temporary object directory
507 * the first time an object might be added, since
508 * callers may not know whether any objects will be
509 * added at the time they call odb_transaction_files_begin.
510 */
511 if (!transaction || transaction->objdir)
512 return 0;
513
514 transaction->objdir = tmp_objdir_create(base->source->odb->repo, transaction->prefix, 0);
515 if (!transaction->objdir)
516 return error(_("unable to create temporary object directory"));
517
518 return 0;
519}
520
521void odb_transaction_files_fsync(struct odb_transaction *base,
522 int fd, const char *filename)
523{
524 struct odb_transaction_files *transaction =
525 container_of_or_null(base, struct odb_transaction_files, base);
526
527 if (!transaction || !transaction->objdir) {
528 fsync_or_die(fd, filename);
529 return;
530 }
531
532 /*
533 * If we have an active ODB transaction, we issue a call that
534 * cleans the filesystem page cache but avoids a hardware flush
535 * command. Later on we will issue a single hardware flush
536 * before renaming the objects to their final names as part of
537 * flush_batch_fsync.
538 */
539 if (git_fsync(fd, FSYNC_WRITEOUT_ONLY) < 0) {
540 if (errno == ENOSYS)
541 warning(_("core.fsyncMethod = batch is unsupported on this platform"));
542 fsync_or_die(fd, filename);
543 }
544}
545
546/*
547 * We can't use the normal fsck_error_function() for index_mem(),
548 * because we don't yet have a valid oid for it to report. Instead,
549 * report the minimal fsck error here, and rely on the caller to
550 * give more context.
551 */
552static int hash_format_check_report(struct fsck_options *opts UNUSED,
553 void *fsck_report UNUSED,
554 enum fsck_msg_type msg_type UNUSED,
555 enum fsck_msg_id msg_id UNUSED,
556 const char *message)
557{
558 error(_("object fails fsck: %s"), message);
559 return 1;
560}
561
562static int index_mem(struct index_state *istate,
563 struct object_id *oid,
564 const void *buf, size_t size,
565 enum object_type type,
566 const char *path, unsigned flags)
567{
568 struct strbuf nbuf = STRBUF_INIT;
569 int ret = 0;
570 int write_object = flags & INDEX_WRITE_OBJECT;
571
572 if (!type)
573 type = OBJ_BLOB;
574
575 /*
576 * Convert blobs to git internal format
577 */
578 if ((type == OBJ_BLOB) && path) {
579 if (convert_to_git(istate, path, buf, size, &nbuf,
580 get_conv_flags(flags))) {
581 buf = nbuf.buf;
582 size = nbuf.len;
583 }
584 }
585 if (flags & INDEX_FORMAT_CHECK) {
586 struct fsck_options opts;
587
588 fsck_options_init(&opts, the_repository, FSCK_OPTIONS_DEFAULT);
589 opts.strict = 1;
590 opts.error_func = hash_format_check_report;
591 if (fsck_buffer(null_oid(istate->repo->hash_algo), type, buf, size, &opts))
592 die(_("refusing to create malformed object"));
593 fsck_finish(&opts);
594 }
595
596 if (write_object)
597 ret = odb_write_object(istate->repo->objects, buf, size, type, oid);
598 else
599 hash_object_file(istate->repo->hash_algo, buf, size, type, oid);
600
601 strbuf_release(&nbuf);
602 return ret;
603}
604
605static int index_stream_convert_blob(struct index_state *istate,
606 struct object_id *oid,
607 int fd,
608 const char *path,
609 unsigned flags)
610{
611 int ret = 0;
612 const int write_object = flags & INDEX_WRITE_OBJECT;
613 struct strbuf sbuf = STRBUF_INIT;
614
615 assert(path);
616 ASSERT(would_convert_to_git_filter_fd(istate, path));
617
618 convert_to_git_filter_fd(istate, path, fd, &sbuf,
619 get_conv_flags(flags));
620
621 if (write_object)
622 ret = odb_write_object(istate->repo->objects, sbuf.buf, sbuf.len, OBJ_BLOB,
623 oid);
624 else
625 hash_object_file(istate->repo->hash_algo, sbuf.buf, sbuf.len, OBJ_BLOB,
626 oid);
627 strbuf_release(&sbuf);
628 return ret;
629}
630
631static int index_pipe(struct index_state *istate, struct object_id *oid,
632 int fd, enum object_type type,
633 const char *path, unsigned flags)
634{
635 struct strbuf sbuf = STRBUF_INIT;
636 int ret;
637
638 if (strbuf_read(&sbuf, fd, 4096) >= 0)
639 ret = index_mem(istate, oid, sbuf.buf, sbuf.len, type, path, flags);
640 else
641 ret = -1;
642 strbuf_release(&sbuf);
643 return ret;
644}
645
646#define SMALL_FILE_SIZE (32*1024)
647
648static int index_core(struct index_state *istate,
649 struct object_id *oid, int fd, size_t size,
650 enum object_type type, const char *path,
651 unsigned flags)
652{
653 int ret;
654
655 if (!size) {
656 ret = index_mem(istate, oid, "", size, type, path, flags);
657 } else if (size <= SMALL_FILE_SIZE) {
658 char *buf = xmalloc(size);
659 ssize_t read_result = read_in_full(fd, buf, size);
660 if (read_result < 0)
661 ret = error_errno(_("read error while indexing %s"),
662 path ? path : "<unknown>");
663 else if ((size_t) read_result != size)
664 ret = error(_("short read while indexing %s"),
665 path ? path : "<unknown>");
666 else
667 ret = index_mem(istate, oid, buf, size, type, path, flags);
668 free(buf);
669 } else {
670 void *buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
671 ret = index_mem(istate, oid, buf, size, type, path, flags);
672 munmap(buf, size);
673 }
674 return ret;
675}
676
677static int already_written(struct odb_transaction_files *transaction,
678 struct object_id *oid)
679{
680 /* The object may already exist in the repository */
681 if (odb_has_object(transaction->base.source->odb, oid,
682 ODB_HAS_OBJECT_RECHECK_PACKED | ODB_HAS_OBJECT_FETCH_PROMISOR))
683 return 1;
684
685 /* Might want to keep the list sorted */
686 for (uint32_t i = 0; i < transaction->packfile.nr_written; i++)
687 if (oideq(&transaction->packfile.written[i]->oid, oid))
688 return 1;
689
690 /* This is a new object we need to keep */
691 return 0;
692}
693
694/* Lazily create backing packfile for the state */
695static void prepare_packfile_transaction(struct odb_transaction_files *transaction)
696{
697 struct transaction_packfile *state = &transaction->packfile;
698 if (state->f)
699 return;
700
701 state->f = create_tmp_packfile(transaction->base.source->odb->repo,
702 &state->pack_tmp_name);
703 reset_pack_idx_option(&state->pack_idx_opts);
704
705 /* Pretend we are going to write only one object */
706 state->offset = write_pack_header(state->f, 1);
707 if (!state->offset)
708 die_errno("unable to write pack header");
709}
710
711static int hash_stream(struct odb_stream *stream,
712 const struct git_hash_algo *hash_algo,
713 struct object_id *result_oid)
714{
715 unsigned char buf[16384];
716 struct git_hash_ctx ctx;
717 unsigned header_len;
718 size_t bytes_hashed = 0;
719
720 header_len = format_object_header((char *)buf, sizeof(buf),
721 stream->type, stream->size);
722 git_hash_init(&ctx, hash_algo);
723 git_hash_update(&ctx, buf, header_len);
724
725 while (1) {
726 ssize_t read_result = odb_stream_read(stream, buf,
727 sizeof(buf));
728 if (read_result < 0)
729 return -1;
730 if (!read_result)
731 break;
732
733 git_hash_update(&ctx, buf, read_result);
734 bytes_hashed += read_result;
735 }
736
737 if (bytes_hashed != stream->size)
738 return -1;
739
740 git_hash_final_oid(result_oid, &ctx);
741
742 return 0;
743}
744
745/*
746 * Read the contents from the stream provided, streaming it to the
747 * packfile in state while updating the hash in ctx.
748 */
749static void stream_to_pack(struct transaction_packfile *state,
750 struct git_hash_ctx *ctx,
751 struct odb_stream *stream)
752{
753 git_zstream s;
754 unsigned char ibuf[16384];
755 unsigned char obuf[16384];
756 unsigned hdrlen;
757 int status = Z_OK;
758 struct repo_config_values *cfg = repo_config_values(the_repository);
759 bool is_finished = false;
760 size_t bytes_read = 0;
761
762 git_deflate_init(&s, cfg->pack_compression_level);
763
764 hdrlen = encode_in_pack_object_header(obuf, sizeof(obuf), stream->type, stream->size);
765 s.next_out = obuf + hdrlen;
766 s.avail_out = sizeof(obuf) - hdrlen;
767
768 while (status != Z_STREAM_END) {
769 if (!is_finished && !s.avail_in) {
770 ssize_t rsize = odb_stream_read(stream, ibuf,
771 sizeof(ibuf));
772 if (rsize < 0)
773 die("failed to read object data");
774 if (!rsize)
775 is_finished = true;
776
777 git_hash_update(ctx, ibuf, rsize);
778
779 s.next_in = ibuf;
780 s.avail_in = rsize;
781 bytes_read += rsize;
782 }
783
784 status = git_deflate(&s, is_finished ? Z_FINISH : 0);
785
786 if (!s.avail_out || status == Z_STREAM_END) {
787 size_t written = s.next_out - obuf;
788
789 hashwrite(state->f, obuf, written);
790 state->offset += written;
791 s.next_out = obuf;
792 s.avail_out = sizeof(obuf);
793 }
794
795 switch (status) {
796 case Z_OK:
797 case Z_BUF_ERROR:
798 case Z_STREAM_END:
799 continue;
800 default:
801 die("unexpected deflate failure: %d", status);
802 }
803 }
804
805 if (bytes_read != stream->size)
806 die("read %" PRIuMAX " bytes of object data, but expected %" PRIuMAX " bytes",
807 (uintmax_t)bytes_read, (uintmax_t)stream->size);
808
809 git_deflate_end(&s);
810}
811
812static void flush_packfile_transaction(struct odb_transaction_files *transaction)
813{
814 struct transaction_packfile *state = &transaction->packfile;
815 struct repository *repo = transaction->base.source->odb->repo;
816 unsigned char hash[GIT_MAX_RAWSZ];
817 struct strbuf packname = STRBUF_INIT;
818 char *idx_tmp_name = NULL;
819
820 if (!state->f)
821 return;
822
823 if (state->nr_written == 0) {
824 close(state->f->fd);
825 free_hashfile(state->f);
826 unlink(state->pack_tmp_name);
827 goto clear_exit;
828 } else if (state->nr_written == 1) {
829 finalize_hashfile(state->f, hash, FSYNC_COMPONENT_PACK,
830 CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE);
831 } else {
832 int fd = finalize_hashfile(state->f, hash, FSYNC_COMPONENT_PACK, 0);
833 fixup_pack_header_footer(repo->hash_algo, fd, hash, state->pack_tmp_name,
834 state->nr_written, hash,
835 state->offset);
836 close(fd);
837 }
838
839 strbuf_addf(&packname, "%s/pack/pack-%s.",
840 repo_get_object_directory(transaction->base.source->odb->repo),
841 hash_to_hex_algop(hash, repo->hash_algo));
842
843 stage_tmp_packfiles(repo, &packname, state->pack_tmp_name,
844 state->written, state->nr_written, NULL,
845 &state->pack_idx_opts, hash, &idx_tmp_name);
846 rename_tmp_packfile_idx(repo, &packname, &idx_tmp_name);
847
848 for (uint32_t i = 0; i < state->nr_written; i++)
849 free(state->written[i]);
850
851clear_exit:
852 free(idx_tmp_name);
853 free(state->pack_tmp_name);
854 free(state->written);
855 memset(state, 0, sizeof(*state));
856
857 strbuf_release(&packname);
858}
859
860/*
861 * This writes the specified object to a packfile. Objects written here
862 * during the same transaction are written to the same packfile. The
863 * packfile is not flushed until the transaction is flushed. The caller
864 * is expected to ensure a valid transaction is setup for objects to be
865 * recorded to.
866 *
867 * This also bypasses the usual "convert-to-git" dance, and that is on
868 * purpose. We could write a streaming version of the converting
869 * functions and insert that before feeding the data to fast-import
870 * (or equivalent in-core API described above). However, that is
871 * somewhat complicated, as we do not know the size of the filter
872 * result, which we need to know beforehand when writing a git object.
873 * Since the primary motivation for trying to stream from the working
874 * tree file and to avoid mmaping it in core is to deal with large
875 * objects, they generally do not want to get any conversion, and
876 * callers should avoid this code path when filters are requested.
877 */
878static int odb_transaction_files_write_object_stream(struct odb_transaction *base,
879 struct odb_stream *stream,
880 struct object_id *result_oid)
881{
882 struct odb_transaction_files *transaction = container_of(base,
883 struct odb_transaction_files,
884 base);
885 struct transaction_packfile *state = &transaction->packfile;
886 struct git_hash_ctx ctx;
887 unsigned char obuf[16384];
888 unsigned header_len;
889 struct hashfile_checkpoint checkpoint;
890 struct pack_idx_entry *idx;
891
892 header_len = format_object_header((char *)obuf, sizeof(obuf),
893 stream->type, stream->size);
894 git_hash_init(&ctx, transaction->base.source->odb->repo->hash_algo);
895 git_hash_update(&ctx, obuf, header_len);
896
897 /*
898 * If writing another object to the packfile could result in it
899 * exceeding the configured size limit, flush the current packfile
900 * transaction.
901 *
902 * Note that this uses the inflated object size as an approximation.
903 * Blob objects written in this manner are not delta-compressed, so
904 * the difference between the inflated and on-disk size is limited
905 * to zlib compression and is sufficient for this check.
906 */
907 if (state->nr_written && pack_size_limit_cfg &&
908 pack_size_limit_cfg < state->offset + stream->size) {
909 flush_packfile_transaction(transaction);
910 odb_reprepare(transaction->base.source->odb);
911 }
912
913 CALLOC_ARRAY(idx, 1);
914 prepare_packfile_transaction(transaction);
915 hashfile_checkpoint_init(state->f, &checkpoint);
916
917 hashfile_checkpoint(state->f, &checkpoint);
918 idx->offset = state->offset;
919 crc32_begin(state->f);
920 stream_to_pack(state, &ctx, stream);
921 git_hash_final_oid(result_oid, &ctx);
922
923 idx->crc32 = crc32_end(state->f);
924 if (already_written(transaction, result_oid)) {
925 hashfile_truncate(state->f, &checkpoint);
926 state->offset = checkpoint.offset;
927 free(idx);
928 } else {
929 oidcpy(&idx->oid, result_oid);
930 ALLOC_GROW(state->written,
931 state->nr_written + 1,
932 state->alloc_written);
933 state->written[state->nr_written++] = idx;
934 }
935
936 hashfile_checkpoint_release(&checkpoint);
937 return 0;
938}
939
940int index_fd(struct index_state *istate, struct object_id *oid,
941 int fd, struct stat *st,
942 enum object_type type, const char *path, unsigned flags)
943{
944 int ret;
945
946 /*
947 * Call xsize_t() only when needed to avoid potentially unnecessary
948 * die() for large files.
949 */
950 if (type == OBJ_BLOB && path && would_convert_to_git_filter_fd(istate, path)) {
951 ret = index_stream_convert_blob(istate, oid, fd, path, flags);
952 } else if (!S_ISREG(st->st_mode)) {
953 ret = index_pipe(istate, oid, fd, type, path, flags);
954 } else if ((st->st_size >= 0 &&
955 (size_t)st->st_size <= repo_settings_get_big_file_threshold(istate->repo)) ||
956 type != OBJ_BLOB ||
957 (path && would_convert_to_git(istate, path))) {
958 ret = index_core(istate, oid, fd, xsize_t(st->st_size),
959 type, path, flags);
960 } else {
961 struct odb_stream *stream = odb_stream_from_fd(fd, xsize_t(st->st_size),
962 OBJ_BLOB);
963
964 if (flags & INDEX_WRITE_OBJECT) {
965 struct object_database *odb = the_repository->objects;
966 struct odb_transaction *transaction = odb->transaction;
967 int inflight = !!transaction;
968
969 if (!inflight)
970 odb_transaction_begin_or_die(odb, &transaction, 0);
971 ret = odb_transaction_write_object_stream(transaction,
972 stream, oid);
973 if (!inflight)
974 odb_transaction_commit_and_finalize_or_die(transaction);
975 } else {
976 ret = hash_stream(stream, the_repository->hash_algo, oid);
977 }
978
979 odb_stream_close(stream);
980 }
981
982 close(fd);
983 return ret;
984}
985
986int index_path(struct index_state *istate, struct object_id *oid,
987 const char *path, struct stat *st, unsigned flags)
988{
989 int fd;
990 struct strbuf sb = STRBUF_INIT;
991 int rc = 0;
992
993 switch (st->st_mode & S_IFMT) {
994 case S_IFREG:
995 fd = open(path, O_RDONLY);
996 if (fd < 0)
997 return error_errno("open(\"%s\")", path);
998 if (index_fd(istate, oid, fd, st, OBJ_BLOB, path, flags) < 0)
999 return error(_("%s: failed to insert into database"),
1000 path);
1001 break;
1002 case S_IFLNK:
1003 if (strbuf_readlink(&sb, path, st->st_size))
1004 return error_errno("readlink(\"%s\")", path);
1005 if (!(flags & INDEX_WRITE_OBJECT))
1006 hash_object_file(istate->repo->hash_algo, sb.buf, sb.len,
1007 OBJ_BLOB, oid);
1008 else if (odb_write_object(istate->repo->objects, sb.buf, sb.len, OBJ_BLOB, oid))
1009 rc = error(_("%s: failed to insert into database"), path);
1010 strbuf_release(&sb);
1011 break;
1012 case S_IFDIR:
1013 if (repo_resolve_gitlink_ref(istate->repo, path, "HEAD", oid))
1014 return error(_("'%s' does not have a commit checked out"), path);
1015 if (&hash_algos[oid->algo] != istate->repo->hash_algo)
1016 return error(_("cannot add a submodule of a different hash algorithm"));
1017 break;
1018 default:
1019 return error(_("%s: unsupported file type"), path);
1020 }
1021 return rc;
1022}
1023
1024int read_pack_header(int fd, struct pack_header *header)
1025{
1026 if (read_in_full(fd, header, sizeof(*header)) != sizeof(*header))
1027 /* "eof before pack header was fully read" */
1028 return PH_ERROR_EOF;
1029
1030 if (header->hdr_signature != htonl(PACK_SIGNATURE))
1031 /* "protocol error (pack signature mismatch detected)" */
1032 return PH_ERROR_PACK_SIGNATURE;
1033 if (!pack_version_ok(header->hdr_version))
1034 /* "protocol error (pack version unsupported)" */
1035 return PH_ERROR_PROTOCOL;
1036 return 0;
1037}
1038
1039int for_each_file_in_obj_subdir(unsigned int subdir_nr,
1040 struct strbuf *path,
1041 const struct git_hash_algo *algop,
1042 each_loose_object_fn obj_cb,
1043 each_loose_cruft_fn cruft_cb,
1044 each_loose_subdir_fn subdir_cb,
1045 void *data)
1046{
1047 size_t origlen, baselen;
1048 DIR *dir;
1049 struct dirent *de;
1050 int r = 0;
1051 struct object_id oid;
1052
1053 if (subdir_nr > 0xff)
1054 BUG("invalid loose object subdirectory: %x", subdir_nr);
1055
1056 origlen = path->len;
1057 strbuf_complete(path, '/');
1058 strbuf_addf(path, "%02x", subdir_nr);
1059
1060 dir = opendir(path->buf);
1061 if (!dir) {
1062 if (errno != ENOENT)
1063 r = error_errno(_("unable to open %s"), path->buf);
1064 strbuf_setlen(path, origlen);
1065 return r;
1066 }
1067
1068 oid.hash[0] = subdir_nr;
1069 strbuf_addch(path, '/');
1070 baselen = path->len;
1071
1072 while ((de = readdir_skip_dot_and_dotdot(dir))) {
1073 size_t namelen;
1074
1075 namelen = strlen(de->d_name);
1076 strbuf_setlen(path, baselen);
1077 strbuf_add(path, de->d_name, namelen);
1078 if (namelen == algop->hexsz - 2 &&
1079 !hex_to_bytes(oid.hash + 1, de->d_name,
1080 algop->rawsz - 1)) {
1081 oid_set_algo(&oid, algop);
1082 memset(oid.hash + algop->rawsz, 0,
1083 GIT_MAX_RAWSZ - algop->rawsz);
1084 if (obj_cb) {
1085 r = obj_cb(&oid, path->buf, data);
1086 if (r)
1087 break;
1088 }
1089 continue;
1090 }
1091
1092 if (cruft_cb) {
1093 r = cruft_cb(de->d_name, path->buf, data);
1094 if (r)
1095 break;
1096 }
1097 }
1098 closedir(dir);
1099
1100 strbuf_setlen(path, baselen - 1);
1101 if (!r && subdir_cb)
1102 r = subdir_cb(subdir_nr, path->buf, data);
1103
1104 strbuf_setlen(path, origlen);
1105
1106 return r;
1107}
1108
1109int for_each_loose_file_in_source(struct odb_source *source,
1110 each_loose_object_fn obj_cb,
1111 each_loose_cruft_fn cruft_cb,
1112 each_loose_subdir_fn subdir_cb,
1113 void *data)
1114{
1115 struct strbuf buf = STRBUF_INIT;
1116 int r;
1117
1118 strbuf_addstr(&buf, source->path);
1119 for (int i = 0; i < 256; i++) {
1120 r = for_each_file_in_obj_subdir(i, &buf, source->odb->repo->hash_algo,
1121 obj_cb, cruft_cb, subdir_cb, data);
1122 if (r)
1123 break;
1124 }
1125
1126 strbuf_release(&buf);
1127 return r;
1128}
1129
1130static int check_stream_oid(git_zstream *stream,
1131 const char *hdr,
1132 unsigned long size,
1133 const char *path,
1134 const struct object_id *expected_oid,
1135 const struct git_hash_algo *algop)
1136{
1137 struct git_hash_ctx c;
1138 struct object_id real_oid;
1139 unsigned char buf[4096];
1140 unsigned long total_read;
1141 int status = Z_OK;
1142
1143 git_hash_init(&c, algop);
1144 git_hash_update(&c, hdr, stream->total_out);
1145
1146 /*
1147 * We already read some bytes into hdr, but the ones up to the NUL
1148 * do not count against the object's content size.
1149 */
1150 total_read = stream->total_out - strlen(hdr) - 1;
1151
1152 /*
1153 * This size comparison must be "<=" to read the final zlib packets;
1154 * see the comment in unpack_loose_rest for details.
1155 */
1156 while (total_read <= size &&
1157 (status == Z_OK ||
1158 (status == Z_BUF_ERROR && !stream->avail_out))) {
1159 stream->next_out = buf;
1160 stream->avail_out = sizeof(buf);
1161 if (size - total_read < stream->avail_out)
1162 stream->avail_out = size - total_read;
1163 status = git_inflate(stream, Z_FINISH);
1164 git_hash_update(&c, buf, stream->next_out - buf);
1165 total_read += stream->next_out - buf;
1166 }
1167
1168 if (status != Z_STREAM_END) {
1169 error(_("corrupt loose object '%s'"), oid_to_hex(expected_oid));
1170 git_hash_discard(&c);
1171 return -1;
1172 }
1173 if (stream->avail_in) {
1174 error(_("garbage at end of loose object '%s'"),
1175 oid_to_hex(expected_oid));
1176 git_hash_discard(&c);
1177 return -1;
1178 }
1179
1180 git_hash_final_oid(&real_oid, &c);
1181 if (!oideq(expected_oid, &real_oid)) {
1182 error(_("hash mismatch for %s (expected %s)"), path,
1183 oid_to_hex(expected_oid));
1184 return -1;
1185 }
1186
1187 return 0;
1188}
1189
1190int read_loose_object(struct repository *repo,
1191 const char *path,
1192 const struct object_id *expected_oid,
1193 struct object_id *real_oid,
1194 void **contents,
1195 struct object_info *oi)
1196{
1197 int ret = -1;
1198 int fd;
1199 void *map = NULL;
1200 unsigned long mapsize;
1201 git_zstream stream;
1202 char hdr[MAX_HEADER_LEN];
1203 size_t *size = oi->sizep;
1204
1205 fd = git_open(path);
1206 if (fd >= 0)
1207 map = map_fd(fd, path, &mapsize);
1208 if (!map) {
1209 error_errno(_("unable to mmap %s"), path);
1210 goto out;
1211 }
1212
1213 if (unpack_loose_header(&stream, map, mapsize, hdr, sizeof(hdr)) != ULHR_OK) {
1214 error(_("unable to unpack header of %s"), path);
1215 goto out_inflate;
1216 }
1217
1218 if (parse_loose_header(hdr, oi) < 0) {
1219 error(_("unable to parse header of %s"), path);
1220 goto out_inflate;
1221 }
1222
1223 if (*oi->typep < 0) {
1224 error(_("unable to parse type from header '%s' of %s"),
1225 hdr, path);
1226 goto out_inflate;
1227 }
1228
1229 if (*oi->typep == OBJ_BLOB &&
1230 *size > repo_settings_get_big_file_threshold(repo)) {
1231 if (check_stream_oid(&stream, hdr, *size, path, expected_oid,
1232 repo->hash_algo) < 0)
1233 goto out_inflate;
1234 } else {
1235 *contents = unpack_loose_rest(&stream, hdr, *size, expected_oid);
1236 if (!*contents) {
1237 error(_("unable to unpack contents of %s"), path);
1238 goto out_inflate;
1239 }
1240 hash_object_file(repo->hash_algo,
1241 *contents, *size,
1242 *oi->typep, real_oid);
1243 if (!oideq(expected_oid, real_oid))
1244 goto out_inflate;
1245 }
1246
1247 ret = 0; /* everything checks out */
1248
1249out_inflate:
1250 git_inflate_end(&stream);
1251out:
1252 if (map)
1253 munmap(map, mapsize);
1254 return ret;
1255}
1256
1257static int write_pack_lockfile(struct odb_source *source, const char *pack_name,
1258 const char *keep_msg, struct tempfile **out)
1259{
1260 struct strbuf lock_path = STRBUF_INIT;
1261 struct strbuf contents = STRBUF_INIT;
1262 int ret;
1263 int fd;
1264
1265 strbuf_addf(&lock_path, "%s/pack/pack-%s.keep",
1266 source->path, pack_name);
1267 strbuf_addf(&contents, "%s\n", keep_msg);
1268
1269 fd = safe_create_file_with_leading_directories(source->odb->repo,
1270 lock_path.buf);
1271 if (fd < 0) {
1272 ret = error_errno(_("unable to create '%s'"), lock_path.buf);
1273 goto out;
1274 }
1275
1276 if (write_in_full(fd, contents.buf, contents.len) < 0) {
1277 ret = error_errno(_("unable to write '%s'"), lock_path.buf);
1278 close(fd);
1279 unlink(lock_path.buf);
1280 goto out;
1281 }
1282
1283 if (close(fd) < 0) {
1284 ret = error_errno(_("unable to close '%s'"), lock_path.buf);
1285 unlink(lock_path.buf);
1286 goto out;
1287 }
1288
1289 *out = register_tempfile(lock_path.buf);
1290 ret = 0;
1291
1292out:
1293 strbuf_release(&lock_path);
1294 strbuf_release(&contents);
1295
1296 return ret;
1297}
1298
1299static int write_pack_lockfiles(struct odb_transaction_files *transaction)
1300{
1301 struct strbuf keep_msg = STRBUF_INIT;
1302 char hostname[HOST_NAME_MAX + 1];
1303 int ret = 0;
1304
1305 if (!transaction->pack_names.nr)
1306 return 0;
1307
1308 if (xgethostname(hostname, sizeof(hostname)))
1309 xsnprintf(hostname, sizeof(hostname), "localhost");
1310 strbuf_addf(&keep_msg, "receive-pack %"PRIuMAX" on %s",
1311 (uintmax_t)getpid(), hostname);
1312
1313 for (size_t i = 0; i < transaction->pack_names.nr; i++) {
1314 struct tempfile *lock;
1315
1316 ret = write_pack_lockfile(transaction->base.source,
1317 transaction->pack_names.v[i],
1318 keep_msg.buf, &lock);
1319 if (ret < 0)
1320 break;
1321
1322 ALLOC_GROW(transaction->pack_lockfiles,
1323 transaction->pack_lockfiles_nr + 1,
1324 transaction->pack_lockfiles_alloc);
1325 transaction->pack_lockfiles[transaction->pack_lockfiles_nr++] = lock;
1326 }
1327
1328 strbuf_release(&keep_msg);
1329
1330 return ret;
1331}
1332
1333static int odb_transaction_files_commit(struct odb_transaction *base)
1334{
1335 struct odb_transaction_files *transaction =
1336 container_of(base, struct odb_transaction_files, base);
1337 int have_packfile = !!transaction->packfile.f;
1338
1339 flush_packfile_transaction(transaction);
1340
1341 if (transaction->objdir) {
1342 struct strbuf temp_path = STRBUF_INIT;
1343 struct tempfile *temp;
1344
1345 /*
1346 * Issue a full hardware flush against a temporary file to ensure
1347 * that all objects are durable before any renames occur. The code in
1348 * odb_transaction_files_fsync has already issued a writeout
1349 * request, but it has not flushed any writeback cache in the storage
1350 * hardware or any filesystem logs. This fsync call acts as a barrier
1351 * to ensure that the data in each new object file is durable before
1352 * the final name is visible.
1353 */
1354 strbuf_addf(&temp_path, "%s/bulk_fsync_XXXXXX",
1355 repo_get_object_directory(transaction->base.source->odb->repo));
1356 temp = xmks_tempfile(temp_path.buf);
1357 fsync_or_die(get_tempfile_fd(temp), get_tempfile_path(temp));
1358 delete_tempfile(&temp);
1359 strbuf_release(&temp_path);
1360
1361 if (write_pack_lockfiles(transaction))
1362 return error(_("unable to lock packfiles"));
1363
1364 /*
1365 * Make the object files visible in the primary ODB after their data is
1366 * fully durable.
1367 */
1368 if (tmp_objdir_migrate(transaction->objdir))
1369 return error(_("unable to migrate temporary objects"));
1370
1371 transaction->objdir = NULL;
1372 }
1373
1374 if (have_packfile)
1375 odb_reprepare(transaction->base.source->odb);
1376
1377 return 0;
1378}
1379
1380static const char *parse_pack_header(struct pack_header *hdr, int pack_fd)
1381{
1382 switch (read_pack_header(pack_fd, hdr)) {
1383 case PH_ERROR_EOF:
1384 return "eof before pack header was fully read";
1385
1386 case PH_ERROR_PACK_SIGNATURE:
1387 return "protocol error (pack signature mismatch detected)";
1388
1389 case PH_ERROR_PROTOCOL:
1390 return "protocol error (pack version unsupported)";
1391
1392 default:
1393 return "unknown error in parse_pack_header";
1394
1395 case 0:
1396 return NULL;
1397 }
1398}
1399
1400static void push_header_arg(struct strvec *args, struct pack_header *hdr)
1401{
1402 strvec_pushf(args, "--pack_header=%"PRIu32",%"PRIu32,
1403 ntohl(hdr->hdr_version), ntohl(hdr->hdr_entries));
1404}
1405
1406static unsigned int get_unpack_limit(struct repository *repo,
1407 enum odb_transaction_flags flags)
1408{
1409 unsigned int limit = 0;
1410
1411 if (flags & ODB_TRANSACTION_RECEIVE) {
1412 limit = 100;
1413 repo_config_get_uint(repo, "transfer.unpacklimit", &limit);
1414 repo_config_get_uint(repo, "receive.unpacklimit", &limit);
1415 }
1416
1417 return limit;
1418}
1419
1420static int odb_transaction_files_write_pack(struct odb_transaction *base,
1421 int pack_fd, struct strbuf *err_msg,
1422 const struct odb_transaction_write_pack_opts *opts)
1423{
1424 struct odb_transaction_files *transaction =
1425 container_of(base, struct odb_transaction_files, base);
1426 struct repository *repo = base->source->odb->repo;
1427 struct child_process child = CHILD_PROCESS_INIT;
1428 struct strbuf pack_name = STRBUF_INIT;
1429 struct pack_header hdr;
1430 const char *hdr_err;
1431 int err_fd = opts->err_fd;
1432 int ret = -1;
1433 int status;
1434
1435 hdr_err = parse_pack_header(&hdr, pack_fd);
1436 if (hdr_err) {
1437 if (err_fd > 0)
1438 close(err_fd);
1439 strbuf_addstr(err_msg, hdr_err);
1440 return -1;
1441 }
1442
1443 if (opts->shallow_file) {
1444 strvec_push(&child.args, "--shallow-file");
1445 strvec_push(&child.args, opts->shallow_file);
1446 }
1447
1448 odb_transaction_env(base, &child.env);
1449
1450 if (ntohl(hdr.hdr_entries) < get_unpack_limit(repo, transaction->flags)) {
1451 strvec_push(&child.args, "unpack-objects");
1452 push_header_arg(&child.args, &hdr);
1453 if (opts->quiet)
1454 strvec_push(&child.args, "-q");
1455 if (opts->fsck_objects)
1456 strvec_pushf(&child.args, "--strict%s",
1457 opts->fsck_msg_types);
1458 if (opts->max_input_size)
1459 strvec_pushf(&child.args, "--max-input-size=%"PRIuMAX,
1460 (uintmax_t)opts->max_input_size);
1461 child.no_stdout = 1;
1462 child.in = pack_fd;
1463 child.err = err_fd;
1464 child.git_cmd = 1;
1465 status = run_command(&child);
1466 if (status) {
1467 strbuf_addstr(err_msg, "unpack-objects abnormal exit");
1468 return -1;
1469 }
1470 } else {
1471 strvec_pushl(&child.args, "index-pack", "--stdin", NULL);
1472 push_header_arg(&child.args, &hdr);
1473
1474 if (!opts->quiet && err_fd)
1475 strvec_push(&child.args, "--show-resolving-progress");
1476 if (err_fd)
1477 strvec_push(&child.args, "--report-end-of-input");
1478 if (opts->fsck_objects)
1479 strvec_pushf(&child.args, "--strict%s",
1480 opts->fsck_msg_types);
1481 if (!opts->reject_thin)
1482 strvec_push(&child.args, "--fix-thin");
1483 if (opts->max_input_size)
1484 strvec_pushf(&child.args, "--max-input-size=%"PRIuMAX,
1485 (uintmax_t)opts->max_input_size);
1486 child.out = -1;
1487 child.in = pack_fd;
1488 child.err = err_fd;
1489 child.git_cmd = 1;
1490 status = start_command(&child);
1491 if (status) {
1492 strbuf_addstr(err_msg, "index-pack fork failed");
1493 return -1;
1494 }
1495
1496 ret = read_index_pack_report(repo, child.out, &pack_name, NULL);
1497
1498 close(child.out);
1499
1500 status = finish_command(&child);
1501 if (status) {
1502 strbuf_addstr(err_msg, "index-pack abnormal exit");
1503 goto out;
1504 }
1505
1506 if (ret) {
1507 strbuf_addstr(err_msg, "index-pack failed to report pack name");
1508 goto out;
1509 }
1510
1511 strvec_push(&transaction->pack_names, pack_name.buf);
1512
1513 odb_source_prepare(base->source, ODB_PREPARE_FLUSH_CACHES);
1514 }
1515
1516 ret = 0;
1517
1518out:
1519 strbuf_release(&pack_name);
1520
1521 return ret;
1522}
1523
1524static int odb_transaction_files_finalize(struct odb_transaction *base)
1525{
1526 struct odb_transaction_files *transaction =
1527 container_of(base, struct odb_transaction_files, base);
1528 int ret = 0;
1529
1530 for (size_t i = 0; i < transaction->pack_lockfiles_nr; i++)
1531 ret |= delete_tempfile(&transaction->pack_lockfiles[i]);
1532
1533 free(transaction->pack_lockfiles);
1534 strvec_clear(&transaction->pack_names);
1535
1536 return ret;
1537}
1538
1539static int odb_transaction_files_env(struct odb_transaction *base,
1540 struct strvec *env)
1541{
1542 struct odb_transaction_files *transaction =
1543 container_of(base, struct odb_transaction_files, base);
1544 int ret;
1545
1546 ret = odb_transaction_files_prepare(&transaction->base);
1547 if (!ret)
1548 strvec_pushv(env, tmp_objdir_env(transaction->objdir));
1549
1550 return ret;
1551}
1552
1553int odb_transaction_files_begin(struct odb_source *source,
1554 struct odb_transaction **out,
1555 enum odb_transaction_flags flags)
1556{
1557 struct odb_transaction_files *transaction;
1558
1559 transaction = xcalloc(1, sizeof(*transaction));
1560 transaction->base.source = source;
1561 transaction->base.commit = odb_transaction_files_commit;
1562 transaction->base.finalize = odb_transaction_files_finalize;
1563 transaction->base.write_object_stream = odb_transaction_files_write_object_stream;
1564 transaction->base.write_pack = odb_transaction_files_write_pack;
1565 transaction->base.env = odb_transaction_files_env;
1566 transaction->flags = flags;
1567 strvec_init(&transaction->pack_names);
1568
1569 transaction->prefix = "bulk-fsync";
1570 if (flags & ODB_TRANSACTION_RECEIVE) {
1571 /*
1572 * ODB transactions for git-receive-pack(1) eagerly create a
1573 * temporary directory and use a different temporary directory
1574 * prefix.
1575 *
1576 * NEEDSWORK: This transaction flag is only used by the "files"
1577 * backend to special case temporary directory set up and
1578 * handling. Ideally transaction users should not have to care
1579 * though. To avoid this, we could eagerly create the temporary
1580 * directory and use the same prefix name for all transactions.
1581 */
1582 transaction->prefix = "incoming";
1583 if (odb_transaction_files_prepare(&transaction->base)) {
1584 free(transaction);
1585 return -1;
1586 }
1587 }
1588
1589 *out = &transaction->base;
1590
1591 return 0;
1592}