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_parse.h"
51 #include "got_lib_repository.h"
53 #ifndef MIN
54 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
55 #endif
57 const struct got_error *
58 got_object_id_str(char **outbuf, struct got_object_id *id)
59 {
60 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
62 *outbuf = malloc(len);
63 if (*outbuf == NULL)
64 return got_error_from_errno();
66 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
67 free(*outbuf);
68 *outbuf = NULL;
69 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
70 }
72 return NULL;
73 }
75 int
76 got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
77 {
78 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
79 }
81 struct got_object_id *
82 got_object_id_dup(struct got_object_id *id1)
83 {
84 struct got_object_id *id2;
86 id2 = malloc(sizeof(*id2));
87 if (id2 == NULL)
88 return NULL;
89 memcpy(id2, id1, sizeof(*id2));
90 return id2;
91 }
93 struct got_object_id *
94 got_object_get_id(struct got_object *obj)
95 {
96 return got_object_id_dup(&obj->id);
97 }
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);
105 int
106 got_object_get_type(struct got_object *obj)
108 switch (obj->type) {
109 case GOT_OBJ_TYPE_COMMIT:
110 case GOT_OBJ_TYPE_TREE:
111 case GOT_OBJ_TYPE_BLOB:
112 case GOT_OBJ_TYPE_TAG:
113 return obj->type;
114 default:
115 abort();
116 break;
119 /* not reached */
120 return 0;
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;
127 char *hex = NULL;
128 char *path_objects = got_repo_get_path_objects(repo);
130 *path = NULL;
132 if (path_objects == NULL)
133 return got_error_from_errno();
135 err = got_object_id_str(&hex, id);
136 if (err)
137 goto done;
139 if (asprintf(path, "%s/%.2x/%s", path_objects,
140 id->sha1[0], hex + 2) == -1)
141 err = got_error_from_errno();
143 done:
144 free(hex);
145 free(path_objects);
146 return err;
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;
153 char *path;
155 err = object_path(&path, &obj->id, repo);
156 if (err)
157 return err;
158 *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
159 if (*fd == -1) {
160 err = got_error_from_errno();
161 goto done;
163 done:
164 free(path);
165 return err;
168 static const struct got_error *
169 get_packfile_path(char **path_packfile, struct got_packidx *packidx)
171 size_t size;
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);
190 return NULL;
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;
200 int idx;
201 char *path_packfile;
203 err = got_repo_search_packidx(&packidx, &idx, repo, id);
204 if (err)
205 return err;
207 err = get_packfile_path(&path_packfile, packidx);
208 if (err)
209 return err;
211 pack = got_repo_get_cached_pack(repo, path_packfile);
212 if (pack == NULL) {
213 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
214 if (err)
215 goto done;
218 err = got_object_packed_read_privsep(obj, repo, pack, packidx, idx, id);
219 if (err)
220 goto done;
222 err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
223 done:
224 free(path_packfile);
225 return err;
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;
233 char *path;
234 int fd;
236 *obj = got_repo_get_cached_object(repo, id);
237 if (*obj != NULL) {
238 (*obj)->refcnt++;
239 return NULL;
242 err = object_path(&path, id, repo);
243 if (err)
244 return err;
246 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
247 if (fd == -1) {
248 if (errno != ENOENT) {
249 err = got_error_from_errno();
250 goto done;
252 err = open_packed_object(obj, id, repo);
253 if (err)
254 goto done;
255 if (*obj == NULL)
256 err = got_error(GOT_ERR_NO_OBJ);
257 } else {
258 err = got_object_read_header_privsep(obj, repo, fd);
259 if (err)
260 goto done;
261 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
264 if (err == NULL) {
265 (*obj)->refcnt++;
266 err = got_repo_cache_object(repo, id, *obj);
268 done:
269 free(path);
270 if (fd != -1)
271 close(fd);
272 return err;
276 const struct got_error *
277 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
278 const char *id_str)
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;
295 *commit = NULL;
297 err = got_object_open(&obj, repo, id);
298 if (err)
299 return err;
300 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
301 err = got_error(GOT_ERR_OBJ_TYPE);
302 goto done;
305 err = got_object_commit_open(commit, repo, obj);
306 done:
307 got_object_close(obj);
308 return err;
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));
317 if (*qid == NULL)
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);
324 *qid = NULL;
325 return err;
328 return NULL;
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);
342 if (pack == NULL) {
343 err = got_repo_cache_pack(&pack, repo,
344 obj->path_packfile, NULL);
345 if (err)
346 return err;
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) {
360 (*commit)->refcnt++;
361 return 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 uint8_t *buf;
369 size_t len;
370 err = extract_packed_object_to_mem(&buf, &len, obj, repo);
371 if (err)
372 return err;
373 obj->size = len;
374 err = got_object_parse_commit(commit, buf, len);
375 free(buf);
376 } else {
377 int fd;
378 err = open_loose_object(&fd, obj, repo);
379 if (err)
380 return err;
381 err = got_object_read_commit_privsep(commit, obj, fd, repo);
382 close(fd);
385 if (err == NULL) {
386 (*commit)->refcnt++;
387 err = got_repo_cache_commit(repo, &obj->id, *commit);
390 return err;
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);
400 if (*tree != NULL) {
401 (*tree)->refcnt++;
402 return NULL;
405 if (obj->type != GOT_OBJ_TYPE_TREE)
406 return got_error(GOT_ERR_OBJ_TYPE);
408 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
409 uint8_t *buf;
410 size_t len;
411 err = extract_packed_object_to_mem(&buf, &len, obj, repo);
412 if (err)
413 return err;
414 obj->size = len;
415 err = got_object_parse_tree(tree, buf, len);
416 free(buf);
417 } else {
418 int fd;
419 err = open_loose_object(&fd, obj, repo);
420 if (err)
421 return err;
422 err = got_object_read_tree_privsep(tree, obj, fd, repo);
423 close(fd);
426 if (err == NULL) {
427 (*tree)->refcnt++;
428 err = got_repo_cache_tree(repo, &obj->id, *tree);
431 return err;
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;
441 *tree = NULL;
443 err = got_object_open(&obj, repo, id);
444 if (err)
445 return err;
446 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
447 err = got_error(GOT_ERR_OBJ_TYPE);
448 goto done;
451 err = got_object_tree_open(tree, repo, obj);
452 done:
453 got_object_close(obj);
454 return err;
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;
469 int fd;
471 if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
472 return got_error(GOT_ERR_OBJ_NOT_PACKED);
474 fd = got_opentempfd();
475 if (fd == -1)
476 return got_error_from_errno();
478 *f = fdopen(fd, "w+");
479 if (*f == NULL) {
480 err = got_error_from_errno();
481 goto done;
484 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
485 if (pack == NULL) {
486 err = got_repo_cache_pack(&pack, repo,
487 obj->path_packfile, NULL);
488 if (err)
489 goto done;
492 err = got_packfile_extract_object(pack, obj, *f);
493 done:
494 if (err) {
495 if (*f == NULL)
496 close(fd);
497 else
498 fclose(*f);
499 *f = NULL;
501 return err;
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));
517 if (*blob == NULL)
518 return got_error_from_errno();
520 (*blob)->read_buf = malloc(blocksize);
521 if ((*blob)->read_buf == NULL) {
522 err = got_error_from_errno();
523 goto done;
525 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
526 err = extract_packed_object(&((*blob)->f), obj, repo);
527 if (err)
528 goto done;
529 } else {
530 int infd, outfd;
531 size_t size;
532 struct stat sb;
534 err = open_loose_object(&infd, obj, repo);
535 if (err)
536 goto done;
539 outfd = got_opentempfd();
540 if (outfd == -1) {
541 err = got_error_from_errno();
542 close(infd);
543 goto done;
546 err = got_object_read_blob_privsep(&size, outfd, infd, repo);
547 close(infd);
548 if (err)
549 goto done;
551 if (size != obj->hdrlen + obj->size) {
552 err = got_error(GOT_ERR_PRIVSEP_LEN);
553 close(outfd);
554 goto done;
557 if (fstat(outfd, &sb) == -1) {
558 err = got_error_from_errno();
559 close(outfd);
560 goto done;
563 if (sb.st_size != size) {
564 err = got_error(GOT_ERR_PRIVSEP_LEN);
565 close(outfd);
566 goto done;
569 (*blob)->f = fdopen(outfd, "rb");
570 if ((*blob)->f == NULL) {
571 err = got_error_from_errno();
572 close(outfd);
573 goto done;
577 (*blob)->hdrlen = obj->hdrlen;
578 (*blob)->blocksize = blocksize;
579 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
581 done:
582 if (err && *blob) {
583 if ((*blob)->f)
584 fclose((*blob)->f);
585 free((*blob)->read_buf);
586 free(*blob);
587 *blob = NULL;
589 return err;
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,
595 size_t blocksize)
597 const struct got_error *err;
598 struct got_object *obj;
600 *blob = NULL;
602 err = got_object_open(&obj, repo, id);
603 if (err)
604 return err;
605 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
606 err = got_error(GOT_ERR_OBJ_TYPE);
607 goto done;
610 err = got_object_blob_open(blob, repo, obj, blocksize);
611 done:
612 got_object_close(obj);
613 return err;
616 void
617 got_object_blob_close(struct got_blob_object *blob)
619 free(blob->read_buf);
620 fclose(blob->f);
621 free(blob);
624 char *
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);
630 size_t
631 got_object_blob_get_hdrlen(struct got_blob_object *blob)
633 return blob->hdrlen;
636 const uint8_t *
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)
645 size_t n;
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);
650 *outlenp = n;
651 return NULL;
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;
659 size_t len, hdrlen;
660 const uint8_t *buf;
661 int i;
663 if (total_len)
664 *total_len = 0;
665 if (nlines)
666 *nlines = 0;
668 hdrlen = got_object_blob_get_hdrlen(blob);
669 do {
670 err = got_object_blob_read_block(&len, blob);
671 if (err)
672 return err;
673 if (len == 0)
674 break;
675 if (total_len)
676 *total_len += len;
677 buf = got_object_blob_get_read_buf(blob);
678 if (nlines) {
679 for (i = 0; i < len; i++) {
680 if (buf[i] == '\n')
681 (*nlines)++;
684 /* Skip blob object header first time around. */
685 fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
686 hdrlen = 0;
687 } while (len != 0);
689 fflush(outfile);
690 rewind(outfile);
692 return NULL;
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)
702 return te;
704 return NULL;
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);
718 *obj = NULL;
720 /* We are expecting an absolute in-repository path. */
721 if (path[0] != '/')
722 return got_error(GOT_ERR_NOT_ABSPATH);
724 err = got_object_open_as_commit(&commit, repo, commit_id);
725 if (err)
726 goto done;
728 /* Handle opening of root of commit's tree. */
729 if (path[1] == '\0') {
730 err = got_object_open(obj, repo, commit->tree_id);
731 goto done;
734 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
735 if (err)
736 goto done;
738 s0 = strdup(path);
739 if (s0 == NULL) {
740 err = got_error_from_errno();
741 goto done;
743 err = got_canonpath(path, s0, len + 1);
744 if (err)
745 goto done;
747 s = s0;
748 s++; /* skip leading '/' */
749 len--;
750 seg = s;
751 while (len > 0) {
752 struct got_tree_object *next_tree;
754 if (*s != '/') {
755 s++;
756 len--;
757 if (*s)
758 continue;
761 /* end of path segment */
762 *s = '\0';
764 te = find_entry_by_name(tree, seg);
765 if (te == NULL) {
766 err = got_error(GOT_ERR_NO_OBJ);
767 goto done;
770 if (len == 0)
771 break;
773 seg = s + 1;
774 s++;
775 len--;
776 if (*s) {
777 err = got_object_open_as_tree(&next_tree, repo,
778 te->id);
779 te = NULL;
780 if (err)
781 goto done;
782 got_object_tree_close(tree);
783 tree = next_tree;
787 if (te)
788 err = got_object_open(obj, repo, te->id);
789 else
790 err = got_error(GOT_ERR_NO_OBJ);
791 done:
792 free(s0);
793 if (commit)
794 got_object_commit_close(commit);
795 if (tree)
796 got_object_tree_close(tree);
797 return err;