Raw
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 "repository.h"
15 #include "strbuf.h"
16
17 static int append_loose_object(const struct object_id *oid,
18 const char *path UNUSED,
19 void *data)
20 {
21 oidtree_insert(data, oid, NULL);
22 return 0;
23 }
24
25 static struct oidtree *odb_source_loose_cache(struct odb_source_loose *loose,
26 const struct object_id *oid)
27 {
28 int subdir_nr = oid->hash[0];
29 struct strbuf buf = STRBUF_INIT;
30 size_t word_bits = bitsizeof(loose->subdir_seen[0]);
31 size_t word_index = subdir_nr / word_bits;
32 size_t mask = (size_t)1u << (subdir_nr % word_bits);
33 uint32_t *bitmap;
34
35 if (subdir_nr < 0 ||
36 (size_t) subdir_nr >= bitsizeof(loose->subdir_seen))
37 BUG("subdir_nr out of range");
38
39 bitmap = &loose->subdir_seen[word_index];
40 if (*bitmap & mask)
41 return loose->cache;
42 if (!loose->cache) {
43 ALLOC_ARRAY(loose->cache, 1);
44 oidtree_init(loose->cache);
45 }
46 strbuf_addstr(&buf, loose->base.path);
47 for_each_file_in_obj_subdir(subdir_nr, &buf,
48 loose->base.odb->repo->hash_algo,
49 append_loose_object,
50 NULL, NULL,
51 loose->cache);
52 *bitmap |= mask;
53 strbuf_release(&buf);
54 return loose->cache;
55 }
56
57 static int quick_has_loose(struct odb_source_loose *loose,
58 const struct object_id *oid)
59 {
60 return !!oidtree_contains(odb_source_loose_cache(loose, oid), oid);
61 }
62
63 static int read_object_info_from_path(struct odb_source_loose *loose,
64 const char *path,
65 const struct object_id *oid,
66 struct object_info *oi,
67 enum object_info_flags flags)
68 {
69 int ret;
70 int fd;
71 unsigned long mapsize;
72 void *map = NULL;
73 git_zstream stream, *stream_to_end = NULL;
74 char hdr[MAX_HEADER_LEN];
75 unsigned long size_scratch;
76 enum object_type type_scratch;
77 struct stat st;
78
79 /*
80 * If we don't care about type or size, then we don't
81 * need to look inside the object at all. Note that we
82 * do not optimize out the stat call, even if the
83 * caller doesn't care about the disk-size, since our
84 * return value implicitly indicates whether the
85 * object even exists.
86 */
87 if (!oi || (!oi->typep && !oi->sizep && !oi->contentp)) {
88 struct stat st;
89
90 if ((!oi || (!oi->disk_sizep && !oi->mtimep)) && (flags & OBJECT_INFO_QUICK)) {
91 ret = quick_has_loose(loose, oid) ? 0 : -1;
92 goto out;
93 }
94
95 if (lstat(path, &st) < 0) {
96 ret = -1;
97 goto out;
98 }
99
100 if (oi) {
101 if (oi->disk_sizep)
102 *oi->disk_sizep = st.st_size;
103 if (oi->mtimep)
104 *oi->mtimep = st.st_mtime;
105 }
106
107 ret = 0;
108 goto out;
109 }
110
111 fd = git_open(path);
112 if (fd < 0) {
113 if (errno != ENOENT)
114 error_errno(_("unable to open loose object %s"), oid_to_hex(oid));
115 ret = -1;
116 goto out;
117 }
118
119 if (fstat(fd, &st)) {
120 close(fd);
121 ret = -1;
122 goto out;
123 }
124
125 mapsize = xsize_t(st.st_size);
126 if (!mapsize) {
127 close(fd);
128 ret = error(_("object file %s is empty"), path);
129 goto out;
130 }
131
132 map = xmmap(NULL, mapsize, PROT_READ, MAP_PRIVATE, fd, 0);
133 close(fd);
134 if (!map) {
135 ret = -1;
136 goto out;
137 }
138
139 if (oi->disk_sizep)
140 *oi->disk_sizep = mapsize;
141 if (oi->mtimep)
142 *oi->mtimep = st.st_mtime;
143
144 stream_to_end = &stream;
145
146 switch (unpack_loose_header(&stream, map, mapsize, hdr, sizeof(hdr))) {
147 case ULHR_OK:
148 if (!oi->sizep)
149 oi->sizep = &size_scratch;
150 if (!oi->typep)
151 oi->typep = &type_scratch;
152
153 if (parse_loose_header(hdr, oi) < 0) {
154 ret = error(_("unable to parse %s header"), oid_to_hex(oid));
155 goto corrupt;
156 }
157
158 if (*oi->typep < 0)
159 die(_("invalid object type"));
160
161 if (oi->contentp) {
162 *oi->contentp = unpack_loose_rest(&stream, hdr, *oi->sizep, oid);
163 if (!*oi->contentp) {
164 ret = -1;
165 goto corrupt;
166 }
167 }
168
169 break;
170 case ULHR_BAD:
171 ret = error(_("unable to unpack %s header"),
172 oid_to_hex(oid));
173 goto corrupt;
174 case ULHR_TOO_LONG:
175 ret = error(_("header for %s too long, exceeds %d bytes"),
176 oid_to_hex(oid), MAX_HEADER_LEN);
177 goto corrupt;
178 }
179
180 ret = 0;
181
182 corrupt:
183 if (ret && (flags & OBJECT_INFO_DIE_IF_CORRUPT))
184 die(_("loose object %s (stored in %s) is corrupt"),
185 oid_to_hex(oid), path);
186
187 out:
188 if (stream_to_end)
189 git_inflate_end(stream_to_end);
190 if (map)
191 munmap(map, mapsize);
192 if (oi) {
193 if (oi->sizep == &size_scratch)
194 oi->sizep = NULL;
195 if (oi->typep == &type_scratch)
196 oi->typep = NULL;
197 if (oi->delta_base_oid)
198 oidclr(oi->delta_base_oid, loose->base.odb->repo->hash_algo);
199 if (!ret)
200 oi->whence = OI_LOOSE;
201 }
202
203 return ret;
204 }
205
206 static int odb_source_loose_read_object_info(struct odb_source *source,
207 const struct object_id *oid,
208 struct object_info *oi,
209 enum object_info_flags flags)
210 {
211 struct odb_source_loose *loose = odb_source_loose_downcast(source);
212 static struct strbuf buf = STRBUF_INIT;
213
214 /*
215 * The second read shouldn't cause new loose objects to show up, unless
216 * there was a race condition with a secondary process. We don't care
217 * about this case though, so we simply skip reading loose objects a
218 * second time.
219 */
220 if (flags & OBJECT_INFO_SECOND_READ)
221 return -1;
222
223 odb_loose_path(loose, &buf, oid);
224 return read_object_info_from_path(loose, buf.buf, oid, oi, flags);
225 }
226
227 /*
228 * Find "oid" as a loose object in given source, open the object and return its
229 * file descriptor. Returns the file descriptor on success, negative on failure.
230 *
231 * The "path" out-parameter will give the path of the object we found (if any).
232 * Note that it may point to static storage and is only valid until another
233 * call to open_loose_object().
234 */
235 static int open_loose_object(struct odb_source_loose *loose,
236 const struct object_id *oid, const char **path)
237 {
238 static struct strbuf buf = STRBUF_INIT;
239 int fd;
240
241 *path = odb_loose_path(loose, &buf, oid);
242 fd = git_open(*path);
243 if (fd >= 0)
244 return fd;
245
246 return -1;
247 }
248
249 static void *odb_source_loose_map_object(struct odb_source_loose *loose,
250 const struct object_id *oid,
251 unsigned long *size)
252 {
253 const char *p;
254 int fd = open_loose_object(loose, oid, &p);
255 void *map = NULL;
256 struct stat st;
257
258 if (fd < 0)
259 return NULL;
260
261 if (!fstat(fd, &st)) {
262 *size = xsize_t(st.st_size);
263 if (!*size) {
264 /* mmap() is forbidden on empty files */
265 error(_("object file %s is empty"), p);
266 goto out;
267 }
268
269 map = xmmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0);
270 }
271
272 out:
273 close(fd);
274 return map;
275 }
276
277 struct odb_loose_read_stream {
278 struct odb_read_stream base;
279 git_zstream z;
280 enum {
281 ODB_LOOSE_READ_STREAM_INUSE,
282 ODB_LOOSE_READ_STREAM_DONE,
283 ODB_LOOSE_READ_STREAM_ERROR,
284 } z_state;
285 void *mapped;
286 unsigned long mapsize;
287 char hdr[32];
288 int hdr_avail;
289 int hdr_used;
290 };
291
292 static ssize_t read_istream_loose(struct odb_read_stream *_st, char *buf, size_t sz)
293 {
294 struct odb_loose_read_stream *st =
295 container_of(_st, struct odb_loose_read_stream, base);
296 size_t total_read = 0;
297
298 switch (st->z_state) {
299 case ODB_LOOSE_READ_STREAM_DONE:
300 return 0;
301 case ODB_LOOSE_READ_STREAM_ERROR:
302 return -1;
303 default:
304 break;
305 }
306
307 if (st->hdr_used < st->hdr_avail) {
308 size_t to_copy = st->hdr_avail - st->hdr_used;
309 if (sz < to_copy)
310 to_copy = sz;
311 memcpy(buf, st->hdr + st->hdr_used, to_copy);
312 st->hdr_used += to_copy;
313 total_read += to_copy;
314 }
315
316 while (total_read < sz) {
317 int status;
318
319 st->z.next_out = (unsigned char *)buf + total_read;
320 st->z.avail_out = sz - total_read;
321 status = git_inflate(&st->z, Z_FINISH);
322
323 total_read = st->z.next_out - (unsigned char *)buf;
324
325 if (status == Z_STREAM_END) {
326 git_inflate_end(&st->z);
327 st->z_state = ODB_LOOSE_READ_STREAM_DONE;
328 break;
329 }
330 if (status != Z_OK && (status != Z_BUF_ERROR || total_read < sz)) {
331 git_inflate_end(&st->z);
332 st->z_state = ODB_LOOSE_READ_STREAM_ERROR;
333 return -1;
334 }
335 }
336 return total_read;
337 }
338
339 static int close_istream_loose(struct odb_read_stream *_st)
340 {
341 struct odb_loose_read_stream *st =
342 container_of(_st, struct odb_loose_read_stream, base);
343
344 if (st->z_state == ODB_LOOSE_READ_STREAM_INUSE)
345 git_inflate_end(&st->z);
346 munmap(st->mapped, st->mapsize);
347 return 0;
348 }
349
350 static int odb_source_loose_read_object_stream(struct odb_read_stream **out,
351 struct odb_source *source,
352 const struct object_id *oid)
353 {
354 struct odb_source_loose *loose = odb_source_loose_downcast(source);
355 struct object_info oi = OBJECT_INFO_INIT;
356 struct odb_loose_read_stream *st;
357 unsigned long mapsize;
358 unsigned long size_ul;
359 void *mapped;
360
361 mapped = odb_source_loose_map_object(loose, oid, &mapsize);
362 if (!mapped)
363 return -1;
364
365 /*
366 * Note: we must allocate this structure early even though we may still
367 * fail. This is because we need to initialize the zlib stream, and it
368 * is not possible to copy the stream around after the fact because it
369 * has self-referencing pointers.
370 */
371 CALLOC_ARRAY(st, 1);
372
373 switch (unpack_loose_header(&st->z, mapped, mapsize, st->hdr,
374 sizeof(st->hdr))) {
375 case ULHR_OK:
376 break;
377 case ULHR_BAD:
378 case ULHR_TOO_LONG:
379 goto error;
380 }
381
382 /*
383 * object_info.sizep is unsigned long* (32-bit on Windows), but
384 * st->base.size is size_t (64-bit). Use temporary variable.
385 * Note: loose objects >4GB would still truncate here, but such
386 * large loose objects are uncommon (they'd normally be packed).
387 */
388 oi.sizep = &size_ul;
389 oi.typep = &st->base.type;
390
391 if (parse_loose_header(st->hdr, &oi) < 0 || st->base.type < 0)
392 goto error;
393 st->base.size = size_ul;
394
395 st->mapped = mapped;
396 st->mapsize = mapsize;
397 st->hdr_used = strlen(st->hdr) + 1;
398 st->hdr_avail = st->z.total_out;
399 st->z_state = ODB_LOOSE_READ_STREAM_INUSE;
400 st->base.close = close_istream_loose;
401 st->base.read = read_istream_loose;
402
403 *out = &st->base;
404
405 return 0;
406 error:
407 git_inflate_end(&st->z);
408 munmap(mapped, mapsize);
409 free(st);
410 return -1;
411 }
412
413 struct for_each_object_wrapper_data {
414 struct odb_source_loose *loose;
415 const struct object_info *request;
416 odb_for_each_object_cb cb;
417 void *cb_data;
418 };
419
420 static int for_each_object_wrapper_cb(const struct object_id *oid,
421 const char *path,
422 void *cb_data)
423 {
424 struct for_each_object_wrapper_data *data = cb_data;
425
426 if (data->request) {
427 struct object_info oi = *data->request;
428
429 if (read_object_info_from_path(data->loose, path, oid, &oi, 0) < 0)
430 return -1;
431
432 return data->cb(oid, &oi, data->cb_data);
433 } else {
434 return data->cb(oid, NULL, data->cb_data);
435 }
436 }
437
438 static int for_each_prefixed_object_wrapper_cb(const struct object_id *oid,
439 void *node_data UNUSED,
440 void *cb_data)
441 {
442 struct for_each_object_wrapper_data *data = cb_data;
443 if (data->request) {
444 struct object_info oi = *data->request;
445
446 if (odb_source_read_object_info(&data->loose->base,
447 oid, &oi, 0) < 0)
448 return -1;
449
450 return data->cb(oid, &oi, data->cb_data);
451 } else {
452 return data->cb(oid, NULL, data->cb_data);
453 }
454 }
455
456 static int odb_source_loose_for_each_object(struct odb_source *source,
457 const struct object_info *request,
458 odb_for_each_object_cb cb,
459 void *cb_data,
460 const struct odb_for_each_object_options *opts)
461 {
462 struct odb_source_loose *loose = odb_source_loose_downcast(source);
463 struct for_each_object_wrapper_data data = {
464 .loose = loose,
465 .request = request,
466 .cb = cb,
467 .cb_data = cb_data,
468 };
469
470 /* There are no loose promisor objects, so we can return immediately. */
471 if ((opts->flags & ODB_FOR_EACH_OBJECT_PROMISOR_ONLY))
472 return 0;
473 if ((opts->flags & ODB_FOR_EACH_OBJECT_LOCAL_ONLY) && !source->local)
474 return 0;
475
476 if (opts->prefix)
477 return oidtree_each(odb_source_loose_cache(loose, opts->prefix),
478 opts->prefix, opts->prefix_hex_len,
479 for_each_prefixed_object_wrapper_cb, &data);
480
481 return for_each_loose_file_in_source(source, for_each_object_wrapper_cb,
482 NULL, NULL, &data);
483 }
484
485 struct find_abbrev_len_data {
486 const struct object_id *oid;
487 unsigned len;
488 };
489
490 static int find_abbrev_len_cb(const struct object_id *oid,
491 struct object_info *oi UNUSED,
492 void *cb_data)
493 {
494 struct find_abbrev_len_data *data = cb_data;
495 unsigned len = oid_common_prefix_hexlen(oid, data->oid);
496 if (len != hash_algos[oid->algo].hexsz && len >= data->len)
497 data->len = len + 1;
498 return 0;
499 }
500
501 static int odb_source_loose_find_abbrev_len(struct odb_source *source,
502 const struct object_id *oid,
503 unsigned min_len,
504 unsigned *out)
505 {
506 struct odb_source_loose *loose = odb_source_loose_downcast(source);
507 struct odb_for_each_object_options opts = {
508 .prefix = oid,
509 .prefix_hex_len = min_len,
510 };
511 struct find_abbrev_len_data data = {
512 .oid = oid,
513 .len = min_len,
514 };
515 int ret;
516
517 ret = odb_source_for_each_object(&loose->base, NULL, find_abbrev_len_cb,
518 &data, &opts);
519 *out = data.len;
520
521 return ret;
522 }
523
524 static int count_loose_object(const struct object_id *oid UNUSED,
525 struct object_info *oi UNUSED,
526 void *payload)
527 {
528 unsigned long *count = payload;
529 (*count)++;
530 return 0;
531 }
532
533 static int odb_source_loose_count_objects(struct odb_source *source,
534 enum odb_count_objects_flags flags,
535 unsigned long *out)
536 {
537 struct odb_source_loose *loose = odb_source_loose_downcast(source);
538 const unsigned hexsz = source->odb->repo->hash_algo->hexsz - 2;
539 char *path = NULL;
540 DIR *dir = NULL;
541 int ret;
542
543 if (flags & ODB_COUNT_OBJECTS_APPROXIMATE) {
544 unsigned long count = 0;
545 struct dirent *ent;
546
547 path = xstrfmt("%s/17", source->path);
548
549 dir = opendir(path);
550 if (!dir) {
551 if (errno == ENOENT) {
552 *out = 0;
553 ret = 0;
554 goto out;
555 }
556
557 ret = error_errno("cannot open object shard '%s'", path);
558 goto out;
559 }
560
561 while ((ent = readdir(dir)) != NULL) {
562 if (strspn(ent->d_name, "0123456789abcdef") != hexsz ||
563 ent->d_name[hexsz] != '\0')
564 continue;
565 count++;
566 }
567
568 *out = count * 256;
569 ret = 0;
570 } else {
571 struct odb_for_each_object_options opts = { 0 };
572 *out = 0;
573 ret = odb_source_for_each_object(&loose->base, NULL, count_loose_object,
574 out, &opts);
575 }
576
577 out:
578 if (dir)
579 closedir(dir);
580 free(path);
581 return ret;
582 }
583
584 static int odb_source_loose_freshen_object(struct odb_source *source,
585 const struct object_id *oid)
586 {
587 struct odb_source_loose *loose = odb_source_loose_downcast(source);
588 static struct strbuf path = STRBUF_INIT;
589 odb_loose_path(loose, &path, oid);
590 return !!check_and_freshen_file(path.buf, 1);
591 }
592
593 static int odb_source_loose_write_object(struct odb_source *source,
594 const void *buf, unsigned long len,
595 enum object_type type, struct object_id *oid,
596 struct object_id *compat_oid_in,
597 enum odb_write_object_flags flags)
598 {
599 struct odb_source_loose *loose = odb_source_loose_downcast(source);
600 const struct git_hash_algo *algo = source->odb->repo->hash_algo;
601 const struct git_hash_algo *compat = source->odb->repo->compat_hash_algo;
602 struct object_id compat_oid;
603 char hdr[MAX_HEADER_LEN];
604 int hdrlen = sizeof(hdr);
605
606 /* Generate compat_oid */
607 if (compat) {
608 if (compat_oid_in)
609 oidcpy(&compat_oid, compat_oid_in);
610 else if (type == OBJ_BLOB)
611 hash_object_file(compat, buf, len, type, &compat_oid);
612 else {
613 struct strbuf converted = STRBUF_INIT;
614 convert_object_file(source->odb->repo, &converted, algo, compat,
615 buf, len, type, 0);
616 hash_object_file(compat, converted.buf, converted.len,
617 type, &compat_oid);
618 strbuf_release(&converted);
619 }
620 }
621
622 /* Normally if we have it in the pack then we do not bother writing
623 * it out into .git/objects/??/?{38} file.
624 */
625 write_object_file_prepare(algo, buf, len, type, oid, hdr, &hdrlen);
626 if (odb_freshen_object(source->odb, oid))
627 return 0;
628 if (write_loose_object(loose, oid, hdr, hdrlen, buf, len, 0, flags))
629 return -1;
630 if (compat)
631 return repo_add_loose_object_map(loose, oid, &compat_oid);
632 return 0;
633 }
634
635 static int odb_source_loose_write_object_stream(struct odb_source *source,
636 struct odb_write_stream *in_stream,
637 size_t len,
638 struct object_id *oid)
639 {
640 /*
641 * TODO: the implementation should be moved here, see the comment on
642 * the called function in "object-file.h".
643 */
644 struct odb_source_loose *loose = odb_source_loose_downcast(source);
645 return odb_source_loose_write_stream(loose, in_stream, len, oid);
646 }
647
648 static int odb_source_loose_begin_transaction(struct odb_source *source UNUSED,
649 struct odb_transaction **out UNUSED)
650 {
651 /* TODO: this is a known omission that we'll want to address eventually. */
652 return error("loose source does not support transactions");
653 }
654
655 static int odb_source_loose_read_alternates(struct odb_source *source UNUSED,
656 struct strvec *out UNUSED)
657 {
658 return 0;
659 }
660
661 static int odb_source_loose_write_alternate(struct odb_source *source UNUSED,
662 const char *alternate UNUSED)
663 {
664 return error("loose source does not support alternates");
665 }
666
667 static void odb_source_loose_clear_cache(struct odb_source_loose *loose)
668 {
669 oidtree_clear(loose->cache);
670 FREE_AND_NULL(loose->cache);
671 memset(&loose->subdir_seen, 0,
672 sizeof(loose->subdir_seen));
673 }
674
675 static void odb_source_loose_reprepare(struct odb_source *source)
676 {
677 struct odb_source_loose *loose = odb_source_loose_downcast(source);
678 odb_source_loose_clear_cache(loose);
679 }
680
681 static void odb_source_loose_close(struct odb_source *source UNUSED)
682 {
683 /* Nothing to do. */
684 }
685
686 static void odb_source_loose_reparent(const char *name UNUSED,
687 const char *old_cwd,
688 const char *new_cwd,
689 void *cb_data)
690 {
691 struct odb_source_loose *loose = cb_data;
692 char *path = reparent_relative_path(old_cwd, new_cwd,
693 loose->base.path);
694 free(loose->base.path);
695 loose->base.path = path;
696 }
697
698 static void odb_source_loose_free(struct odb_source *source)
699 {
700 struct odb_source_loose *loose = odb_source_loose_downcast(source);
701 odb_source_loose_clear_cache(loose);
702 loose_object_map_clear(&loose->map);
703 chdir_notify_unregister(NULL, odb_source_loose_reparent, loose);
704 odb_source_release(&loose->base);
705 free(loose);
706 }
707
708 struct odb_source_loose *odb_source_loose_new(struct object_database *odb,
709 const char *path,
710 bool local)
711 {
712 struct odb_source_loose *loose;
713
714 CALLOC_ARRAY(loose, 1);
715 odb_source_init(&loose->base, odb, ODB_SOURCE_LOOSE, path, local);
716
717 loose->base.free = odb_source_loose_free;
718 loose->base.close = odb_source_loose_close;
719 loose->base.reprepare = odb_source_loose_reprepare;
720 loose->base.read_object_info = odb_source_loose_read_object_info;
721 loose->base.read_object_stream = odb_source_loose_read_object_stream;
722 loose->base.for_each_object = odb_source_loose_for_each_object;
723 loose->base.find_abbrev_len = odb_source_loose_find_abbrev_len;
724 loose->base.count_objects = odb_source_loose_count_objects;
725 loose->base.freshen_object = odb_source_loose_freshen_object;
726 loose->base.write_object = odb_source_loose_write_object;
727 loose->base.write_object_stream = odb_source_loose_write_object_stream;
728 loose->base.begin_transaction = odb_source_loose_begin_transaction;
729 loose->base.read_alternates = odb_source_loose_read_alternates;
730 loose->base.write_alternate = odb_source_loose_write_alternate;
731
732 if (!is_absolute_path(loose->base.path))
733 chdir_notify_register(NULL, odb_source_loose_reparent, loose);
734
735 return loose;
736 }