2 * Copyright (c) 2018 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>
21 #include <sys/socket.h>
23 #include <sys/syslimits.h>
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_repository.h"
41 #include "got_opentemp.h"
43 #include "got_lib_sha1.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_pack.h"
46 #include "got_lib_path.h"
47 #include "got_lib_inflate.h"
48 #include "got_lib_object.h"
49 #include "got_lib_privsep.h"
50 #include "got_lib_object_idcache.h"
51 #include "got_lib_object_cache.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_repository.h"
56 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
62 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
65 struct got_object_id *
66 got_object_id_dup(struct got_object_id *id1)
68 struct got_object_id *id2;
70 id2 = malloc(sizeof(*id2));
73 memcpy(id2, id1, sizeof(*id2));
77 struct got_object_id *
78 got_object_get_id(struct got_object *obj)
83 const struct got_error *
84 got_object_get_id_str(char **outbuf, struct got_object *obj)
86 return got_object_id_str(outbuf, &obj->id);
90 got_object_get_type(struct got_object *obj)
93 case GOT_OBJ_TYPE_COMMIT:
94 case GOT_OBJ_TYPE_TREE:
95 case GOT_OBJ_TYPE_BLOB:
96 case GOT_OBJ_TYPE_TAG:
107 static const struct got_error *
108 object_path(char **path, struct got_object_id *id, struct got_repository *repo)
110 const struct got_error *err = NULL;
112 char *path_objects = got_repo_get_path_objects(repo);
116 if (path_objects == NULL)
117 return got_error_from_errno();
119 err = got_object_id_str(&hex, id);
123 if (asprintf(path, "%s/%.2x/%s", path_objects,
124 id->sha1[0], hex + 2) == -1)
125 err = got_error_from_errno();
133 static const struct got_error *
134 open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
136 const struct got_error *err = NULL;
139 err = object_path(&path, &obj->id, repo);
142 *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
144 err = got_error_from_errno();
152 static const struct got_error *
153 get_packfile_path(char **path_packfile, struct got_packidx *packidx)
157 /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
158 size = strlen(packidx->path_packidx) + 2;
159 if (size < GOT_PACKFILE_NAMELEN + 1)
160 return got_error(GOT_ERR_BAD_PATH);
162 *path_packfile = calloc(size, sizeof(**path_packfile));
163 if (*path_packfile == NULL)
164 return got_error_from_errno();
166 /* Copy up to and excluding ".idx". */
167 if (strlcpy(*path_packfile, packidx->path_packidx,
168 size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
169 return got_error(GOT_ERR_NO_SPACE);
171 if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
172 return got_error(GOT_ERR_NO_SPACE);
177 static const struct got_error *
178 open_packed_object(struct got_object **obj, struct got_object_id *id,
179 struct got_repository *repo)
181 const struct got_error *err = NULL;
182 struct got_pack *pack = NULL;
183 struct got_packidx *packidx = NULL;
187 err = got_repo_search_packidx(&packidx, &idx, repo, id);
191 err = get_packfile_path(&path_packfile, packidx);
195 pack = got_repo_get_cached_pack(repo, path_packfile);
197 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
202 err = got_object_packed_read_privsep(obj, repo, pack, packidx, idx, id);
206 err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
212 const struct got_error *
213 got_object_open(struct got_object **obj, struct got_repository *repo,
214 struct got_object_id *id)
216 const struct got_error *err = NULL;
220 *obj = got_repo_get_cached_object(repo, id);
226 err = object_path(&path, id, repo);
230 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
232 if (errno != ENOENT) {
233 err = got_error_from_errno();
236 err = open_packed_object(obj, id, repo);
240 err = got_error(GOT_ERR_NO_OBJ);
242 err = got_object_read_header_privsep(obj, repo, fd);
245 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
250 err = got_repo_cache_object(repo, id, *obj);
260 const struct got_error *
261 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
264 struct got_object_id id;
266 if (!got_parse_sha1_digest(id.sha1, id_str))
267 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
269 return got_object_open(obj, repo, &id);
272 const struct got_error *
273 got_object_open_as_commit(struct got_commit_object **commit,
274 struct got_repository *repo, struct got_object_id *id)
276 const struct got_error *err;
277 struct got_object *obj;
281 err = got_object_open(&obj, repo, id);
284 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
285 err = got_error(GOT_ERR_OBJ_TYPE);
289 err = got_object_commit_open(commit, repo, obj);
291 got_object_close(obj);
295 const struct got_error *
296 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
298 const struct got_error *err = NULL;
300 *qid = calloc(1, sizeof(**qid));
302 return got_error_from_errno();
304 (*qid)->id = got_object_id_dup(id);
305 if ((*qid)->id == NULL) {
306 err = got_error_from_errno();
307 got_object_qid_free(*qid);
315 const struct got_error *
316 got_object_commit_open(struct got_commit_object **commit,
317 struct got_repository *repo, struct got_object *obj)
319 const struct got_error *err = NULL;
321 *commit = got_repo_get_cached_commit(repo, &obj->id);
322 if (*commit != NULL) {
327 if (obj->type != GOT_OBJ_TYPE_COMMIT)
328 return got_error(GOT_ERR_OBJ_TYPE);
330 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
331 struct got_pack *pack;
332 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
334 err = got_repo_cache_pack(&pack, repo,
335 obj->path_packfile, NULL);
339 err = got_object_read_packed_commit_privsep(commit, obj, pack);
342 err = open_loose_object(&fd, obj, repo);
345 err = got_object_read_commit_privsep(commit, obj, fd, repo);
351 err = got_repo_cache_commit(repo, &obj->id, *commit);
357 const struct got_error *
358 got_object_tree_open(struct got_tree_object **tree,
359 struct got_repository *repo, struct got_object *obj)
361 const struct got_error *err = NULL;
363 *tree = got_repo_get_cached_tree(repo, &obj->id);
369 if (obj->type != GOT_OBJ_TYPE_TREE)
370 return got_error(GOT_ERR_OBJ_TYPE);
372 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
373 struct got_pack *pack;
374 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
376 err = got_repo_cache_pack(&pack, repo,
377 obj->path_packfile, NULL);
381 err = got_object_read_packed_tree_privsep(tree, obj, pack);
384 err = open_loose_object(&fd, obj, repo);
387 err = got_object_read_tree_privsep(tree, obj, fd, repo);
393 err = got_repo_cache_tree(repo, &obj->id, *tree);
399 const struct got_error *
400 got_object_open_as_tree(struct got_tree_object **tree,
401 struct got_repository *repo, struct got_object_id *id)
403 const struct got_error *err;
404 struct got_object *obj;
408 err = got_object_open(&obj, repo, id);
411 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
412 err = got_error(GOT_ERR_OBJ_TYPE);
416 err = got_object_tree_open(tree, repo, obj);
418 got_object_close(obj);
422 const struct got_tree_entries *
423 got_object_tree_get_entries(struct got_tree_object *tree)
425 return &tree->entries;
428 static const struct got_error *
429 read_packed_blob_privsep(size_t *size, int outfd, struct got_object *obj,
430 struct got_pack *pack)
432 const struct got_error *err = NULL;
434 int basefd, accumfd; /* temporary files for delta application */
436 basefd = got_opentempfd();
438 return got_error_from_errno();
439 accumfd = got_opentempfd();
441 return got_error_from_errno();
443 outfd_child = dup(outfd);
444 if (outfd_child == -1)
445 return got_error_from_errno();
447 err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
451 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
457 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
466 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
474 err = got_privsep_recv_blob(size, pack->privsep_child->ibuf);
478 if (lseek(outfd, SEEK_SET, 0) == -1)
479 err = got_error_from_errno();
484 const struct got_error *
485 got_object_blob_open(struct got_blob_object **blob,
486 struct got_repository *repo, struct got_object *obj, size_t blocksize)
488 const struct got_error *err = NULL;
493 if (obj->type != GOT_OBJ_TYPE_BLOB)
494 return got_error(GOT_ERR_OBJ_TYPE);
496 if (blocksize < obj->hdrlen)
497 return got_error(GOT_ERR_NO_SPACE);
499 *blob = calloc(1, sizeof(**blob));
501 return got_error_from_errno();
503 outfd = got_opentempfd();
505 return got_error_from_errno();
507 (*blob)->read_buf = malloc(blocksize);
508 if ((*blob)->read_buf == NULL) {
509 err = got_error_from_errno();
512 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
513 struct got_pack *pack;
514 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
516 err = got_repo_cache_pack(&pack, repo,
517 obj->path_packfile, NULL);
521 err = read_packed_blob_privsep(&size, outfd, obj, pack);
528 err = open_loose_object(&infd, obj, repo);
532 err = got_object_read_blob_privsep(&size, outfd, infd, repo);
537 if (size != obj->hdrlen + obj->size) {
538 err = got_error(GOT_ERR_PRIVSEP_LEN);
543 if (fstat(outfd, &sb) == -1) {
544 err = got_error_from_errno();
548 if (sb.st_size != obj->hdrlen + obj->size) {
549 err = got_error(GOT_ERR_PRIVSEP_LEN);
553 (*blob)->f = fdopen(outfd, "rb");
554 if ((*blob)->f == NULL) {
555 err = got_error_from_errno();
560 (*blob)->hdrlen = obj->hdrlen;
561 (*blob)->blocksize = blocksize;
562 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
569 free((*blob)->read_buf);
572 } else if (outfd != -1)
578 const struct got_error *
579 got_object_open_as_blob(struct got_blob_object **blob,
580 struct got_repository *repo, struct got_object_id *id,
583 const struct got_error *err;
584 struct got_object *obj;
588 err = got_object_open(&obj, repo, id);
591 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
592 err = got_error(GOT_ERR_OBJ_TYPE);
596 err = got_object_blob_open(blob, repo, obj, blocksize);
598 got_object_close(obj);
603 got_object_blob_close(struct got_blob_object *blob)
605 free(blob->read_buf);
611 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
613 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
617 got_object_blob_get_hdrlen(struct got_blob_object *blob)
623 got_object_blob_get_read_buf(struct got_blob_object *blob)
625 return blob->read_buf;
628 const struct got_error *
629 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
633 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
634 if (n == 0 && ferror(blob->f))
635 return got_ferror(blob->f, GOT_ERR_IO);
640 const struct got_error *
641 got_object_blob_dump_to_file(size_t *total_len, size_t *nlines,
642 FILE *outfile, struct got_blob_object *blob)
644 const struct got_error *err = NULL;
654 hdrlen = got_object_blob_get_hdrlen(blob);
656 err = got_object_blob_read_block(&len, blob);
663 buf = got_object_blob_get_read_buf(blob);
665 for (i = 0; i < len; i++) {
670 /* Skip blob object header first time around. */
671 fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
681 static struct got_tree_entry *
682 find_entry_by_name(struct got_tree_object *tree, const char *name)
684 struct got_tree_entry *te;
686 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
687 if (strcmp(te->name, name) == 0)
693 const struct got_error *
694 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
695 struct got_object_id *commit_id, const char *path)
697 const struct got_error *err = NULL;
698 struct got_commit_object *commit = NULL;
699 struct got_tree_object *tree = NULL;
700 struct got_tree_entry *te = NULL;
701 char *seg, *s, *s0 = NULL;
702 size_t len = strlen(path);
706 /* We are expecting an absolute in-repository path. */
708 return got_error(GOT_ERR_NOT_ABSPATH);
710 err = got_object_open_as_commit(&commit, repo, commit_id);
714 /* Handle opening of root of commit's tree. */
715 if (path[1] == '\0') {
716 *id = got_object_id_dup(commit->tree_id);
718 err = got_error_from_errno();
722 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
728 err = got_error_from_errno();
731 err = got_canonpath(path, s0, len + 1);
736 s++; /* skip leading '/' */
740 struct got_tree_object *next_tree;
749 /* end of path segment */
752 te = find_entry_by_name(tree, seg);
754 err = got_error(GOT_ERR_NO_OBJ);
765 err = got_object_open_as_tree(&next_tree, repo,
770 got_object_tree_close(tree);
776 *id = got_object_id_dup(te->id);
778 return got_error_from_errno();
780 err = got_error(GOT_ERR_NO_OBJ);
784 got_object_commit_close(commit);
786 got_object_tree_close(tree);
790 const struct got_error *
791 got_object_tree_path_changed(int *changed,
792 struct got_tree_object *tree01, struct got_tree_object *tree02,
793 const char *path, struct got_repository *repo)
795 const struct got_error *err = NULL;
796 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
797 struct got_tree_entry *te1 = NULL, *te2 = NULL;
798 char *seg, *s, *s0 = NULL;
799 size_t len = strlen(path);
803 /* We are expecting an absolute in-repository path. */
805 return got_error(GOT_ERR_NOT_ABSPATH);
807 /* We not do support comparing the root path. */
809 return got_error(GOT_ERR_BAD_PATH);
813 err = got_error_from_errno();
816 err = got_canonpath(path, s0, len + 1);
823 s++; /* skip leading '/' */
827 struct got_tree_object *next_tree1, *next_tree2;
836 /* end of path segment */
839 te1 = find_entry_by_name(tree1, seg);
841 err = got_error(GOT_ERR_NO_OBJ);
845 te2 = find_entry_by_name(tree2, seg);
851 if (te1->mode != te2->mode) {
856 if (got_object_id_cmp(te1->id, te2->id) == 0) {
861 if (S_ISREG(te1->mode)) { /* final path element */
873 err = got_object_open_as_tree(&next_tree1, repo,
880 err = got_object_open_as_tree(&next_tree2, repo,
891 got_object_tree_close(tree1);
893 got_object_tree_close(tree2);