at commit 0d5ebb32

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