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 const struct got_error *
66 got_object_qid_alloc_partial(struct got_object_qid **qid)
67 {
68 const struct got_error *err = NULL;
70 *qid = malloc(sizeof(**qid));
71 if (*qid == NULL)
72 return got_error_from_errno();
74 (*qid)->id = malloc(sizeof(*((*qid)->id)));
75 if ((*qid)->id == NULL) {
76 err = got_error_from_errno();
77 got_object_qid_free(*qid);
78 *qid = NULL;
79 return err;
80 }
82 return NULL;
83 }
85 const struct got_error *
86 got_object_id_str(char **outbuf, struct got_object_id *id)
87 {
88 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
90 *outbuf = malloc(len);
91 if (*outbuf == NULL)
92 return got_error_from_errno();
94 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
95 free(*outbuf);
96 *outbuf = NULL;
97 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
98 }
100 return NULL;
103 void
104 got_object_close(struct got_object *obj)
106 if (obj->refcnt > 0) {
107 obj->refcnt--;
108 if (obj->refcnt > 0)
109 return;
112 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
113 struct got_delta *delta;
114 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
115 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
116 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
117 got_delta_close(delta);
120 if (obj->flags & GOT_OBJ_FLAG_PACKED)
121 free(obj->path_packfile);
122 free(obj);
125 void
126 got_object_qid_free(struct got_object_qid *qid)
128 free(qid->id);
129 free(qid);
132 struct got_commit_object *
133 got_object_commit_alloc_partial(void)
135 struct got_commit_object *commit;
137 commit = calloc(1, sizeof(*commit));
138 if (commit == NULL)
139 return NULL;
140 commit->tree_id = malloc(sizeof(*commit->tree_id));
141 if (commit->tree_id == NULL) {
142 free(commit);
143 return NULL;
146 SIMPLEQ_INIT(&commit->parent_ids);
148 return commit;
151 const struct got_error *
152 got_object_commit_add_parent(struct got_commit_object *commit,
153 const char *id_str)
155 const struct got_error *err = NULL;
156 struct got_object_qid *qid;
158 err = got_object_qid_alloc_partial(&qid);
159 if (err)
160 return err;
162 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
163 err = got_error(GOT_ERR_BAD_OBJ_DATA);
164 free(qid->id);
165 free(qid);
166 return err;
169 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
170 commit->nparents++;
172 return NULL;
175 static const struct got_error *
176 parse_gmtoff(time_t *gmtoff, const char *tzstr)
178 int sign = 1;
179 const char *p = tzstr;
180 time_t h, m;
182 *gmtoff = 0;
184 if (*p == '-')
185 sign = -1;
186 else if (*p != '+')
187 return got_error(GOT_ERR_BAD_OBJ_DATA);
188 p++;
189 if (!isdigit(*p) && !isdigit(*(p + 1)))
190 return got_error(GOT_ERR_BAD_OBJ_DATA);
191 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
193 p += 2;
194 if (!isdigit(*p) && !isdigit(*(p + 1)))
195 return got_error(GOT_ERR_BAD_OBJ_DATA);
196 m = ((*p - '0') * 10) + (*(p + 1) - '0');
198 *gmtoff = (h * 60 * 60 + m * 60) * sign;
199 return NULL;
202 static const struct got_error *
203 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
205 const struct got_error *err = NULL;
206 const char *errstr;
207 char *space, *tzstr;
209 /* Parse and strip off trailing timezone indicator string. */
210 space = strrchr(committer, ' ');
211 if (space == NULL)
212 return got_error(GOT_ERR_BAD_OBJ_DATA);
213 tzstr = strdup(space + 1);
214 if (tzstr == NULL)
215 return got_error_from_errno();
216 err = parse_gmtoff(gmtoff, tzstr);
217 free(tzstr);
218 if (err)
219 return err;
220 *space = '\0';
222 /* Timestamp is separated from committer name + email by space. */
223 space = strrchr(committer, ' ');
224 if (space == NULL)
225 return got_error(GOT_ERR_BAD_OBJ_DATA);
227 /* Timestamp parsed here is expressed in comitter's local time. */
228 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
229 if (errstr)
230 return got_error(GOT_ERR_BAD_OBJ_DATA);
232 /* Express the time stamp in UTC. */
233 *time -= *gmtoff;
235 /* Strip off parsed time information, leaving just author and email. */
236 *space = '\0';
238 return NULL;
241 void
242 got_object_commit_close(struct got_commit_object *commit)
244 struct got_object_qid *qid;
246 if (commit->refcnt > 0) {
247 commit->refcnt--;
248 if (commit->refcnt > 0)
249 return;
252 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
253 qid = SIMPLEQ_FIRST(&commit->parent_ids);
254 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
255 got_object_qid_free(qid);
258 free(commit->tree_id);
259 free(commit->author);
260 free(commit->committer);
261 free(commit->logmsg);
262 free(commit);
265 const struct got_error *
266 got_object_parse_commit(struct got_commit_object **commit, char *buf, size_t len)
268 const struct got_error *err = NULL;
269 char *s = buf;
270 size_t tlen;
271 ssize_t remain = (ssize_t)len;
273 *commit = got_object_commit_alloc_partial();
274 if (*commit == NULL)
275 return got_error_from_errno();
277 tlen = strlen(GOT_COMMIT_TAG_TREE);
278 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
279 remain -= tlen;
280 if (remain < SHA1_DIGEST_STRING_LENGTH) {
281 err = got_error(GOT_ERR_BAD_OBJ_DATA);
282 goto done;
284 s += tlen;
285 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
286 err = got_error(GOT_ERR_BAD_OBJ_DATA);
287 goto done;
289 remain -= SHA1_DIGEST_STRING_LENGTH;
290 s += SHA1_DIGEST_STRING_LENGTH;
291 } else {
292 err = got_error(GOT_ERR_BAD_OBJ_DATA);
293 goto done;
296 tlen = strlen(GOT_COMMIT_TAG_PARENT);
297 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
298 remain -= tlen;
299 if (remain < SHA1_DIGEST_STRING_LENGTH) {
300 err = got_error(GOT_ERR_BAD_OBJ_DATA);
301 goto done;
303 s += tlen;
304 err = got_object_commit_add_parent(*commit, s);
305 if (err)
306 goto done;
308 remain -= SHA1_DIGEST_STRING_LENGTH;
309 s += SHA1_DIGEST_STRING_LENGTH;
312 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
313 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
314 char *p;
315 size_t slen;
317 remain -= tlen;
318 if (remain <= 0) {
319 err = got_error(GOT_ERR_BAD_OBJ_DATA);
320 goto done;
322 s += tlen;
323 p = strchr(s, '\n');
324 if (p == NULL) {
325 err = got_error(GOT_ERR_BAD_OBJ_DATA);
326 goto done;
328 *p = '\0';
329 slen = strlen(s);
330 err = parse_commit_time(&(*commit)->author_time,
331 &(*commit)->author_gmtoff, s);
332 if (err)
333 goto done;
334 (*commit)->author = strdup(s);
335 if ((*commit)->author == NULL) {
336 err = got_error_from_errno();
337 goto done;
339 s += slen + 1;
340 remain -= slen + 1;
343 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
344 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
345 char *p;
346 size_t slen;
348 remain -= tlen;
349 if (remain <= 0) {
350 err = got_error(GOT_ERR_BAD_OBJ_DATA);
351 goto done;
353 s += tlen;
354 p = strchr(s, '\n');
355 if (p == NULL) {
356 err = got_error(GOT_ERR_BAD_OBJ_DATA);
357 goto done;
359 *p = '\0';
360 slen = strlen(s);
361 err = parse_commit_time(&(*commit)->committer_time,
362 &(*commit)->committer_gmtoff, s);
363 if (err)
364 goto done;
365 (*commit)->committer = strdup(s);
366 if ((*commit)->committer == NULL) {
367 err = got_error_from_errno();
368 goto done;
370 s += slen + 1;
371 remain -= slen + 1;
374 (*commit)->logmsg = strndup(s, remain);
375 if ((*commit)->logmsg == NULL) {
376 err = got_error_from_errno();
377 goto done;
379 done:
380 if (err) {
381 got_object_commit_close(*commit);
382 *commit = NULL;
384 return err;
387 void
388 got_object_tree_entry_close(struct got_tree_entry *te)
390 free(te->id);
391 free(te->name);
392 free(te);
395 void
396 got_object_tree_close(struct got_tree_object *tree)
398 struct got_tree_entry *te;
400 if (tree->refcnt > 0) {
401 tree->refcnt--;
402 if (tree->refcnt > 0)
403 return;
406 while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
407 te = SIMPLEQ_FIRST(&tree->entries.head);
408 SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
409 got_object_tree_entry_close(te);
412 free(tree);
415 struct got_tree_entry *
416 got_alloc_tree_entry_partial(void)
418 struct got_tree_entry *te;
420 te = malloc(sizeof(*te));
421 if (te == NULL)
422 return NULL;
424 te->id = malloc(sizeof(*te->id));
425 if (te->id == NULL) {
426 free(te);
427 te = NULL;
429 return te;
432 static const struct got_error *
433 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
434 size_t maxlen)
436 char *p = buf, *space;
437 const struct got_error *err = NULL;
439 *te = got_alloc_tree_entry_partial();
440 if (*te == NULL)
441 return got_error_from_errno();
443 *elen = strlen(buf) + 1;
444 if (*elen > maxlen) {
445 free(*te);
446 *te = NULL;
447 return got_error(GOT_ERR_BAD_OBJ_DATA);
450 space = strchr(buf, ' ');
451 if (space == NULL) {
452 err = got_error(GOT_ERR_BAD_OBJ_DATA);
453 free(*te);
454 *te = NULL;
455 return err;
457 while (*p != ' ') {
458 if (*p < '0' && *p > '7') {
459 err = got_error(GOT_ERR_BAD_OBJ_DATA);
460 goto done;
462 (*te)->mode <<= 3;
463 (*te)->mode |= *p - '0';
464 p++;
467 (*te)->name = strdup(space + 1);
468 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
469 err = got_error(GOT_ERR_BAD_OBJ_DATA);
470 goto done;
472 buf += strlen(buf) + 1;
473 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
474 *elen += SHA1_DIGEST_LENGTH;
475 done:
476 if (err) {
477 got_object_tree_entry_close(*te);
478 *te = NULL;
480 return err;
483 const struct got_error *
484 got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
486 const struct got_error *err;
487 size_t remain = len;
489 *tree = calloc(1, sizeof(**tree));
490 if (*tree == NULL)
491 return got_error_from_errno();
493 SIMPLEQ_INIT(&(*tree)->entries.head);
495 while (remain > 0) {
496 struct got_tree_entry *te;
497 size_t elen;
499 err = parse_tree_entry(&te, &elen, buf, remain);
500 if (err)
501 return err;
502 (*tree)->entries.nentries++;
503 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
504 buf += elen;
505 remain -= elen;
508 if (remain != 0) {
509 got_object_tree_close(*tree);
510 return got_error(GOT_ERR_BAD_OBJ_DATA);
513 return NULL;
516 const struct got_error *
517 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
519 const struct got_error *err = NULL;
520 static const size_t blocksize = 512;
521 size_t n, total, remain;
522 uint8_t *buf;
524 *outbuf = NULL;
525 *outlen = 0;
527 buf = malloc(blocksize);
528 if (buf == NULL)
529 return got_error_from_errno();
531 remain = blocksize;
532 total = 0;
533 while (1) {
534 if (remain == 0) {
535 uint8_t *newbuf;
536 newbuf = reallocarray(buf, 1, total + blocksize);
537 if (newbuf == NULL) {
538 err = got_error_from_errno();
539 goto done;
541 buf = newbuf;
542 remain += blocksize;
544 n = fread(buf + total, 1, remain, f);
545 if (n == 0) {
546 if (ferror(f)) {
547 err = got_ferror(f, GOT_ERR_IO);
548 goto done;
550 break; /* EOF */
552 remain -= n;
553 total += n;
554 };
556 done:
557 if (err == NULL) {
558 *outbuf = buf;
559 *outlen = total;
560 } else
561 free(buf);
562 return err;