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_COMMIT_TAG_TREE "tree "
57 #define GOT_COMMIT_TAG_PARENT "parent "
58 #define GOT_COMMIT_TAG_AUTHOR "author "
59 #define GOT_COMMIT_TAG_COMMITTER "committer "
61 int
62 got_object_id_cmp(const struct got_object_id *id1,
63 const struct got_object_id *id2)
64 {
65 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
66 }
68 const struct got_error *
69 got_object_qid_alloc_partial(struct got_object_qid **qid)
70 {
71 const struct got_error *err = NULL;
73 *qid = malloc(sizeof(**qid));
74 if (*qid == NULL)
75 return got_error_from_errno();
77 (*qid)->id = malloc(sizeof(*((*qid)->id)));
78 if ((*qid)->id == NULL) {
79 err = got_error_from_errno();
80 got_object_qid_free(*qid);
81 *qid = NULL;
82 return err;
83 }
85 return NULL;
86 }
88 const struct got_error *
89 got_object_id_str(char **outbuf, struct got_object_id *id)
90 {
91 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
93 *outbuf = malloc(len);
94 if (*outbuf == NULL)
95 return got_error_from_errno();
97 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
98 free(*outbuf);
99 *outbuf = NULL;
100 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
103 return NULL;
106 void
107 got_object_close(struct got_object *obj)
109 if (obj->refcnt > 0) {
110 obj->refcnt--;
111 if (obj->refcnt > 0)
112 return;
115 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
116 struct got_delta *delta;
117 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
118 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
119 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
120 got_delta_close(delta);
123 if (obj->flags & GOT_OBJ_FLAG_PACKED)
124 free(obj->path_packfile);
125 free(obj);
128 void
129 got_object_qid_free(struct got_object_qid *qid)
131 free(qid->id);
132 free(qid);
135 struct got_commit_object *
136 got_object_commit_alloc_partial(void)
138 struct got_commit_object *commit;
140 commit = calloc(1, sizeof(*commit));
141 if (commit == NULL)
142 return NULL;
143 commit->tree_id = malloc(sizeof(*commit->tree_id));
144 if (commit->tree_id == NULL) {
145 free(commit);
146 return NULL;
149 SIMPLEQ_INIT(&commit->parent_ids);
151 return commit;
154 const struct got_error *
155 got_object_commit_add_parent(struct got_commit_object *commit,
156 const char *id_str)
158 const struct got_error *err = NULL;
159 struct got_object_qid *qid;
161 err = got_object_qid_alloc_partial(&qid);
162 if (err)
163 return err;
165 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
166 err = got_error(GOT_ERR_BAD_OBJ_DATA);
167 free(qid->id);
168 free(qid);
169 return err;
172 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
173 commit->nparents++;
175 return NULL;
178 static const struct got_error *
179 parse_gmtoff(time_t *gmtoff, const char *tzstr)
181 int sign = 1;
182 const char *p = tzstr;
183 time_t h, m;
185 *gmtoff = 0;
187 if (*p == '-')
188 sign = -1;
189 else if (*p != '+')
190 return got_error(GOT_ERR_BAD_OBJ_DATA);
191 p++;
192 if (!isdigit(*p) && !isdigit(*(p + 1)))
193 return got_error(GOT_ERR_BAD_OBJ_DATA);
194 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
196 p += 2;
197 if (!isdigit(*p) && !isdigit(*(p + 1)))
198 return got_error(GOT_ERR_BAD_OBJ_DATA);
199 m = ((*p - '0') * 10) + (*(p + 1) - '0');
201 *gmtoff = (h * 60 * 60 + m * 60) * sign;
202 return NULL;
205 static const struct got_error *
206 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
208 const struct got_error *err = NULL;
209 const char *errstr;
210 char *space, *tzstr;
212 /* Parse and strip off trailing timezone indicator string. */
213 space = strrchr(committer, ' ');
214 if (space == NULL)
215 return got_error(GOT_ERR_BAD_OBJ_DATA);
216 tzstr = strdup(space + 1);
217 if (tzstr == NULL)
218 return got_error_from_errno();
219 err = parse_gmtoff(gmtoff, tzstr);
220 free(tzstr);
221 if (err)
222 return err;
223 *space = '\0';
225 /* Timestamp is separated from committer name + email by space. */
226 space = strrchr(committer, ' ');
227 if (space == NULL)
228 return got_error(GOT_ERR_BAD_OBJ_DATA);
230 /* Timestamp parsed here is expressed in comitter's local time. */
231 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
232 if (errstr)
233 return got_error(GOT_ERR_BAD_OBJ_DATA);
235 /* Express the time stamp in UTC. */
236 *time -= *gmtoff;
238 /* Strip off parsed time information, leaving just author and email. */
239 *space = '\0';
241 return NULL;
244 void
245 got_object_commit_close(struct got_commit_object *commit)
247 struct got_object_qid *qid;
249 if (commit->refcnt > 0) {
250 commit->refcnt--;
251 if (commit->refcnt > 0)
252 return;
255 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
256 qid = SIMPLEQ_FIRST(&commit->parent_ids);
257 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
258 got_object_qid_free(qid);
261 free(commit->tree_id);
262 free(commit->author);
263 free(commit->committer);
264 free(commit->logmsg);
265 free(commit);
268 const struct got_error *
269 got_object_parse_commit(struct got_commit_object **commit, char *buf, size_t len)
271 const struct got_error *err = NULL;
272 char *s = buf;
273 size_t tlen;
274 ssize_t remain = (ssize_t)len;
276 *commit = got_object_commit_alloc_partial();
277 if (*commit == NULL)
278 return got_error_from_errno();
280 tlen = strlen(GOT_COMMIT_TAG_TREE);
281 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
282 remain -= tlen;
283 if (remain < SHA1_DIGEST_STRING_LENGTH) {
284 err = got_error(GOT_ERR_BAD_OBJ_DATA);
285 goto done;
287 s += tlen;
288 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
289 err = got_error(GOT_ERR_BAD_OBJ_DATA);
290 goto done;
292 remain -= SHA1_DIGEST_STRING_LENGTH;
293 s += SHA1_DIGEST_STRING_LENGTH;
294 } else {
295 err = got_error(GOT_ERR_BAD_OBJ_DATA);
296 goto done;
299 tlen = strlen(GOT_COMMIT_TAG_PARENT);
300 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
301 remain -= tlen;
302 if (remain < SHA1_DIGEST_STRING_LENGTH) {
303 err = got_error(GOT_ERR_BAD_OBJ_DATA);
304 goto done;
306 s += tlen;
307 err = got_object_commit_add_parent(*commit, s);
308 if (err)
309 goto done;
311 remain -= SHA1_DIGEST_STRING_LENGTH;
312 s += SHA1_DIGEST_STRING_LENGTH;
315 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
316 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
317 char *p;
318 size_t slen;
320 remain -= tlen;
321 if (remain <= 0) {
322 err = got_error(GOT_ERR_BAD_OBJ_DATA);
323 goto done;
325 s += tlen;
326 p = strchr(s, '\n');
327 if (p == NULL) {
328 err = got_error(GOT_ERR_BAD_OBJ_DATA);
329 goto done;
331 *p = '\0';
332 slen = strlen(s);
333 err = parse_commit_time(&(*commit)->author_time,
334 &(*commit)->author_gmtoff, s);
335 if (err)
336 goto done;
337 (*commit)->author = strdup(s);
338 if ((*commit)->author == NULL) {
339 err = got_error_from_errno();
340 goto done;
342 s += slen + 1;
343 remain -= slen + 1;
346 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
347 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
348 char *p;
349 size_t slen;
351 remain -= tlen;
352 if (remain <= 0) {
353 err = got_error(GOT_ERR_BAD_OBJ_DATA);
354 goto done;
356 s += tlen;
357 p = strchr(s, '\n');
358 if (p == NULL) {
359 err = got_error(GOT_ERR_BAD_OBJ_DATA);
360 goto done;
362 *p = '\0';
363 slen = strlen(s);
364 err = parse_commit_time(&(*commit)->committer_time,
365 &(*commit)->committer_gmtoff, s);
366 if (err)
367 goto done;
368 (*commit)->committer = strdup(s);
369 if ((*commit)->committer == NULL) {
370 err = got_error_from_errno();
371 goto done;
373 s += slen + 1;
374 remain -= slen + 1;
377 (*commit)->logmsg = strndup(s, remain);
378 if ((*commit)->logmsg == NULL) {
379 err = got_error_from_errno();
380 goto done;
382 done:
383 if (err) {
384 got_object_commit_close(*commit);
385 *commit = NULL;
387 return err;
390 void
391 got_object_tree_entry_close(struct got_tree_entry *te)
393 free(te->id);
394 free(te->name);
395 free(te);
398 void
399 got_object_tree_close(struct got_tree_object *tree)
401 struct got_tree_entry *te;
403 if (tree->refcnt > 0) {
404 tree->refcnt--;
405 if (tree->refcnt > 0)
406 return;
409 while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
410 te = SIMPLEQ_FIRST(&tree->entries.head);
411 SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
412 got_object_tree_entry_close(te);
415 free(tree);
418 struct got_tree_entry *
419 got_alloc_tree_entry_partial(void)
421 struct got_tree_entry *te;
423 te = malloc(sizeof(*te));
424 if (te == NULL)
425 return NULL;
427 te->id = malloc(sizeof(*te->id));
428 if (te->id == NULL) {
429 free(te);
430 te = NULL;
432 return te;
435 static const struct got_error *
436 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
437 size_t maxlen)
439 char *p = buf, *space;
440 const struct got_error *err = NULL;
442 *te = got_alloc_tree_entry_partial();
443 if (*te == NULL)
444 return got_error_from_errno();
446 *elen = strlen(buf) + 1;
447 if (*elen > maxlen) {
448 free(*te);
449 *te = NULL;
450 return got_error(GOT_ERR_BAD_OBJ_DATA);
453 space = strchr(buf, ' ');
454 if (space == NULL) {
455 err = got_error(GOT_ERR_BAD_OBJ_DATA);
456 free(*te);
457 *te = NULL;
458 return err;
460 (*te)->mode = 0;
461 while (*p != ' ') {
462 if (*p < '0' && *p > '7') {
463 err = got_error(GOT_ERR_BAD_OBJ_DATA);
464 goto done;
466 (*te)->mode <<= 3;
467 (*te)->mode |= *p - '0';
468 p++;
471 (*te)->name = strdup(space + 1);
472 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
473 err = got_error(GOT_ERR_BAD_OBJ_DATA);
474 goto done;
476 buf += *elen;
477 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
478 *elen += SHA1_DIGEST_LENGTH;
479 done:
480 if (err) {
481 got_object_tree_entry_close(*te);
482 *te = NULL;
484 return err;
487 const struct got_error *
488 got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
490 const struct got_error *err;
491 size_t remain = len;
493 *tree = calloc(1, sizeof(**tree));
494 if (*tree == NULL)
495 return got_error_from_errno();
497 SIMPLEQ_INIT(&(*tree)->entries.head);
499 while (remain > 0) {
500 struct got_tree_entry *te;
501 size_t elen;
503 err = parse_tree_entry(&te, &elen, buf, remain);
504 if (err)
505 return err;
506 (*tree)->entries.nentries++;
507 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
508 buf += elen;
509 remain -= elen;
512 if (remain != 0) {
513 got_object_tree_close(*tree);
514 return got_error(GOT_ERR_BAD_OBJ_DATA);
517 return NULL;
520 const struct got_error *
521 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
523 const struct got_error *err = NULL;
524 static const size_t blocksize = 512;
525 size_t n, total, remain;
526 uint8_t *buf;
528 *outbuf = NULL;
529 *outlen = 0;
531 buf = malloc(blocksize);
532 if (buf == NULL)
533 return got_error_from_errno();
535 remain = blocksize;
536 total = 0;
537 while (1) {
538 if (remain == 0) {
539 uint8_t *newbuf;
540 newbuf = reallocarray(buf, 1, total + blocksize);
541 if (newbuf == NULL) {
542 err = got_error_from_errno();
543 goto done;
545 buf = newbuf;
546 remain += blocksize;
548 n = fread(buf + total, 1, remain, f);
549 if (n == 0) {
550 if (ferror(f)) {
551 err = got_ferror(f, GOT_ERR_IO);
552 goto done;
554 break; /* EOF */
556 remain -= n;
557 total += n;
558 };
560 done:
561 if (err == NULL) {
562 *outbuf = buf;
563 *outlen = total;
564 } else
565 free(buf);
566 return err;