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_commit_object_mini *
150 got_object_mini_commit_alloc_partial(void)
152 struct got_commit_object_mini *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_commit_object_mini *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_commit_object_mini *commit)
315 struct got_object_qid *qid;
317 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
318 qid = SIMPLEQ_FIRST(&commit->parent_ids);
319 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
320 got_object_qid_free(qid);
323 free(commit->tree_id);
324 free(commit);
327 const struct got_error *
328 got_object_parse_commit(struct got_commit_object **commit, char *buf,
329 size_t len)
331 const struct got_error *err = NULL;
332 char *s = buf;
333 size_t tlen;
334 ssize_t remain = (ssize_t)len;
336 *commit = got_object_commit_alloc_partial();
337 if (*commit == NULL)
338 return got_error_from_errno();
340 tlen = strlen(GOT_COMMIT_TAG_TREE);
341 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
342 remain -= tlen;
343 if (remain < SHA1_DIGEST_STRING_LENGTH) {
344 err = got_error(GOT_ERR_BAD_OBJ_DATA);
345 goto done;
347 s += tlen;
348 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
349 err = got_error(GOT_ERR_BAD_OBJ_DATA);
350 goto done;
352 remain -= SHA1_DIGEST_STRING_LENGTH;
353 s += SHA1_DIGEST_STRING_LENGTH;
354 } else {
355 err = got_error(GOT_ERR_BAD_OBJ_DATA);
356 goto done;
359 tlen = strlen(GOT_COMMIT_TAG_PARENT);
360 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
361 remain -= tlen;
362 if (remain < SHA1_DIGEST_STRING_LENGTH) {
363 err = got_error(GOT_ERR_BAD_OBJ_DATA);
364 goto done;
366 s += tlen;
367 err = got_object_commit_add_parent(*commit, s);
368 if (err)
369 goto done;
371 remain -= SHA1_DIGEST_STRING_LENGTH;
372 s += SHA1_DIGEST_STRING_LENGTH;
375 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
376 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
377 char *p;
378 size_t slen;
380 remain -= tlen;
381 if (remain <= 0) {
382 err = got_error(GOT_ERR_BAD_OBJ_DATA);
383 goto done;
385 s += tlen;
386 p = strchr(s, '\n');
387 if (p == NULL) {
388 err = got_error(GOT_ERR_BAD_OBJ_DATA);
389 goto done;
391 *p = '\0';
392 slen = strlen(s);
393 err = parse_commit_time(&(*commit)->tm_author, s);
394 if (err)
395 goto done;
396 (*commit)->author = strdup(s);
397 if ((*commit)->author == NULL) {
398 err = got_error_from_errno();
399 goto done;
401 s += slen + 1;
402 remain -= slen + 1;
405 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
406 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
407 char *p;
408 size_t slen;
410 remain -= tlen;
411 if (remain <= 0) {
412 err = got_error(GOT_ERR_BAD_OBJ_DATA);
413 goto done;
415 s += tlen;
416 p = strchr(s, '\n');
417 if (p == NULL) {
418 err = got_error(GOT_ERR_BAD_OBJ_DATA);
419 goto done;
421 *p = '\0';
422 slen = strlen(s);
423 err = parse_commit_time(&(*commit)->tm_committer, s);
424 if (err)
425 goto done;
426 (*commit)->committer = strdup(s);
427 if ((*commit)->committer == NULL) {
428 err = got_error_from_errno();
429 goto done;
431 s += slen + 1;
432 remain -= slen + 1;
435 (*commit)->logmsg = strndup(s, remain);
436 if ((*commit)->logmsg == NULL) {
437 err = got_error_from_errno();
438 goto done;
440 done:
441 if (err) {
442 got_object_commit_close(*commit);
443 *commit = NULL;
445 return err;
448 const struct got_error *
449 got_object_parse_mini_commit(struct got_commit_object_mini **commit, char *buf,
450 size_t len)
452 const struct got_error *err = NULL;
453 char *s = buf;
454 size_t tlen;
455 ssize_t remain = (ssize_t)len;
457 *commit = got_object_mini_commit_alloc_partial();
458 if (*commit == NULL)
459 return got_error_from_errno();
461 tlen = strlen(GOT_COMMIT_TAG_TREE);
462 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
463 remain -= tlen;
464 if (remain < SHA1_DIGEST_STRING_LENGTH) {
465 err = got_error(GOT_ERR_BAD_OBJ_DATA);
466 goto done;
468 s += tlen;
469 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
470 err = got_error(GOT_ERR_BAD_OBJ_DATA);
471 goto done;
473 remain -= SHA1_DIGEST_STRING_LENGTH;
474 s += SHA1_DIGEST_STRING_LENGTH;
475 } else {
476 err = got_error(GOT_ERR_BAD_OBJ_DATA);
477 goto done;
480 tlen = strlen(GOT_COMMIT_TAG_PARENT);
481 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
482 remain -= tlen;
483 if (remain < SHA1_DIGEST_STRING_LENGTH) {
484 err = got_error(GOT_ERR_BAD_OBJ_DATA);
485 goto done;
487 s += tlen;
488 err = got_object_mini_commit_add_parent(*commit, s);
489 if (err)
490 goto done;
492 remain -= SHA1_DIGEST_STRING_LENGTH;
493 s += SHA1_DIGEST_STRING_LENGTH;
496 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
497 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
498 char *p;
499 size_t slen;
501 remain -= tlen;
502 if (remain <= 0) {
503 err = got_error(GOT_ERR_BAD_OBJ_DATA);
504 goto done;
506 s += tlen;
507 p = strchr(s, '\n');
508 if (p == NULL) {
509 err = got_error(GOT_ERR_BAD_OBJ_DATA);
510 goto done;
512 *p = '\0';
513 slen = strlen(s);
514 s += slen + 1;
515 remain -= slen + 1;
518 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
519 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
520 char *p;
521 size_t slen;
523 remain -= tlen;
524 if (remain <= 0) {
525 err = got_error(GOT_ERR_BAD_OBJ_DATA);
526 goto done;
528 s += tlen;
529 p = strchr(s, '\n');
530 if (p == NULL) {
531 err = got_error(GOT_ERR_BAD_OBJ_DATA);
532 goto done;
534 *p = '\0';
535 slen = strlen(s);
536 err = parse_commit_time(&(*commit)->tm_committer, s);
537 if (err)
538 goto done;
539 s += slen + 1;
540 remain -= slen + 1;
543 done:
544 if (err) {
545 got_object_mini_commit_close(*commit);
546 *commit = NULL;
548 return err;
551 void
552 got_object_tree_entry_close(struct got_tree_entry *te)
554 free(te->id);
555 free(te->name);
556 free(te);
559 void
560 got_object_tree_close(struct got_tree_object *tree)
562 struct got_tree_entry *te;
564 if (tree->refcnt > 0) {
565 tree->refcnt--;
566 if (tree->refcnt > 0)
567 return;
570 while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
571 te = SIMPLEQ_FIRST(&tree->entries.head);
572 SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
573 got_object_tree_entry_close(te);
576 free(tree);
579 struct got_tree_entry *
580 got_alloc_tree_entry_partial(void)
582 struct got_tree_entry *te;
584 te = calloc(1, sizeof(*te));
585 if (te == NULL)
586 return NULL;
588 te->id = calloc(1, sizeof(*te->id));
589 if (te->id == NULL) {
590 free(te);
591 te = NULL;
593 return te;
596 static const struct got_error *
597 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
598 size_t maxlen)
600 char *p = buf, *space;
601 const struct got_error *err = NULL;
603 *te = got_alloc_tree_entry_partial();
604 if (*te == NULL)
605 return got_error_from_errno();
607 *elen = strlen(buf) + 1;
608 if (*elen > maxlen) {
609 free(*te);
610 *te = NULL;
611 return got_error(GOT_ERR_BAD_OBJ_DATA);
614 space = strchr(buf, ' ');
615 if (space == NULL) {
616 err = got_error(GOT_ERR_BAD_OBJ_DATA);
617 free(*te);
618 *te = NULL;
619 return err;
621 while (*p != ' ') {
622 if (*p < '0' && *p > '7') {
623 err = got_error(GOT_ERR_BAD_OBJ_DATA);
624 goto done;
626 (*te)->mode <<= 3;
627 (*te)->mode |= *p - '0';
628 p++;
631 (*te)->name = strdup(space + 1);
632 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
633 err = got_error(GOT_ERR_BAD_OBJ_DATA);
634 goto done;
636 buf += strlen(buf) + 1;
637 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
638 *elen += SHA1_DIGEST_LENGTH;
639 done:
640 if (err) {
641 got_object_tree_entry_close(*te);
642 *te = NULL;
644 return err;
647 const struct got_error *
648 got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
650 const struct got_error *err;
651 size_t remain = len;
653 *tree = calloc(1, sizeof(**tree));
654 if (*tree == NULL)
655 return got_error_from_errno();
657 SIMPLEQ_INIT(&(*tree)->entries.head);
659 while (remain > 0) {
660 struct got_tree_entry *te;
661 size_t elen;
663 err = parse_tree_entry(&te, &elen, buf, remain);
664 if (err)
665 return err;
666 (*tree)->entries.nentries++;
667 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
668 buf += elen;
669 remain -= elen;
672 if (remain != 0) {
673 got_object_tree_close(*tree);
674 return got_error(GOT_ERR_BAD_OBJ_DATA);
677 return NULL;
680 const struct got_error *
681 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
683 const struct got_error *err = NULL;
684 static const size_t blocksize = 512;
685 size_t n, total, remain;
686 uint8_t *buf;
688 *outbuf = NULL;
689 *outlen = 0;
691 buf = malloc(blocksize);
692 if (buf == NULL)
693 return got_error_from_errno();
695 remain = blocksize;
696 total = 0;
697 while (1) {
698 if (remain == 0) {
699 uint8_t *newbuf;
700 newbuf = reallocarray(buf, 1, total + blocksize);
701 if (newbuf == NULL) {
702 err = got_error_from_errno();
703 goto done;
705 buf = newbuf;
706 remain += blocksize;
708 n = fread(buf + total, 1, remain, f);
709 if (n == 0) {
710 if (ferror(f)) {
711 err = got_ferror(f, GOT_ERR_IO);
712 goto done;
714 break; /* EOF */
716 remain -= n;
717 total += n;
718 };
720 done:
721 if (err == NULL) {
722 *outbuf = buf;
723 *outlen = total;
724 } else
725 free(buf);
726 return err;