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) {
370 err = extract_packed_object_to_mem(&buf, &len, obj, repo);
374 err = got_object_parse_commit(commit, buf, len);
378 err = open_loose_object(&fd, obj, repo);
381 err = got_object_read_commit_privsep(commit, obj, fd, repo);
387 err = got_repo_cache_commit(repo, &obj->id, *commit);
393 const struct got_error *
394 got_object_tree_open(struct got_tree_object **tree,
395 struct got_repository *repo, struct got_object *obj)
397 const struct got_error *err = NULL;
399 *tree = got_repo_get_cached_tree(repo, &obj->id);
405 if (obj->type != GOT_OBJ_TYPE_TREE)
406 return got_error(GOT_ERR_OBJ_TYPE);
408 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
411 err = extract_packed_object_to_mem(&buf, &len, obj, repo);
415 err = got_object_parse_tree(tree, buf, len);
419 err = open_loose_object(&fd, obj, repo);
422 err = got_object_read_tree_privsep(tree, obj, fd, repo);
428 err = got_repo_cache_tree(repo, &obj->id, *tree);
434 const struct got_error *
435 got_object_open_as_tree(struct got_tree_object **tree,
436 struct got_repository *repo, struct got_object_id *id)
438 const struct got_error *err;
439 struct got_object *obj;
443 err = got_object_open(&obj, repo, id);
446 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
447 err = got_error(GOT_ERR_OBJ_TYPE);
451 err = got_object_tree_open(tree, repo, obj);
453 got_object_close(obj);
457 const struct got_tree_entries *
458 got_object_tree_get_entries(struct got_tree_object *tree)
460 return &tree->entries;
463 static const struct got_error *
464 extract_packed_object(FILE **f, struct got_object *obj,
465 struct got_repository *repo)
467 const struct got_error *err = NULL;
468 struct got_pack *pack;
471 if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
472 return got_error(GOT_ERR_OBJ_NOT_PACKED);
474 fd = got_opentempfd();
476 return got_error_from_errno();
478 *f = fdopen(fd, "w+");
480 err = got_error_from_errno();
484 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
486 err = got_repo_cache_pack(&pack, repo,
487 obj->path_packfile, NULL);
492 err = got_packfile_extract_object(pack, obj, *f);
504 const struct got_error *
505 got_object_blob_open(struct got_blob_object **blob,
506 struct got_repository *repo, struct got_object *obj, size_t blocksize)
508 const struct got_error *err = NULL;
510 if (obj->type != GOT_OBJ_TYPE_BLOB)
511 return got_error(GOT_ERR_OBJ_TYPE);
513 if (blocksize < obj->hdrlen)
514 return got_error(GOT_ERR_NO_SPACE);
516 *blob = calloc(1, sizeof(**blob));
518 return got_error_from_errno();
520 (*blob)->read_buf = malloc(blocksize);
521 if ((*blob)->read_buf == NULL) {
522 err = got_error_from_errno();
525 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
526 err = extract_packed_object(&((*blob)->f), obj, repo);
534 err = open_loose_object(&infd, obj, repo);
539 outfd = got_opentempfd();
541 err = got_error_from_errno();
546 err = got_object_read_blob_privsep(&size, outfd, infd, repo);
551 if (size != obj->hdrlen + obj->size) {
552 err = got_error(GOT_ERR_PRIVSEP_LEN);
557 if (fstat(outfd, &sb) == -1) {
558 err = got_error_from_errno();
563 if (sb.st_size != size) {
564 err = got_error(GOT_ERR_PRIVSEP_LEN);
569 (*blob)->f = fdopen(outfd, "rb");
570 if ((*blob)->f == NULL) {
571 err = got_error_from_errno();
577 (*blob)->hdrlen = obj->hdrlen;
578 (*blob)->blocksize = blocksize;
579 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
585 free((*blob)->read_buf);
592 const struct got_error *
593 got_object_open_as_blob(struct got_blob_object **blob,
594 struct got_repository *repo, struct got_object_id *id,
597 const struct got_error *err;
598 struct got_object *obj;
602 err = got_object_open(&obj, repo, id);
605 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
606 err = got_error(GOT_ERR_OBJ_TYPE);
610 err = got_object_blob_open(blob, repo, obj, blocksize);
612 got_object_close(obj);
617 got_object_blob_close(struct got_blob_object *blob)
619 free(blob->read_buf);
625 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
627 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
631 got_object_blob_get_hdrlen(struct got_blob_object *blob)
637 got_object_blob_get_read_buf(struct got_blob_object *blob)
639 return blob->read_buf;
642 const struct got_error *
643 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
647 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
648 if (n == 0 && ferror(blob->f))
649 return got_ferror(blob->f, GOT_ERR_IO);
654 const struct got_error *
655 got_object_blob_dump_to_file(size_t *total_len, size_t *nlines,
656 FILE *outfile, struct got_blob_object *blob)
658 const struct got_error *err = NULL;
668 hdrlen = got_object_blob_get_hdrlen(blob);
670 err = got_object_blob_read_block(&len, blob);
677 buf = got_object_blob_get_read_buf(blob);
679 for (i = 0; i < len; i++) {
684 /* Skip blob object header first time around. */
685 fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
695 static struct got_tree_entry *
696 find_entry_by_name(struct got_tree_object *tree, const char *name)
698 struct got_tree_entry *te;
700 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
701 if (strcmp(te->name, name) == 0)
707 const struct got_error *
708 got_object_open_by_path(struct got_object **obj, struct got_repository *repo,
709 struct got_object_id *commit_id, const char *path)
711 const struct got_error *err = NULL;
712 struct got_commit_object *commit = NULL;
713 struct got_tree_object *tree = NULL;
714 struct got_tree_entry *te = NULL;
715 char *seg, *s, *s0 = NULL;
716 size_t len = strlen(path);
720 /* We are expecting an absolute in-repository path. */
722 return got_error(GOT_ERR_NOT_ABSPATH);
724 err = got_object_open_as_commit(&commit, repo, commit_id);
728 /* Handle opening of root of commit's tree. */
729 if (path[1] == '\0') {
730 err = got_object_open(obj, repo, commit->tree_id);
734 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
740 err = got_error_from_errno();
743 err = got_canonpath(path, s0, len + 1);
748 s++; /* skip leading '/' */
752 struct got_tree_object *next_tree;
761 /* end of path segment */
764 te = find_entry_by_name(tree, seg);
766 err = got_error(GOT_ERR_NO_OBJ);
777 err = got_object_open_as_tree(&next_tree, repo,
782 got_object_tree_close(tree);
788 err = got_object_open(obj, repo, te->id);
790 err = got_error(GOT_ERR_NO_OBJ);
794 got_object_commit_close(commit);
796 got_object_tree_close(tree);