2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
19 #include <sys/queue.h>
37 #include "got_error.h"
38 #include "got_object.h"
39 #include "got_repository.h"
40 #include "got_opentemp.h"
43 #include "got_lib_hash.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_inflate.h"
46 #include "got_lib_object.h"
47 #include "got_lib_object_idcache.h"
48 #include "got_lib_object_cache.h"
49 #include "got_lib_object_parse.h"
50 #include "got_lib_pack.h"
51 #include "got_lib_repository.h"
54 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
58 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
61 struct got_object_id *
62 got_object_get_id(struct got_object *obj)
67 const struct got_error *
68 got_object_get_id_str(char **outbuf, struct got_object *obj)
70 return got_object_id_str(outbuf, &obj->id);
73 const struct got_error *
74 got_object_get_type(int *type, struct got_repository *repo,
75 struct got_object_id *id)
77 const struct got_error *err = NULL;
78 struct got_object *obj;
80 err = got_object_open(&obj, repo, id);
85 case GOT_OBJ_TYPE_COMMIT:
86 case GOT_OBJ_TYPE_TREE:
87 case GOT_OBJ_TYPE_BLOB:
88 case GOT_OBJ_TYPE_TAG:
92 err = got_error(GOT_ERR_OBJ_TYPE);
96 got_object_close(obj);
100 const struct got_error *
101 got_object_get_path(char **path, struct got_object_id *id,
102 struct got_repository *repo)
104 const struct got_error *err = NULL;
110 path_objects = got_repo_get_path_objects(repo);
111 if (path_objects == NULL)
112 return got_error_from_errno("got_repo_get_path_objects");
114 err = got_object_id_str(&hex, id);
118 if (asprintf(path, "%s/%.2x/%s", path_objects,
119 id->hash[0], hex + 2) == -1)
120 err = got_error_from_errno("asprintf");
128 const struct got_error *
129 got_object_open_loose_fd(int *fd, struct got_object_id *id,
130 struct got_repository *repo)
132 const struct got_error *err = NULL;
135 err = got_object_get_path(&path, id, repo);
138 *fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
140 err = got_error_from_errno2("open", path);
148 const struct got_error *
149 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
152 struct got_object_id id;
154 memset(&id, 0, sizeof(id));
155 id.algo = got_repo_get_object_format(repo);
156 if (!got_parse_hash_digest(id.hash, id_str, id.algo))
157 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
159 return got_object_open(obj, repo, &id);
162 const struct got_error *
163 got_object_resolve_id_str(struct got_object_id **id,
164 struct got_repository *repo, const char *id_str)
166 const struct got_error *err = NULL;
167 struct got_object *obj;
169 err = got_object_open_by_id_str(&obj, repo, id_str);
173 *id = got_object_id_dup(got_object_get_id(obj));
174 got_object_close(obj);
176 return got_error_from_errno("got_object_id_dup");
181 const struct got_error *
182 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
184 *qid = calloc(1, sizeof(**qid));
186 return got_error_from_errno("calloc");
188 memcpy(&(*qid)->id, id, sizeof((*qid)->id));
192 const struct got_error *
193 got_object_id_queue_copy(const struct got_object_id_queue *src,
194 struct got_object_id_queue *dest)
196 const struct got_error *err;
197 struct got_object_qid *qid;
199 STAILQ_FOREACH(qid, src, entry) {
200 struct got_object_qid *new;
202 * Deep-copy the object ID only. Let the caller deal
203 * with setting up the new->data pointer if needed.
205 err = got_object_qid_alloc(&new, &qid->id);
207 got_object_id_queue_free(dest);
210 STAILQ_INSERT_TAIL(dest, new, entry);
217 got_object_tree_get_nentries(struct got_tree_object *tree)
219 return tree->nentries;
222 struct got_tree_entry *
223 got_object_tree_get_first_entry(struct got_tree_object *tree)
225 return got_object_tree_get_entry(tree, 0);
228 struct got_tree_entry *
229 got_object_tree_get_last_entry(struct got_tree_object *tree)
231 return got_object_tree_get_entry(tree, tree->nentries - 1);
234 struct got_tree_entry *
235 got_object_tree_get_entry(struct got_tree_object *tree, int i)
237 if (i < 0 || i >= tree->nentries)
239 return &tree->entries[i];
243 got_tree_entry_get_mode(struct got_tree_entry *te)
249 got_tree_entry_get_name(struct got_tree_entry *te)
254 struct got_object_id *
255 got_tree_entry_get_id(struct got_tree_entry *te)
260 const struct got_error *
261 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
263 const struct got_error *err = NULL;
264 size_t len, totlen, hdrlen, offset;
268 hdrlen = got_object_blob_get_hdrlen(blob);
274 err = got_object_blob_read_block(&len, blob);
281 totlen += len - hdrlen;
282 p = realloc(*s, totlen + 1);
284 err = got_error_from_errno("realloc");
290 /* Skip blob object header first time around. */
292 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
301 const struct got_error *
302 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
303 struct got_repository *repo)
305 const struct got_error *err = NULL;
306 struct got_blob_object *blob = NULL;
311 if (!got_object_tree_entry_is_symlink(te))
312 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
314 fd = got_opentempfd();
316 err = got_error_from_errno("got_opentempfd");
320 err = got_object_open_as_blob(&blob, repo,
321 got_tree_entry_get_id(te), PATH_MAX, fd);
325 err = got_object_blob_read_to_str(link_target, blob);
327 if (fd != -1 && close(fd) == -1 && err == NULL)
328 err = got_error_from_errno("close");
330 got_object_blob_close(blob);
339 got_tree_entry_get_index(struct got_tree_entry *te)
344 struct got_tree_entry *
345 got_tree_entry_get_next(struct got_tree_object *tree,
346 struct got_tree_entry *te)
348 return got_object_tree_get_entry(tree, te->idx + 1);
351 struct got_tree_entry *
352 got_tree_entry_get_prev(struct got_tree_object *tree,
353 struct got_tree_entry *te)
355 return got_object_tree_get_entry(tree, te->idx - 1);
358 const struct got_error *
359 got_object_blob_close(struct got_blob_object *blob)
361 const struct got_error *err = NULL;
362 free(blob->read_buf);
363 if (blob->f && fclose(blob->f) == EOF)
364 err = got_error_from_errno("fclose");
371 got_object_blob_rewind(struct got_blob_object *blob)
378 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
380 return got_object_id_hex(&blob->id, buf, size);
384 got_object_blob_get_hdrlen(struct got_blob_object *blob)
390 got_object_blob_get_read_buf(struct got_blob_object *blob)
392 return blob->read_buf;
395 const struct got_error *
396 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
400 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
401 if (n == 0 && ferror(blob->f))
402 return got_ferror(blob->f, GOT_ERR_IO);
407 const struct got_error *
408 got_object_blob_is_binary(int *binary, struct got_blob_object *blob)
410 const struct got_error *err;
414 hdrlen = got_object_blob_get_hdrlen(blob);
416 if (fseeko(blob->f, hdrlen, SEEK_SET) == -1)
417 return got_error_from_errno("fseeko");
419 err = got_object_blob_read_block(&len, blob);
423 *binary = memchr(blob->read_buf, '\0', len) != NULL;
425 if (fseeko(blob->f, hdrlen, SEEK_SET) == -1)
426 return got_error_from_errno("fseeko");
430 const struct got_error *
431 got_object_blob_getline(char **line, ssize_t *linelen, size_t *linesize,
432 struct got_blob_object *blob)
434 *linelen = getline(line, linesize, blob->f);
435 if (*linelen == -1 && !feof(blob->f))
436 return got_error_from_errno("getline");
440 const struct got_error *
441 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
442 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
444 const struct got_error *err = NULL;
445 size_t n, len, hdrlen;
448 const int alloc_chunksz = 512;
450 off_t off = 0, total_len = 0;
453 *line_offsets = NULL;
459 hdrlen = got_object_blob_get_hdrlen(blob);
461 err = got_object_blob_read_block(&len, blob);
466 buf = got_object_blob_get_read_buf(blob);
469 if (line_offsets && *line_offsets == NULL) {
470 /* Have some data but perhaps no '\n'. */
472 nalloc = alloc_chunksz;
473 *line_offsets = calloc(nalloc,
474 sizeof(**line_offsets));
475 if (*line_offsets == NULL)
476 return got_error_from_errno("calloc");
478 /* Skip forward over end of first line. */
485 /* Scan '\n' offsets in remaining chunk of data. */
487 if (buf[i] != '\n') {
492 if (line_offsets && nalloc < *nlines) {
493 size_t n = *nlines + alloc_chunksz;
494 off_t *o = recallocarray(*line_offsets,
495 nalloc, n, sizeof(**line_offsets));
498 *line_offsets = NULL;
499 return got_error_from_errno(
506 off = total_len + i - hdrlen + 1;
507 (*line_offsets)[*nlines - 1] = off;
512 /* Skip blob object header first time around. */
513 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
514 if (n != len - hdrlen)
515 return got_ferror(outfile, GOT_ERR_IO);
516 total_len += len - hdrlen;
520 if (fflush(outfile) != 0)
521 return got_error_from_errno("fflush");
525 *filesize = total_len;
531 got_object_tag_get_name(struct got_tag_object *tag)
537 got_object_tag_get_object_type(struct got_tag_object *tag)
539 return tag->obj_type;
542 struct got_object_id *
543 got_object_tag_get_object_id(struct got_tag_object *tag)
549 got_object_tag_get_tagger_time(struct got_tag_object *tag)
551 return tag->tagger_time;
555 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
557 return tag->tagger_gmtoff;
561 got_object_tag_get_tagger(struct got_tag_object *tag)
567 got_object_tag_get_message(struct got_tag_object *tag)
572 static struct got_tree_entry *
573 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
577 /* Note that tree entries are sorted in strncmp() order. */
578 for (i = 0; i < tree->nentries; i++) {
579 struct got_tree_entry *te = &tree->entries[i];
580 int cmp = strncmp(te->name, name, len);
585 if (te->name[len] == '\0')
591 struct got_tree_entry *
592 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
594 return find_entry_by_name(tree, name, strlen(name));
597 const struct got_error *
598 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
599 struct got_repository *repo, struct got_tree_object *tree,
602 const struct got_error *err = NULL;
603 struct got_tree_object *subtree = NULL;
604 struct got_tree_entry *te = NULL;
617 struct got_tree_object *next_tree;
626 te = find_entry_by_name(subtree, seg, seglen);
628 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
639 err = got_object_open_as_tree(&next_tree, repo,
645 got_object_tree_close(subtree);
651 *id = got_object_id_dup(&te->id);
653 return got_error_from_errno("got_object_id_dup");
657 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
659 if (subtree && subtree != tree)
660 got_object_tree_close(subtree);
664 const struct got_error *
665 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
666 struct got_commit_object *commit, const char *path)
668 const struct got_error *err = NULL;
669 struct got_tree_object *tree = NULL;
673 /* Handle opening of root of commit's tree. */
674 if (got_path_is_root_dir(path)) {
675 *id = got_object_id_dup(commit->tree_id);
677 err = got_error_from_errno("got_object_id_dup");
679 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
682 err = got_object_tree_find_path(id, NULL, repo, tree, path);
686 got_object_tree_close(tree);
691 * Normalize file mode bits to avoid false positive tree entry differences
692 * in case tree entries have unexpected mode bits set.
695 normalize_mode_for_comparison(mode_t mode)
698 * For directories, the only relevant bit is the IFDIR bit.
699 * This allows us to detect paths changing from a directory
700 * to a file and vice versa.
703 return mode & S_IFDIR;
706 * For symlinks, the only relevant bit is the IFLNK bit.
707 * This allows us to detect paths changing from a symlinks
708 * to a file or directory and vice versa.
711 return mode & S_IFLNK;
713 /* For files, the only change we care about is the executable bit. */
714 return mode & S_IXUSR;
717 const struct got_error *
718 got_object_tree_path_changed(int *changed,
719 struct got_tree_object *tree01, struct got_tree_object *tree02,
720 const char *path, struct got_repository *repo)
722 const struct got_error *err = NULL;
723 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
724 struct got_tree_entry *te1 = NULL, *te2 = NULL;
730 /* We not do support comparing the root path. */
731 if (got_path_is_root_dir(path))
732 return got_error_path(path, GOT_ERR_BAD_PATH);
742 struct got_tree_object *next_tree1, *next_tree2;
752 te1 = find_entry_by_name(tree1, seg, seglen);
754 err = got_error(GOT_ERR_NO_OBJ);
759 te2 = find_entry_by_name(tree2, seg, seglen);
762 mode1 = normalize_mode_for_comparison(te1->mode);
763 mode2 = normalize_mode_for_comparison(te2->mode);
764 if (mode1 != mode2) {
769 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
775 if (*s == '\0') { /* final path element */
784 err = got_object_open_as_tree(&next_tree1, repo,
790 got_object_tree_close(tree1);
794 err = got_object_open_as_tree(&next_tree2, repo,
800 got_object_tree_close(tree2);
804 got_object_tree_close(tree2);
810 if (tree1 && tree1 != tree01)
811 got_object_tree_close(tree1);
812 if (tree2 && tree2 != tree02)
813 got_object_tree_close(tree2);
817 const struct got_error *
818 got_object_tree_entry_dup(struct got_tree_entry **new_te,
819 struct got_tree_entry *te)
821 const struct got_error *err = NULL;
823 *new_te = calloc(1, sizeof(**new_te));
825 return got_error_from_errno("calloc");
827 (*new_te)->mode = te->mode;
828 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
829 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
834 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
836 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
840 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
842 /* S_IFDIR check avoids confusing symlinks with submodules. */
843 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
846 static const struct got_error *
847 resolve_symlink(char **link_target, const char *path,
848 struct got_commit_object *commit, struct got_repository *repo)
850 const struct got_error *err = NULL;
852 char *name, *parent_path = NULL;
853 struct got_object_id *tree_obj_id = NULL;
854 struct got_tree_object *tree = NULL;
855 struct got_tree_entry *te = NULL;
859 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
860 return got_error(GOT_ERR_NO_SPACE);
862 name = basename(buf);
864 return got_error_from_errno2("basename", path);
866 err = got_path_dirname(&parent_path, path);
870 err = got_object_id_by_path(&tree_obj_id, repo, commit,
873 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
874 /* Display the complete path in error message. */
875 err = got_error_path(path, err->code);
880 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
884 te = got_object_tree_find_entry(tree, name);
886 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
890 if (got_object_tree_entry_is_symlink(te)) {
891 err = got_tree_entry_get_symlink_target(link_target, te, repo);
894 if (!got_path_is_absolute(*link_target)) {
896 if (asprintf(&abspath, "%s/%s", parent_path,
897 *link_target) == -1) {
898 err = got_error_from_errno("asprintf");
902 *link_target = malloc(PATH_MAX);
903 if (*link_target == NULL) {
904 err = got_error_from_errno("malloc");
907 err = got_canonpath(abspath, *link_target, PATH_MAX);
917 got_object_tree_close(tree);
925 const struct got_error *
926 got_object_resolve_symlinks(char **link_target, const char *path,
927 struct got_commit_object *commit, struct got_repository *repo)
929 const struct got_error *err = NULL;
930 char *next_target = NULL;
931 int max_recursion = 40; /* matches Git */
936 err = resolve_symlink(&next_target,
937 *link_target ? *link_target : path, commit, repo);
942 if (--max_recursion == 0) {
943 err = got_error_path(path, GOT_ERR_RECURSION);
947 *link_target = next_target;
949 } while (next_target);
955 got_object_commit_retain(struct got_commit_object *commit)
960 const struct got_error *
961 got_object_raw_alloc(struct got_raw_object **obj, uint8_t *outbuf, int *outfd,
962 size_t max_in_mem_size, size_t hdrlen, off_t size)
964 const struct got_error *err = NULL;
969 *obj = calloc(1, sizeof(**obj));
971 err = got_error_from_errno("calloc");
975 (*obj)->tempfile_idx = -1;
978 (*obj)->data = outbuf;
981 if (fstat(*outfd, &sb) == -1) {
982 err = got_error_from_errno("fstat");
986 if (sb.st_size != tot) {
987 err = got_error_msg(GOT_ERR_BAD_OBJ_HDR,
988 "raw object has unexpected size");
991 #ifndef GOT_PACK_NO_MMAP
992 if (tot > 0 && tot <= max_in_mem_size) {
993 (*obj)->data = mmap(NULL, tot, PROT_READ,
994 MAP_PRIVATE, *outfd, 0);
995 if ((*obj)->data == MAP_FAILED) {
996 if (errno != ENOMEM) {
997 err = got_error_from_errno("mmap");
1000 (*obj)->data = NULL;
1002 (*obj)->fd = *outfd;
1008 (*obj)->f = fdopen(*outfd, "r");
1009 if ((*obj)->f == NULL) {
1010 err = got_error_from_errno("fdopen");
1016 (*obj)->hdrlen = hdrlen;
1017 (*obj)->size = size;
1021 got_object_raw_close(*obj);