at commit a0189536

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
36 static 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
46 static 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
60 const 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. */
72 static 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 */
92 int 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
102 int 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
113 int 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
126 int 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 */
158 static 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
177 enum 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
211 void *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 */
271 int 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
329 static 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
375 out:
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 */
386 int 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
392 int 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
400 retry:
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
453 out:
454 if (adjust_shared_perm(repo, filename))
455 return error(_("unable to set permission to '%s'"), filename);
456 return 0;
457 }
458
459 void 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
475 struct 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
486 struct odb_transaction_files {
487 struct odb_transaction base;
488 enum odb_transaction_flags flags;
489
490 struct tmp_objdir *objdir;
491 struct odb_source *quarantine;
492 struct transaction_packfile packfile;
493 const char *prefix;
494
495 struct tempfile **pack_lockfiles;
496 size_t pack_lockfiles_nr;
497 size_t pack_lockfiles_alloc;
498 };
499
500 int 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);
515 if (!transaction->objdir)
516 return error(_("unable to create temporary object directory"));
517
518 transaction->quarantine = tmp_objdir_replace_primary_odb(transaction->objdir, 0);
519
520 return 0;
521 }
522
523 void odb_transaction_files_fsync(struct odb_transaction *base,
524 int fd, const char *filename)
525 {
526 struct odb_transaction_files *transaction =
527 container_of_or_null(base, struct odb_transaction_files, base);
528
529 if (!transaction || !transaction->objdir) {
530 fsync_or_die(fd, filename);
531 return;
532 }
533
534 /*
535 * If we have an active ODB transaction, we issue a call that
536 * cleans the filesystem page cache but avoids a hardware flush
537 * command. Later on we will issue a single hardware flush
538 * before renaming the objects to their final names as part of
539 * flush_batch_fsync.
540 */
541 if (git_fsync(fd, FSYNC_WRITEOUT_ONLY) < 0) {
542 if (errno == ENOSYS)
543 warning(_("core.fsyncMethod = batch is unsupported on this platform"));
544 fsync_or_die(fd, filename);
545 }
546 }
547
548 /*
549 * We can't use the normal fsck_error_function() for index_mem(),
550 * because we don't yet have a valid oid for it to report. Instead,
551 * report the minimal fsck error here, and rely on the caller to
552 * give more context.
553 */
554 static int hash_format_check_report(struct fsck_options *opts UNUSED,
555 void *fsck_report UNUSED,
556 enum fsck_msg_type msg_type UNUSED,
557 enum fsck_msg_id msg_id UNUSED,
558 const char *message)
559 {
560 error(_("object fails fsck: %s"), message);
561 return 1;
562 }
563
564 static int index_mem(struct index_state *istate,
565 struct object_id *oid,
566 const void *buf, size_t size,
567 enum object_type type,
568 const char *path, unsigned flags)
569 {
570 struct strbuf nbuf = STRBUF_INIT;
571 int ret = 0;
572 int write_object = flags & INDEX_WRITE_OBJECT;
573
574 if (!type)
575 type = OBJ_BLOB;
576
577 /*
578 * Convert blobs to git internal format
579 */
580 if ((type == OBJ_BLOB) && path) {
581 if (convert_to_git(istate, path, buf, size, &nbuf,
582 get_conv_flags(flags))) {
583 buf = nbuf.buf;
584 size = nbuf.len;
585 }
586 }
587 if (flags & INDEX_FORMAT_CHECK) {
588 struct fsck_options opts;
589
590 fsck_options_init(&opts, the_repository, FSCK_OPTIONS_DEFAULT);
591 opts.strict = 1;
592 opts.error_func = hash_format_check_report;
593 if (fsck_buffer(null_oid(istate->repo->hash_algo), type, buf, size, &opts))
594 die(_("refusing to create malformed object"));
595 fsck_finish(&opts);
596 }
597
598 if (write_object)
599 ret = odb_write_object(istate->repo->objects, buf, size, type, oid);
600 else
601 hash_object_file(istate->repo->hash_algo, buf, size, type, oid);
602
603 strbuf_release(&nbuf);
604 return ret;
605 }
606
607 static int index_stream_convert_blob(struct index_state *istate,
608 struct object_id *oid,
609 int fd,
610 const char *path,
611 unsigned flags)
612 {
613 int ret = 0;
614 const int write_object = flags & INDEX_WRITE_OBJECT;
615 struct strbuf sbuf = STRBUF_INIT;
616
617 assert(path);
618 ASSERT(would_convert_to_git_filter_fd(istate, path));
619
620 convert_to_git_filter_fd(istate, path, fd, &sbuf,
621 get_conv_flags(flags));
622
623 if (write_object)
624 ret = odb_write_object(istate->repo->objects, sbuf.buf, sbuf.len, OBJ_BLOB,
625 oid);
626 else
627 hash_object_file(istate->repo->hash_algo, sbuf.buf, sbuf.len, OBJ_BLOB,
628 oid);
629 strbuf_release(&sbuf);
630 return ret;
631 }
632
633 static int index_pipe(struct index_state *istate, struct object_id *oid,
634 int fd, enum object_type type,
635 const char *path, unsigned flags)
636 {
637 struct strbuf sbuf = STRBUF_INIT;
638 int ret;
639
640 if (strbuf_read(&sbuf, fd, 4096) >= 0)
641 ret = index_mem(istate, oid, sbuf.buf, sbuf.len, type, path, flags);
642 else
643 ret = -1;
644 strbuf_release(&sbuf);
645 return ret;
646 }
647
648 #define SMALL_FILE_SIZE (32*1024)
649
650 static int index_core(struct index_state *istate,
651 struct object_id *oid, int fd, size_t size,
652 enum object_type type, const char *path,
653 unsigned flags)
654 {
655 int ret;
656
657 if (!size) {
658 ret = index_mem(istate, oid, "", size, type, path, flags);
659 } else if (size <= SMALL_FILE_SIZE) {
660 char *buf = xmalloc(size);
661 ssize_t read_result = read_in_full(fd, buf, size);
662 if (read_result < 0)
663 ret = error_errno(_("read error while indexing %s"),
664 path ? path : "<unknown>");
665 else if ((size_t) read_result != size)
666 ret = error(_("short read while indexing %s"),
667 path ? path : "<unknown>");
668 else
669 ret = index_mem(istate, oid, buf, size, type, path, flags);
670 free(buf);
671 } else {
672 void *buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
673 ret = index_mem(istate, oid, buf, size, type, path, flags);
674 munmap(buf, size);
675 }
676 return ret;
677 }
678
679 static int already_written(struct odb_transaction_files *transaction,
680 struct object_id *oid)
681 {
682 /* The object may already exist in the repository */
683 if (odb_has_object(transaction->base.source->odb, oid,
684 ODB_HAS_OBJECT_RECHECK_PACKED | ODB_HAS_OBJECT_FETCH_PROMISOR))
685 return 1;
686
687 /* Might want to keep the list sorted */
688 for (uint32_t i = 0; i < transaction->packfile.nr_written; i++)
689 if (oideq(&transaction->packfile.written[i]->oid, oid))
690 return 1;
691
692 /* This is a new object we need to keep */
693 return 0;
694 }
695
696 /* Lazily create backing packfile for the state */
697 static void prepare_packfile_transaction(struct odb_transaction_files *transaction)
698 {
699 struct transaction_packfile *state = &transaction->packfile;
700 if (state->f)
701 return;
702
703 state->f = create_tmp_packfile(transaction->base.source->odb->repo,
704 &state->pack_tmp_name);
705 reset_pack_idx_option(&state->pack_idx_opts);
706
707 /* Pretend we are going to write only one object */
708 state->offset = write_pack_header(state->f, 1);
709 if (!state->offset)
710 die_errno("unable to write pack header");
711 }
712
713 static int hash_stream(struct odb_stream *stream,
714 const struct git_hash_algo *hash_algo,
715 struct object_id *result_oid)
716 {
717 unsigned char buf[16384];
718 struct git_hash_ctx ctx;
719 unsigned header_len;
720 size_t bytes_hashed = 0;
721
722 header_len = format_object_header((char *)buf, sizeof(buf),
723 stream->type, stream->size);
724 git_hash_init(&ctx, hash_algo);
725 git_hash_update(&ctx, buf, header_len);
726
727 while (1) {
728 ssize_t read_result = odb_stream_read(stream, buf,
729 sizeof(buf));
730 if (read_result < 0)
731 return -1;
732 if (!read_result)
733 break;
734
735 git_hash_update(&ctx, buf, read_result);
736 bytes_hashed += read_result;
737 }
738
739 if (bytes_hashed != stream->size)
740 return -1;
741
742 git_hash_final_oid(result_oid, &ctx);
743
744 return 0;
745 }
746
747 /*
748 * Read the contents from the stream provided, streaming it to the
749 * packfile in state while updating the hash in ctx.
750 */
751 static void stream_to_pack(struct transaction_packfile *state,
752 struct git_hash_ctx *ctx,
753 struct odb_stream *stream)
754 {
755 git_zstream s;
756 unsigned char ibuf[16384];
757 unsigned char obuf[16384];
758 unsigned hdrlen;
759 int status = Z_OK;
760 struct repo_config_values *cfg = repo_config_values(the_repository);
761 bool is_finished = false;
762 size_t bytes_read = 0;
763
764 git_deflate_init(&s, cfg->pack_compression_level);
765
766 hdrlen = encode_in_pack_object_header(obuf, sizeof(obuf), stream->type, stream->size);
767 s.next_out = obuf + hdrlen;
768 s.avail_out = sizeof(obuf) - hdrlen;
769
770 while (status != Z_STREAM_END) {
771 if (!is_finished && !s.avail_in) {
772 ssize_t rsize = odb_stream_read(stream, ibuf,
773 sizeof(ibuf));
774 if (rsize < 0)
775 die("failed to read object data");
776 if (!rsize)
777 is_finished = true;
778
779 git_hash_update(ctx, ibuf, rsize);
780
781 s.next_in = ibuf;
782 s.avail_in = rsize;
783 bytes_read += rsize;
784 }
785
786 status = git_deflate(&s, is_finished ? Z_FINISH : 0);
787
788 if (!s.avail_out || status == Z_STREAM_END) {
789 size_t written = s.next_out - obuf;
790
791 hashwrite(state->f, obuf, written);
792 state->offset += written;
793 s.next_out = obuf;
794 s.avail_out = sizeof(obuf);
795 }
796
797 switch (status) {
798 case Z_OK:
799 case Z_BUF_ERROR:
800 case Z_STREAM_END:
801 continue;
802 default:
803 die("unexpected deflate failure: %d", status);
804 }
805 }
806
807 if (bytes_read != stream->size)
808 die("read %" PRIuMAX " bytes of object data, but expected %" PRIuMAX " bytes",
809 (uintmax_t)bytes_read, (uintmax_t)stream->size);
810
811 git_deflate_end(&s);
812 }
813
814 static void flush_packfile_transaction(struct odb_transaction_files *transaction)
815 {
816 struct transaction_packfile *state = &transaction->packfile;
817 struct repository *repo = transaction->base.source->odb->repo;
818 unsigned char hash[GIT_MAX_RAWSZ];
819 struct strbuf packname = STRBUF_INIT;
820 char *idx_tmp_name = NULL;
821
822 if (!state->f)
823 return;
824
825 if (state->nr_written == 0) {
826 close(state->f->fd);
827 free_hashfile(state->f);
828 unlink(state->pack_tmp_name);
829 goto clear_exit;
830 } else if (state->nr_written == 1) {
831 finalize_hashfile(state->f, hash, FSYNC_COMPONENT_PACK,
832 CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE);
833 } else {
834 int fd = finalize_hashfile(state->f, hash, FSYNC_COMPONENT_PACK, 0);
835 fixup_pack_header_footer(repo->hash_algo, fd, hash, state->pack_tmp_name,
836 state->nr_written, hash,
837 state->offset);
838 close(fd);
839 }
840
841 strbuf_addf(&packname, "%s/pack/pack-%s.",
842 repo_get_object_directory(transaction->base.source->odb->repo),
843 hash_to_hex_algop(hash, repo->hash_algo));
844
845 stage_tmp_packfiles(repo, &packname, state->pack_tmp_name,
846 state->written, state->nr_written, NULL,
847 &state->pack_idx_opts, hash, &idx_tmp_name);
848 rename_tmp_packfile_idx(repo, &packname, &idx_tmp_name);
849
850 for (uint32_t i = 0; i < state->nr_written; i++)
851 free(state->written[i]);
852
853 clear_exit:
854 free(idx_tmp_name);
855 free(state->pack_tmp_name);
856 free(state->written);
857 memset(state, 0, sizeof(*state));
858
859 strbuf_release(&packname);
860 /* Make objects we just wrote available to ourselves */
861 odb_reprepare(repo->objects);
862 }
863
864 /*
865 * This writes the specified object to a packfile. Objects written here
866 * during the same transaction are written to the same packfile. The
867 * packfile is not flushed until the transaction is flushed. The caller
868 * is expected to ensure a valid transaction is setup for objects to be
869 * recorded to.
870 *
871 * This also bypasses the usual "convert-to-git" dance, and that is on
872 * purpose. We could write a streaming version of the converting
873 * functions and insert that before feeding the data to fast-import
874 * (or equivalent in-core API described above). However, that is
875 * somewhat complicated, as we do not know the size of the filter
876 * result, which we need to know beforehand when writing a git object.
877 * Since the primary motivation for trying to stream from the working
878 * tree file and to avoid mmaping it in core is to deal with large
879 * objects, they generally do not want to get any conversion, and
880 * callers should avoid this code path when filters are requested.
881 */
882 static int odb_transaction_files_write_object_stream(struct odb_transaction *base,
883 struct odb_stream *stream,
884 struct object_id *result_oid)
885 {
886 struct odb_transaction_files *transaction = container_of(base,
887 struct odb_transaction_files,
888 base);
889 struct transaction_packfile *state = &transaction->packfile;
890 struct git_hash_ctx ctx;
891 unsigned char obuf[16384];
892 unsigned header_len;
893 struct hashfile_checkpoint checkpoint;
894 struct pack_idx_entry *idx;
895
896 header_len = format_object_header((char *)obuf, sizeof(obuf),
897 stream->type, stream->size);
898 git_hash_init(&ctx, transaction->base.source->odb->repo->hash_algo);
899 git_hash_update(&ctx, obuf, header_len);
900
901 /*
902 * If writing another object to the packfile could result in it
903 * exceeding the configured size limit, flush the current packfile
904 * transaction.
905 *
906 * Note that this uses the inflated object size as an approximation.
907 * Blob objects written in this manner are not delta-compressed, so
908 * the difference between the inflated and on-disk size is limited
909 * to zlib compression and is sufficient for this check.
910 */
911 if (state->nr_written && pack_size_limit_cfg &&
912 pack_size_limit_cfg < state->offset + stream->size)
913 flush_packfile_transaction(transaction);
914
915 CALLOC_ARRAY(idx, 1);
916 prepare_packfile_transaction(transaction);
917 hashfile_checkpoint_init(state->f, &checkpoint);
918
919 hashfile_checkpoint(state->f, &checkpoint);
920 idx->offset = state->offset;
921 crc32_begin(state->f);
922 stream_to_pack(state, &ctx, stream);
923 git_hash_final_oid(result_oid, &ctx);
924
925 idx->crc32 = crc32_end(state->f);
926 if (already_written(transaction, result_oid)) {
927 hashfile_truncate(state->f, &checkpoint);
928 state->offset = checkpoint.offset;
929 free(idx);
930 } else {
931 oidcpy(&idx->oid, result_oid);
932 ALLOC_GROW(state->written,
933 state->nr_written + 1,
934 state->alloc_written);
935 state->written[state->nr_written++] = idx;
936 }
937
938 hashfile_checkpoint_release(&checkpoint);
939 return 0;
940 }
941
942 int index_fd(struct index_state *istate, struct object_id *oid,
943 int fd, struct stat *st,
944 enum object_type type, const char *path, unsigned flags)
945 {
946 int ret;
947
948 /*
949 * Call xsize_t() only when needed to avoid potentially unnecessary
950 * die() for large files.
951 */
952 if (type == OBJ_BLOB && path && would_convert_to_git_filter_fd(istate, path)) {
953 ret = index_stream_convert_blob(istate, oid, fd, path, flags);
954 } else if (!S_ISREG(st->st_mode)) {
955 ret = index_pipe(istate, oid, fd, type, path, flags);
956 } else if ((st->st_size >= 0 &&
957 (size_t)st->st_size <= repo_settings_get_big_file_threshold(istate->repo)) ||
958 type != OBJ_BLOB ||
959 (path && would_convert_to_git(istate, path))) {
960 ret = index_core(istate, oid, fd, xsize_t(st->st_size),
961 type, path, flags);
962 } else {
963 struct odb_stream *stream = odb_stream_from_fd(fd, xsize_t(st->st_size),
964 OBJ_BLOB);
965
966 if (flags & INDEX_WRITE_OBJECT) {
967 struct object_database *odb = the_repository->objects;
968 struct odb_transaction *transaction = odb->transaction;
969 int inflight = !!transaction;
970
971 if (!inflight)
972 odb_transaction_begin_or_die(odb, &transaction, 0);
973 ret = odb_transaction_write_object_stream(transaction,
974 stream, oid);
975 if (!inflight)
976 odb_transaction_commit_and_finalize_or_die(transaction);
977 } else {
978 ret = hash_stream(stream, the_repository->hash_algo, oid);
979 }
980
981 odb_stream_close(stream);
982 }
983
984 close(fd);
985 return ret;
986 }
987
988 int index_path(struct index_state *istate, struct object_id *oid,
989 const char *path, struct stat *st, unsigned flags)
990 {
991 int fd;
992 struct strbuf sb = STRBUF_INIT;
993 int rc = 0;
994
995 switch (st->st_mode & S_IFMT) {
996 case S_IFREG:
997 fd = open(path, O_RDONLY);
998 if (fd < 0)
999 return error_errno("open(\"%s\")", path);
1000 if (index_fd(istate, oid, fd, st, OBJ_BLOB, path, flags) < 0)
1001 return error(_("%s: failed to insert into database"),
1002 path);
1003 break;
1004 case S_IFLNK:
1005 if (strbuf_readlink(&sb, path, st->st_size))
1006 return error_errno("readlink(\"%s\")", path);
1007 if (!(flags & INDEX_WRITE_OBJECT))
1008 hash_object_file(istate->repo->hash_algo, sb.buf, sb.len,
1009 OBJ_BLOB, oid);
1010 else if (odb_write_object(istate->repo->objects, sb.buf, sb.len, OBJ_BLOB, oid))
1011 rc = error(_("%s: failed to insert into database"), path);
1012 strbuf_release(&sb);
1013 break;
1014 case S_IFDIR:
1015 if (repo_resolve_gitlink_ref(istate->repo, path, "HEAD", oid))
1016 return error(_("'%s' does not have a commit checked out"), path);
1017 if (&hash_algos[oid->algo] != istate->repo->hash_algo)
1018 return error(_("cannot add a submodule of a different hash algorithm"));
1019 break;
1020 default:
1021 return error(_("%s: unsupported file type"), path);
1022 }
1023 return rc;
1024 }
1025
1026 int read_pack_header(int fd, struct pack_header *header)
1027 {
1028 if (read_in_full(fd, header, sizeof(*header)) != sizeof(*header))
1029 /* "eof before pack header was fully read" */
1030 return PH_ERROR_EOF;
1031
1032 if (header->hdr_signature != htonl(PACK_SIGNATURE))
1033 /* "protocol error (pack signature mismatch detected)" */
1034 return PH_ERROR_PACK_SIGNATURE;
1035 if (!pack_version_ok(header->hdr_version))
1036 /* "protocol error (pack version unsupported)" */
1037 return PH_ERROR_PROTOCOL;
1038 return 0;
1039 }
1040
1041 int for_each_file_in_obj_subdir(unsigned int subdir_nr,
1042 struct strbuf *path,
1043 const struct git_hash_algo *algop,
1044 each_loose_object_fn obj_cb,
1045 each_loose_cruft_fn cruft_cb,
1046 each_loose_subdir_fn subdir_cb,
1047 void *data)
1048 {
1049 size_t origlen, baselen;
1050 DIR *dir;
1051 struct dirent *de;
1052 int r = 0;
1053 struct object_id oid;
1054
1055 if (subdir_nr > 0xff)
1056 BUG("invalid loose object subdirectory: %x", subdir_nr);
1057
1058 origlen = path->len;
1059 strbuf_complete(path, '/');
1060 strbuf_addf(path, "%02x", subdir_nr);
1061
1062 dir = opendir(path->buf);
1063 if (!dir) {
1064 if (errno != ENOENT)
1065 r = error_errno(_("unable to open %s"), path->buf);
1066 strbuf_setlen(path, origlen);
1067 return r;
1068 }
1069
1070 oid.hash[0] = subdir_nr;
1071 strbuf_addch(path, '/');
1072 baselen = path->len;
1073
1074 while ((de = readdir_skip_dot_and_dotdot(dir))) {
1075 size_t namelen;
1076
1077 namelen = strlen(de->d_name);
1078 strbuf_setlen(path, baselen);
1079 strbuf_add(path, de->d_name, namelen);
1080 if (namelen == algop->hexsz - 2 &&
1081 !hex_to_bytes(oid.hash + 1, de->d_name,
1082 algop->rawsz - 1)) {
1083 oid_set_algo(&oid, algop);
1084 memset(oid.hash + algop->rawsz, 0,
1085 GIT_MAX_RAWSZ - algop->rawsz);
1086 if (obj_cb) {
1087 r = obj_cb(&oid, path->buf, data);
1088 if (r)
1089 break;
1090 }
1091 continue;
1092 }
1093
1094 if (cruft_cb) {
1095 r = cruft_cb(de->d_name, path->buf, data);
1096 if (r)
1097 break;
1098 }
1099 }
1100 closedir(dir);
1101
1102 strbuf_setlen(path, baselen - 1);
1103 if (!r && subdir_cb)
1104 r = subdir_cb(subdir_nr, path->buf, data);
1105
1106 strbuf_setlen(path, origlen);
1107
1108 return r;
1109 }
1110
1111 int for_each_loose_file_in_source(struct odb_source *source,
1112 each_loose_object_fn obj_cb,
1113 each_loose_cruft_fn cruft_cb,
1114 each_loose_subdir_fn subdir_cb,
1115 void *data)
1116 {
1117 struct strbuf buf = STRBUF_INIT;
1118 int r;
1119
1120 strbuf_addstr(&buf, source->path);
1121 for (int i = 0; i < 256; i++) {
1122 r = for_each_file_in_obj_subdir(i, &buf, source->odb->repo->hash_algo,
1123 obj_cb, cruft_cb, subdir_cb, data);
1124 if (r)
1125 break;
1126 }
1127
1128 strbuf_release(&buf);
1129 return r;
1130 }
1131
1132 static int check_stream_oid(git_zstream *stream,
1133 const char *hdr,
1134 unsigned long size,
1135 const char *path,
1136 const struct object_id *expected_oid,
1137 const struct git_hash_algo *algop)
1138 {
1139 struct git_hash_ctx c;
1140 struct object_id real_oid;
1141 unsigned char buf[4096];
1142 unsigned long total_read;
1143 int status = Z_OK;
1144
1145 git_hash_init(&c, algop);
1146 git_hash_update(&c, hdr, stream->total_out);
1147
1148 /*
1149 * We already read some bytes into hdr, but the ones up to the NUL
1150 * do not count against the object's content size.
1151 */
1152 total_read = stream->total_out - strlen(hdr) - 1;
1153
1154 /*
1155 * This size comparison must be "<=" to read the final zlib packets;
1156 * see the comment in unpack_loose_rest for details.
1157 */
1158 while (total_read <= size &&
1159 (status == Z_OK ||
1160 (status == Z_BUF_ERROR && !stream->avail_out))) {
1161 stream->next_out = buf;
1162 stream->avail_out = sizeof(buf);
1163 if (size - total_read < stream->avail_out)
1164 stream->avail_out = size - total_read;
1165 status = git_inflate(stream, Z_FINISH);
1166 git_hash_update(&c, buf, stream->next_out - buf);
1167 total_read += stream->next_out - buf;
1168 }
1169
1170 if (status != Z_STREAM_END) {
1171 error(_("corrupt loose object '%s'"), oid_to_hex(expected_oid));
1172 git_hash_discard(&c);
1173 return -1;
1174 }
1175 if (stream->avail_in) {
1176 error(_("garbage at end of loose object '%s'"),
1177 oid_to_hex(expected_oid));
1178 git_hash_discard(&c);
1179 return -1;
1180 }
1181
1182 git_hash_final_oid(&real_oid, &c);
1183 if (!oideq(expected_oid, &real_oid)) {
1184 error(_("hash mismatch for %s (expected %s)"), path,
1185 oid_to_hex(expected_oid));
1186 return -1;
1187 }
1188
1189 return 0;
1190 }
1191
1192 int read_loose_object(struct repository *repo,
1193 const char *path,
1194 const struct object_id *expected_oid,
1195 struct object_id *real_oid,
1196 void **contents,
1197 struct object_info *oi)
1198 {
1199 int ret = -1;
1200 int fd;
1201 void *map = NULL;
1202 unsigned long mapsize;
1203 git_zstream stream;
1204 char hdr[MAX_HEADER_LEN];
1205 size_t *size = oi->sizep;
1206
1207 fd = git_open(path);
1208 if (fd >= 0)
1209 map = map_fd(fd, path, &mapsize);
1210 if (!map) {
1211 error_errno(_("unable to mmap %s"), path);
1212 goto out;
1213 }
1214
1215 if (unpack_loose_header(&stream, map, mapsize, hdr, sizeof(hdr)) != ULHR_OK) {
1216 error(_("unable to unpack header of %s"), path);
1217 goto out_inflate;
1218 }
1219
1220 if (parse_loose_header(hdr, oi) < 0) {
1221 error(_("unable to parse header of %s"), path);
1222 goto out_inflate;
1223 }
1224
1225 if (*oi->typep < 0) {
1226 error(_("unable to parse type from header '%s' of %s"),
1227 hdr, path);
1228 goto out_inflate;
1229 }
1230
1231 if (*oi->typep == OBJ_BLOB &&
1232 *size > repo_settings_get_big_file_threshold(repo)) {
1233 if (check_stream_oid(&stream, hdr, *size, path, expected_oid,
1234 repo->hash_algo) < 0)
1235 goto out_inflate;
1236 } else {
1237 *contents = unpack_loose_rest(&stream, hdr, *size, expected_oid);
1238 if (!*contents) {
1239 error(_("unable to unpack contents of %s"), path);
1240 goto out_inflate;
1241 }
1242 hash_object_file(repo->hash_algo,
1243 *contents, *size,
1244 *oi->typep, real_oid);
1245 if (!oideq(expected_oid, real_oid))
1246 goto out_inflate;
1247 }
1248
1249 ret = 0; /* everything checks out */
1250
1251 out_inflate:
1252 git_inflate_end(&stream);
1253 out:
1254 if (map)
1255 munmap(map, mapsize);
1256 return ret;
1257 }
1258
1259 static int odb_transaction_files_commit(struct odb_transaction *base)
1260 {
1261 struct odb_transaction_files *transaction =
1262 container_of(base, struct odb_transaction_files, base);
1263
1264 if (transaction->objdir) {
1265 struct strbuf temp_path = STRBUF_INIT;
1266 struct tempfile *temp;
1267
1268 /*
1269 * Issue a full hardware flush against a temporary file to ensure
1270 * that all objects are durable before any renames occur. The code in
1271 * odb_transaction_files_fsync has already issued a writeout
1272 * request, but it has not flushed any writeback cache in the storage
1273 * hardware or any filesystem logs. This fsync call acts as a barrier
1274 * to ensure that the data in each new object file is durable before
1275 * the final name is visible.
1276 */
1277 strbuf_addf(&temp_path, "%s/bulk_fsync_XXXXXX",
1278 repo_get_object_directory(transaction->base.source->odb->repo));
1279 temp = xmks_tempfile(temp_path.buf);
1280 fsync_or_die(get_tempfile_fd(temp), get_tempfile_path(temp));
1281 delete_tempfile(&temp);
1282 strbuf_release(&temp_path);
1283
1284 /*
1285 * Make the object files visible in the primary ODB after their data is
1286 * fully durable.
1287 */
1288 if (tmp_objdir_migrate(transaction->objdir))
1289 return error(_("unable to migrate temporary objects"));
1290
1291 transaction->objdir = NULL;
1292 }
1293
1294 flush_packfile_transaction(transaction);
1295
1296 return 0;
1297 }
1298
1299 static const char *parse_pack_header(struct pack_header *hdr, int pack_fd)
1300 {
1301 switch (read_pack_header(pack_fd, hdr)) {
1302 case PH_ERROR_EOF:
1303 return "eof before pack header was fully read";
1304
1305 case PH_ERROR_PACK_SIGNATURE:
1306 return "protocol error (pack signature mismatch detected)";
1307
1308 case PH_ERROR_PROTOCOL:
1309 return "protocol error (pack version unsupported)";
1310
1311 default:
1312 return "unknown error in parse_pack_header";
1313
1314 case 0:
1315 return NULL;
1316 }
1317 }
1318
1319 static void push_header_arg(struct strvec *args, struct pack_header *hdr)
1320 {
1321 strvec_pushf(args, "--pack_header=%"PRIu32",%"PRIu32,
1322 ntohl(hdr->hdr_version), ntohl(hdr->hdr_entries));
1323 }
1324
1325 static unsigned int get_unpack_limit(struct repository *repo,
1326 enum odb_transaction_flags flags)
1327 {
1328 unsigned int limit = 0;
1329
1330 if (flags & ODB_TRANSACTION_RECEIVE) {
1331 limit = 100;
1332 repo_config_get_uint(repo, "transfer.unpacklimit", &limit);
1333 repo_config_get_uint(repo, "receive.unpacklimit", &limit);
1334 }
1335
1336 return limit;
1337 }
1338
1339 static int odb_transaction_files_write_pack(struct odb_transaction *base,
1340 int pack_fd, struct strbuf *err_msg,
1341 const struct odb_transaction_write_pack_opts *opts)
1342 {
1343 struct odb_transaction_files *transaction =
1344 container_of(base, struct odb_transaction_files, base);
1345 struct repository *repo = base->source->odb->repo;
1346 struct child_process child = CHILD_PROCESS_INIT;
1347 struct pack_header hdr;
1348 const char *hdr_err;
1349 int err_fd = opts->err_fd;
1350 int status;
1351
1352 hdr_err = parse_pack_header(&hdr, pack_fd);
1353 if (hdr_err) {
1354 if (err_fd > 0)
1355 close(err_fd);
1356 strbuf_addstr(err_msg, hdr_err);
1357 return -1;
1358 }
1359
1360 if (opts->shallow_file) {
1361 strvec_push(&child.args, "--shallow-file");
1362 strvec_push(&child.args, opts->shallow_file);
1363 }
1364
1365 odb_transaction_env(base, &child.env);
1366
1367 if (ntohl(hdr.hdr_entries) < get_unpack_limit(repo, transaction->flags)) {
1368 strvec_push(&child.args, "unpack-objects");
1369 push_header_arg(&child.args, &hdr);
1370 if (opts->quiet)
1371 strvec_push(&child.args, "-q");
1372 if (opts->fsck_objects)
1373 strvec_pushf(&child.args, "--strict%s",
1374 opts->fsck_msg_types);
1375 if (opts->max_input_size)
1376 strvec_pushf(&child.args, "--max-input-size=%"PRIuMAX,
1377 (uintmax_t)opts->max_input_size);
1378 child.no_stdout = 1;
1379 child.in = pack_fd;
1380 child.err = err_fd;
1381 child.git_cmd = 1;
1382 status = run_command(&child);
1383 if (status) {
1384 strbuf_addstr(err_msg, "unpack-objects abnormal exit");
1385 return -1;
1386 }
1387 } else {
1388 char hostname[HOST_NAME_MAX + 1];
1389 char *lockfile;
1390
1391 strvec_pushl(&child.args, "index-pack", "--stdin", NULL);
1392 push_header_arg(&child.args, &hdr);
1393
1394 if (xgethostname(hostname, sizeof(hostname)))
1395 xsnprintf(hostname, sizeof(hostname), "localhost");
1396 strvec_pushf(&child.args,
1397 "--keep=receive-pack %"PRIuMAX" on %s",
1398 (uintmax_t)getpid(),
1399 hostname);
1400
1401 if (!opts->quiet && err_fd)
1402 strvec_push(&child.args, "--show-resolving-progress");
1403 if (err_fd)
1404 strvec_push(&child.args, "--report-end-of-input");
1405 if (opts->fsck_objects)
1406 strvec_pushf(&child.args, "--strict%s",
1407 opts->fsck_msg_types);
1408 if (!opts->reject_thin)
1409 strvec_push(&child.args, "--fix-thin");
1410 if (opts->max_input_size)
1411 strvec_pushf(&child.args, "--max-input-size=%"PRIuMAX,
1412 (uintmax_t)opts->max_input_size);
1413 child.out = -1;
1414 child.in = pack_fd;
1415 child.err = err_fd;
1416 child.git_cmd = 1;
1417 status = start_command(&child);
1418 if (status) {
1419 strbuf_addstr(err_msg, "index-pack fork failed");
1420 return -1;
1421 }
1422
1423 /*
1424 * The lockfile filepath is expected to be the final location of
1425 * the ".keep" file after being migrated to the main ODB source.
1426 * This ensures the lockfile can be found and removed later
1427 * after the ODB transaction has been committed.
1428 */
1429 lockfile = index_pack_lockfile(base->source, child.out, NULL);
1430 if (lockfile) {
1431 ALLOC_GROW(transaction->pack_lockfiles,
1432 transaction->pack_lockfiles_nr + 1,
1433 transaction->pack_lockfiles_alloc);
1434 transaction->pack_lockfiles[transaction->pack_lockfiles_nr++] =
1435 register_tempfile(lockfile);
1436 free(lockfile);
1437 }
1438 close(child.out);
1439
1440 status = finish_command(&child);
1441 if (status) {
1442 strbuf_addstr(err_msg, "index-pack abnormal exit");
1443 return -1;
1444 }
1445
1446 odb_source_prepare(transaction->quarantine,
1447 ODB_PREPARE_FLUSH_CACHES);
1448 }
1449
1450 return 0;
1451 }
1452
1453 static int odb_transaction_files_finalize(struct odb_transaction *base)
1454 {
1455 struct odb_transaction_files *transaction =
1456 container_of(base, struct odb_transaction_files, base);
1457 int ret = 0;
1458
1459 for (size_t i = 0; i < transaction->pack_lockfiles_nr; i++)
1460 ret |= delete_tempfile(&transaction->pack_lockfiles[i]);
1461
1462 free(transaction->pack_lockfiles);
1463
1464 return ret;
1465 }
1466
1467 static int odb_transaction_files_env(struct odb_transaction *base,
1468 struct strvec *env)
1469 {
1470 struct odb_transaction_files *transaction =
1471 container_of(base, struct odb_transaction_files, base);
1472 int ret;
1473
1474 ret = odb_transaction_files_prepare(&transaction->base);
1475 if (!ret)
1476 strvec_pushv(env, tmp_objdir_env(transaction->objdir));
1477
1478 return ret;
1479 }
1480
1481 int odb_transaction_files_begin(struct odb_source *source,
1482 struct odb_transaction **out,
1483 enum odb_transaction_flags flags)
1484 {
1485 struct odb_transaction_files *transaction;
1486
1487 transaction = xcalloc(1, sizeof(*transaction));
1488 transaction->base.source = source;
1489 transaction->base.commit = odb_transaction_files_commit;
1490 transaction->base.finalize = odb_transaction_files_finalize;
1491 transaction->base.write_object_stream = odb_transaction_files_write_object_stream;
1492 transaction->base.write_pack = odb_transaction_files_write_pack;
1493 transaction->base.env = odb_transaction_files_env;
1494 transaction->flags = flags;
1495
1496 transaction->prefix = "bulk-fsync";
1497 if (flags & ODB_TRANSACTION_RECEIVE) {
1498 /*
1499 * ODB transactions for git-receive-pack(1) eagerly create a
1500 * temporary directory and use a different temporary directory
1501 * prefix.
1502 *
1503 * NEEDSWORK: This transaction flag is only used by the "files"
1504 * backend to special case temporary directory set up and
1505 * handling. Ideally transaction users should not have to care
1506 * though. To avoid this, we could eagerly create the temporary
1507 * directory and use the same prefix name for all transactions.
1508 */
1509 transaction->prefix = "incoming";
1510 if (odb_transaction_files_prepare(&transaction->base)) {
1511 free(transaction);
1512 return -1;
1513 }
1514 }
1515
1516 *out = &transaction->base;
1517
1518 return 0;
1519 }