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))
59 const struct got_error *
60 got_object_id_str(char **outbuf, struct got_object_id *id)
62 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
64 *outbuf = malloc(len);
66 return got_error_from_errno();
68 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
71 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
78 got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
80 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
83 struct got_object_id *
84 got_object_id_dup(struct got_object_id *id1)
86 struct got_object_id *id2;
88 id2 = malloc(sizeof(*id2));
91 memcpy(id2, id1, sizeof(*id2));
95 struct got_object_id *
96 got_object_get_id(struct got_object *obj)
98 return got_object_id_dup(&obj->id);
101 const struct got_error *
102 got_object_get_id_str(char **outbuf, struct got_object *obj)
104 return got_object_id_str(outbuf, &obj->id);
108 got_object_get_type(struct got_object *obj)
111 case GOT_OBJ_TYPE_COMMIT:
112 case GOT_OBJ_TYPE_TREE:
113 case GOT_OBJ_TYPE_BLOB:
114 case GOT_OBJ_TYPE_TAG:
125 static const struct got_error *
126 object_path(char **path, struct got_object_id *id, struct got_repository *repo)
128 const struct got_error *err = NULL;
130 char *path_objects = got_repo_get_path_objects(repo);
134 if (path_objects == NULL)
135 return got_error_from_errno();
137 err = got_object_id_str(&hex, id);
141 if (asprintf(path, "%s/%.2x/%s", path_objects,
142 id->sha1[0], hex + 2) == -1)
143 err = got_error_from_errno();
151 static const struct got_error *
152 open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
154 const struct got_error *err = NULL;
157 err = object_path(&path, &obj->id, repo);
160 *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
162 err = got_error_from_errno();
170 static const struct got_error *
171 get_packfile_path(char **path_packfile, struct got_packidx *packidx)
175 /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
176 size = strlen(packidx->path_packidx) + 2;
177 if (size < GOT_PACKFILE_NAMELEN + 1)
178 return got_error(GOT_ERR_BAD_PATH);
180 *path_packfile = calloc(size, sizeof(**path_packfile));
181 if (*path_packfile == NULL)
182 return got_error_from_errno();
184 /* Copy up to and excluding ".idx". */
185 if (strlcpy(*path_packfile, packidx->path_packidx,
186 size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
187 return got_error(GOT_ERR_NO_SPACE);
189 if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
190 return got_error(GOT_ERR_NO_SPACE);
195 static const struct got_error *
196 open_packed_object(struct got_object **obj, struct got_object_id *id,
197 struct got_repository *repo)
199 const struct got_error *err = NULL;
200 struct got_pack *pack = NULL;
201 struct got_packidx *packidx = NULL;
205 err = got_repo_search_packidx(&packidx, &idx, repo, id);
209 err = get_packfile_path(&path_packfile, packidx);
213 pack = got_repo_get_cached_pack(repo, path_packfile);
215 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
220 err = got_object_packed_read_privsep(obj, repo, pack, packidx, idx, id);
224 err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
230 const struct got_error *
231 got_object_open(struct got_object **obj, struct got_repository *repo,
232 struct got_object_id *id)
234 const struct got_error *err = NULL;
238 *obj = got_repo_get_cached_object(repo, id);
244 err = object_path(&path, id, repo);
248 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
250 if (errno != ENOENT) {
251 err = got_error_from_errno();
254 err = open_packed_object(obj, id, repo);
258 err = got_error(GOT_ERR_NO_OBJ);
260 err = got_object_read_header_privsep(obj, repo, fd);
263 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
268 err = got_repo_cache_object(repo, id, *obj);
278 const struct got_error *
279 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
282 struct got_object_id id;
284 if (!got_parse_sha1_digest(id.sha1, id_str))
285 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
287 return got_object_open(obj, repo, &id);
290 const struct got_error *
291 got_object_open_as_commit(struct got_commit_object **commit,
292 struct got_repository *repo, struct got_object_id *id)
294 const struct got_error *err;
295 struct got_object *obj;
299 err = got_object_open(&obj, repo, id);
302 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
303 err = got_error(GOT_ERR_OBJ_TYPE);
307 err = got_object_commit_open(commit, repo, obj);
309 got_object_close(obj);
313 const struct got_error *
314 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
316 const struct got_error *err = NULL;
318 *qid = calloc(1, sizeof(**qid));
320 return got_error_from_errno();
322 (*qid)->id = got_object_id_dup(id);
323 if ((*qid)->id == NULL) {
324 err = got_error_from_errno();
325 got_object_qid_free(*qid);
333 const struct got_error *
334 got_object_commit_open(struct got_commit_object **commit,
335 struct got_repository *repo, struct got_object *obj)
337 const struct got_error *err = NULL;
339 *commit = got_repo_get_cached_commit(repo, &obj->id);
340 if (*commit != NULL) {
345 if (obj->type != GOT_OBJ_TYPE_COMMIT)
346 return got_error(GOT_ERR_OBJ_TYPE);
348 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
349 struct got_pack *pack;
350 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
352 err = got_repo_cache_pack(&pack, repo,
353 obj->path_packfile, NULL);
357 err = got_object_read_packed_commit_privsep(commit, obj, pack);
360 err = open_loose_object(&fd, obj, repo);
363 err = got_object_read_commit_privsep(commit, obj, fd, repo);
369 err = got_repo_cache_commit(repo, &obj->id, *commit);
375 const struct got_error *
376 got_object_tree_open(struct got_tree_object **tree,
377 struct got_repository *repo, struct got_object *obj)
379 const struct got_error *err = NULL;
381 *tree = got_repo_get_cached_tree(repo, &obj->id);
387 if (obj->type != GOT_OBJ_TYPE_TREE)
388 return got_error(GOT_ERR_OBJ_TYPE);
390 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
391 struct got_pack *pack;
392 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
394 err = got_repo_cache_pack(&pack, repo,
395 obj->path_packfile, NULL);
399 err = got_object_read_packed_tree_privsep(tree, obj, pack);
402 err = open_loose_object(&fd, obj, repo);
405 err = got_object_read_tree_privsep(tree, obj, fd, repo);
411 err = got_repo_cache_tree(repo, &obj->id, *tree);
417 const struct got_error *
418 got_object_open_as_tree(struct got_tree_object **tree,
419 struct got_repository *repo, struct got_object_id *id)
421 const struct got_error *err;
422 struct got_object *obj;
426 err = got_object_open(&obj, repo, id);
429 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
430 err = got_error(GOT_ERR_OBJ_TYPE);
434 err = got_object_tree_open(tree, repo, obj);
436 got_object_close(obj);
440 const struct got_tree_entries *
441 got_object_tree_get_entries(struct got_tree_object *tree)
443 return &tree->entries;
446 const struct got_error *
447 got_object_blob_open(struct got_blob_object **blob,
448 struct got_repository *repo, struct got_object *obj, size_t blocksize)
450 const struct got_error *err = NULL;
455 if (obj->type != GOT_OBJ_TYPE_BLOB)
456 return got_error(GOT_ERR_OBJ_TYPE);
458 if (blocksize < obj->hdrlen)
459 return got_error(GOT_ERR_NO_SPACE);
461 *blob = calloc(1, sizeof(**blob));
463 return got_error_from_errno();
465 outfd = got_opentempfd();
467 return got_error_from_errno();
469 (*blob)->read_buf = malloc(blocksize);
470 if ((*blob)->read_buf == NULL) {
471 err = got_error_from_errno();
474 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
475 struct got_pack *pack;
476 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
478 err = got_repo_cache_pack(&pack, repo,
479 obj->path_packfile, NULL);
483 err = got_object_read_packed_blob_privsep(&size, outfd,
491 err = open_loose_object(&infd, obj, repo);
495 err = got_object_read_blob_privsep(&size, outfd, infd, repo);
500 if (size != obj->hdrlen + obj->size) {
501 err = got_error(GOT_ERR_PRIVSEP_LEN);
506 if (fstat(outfd, &sb) == -1) {
507 err = got_error_from_errno();
511 if (sb.st_size != obj->hdrlen + obj->size) {
512 err = got_error(GOT_ERR_PRIVSEP_LEN);
516 (*blob)->f = fdopen(outfd, "rb");
517 if ((*blob)->f == NULL) {
518 err = got_error_from_errno();
523 (*blob)->hdrlen = obj->hdrlen;
524 (*blob)->blocksize = blocksize;
525 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
532 free((*blob)->read_buf);
535 } else if (outfd != -1)
541 const struct got_error *
542 got_object_open_as_blob(struct got_blob_object **blob,
543 struct got_repository *repo, struct got_object_id *id,
546 const struct got_error *err;
547 struct got_object *obj;
551 err = got_object_open(&obj, repo, id);
554 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
555 err = got_error(GOT_ERR_OBJ_TYPE);
559 err = got_object_blob_open(blob, repo, obj, blocksize);
561 got_object_close(obj);
566 got_object_blob_close(struct got_blob_object *blob)
568 free(blob->read_buf);
574 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
576 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
580 got_object_blob_get_hdrlen(struct got_blob_object *blob)
586 got_object_blob_get_read_buf(struct got_blob_object *blob)
588 return blob->read_buf;
591 const struct got_error *
592 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
596 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
597 if (n == 0 && ferror(blob->f))
598 return got_ferror(blob->f, GOT_ERR_IO);
603 const struct got_error *
604 got_object_blob_dump_to_file(size_t *total_len, size_t *nlines,
605 FILE *outfile, struct got_blob_object *blob)
607 const struct got_error *err = NULL;
617 hdrlen = got_object_blob_get_hdrlen(blob);
619 err = got_object_blob_read_block(&len, blob);
626 buf = got_object_blob_get_read_buf(blob);
628 for (i = 0; i < len; i++) {
633 /* Skip blob object header first time around. */
634 fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
644 static struct got_tree_entry *
645 find_entry_by_name(struct got_tree_object *tree, const char *name)
647 struct got_tree_entry *te;
649 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
650 if (strcmp(te->name, name) == 0)
656 const struct got_error *
657 got_object_open_by_path(struct got_object **obj, struct got_repository *repo,
658 struct got_object_id *commit_id, const char *path)
660 const struct got_error *err = NULL;
661 struct got_commit_object *commit = NULL;
662 struct got_tree_object *tree = NULL;
663 struct got_tree_entry *te = NULL;
664 char *seg, *s, *s0 = NULL;
665 size_t len = strlen(path);
669 /* We are expecting an absolute in-repository path. */
671 return got_error(GOT_ERR_NOT_ABSPATH);
673 err = got_object_open_as_commit(&commit, repo, commit_id);
677 /* Handle opening of root of commit's tree. */
678 if (path[1] == '\0') {
679 err = got_object_open(obj, repo, commit->tree_id);
683 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
689 err = got_error_from_errno();
692 err = got_canonpath(path, s0, len + 1);
697 s++; /* skip leading '/' */
701 struct got_tree_object *next_tree;
710 /* end of path segment */
713 te = find_entry_by_name(tree, seg);
715 err = got_error(GOT_ERR_NO_OBJ);
726 err = got_object_open_as_tree(&next_tree, repo,
731 got_object_tree_close(tree);
737 err = got_object_open(obj, repo, te->id);
739 err = got_error(GOT_ERR_NO_OBJ);
743 got_object_commit_close(commit);
745 got_object_tree_close(tree);