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/syslimits.h>
23 #include <sys/wait.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdint.h>
30 #include <sha1.h>
31 #include <zlib.h>
32 #include <ctype.h>
33 #include <limits.h>
34 #include <imsg.h>
35 #include <time.h>
36 #include <unistd.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_inflate.h"
46 #include "got_lib_object.h"
47 #include "got_lib_privsep.h"
48 #include "got_lib_pack.h"
49 #include "got_lib_object_cache.h"
50 #include "got_lib_repository.h"
52 #ifndef nitems
53 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
54 #endif
56 #define GOT_OBJ_TAG_COMMIT "commit"
57 #define GOT_OBJ_TAG_TREE "tree"
58 #define GOT_OBJ_TAG_BLOB "blob"
60 #define GOT_COMMIT_TAG_TREE "tree "
61 #define GOT_COMMIT_TAG_PARENT "parent "
62 #define GOT_COMMIT_TAG_AUTHOR "author "
63 #define GOT_COMMIT_TAG_COMMITTER "committer "
65 const struct got_error *
66 got_object_id_str(char **outbuf, struct got_object_id *id)
67 {
68 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
70 *outbuf = malloc(len);
71 if (*outbuf == NULL)
72 return got_error_from_errno();
74 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
75 free(*outbuf);
76 *outbuf = NULL;
77 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
78 }
80 return NULL;
81 }
83 void
84 got_object_close(struct got_object *obj)
85 {
86 if (obj->refcnt > 0) {
87 obj->refcnt--;
88 if (obj->refcnt > 0)
89 return;
90 }
92 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
93 struct got_delta *delta;
94 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
95 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
96 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
97 got_delta_close(delta);
98 }
99 }
100 if (obj->flags & GOT_OBJ_FLAG_PACKED)
101 free(obj->path_packfile);
102 free(obj);
105 const struct got_error *
106 got_object_qid_alloc_partial(struct got_object_qid **qid)
108 const struct got_error *err = NULL;
110 *qid = malloc(sizeof(**qid));
111 if (*qid == NULL)
112 return got_error_from_errno();
114 (*qid)->id = malloc(sizeof(*((*qid)->id)));
115 if ((*qid)->id == NULL) {
116 err = got_error_from_errno();
117 got_object_qid_free(*qid);
118 *qid = NULL;
120 return err;
123 void
124 got_object_qid_free(struct got_object_qid *qid)
126 free(qid->id);
127 free(qid);
130 struct got_commit_object *
131 got_object_commit_alloc_partial(void)
133 struct got_commit_object *commit;
135 commit = calloc(1, sizeof(*commit));
136 if (commit == NULL)
137 return NULL;
138 commit->tree_id = calloc(1, sizeof(*commit->tree_id));
139 if (commit->tree_id == NULL) {
140 free(commit);
141 return NULL;
144 SIMPLEQ_INIT(&commit->parent_ids);
146 return commit;
149 struct got_mini_commit_object *
150 got_object_mini_commit_alloc_partial(void)
152 struct got_mini_commit_object *commit;
154 commit = calloc(1, sizeof(*commit));
155 if (commit == NULL)
156 return NULL;
157 commit->tree_id = calloc(1, sizeof(*commit->tree_id));
158 if (commit->tree_id == NULL) {
159 free(commit);
160 return NULL;
163 SIMPLEQ_INIT(&commit->parent_ids);
165 return commit;
168 const struct got_error *
169 got_object_commit_add_parent(struct got_commit_object *commit,
170 const char *id_str)
172 const struct got_error *err = NULL;
173 struct got_object_qid *qid;
175 err = got_object_qid_alloc_partial(&qid);
176 if (err)
177 return err;
179 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
180 err = got_error(GOT_ERR_BAD_OBJ_DATA);
181 free(qid->id);
182 free(qid);
183 return err;
186 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
187 commit->nparents++;
189 return NULL;
192 const struct got_error *
193 got_object_mini_commit_add_parent(struct got_mini_commit_object *commit,
194 const char *id_str)
196 const struct got_error *err = NULL;
197 struct got_object_qid *qid;
199 err = got_object_qid_alloc_partial(&qid);
200 if (err)
201 return err;
203 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
204 err = got_error(GOT_ERR_BAD_OBJ_DATA);
205 free(qid->id);
206 free(qid);
207 return err;
210 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
211 commit->nparents++;
213 return NULL;
216 static const struct got_error *
217 parse_gmtoff(time_t *gmtoff, const char *tzstr)
219 int sign = 1;
220 const char *p = tzstr;
221 time_t h, m;
223 *gmtoff = 0;
225 if (*p == '-')
226 sign = -1;
227 else if (*p != '+')
228 return got_error(GOT_ERR_BAD_OBJ_DATA);
229 p++;
230 if (!isdigit(*p) && !isdigit(*(p + 1)))
231 return got_error(GOT_ERR_BAD_OBJ_DATA);
232 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
234 p += 2;
235 if (!isdigit(*p) && !isdigit(*(p + 1)))
236 return got_error(GOT_ERR_BAD_OBJ_DATA);
237 m = ((*p - '0') * 10) + (*(p + 1) - '0');
239 *gmtoff = (h * 60 * 60 + m * 60) * sign;
240 return NULL;
243 static const struct got_error *
244 parse_commit_time(struct tm *tm, char *committer)
246 const struct got_error *err = NULL;
247 const char *errstr;
248 char *space, *tzstr;
249 time_t gmtoff;
250 time_t time;
252 /* Parse and strip off trailing timezone indicator string. */
253 space = strrchr(committer, ' ');
254 if (space == NULL)
255 return got_error(GOT_ERR_BAD_OBJ_DATA);
256 tzstr = strdup(space + 1);
257 if (tzstr == NULL)
258 return got_error_from_errno();
259 err = parse_gmtoff(&gmtoff, tzstr);
260 free(tzstr);
261 if (err)
262 return err;
263 *space = '\0';
265 /* Timestamp is separated from committer name + email by space. */
266 space = strrchr(committer, ' ');
267 if (space == NULL)
268 return got_error(GOT_ERR_BAD_OBJ_DATA);
270 /* Timestamp parsed here is expressed in comitter's local time. */
271 time = strtonum(space + 1, 0, INT64_MAX, &errstr);
272 if (errstr)
273 return got_error(GOT_ERR_BAD_OBJ_DATA);
275 /* Express the time stamp in UTC. */
276 memset(tm, 0, sizeof(*tm));
277 time -= gmtoff;
278 if (localtime_r(&time, tm) == NULL)
279 return got_error_from_errno();
280 tm->tm_gmtoff = gmtoff;
282 /* Strip off parsed time information, leaving just author and email. */
283 *space = '\0';
285 return NULL;
288 void
289 got_object_commit_close(struct got_commit_object *commit)
291 struct got_object_qid *qid;
293 if (commit->refcnt > 0) {
294 commit->refcnt--;
295 if (commit->refcnt > 0)
296 return;
299 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
300 qid = SIMPLEQ_FIRST(&commit->parent_ids);
301 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
302 got_object_qid_free(qid);
305 free(commit->tree_id);
306 free(commit->author);
307 free(commit->committer);
308 free(commit->logmsg);
309 free(commit);
312 void
313 got_object_mini_commit_close(struct got_mini_commit_object *commit)
315 struct got_object_qid *qid;
317 if (commit->refcnt > 0) {
318 commit->refcnt--;
319 if (commit->refcnt > 0)
320 return;
323 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
324 qid = SIMPLEQ_FIRST(&commit->parent_ids);
325 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
326 got_object_qid_free(qid);
329 free(commit->tree_id);
330 free(commit);
333 const struct got_error *
334 got_object_parse_commit(struct got_commit_object **commit, char *buf,
335 size_t len)
337 const struct got_error *err = NULL;
338 char *s = buf;
339 size_t tlen;
340 ssize_t remain = (ssize_t)len;
342 *commit = got_object_commit_alloc_partial();
343 if (*commit == NULL)
344 return got_error_from_errno();
346 tlen = strlen(GOT_COMMIT_TAG_TREE);
347 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
348 remain -= tlen;
349 if (remain < SHA1_DIGEST_STRING_LENGTH) {
350 err = got_error(GOT_ERR_BAD_OBJ_DATA);
351 goto done;
353 s += tlen;
354 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
355 err = got_error(GOT_ERR_BAD_OBJ_DATA);
356 goto done;
358 remain -= SHA1_DIGEST_STRING_LENGTH;
359 s += SHA1_DIGEST_STRING_LENGTH;
360 } else {
361 err = got_error(GOT_ERR_BAD_OBJ_DATA);
362 goto done;
365 tlen = strlen(GOT_COMMIT_TAG_PARENT);
366 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
367 remain -= tlen;
368 if (remain < SHA1_DIGEST_STRING_LENGTH) {
369 err = got_error(GOT_ERR_BAD_OBJ_DATA);
370 goto done;
372 s += tlen;
373 err = got_object_commit_add_parent(*commit, s);
374 if (err)
375 goto done;
377 remain -= SHA1_DIGEST_STRING_LENGTH;
378 s += SHA1_DIGEST_STRING_LENGTH;
381 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
382 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
383 char *p;
384 size_t slen;
386 remain -= tlen;
387 if (remain <= 0) {
388 err = got_error(GOT_ERR_BAD_OBJ_DATA);
389 goto done;
391 s += tlen;
392 p = strchr(s, '\n');
393 if (p == NULL) {
394 err = got_error(GOT_ERR_BAD_OBJ_DATA);
395 goto done;
397 *p = '\0';
398 slen = strlen(s);
399 err = parse_commit_time(&(*commit)->tm_author, s);
400 if (err)
401 goto done;
402 (*commit)->author = strdup(s);
403 if ((*commit)->author == NULL) {
404 err = got_error_from_errno();
405 goto done;
407 s += slen + 1;
408 remain -= slen + 1;
411 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
412 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
413 char *p;
414 size_t slen;
416 remain -= tlen;
417 if (remain <= 0) {
418 err = got_error(GOT_ERR_BAD_OBJ_DATA);
419 goto done;
421 s += tlen;
422 p = strchr(s, '\n');
423 if (p == NULL) {
424 err = got_error(GOT_ERR_BAD_OBJ_DATA);
425 goto done;
427 *p = '\0';
428 slen = strlen(s);
429 err = parse_commit_time(&(*commit)->tm_committer, s);
430 if (err)
431 goto done;
432 (*commit)->committer = strdup(s);
433 if ((*commit)->committer == NULL) {
434 err = got_error_from_errno();
435 goto done;
437 s += slen + 1;
438 remain -= slen + 1;
441 (*commit)->logmsg = strndup(s, remain);
442 if ((*commit)->logmsg == NULL) {
443 err = got_error_from_errno();
444 goto done;
446 done:
447 if (err) {
448 got_object_commit_close(*commit);
449 *commit = NULL;
451 return err;
454 const struct got_error *
455 got_object_parse_mini_commit(struct got_mini_commit_object **commit, char *buf,
456 size_t len)
458 const struct got_error *err = NULL;
459 char *s = buf;
460 size_t tlen;
461 ssize_t remain = (ssize_t)len;
463 *commit = got_object_mini_commit_alloc_partial();
464 if (*commit == NULL)
465 return got_error_from_errno();
467 tlen = strlen(GOT_COMMIT_TAG_TREE);
468 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
469 remain -= tlen;
470 if (remain < SHA1_DIGEST_STRING_LENGTH) {
471 err = got_error(GOT_ERR_BAD_OBJ_DATA);
472 goto done;
474 s += tlen;
475 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
476 err = got_error(GOT_ERR_BAD_OBJ_DATA);
477 goto done;
479 remain -= SHA1_DIGEST_STRING_LENGTH;
480 s += SHA1_DIGEST_STRING_LENGTH;
481 } else {
482 err = got_error(GOT_ERR_BAD_OBJ_DATA);
483 goto done;
486 tlen = strlen(GOT_COMMIT_TAG_PARENT);
487 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
488 remain -= tlen;
489 if (remain < SHA1_DIGEST_STRING_LENGTH) {
490 err = got_error(GOT_ERR_BAD_OBJ_DATA);
491 goto done;
493 s += tlen;
494 err = got_object_mini_commit_add_parent(*commit, s);
495 if (err)
496 goto done;
498 remain -= SHA1_DIGEST_STRING_LENGTH;
499 s += SHA1_DIGEST_STRING_LENGTH;
502 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
503 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
504 char *p;
505 size_t slen;
507 remain -= tlen;
508 if (remain <= 0) {
509 err = got_error(GOT_ERR_BAD_OBJ_DATA);
510 goto done;
512 s += tlen;
513 p = strchr(s, '\n');
514 if (p == NULL) {
515 err = got_error(GOT_ERR_BAD_OBJ_DATA);
516 goto done;
518 *p = '\0';
519 slen = strlen(s);
520 s += slen + 1;
521 remain -= slen + 1;
524 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
525 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
526 char *p;
527 size_t slen;
529 remain -= tlen;
530 if (remain <= 0) {
531 err = got_error(GOT_ERR_BAD_OBJ_DATA);
532 goto done;
534 s += tlen;
535 p = strchr(s, '\n');
536 if (p == NULL) {
537 err = got_error(GOT_ERR_BAD_OBJ_DATA);
538 goto done;
540 *p = '\0';
541 slen = strlen(s);
542 err = parse_commit_time(&(*commit)->tm_committer, s);
543 if (err)
544 goto done;
545 s += slen + 1;
546 remain -= slen + 1;
549 done:
550 if (err) {
551 got_object_mini_commit_close(*commit);
552 *commit = NULL;
554 return err;
557 void
558 got_object_tree_entry_close(struct got_tree_entry *te)
560 free(te->id);
561 free(te->name);
562 free(te);
565 void
566 got_object_tree_close(struct got_tree_object *tree)
568 struct got_tree_entry *te;
570 if (tree->refcnt > 0) {
571 tree->refcnt--;
572 if (tree->refcnt > 0)
573 return;
576 while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
577 te = SIMPLEQ_FIRST(&tree->entries.head);
578 SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
579 got_object_tree_entry_close(te);
582 free(tree);
585 struct got_tree_entry *
586 got_alloc_tree_entry_partial(void)
588 struct got_tree_entry *te;
590 te = calloc(1, sizeof(*te));
591 if (te == NULL)
592 return NULL;
594 te->id = calloc(1, sizeof(*te->id));
595 if (te->id == NULL) {
596 free(te);
597 te = NULL;
599 return te;
602 static const struct got_error *
603 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
604 size_t maxlen)
606 char *p = buf, *space;
607 const struct got_error *err = NULL;
609 *te = got_alloc_tree_entry_partial();
610 if (*te == NULL)
611 return got_error_from_errno();
613 *elen = strlen(buf) + 1;
614 if (*elen > maxlen) {
615 free(*te);
616 *te = NULL;
617 return got_error(GOT_ERR_BAD_OBJ_DATA);
620 space = strchr(buf, ' ');
621 if (space == NULL) {
622 err = got_error(GOT_ERR_BAD_OBJ_DATA);
623 free(*te);
624 *te = NULL;
625 return err;
627 while (*p != ' ') {
628 if (*p < '0' && *p > '7') {
629 err = got_error(GOT_ERR_BAD_OBJ_DATA);
630 goto done;
632 (*te)->mode <<= 3;
633 (*te)->mode |= *p - '0';
634 p++;
637 (*te)->name = strdup(space + 1);
638 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
639 err = got_error(GOT_ERR_BAD_OBJ_DATA);
640 goto done;
642 buf += strlen(buf) + 1;
643 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
644 *elen += SHA1_DIGEST_LENGTH;
645 done:
646 if (err) {
647 got_object_tree_entry_close(*te);
648 *te = NULL;
650 return err;
653 const struct got_error *
654 got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
656 const struct got_error *err;
657 size_t remain = len;
659 *tree = calloc(1, sizeof(**tree));
660 if (*tree == NULL)
661 return got_error_from_errno();
663 SIMPLEQ_INIT(&(*tree)->entries.head);
665 while (remain > 0) {
666 struct got_tree_entry *te;
667 size_t elen;
669 err = parse_tree_entry(&te, &elen, buf, remain);
670 if (err)
671 return err;
672 (*tree)->entries.nentries++;
673 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
674 buf += elen;
675 remain -= elen;
678 if (remain != 0) {
679 got_object_tree_close(*tree);
680 return got_error(GOT_ERR_BAD_OBJ_DATA);
683 return NULL;
686 const struct got_error *
687 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
689 const struct got_error *err = NULL;
690 static const size_t blocksize = 512;
691 size_t n, total, remain;
692 uint8_t *buf;
694 *outbuf = NULL;
695 *outlen = 0;
697 buf = malloc(blocksize);
698 if (buf == NULL)
699 return got_error_from_errno();
701 remain = blocksize;
702 total = 0;
703 while (1) {
704 if (remain == 0) {
705 uint8_t *newbuf;
706 newbuf = reallocarray(buf, 1, total + blocksize);
707 if (newbuf == NULL) {
708 err = got_error_from_errno();
709 goto done;
711 buf = newbuf;
712 remain += blocksize;
714 n = fread(buf + total, 1, remain, f);
715 if (n == 0) {
716 if (ferror(f)) {
717 err = got_ferror(f, GOT_ERR_IO);
718 goto done;
720 break; /* EOF */
722 remain -= n;
723 total += n;
724 };
726 done:
727 if (err == NULL) {
728 *outbuf = buf;
729 *outlen = total;
730 } else
731 free(buf);
732 return err;