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_parse.h"
51 #include "got_lib_repository.h"
54 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
57 const struct got_error *
58 got_object_id_str(char **outbuf, struct got_object_id *id)
60 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
62 *outbuf = malloc(len);
64 return got_error_from_errno();
66 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
69 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
76 got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
78 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
81 struct got_object_id *
82 got_object_id_dup(struct got_object_id *id1)
84 struct got_object_id *id2;
86 id2 = malloc(sizeof(*id2));
89 memcpy(id2, id1, sizeof(*id2));
93 struct got_object_id *
94 got_object_get_id(struct got_object *obj)
96 return got_object_id_dup(&obj->id);
99 const struct got_error *
100 got_object_get_id_str(char **outbuf, struct got_object *obj)
102 return got_object_id_str(outbuf, &obj->id);
106 got_object_get_type(struct got_object *obj)
109 case GOT_OBJ_TYPE_COMMIT:
110 case GOT_OBJ_TYPE_TREE:
111 case GOT_OBJ_TYPE_BLOB:
112 case GOT_OBJ_TYPE_TAG:
123 static const struct got_error *
124 object_path(char **path, struct got_object_id *id, struct got_repository *repo)
126 const struct got_error *err = NULL;
128 char *path_objects = got_repo_get_path_objects(repo);
132 if (path_objects == NULL)
133 return got_error_from_errno();
135 err = got_object_id_str(&hex, id);
139 if (asprintf(path, "%s/%.2x/%s", path_objects,
140 id->sha1[0], hex + 2) == -1)
141 err = got_error_from_errno();
149 static const struct got_error *
150 open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
152 const struct got_error *err = NULL;
155 err = object_path(&path, &obj->id, repo);
158 *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
160 err = got_error_from_errno();
168 static const struct got_error *
169 get_packfile_path(char **path_packfile, struct got_packidx *packidx)
173 /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
174 size = strlen(packidx->path_packidx) + 2;
175 if (size < GOT_PACKFILE_NAMELEN + 1)
176 return got_error(GOT_ERR_BAD_PATH);
178 *path_packfile = calloc(size, sizeof(**path_packfile));
179 if (*path_packfile == NULL)
180 return got_error_from_errno();
182 /* Copy up to and excluding ".idx". */
183 if (strlcpy(*path_packfile, packidx->path_packidx,
184 size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
185 return got_error(GOT_ERR_NO_SPACE);
187 if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
188 return got_error(GOT_ERR_NO_SPACE);
193 static const struct got_error *
194 open_packed_object(struct got_object **obj, struct got_object_id *id,
195 struct got_repository *repo)
197 const struct got_error *err = NULL;
198 struct got_pack *pack = NULL;
199 struct got_packidx *packidx = NULL;
203 err = got_repo_search_packidx(&packidx, &idx, repo, id);
207 err = get_packfile_path(&path_packfile, packidx);
211 pack = got_repo_get_cached_pack(repo, path_packfile);
213 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
218 err = got_object_packed_read_privsep(obj, repo, pack, packidx, idx, id);
222 err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
228 const struct got_error *
229 got_object_open(struct got_object **obj, struct got_repository *repo,
230 struct got_object_id *id)
232 const struct got_error *err = NULL;
236 *obj = got_repo_get_cached_object(repo, id);
242 err = object_path(&path, id, repo);
246 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
248 if (errno != ENOENT) {
249 err = got_error_from_errno();
252 err = open_packed_object(obj, id, repo);
256 err = got_error(GOT_ERR_NO_OBJ);
258 err = got_object_read_header_privsep(obj, repo, fd);
261 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
266 err = got_repo_cache_object(repo, id, *obj);
276 const struct got_error *
277 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
280 struct got_object_id id;
282 if (!got_parse_sha1_digest(id.sha1, id_str))
283 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
285 return got_object_open(obj, repo, &id);
288 const struct got_error *
289 got_object_open_as_commit(struct got_commit_object **commit,
290 struct got_repository *repo, struct got_object_id *id)
292 const struct got_error *err;
293 struct got_object *obj;
297 err = got_object_open(&obj, repo, id);
300 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
301 err = got_error(GOT_ERR_OBJ_TYPE);
305 err = got_object_commit_open(commit, repo, obj);
307 got_object_close(obj);
311 const struct got_error *
312 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
314 const struct got_error *err = NULL;
316 *qid = calloc(1, sizeof(**qid));
318 return got_error_from_errno();
320 (*qid)->id = got_object_id_dup(id);
321 if ((*qid)->id == NULL) {
322 err = got_error_from_errno();
323 got_object_qid_free(*qid);
331 static const struct got_error *
332 extract_packed_object_to_mem(uint8_t **buf, size_t *len,
333 struct got_object *obj, struct got_repository *repo)
335 const struct got_error *err = NULL;
336 struct got_pack *pack;
338 if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
339 return got_error(GOT_ERR_OBJ_NOT_PACKED);
341 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
343 err = got_repo_cache_pack(&pack, repo,
344 obj->path_packfile, NULL);
349 return got_packfile_extract_object_to_mem(buf, len, obj, pack);
352 const struct got_error *
353 got_object_commit_open(struct got_commit_object **commit,
354 struct got_repository *repo, struct got_object *obj)
356 const struct got_error *err = NULL;
358 *commit = got_repo_get_cached_commit(repo, &obj->id);
359 if (*commit != NULL) {
364 if (obj->type != GOT_OBJ_TYPE_COMMIT)
365 return got_error(GOT_ERR_OBJ_TYPE);
367 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
368 struct got_pack *pack;
369 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
371 err = got_repo_cache_pack(&pack, repo,
372 obj->path_packfile, NULL);
376 err = got_object_read_packed_commit_privsep(commit, obj, pack);
379 err = open_loose_object(&fd, obj, repo);
382 err = got_object_read_commit_privsep(commit, obj, fd, repo);
388 err = got_repo_cache_commit(repo, &obj->id, *commit);
394 const struct got_error *
395 got_object_tree_open(struct got_tree_object **tree,
396 struct got_repository *repo, struct got_object *obj)
398 const struct got_error *err = NULL;
400 *tree = got_repo_get_cached_tree(repo, &obj->id);
406 if (obj->type != GOT_OBJ_TYPE_TREE)
407 return got_error(GOT_ERR_OBJ_TYPE);
409 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
412 err = extract_packed_object_to_mem(&buf, &len, obj, repo);
416 err = got_object_parse_tree(tree, buf, len);
420 err = open_loose_object(&fd, obj, repo);
423 err = got_object_read_tree_privsep(tree, obj, fd, repo);
429 err = got_repo_cache_tree(repo, &obj->id, *tree);
435 const struct got_error *
436 got_object_open_as_tree(struct got_tree_object **tree,
437 struct got_repository *repo, struct got_object_id *id)
439 const struct got_error *err;
440 struct got_object *obj;
444 err = got_object_open(&obj, repo, id);
447 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
448 err = got_error(GOT_ERR_OBJ_TYPE);
452 err = got_object_tree_open(tree, repo, obj);
454 got_object_close(obj);
458 const struct got_tree_entries *
459 got_object_tree_get_entries(struct got_tree_object *tree)
461 return &tree->entries;
464 static const struct got_error *
465 extract_packed_object(FILE **f, struct got_object *obj,
466 struct got_repository *repo)
468 const struct got_error *err = NULL;
469 struct got_pack *pack;
472 if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
473 return got_error(GOT_ERR_OBJ_NOT_PACKED);
475 fd = got_opentempfd();
477 return got_error_from_errno();
479 *f = fdopen(fd, "w+");
481 err = got_error_from_errno();
485 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
487 err = got_repo_cache_pack(&pack, repo,
488 obj->path_packfile, NULL);
493 err = got_packfile_extract_object(pack, obj, *f);
505 const struct got_error *
506 got_object_blob_open(struct got_blob_object **blob,
507 struct got_repository *repo, struct got_object *obj, size_t blocksize)
509 const struct got_error *err = NULL;
511 if (obj->type != GOT_OBJ_TYPE_BLOB)
512 return got_error(GOT_ERR_OBJ_TYPE);
514 if (blocksize < obj->hdrlen)
515 return got_error(GOT_ERR_NO_SPACE);
517 *blob = calloc(1, sizeof(**blob));
519 return got_error_from_errno();
521 (*blob)->read_buf = malloc(blocksize);
522 if ((*blob)->read_buf == NULL) {
523 err = got_error_from_errno();
526 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
527 err = extract_packed_object(&((*blob)->f), obj, repo);
535 err = open_loose_object(&infd, obj, repo);
540 outfd = got_opentempfd();
542 err = got_error_from_errno();
547 err = got_object_read_blob_privsep(&size, outfd, infd, repo);
552 if (size != obj->hdrlen + obj->size) {
553 err = got_error(GOT_ERR_PRIVSEP_LEN);
558 if (fstat(outfd, &sb) == -1) {
559 err = got_error_from_errno();
564 if (sb.st_size != size) {
565 err = got_error(GOT_ERR_PRIVSEP_LEN);
570 (*blob)->f = fdopen(outfd, "rb");
571 if ((*blob)->f == NULL) {
572 err = got_error_from_errno();
578 (*blob)->hdrlen = obj->hdrlen;
579 (*blob)->blocksize = blocksize;
580 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
586 free((*blob)->read_buf);
593 const struct got_error *
594 got_object_open_as_blob(struct got_blob_object **blob,
595 struct got_repository *repo, struct got_object_id *id,
598 const struct got_error *err;
599 struct got_object *obj;
603 err = got_object_open(&obj, repo, id);
606 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
607 err = got_error(GOT_ERR_OBJ_TYPE);
611 err = got_object_blob_open(blob, repo, obj, blocksize);
613 got_object_close(obj);
618 got_object_blob_close(struct got_blob_object *blob)
620 free(blob->read_buf);
626 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
628 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
632 got_object_blob_get_hdrlen(struct got_blob_object *blob)
638 got_object_blob_get_read_buf(struct got_blob_object *blob)
640 return blob->read_buf;
643 const struct got_error *
644 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
648 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
649 if (n == 0 && ferror(blob->f))
650 return got_ferror(blob->f, GOT_ERR_IO);
655 const struct got_error *
656 got_object_blob_dump_to_file(size_t *total_len, size_t *nlines,
657 FILE *outfile, struct got_blob_object *blob)
659 const struct got_error *err = NULL;
669 hdrlen = got_object_blob_get_hdrlen(blob);
671 err = got_object_blob_read_block(&len, blob);
678 buf = got_object_blob_get_read_buf(blob);
680 for (i = 0; i < len; i++) {
685 /* Skip blob object header first time around. */
686 fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
696 static struct got_tree_entry *
697 find_entry_by_name(struct got_tree_object *tree, const char *name)
699 struct got_tree_entry *te;
701 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
702 if (strcmp(te->name, name) == 0)
708 const struct got_error *
709 got_object_open_by_path(struct got_object **obj, struct got_repository *repo,
710 struct got_object_id *commit_id, const char *path)
712 const struct got_error *err = NULL;
713 struct got_commit_object *commit = NULL;
714 struct got_tree_object *tree = NULL;
715 struct got_tree_entry *te = NULL;
716 char *seg, *s, *s0 = NULL;
717 size_t len = strlen(path);
721 /* We are expecting an absolute in-repository path. */
723 return got_error(GOT_ERR_NOT_ABSPATH);
725 err = got_object_open_as_commit(&commit, repo, commit_id);
729 /* Handle opening of root of commit's tree. */
730 if (path[1] == '\0') {
731 err = got_object_open(obj, repo, commit->tree_id);
735 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
741 err = got_error_from_errno();
744 err = got_canonpath(path, s0, len + 1);
749 s++; /* skip leading '/' */
753 struct got_tree_object *next_tree;
762 /* end of path segment */
765 te = find_entry_by_name(tree, seg);
767 err = got_error(GOT_ERR_NO_OBJ);
778 err = got_object_open_as_tree(&next_tree, repo,
783 got_object_tree_close(tree);
789 err = got_object_open(obj, repo, te->id);
791 err = got_error(GOT_ERR_NO_OBJ);
795 got_object_commit_close(commit);
797 got_object_tree_close(tree);