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 = { .algo = repo->algo };
154 if (repo->algo == GOT_HASH_SHA256) {
155 if (!got_parse_sha256_digest(id.hash, id_str))
156 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
157 } else if (repo->algo == GOT_HASH_SHA1) {
158 if (!got_parse_sha1_digest(id.hash, id_str))
159 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
161 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
163 return got_object_open(obj, repo, &id);
166 const struct got_error *
167 got_object_resolve_id_str(struct got_object_id **id,
168 struct got_repository *repo, const char *id_str)
170 const struct got_error *err = NULL;
171 struct got_object *obj;
173 err = got_object_open_by_id_str(&obj, repo, id_str);
177 *id = got_object_id_dup(got_object_get_id(obj));
178 got_object_close(obj);
180 return got_error_from_errno("got_object_id_dup");
185 const struct got_error *
186 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
188 *qid = calloc(1, sizeof(**qid));
190 return got_error_from_errno("calloc");
192 memcpy(&(*qid)->id, id, sizeof((*qid)->id));
196 const struct got_error *
197 got_object_id_queue_copy(const struct got_object_id_queue *src,
198 struct got_object_id_queue *dest)
200 const struct got_error *err;
201 struct got_object_qid *qid;
203 STAILQ_FOREACH(qid, src, entry) {
204 struct got_object_qid *new;
206 * Deep-copy the object ID only. Let the caller deal
207 * with setting up the new->data pointer if needed.
209 err = got_object_qid_alloc(&new, &qid->id);
211 got_object_id_queue_free(dest);
214 STAILQ_INSERT_TAIL(dest, new, entry);
221 got_object_tree_get_nentries(struct got_tree_object *tree)
223 return tree->nentries;
226 struct got_tree_entry *
227 got_object_tree_get_first_entry(struct got_tree_object *tree)
229 return got_object_tree_get_entry(tree, 0);
232 struct got_tree_entry *
233 got_object_tree_get_last_entry(struct got_tree_object *tree)
235 return got_object_tree_get_entry(tree, tree->nentries - 1);
238 struct got_tree_entry *
239 got_object_tree_get_entry(struct got_tree_object *tree, int i)
241 if (i < 0 || i >= tree->nentries)
243 return &tree->entries[i];
247 got_tree_entry_get_mode(struct got_tree_entry *te)
253 got_tree_entry_get_name(struct got_tree_entry *te)
258 struct got_object_id *
259 got_tree_entry_get_id(struct got_tree_entry *te)
264 const struct got_error *
265 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
267 const struct got_error *err = NULL;
268 size_t len, totlen, hdrlen, offset;
272 hdrlen = got_object_blob_get_hdrlen(blob);
278 err = got_object_blob_read_block(&len, blob);
285 totlen += len - hdrlen;
286 p = realloc(*s, totlen + 1);
288 err = got_error_from_errno("realloc");
294 /* Skip blob object header first time around. */
296 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
305 const struct got_error *
306 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
307 struct got_repository *repo)
309 const struct got_error *err = NULL;
310 struct got_blob_object *blob = NULL;
315 if (!got_object_tree_entry_is_symlink(te))
316 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
318 fd = got_opentempfd();
320 err = got_error_from_errno("got_opentempfd");
324 err = got_object_open_as_blob(&blob, repo,
325 got_tree_entry_get_id(te), PATH_MAX, fd);
329 err = got_object_blob_read_to_str(link_target, blob);
331 if (fd != -1 && close(fd) == -1 && err == NULL)
332 err = got_error_from_errno("close");
334 got_object_blob_close(blob);
343 got_tree_entry_get_index(struct got_tree_entry *te)
348 struct got_tree_entry *
349 got_tree_entry_get_next(struct got_tree_object *tree,
350 struct got_tree_entry *te)
352 return got_object_tree_get_entry(tree, te->idx + 1);
355 struct got_tree_entry *
356 got_tree_entry_get_prev(struct got_tree_object *tree,
357 struct got_tree_entry *te)
359 return got_object_tree_get_entry(tree, te->idx - 1);
362 const struct got_error *
363 got_object_blob_close(struct got_blob_object *blob)
365 const struct got_error *err = NULL;
366 free(blob->read_buf);
367 if (blob->f && fclose(blob->f) == EOF)
368 err = got_error_from_errno("fclose");
375 got_object_blob_rewind(struct got_blob_object *blob)
382 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
384 return got_object_id_hex(&blob->id, buf, size);
388 got_object_blob_get_hdrlen(struct got_blob_object *blob)
394 got_object_blob_get_read_buf(struct got_blob_object *blob)
396 return blob->read_buf;
399 const struct got_error *
400 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
404 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
405 if (n == 0 && ferror(blob->f))
406 return got_ferror(blob->f, GOT_ERR_IO);
411 const struct got_error *
412 got_object_blob_is_binary(int *binary, struct got_blob_object *blob)
414 const struct got_error *err;
418 hdrlen = got_object_blob_get_hdrlen(blob);
420 if (fseeko(blob->f, hdrlen, SEEK_SET) == -1)
421 return got_error_from_errno("fseeko");
423 err = got_object_blob_read_block(&len, blob);
427 *binary = memchr(blob->read_buf, '\0', len) != NULL;
429 if (fseeko(blob->f, hdrlen, SEEK_SET) == -1)
430 return got_error_from_errno("fseeko");
434 const struct got_error *
435 got_object_blob_getline(char **line, ssize_t *linelen, size_t *linesize,
436 struct got_blob_object *blob)
438 *linelen = getline(line, linesize, blob->f);
439 if (*linelen == -1 && !feof(blob->f))
440 return got_error_from_errno("getline");
444 const struct got_error *
445 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
446 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
448 const struct got_error *err = NULL;
449 size_t n, len, hdrlen;
452 const int alloc_chunksz = 512;
454 off_t off = 0, total_len = 0;
457 *line_offsets = NULL;
463 hdrlen = got_object_blob_get_hdrlen(blob);
465 err = got_object_blob_read_block(&len, blob);
470 buf = got_object_blob_get_read_buf(blob);
473 if (line_offsets && *line_offsets == NULL) {
474 /* Have some data but perhaps no '\n'. */
476 nalloc = alloc_chunksz;
477 *line_offsets = calloc(nalloc,
478 sizeof(**line_offsets));
479 if (*line_offsets == NULL)
480 return got_error_from_errno("calloc");
482 /* Skip forward over end of first line. */
489 /* Scan '\n' offsets in remaining chunk of data. */
491 if (buf[i] != '\n') {
496 if (line_offsets && nalloc < *nlines) {
497 size_t n = *nlines + alloc_chunksz;
498 off_t *o = recallocarray(*line_offsets,
499 nalloc, n, sizeof(**line_offsets));
502 *line_offsets = NULL;
503 return got_error_from_errno(
510 off = total_len + i - hdrlen + 1;
511 (*line_offsets)[*nlines - 1] = off;
516 /* Skip blob object header first time around. */
517 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
518 if (n != len - hdrlen)
519 return got_ferror(outfile, GOT_ERR_IO);
520 total_len += len - hdrlen;
524 if (fflush(outfile) != 0)
525 return got_error_from_errno("fflush");
529 *filesize = total_len;
535 got_object_tag_get_name(struct got_tag_object *tag)
541 got_object_tag_get_object_type(struct got_tag_object *tag)
543 return tag->obj_type;
546 struct got_object_id *
547 got_object_tag_get_object_id(struct got_tag_object *tag)
553 got_object_tag_get_tagger_time(struct got_tag_object *tag)
555 return tag->tagger_time;
559 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
561 return tag->tagger_gmtoff;
565 got_object_tag_get_tagger(struct got_tag_object *tag)
571 got_object_tag_get_message(struct got_tag_object *tag)
576 static struct got_tree_entry *
577 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
581 /* Note that tree entries are sorted in strncmp() order. */
582 for (i = 0; i < tree->nentries; i++) {
583 struct got_tree_entry *te = &tree->entries[i];
584 int cmp = strncmp(te->name, name, len);
589 if (te->name[len] == '\0')
595 struct got_tree_entry *
596 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
598 return find_entry_by_name(tree, name, strlen(name));
601 const struct got_error *
602 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
603 struct got_repository *repo, struct got_tree_object *tree,
606 const struct got_error *err = NULL;
607 struct got_tree_object *subtree = NULL;
608 struct got_tree_entry *te = NULL;
621 struct got_tree_object *next_tree;
630 te = find_entry_by_name(subtree, seg, seglen);
632 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
643 err = got_object_open_as_tree(&next_tree, repo,
649 got_object_tree_close(subtree);
655 *id = got_object_id_dup(&te->id);
657 return got_error_from_errno("got_object_id_dup");
661 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
663 if (subtree && subtree != tree)
664 got_object_tree_close(subtree);
668 const struct got_error *
669 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
670 struct got_commit_object *commit, const char *path)
672 const struct got_error *err = NULL;
673 struct got_tree_object *tree = NULL;
677 /* Handle opening of root of commit's tree. */
678 if (got_path_is_root_dir(path)) {
679 *id = got_object_id_dup(commit->tree_id);
681 err = got_error_from_errno("got_object_id_dup");
683 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
686 err = got_object_tree_find_path(id, NULL, repo, tree, path);
690 got_object_tree_close(tree);
695 * Normalize file mode bits to avoid false positive tree entry differences
696 * in case tree entries have unexpected mode bits set.
699 normalize_mode_for_comparison(mode_t mode)
702 * For directories, the only relevant bit is the IFDIR bit.
703 * This allows us to detect paths changing from a directory
704 * to a file and vice versa.
707 return mode & S_IFDIR;
710 * For symlinks, the only relevant bit is the IFLNK bit.
711 * This allows us to detect paths changing from a symlinks
712 * to a file or directory and vice versa.
715 return mode & S_IFLNK;
717 /* For files, the only change we care about is the executable bit. */
718 return mode & S_IXUSR;
721 const struct got_error *
722 got_object_tree_path_changed(int *changed,
723 struct got_tree_object *tree01, struct got_tree_object *tree02,
724 const char *path, struct got_repository *repo)
726 const struct got_error *err = NULL;
727 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
728 struct got_tree_entry *te1 = NULL, *te2 = NULL;
734 /* We not do support comparing the root path. */
735 if (got_path_is_root_dir(path))
736 return got_error_path(path, GOT_ERR_BAD_PATH);
746 struct got_tree_object *next_tree1, *next_tree2;
756 te1 = find_entry_by_name(tree1, seg, seglen);
758 err = got_error(GOT_ERR_NO_OBJ);
763 te2 = find_entry_by_name(tree2, seg, seglen);
766 mode1 = normalize_mode_for_comparison(te1->mode);
767 mode2 = normalize_mode_for_comparison(te2->mode);
768 if (mode1 != mode2) {
773 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
779 if (*s == '\0') { /* final path element */
788 err = got_object_open_as_tree(&next_tree1, repo,
794 got_object_tree_close(tree1);
798 err = got_object_open_as_tree(&next_tree2, repo,
804 got_object_tree_close(tree2);
808 got_object_tree_close(tree2);
814 if (tree1 && tree1 != tree01)
815 got_object_tree_close(tree1);
816 if (tree2 && tree2 != tree02)
817 got_object_tree_close(tree2);
821 const struct got_error *
822 got_object_tree_entry_dup(struct got_tree_entry **new_te,
823 struct got_tree_entry *te)
825 const struct got_error *err = NULL;
827 *new_te = calloc(1, sizeof(**new_te));
829 return got_error_from_errno("calloc");
831 (*new_te)->mode = te->mode;
832 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
833 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
838 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
840 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
844 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
846 /* S_IFDIR check avoids confusing symlinks with submodules. */
847 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
850 static const struct got_error *
851 resolve_symlink(char **link_target, const char *path,
852 struct got_commit_object *commit, struct got_repository *repo)
854 const struct got_error *err = NULL;
856 char *name, *parent_path = NULL;
857 struct got_object_id *tree_obj_id = NULL;
858 struct got_tree_object *tree = NULL;
859 struct got_tree_entry *te = NULL;
863 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
864 return got_error(GOT_ERR_NO_SPACE);
866 name = basename(buf);
868 return got_error_from_errno2("basename", path);
870 err = got_path_dirname(&parent_path, path);
874 err = got_object_id_by_path(&tree_obj_id, repo, commit,
877 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
878 /* Display the complete path in error message. */
879 err = got_error_path(path, err->code);
884 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
888 te = got_object_tree_find_entry(tree, name);
890 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
894 if (got_object_tree_entry_is_symlink(te)) {
895 err = got_tree_entry_get_symlink_target(link_target, te, repo);
898 if (!got_path_is_absolute(*link_target)) {
900 if (asprintf(&abspath, "%s/%s", parent_path,
901 *link_target) == -1) {
902 err = got_error_from_errno("asprintf");
906 *link_target = malloc(PATH_MAX);
907 if (*link_target == NULL) {
908 err = got_error_from_errno("malloc");
911 err = got_canonpath(abspath, *link_target, PATH_MAX);
921 got_object_tree_close(tree);
929 const struct got_error *
930 got_object_resolve_symlinks(char **link_target, const char *path,
931 struct got_commit_object *commit, struct got_repository *repo)
933 const struct got_error *err = NULL;
934 char *next_target = NULL;
935 int max_recursion = 40; /* matches Git */
940 err = resolve_symlink(&next_target,
941 *link_target ? *link_target : path, commit, repo);
946 if (--max_recursion == 0) {
947 err = got_error_path(path, GOT_ERR_RECURSION);
951 *link_target = next_target;
953 } while (next_target);
959 got_object_commit_retain(struct got_commit_object *commit)
964 const struct got_error *
965 got_object_raw_alloc(struct got_raw_object **obj, uint8_t *outbuf, int *outfd,
966 size_t max_in_mem_size, size_t hdrlen, off_t size)
968 const struct got_error *err = NULL;
973 *obj = calloc(1, sizeof(**obj));
975 err = got_error_from_errno("calloc");
979 (*obj)->tempfile_idx = -1;
982 (*obj)->data = outbuf;
985 if (fstat(*outfd, &sb) == -1) {
986 err = got_error_from_errno("fstat");
990 if (sb.st_size != tot) {
991 err = got_error_msg(GOT_ERR_BAD_OBJ_HDR,
992 "raw object has unexpected size");
995 #ifndef GOT_PACK_NO_MMAP
996 if (tot > 0 && tot <= max_in_mem_size) {
997 (*obj)->data = mmap(NULL, tot, PROT_READ,
998 MAP_PRIVATE, *outfd, 0);
999 if ((*obj)->data == MAP_FAILED) {
1000 if (errno != ENOMEM) {
1001 err = got_error_from_errno("mmap");
1004 (*obj)->data = NULL;
1006 (*obj)->fd = *outfd;
1012 (*obj)->f = fdopen(*outfd, "r");
1013 if ((*obj)->f == NULL) {
1014 err = got_error_from_errno("fdopen");
1020 (*obj)->hdrlen = hdrlen;
1021 (*obj)->size = size;
1025 got_object_raw_close(*obj);