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->sha1[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 if (!got_parse_object_id(&id, id_str, GOT_HASH_SHA1))
155 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
157 return got_object_open(obj, repo, &id);
160 const struct got_error *
161 got_object_resolve_id_str(struct got_object_id **id,
162 struct got_repository *repo, const char *id_str)
164 const struct got_error *err = NULL;
165 struct got_object *obj;
167 err = got_object_open_by_id_str(&obj, repo, id_str);
171 *id = got_object_id_dup(got_object_get_id(obj));
172 got_object_close(obj);
174 return got_error_from_errno("got_object_id_dup");
179 const struct got_error *
180 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
182 *qid = calloc(1, sizeof(**qid));
184 return got_error_from_errno("calloc");
186 memcpy(&(*qid)->id, id, sizeof((*qid)->id));
190 const struct got_error *
191 got_object_id_queue_copy(const struct got_object_id_queue *src,
192 struct got_object_id_queue *dest)
194 const struct got_error *err;
195 struct got_object_qid *qid;
197 STAILQ_FOREACH(qid, src, entry) {
198 struct got_object_qid *new;
200 * Deep-copy the object ID only. Let the caller deal
201 * with setting up the new->data pointer if needed.
203 err = got_object_qid_alloc(&new, &qid->id);
205 got_object_id_queue_free(dest);
208 STAILQ_INSERT_TAIL(dest, new, entry);
215 got_object_tree_get_nentries(struct got_tree_object *tree)
217 return tree->nentries;
220 struct got_tree_entry *
221 got_object_tree_get_first_entry(struct got_tree_object *tree)
223 return got_object_tree_get_entry(tree, 0);
226 struct got_tree_entry *
227 got_object_tree_get_last_entry(struct got_tree_object *tree)
229 return got_object_tree_get_entry(tree, tree->nentries - 1);
232 struct got_tree_entry *
233 got_object_tree_get_entry(struct got_tree_object *tree, int i)
235 if (i < 0 || i >= tree->nentries)
237 return &tree->entries[i];
241 got_tree_entry_get_mode(struct got_tree_entry *te)
247 got_tree_entry_get_name(struct got_tree_entry *te)
252 struct got_object_id *
253 got_tree_entry_get_id(struct got_tree_entry *te)
258 const struct got_error *
259 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
261 const struct got_error *err = NULL;
262 size_t len, totlen, hdrlen, offset;
266 hdrlen = got_object_blob_get_hdrlen(blob);
272 err = got_object_blob_read_block(&len, blob);
279 totlen += len - hdrlen;
280 p = realloc(*s, totlen + 1);
282 err = got_error_from_errno("realloc");
288 /* Skip blob object header first time around. */
290 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
299 const struct got_error *
300 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
301 struct got_repository *repo)
303 const struct got_error *err = NULL;
304 struct got_blob_object *blob = NULL;
309 if (!got_object_tree_entry_is_symlink(te))
310 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
312 fd = got_opentempfd();
314 err = got_error_from_errno("got_opentempfd");
318 err = got_object_open_as_blob(&blob, repo,
319 got_tree_entry_get_id(te), PATH_MAX, fd);
323 err = got_object_blob_read_to_str(link_target, blob);
325 if (fd != -1 && close(fd) == -1 && err == NULL)
326 err = got_error_from_errno("close");
328 got_object_blob_close(blob);
337 got_tree_entry_get_index(struct got_tree_entry *te)
342 struct got_tree_entry *
343 got_tree_entry_get_next(struct got_tree_object *tree,
344 struct got_tree_entry *te)
346 return got_object_tree_get_entry(tree, te->idx + 1);
349 struct got_tree_entry *
350 got_tree_entry_get_prev(struct got_tree_object *tree,
351 struct got_tree_entry *te)
353 return got_object_tree_get_entry(tree, te->idx - 1);
356 const struct got_error *
357 got_object_blob_close(struct got_blob_object *blob)
359 const struct got_error *err = NULL;
360 free(blob->read_buf);
361 if (blob->f && fclose(blob->f) == EOF)
362 err = got_error_from_errno("fclose");
369 got_object_blob_rewind(struct got_blob_object *blob)
376 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
378 return got_object_id_hex(&blob->id, buf, size);
382 got_object_blob_get_hdrlen(struct got_blob_object *blob)
388 got_object_blob_get_read_buf(struct got_blob_object *blob)
390 return blob->read_buf;
393 const struct got_error *
394 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
398 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
399 if (n == 0 && ferror(blob->f))
400 return got_ferror(blob->f, GOT_ERR_IO);
405 const struct got_error *
406 got_object_blob_is_binary(int *binary, struct got_blob_object *blob)
408 const struct got_error *err;
412 hdrlen = got_object_blob_get_hdrlen(blob);
414 if (fseeko(blob->f, hdrlen, SEEK_SET) == -1)
415 return got_error_from_errno("fseeko");
417 err = got_object_blob_read_block(&len, blob);
421 *binary = memchr(blob->read_buf, '\0', len) != NULL;
423 if (fseeko(blob->f, hdrlen, SEEK_SET) == -1)
424 return got_error_from_errno("fseeko");
428 const struct got_error *
429 got_object_blob_getline(char **line, ssize_t *linelen, size_t *linesize,
430 struct got_blob_object *blob)
432 *linelen = getline(line, linesize, blob->f);
433 if (*linelen == -1 && !feof(blob->f))
434 return got_error_from_errno("getline");
438 const struct got_error *
439 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
440 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
442 const struct got_error *err = NULL;
443 size_t n, len, hdrlen;
446 const int alloc_chunksz = 512;
448 off_t off = 0, total_len = 0;
451 *line_offsets = NULL;
457 hdrlen = got_object_blob_get_hdrlen(blob);
459 err = got_object_blob_read_block(&len, blob);
464 buf = got_object_blob_get_read_buf(blob);
467 if (line_offsets && *line_offsets == NULL) {
468 /* Have some data but perhaps no '\n'. */
470 nalloc = alloc_chunksz;
471 *line_offsets = calloc(nalloc,
472 sizeof(**line_offsets));
473 if (*line_offsets == NULL)
474 return got_error_from_errno("calloc");
476 /* Skip forward over end of first line. */
483 /* Scan '\n' offsets in remaining chunk of data. */
485 if (buf[i] != '\n') {
490 if (line_offsets && nalloc < *nlines) {
491 size_t n = *nlines + alloc_chunksz;
492 off_t *o = recallocarray(*line_offsets,
493 nalloc, n, sizeof(**line_offsets));
496 *line_offsets = NULL;
497 return got_error_from_errno(
504 off = total_len + i - hdrlen + 1;
505 (*line_offsets)[*nlines - 1] = off;
510 /* Skip blob object header first time around. */
511 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
512 if (n != len - hdrlen)
513 return got_ferror(outfile, GOT_ERR_IO);
514 total_len += len - hdrlen;
518 if (fflush(outfile) != 0)
519 return got_error_from_errno("fflush");
523 *filesize = total_len;
529 got_object_tag_get_name(struct got_tag_object *tag)
535 got_object_tag_get_object_type(struct got_tag_object *tag)
537 return tag->obj_type;
540 struct got_object_id *
541 got_object_tag_get_object_id(struct got_tag_object *tag)
547 got_object_tag_get_tagger_time(struct got_tag_object *tag)
549 return tag->tagger_time;
553 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
555 return tag->tagger_gmtoff;
559 got_object_tag_get_tagger(struct got_tag_object *tag)
565 got_object_tag_get_message(struct got_tag_object *tag)
570 static struct got_tree_entry *
571 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
575 /* Note that tree entries are sorted in strncmp() order. */
576 for (i = 0; i < tree->nentries; i++) {
577 struct got_tree_entry *te = &tree->entries[i];
578 int cmp = strncmp(te->name, name, len);
583 if (te->name[len] == '\0')
589 struct got_tree_entry *
590 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
592 return find_entry_by_name(tree, name, strlen(name));
595 const struct got_error *
596 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
597 struct got_repository *repo, struct got_tree_object *tree,
600 const struct got_error *err = NULL;
601 struct got_tree_object *subtree = NULL;
602 struct got_tree_entry *te = NULL;
615 struct got_tree_object *next_tree;
624 te = find_entry_by_name(subtree, seg, seglen);
626 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
637 err = got_object_open_as_tree(&next_tree, repo,
643 got_object_tree_close(subtree);
649 *id = got_object_id_dup(&te->id);
651 return got_error_from_errno("got_object_id_dup");
655 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
657 if (subtree && subtree != tree)
658 got_object_tree_close(subtree);
662 const struct got_error *
663 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
664 struct got_commit_object *commit, const char *path)
666 const struct got_error *err = NULL;
667 struct got_tree_object *tree = NULL;
671 /* Handle opening of root of commit's tree. */
672 if (got_path_is_root_dir(path)) {
673 *id = got_object_id_dup(commit->tree_id);
675 err = got_error_from_errno("got_object_id_dup");
677 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
680 err = got_object_tree_find_path(id, NULL, repo, tree, path);
684 got_object_tree_close(tree);
689 * Normalize file mode bits to avoid false positive tree entry differences
690 * in case tree entries have unexpected mode bits set.
693 normalize_mode_for_comparison(mode_t mode)
696 * For directories, the only relevant bit is the IFDIR bit.
697 * This allows us to detect paths changing from a directory
698 * to a file and vice versa.
701 return mode & S_IFDIR;
704 * For symlinks, the only relevant bit is the IFLNK bit.
705 * This allows us to detect paths changing from a symlinks
706 * to a file or directory and vice versa.
709 return mode & S_IFLNK;
711 /* For files, the only change we care about is the executable bit. */
712 return mode & S_IXUSR;
715 const struct got_error *
716 got_object_tree_path_changed(int *changed,
717 struct got_tree_object *tree01, struct got_tree_object *tree02,
718 const char *path, struct got_repository *repo)
720 const struct got_error *err = NULL;
721 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
722 struct got_tree_entry *te1 = NULL, *te2 = NULL;
728 /* We not do support comparing the root path. */
729 if (got_path_is_root_dir(path))
730 return got_error_path(path, GOT_ERR_BAD_PATH);
740 struct got_tree_object *next_tree1, *next_tree2;
750 te1 = find_entry_by_name(tree1, seg, seglen);
752 err = got_error(GOT_ERR_NO_OBJ);
757 te2 = find_entry_by_name(tree2, seg, seglen);
760 mode1 = normalize_mode_for_comparison(te1->mode);
761 mode2 = normalize_mode_for_comparison(te2->mode);
762 if (mode1 != mode2) {
767 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
773 if (*s == '\0') { /* final path element */
782 err = got_object_open_as_tree(&next_tree1, repo,
788 got_object_tree_close(tree1);
792 err = got_object_open_as_tree(&next_tree2, repo,
798 got_object_tree_close(tree2);
802 got_object_tree_close(tree2);
808 if (tree1 && tree1 != tree01)
809 got_object_tree_close(tree1);
810 if (tree2 && tree2 != tree02)
811 got_object_tree_close(tree2);
815 const struct got_error *
816 got_object_tree_entry_dup(struct got_tree_entry **new_te,
817 struct got_tree_entry *te)
819 const struct got_error *err = NULL;
821 *new_te = calloc(1, sizeof(**new_te));
823 return got_error_from_errno("calloc");
825 (*new_te)->mode = te->mode;
826 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
827 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
832 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
834 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
838 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
840 /* S_IFDIR check avoids confusing symlinks with submodules. */
841 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
844 static const struct got_error *
845 resolve_symlink(char **link_target, const char *path,
846 struct got_commit_object *commit, struct got_repository *repo)
848 const struct got_error *err = NULL;
850 char *name, *parent_path = NULL;
851 struct got_object_id *tree_obj_id = NULL;
852 struct got_tree_object *tree = NULL;
853 struct got_tree_entry *te = NULL;
857 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
858 return got_error(GOT_ERR_NO_SPACE);
860 name = basename(buf);
862 return got_error_from_errno2("basename", path);
864 err = got_path_dirname(&parent_path, path);
868 err = got_object_id_by_path(&tree_obj_id, repo, commit,
871 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
872 /* Display the complete path in error message. */
873 err = got_error_path(path, err->code);
878 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
882 te = got_object_tree_find_entry(tree, name);
884 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
888 if (got_object_tree_entry_is_symlink(te)) {
889 err = got_tree_entry_get_symlink_target(link_target, te, repo);
892 if (!got_path_is_absolute(*link_target)) {
894 if (asprintf(&abspath, "%s/%s", parent_path,
895 *link_target) == -1) {
896 err = got_error_from_errno("asprintf");
900 *link_target = malloc(PATH_MAX);
901 if (*link_target == NULL) {
902 err = got_error_from_errno("malloc");
905 err = got_canonpath(abspath, *link_target, PATH_MAX);
915 got_object_tree_close(tree);
923 const struct got_error *
924 got_object_resolve_symlinks(char **link_target, const char *path,
925 struct got_commit_object *commit, struct got_repository *repo)
927 const struct got_error *err = NULL;
928 char *next_target = NULL;
929 int max_recursion = 40; /* matches Git */
934 err = resolve_symlink(&next_target,
935 *link_target ? *link_target : path, commit, repo);
940 if (--max_recursion == 0) {
941 err = got_error_path(path, GOT_ERR_RECURSION);
945 *link_target = next_target;
947 } while (next_target);
953 got_object_commit_retain(struct got_commit_object *commit)
958 const struct got_error *
959 got_object_raw_alloc(struct got_raw_object **obj, uint8_t *outbuf, int *outfd,
960 size_t max_in_mem_size, size_t hdrlen, off_t size)
962 const struct got_error *err = NULL;
967 *obj = calloc(1, sizeof(**obj));
969 err = got_error_from_errno("calloc");
973 (*obj)->tempfile_idx = -1;
976 (*obj)->data = outbuf;
979 if (fstat(*outfd, &sb) == -1) {
980 err = got_error_from_errno("fstat");
984 if (sb.st_size != tot) {
985 err = got_error_msg(GOT_ERR_BAD_OBJ_HDR,
986 "raw object has unexpected size");
989 #ifndef GOT_PACK_NO_MMAP
990 if (tot > 0 && tot <= max_in_mem_size) {
991 (*obj)->data = mmap(NULL, tot, PROT_READ,
992 MAP_PRIVATE, *outfd, 0);
993 if ((*obj)->data == MAP_FAILED) {
994 if (errno != ENOMEM) {
995 err = got_error_from_errno("mmap");
1000 (*obj)->fd = *outfd;
1006 (*obj)->f = fdopen(*outfd, "r");
1007 if ((*obj)->f == NULL) {
1008 err = got_error_from_errno("fdopen");
1014 (*obj)->hdrlen = hdrlen;
1015 (*obj)->size = size;
1019 got_object_raw_close(*obj);