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 = open_packed_object(obj, id, repo);
227 if (err && err->code != GOT_ERR_NO_OBJ)
231 return got_repo_cache_object(repo, id, *obj);
234 err = object_path(&path, id, repo);
238 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
241 err = got_error(GOT_ERR_NO_OBJ);
243 err = got_error_from_errno();
246 err = got_object_read_header_privsep(obj, repo, fd);
249 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
253 err = got_repo_cache_object(repo, id, *obj);
262 const struct got_error *
263 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
266 struct got_object_id id;
268 if (!got_parse_sha1_digest(id.sha1, id_str))
269 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
271 return got_object_open(obj, repo, &id);
274 const struct got_error *
275 got_object_open_as_commit(struct got_commit_object **commit,
276 struct got_repository *repo, struct got_object_id *id)
278 const struct got_error *err;
279 struct got_object *obj;
281 *commit = got_repo_get_cached_commit(repo, id);
282 if (*commit != NULL) {
287 err = got_object_open(&obj, repo, id);
290 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
291 err = got_error(GOT_ERR_OBJ_TYPE);
295 err = got_object_commit_open(commit, repo, obj);
297 got_object_close(obj);
301 const struct got_error *
302 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
304 const struct got_error *err = NULL;
306 *qid = calloc(1, sizeof(**qid));
308 return got_error_from_errno();
310 (*qid)->id = got_object_id_dup(id);
311 if ((*qid)->id == NULL) {
312 err = got_error_from_errno();
313 got_object_qid_free(*qid);
321 const struct got_error *
322 got_object_commit_open(struct got_commit_object **commit,
323 struct got_repository *repo, struct got_object *obj)
325 const struct got_error *err = NULL;
327 *commit = got_repo_get_cached_commit(repo, &obj->id);
328 if (*commit != NULL) {
333 if (obj->type != GOT_OBJ_TYPE_COMMIT)
334 return got_error(GOT_ERR_OBJ_TYPE);
336 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
337 struct got_pack *pack;
338 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
340 err = got_repo_cache_pack(&pack, repo,
341 obj->path_packfile, NULL);
345 err = got_object_read_packed_commit_privsep(commit, obj, pack);
348 err = open_loose_object(&fd, obj, repo);
351 err = got_object_read_commit_privsep(commit, obj, fd, repo);
357 err = got_repo_cache_commit(repo, &obj->id, *commit);
363 const struct got_error *
364 got_object_tree_open(struct got_tree_object **tree,
365 struct got_repository *repo, struct got_object *obj)
367 const struct got_error *err = NULL;
369 *tree = got_repo_get_cached_tree(repo, &obj->id);
375 if (obj->type != GOT_OBJ_TYPE_TREE)
376 return got_error(GOT_ERR_OBJ_TYPE);
378 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
379 struct got_pack *pack;
380 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
382 err = got_repo_cache_pack(&pack, repo,
383 obj->path_packfile, NULL);
387 err = got_object_read_packed_tree_privsep(tree, obj, pack);
390 err = open_loose_object(&fd, obj, repo);
393 err = got_object_read_tree_privsep(tree, obj, fd, repo);
399 err = got_repo_cache_tree(repo, &obj->id, *tree);
405 const struct got_error *
406 got_object_open_as_tree(struct got_tree_object **tree,
407 struct got_repository *repo, struct got_object_id *id)
409 const struct got_error *err;
410 struct got_object *obj;
412 *tree = got_repo_get_cached_tree(repo, id);
418 err = got_object_open(&obj, repo, id);
421 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
422 err = got_error(GOT_ERR_OBJ_TYPE);
426 err = got_object_tree_open(tree, repo, obj);
428 got_object_close(obj);
432 const struct got_tree_entries *
433 got_object_tree_get_entries(struct got_tree_object *tree)
435 return &tree->entries;
438 static const struct got_error *
439 read_packed_blob_privsep(size_t *size, int outfd, struct got_object *obj,
440 struct got_pack *pack)
442 const struct got_error *err = NULL;
444 int basefd, accumfd; /* temporary files for delta application */
446 basefd = got_opentempfd();
448 return got_error_from_errno();
449 accumfd = got_opentempfd();
451 return got_error_from_errno();
453 outfd_child = dup(outfd);
454 if (outfd_child == -1)
455 return got_error_from_errno();
457 err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
461 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
467 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
476 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
484 err = got_privsep_recv_blob(size, pack->privsep_child->ibuf);
488 if (lseek(outfd, SEEK_SET, 0) == -1)
489 err = got_error_from_errno();
494 const struct got_error *
495 got_object_blob_open(struct got_blob_object **blob,
496 struct got_repository *repo, struct got_object *obj, size_t blocksize)
498 const struct got_error *err = NULL;
503 if (obj->type != GOT_OBJ_TYPE_BLOB)
504 return got_error(GOT_ERR_OBJ_TYPE);
506 if (blocksize < obj->hdrlen)
507 return got_error(GOT_ERR_NO_SPACE);
509 *blob = calloc(1, sizeof(**blob));
511 return got_error_from_errno();
513 outfd = got_opentempfd();
515 return got_error_from_errno();
517 (*blob)->read_buf = malloc(blocksize);
518 if ((*blob)->read_buf == NULL) {
519 err = got_error_from_errno();
522 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
523 struct got_pack *pack;
524 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
526 err = got_repo_cache_pack(&pack, repo,
527 obj->path_packfile, NULL);
531 err = read_packed_blob_privsep(&size, outfd, obj, pack);
538 err = open_loose_object(&infd, obj, repo);
542 err = got_object_read_blob_privsep(&size, outfd, infd, repo);
547 if (size != obj->hdrlen + obj->size) {
548 err = got_error(GOT_ERR_PRIVSEP_LEN);
553 if (fstat(outfd, &sb) == -1) {
554 err = got_error_from_errno();
558 if (sb.st_size != obj->hdrlen + obj->size) {
559 err = got_error(GOT_ERR_PRIVSEP_LEN);
563 (*blob)->f = fdopen(outfd, "rb");
564 if ((*blob)->f == NULL) {
565 err = got_error_from_errno();
570 (*blob)->hdrlen = obj->hdrlen;
571 (*blob)->blocksize = blocksize;
572 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
579 free((*blob)->read_buf);
582 } else if (outfd != -1)
588 const struct got_error *
589 got_object_open_as_blob(struct got_blob_object **blob,
590 struct got_repository *repo, struct got_object_id *id,
593 const struct got_error *err;
594 struct got_object *obj;
598 err = got_object_open(&obj, repo, id);
601 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
602 err = got_error(GOT_ERR_OBJ_TYPE);
606 err = got_object_blob_open(blob, repo, obj, blocksize);
608 got_object_close(obj);
613 got_object_blob_close(struct got_blob_object *blob)
615 free(blob->read_buf);
621 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
623 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
627 got_object_blob_get_hdrlen(struct got_blob_object *blob)
633 got_object_blob_get_read_buf(struct got_blob_object *blob)
635 return blob->read_buf;
638 const struct got_error *
639 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
643 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
644 if (n == 0 && ferror(blob->f))
645 return got_ferror(blob->f, GOT_ERR_IO);
650 const struct got_error *
651 got_object_blob_dump_to_file(size_t *total_len, size_t *nlines,
652 FILE *outfile, struct got_blob_object *blob)
654 const struct got_error *err = NULL;
664 hdrlen = got_object_blob_get_hdrlen(blob);
666 err = got_object_blob_read_block(&len, blob);
673 buf = got_object_blob_get_read_buf(blob);
675 for (i = 0; i < len; i++) {
680 /* Skip blob object header first time around. */
681 fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
691 static struct got_tree_entry *
692 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
694 struct got_tree_entry *te;
696 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
697 if (strncmp(te->name, name, len) == 0)
703 const struct got_error *
704 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
705 struct got_object_id *commit_id, const char *path)
707 const struct got_error *err = NULL;
708 struct got_commit_object *commit = NULL;
709 struct got_tree_object *tree = NULL;
710 struct got_tree_entry *te = NULL;
712 size_t seglen, len = strlen(path);
716 /* We are expecting an absolute in-repository path. */
718 return got_error(GOT_ERR_NOT_ABSPATH);
720 err = got_object_open_as_commit(&commit, repo, commit_id);
724 /* Handle opening of root of commit's tree. */
725 if (path[1] == '\0') {
726 *id = got_object_id_dup(commit->tree_id);
728 err = got_error_from_errno();
732 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
737 s++; /* skip leading '/' */
742 struct got_tree_object *next_tree;
752 te = find_entry_by_name(tree, seg, seglen);
754 err = got_error(GOT_ERR_NO_OBJ);
766 err = got_object_open_as_tree(&next_tree, repo,
771 got_object_tree_close(tree);
777 *id = got_object_id_dup(te->id);
779 return got_error_from_errno();
781 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;
799 size_t seglen, 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);
814 s++; /* skip leading '/' */
819 struct got_tree_object *next_tree1, *next_tree2;
829 te1 = find_entry_by_name(tree1, seg, seglen);
831 err = got_error(GOT_ERR_NO_OBJ);
835 te2 = find_entry_by_name(tree2, seg, seglen);
841 if (te1->mode != te2->mode) {
846 if (got_object_id_cmp(te1->id, te2->id) == 0) {
851 if (len == 0) { /* final path element */
861 err = got_object_open_as_tree(&next_tree1, repo,
867 got_object_tree_close(tree1);
870 err = got_object_open_as_tree(&next_tree2, repo,
876 got_object_tree_close(tree2);
881 if (tree1 && tree1 != tree01)
882 got_object_tree_close(tree1);
883 if (tree2 && tree2 != tree02)
884 got_object_tree_close(tree2);