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(struct tm *tm, char *committer)
192 const struct got_error *err = NULL;
193 const char *errstr;
194 char *space, *tzstr;
195 time_t gmtoff;
196 time_t time;
198 /* Parse and strip off trailing timezone indicator string. */
199 space = strrchr(committer, ' ');
200 if (space == NULL)
201 return got_error(GOT_ERR_BAD_OBJ_DATA);
202 tzstr = strdup(space + 1);
203 if (tzstr == NULL)
204 return got_error_from_errno();
205 err = parse_gmtoff(&gmtoff, tzstr);
206 free(tzstr);
207 if (err)
208 return err;
209 *space = '\0';
211 /* Timestamp is separated from committer name + email by space. */
212 space = strrchr(committer, ' ');
213 if (space == NULL)
214 return got_error(GOT_ERR_BAD_OBJ_DATA);
216 /* Timestamp parsed here is expressed in comitter's local time. */
217 time = strtonum(space + 1, 0, INT64_MAX, &errstr);
218 if (errstr)
219 return got_error(GOT_ERR_BAD_OBJ_DATA);
221 /* Express the time stamp in UTC. */
222 memset(tm, 0, sizeof(*tm));
223 time -= gmtoff;
224 if (localtime_r(&time, tm) == NULL)
225 return got_error_from_errno();
226 tm->tm_gmtoff = gmtoff;
228 /* Strip off parsed time information, leaving just author and email. */
229 *space = '\0';
231 return NULL;
234 void
235 got_object_commit_close(struct got_commit_object *commit)
237 struct got_object_qid *qid;
239 if (commit->refcnt > 0) {
240 commit->refcnt--;
241 if (commit->refcnt > 0)
242 return;
245 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
246 qid = SIMPLEQ_FIRST(&commit->parent_ids);
247 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
248 got_object_qid_free(qid);
251 free(commit->tree_id);
252 free(commit->author);
253 free(commit->committer);
254 free(commit->logmsg);
255 free(commit);
258 const struct got_error *
259 got_object_parse_commit(struct got_commit_object **commit, char *buf, size_t len)
261 const struct got_error *err = NULL;
262 char *s = buf;
263 size_t tlen;
264 ssize_t remain = (ssize_t)len;
266 *commit = got_object_commit_alloc_partial();
267 if (*commit == NULL)
268 return got_error_from_errno();
270 tlen = strlen(GOT_COMMIT_TAG_TREE);
271 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
272 remain -= tlen;
273 if (remain < SHA1_DIGEST_STRING_LENGTH) {
274 err = got_error(GOT_ERR_BAD_OBJ_DATA);
275 goto done;
277 s += tlen;
278 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
279 err = got_error(GOT_ERR_BAD_OBJ_DATA);
280 goto done;
282 remain -= SHA1_DIGEST_STRING_LENGTH;
283 s += SHA1_DIGEST_STRING_LENGTH;
284 } else {
285 err = got_error(GOT_ERR_BAD_OBJ_DATA);
286 goto done;
289 tlen = strlen(GOT_COMMIT_TAG_PARENT);
290 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
291 remain -= tlen;
292 if (remain < SHA1_DIGEST_STRING_LENGTH) {
293 err = got_error(GOT_ERR_BAD_OBJ_DATA);
294 goto done;
296 s += tlen;
297 err = got_object_commit_add_parent(*commit, s);
298 if (err)
299 goto done;
301 remain -= SHA1_DIGEST_STRING_LENGTH;
302 s += SHA1_DIGEST_STRING_LENGTH;
305 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
306 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
307 char *p;
308 size_t slen;
310 remain -= tlen;
311 if (remain <= 0) {
312 err = got_error(GOT_ERR_BAD_OBJ_DATA);
313 goto done;
315 s += tlen;
316 p = strchr(s, '\n');
317 if (p == NULL) {
318 err = got_error(GOT_ERR_BAD_OBJ_DATA);
319 goto done;
321 *p = '\0';
322 slen = strlen(s);
323 err = parse_commit_time(&(*commit)->tm_author, s);
324 if (err)
325 goto done;
326 (*commit)->author = strdup(s);
327 if ((*commit)->author == NULL) {
328 err = got_error_from_errno();
329 goto done;
331 s += slen + 1;
332 remain -= slen + 1;
335 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
336 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
337 char *p;
338 size_t slen;
340 remain -= tlen;
341 if (remain <= 0) {
342 err = got_error(GOT_ERR_BAD_OBJ_DATA);
343 goto done;
345 s += tlen;
346 p = strchr(s, '\n');
347 if (p == NULL) {
348 err = got_error(GOT_ERR_BAD_OBJ_DATA);
349 goto done;
351 *p = '\0';
352 slen = strlen(s);
353 err = parse_commit_time(&(*commit)->tm_committer, s);
354 if (err)
355 goto done;
356 (*commit)->committer = strdup(s);
357 if ((*commit)->committer == NULL) {
358 err = got_error_from_errno();
359 goto done;
361 s += slen + 1;
362 remain -= slen + 1;
365 (*commit)->logmsg = strndup(s, remain);
366 if ((*commit)->logmsg == NULL) {
367 err = got_error_from_errno();
368 goto done;
370 done:
371 if (err) {
372 got_object_commit_close(*commit);
373 *commit = NULL;
375 return err;
378 void
379 got_object_tree_entry_close(struct got_tree_entry *te)
381 free(te->id);
382 free(te->name);
383 free(te);
386 void
387 got_object_tree_close(struct got_tree_object *tree)
389 struct got_tree_entry *te;
391 if (tree->refcnt > 0) {
392 tree->refcnt--;
393 if (tree->refcnt > 0)
394 return;
397 while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
398 te = SIMPLEQ_FIRST(&tree->entries.head);
399 SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
400 got_object_tree_entry_close(te);
403 free(tree);
406 struct got_tree_entry *
407 got_alloc_tree_entry_partial(void)
409 struct got_tree_entry *te;
411 te = calloc(1, sizeof(*te));
412 if (te == NULL)
413 return NULL;
415 te->id = calloc(1, sizeof(*te->id));
416 if (te->id == NULL) {
417 free(te);
418 te = NULL;
420 return te;
423 static const struct got_error *
424 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
425 size_t maxlen)
427 char *p = buf, *space;
428 const struct got_error *err = NULL;
430 *te = got_alloc_tree_entry_partial();
431 if (*te == NULL)
432 return got_error_from_errno();
434 *elen = strlen(buf) + 1;
435 if (*elen > maxlen) {
436 free(*te);
437 *te = NULL;
438 return got_error(GOT_ERR_BAD_OBJ_DATA);
441 space = strchr(buf, ' ');
442 if (space == NULL) {
443 err = got_error(GOT_ERR_BAD_OBJ_DATA);
444 free(*te);
445 *te = NULL;
446 return err;
448 while (*p != ' ') {
449 if (*p < '0' && *p > '7') {
450 err = got_error(GOT_ERR_BAD_OBJ_DATA);
451 goto done;
453 (*te)->mode <<= 3;
454 (*te)->mode |= *p - '0';
455 p++;
458 (*te)->name = strdup(space + 1);
459 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
460 err = got_error(GOT_ERR_BAD_OBJ_DATA);
461 goto done;
463 buf += strlen(buf) + 1;
464 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
465 *elen += SHA1_DIGEST_LENGTH;
466 done:
467 if (err) {
468 got_object_tree_entry_close(*te);
469 *te = NULL;
471 return err;
474 const struct got_error *
475 got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
477 const struct got_error *err;
478 size_t remain = len;
480 *tree = calloc(1, sizeof(**tree));
481 if (*tree == NULL)
482 return got_error_from_errno();
484 SIMPLEQ_INIT(&(*tree)->entries.head);
486 while (remain > 0) {
487 struct got_tree_entry *te;
488 size_t elen;
490 err = parse_tree_entry(&te, &elen, buf, remain);
491 if (err)
492 return err;
493 (*tree)->entries.nentries++;
494 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
495 buf += elen;
496 remain -= elen;
499 if (remain != 0) {
500 got_object_tree_close(*tree);
501 return got_error(GOT_ERR_BAD_OBJ_DATA);
504 return NULL;
507 const struct got_error *
508 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
510 const struct got_error *err = NULL;
511 static const size_t blocksize = 512;
512 size_t n, total, remain;
513 uint8_t *buf;
515 *outbuf = NULL;
516 *outlen = 0;
518 buf = malloc(blocksize);
519 if (buf == NULL)
520 return got_error_from_errno();
522 remain = blocksize;
523 total = 0;
524 while (1) {
525 if (remain == 0) {
526 uint8_t *newbuf;
527 newbuf = reallocarray(buf, 1, total + blocksize);
528 if (newbuf == NULL) {
529 err = got_error_from_errno();
530 goto done;
532 buf = newbuf;
533 remain += blocksize;
535 n = fread(buf + total, 1, remain, f);
536 if (n == 0) {
537 if (ferror(f)) {
538 err = got_ferror(f, GOT_ERR_IO);
539 goto done;
541 break; /* EOF */
543 remain -= n;
544 total += n;
545 };
547 done:
548 if (err == NULL) {
549 *outbuf = buf;
550 *outlen = total;
551 } else
552 free(buf);
553 return err;