Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/queue.h>
20 #include <sys/uio.h>
21 #include <sys/socket.h>
22 #include <sys/wait.h>
23 #include <sys/syslimits.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdint.h>
31 #include <sha1.h>
32 #include <zlib.h>
33 #include <ctype.h>
34 #include <limits.h>
35 #include <imsg.h>
36 #include <time.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"
55 #ifndef MIN
56 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
57 #endif
59 int
60 got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
61 {
62 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
63 }
65 struct got_object_id *
66 got_object_id_dup(struct got_object_id *id1)
67 {
68 struct got_object_id *id2;
70 id2 = malloc(sizeof(*id2));
71 if (id2 == NULL)
72 return NULL;
73 memcpy(id2, id1, sizeof(*id2));
74 return id2;
75 }
77 struct got_object_id *
78 got_object_get_id(struct got_object *obj)
79 {
80 return &obj->id;
81 }
83 const struct got_error *
84 got_object_get_id_str(char **outbuf, struct got_object *obj)
85 {
86 return got_object_id_str(outbuf, &obj->id);
87 }
89 int
90 got_object_get_type(struct got_object *obj)
91 {
92 switch (obj->type) {
93 case GOT_OBJ_TYPE_COMMIT:
94 case GOT_OBJ_TYPE_TREE:
95 case GOT_OBJ_TYPE_BLOB:
96 case GOT_OBJ_TYPE_TAG:
97 return obj->type;
98 default:
99 abort();
100 break;
103 /* not reached */
104 return 0;
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;
111 char *hex = NULL;
112 char *path_objects = got_repo_get_path_objects(repo);
114 *path = NULL;
116 if (path_objects == NULL)
117 return got_error_from_errno();
119 err = got_object_id_str(&hex, id);
120 if (err)
121 goto done;
123 if (asprintf(path, "%s/%.2x/%s", path_objects,
124 id->sha1[0], hex + 2) == -1)
125 err = got_error_from_errno();
127 done:
128 free(hex);
129 free(path_objects);
130 return err;
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;
137 char *path;
139 err = object_path(&path, &obj->id, repo);
140 if (err)
141 return err;
142 *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
143 if (*fd == -1) {
144 err = got_error_from_errno();
145 goto done;
147 done:
148 free(path);
149 return err;
152 static const struct got_error *
153 get_packfile_path(char **path_packfile, struct got_packidx *packidx)
155 size_t size;
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);
174 return NULL;
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;
184 int idx;
185 char *path_packfile;
187 err = got_repo_search_packidx(&packidx, &idx, repo, id);
188 if (err)
189 return err;
191 err = get_packfile_path(&path_packfile, packidx);
192 if (err)
193 return err;
195 pack = got_repo_get_cached_pack(repo, path_packfile);
196 if (pack == NULL) {
197 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
198 if (err)
199 goto done;
202 err = got_object_packed_read_privsep(obj, repo, pack, packidx, idx, id);
203 if (err)
204 goto done;
206 err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
207 done:
208 free(path_packfile);
209 return err;
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;
217 char *path;
218 int fd;
220 *obj = got_repo_get_cached_object(repo, id);
221 if (*obj != NULL) {
222 (*obj)->refcnt++;
223 return NULL;
226 err = object_path(&path, id, repo);
227 if (err)
228 return err;
230 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
231 if (fd == -1) {
232 if (errno != ENOENT) {
233 err = got_error_from_errno();
234 goto done;
236 err = open_packed_object(obj, id, repo);
237 if (err)
238 goto done;
239 if (*obj == NULL)
240 err = got_error(GOT_ERR_NO_OBJ);
241 } else {
242 err = got_object_read_header_privsep(obj, repo, fd);
243 if (err)
244 goto done;
245 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
248 if (err == NULL) {
249 (*obj)->refcnt++;
250 err = got_repo_cache_object(repo, id, *obj);
252 done:
253 free(path);
254 if (fd != -1)
255 close(fd);
256 return err;
260 const struct got_error *
261 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
262 const char *id_str)
264 struct got_object_id id;
266 if (!got_parse_sha1_digest(id.sha1, id_str))
267 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
269 return got_object_open(obj, repo, &id);
272 const struct got_error *
273 got_object_open_as_commit(struct got_commit_object **commit,
274 struct got_repository *repo, struct got_object_id *id)
276 const struct got_error *err;
277 struct got_object *obj;
279 *commit = NULL;
281 err = got_object_open(&obj, repo, id);
282 if (err)
283 return err;
284 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
285 err = got_error(GOT_ERR_OBJ_TYPE);
286 goto done;
289 err = got_object_commit_open(commit, repo, obj);
290 done:
291 got_object_close(obj);
292 return err;
295 const struct got_error *
296 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
298 const struct got_error *err = NULL;
300 *qid = calloc(1, sizeof(**qid));
301 if (*qid == NULL)
302 return got_error_from_errno();
304 (*qid)->id = got_object_id_dup(id);
305 if ((*qid)->id == NULL) {
306 err = got_error_from_errno();
307 got_object_qid_free(*qid);
308 *qid = NULL;
309 return err;
312 return NULL;
315 const struct got_error *
316 got_object_commit_open(struct got_commit_object **commit,
317 struct got_repository *repo, struct got_object *obj)
319 const struct got_error *err = NULL;
321 *commit = got_repo_get_cached_commit(repo, &obj->id);
322 if (*commit != NULL) {
323 (*commit)->refcnt++;
324 return NULL;
327 if (obj->type != GOT_OBJ_TYPE_COMMIT)
328 return got_error(GOT_ERR_OBJ_TYPE);
330 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
331 struct got_pack *pack;
332 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
333 if (pack == NULL) {
334 err = got_repo_cache_pack(&pack, repo,
335 obj->path_packfile, NULL);
336 if (err)
337 return err;
339 err = got_object_read_packed_commit_privsep(commit, obj, pack);
340 } else {
341 int fd;
342 err = open_loose_object(&fd, obj, repo);
343 if (err)
344 return err;
345 err = got_object_read_commit_privsep(commit, obj, fd, repo);
346 close(fd);
349 if (err == NULL) {
350 (*commit)->refcnt++;
351 err = got_repo_cache_commit(repo, &obj->id, *commit);
354 return err;
357 const struct got_error *
358 got_object_tree_open(struct got_tree_object **tree,
359 struct got_repository *repo, struct got_object *obj)
361 const struct got_error *err = NULL;
363 *tree = got_repo_get_cached_tree(repo, &obj->id);
364 if (*tree != NULL) {
365 (*tree)->refcnt++;
366 return NULL;
369 if (obj->type != GOT_OBJ_TYPE_TREE)
370 return got_error(GOT_ERR_OBJ_TYPE);
372 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
373 struct got_pack *pack;
374 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
375 if (pack == NULL) {
376 err = got_repo_cache_pack(&pack, repo,
377 obj->path_packfile, NULL);
378 if (err)
379 return err;
381 err = got_object_read_packed_tree_privsep(tree, obj, pack);
382 } else {
383 int fd;
384 err = open_loose_object(&fd, obj, repo);
385 if (err)
386 return err;
387 err = got_object_read_tree_privsep(tree, obj, fd, repo);
388 close(fd);
391 if (err == NULL) {
392 (*tree)->refcnt++;
393 err = got_repo_cache_tree(repo, &obj->id, *tree);
396 return err;
399 const struct got_error *
400 got_object_open_as_tree(struct got_tree_object **tree,
401 struct got_repository *repo, struct got_object_id *id)
403 const struct got_error *err;
404 struct got_object *obj;
406 *tree = NULL;
408 err = got_object_open(&obj, repo, id);
409 if (err)
410 return err;
411 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
412 err = got_error(GOT_ERR_OBJ_TYPE);
413 goto done;
416 err = got_object_tree_open(tree, repo, obj);
417 done:
418 got_object_close(obj);
419 return err;
422 const struct got_tree_entries *
423 got_object_tree_get_entries(struct got_tree_object *tree)
425 return &tree->entries;
428 static const struct got_error *
429 read_packed_blob_privsep(size_t *size, int outfd, struct got_object *obj,
430 struct got_pack *pack)
432 const struct got_error *err = NULL;
433 int outfd_child;
434 int basefd, accumfd; /* temporary files for delta application */
436 basefd = got_opentempfd();
437 if (basefd == -1)
438 return got_error_from_errno();
439 accumfd = got_opentempfd();
440 if (accumfd == -1)
441 return got_error_from_errno();
443 outfd_child = dup(outfd);
444 if (outfd_child == -1)
445 return got_error_from_errno();
447 err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
448 if (err)
449 return err;
451 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
452 outfd_child);
453 if (err) {
454 close(outfd_child);
455 return err;
457 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
458 basefd);
459 if (err) {
460 close(basefd);
461 close(accumfd);
462 close(outfd_child);
463 return err;
466 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
467 accumfd);
468 if (err) {
469 close(accumfd);
470 close(outfd_child);
471 return err;
474 err = got_privsep_recv_blob(size, pack->privsep_child->ibuf);
475 if (err)
476 return err;
478 if (lseek(outfd, SEEK_SET, 0) == -1)
479 err = got_error_from_errno();
481 return err;
484 const struct got_error *
485 got_object_blob_open(struct got_blob_object **blob,
486 struct got_repository *repo, struct got_object *obj, size_t blocksize)
488 const struct got_error *err = NULL;
489 int outfd;
490 size_t size;
491 struct stat sb;
493 if (obj->type != GOT_OBJ_TYPE_BLOB)
494 return got_error(GOT_ERR_OBJ_TYPE);
496 if (blocksize < obj->hdrlen)
497 return got_error(GOT_ERR_NO_SPACE);
499 *blob = calloc(1, sizeof(**blob));
500 if (*blob == NULL)
501 return got_error_from_errno();
503 outfd = got_opentempfd();
504 if (outfd == -1)
505 return got_error_from_errno();
507 (*blob)->read_buf = malloc(blocksize);
508 if ((*blob)->read_buf == NULL) {
509 err = got_error_from_errno();
510 goto done;
512 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
513 struct got_pack *pack;
514 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
515 if (pack == NULL) {
516 err = got_repo_cache_pack(&pack, repo,
517 obj->path_packfile, NULL);
518 if (err)
519 goto done;
521 err = read_packed_blob_privsep(&size, outfd, obj, pack);
522 if (err)
523 goto done;
524 obj->size = size;
525 } else {
526 int infd;
528 err = open_loose_object(&infd, obj, repo);
529 if (err)
530 goto done;
532 err = got_object_read_blob_privsep(&size, outfd, infd, repo);
533 close(infd);
534 if (err)
535 goto done;
537 if (size != obj->hdrlen + obj->size) {
538 err = got_error(GOT_ERR_PRIVSEP_LEN);
539 goto done;
543 if (fstat(outfd, &sb) == -1) {
544 err = got_error_from_errno();
545 goto done;
548 if (sb.st_size != obj->hdrlen + obj->size) {
549 err = got_error(GOT_ERR_PRIVSEP_LEN);
550 goto done;
553 (*blob)->f = fdopen(outfd, "rb");
554 if ((*blob)->f == NULL) {
555 err = got_error_from_errno();
556 close(outfd);
557 goto done;
560 (*blob)->hdrlen = obj->hdrlen;
561 (*blob)->blocksize = blocksize;
562 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
564 done:
565 if (err) {
566 if (*blob) {
567 if ((*blob)->f)
568 fclose((*blob)->f);
569 free((*blob)->read_buf);
570 free(*blob);
571 *blob = NULL;
572 } else if (outfd != -1)
573 close(outfd);
575 return err;
578 const struct got_error *
579 got_object_open_as_blob(struct got_blob_object **blob,
580 struct got_repository *repo, struct got_object_id *id,
581 size_t blocksize)
583 const struct got_error *err;
584 struct got_object *obj;
586 *blob = NULL;
588 err = got_object_open(&obj, repo, id);
589 if (err)
590 return err;
591 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
592 err = got_error(GOT_ERR_OBJ_TYPE);
593 goto done;
596 err = got_object_blob_open(blob, repo, obj, blocksize);
597 done:
598 got_object_close(obj);
599 return err;
602 void
603 got_object_blob_close(struct got_blob_object *blob)
605 free(blob->read_buf);
606 fclose(blob->f);
607 free(blob);
610 char *
611 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
613 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
616 size_t
617 got_object_blob_get_hdrlen(struct got_blob_object *blob)
619 return blob->hdrlen;
622 const uint8_t *
623 got_object_blob_get_read_buf(struct got_blob_object *blob)
625 return blob->read_buf;
628 const struct got_error *
629 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
631 size_t n;
633 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
634 if (n == 0 && ferror(blob->f))
635 return got_ferror(blob->f, GOT_ERR_IO);
636 *outlenp = n;
637 return NULL;
640 const struct got_error *
641 got_object_blob_dump_to_file(size_t *total_len, size_t *nlines,
642 FILE *outfile, struct got_blob_object *blob)
644 const struct got_error *err = NULL;
645 size_t len, hdrlen;
646 const uint8_t *buf;
647 int i;
649 if (total_len)
650 *total_len = 0;
651 if (nlines)
652 *nlines = 0;
654 hdrlen = got_object_blob_get_hdrlen(blob);
655 do {
656 err = got_object_blob_read_block(&len, blob);
657 if (err)
658 return err;
659 if (len == 0)
660 break;
661 if (total_len)
662 *total_len += len;
663 buf = got_object_blob_get_read_buf(blob);
664 if (nlines) {
665 for (i = 0; i < len; i++) {
666 if (buf[i] == '\n')
667 (*nlines)++;
670 /* Skip blob object header first time around. */
671 fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
672 hdrlen = 0;
673 } while (len != 0);
675 fflush(outfile);
676 rewind(outfile);
678 return NULL;
681 static struct got_tree_entry *
682 find_entry_by_name(struct got_tree_object *tree, const char *name)
684 struct got_tree_entry *te;
686 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
687 if (strcmp(te->name, name) == 0)
688 return te;
690 return NULL;
693 const struct got_error *
694 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
695 struct got_object_id *commit_id, const char *path)
697 const struct got_error *err = NULL;
698 struct got_commit_object *commit = NULL;
699 struct got_tree_object *tree = NULL;
700 struct got_tree_entry *te = NULL;
701 char *seg, *s, *s0 = NULL;
702 size_t len = strlen(path);
704 *id = NULL;
706 /* We are expecting an absolute in-repository path. */
707 if (path[0] != '/')
708 return got_error(GOT_ERR_NOT_ABSPATH);
710 err = got_object_open_as_commit(&commit, repo, commit_id);
711 if (err)
712 goto done;
714 /* Handle opening of root of commit's tree. */
715 if (path[1] == '\0') {
716 *id = got_object_id_dup(commit->tree_id);
717 if (*id == NULL)
718 err = got_error_from_errno();
719 goto done;
722 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
723 if (err)
724 goto done;
726 s0 = strdup(path);
727 if (s0 == NULL) {
728 err = got_error_from_errno();
729 goto done;
731 err = got_canonpath(path, s0, len + 1);
732 if (err)
733 goto done;
735 s = s0;
736 s++; /* skip leading '/' */
737 len--;
738 seg = s;
739 while (len > 0) {
740 struct got_tree_object *next_tree;
742 if (*s != '/') {
743 s++;
744 len--;
745 if (*s)
746 continue;
749 /* end of path segment */
750 *s = '\0';
752 te = find_entry_by_name(tree, seg);
753 if (te == NULL) {
754 err = got_error(GOT_ERR_NO_OBJ);
755 goto done;
758 if (len == 0)
759 break;
761 seg = s + 1;
762 s++;
763 len--;
764 if (*s) {
765 err = got_object_open_as_tree(&next_tree, repo,
766 te->id);
767 te = NULL;
768 if (err)
769 goto done;
770 got_object_tree_close(tree);
771 tree = next_tree;
775 if (te) {
776 *id = got_object_id_dup(te->id);
777 if (*id == NULL)
778 return got_error_from_errno();
779 } else
780 err = got_error(GOT_ERR_NO_OBJ);
781 done:
782 free(s0);
783 if (commit)
784 got_object_commit_close(commit);
785 if (tree)
786 got_object_tree_close(tree);
787 return err;
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;
798 char *seg, *s, *s0 = NULL;
799 size_t len = strlen(path);
801 *changed = 0;
803 /* We are expecting an absolute in-repository path. */
804 if (path[0] != '/')
805 return got_error(GOT_ERR_NOT_ABSPATH);
807 /* We not do support comparing the root path. */
808 if (path[1] == '\0')
809 return got_error(GOT_ERR_BAD_PATH);
811 s0 = strdup(path);
812 if (s0 == NULL) {
813 err = got_error_from_errno();
814 goto done;
816 err = got_canonpath(path, s0, len + 1);
817 if (err)
818 goto done;
820 tree1 = tree01;
821 tree2 = tree02;
822 s = s0;
823 s++; /* skip leading '/' */
824 len--;
825 seg = s;
826 while (len > 0) {
827 struct got_tree_object *next_tree1, *next_tree2;
829 if (*s != '/') {
830 s++;
831 len--;
832 if (*s)
833 continue;
836 /* end of path segment */
837 *s = '\0';
839 te1 = find_entry_by_name(tree1, seg);
840 if (te1 == NULL) {
841 err = got_error(GOT_ERR_NO_OBJ);
842 goto done;
845 te2 = find_entry_by_name(tree2, seg);
846 if (te2 == NULL) {
847 *changed = 1;
848 goto done;
851 if (te1->mode != te2->mode) {
852 *changed = 1;
853 goto done;
856 if (got_object_id_cmp(te1->id, te2->id) == 0) {
857 *changed = 0;
858 goto done;
861 if (S_ISREG(te1->mode)) { /* final path element */
862 *changed = 1;
863 goto done;
866 if (len == 0)
867 break;
869 seg = s + 1;
870 s++;
871 len--;
872 if (*s) {
873 err = got_object_open_as_tree(&next_tree1, repo,
874 te1->id);
875 te1 = NULL;
876 if (err)
877 goto done;
878 tree1 = next_tree1;
880 err = got_object_open_as_tree(&next_tree2, repo,
881 te2->id);
882 te2 = NULL;
883 if (err)
884 goto done;
885 tree2 = next_tree2;
888 done:
889 free(s0);
890 if (tree1 != tree01)
891 got_object_tree_close(tree1);
892 if (tree2 != tree02)
893 got_object_tree_close(tree2);
894 return err;