at commit a0189536

1 #include "git-compat-util.h"
2 #include "abspath.h"
3 #include "chdir-notify.h"
4 #include "gettext.h"
5 #include "hex.h"
6 #include "loose.h"
7 #include "object-file.h"
8 #include "object-file-convert.h"
9 #include "odb.h"
10 #include "odb/source-files.h"
11 #include "odb/source-loose.h"
12 #include "odb/streaming.h"
13 #include "oidtree.h"
14 #include "path.h"
15 #include "progress.h"
16 #include "repository.h"
17 #include "strbuf.h"
18 #include "tempfile.h"
19 #include "write-or-die.h"
20
21 static int append_loose_object(const struct object_id *oid,
22 const char *path UNUSED,
23 void *data)
24 {
25 oidtree_insert(data, oid, NULL);
26 return 0;
27 }
28
29 static struct oidtree *odb_source_loose_cache(struct odb_source_loose *loose,
30 const struct object_id *oid)
31 {
32 int subdir_nr = oid->hash[0];
33 struct strbuf buf = STRBUF_INIT;
34 size_t word_bits = bitsizeof(loose->subdir_seen[0]);
35 size_t word_index = subdir_nr / word_bits;
36 size_t mask = (size_t)1u << (subdir_nr % word_bits);
37 uint32_t *bitmap;
38
39 if (subdir_nr < 0 ||
40 (size_t) subdir_nr >= bitsizeof(loose->subdir_seen))
41 BUG("subdir_nr out of range");
42
43 bitmap = &loose->subdir_seen[word_index];
44 if (*bitmap & mask)
45 return loose->cache;
46 if (!loose->cache) {
47 ALLOC_ARRAY(loose->cache, 1);
48 oidtree_init(loose->cache);
49 }
50 strbuf_addstr(&buf, loose->base.path);
51 for_each_file_in_obj_subdir(subdir_nr, &buf,
52 loose->base.odb->repo->hash_algo,
53 append_loose_object,
54 NULL, NULL,
55 loose->cache);
56 *bitmap |= mask;
57 strbuf_release(&buf);
58 return loose->cache;
59 }
60
61 static int quick_has_loose(struct odb_source_loose *loose,
62 const struct object_id *oid)
63 {
64 return !!oidtree_contains(odb_source_loose_cache(loose, oid), oid);
65 }
66
67 static int read_object_info_from_path(struct odb_source_loose *loose,
68 const char *path,
69 const struct object_id *oid,
70 struct object_info *oi,
71 enum object_info_flags flags,
72 struct strbuf *errmsg)
73 {
74 int ret;
75 int fd;
76 unsigned long mapsize;
77 void *map = NULL;
78 git_zstream stream, *stream_to_end = NULL;
79 char hdr[MAX_HEADER_LEN];
80 size_t size_scratch;
81 enum object_type type_scratch;
82 struct stat st;
83
84 /*
85 * If we don't care about type or size, then we don't
86 * need to look inside the object at all. Note that we
87 * do not optimize out the stat call, even if the
88 * caller doesn't care about the disk-size, since our
89 * return value implicitly indicates whether the
90 * object even exists.
91 */
92 if (!oi || (!oi->typep && !oi->sizep && !oi->contentp)) {
93 struct stat st;
94
95 if ((!oi || (!oi->disk_sizep && !oi->mtimep)) && (flags & OBJECT_INFO_QUICK)) {
96 ret = quick_has_loose(loose, oid) ? 0 : ODB_READ_NOT_FOUND;
97 goto out;
98 }
99
100 if (lstat(path, &st) < 0) {
101 if (errno == ENOENT) {
102 ret = ODB_READ_NOT_FOUND;
103 goto out;
104 }
105
106 ret = -1;
107 goto out;
108 }
109
110 if (oi) {
111 if (oi->disk_sizep)
112 *oi->disk_sizep = st.st_size;
113 if (oi->mtimep)
114 *oi->mtimep = st.st_mtime;
115 }
116
117 ret = 0;
118 goto out;
119 }
120
121 fd = git_open(path);
122 if (fd < 0) {
123 if (errno == ENOENT) {
124 ret = ODB_READ_NOT_FOUND;
125 goto out;
126 }
127
128 ret = error_errno(_("unable to open loose object %s"), oid_to_hex(oid));
129 goto out;
130 }
131
132 if (fstat(fd, &st)) {
133 close(fd);
134 ret = -1;
135 goto out;
136 }
137
138 mapsize = xsize_t(st.st_size);
139 if (!mapsize) {
140 close(fd);
141 ret = error(_("object file %s is empty"), path);
142 goto out;
143 }
144
145 map = xmmap(NULL, mapsize, PROT_READ, MAP_PRIVATE, fd, 0);
146 close(fd);
147 if (!map) {
148 ret = -1;
149 goto out;
150 }
151
152 if (oi->disk_sizep)
153 *oi->disk_sizep = mapsize;
154 if (oi->mtimep)
155 *oi->mtimep = st.st_mtime;
156
157 stream_to_end = &stream;
158
159 switch (unpack_loose_header(&stream, map, mapsize, hdr, sizeof(hdr))) {
160 case ULHR_OK:
161 if (!oi->sizep)
162 oi->sizep = &size_scratch;
163 if (!oi->typep)
164 oi->typep = &type_scratch;
165
166 if (parse_loose_header(hdr, oi) < 0) {
167 ret = error(_("unable to parse %s header"), oid_to_hex(oid));
168 goto out;
169 }
170
171 if (*oi->typep < 0)
172 die(_("invalid object type"));
173
174 if (oi->contentp) {
175 *oi->contentp = unpack_loose_rest(&stream, hdr, *oi->sizep, oid);
176 if (!*oi->contentp) {
177 ret = -1;
178 goto out;
179 }
180 }
181
182 break;
183 case ULHR_BAD:
184 ret = error(_("unable to unpack %s header"),
185 oid_to_hex(oid));
186 goto out;
187 case ULHR_TOO_LONG:
188 ret = error(_("header for %s too long, exceeds %d bytes"),
189 oid_to_hex(oid), MAX_HEADER_LEN);
190 goto out;
191 }
192
193 ret = 0;
194
195 out:
196 if (ret && ret != ODB_READ_NOT_FOUND && errmsg)
197 strbuf_addf(errmsg, _("loose object %s (stored in %s) is corrupt"),
198 oid_to_hex(oid), path);
199 if (stream_to_end)
200 git_inflate_end(stream_to_end);
201 if (map)
202 munmap(map, mapsize);
203 if (oi) {
204 if (oi->sizep == &size_scratch)
205 oi->sizep = NULL;
206 if (oi->typep == &type_scratch)
207 oi->typep = NULL;
208 if (oi->delta_base_oid)
209 oidclr(oi->delta_base_oid, loose->base.odb->repo->hash_algo);
210 if (oi->source_infop && !ret)
211 oi->source_infop->source = &loose->base;
212 }
213
214 return ret;
215 }
216
217 static enum odb_read_status odb_source_loose_read_object_info(struct odb_source *source,
218 const struct object_id *oid,
219 struct object_info *oi,
220 enum object_info_flags flags,
221 struct strbuf *errmsg)
222 {
223 struct odb_source_loose *loose = odb_source_loose_downcast(source);
224 static struct strbuf buf = STRBUF_INIT;
225
226 /*
227 * The second read shouldn't cause new loose objects to show up, unless
228 * there was a race condition with a secondary process. We don't care
229 * about this case though, so we simply skip reading loose objects a
230 * second time.
231 */
232 if (flags & OBJECT_INFO_SECOND_READ)
233 return ODB_READ_NOT_FOUND;
234
235 odb_loose_path(loose, &buf, oid);
236 return read_object_info_from_path(loose, buf.buf, oid, oi, flags, errmsg);
237 }
238
239 /*
240 * Find "oid" as a loose object in given source, open the object and return its
241 * file descriptor. Returns the file descriptor on success, negative on failure.
242 *
243 * The "path" out-parameter will give the path of the object we found (if any).
244 * Note that it may point to static storage and is only valid until another
245 * call to open_loose_object().
246 */
247 static int open_loose_object(struct odb_source_loose *loose,
248 const struct object_id *oid, const char **path)
249 {
250 static struct strbuf buf = STRBUF_INIT;
251 int fd;
252
253 *path = odb_loose_path(loose, &buf, oid);
254 fd = git_open(*path);
255 if (fd >= 0)
256 return fd;
257
258 return -1;
259 }
260
261 static void *odb_source_loose_map_object(struct odb_source_loose *loose,
262 const struct object_id *oid,
263 unsigned long *size)
264 {
265 const char *p;
266 int fd = open_loose_object(loose, oid, &p);
267 void *map = NULL;
268 struct stat st;
269
270 if (fd < 0)
271 return NULL;
272
273 if (!fstat(fd, &st)) {
274 *size = xsize_t(st.st_size);
275 if (!*size) {
276 /* mmap() is forbidden on empty files */
277 error(_("object file %s is empty"), p);
278 goto out;
279 }
280
281 map = xmmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0);
282 }
283
284 out:
285 close(fd);
286 return map;
287 }
288
289 struct odb_loose_read_stream {
290 struct odb_stream base;
291 git_zstream z;
292 enum {
293 ODB_LOOSE_READ_STREAM_INUSE,
294 ODB_LOOSE_READ_STREAM_DONE,
295 ODB_LOOSE_READ_STREAM_ERROR,
296 } z_state;
297 void *mapped;
298 unsigned long mapsize;
299 char hdr[32];
300 int hdr_avail;
301 int hdr_used;
302 };
303
304 static ssize_t read_istream_loose(struct odb_stream *_st, char *buf, size_t sz)
305 {
306 struct odb_loose_read_stream *st =
307 container_of(_st, struct odb_loose_read_stream, base);
308 size_t total_read = 0;
309
310 switch (st->z_state) {
311 case ODB_LOOSE_READ_STREAM_DONE:
312 return 0;
313 case ODB_LOOSE_READ_STREAM_ERROR:
314 return -1;
315 default:
316 break;
317 }
318
319 if (st->hdr_used < st->hdr_avail) {
320 size_t to_copy = st->hdr_avail - st->hdr_used;
321 if (sz < to_copy)
322 to_copy = sz;
323 memcpy(buf, st->hdr + st->hdr_used, to_copy);
324 st->hdr_used += to_copy;
325 total_read += to_copy;
326 }
327
328 while (total_read < sz) {
329 int status;
330
331 st->z.next_out = (unsigned char *)buf + total_read;
332 st->z.avail_out = sz - total_read;
333 status = git_inflate(&st->z, Z_FINISH);
334
335 total_read = st->z.next_out - (unsigned char *)buf;
336
337 if (status == Z_STREAM_END) {
338 git_inflate_end(&st->z);
339 st->z_state = ODB_LOOSE_READ_STREAM_DONE;
340 break;
341 }
342 if (status != Z_OK && (status != Z_BUF_ERROR || total_read < sz)) {
343 git_inflate_end(&st->z);
344 st->z_state = ODB_LOOSE_READ_STREAM_ERROR;
345 return -1;
346 }
347 }
348 return total_read;
349 }
350
351 static int close_istream_loose(struct odb_stream *_st)
352 {
353 struct odb_loose_read_stream *st =
354 container_of(_st, struct odb_loose_read_stream, base);
355
356 if (st->z_state == ODB_LOOSE_READ_STREAM_INUSE)
357 git_inflate_end(&st->z);
358 munmap(st->mapped, st->mapsize);
359 return 0;
360 }
361
362 static int odb_source_loose_read_object_stream(struct odb_stream **out,
363 struct odb_source *source,
364 const struct object_id *oid)
365 {
366 struct odb_source_loose *loose = odb_source_loose_downcast(source);
367 struct object_info oi = OBJECT_INFO_INIT;
368 struct odb_loose_read_stream *st;
369 unsigned long mapsize;
370 void *mapped;
371
372 mapped = odb_source_loose_map_object(loose, oid, &mapsize);
373 if (!mapped)
374 return -1;
375
376 /*
377 * Note: we must allocate this structure early even though we may still
378 * fail. This is because we need to initialize the zlib stream, and it
379 * is not possible to copy the stream around after the fact because it
380 * has self-referencing pointers.
381 */
382 CALLOC_ARRAY(st, 1);
383
384 switch (unpack_loose_header(&st->z, mapped, mapsize, st->hdr,
385 sizeof(st->hdr))) {
386 case ULHR_OK:
387 break;
388 case ULHR_BAD:
389 case ULHR_TOO_LONG:
390 goto error;
391 }
392
393 oi.sizep = &st->base.size;
394 oi.typep = &st->base.type;
395
396 if (parse_loose_header(st->hdr, &oi) < 0 || st->base.type < 0)
397 goto error;
398
399 st->mapped = mapped;
400 st->mapsize = mapsize;
401 st->hdr_used = strlen(st->hdr) + 1;
402 st->hdr_avail = st->z.total_out;
403 st->z_state = ODB_LOOSE_READ_STREAM_INUSE;
404 st->base.close = close_istream_loose;
405 st->base.read = read_istream_loose;
406
407 *out = &st->base;
408
409 return 0;
410 error:
411 git_inflate_end(&st->z);
412 munmap(mapped, mapsize);
413 free(st);
414 return -1;
415 }
416
417 struct for_each_object_wrapper_data {
418 struct odb_source_loose *loose;
419 const struct object_info *request;
420 odb_for_each_object_cb cb;
421 void *cb_data;
422 };
423
424 static int for_each_object_wrapper_cb(const struct object_id *oid,
425 const char *path,
426 void *cb_data)
427 {
428 struct for_each_object_wrapper_data *data = cb_data;
429
430 if (data->request) {
431 struct object_info oi = *data->request;
432
433 if (read_object_info_from_path(data->loose, path, oid, &oi, 0, NULL) < 0)
434 return -1;
435
436 return data->cb(oid, &oi, data->cb_data);
437 } else {
438 return data->cb(oid, NULL, data->cb_data);
439 }
440 }
441
442 static int for_each_prefixed_object_wrapper_cb(const struct object_id *oid,
443 void *node_data UNUSED,
444 void *cb_data)
445 {
446 struct for_each_object_wrapper_data *data = cb_data;
447 if (data->request) {
448 struct object_info oi = *data->request;
449
450 if (odb_source_read_object_info(&data->loose->base,
451 oid, &oi, 0, NULL) < 0)
452 return -1;
453
454 return data->cb(oid, &oi, data->cb_data);
455 } else {
456 return data->cb(oid, NULL, data->cb_data);
457 }
458 }
459
460 static int odb_source_loose_for_each_object(struct odb_source *source,
461 const struct object_info *request,
462 odb_for_each_object_cb cb,
463 void *cb_data,
464 const struct odb_for_each_object_options *opts)
465 {
466 struct odb_source_loose *loose = odb_source_loose_downcast(source);
467 struct for_each_object_wrapper_data data = {
468 .loose = loose,
469 .request = request,
470 .cb = cb,
471 .cb_data = cb_data,
472 };
473
474 /* There are no loose promisor objects, so we can return immediately. */
475 if ((opts->flags & ODB_FOR_EACH_OBJECT_PROMISOR_ONLY))
476 return 0;
477 if ((opts->flags & ODB_FOR_EACH_OBJECT_LOCAL_ONLY) && !source->local)
478 return 0;
479
480 if (opts->prefix)
481 return oidtree_each(odb_source_loose_cache(loose, opts->prefix),
482 opts->prefix, opts->prefix_hex_len,
483 for_each_prefixed_object_wrapper_cb, &data);
484
485 return for_each_loose_file_in_source(source, for_each_object_wrapper_cb,
486 NULL, NULL, &data);
487 }
488
489 struct find_abbrev_len_data {
490 const struct object_id *oid;
491 unsigned len;
492 };
493
494 static int find_abbrev_len_cb(const struct object_id *oid,
495 struct object_info *oi UNUSED,
496 void *cb_data)
497 {
498 struct find_abbrev_len_data *data = cb_data;
499 unsigned len = oid_common_prefix_hexlen(oid, data->oid);
500 if (len != hash_algos[oid->algo].hexsz && len >= data->len)
501 data->len = len + 1;
502 return 0;
503 }
504
505 static int odb_source_loose_find_abbrev_len(struct odb_source *source,
506 const struct object_id *oid,
507 unsigned min_len,
508 unsigned *out)
509 {
510 struct odb_source_loose *loose = odb_source_loose_downcast(source);
511 struct odb_for_each_object_options opts = {
512 .prefix = oid,
513 .prefix_hex_len = min_len,
514 };
515 struct find_abbrev_len_data data = {
516 .oid = oid,
517 .len = min_len,
518 };
519 int ret;
520
521 ret = odb_source_for_each_object(&loose->base, NULL, find_abbrev_len_cb,
522 &data, &opts);
523 *out = data.len;
524
525 return ret;
526 }
527
528 static int count_loose_object(const struct object_id *oid UNUSED,
529 struct object_info *oi UNUSED,
530 void *payload)
531 {
532 unsigned long *count = payload;
533 (*count)++;
534 return 0;
535 }
536
537 static int odb_source_loose_count_objects(struct odb_source *source,
538 enum odb_count_objects_flags flags,
539 unsigned long *out)
540 {
541 struct odb_source_loose *loose = odb_source_loose_downcast(source);
542 const unsigned hexsz = source->odb->repo->hash_algo->hexsz - 2;
543 char *path = NULL;
544 DIR *dir = NULL;
545 int ret;
546
547 if (flags & ODB_COUNT_OBJECTS_APPROXIMATE) {
548 unsigned long count = 0;
549 struct dirent *ent;
550
551 path = xstrfmt("%s/17", source->path);
552
553 dir = opendir(path);
554 if (!dir) {
555 if (errno == ENOENT) {
556 *out = 0;
557 ret = 0;
558 goto out;
559 }
560
561 ret = error_errno("cannot open object shard '%s'", path);
562 goto out;
563 }
564
565 while ((ent = readdir(dir)) != NULL) {
566 if (strspn(ent->d_name, "0123456789abcdef") != hexsz ||
567 ent->d_name[hexsz] != '\0')
568 continue;
569 count++;
570 }
571
572 *out = count * 256;
573 ret = 0;
574 } else {
575 struct odb_for_each_object_options opts = { 0 };
576 *out = 0;
577 ret = odb_source_for_each_object(&loose->base, NULL, count_loose_object,
578 out, &opts);
579 }
580
581 out:
582 if (dir)
583 closedir(dir);
584 free(path);
585 return ret;
586 }
587
588 static int odb_source_loose_freshen_object(struct odb_source *source,
589 const struct object_id *oid,
590 const time_t *mtime)
591 {
592 struct odb_source_loose *loose = odb_source_loose_downcast(source);
593 static struct strbuf path = STRBUF_INIT;
594 odb_loose_path(loose, &path, oid);
595 return !!check_and_freshen_file(path.buf, 1, mtime);
596 }
597
598 /* Finalize a file on disk, and close it. */
599 static void close_loose_object(struct odb_source_loose *loose,
600 int fd, const char *filename)
601 {
602 if (loose->base.will_destroy)
603 goto out;
604
605 if (batch_fsync_enabled(FSYNC_COMPONENT_LOOSE_OBJECT))
606 odb_transaction_files_fsync(loose->base.odb->transaction, fd, filename);
607 else if (fsync_object_files > 0)
608 fsync_or_die(fd, filename);
609 else
610 fsync_component_or_die(FSYNC_COMPONENT_LOOSE_OBJECT, fd,
611 filename);
612
613 out:
614 if (close(fd) != 0)
615 die_errno(_("error when closing loose object file"));
616 }
617
618 /* Size of directory component, including the ending '/' */
619 static inline int directory_size(const char *filename)
620 {
621 const char *s = strrchr(filename, '/');
622 if (!s)
623 return 0;
624 return s - filename + 1;
625 }
626
627 /*
628 * This creates a temporary file in the same directory as the final
629 * 'filename'
630 *
631 * We want to avoid cross-directory filename renames, because those
632 * can have problems on various filesystems (FAT, NFS, Coda).
633 */
634 static int create_tmpfile(struct repository *repo,
635 struct strbuf *tmp, const char *filename)
636 {
637 int fd, dirlen = directory_size(filename);
638
639 strbuf_reset(tmp);
640 strbuf_add(tmp, filename, dirlen);
641 strbuf_addstr(tmp, "tmp_obj_XXXXXX");
642 fd = git_mkstemp_mode(tmp->buf, 0444);
643 if (fd < 0 && dirlen && errno == ENOENT) {
644 /*
645 * Make sure the directory exists; note that the contents
646 * of the buffer are undefined after mkstemp returns an
647 * error, so we have to rewrite the whole buffer from
648 * scratch.
649 */
650 strbuf_reset(tmp);
651 strbuf_add(tmp, filename, dirlen - 1);
652 if (mkdir(tmp->buf, 0777) && errno != EEXIST)
653 return -1;
654 if (adjust_shared_perm(repo, tmp->buf))
655 return -1;
656
657 /* Try again */
658 strbuf_addstr(tmp, "/tmp_obj_XXXXXX");
659 fd = git_mkstemp_mode(tmp->buf, 0444);
660 }
661 return fd;
662 }
663
664 /**
665 * Common steps for loose object writers to start writing loose
666 * objects:
667 *
668 * - Create tmpfile for the loose object.
669 * - Setup zlib stream for compression.
670 * - Start to feed header to zlib stream.
671 *
672 * Returns a "fd", which should later be provided to
673 * end_loose_object_common().
674 */
675 static int start_loose_object_common(struct odb_source_loose *loose,
676 struct strbuf *tmp_file,
677 const char *filename, unsigned flags,
678 git_zstream *stream,
679 unsigned char *buf, size_t buflen,
680 struct git_hash_ctx *c, struct git_hash_ctx *compat_c,
681 char *hdr, int hdrlen)
682 {
683 const struct git_hash_algo *algo = loose->base.odb->repo->hash_algo;
684 const struct git_hash_algo *compat = loose->base.odb->repo->compat_hash_algo;
685 int fd;
686 struct repo_config_values *cfg = repo_config_values(loose->base.odb->repo);
687
688 fd = create_tmpfile(loose->base.odb->repo, tmp_file, filename);
689 if (fd < 0) {
690 if (flags & ODB_WRITE_OBJECT_SILENT)
691 return -1;
692 else if (errno == EACCES)
693 return error(_("insufficient permission for adding "
694 "an object to repository database %s"),
695 loose->base.path);
696 else
697 return error_errno(
698 _("unable to create temporary file"));
699 }
700
701 /* Setup zlib stream for compression */
702 git_deflate_init(stream, cfg->zlib_compression_level);
703 stream->next_out = buf;
704 stream->avail_out = buflen;
705 git_hash_init(c, algo);
706 if (compat && compat_c)
707 git_hash_init(compat_c, compat);
708
709 /* Start to feed header to zlib stream */
710 stream->next_in = (unsigned char *)hdr;
711 stream->avail_in = hdrlen;
712 while (git_deflate(stream, 0) == Z_OK)
713 ; /* nothing */
714 git_hash_update(c, hdr, hdrlen);
715 if (compat && compat_c)
716 git_hash_update(compat_c, hdr, hdrlen);
717
718 return fd;
719 }
720
721 /**
722 * Common steps for the inner git_deflate() loop for writing loose
723 * objects. Returns what git_deflate() returns.
724 */
725 static int write_loose_object_common(struct odb_source_loose *loose,
726 struct git_hash_ctx *c, struct git_hash_ctx *compat_c,
727 git_zstream *stream, const int flush,
728 unsigned char *in0, const int fd,
729 unsigned char *compressed,
730 const size_t compressed_len)
731 {
732 const struct git_hash_algo *compat = loose->base.odb->repo->compat_hash_algo;
733 int ret;
734
735 ret = git_deflate(stream, flush ? Z_FINISH : 0);
736 git_hash_update(c, in0, stream->next_in - in0);
737 if (compat && compat_c)
738 git_hash_update(compat_c, in0, stream->next_in - in0);
739 if (write_in_full(fd, compressed, stream->next_out - compressed) < 0)
740 die_errno(_("unable to write loose object file"));
741 stream->next_out = compressed;
742 stream->avail_out = compressed_len;
743
744 return ret;
745 }
746
747 /**
748 * Common steps for loose object writers to end writing loose objects:
749 *
750 * - End the compression of zlib stream.
751 * - Get the calculated oid to "oid".
752 */
753 static int end_loose_object_common(struct odb_source_loose *loose,
754 struct git_hash_ctx *c, struct git_hash_ctx *compat_c,
755 git_zstream *stream, struct object_id *oid,
756 struct object_id *compat_oid)
757 {
758 const struct git_hash_algo *compat = loose->base.odb->repo->compat_hash_algo;
759 int ret;
760
761 ret = git_deflate_end_gently(stream);
762 if (ret != Z_OK)
763 return ret;
764 git_hash_final_oid(oid, c);
765 if (compat && compat_c)
766 git_hash_final_oid(compat_oid, compat_c);
767
768 return Z_OK;
769 }
770
771 static int write_loose_object(struct odb_source_loose *loose,
772 const struct object_id *oid, char *hdr,
773 int hdrlen, const void *buf, unsigned long len,
774 const time_t *mtime, unsigned flags)
775 {
776 int fd, ret;
777 unsigned char compressed[4096];
778 git_zstream stream;
779 struct git_hash_ctx c;
780 struct object_id parano_oid;
781 static struct strbuf tmp_file = STRBUF_INIT;
782 static struct strbuf filename = STRBUF_INIT;
783
784 if (batch_fsync_enabled(FSYNC_COMPONENT_LOOSE_OBJECT))
785 odb_transaction_files_prepare(loose->base.odb->transaction);
786
787 odb_loose_path(loose, &filename, oid);
788
789 fd = start_loose_object_common(loose, &tmp_file, filename.buf, flags,
790 &stream, compressed, sizeof(compressed),
791 &c, NULL, hdr, hdrlen);
792 if (fd < 0)
793 return -1;
794
795 /* Then the data itself.. */
796 stream.next_in = (void *)buf;
797 stream.avail_in = len;
798 do {
799 unsigned char *in0 = stream.next_in;
800
801 ret = write_loose_object_common(loose, &c, NULL, &stream, 1, in0, fd,
802 compressed, sizeof(compressed));
803 } while (ret == Z_OK);
804
805 if (ret != Z_STREAM_END)
806 die(_("unable to deflate new object %s (%d)"), oid_to_hex(oid),
807 ret);
808 ret = end_loose_object_common(loose, &c, NULL, &stream, &parano_oid, NULL);
809 if (ret != Z_OK)
810 die(_("deflateEnd on object %s failed (%d)"), oid_to_hex(oid),
811 ret);
812 if (!oideq(oid, &parano_oid))
813 die(_("confused by unstable object source data for %s"),
814 oid_to_hex(oid));
815
816 close_loose_object(loose, fd, tmp_file.buf);
817
818 if (mtime) {
819 struct utimbuf utb = {
820 .actime = *mtime,
821 .modtime = *mtime,
822 };
823
824 if (utime(tmp_file.buf, &utb) < 0 &&
825 !(flags & ODB_WRITE_OBJECT_SILENT))
826 warning_errno(_("failed utime() on %s"), tmp_file.buf);
827 }
828
829 return finalize_object_file_flags(loose->base.odb->repo, tmp_file.buf, filename.buf,
830 FOF_SKIP_COLLISION_CHECK);
831 }
832
833 static int odb_source_loose_write_object(struct odb_source *source,
834 const void *buf, size_t len,
835 enum object_type type,
836 const struct object_id *oid,
837 const struct object_id *compat_oid,
838 const time_t *mtime,
839 enum odb_write_object_flags flags)
840 {
841 struct odb_source_loose *loose = odb_source_loose_downcast(source);
842 char hdr[MAX_HEADER_LEN];
843 int hdrlen;
844
845 hdrlen = format_object_header(hdr, sizeof(hdr), type, len);
846
847 if (write_loose_object(loose, oid, hdr, hdrlen, buf, len, mtime, flags))
848 return -1;
849
850 if (compat_oid)
851 return repo_add_loose_object_map(loose, oid, compat_oid);
852
853 return 0;
854 }
855
856 static int odb_source_loose_write_object_stream(struct odb_source *source,
857 struct odb_stream *in_stream,
858 struct object_id *oid)
859 {
860 struct odb_source_loose *loose = odb_source_loose_downcast(source);
861 const struct git_hash_algo *compat = loose->base.odb->repo->compat_hash_algo;
862 struct object_id compat_oid;
863 int fd, ret, err = 0, flush = 0;
864 unsigned char compressed[4096];
865 git_zstream stream;
866 struct git_hash_ctx c, compat_c;
867 struct strbuf tmp_file = STRBUF_INIT;
868 struct strbuf filename = STRBUF_INIT;
869 unsigned char buf[8192];
870 int dirlen;
871 bool is_finished = false;
872 char hdr[MAX_HEADER_LEN];
873 int hdrlen;
874
875 if (batch_fsync_enabled(FSYNC_COMPONENT_LOOSE_OBJECT))
876 odb_transaction_files_prepare(loose->base.odb->transaction);
877
878 /* Since oid is not determined, save tmp file to odb path. */
879 strbuf_addf(&filename, "%s/", loose->base.path);
880 hdrlen = format_object_header(hdr, sizeof(hdr), in_stream->type, in_stream->size);
881
882 /*
883 * Common steps for write_loose_object and stream_loose_object to
884 * start writing loose objects:
885 *
886 * - Create tmpfile for the loose object.
887 * - Setup zlib stream for compression.
888 * - Start to feed header to zlib stream.
889 */
890 fd = start_loose_object_common(loose, &tmp_file, filename.buf, 0,
891 &stream, compressed, sizeof(compressed),
892 &c, &compat_c, hdr, hdrlen);
893 if (fd < 0) {
894 err = -1;
895 goto cleanup;
896 }
897
898 /* Then the data itself.. */
899 do {
900 unsigned char *in0 = stream.next_in;
901
902 if (!stream.avail_in && !is_finished) {
903 ssize_t read_len = odb_stream_read(in_stream, buf,
904 sizeof(buf));
905 if (read_len < 0) {
906 close(fd);
907 err = -1;
908 goto cleanup;
909 }
910
911 /* All data has been read. */
912 if (!read_len) {
913 is_finished = true;
914 flush = 1;
915 }
916
917 stream.avail_in = read_len;
918 stream.next_in = buf;
919 in0 = buf;
920 }
921 ret = write_loose_object_common(loose, &c, &compat_c, &stream, flush, in0, fd,
922 compressed, sizeof(compressed));
923 /*
924 * Unlike write_loose_object(), we do not have the entire
925 * buffer. If we get Z_BUF_ERROR due to too few input bytes,
926 * then we'll replenish them in the next input_stream->read()
927 * call when we loop.
928 */
929 } while (ret == Z_OK || ret == Z_BUF_ERROR);
930
931 if (stream.total_in != in_stream->size + hdrlen)
932 die(_("write stream object %"PRIuMAX" != %"PRIuMAX), (uintmax_t)stream.total_in,
933 (uintmax_t)in_stream->size + hdrlen);
934
935 /*
936 * Common steps for write_loose_object and stream_loose_object to
937 * end writing loose object:
938 *
939 * - End the compression of zlib stream.
940 * - Get the calculated oid.
941 */
942 if (ret != Z_STREAM_END)
943 die(_("unable to stream deflate new object (%d)"), ret);
944 ret = end_loose_object_common(loose, &c, &compat_c, &stream, oid, &compat_oid);
945 if (ret != Z_OK)
946 die(_("deflateEnd on stream object failed (%d)"), ret);
947 close_loose_object(loose, fd, tmp_file.buf);
948
949 if (odb_freshen_object(loose->base.odb, oid)) {
950 unlink_or_warn(tmp_file.buf);
951 goto cleanup;
952 }
953 odb_loose_path(loose, &filename, oid);
954
955 /* We finally know the object path, and create the missing dir. */
956 dirlen = directory_size(filename.buf);
957 if (dirlen) {
958 struct strbuf dir = STRBUF_INIT;
959 strbuf_add(&dir, filename.buf, dirlen);
960
961 if (safe_create_dir_in_gitdir(loose->base.odb->repo, dir.buf) &&
962 errno != EEXIST) {
963 err = error_errno(_("unable to create directory %s"), dir.buf);
964 strbuf_release(&dir);
965 goto cleanup;
966 }
967 strbuf_release(&dir);
968 }
969
970 err = finalize_object_file_flags(loose->base.odb->repo, tmp_file.buf, filename.buf,
971 FOF_SKIP_COLLISION_CHECK);
972 if (!err && compat)
973 err = repo_add_loose_object_map(loose, oid, &compat_oid);
974 cleanup:
975 strbuf_release(&tmp_file);
976 strbuf_release(&filename);
977 return err;
978 }
979
980 static int odb_source_loose_begin_transaction(struct odb_source *source UNUSED,
981 struct odb_transaction **out UNUSED,
982 enum odb_transaction_flags flags UNUSED)
983 {
984 /* TODO: this is a known omission that we'll want to address eventually. */
985 return error("loose source does not support transactions");
986 }
987
988 static int odb_source_loose_read_alternates(struct odb_source *source UNUSED,
989 struct strvec *out UNUSED)
990 {
991 return 0;
992 }
993
994 static int odb_source_loose_write_alternate(struct odb_source *source UNUSED,
995 const char *alternate UNUSED)
996 {
997 return error("loose source does not support alternates");
998 }
999
1000 static void odb_source_loose_clear_cache(struct odb_source_loose *loose)
1001 {
1002 oidtree_clear(loose->cache);
1003 FREE_AND_NULL(loose->cache);
1004 memset(&loose->subdir_seen, 0,
1005 sizeof(loose->subdir_seen));
1006 }
1007
1008 static void odb_source_loose_prepare(struct odb_source *source,
1009 enum odb_prepare_flags flags)
1010 {
1011 struct odb_source_loose *loose = odb_source_loose_downcast(source);
1012 if (flags & ODB_PREPARE_FLUSH_CACHES)
1013 odb_source_loose_clear_cache(loose);
1014 }
1015
1016 static void odb_source_loose_close(struct odb_source *source UNUSED)
1017 {
1018 /* Nothing to do. */
1019 }
1020
1021 static void odb_source_loose_reparent(const char *old_cwd,
1022 const char *new_cwd,
1023 void *cb_data)
1024 {
1025 struct odb_source_loose *loose = cb_data;
1026 char *path = reparent_relative_path(old_cwd, new_cwd,
1027 loose->base.path);
1028 free(loose->base.path);
1029 loose->base.path = path;
1030 }
1031
1032 static void odb_source_loose_free(struct odb_source *source)
1033 {
1034 struct odb_source_loose *loose = odb_source_loose_downcast(source);
1035 odb_source_loose_clear_cache(loose);
1036 loose_object_map_clear(&loose->map);
1037 chdir_notify_unregister(odb_source_loose_reparent, loose);
1038 odb_source_release(&loose->base);
1039 free(loose);
1040 }
1041
1042 struct fsck_loose_data {
1043 struct odb_source_loose *source;
1044 struct odb_fsck_options *opts;
1045 struct progress *progress;
1046 bool error_found;
1047 };
1048
1049 static int fsck_loose(const struct object_id *oid, const char *path,
1050 void *cb_data)
1051 {
1052 struct fsck_loose_data *data = cb_data;
1053 enum object_type type = OBJ_NONE;
1054 size_t size;
1055 void *contents = NULL;
1056 int eaten = 0;
1057 struct object_info oi = OBJECT_INFO_INIT;
1058 struct object_id real_oid = *null_oid(data->source->base.odb->repo->hash_algo);
1059 int err = 0;
1060
1061 oi.sizep = &size;
1062 oi.typep = &type;
1063
1064 if (read_loose_object(data->source->base.odb->repo,
1065 path, oid, &real_oid, &contents, &oi) < 0) {
1066 if (contents && !oideq(&real_oid, oid))
1067 err = error(_("%s: hash-path mismatch, found at: %s"),
1068 oid_to_hex(&real_oid), path);
1069 else
1070 err = error(_("%s: object corrupt or missing: %s"),
1071 oid_to_hex(oid), path);
1072 }
1073 if (err < 0)
1074 goto out;
1075
1076 if (!contents && type != OBJ_BLOB)
1077 BUG("read_loose_object streamed a non-blob");
1078
1079 if (data->opts->object_cb(oid, type, size, contents, &eaten,
1080 data->opts->object_payload)) {
1081 err = -1;
1082 goto out;
1083 }
1084
1085 out:
1086 if (err)
1087 data->error_found = true;
1088 if (!eaten)
1089 free(contents);
1090 return 0; /* keep checking other objects, even if we saw an error */
1091 }
1092
1093 static int fsck_cruft(const char *basename, const char *path,
1094 void *data UNUSED)
1095 {
1096 if (!starts_with(basename, "tmp_obj_"))
1097 fprintf_ln(stderr, _("bad sha1 file: %s"), path);
1098 return 0;
1099 }
1100
1101 static int fsck_subdir(unsigned int nr, const char *path UNUSED, void *cb_data)
1102 {
1103 struct fsck_loose_data *data = cb_data;
1104 display_progress(data->progress, nr + 1);
1105 return 0;
1106 }
1107
1108 static int odb_source_loose_fsck(struct odb_source *source,
1109 struct odb_fsck_options *opts)
1110 {
1111 struct odb_source_loose *loose = odb_source_loose_downcast(source);
1112 struct fsck_loose_data data = {
1113 .source = loose,
1114 .opts = opts,
1115 };
1116
1117 if (opts->flags & ODB_FSCK_VERBOSE)
1118 fprintf_ln(stderr, _("Checking object directory"));
1119
1120 if (opts->flags & ODB_FSCK_PROGRESS)
1121 data.progress = start_progress(source->odb->repo,
1122 _("Checking object directories"), 256);
1123
1124 for_each_loose_file_in_source(source, fsck_loose,
1125 fsck_cruft, fsck_subdir, &data);
1126 display_progress(data.progress, 256);
1127 stop_progress(&data.progress);
1128
1129 return data.error_found ? -1 : 0;
1130 }
1131
1132 struct odb_source_loose *odb_source_loose_new(struct object_database *odb,
1133 const char *path,
1134 bool local)
1135 {
1136 struct odb_source_loose *loose;
1137
1138 CALLOC_ARRAY(loose, 1);
1139 odb_source_init(&loose->base, odb, ODB_SOURCE_LOOSE, path, local);
1140
1141 loose->base.free = odb_source_loose_free;
1142 loose->base.close = odb_source_loose_close;
1143 loose->base.prepare = odb_source_loose_prepare;
1144 loose->base.fsck = odb_source_loose_fsck;
1145 loose->base.read_object_info = odb_source_loose_read_object_info;
1146 loose->base.read_object_stream = odb_source_loose_read_object_stream;
1147 loose->base.for_each_object = odb_source_loose_for_each_object;
1148 loose->base.find_abbrev_len = odb_source_loose_find_abbrev_len;
1149 loose->base.count_objects = odb_source_loose_count_objects;
1150 loose->base.freshen_object = odb_source_loose_freshen_object;
1151 loose->base.write_object = odb_source_loose_write_object;
1152 loose->base.write_object_stream = odb_source_loose_write_object_stream;
1153 loose->base.begin_transaction = odb_source_loose_begin_transaction;
1154 loose->base.read_alternates = odb_source_loose_read_alternates;
1155 loose->base.write_alternate = odb_source_loose_write_alternate;
1156
1157 if (!is_absolute_path(loose->base.path))
1158 chdir_notify_register(odb_source_loose_reparent, loose);
1159
1160 loose_object_map_load(loose);
1161
1162 return loose;
1163 }