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_privsep.h"
46 #include "got_lib_pack.h"
47 #include "got_lib_inflate.h"
48 #include "got_lib_object.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 int
66 got_object_id_cmp(const struct got_object_id *id1,
67 const struct got_object_id *id2)
68 {
69 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
70 }
72 const struct got_error *
73 got_object_qid_alloc_partial(struct got_object_qid **qid)
74 {
75 const struct got_error *err = NULL;
77 *qid = malloc(sizeof(**qid));
78 if (*qid == NULL)
79 return got_error_from_errno();
81 (*qid)->id = malloc(sizeof(*((*qid)->id)));
82 if ((*qid)->id == NULL) {
83 err = got_error_from_errno();
84 got_object_qid_free(*qid);
85 *qid = NULL;
86 return err;
87 }
89 return NULL;
90 }
92 const struct got_error *
93 got_object_id_str(char **outbuf, struct got_object_id *id)
94 {
95 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
97 *outbuf = malloc(len);
98 if (*outbuf == NULL)
99 return got_error_from_errno();
101 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
102 free(*outbuf);
103 *outbuf = NULL;
104 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
107 return NULL;
110 void
111 got_object_close(struct got_object *obj)
113 if (obj->refcnt > 0) {
114 obj->refcnt--;
115 if (obj->refcnt > 0)
116 return;
119 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
120 struct got_delta *delta;
121 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
122 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
123 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
124 got_delta_close(delta);
127 if (obj->flags & GOT_OBJ_FLAG_PACKED)
128 free(obj->path_packfile);
129 free(obj);
132 void
133 got_object_qid_free(struct got_object_qid *qid)
135 free(qid->id);
136 free(qid);
139 struct got_commit_object *
140 got_object_commit_alloc_partial(void)
142 struct got_commit_object *commit;
144 commit = calloc(1, sizeof(*commit));
145 if (commit == NULL)
146 return NULL;
147 commit->tree_id = malloc(sizeof(*commit->tree_id));
148 if (commit->tree_id == NULL) {
149 free(commit);
150 return NULL;
153 SIMPLEQ_INIT(&commit->parent_ids);
155 return commit;
158 const struct got_error *
159 got_object_commit_add_parent(struct got_commit_object *commit,
160 const char *id_str)
162 const struct got_error *err = NULL;
163 struct got_object_qid *qid;
165 err = got_object_qid_alloc_partial(&qid);
166 if (err)
167 return err;
169 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
170 err = got_error(GOT_ERR_BAD_OBJ_DATA);
171 free(qid->id);
172 free(qid);
173 return err;
176 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
177 commit->nparents++;
179 return NULL;
182 static const struct got_error *
183 parse_gmtoff(time_t *gmtoff, const char *tzstr)
185 int sign = 1;
186 const char *p = tzstr;
187 time_t h, m;
189 *gmtoff = 0;
191 if (*p == '-')
192 sign = -1;
193 else if (*p != '+')
194 return got_error(GOT_ERR_BAD_OBJ_DATA);
195 p++;
196 if (!isdigit(*p) && !isdigit(*(p + 1)))
197 return got_error(GOT_ERR_BAD_OBJ_DATA);
198 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
200 p += 2;
201 if (!isdigit(*p) && !isdigit(*(p + 1)))
202 return got_error(GOT_ERR_BAD_OBJ_DATA);
203 m = ((*p - '0') * 10) + (*(p + 1) - '0');
205 *gmtoff = (h * 60 * 60 + m * 60) * sign;
206 return NULL;
209 static const struct got_error *
210 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
212 const struct got_error *err = NULL;
213 const char *errstr;
214 char *space, *tzstr;
216 /* Parse and strip off trailing timezone indicator string. */
217 space = strrchr(committer, ' ');
218 if (space == NULL)
219 return got_error(GOT_ERR_BAD_OBJ_DATA);
220 tzstr = strdup(space + 1);
221 if (tzstr == NULL)
222 return got_error_from_errno();
223 err = parse_gmtoff(gmtoff, tzstr);
224 free(tzstr);
225 if (err)
226 return err;
227 *space = '\0';
229 /* Timestamp is separated from committer name + email by space. */
230 space = strrchr(committer, ' ');
231 if (space == NULL)
232 return got_error(GOT_ERR_BAD_OBJ_DATA);
234 /* Timestamp parsed here is expressed in comitter's local time. */
235 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
236 if (errstr)
237 return got_error(GOT_ERR_BAD_OBJ_DATA);
239 /* Express the time stamp in UTC. */
240 *time -= *gmtoff;
242 /* Strip off parsed time information, leaving just author and email. */
243 *space = '\0';
245 return NULL;
248 void
249 got_object_commit_close(struct got_commit_object *commit)
251 struct got_object_qid *qid;
253 if (commit->refcnt > 0) {
254 commit->refcnt--;
255 if (commit->refcnt > 0)
256 return;
259 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
260 qid = SIMPLEQ_FIRST(&commit->parent_ids);
261 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
262 got_object_qid_free(qid);
265 free(commit->tree_id);
266 free(commit->author);
267 free(commit->committer);
268 free(commit->logmsg);
269 free(commit);
272 const struct got_error *
273 got_object_parse_commit(struct got_commit_object **commit, char *buf, size_t len)
275 const struct got_error *err = NULL;
276 char *s = buf;
277 size_t tlen;
278 ssize_t remain = (ssize_t)len;
280 *commit = got_object_commit_alloc_partial();
281 if (*commit == NULL)
282 return got_error_from_errno();
284 tlen = strlen(GOT_COMMIT_TAG_TREE);
285 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
286 remain -= tlen;
287 if (remain < SHA1_DIGEST_STRING_LENGTH) {
288 err = got_error(GOT_ERR_BAD_OBJ_DATA);
289 goto done;
291 s += tlen;
292 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
293 err = got_error(GOT_ERR_BAD_OBJ_DATA);
294 goto done;
296 remain -= SHA1_DIGEST_STRING_LENGTH;
297 s += SHA1_DIGEST_STRING_LENGTH;
298 } else {
299 err = got_error(GOT_ERR_BAD_OBJ_DATA);
300 goto done;
303 tlen = strlen(GOT_COMMIT_TAG_PARENT);
304 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
305 remain -= tlen;
306 if (remain < SHA1_DIGEST_STRING_LENGTH) {
307 err = got_error(GOT_ERR_BAD_OBJ_DATA);
308 goto done;
310 s += tlen;
311 err = got_object_commit_add_parent(*commit, s);
312 if (err)
313 goto done;
315 remain -= SHA1_DIGEST_STRING_LENGTH;
316 s += SHA1_DIGEST_STRING_LENGTH;
319 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
320 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
321 char *p;
322 size_t slen;
324 remain -= tlen;
325 if (remain <= 0) {
326 err = got_error(GOT_ERR_BAD_OBJ_DATA);
327 goto done;
329 s += tlen;
330 p = strchr(s, '\n');
331 if (p == NULL) {
332 err = got_error(GOT_ERR_BAD_OBJ_DATA);
333 goto done;
335 *p = '\0';
336 slen = strlen(s);
337 err = parse_commit_time(&(*commit)->author_time,
338 &(*commit)->author_gmtoff, s);
339 if (err)
340 goto done;
341 (*commit)->author = strdup(s);
342 if ((*commit)->author == NULL) {
343 err = got_error_from_errno();
344 goto done;
346 s += slen + 1;
347 remain -= slen + 1;
350 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
351 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
352 char *p;
353 size_t slen;
355 remain -= tlen;
356 if (remain <= 0) {
357 err = got_error(GOT_ERR_BAD_OBJ_DATA);
358 goto done;
360 s += tlen;
361 p = strchr(s, '\n');
362 if (p == NULL) {
363 err = got_error(GOT_ERR_BAD_OBJ_DATA);
364 goto done;
366 *p = '\0';
367 slen = strlen(s);
368 err = parse_commit_time(&(*commit)->committer_time,
369 &(*commit)->committer_gmtoff, s);
370 if (err)
371 goto done;
372 (*commit)->committer = strdup(s);
373 if ((*commit)->committer == NULL) {
374 err = got_error_from_errno();
375 goto done;
377 s += slen + 1;
378 remain -= slen + 1;
381 (*commit)->logmsg = strndup(s, remain);
382 if ((*commit)->logmsg == NULL) {
383 err = got_error_from_errno();
384 goto done;
386 done:
387 if (err) {
388 got_object_commit_close(*commit);
389 *commit = NULL;
391 return err;
394 void
395 got_object_tree_entry_close(struct got_tree_entry *te)
397 free(te->id);
398 free(te->name);
399 free(te);
402 void
403 got_object_tree_close(struct got_tree_object *tree)
405 struct got_tree_entry *te;
407 if (tree->refcnt > 0) {
408 tree->refcnt--;
409 if (tree->refcnt > 0)
410 return;
413 while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
414 te = SIMPLEQ_FIRST(&tree->entries.head);
415 SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
416 got_object_tree_entry_close(te);
419 free(tree);
422 struct got_tree_entry *
423 got_alloc_tree_entry_partial(void)
425 struct got_tree_entry *te;
427 te = malloc(sizeof(*te));
428 if (te == NULL)
429 return NULL;
431 te->id = malloc(sizeof(*te->id));
432 if (te->id == NULL) {
433 free(te);
434 te = NULL;
436 return te;
439 static const struct got_error *
440 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
441 size_t maxlen)
443 char *p = buf, *space;
444 const struct got_error *err = NULL;
446 *te = got_alloc_tree_entry_partial();
447 if (*te == NULL)
448 return got_error_from_errno();
450 *elen = strlen(buf) + 1;
451 if (*elen > maxlen) {
452 free(*te);
453 *te = NULL;
454 return got_error(GOT_ERR_BAD_OBJ_DATA);
457 space = strchr(buf, ' ');
458 if (space == NULL) {
459 err = got_error(GOT_ERR_BAD_OBJ_DATA);
460 free(*te);
461 *te = NULL;
462 return err;
464 (*te)->mode = 0;
465 while (*p != ' ') {
466 if (*p < '0' && *p > '7') {
467 err = got_error(GOT_ERR_BAD_OBJ_DATA);
468 goto done;
470 (*te)->mode <<= 3;
471 (*te)->mode |= *p - '0';
472 p++;
475 (*te)->name = strdup(space + 1);
476 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
477 err = got_error(GOT_ERR_BAD_OBJ_DATA);
478 goto done;
480 buf += strlen(buf) + 1;
481 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
482 *elen += SHA1_DIGEST_LENGTH;
483 done:
484 if (err) {
485 got_object_tree_entry_close(*te);
486 *te = NULL;
488 return err;
491 const struct got_error *
492 got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
494 const struct got_error *err;
495 size_t remain = len;
497 *tree = calloc(1, sizeof(**tree));
498 if (*tree == NULL)
499 return got_error_from_errno();
501 SIMPLEQ_INIT(&(*tree)->entries.head);
503 while (remain > 0) {
504 struct got_tree_entry *te;
505 size_t elen;
507 err = parse_tree_entry(&te, &elen, buf, remain);
508 if (err)
509 return err;
510 (*tree)->entries.nentries++;
511 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
512 buf += elen;
513 remain -= elen;
516 if (remain != 0) {
517 got_object_tree_close(*tree);
518 return got_error(GOT_ERR_BAD_OBJ_DATA);
521 return NULL;
524 const struct got_error *
525 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
527 const struct got_error *err = NULL;
528 static const size_t blocksize = 512;
529 size_t n, total, remain;
530 uint8_t *buf;
532 *outbuf = NULL;
533 *outlen = 0;
535 buf = malloc(blocksize);
536 if (buf == NULL)
537 return got_error_from_errno();
539 remain = blocksize;
540 total = 0;
541 while (1) {
542 if (remain == 0) {
543 uint8_t *newbuf;
544 newbuf = reallocarray(buf, 1, total + blocksize);
545 if (newbuf == NULL) {
546 err = got_error_from_errno();
547 goto done;
549 buf = newbuf;
550 remain += blocksize;
552 n = fread(buf + total, 1, remain, f);
553 if (n == 0) {
554 if (ferror(f)) {
555 err = got_ferror(f, GOT_ERR_IO);
556 goto done;
558 break; /* EOF */
560 remain -= n;
561 total += n;
562 };
564 done:
565 if (err == NULL) {
566 *outbuf = buf;
567 *outlen = total;
568 } else
569 free(buf);
570 return err;