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_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 void
106 got_object_qid_free(struct got_object_qid *qid)
108 free(qid->id);
109 free(qid);
112 struct got_commit_object *
113 got_object_commit_alloc_partial(void)
115 struct got_commit_object *commit;
117 commit = calloc(1, sizeof(*commit));
118 if (commit == NULL)
119 return NULL;
120 commit->tree_id = calloc(1, sizeof(*commit->tree_id));
121 if (commit->tree_id == NULL) {
122 free(commit);
123 return NULL;
126 SIMPLEQ_INIT(&commit->parent_ids);
128 return commit;
131 const struct got_error *
132 got_object_commit_add_parent(struct got_commit_object *commit,
133 const char *id_str)
135 const struct got_error *err = NULL;
136 struct got_object_qid *qid;
138 qid = malloc(sizeof(*qid));
139 if (qid == NULL)
140 return got_error_from_errno();
142 qid->id = malloc(sizeof(*qid->id));
143 if (qid->id == NULL) {
144 err = got_error_from_errno();
145 got_object_qid_free(qid);
146 return err;
149 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
150 err = got_error(GOT_ERR_BAD_OBJ_DATA);
151 free(qid->id);
152 free(qid);
153 return err;
156 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
157 commit->nparents++;
159 return NULL;
162 static const struct got_error *
163 parse_gmtoff(time_t *gmtoff, const char *tzstr)
165 int sign = 1;
166 const char *p = tzstr;
167 time_t h, m;
169 *gmtoff = 0;
171 if (*p == '-')
172 sign = -1;
173 else if (*p != '+')
174 return got_error(GOT_ERR_BAD_OBJ_DATA);
175 p++;
176 if (!isdigit(*p) && !isdigit(*(p + 1)))
177 return got_error(GOT_ERR_BAD_OBJ_DATA);
178 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
180 p += 2;
181 if (!isdigit(*p) && !isdigit(*(p + 1)))
182 return got_error(GOT_ERR_BAD_OBJ_DATA);
183 m = ((*p - '0') * 10) + (*(p + 1) - '0');
185 *gmtoff = (h * 60 * 60 + m * 60) * sign;
186 return NULL;
189 static const struct got_error *
190 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
192 const struct got_error *err = NULL;
193 const char *errstr;
194 char *space, *tzstr;
196 /* Parse and strip off trailing timezone indicator string. */
197 space = strrchr(committer, ' ');
198 if (space == NULL)
199 return got_error(GOT_ERR_BAD_OBJ_DATA);
200 tzstr = strdup(space + 1);
201 if (tzstr == NULL)
202 return got_error_from_errno();
203 err = parse_gmtoff(gmtoff, tzstr);
204 free(tzstr);
205 if (err)
206 return err;
207 *space = '\0';
209 /* Timestamp is separated from committer name + email by space. */
210 space = strrchr(committer, ' ');
211 if (space == NULL)
212 return got_error(GOT_ERR_BAD_OBJ_DATA);
214 /* Timestamp parsed here is expressed in comitter's local time. */
215 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
216 if (errstr)
217 return got_error(GOT_ERR_BAD_OBJ_DATA);
219 /* Express the time stamp in UTC. */
220 *time -= *gmtoff;
222 /* Strip off parsed time information, leaving just author and email. */
223 *space = '\0';
225 return NULL;
228 void
229 got_object_commit_close(struct got_commit_object *commit)
231 struct got_object_qid *qid;
233 if (commit->refcnt > 0) {
234 commit->refcnt--;
235 if (commit->refcnt > 0)
236 return;
239 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
240 qid = SIMPLEQ_FIRST(&commit->parent_ids);
241 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
242 got_object_qid_free(qid);
245 free(commit->tree_id);
246 free(commit->author);
247 free(commit->committer);
248 free(commit->logmsg);
249 free(commit);
252 const struct got_error *
253 got_object_parse_commit(struct got_commit_object **commit, char *buf, size_t len)
255 const struct got_error *err = NULL;
256 char *s = buf;
257 size_t tlen;
258 ssize_t remain = (ssize_t)len;
260 *commit = got_object_commit_alloc_partial();
261 if (*commit == NULL)
262 return got_error_from_errno();
264 tlen = strlen(GOT_COMMIT_TAG_TREE);
265 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
266 remain -= tlen;
267 if (remain < SHA1_DIGEST_STRING_LENGTH) {
268 err = got_error(GOT_ERR_BAD_OBJ_DATA);
269 goto done;
271 s += tlen;
272 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
273 err = got_error(GOT_ERR_BAD_OBJ_DATA);
274 goto done;
276 remain -= SHA1_DIGEST_STRING_LENGTH;
277 s += SHA1_DIGEST_STRING_LENGTH;
278 } else {
279 err = got_error(GOT_ERR_BAD_OBJ_DATA);
280 goto done;
283 tlen = strlen(GOT_COMMIT_TAG_PARENT);
284 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
285 remain -= tlen;
286 if (remain < SHA1_DIGEST_STRING_LENGTH) {
287 err = got_error(GOT_ERR_BAD_OBJ_DATA);
288 goto done;
290 s += tlen;
291 err = got_object_commit_add_parent(*commit, s);
292 if (err)
293 goto done;
295 remain -= SHA1_DIGEST_STRING_LENGTH;
296 s += SHA1_DIGEST_STRING_LENGTH;
299 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
300 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
301 char *p;
302 size_t slen;
304 remain -= tlen;
305 if (remain <= 0) {
306 err = got_error(GOT_ERR_BAD_OBJ_DATA);
307 goto done;
309 s += tlen;
310 p = strchr(s, '\n');
311 if (p == NULL) {
312 err = got_error(GOT_ERR_BAD_OBJ_DATA);
313 goto done;
315 *p = '\0';
316 slen = strlen(s);
317 err = parse_commit_time(&(*commit)->author_time,
318 &(*commit)->author_gmtoff, s);
319 if (err)
320 goto done;
321 (*commit)->author = strdup(s);
322 if ((*commit)->author == NULL) {
323 err = got_error_from_errno();
324 goto done;
326 s += slen + 1;
327 remain -= slen + 1;
330 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
331 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
332 char *p;
333 size_t slen;
335 remain -= tlen;
336 if (remain <= 0) {
337 err = got_error(GOT_ERR_BAD_OBJ_DATA);
338 goto done;
340 s += tlen;
341 p = strchr(s, '\n');
342 if (p == NULL) {
343 err = got_error(GOT_ERR_BAD_OBJ_DATA);
344 goto done;
346 *p = '\0';
347 slen = strlen(s);
348 err = parse_commit_time(&(*commit)->committer_time,
349 &(*commit)->committer_gmtoff, s);
350 if (err)
351 goto done;
352 (*commit)->committer = strdup(s);
353 if ((*commit)->committer == NULL) {
354 err = got_error_from_errno();
355 goto done;
357 s += slen + 1;
358 remain -= slen + 1;
361 (*commit)->logmsg = strndup(s, remain);
362 if ((*commit)->logmsg == NULL) {
363 err = got_error_from_errno();
364 goto done;
366 done:
367 if (err) {
368 got_object_commit_close(*commit);
369 *commit = NULL;
371 return err;
374 void
375 got_object_tree_entry_close(struct got_tree_entry *te)
377 free(te->id);
378 free(te->name);
379 free(te);
382 void
383 got_object_tree_close(struct got_tree_object *tree)
385 struct got_tree_entry *te;
387 if (tree->refcnt > 0) {
388 tree->refcnt--;
389 if (tree->refcnt > 0)
390 return;
393 while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
394 te = SIMPLEQ_FIRST(&tree->entries.head);
395 SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
396 got_object_tree_entry_close(te);
399 free(tree);
402 struct got_tree_entry *
403 got_alloc_tree_entry_partial(void)
405 struct got_tree_entry *te;
407 te = calloc(1, sizeof(*te));
408 if (te == NULL)
409 return NULL;
411 te->id = calloc(1, sizeof(*te->id));
412 if (te->id == NULL) {
413 free(te);
414 te = NULL;
416 return te;
419 static const struct got_error *
420 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
421 size_t maxlen)
423 char *p = buf, *space;
424 const struct got_error *err = NULL;
426 *te = got_alloc_tree_entry_partial();
427 if (*te == NULL)
428 return got_error_from_errno();
430 *elen = strlen(buf) + 1;
431 if (*elen > maxlen) {
432 free(*te);
433 *te = NULL;
434 return got_error(GOT_ERR_BAD_OBJ_DATA);
437 space = strchr(buf, ' ');
438 if (space == NULL) {
439 err = got_error(GOT_ERR_BAD_OBJ_DATA);
440 free(*te);
441 *te = NULL;
442 return err;
444 while (*p != ' ') {
445 if (*p < '0' && *p > '7') {
446 err = got_error(GOT_ERR_BAD_OBJ_DATA);
447 goto done;
449 (*te)->mode <<= 3;
450 (*te)->mode |= *p - '0';
451 p++;
454 (*te)->name = strdup(space + 1);
455 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
456 err = got_error(GOT_ERR_BAD_OBJ_DATA);
457 goto done;
459 buf += strlen(buf) + 1;
460 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
461 *elen += SHA1_DIGEST_LENGTH;
462 done:
463 if (err) {
464 got_object_tree_entry_close(*te);
465 *te = NULL;
467 return err;
470 const struct got_error *
471 got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
473 const struct got_error *err;
474 size_t remain = len;
476 *tree = calloc(1, sizeof(**tree));
477 if (*tree == NULL)
478 return got_error_from_errno();
480 SIMPLEQ_INIT(&(*tree)->entries.head);
482 while (remain > 0) {
483 struct got_tree_entry *te;
484 size_t elen;
486 err = parse_tree_entry(&te, &elen, buf, remain);
487 if (err)
488 return err;
489 (*tree)->entries.nentries++;
490 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
491 buf += elen;
492 remain -= elen;
495 if (remain != 0) {
496 got_object_tree_close(*tree);
497 return got_error(GOT_ERR_BAD_OBJ_DATA);
500 return NULL;
503 const struct got_error *
504 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
506 const struct got_error *err = NULL;
507 static const size_t blocksize = 512;
508 size_t n, total, remain;
509 uint8_t *buf;
511 *outbuf = NULL;
512 *outlen = 0;
514 buf = malloc(blocksize);
515 if (buf == NULL)
516 return got_error_from_errno();
518 remain = blocksize;
519 total = 0;
520 while (1) {
521 if (remain == 0) {
522 uint8_t *newbuf;
523 newbuf = reallocarray(buf, 1, total + blocksize);
524 if (newbuf == NULL) {
525 err = got_error_from_errno();
526 goto done;
528 buf = newbuf;
529 remain += blocksize;
531 n = fread(buf + total, 1, remain, f);
532 if (n == 0) {
533 if (ferror(f)) {
534 err = got_ferror(f, GOT_ERR_IO);
535 goto done;
537 break; /* EOF */
539 remain -= n;
540 total += n;
541 };
543 done:
544 if (err == NULL) {
545 *outbuf = buf;
546 *outlen = total;
547 } else
548 free(buf);
549 return err;