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>
37 #include "got_error.h"
38 #include "got_object.h"
39 #include "got_repository.h"
40 #include "got_opentemp.h"
42 #include "got_lib_sha1.h"
43 #include "got_lib_delta.h"
44 #include "got_lib_pack.h"
45 #include "got_lib_path.h"
46 #include "got_lib_inflate.h"
47 #include "got_lib_object.h"
48 #include "got_lib_privsep.h"
49 #include "got_lib_object_parse.h"
50 #include "got_lib_repository.h"
53 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
56 const struct got_error *
57 got_object_id_str(char **outbuf, struct got_object_id *id)
59 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
61 *outbuf = malloc(len);
63 return got_error_from_errno();
65 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
68 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
75 got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
77 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
80 struct got_object_id *
81 got_object_id_dup(struct got_object_id *id1)
83 struct got_object_id *id2;
85 id2 = malloc(sizeof(*id2));
88 memcpy(id2, id1, sizeof(*id2));
92 struct got_object_id *
93 got_object_get_id(struct got_object *obj)
95 return got_object_id_dup(&obj->id);
98 const struct got_error *
99 got_object_get_id_str(char **outbuf, struct got_object *obj)
101 return got_object_id_str(outbuf, &obj->id);
105 got_object_get_type(struct got_object *obj)
108 case GOT_OBJ_TYPE_COMMIT:
109 case GOT_OBJ_TYPE_TREE:
110 case GOT_OBJ_TYPE_BLOB:
111 case GOT_OBJ_TYPE_TAG:
122 static const struct got_error *
123 object_path(char **path, struct got_object_id *id, struct got_repository *repo)
125 const struct got_error *err = NULL;
127 char *path_objects = got_repo_get_path_objects(repo);
131 if (path_objects == NULL)
132 return got_error_from_errno();
134 err = got_object_id_str(&hex, id);
138 if (asprintf(path, "%s/%.2x/%s", path_objects,
139 id->sha1[0], hex + 2) == -1)
140 err = got_error_from_errno();
148 static const struct got_error *
149 open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
151 const struct got_error *err = NULL;
154 err = object_path(&path, &obj->id, repo);
157 *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
159 err = got_error_from_errno();
167 static const struct got_error *
168 get_packfile_path(char **path_packfile, struct got_packidx *packidx)
172 /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
173 size = strlen(packidx->path_packidx) + 2;
174 if (size < GOT_PACKFILE_NAMELEN + 1)
175 return got_error(GOT_ERR_BAD_PATH);
177 *path_packfile = calloc(size, sizeof(**path_packfile));
178 if (*path_packfile == NULL)
179 return got_error_from_errno();
181 /* Copy up to and excluding ".idx". */
182 if (strlcpy(*path_packfile, packidx->path_packidx,
183 size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
184 return got_error(GOT_ERR_NO_SPACE);
186 if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
187 return got_error(GOT_ERR_NO_SPACE);
192 static const struct got_error *
193 open_packed_object(struct got_object **obj, struct got_object_id *id,
194 struct got_repository *repo)
196 const struct got_error *err = NULL;
197 struct got_pack *pack = NULL;
198 struct got_packidx *packidx = NULL;
202 err = got_repo_search_packidx(&packidx, &idx, repo, id);
206 err = get_packfile_path(&path_packfile, packidx);
210 pack = got_repo_get_cached_pack(repo, path_packfile);
212 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
217 err = got_packfile_open_object(obj, pack, packidx, idx, id);
221 err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
227 const struct got_error *
228 got_object_open(struct got_object **obj, struct got_repository *repo,
229 struct got_object_id *id)
231 const struct got_error *err = NULL;
235 *obj = got_repo_get_cached_object(repo, id);
241 err = object_path(&path, id, repo);
245 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
247 if (errno != ENOENT) {
248 err = got_error_from_errno();
251 err = open_packed_object(obj, id, repo);
255 err = got_error(GOT_ERR_NO_OBJ);
257 err = got_object_read_header_privsep(obj, repo, fd);
260 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
265 err = got_repo_cache_object(repo, id, *obj);
275 const struct got_error *
276 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
279 struct got_object_id id;
281 if (!got_parse_sha1_digest(id.sha1, id_str))
282 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
284 return got_object_open(obj, repo, &id);
287 const struct got_error *
288 got_object_open_as_commit(struct got_commit_object **commit,
289 struct got_repository *repo, struct got_object_id *id)
291 const struct got_error *err;
292 struct got_object *obj;
296 err = got_object_open(&obj, repo, id);
299 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
300 err = got_error(GOT_ERR_OBJ_TYPE);
304 err = got_object_commit_open(commit, repo, obj);
306 got_object_close(obj);
310 const struct got_error *
311 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
313 const struct got_error *err = NULL;
315 *qid = calloc(1, sizeof(**qid));
317 return got_error_from_errno();
319 (*qid)->id = got_object_id_dup(id);
320 if ((*qid)->id == NULL) {
321 err = got_error_from_errno();
322 got_object_qid_free(*qid);
330 static const struct got_error *
331 extract_packed_object_to_mem(uint8_t **buf, size_t *len,
332 struct got_object *obj, struct got_repository *repo)
334 const struct got_error *err = NULL;
335 struct got_pack *pack;
337 if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
338 return got_error(GOT_ERR_OBJ_NOT_PACKED);
340 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
342 err = got_repo_cache_pack(&pack, repo,
343 obj->path_packfile, NULL);
348 return got_packfile_extract_object_to_mem(buf, len, obj, pack);
351 const struct got_error *
352 got_object_commit_open(struct got_commit_object **commit,
353 struct got_repository *repo, struct got_object *obj)
355 const struct got_error *err = NULL;
357 *commit = got_repo_get_cached_commit(repo, &obj->id);
358 if (*commit != NULL) {
363 if (obj->type != GOT_OBJ_TYPE_COMMIT)
364 return got_error(GOT_ERR_OBJ_TYPE);
366 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
369 err = extract_packed_object_to_mem(&buf, &len, obj, repo);
373 err = got_object_parse_commit(commit, buf, len);
377 err = open_loose_object(&fd, obj, repo);
380 err = got_object_read_commit_privsep(commit, obj, fd, repo);
386 err = got_repo_cache_commit(repo, &obj->id, *commit);
392 const struct got_error *
393 got_object_tree_open(struct got_tree_object **tree,
394 struct got_repository *repo, struct got_object *obj)
396 const struct got_error *err = NULL;
398 *tree = got_repo_get_cached_tree(repo, &obj->id);
404 if (obj->type != GOT_OBJ_TYPE_TREE)
405 return got_error(GOT_ERR_OBJ_TYPE);
407 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
410 err = extract_packed_object_to_mem(&buf, &len, obj, repo);
414 err = got_object_parse_tree(tree, buf, len);
418 err = open_loose_object(&fd, obj, repo);
421 err = got_object_read_tree_privsep(tree, obj, fd, repo);
427 err = got_repo_cache_tree(repo, &obj->id, *tree);
433 const struct got_error *
434 got_object_open_as_tree(struct got_tree_object **tree,
435 struct got_repository *repo, struct got_object_id *id)
437 const struct got_error *err;
438 struct got_object *obj;
442 err = got_object_open(&obj, repo, id);
445 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
446 err = got_error(GOT_ERR_OBJ_TYPE);
450 err = got_object_tree_open(tree, repo, obj);
452 got_object_close(obj);
456 const struct got_tree_entries *
457 got_object_tree_get_entries(struct got_tree_object *tree)
459 return &tree->entries;
462 static const struct got_error *
463 extract_packed_object(FILE **f, struct got_object *obj,
464 struct got_repository *repo)
466 const struct got_error *err = NULL;
467 struct got_pack *pack;
470 if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
471 return got_error(GOT_ERR_OBJ_NOT_PACKED);
473 fd = got_opentempfd();
475 return got_error_from_errno();
477 *f = fdopen(fd, "w+");
479 err = got_error_from_errno();
483 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
485 err = got_repo_cache_pack(&pack, repo,
486 obj->path_packfile, NULL);
491 err = got_packfile_extract_object(pack, obj, *f);
503 const struct got_error *
504 got_object_blob_open(struct got_blob_object **blob,
505 struct got_repository *repo, struct got_object *obj, size_t blocksize)
507 const struct got_error *err = NULL;
509 if (obj->type != GOT_OBJ_TYPE_BLOB)
510 return got_error(GOT_ERR_OBJ_TYPE);
512 if (blocksize < obj->hdrlen)
513 return got_error(GOT_ERR_NO_SPACE);
515 *blob = calloc(1, sizeof(**blob));
517 return got_error_from_errno();
519 (*blob)->read_buf = malloc(blocksize);
520 if ((*blob)->read_buf == NULL) {
521 err = got_error_from_errno();
524 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
525 err = extract_packed_object(&((*blob)->f), obj, repo);
533 err = open_loose_object(&infd, obj, repo);
538 outfd = got_opentempfd();
540 err = got_error_from_errno();
545 err = got_object_read_blob_privsep(&size, outfd, infd, repo);
550 if (size != obj->hdrlen + obj->size) {
551 err = got_error(GOT_ERR_PRIVSEP_LEN);
556 if (fstat(outfd, &sb) == -1) {
557 err = got_error_from_errno();
562 if (sb.st_size != size) {
563 err = got_error(GOT_ERR_PRIVSEP_LEN);
568 (*blob)->f = fdopen(outfd, "rb");
569 if ((*blob)->f == NULL) {
570 err = got_error_from_errno();
576 (*blob)->hdrlen = obj->hdrlen;
577 (*blob)->blocksize = blocksize;
578 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
584 free((*blob)->read_buf);
591 const struct got_error *
592 got_object_open_as_blob(struct got_blob_object **blob,
593 struct got_repository *repo, struct got_object_id *id,
596 const struct got_error *err;
597 struct got_object *obj;
601 err = got_object_open(&obj, repo, id);
604 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
605 err = got_error(GOT_ERR_OBJ_TYPE);
609 err = got_object_blob_open(blob, repo, obj, blocksize);
611 got_object_close(obj);
616 got_object_blob_close(struct got_blob_object *blob)
618 free(blob->read_buf);
624 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
626 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
630 got_object_blob_get_hdrlen(struct got_blob_object *blob)
636 got_object_blob_get_read_buf(struct got_blob_object *blob)
638 return blob->read_buf;
641 const struct got_error *
642 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
646 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
647 if (n == 0 && ferror(blob->f))
648 return got_ferror(blob->f, GOT_ERR_IO);
653 const struct got_error *
654 got_object_blob_dump_to_file(size_t *total_len, size_t *nlines,
655 FILE *outfile, struct got_blob_object *blob)
657 const struct got_error *err = NULL;
667 hdrlen = got_object_blob_get_hdrlen(blob);
669 err = got_object_blob_read_block(&len, blob);
676 buf = got_object_blob_get_read_buf(blob);
678 for (i = 0; i < len; i++) {
683 /* Skip blob object header first time around. */
684 fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
694 static struct got_tree_entry *
695 find_entry_by_name(struct got_tree_object *tree, const char *name)
697 struct got_tree_entry *te;
699 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
700 if (strcmp(te->name, name) == 0)
706 const struct got_error *
707 got_object_open_by_path(struct got_object **obj, struct got_repository *repo,
708 struct got_object_id *commit_id, const char *path)
710 const struct got_error *err = NULL;
711 struct got_commit_object *commit = NULL;
712 struct got_tree_object *tree = NULL;
713 struct got_tree_entry *te = NULL;
714 char *seg, *s, *s0 = NULL;
715 size_t len = strlen(path);
719 /* We are expecting an absolute in-repository path. */
721 return got_error(GOT_ERR_NOT_ABSPATH);
723 err = got_object_open_as_commit(&commit, repo, commit_id);
727 /* Handle opening of root of commit's tree. */
728 if (path[1] == '\0') {
729 err = got_object_open(obj, repo, commit->tree_id);
733 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
739 err = got_error_from_errno();
742 err = got_canonpath(path, s0, len + 1);
747 s++; /* skip leading '/' */
751 struct got_tree_object *next_tree;
760 /* end of path segment */
763 te = find_entry_by_name(tree, seg);
765 err = got_error(GOT_ERR_NO_OBJ);
776 err = got_object_open_as_tree(&next_tree, repo,
781 got_object_tree_close(tree);
787 err = got_object_open(obj, repo, te->id);
789 err = got_error(GOT_ERR_NO_OBJ);
793 got_object_commit_close(commit);
795 got_object_tree_close(tree);