Blob


1 /*
2 * Copyright (c) 2019 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>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdint.h>
28 #include <sha1.h>
29 #include <unistd.h>
30 #include <zlib.h>
32 #include "got_error.h"
33 #include "got_object.h"
34 #include "got_repository.h"
35 #include "got_opentemp.h"
36 #include "got_path.h"
38 #include "got_lib_sha1.h"
39 #include "got_lib_deflate.h"
40 #include "got_lib_delta.h"
41 #include "got_lib_object.h"
42 #include "got_lib_object_parse.h"
43 #include "got_lib_lockfile.h"
45 #ifndef nitems
46 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
47 #endif
49 static const struct got_error *
50 create_object_file(struct got_object_id *id, FILE *content,
51 struct got_repository *repo)
52 {
53 const struct got_error *err = NULL, *unlock_err = NULL;
54 char *objpath = NULL, *tmppath = NULL;
55 FILE *tmpfile = NULL;
56 struct got_lockfile *lf = NULL;
57 size_t tmplen = 0;
59 err = got_object_get_path(&objpath, id, repo);
60 if (err)
61 return err;
63 err = got_opentemp_named(&tmppath, &tmpfile, objpath);
64 if (err) {
65 char *parent_path;
66 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
67 goto done;
68 err = got_path_dirname(&parent_path, objpath);
69 if (err)
70 goto done;
71 err = got_path_mkdir(parent_path);
72 free(parent_path);
73 if (err)
74 goto done;
75 err = got_opentemp_named(&tmppath, &tmpfile, objpath);
76 if (err)
77 goto done;
78 }
80 err = got_deflate_to_file(&tmplen, content, tmpfile);
81 if (err)
82 goto done;
84 err = got_lockfile_lock(&lf, objpath);
85 if (err)
86 goto done;
88 if (rename(tmppath, objpath) != 0) {
89 err = got_error_from_errno3("rename", tmppath, objpath);
90 goto done;
91 }
92 free(tmppath);
93 tmppath = NULL;
95 if (chmod(objpath, GOT_DEFAULT_FILE_MODE) != 0) {
96 err = got_error_from_errno2("chmod", objpath);
97 goto done;
98 }
99 done:
100 free(objpath);
101 if (tmppath) {
102 if (unlink(tmppath) != 0 && err == NULL)
103 err = got_error_from_errno2("unlink", tmppath);
104 free(tmppath);
106 if (tmpfile && fclose(tmpfile) != 0 && err == NULL)
107 err = got_error_from_errno("fclose");
108 if (lf)
109 unlock_err = got_lockfile_unlock(lf);
110 return err ? err : unlock_err;
113 const struct got_error *
114 got_object_blob_create(struct got_object_id **id, const char *ondisk_path,
115 struct got_repository *repo)
117 const struct got_error *err = NULL;
118 char *header = NULL;
119 FILE *blobfile = NULL;
120 int fd = -1;
121 struct stat sb;
122 SHA1_CTX sha1_ctx;
123 size_t headerlen = 0, n;
125 *id = NULL;
127 SHA1Init(&sha1_ctx);
129 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
130 if (fd == -1)
131 return got_error_from_errno2("open", ondisk_path);
133 if (fstat(fd, &sb) == -1) {
134 err = got_error_from_errno2("fstat", ondisk_path);
135 goto done;
138 if (asprintf(&header, "%s %lld", GOT_OBJ_LABEL_BLOB,
139 sb.st_size) == -1) {
140 err = got_error_from_errno("asprintf");
141 goto done;
143 headerlen = strlen(header) + 1;
144 SHA1Update(&sha1_ctx, header, headerlen);
146 blobfile = got_opentemp();
147 if (blobfile == NULL) {
148 err = got_error_from_errno("got_opentemp");
149 goto done;
152 n = fwrite(header, 1, headerlen, blobfile);
153 if (n != headerlen) {
154 err = got_ferror(blobfile, GOT_ERR_IO);
155 goto done;
157 for (;;) {
158 char buf[8192];
159 ssize_t inlen;
161 inlen = read(fd, buf, sizeof(buf));
162 if (inlen == -1) {
163 err = got_error_from_errno("read");
164 goto done;
166 if (inlen == 0)
167 break; /* EOF */
168 SHA1Update(&sha1_ctx, buf, inlen);
169 n = fwrite(buf, 1, inlen, blobfile);
170 if (n != inlen) {
171 err = got_ferror(blobfile, GOT_ERR_IO);
172 goto done;
176 *id = malloc(sizeof(**id));
177 if (*id == NULL) {
178 err = got_error_from_errno("malloc");
179 goto done;
181 SHA1Final((*id)->sha1, &sha1_ctx);
183 if (fflush(blobfile) != 0) {
184 err = got_error_from_errno("fflush");
185 goto done;
187 rewind(blobfile);
189 err = create_object_file(*id, blobfile, repo);
190 done:
191 free(header);
192 if (fd != -1 && close(fd) != 0 && err == NULL)
193 err = got_error_from_errno("close");
194 if (blobfile && fclose(blobfile) != 0 && err == NULL)
195 err = got_error_from_errno("fclose");
196 if (err) {
197 free(*id);
198 *id = NULL;
200 return err;
203 static const struct got_error *
204 mode2str(char *buf, size_t len, mode_t mode)
206 int ret;
207 ret = snprintf(buf, len, "%o ", mode);
208 if (ret == -1 || ret >= len)
209 return got_error(GOT_ERR_NO_SPACE);
210 return NULL;
214 static const struct got_error *
215 te_git_name(char **name, struct got_tree_entry *te)
217 const struct got_error *err = NULL;
219 if (S_ISDIR(te->mode)) {
220 if (asprintf(name, "%s/", te->name) == -1)
221 err = got_error_from_errno("asprintf");
222 } else {
223 *name = strdup(te->name);
224 if (*name == NULL)
225 err = got_error_from_errno("strdup");
227 return err;
230 static const struct got_error *
231 insert_tree_entry(struct got_tree_entries *sorted, struct got_tree_entry *new)
233 const struct got_error *err = NULL;
234 struct got_tree_entry *te = NULL, *prev = NULL;
235 char *name;
236 int cmp;
238 if (SIMPLEQ_EMPTY(&sorted->head)) {
239 SIMPLEQ_INSERT_HEAD(&sorted->head, new, entry);
240 sorted->nentries++;
241 return NULL;
244 err = te_git_name(&name, new);
245 if (err)
246 return err;
248 te = SIMPLEQ_FIRST(&sorted->head);
249 while (te) {
250 char *te_name;
251 if (prev == NULL) {
252 prev = te;
253 te = SIMPLEQ_NEXT(te, entry);
254 continue;
256 err = te_git_name(&te_name, te);
257 if (err)
258 goto done;
259 cmp = strcmp(te_name, name);
260 free(te_name);
261 if (cmp == 0) {
262 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
263 goto done;
265 if (cmp > 0) {
266 SIMPLEQ_INSERT_AFTER(&sorted->head, prev, new, entry);
267 sorted->nentries++;
268 break;
270 prev = te;
271 te = SIMPLEQ_NEXT(te, entry);
274 if (te == NULL) {
275 SIMPLEQ_INSERT_TAIL(&sorted->head, new, entry);
276 sorted->nentries++;
278 done:
279 free(name);
280 return err;
283 /*
284 * Git expects directory tree entries to be sorted with an imaginary slash
285 * appended to their name, and will break otherwise. Let's be nice.
286 */
287 static const struct got_error *
288 sort_tree_entries_the_way_git_likes_it(struct got_tree_entries **sorted,
289 struct got_tree_entries *tree_entries)
291 const struct got_error *err = NULL;
292 struct got_tree_entry *te;
294 *sorted = malloc(sizeof(**sorted));
295 if (*sorted == NULL)
296 return got_error_from_errno("malloc");
298 (*sorted)->nentries = 0;
299 SIMPLEQ_INIT(&(*sorted)->head);
301 SIMPLEQ_FOREACH(te, &tree_entries->head, entry) {
302 struct got_tree_entry *new;
303 err = got_object_tree_entry_dup(&new, te);
304 if (err)
305 break;
306 err = insert_tree_entry(*sorted, new);
307 if (err) {
308 got_object_tree_entry_close(new);
309 break;
313 if (err) {
314 free(*sorted);
315 *sorted = NULL;
318 return err;
321 const struct got_error *
322 got_object_tree_create(struct got_object_id **id,
323 struct got_tree_entries *tree_entries, struct got_repository *repo)
325 const struct got_error *err = NULL;
326 char modebuf[sizeof("100644 ")];
327 SHA1_CTX sha1_ctx;
328 char *header = NULL;
329 size_t headerlen, len = 0, n;
330 FILE *treefile = NULL;
331 struct got_tree_entries *entries; /* sorted according to Git rules */
332 struct got_tree_entry *te;
334 *id = NULL;
336 SHA1Init(&sha1_ctx);
338 err = sort_tree_entries_the_way_git_likes_it(&entries, tree_entries);
339 if (err)
340 return err;
342 SIMPLEQ_FOREACH(te, &entries->head, entry) {
343 err = mode2str(modebuf, sizeof(modebuf), te->mode);
344 if (err)
345 goto done;
346 len += strlen(modebuf) + strlen(te->name) + 1 +
347 SHA1_DIGEST_LENGTH;
350 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TREE, len) == -1) {
351 err = got_error_from_errno("asprintf");
352 goto done;
354 headerlen = strlen(header) + 1;
355 SHA1Update(&sha1_ctx, header, headerlen);
357 treefile = got_opentemp();
358 if (treefile == NULL) {
359 err = got_error_from_errno("got_opentemp");
360 goto done;
363 n = fwrite(header, 1, headerlen, treefile);
364 if (n != headerlen) {
365 err = got_ferror(treefile, GOT_ERR_IO);
366 goto done;
369 SIMPLEQ_FOREACH(te, &entries->head, entry) {
370 err = mode2str(modebuf, sizeof(modebuf), te->mode);
371 if (err)
372 goto done;
373 len = strlen(modebuf);
374 n = fwrite(modebuf, 1, len, treefile);
375 if (n != len) {
376 err = got_ferror(treefile, GOT_ERR_IO);
377 goto done;
379 SHA1Update(&sha1_ctx, modebuf, len);
381 len = strlen(te->name) + 1; /* must include NUL */
382 n = fwrite(te->name, 1, len, treefile);
383 if (n != len) {
384 err = got_ferror(treefile, GOT_ERR_IO);
385 goto done;
387 SHA1Update(&sha1_ctx, te->name, len);
389 len = SHA1_DIGEST_LENGTH;
390 n = fwrite(te->id->sha1, 1, len, treefile);
391 if (n != len) {
392 err = got_ferror(treefile, GOT_ERR_IO);
393 goto done;
395 SHA1Update(&sha1_ctx, te->id->sha1, len);
398 *id = malloc(sizeof(**id));
399 if (*id == NULL) {
400 err = got_error_from_errno("malloc");
401 goto done;
403 SHA1Final((*id)->sha1, &sha1_ctx);
405 if (fflush(treefile) != 0) {
406 err = got_error_from_errno("fflush");
407 goto done;
409 rewind(treefile);
411 err = create_object_file(*id, treefile, repo);
412 done:
413 free(header);
414 got_object_tree_entries_close(entries);
415 free(entries);
416 if (treefile && fclose(treefile) != 0 && err == NULL)
417 err = got_error_from_errno("fclose");
418 if (err) {
419 free(*id);
420 *id = NULL;
422 return err;
425 const struct got_error *
426 got_object_commit_create(struct got_object_id **id,
427 struct got_object_id *tree_id, struct got_object_id_queue *parent_ids,
428 int nparents, const char *author, time_t author_time,
429 const char *committer, time_t committer_time,
430 const char *logmsg, struct got_repository *repo)
432 const struct got_error *err = NULL;
433 SHA1_CTX sha1_ctx;
434 char *header = NULL, *tree_str = NULL;
435 char *author_str = NULL, *committer_str = NULL;
436 char *id_str = NULL;
437 size_t headerlen, len = 0, n;
438 FILE *commitfile = NULL;
439 struct got_object_qid *qid;
440 char *msg0, *msg;
442 *id = NULL;
444 SHA1Init(&sha1_ctx);
446 msg0 = strdup(logmsg);
447 if (msg0 == NULL)
448 return got_error_from_errno("strdup");
449 msg = msg0;
451 while (isspace((unsigned char)msg[0]))
452 msg++;
453 len = strlen(msg);
454 while (len > 0 && isspace((unsigned char)msg[len - 1])) {
455 msg[len - 1] = '\0';
456 len--;
459 if (asprintf(&author_str, "%s%s %lld +0000\n",
460 GOT_COMMIT_LABEL_AUTHOR, author, author_time) == -1)
461 return got_error_from_errno("asprintf");
463 if (asprintf(&committer_str, "%s%s %lld +0000\n",
464 GOT_COMMIT_LABEL_COMMITTER, committer ? committer : author,
465 committer ? committer_time : author_time)
466 == -1) {
467 err = got_error_from_errno("asprintf");
468 goto done;
471 len = strlen(GOT_COMMIT_LABEL_TREE) + SHA1_DIGEST_STRING_LENGTH +
472 nparents *
473 (strlen(GOT_COMMIT_LABEL_PARENT) + SHA1_DIGEST_STRING_LENGTH) +
474 + strlen(author_str) + strlen(committer_str) + 2 + strlen(msg);
476 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_COMMIT, len) == -1) {
477 err = got_error_from_errno("asprintf");
478 goto done;
480 headerlen = strlen(header) + 1;
481 SHA1Update(&sha1_ctx, header, headerlen);
483 commitfile = got_opentemp();
484 if (commitfile == NULL) {
485 err = got_error_from_errno("got_opentemp");
486 goto done;
489 n = fwrite(header, 1, headerlen, commitfile);
490 if (n != headerlen) {
491 err = got_ferror(commitfile, GOT_ERR_IO);
492 goto done;
495 err = got_object_id_str(&id_str, tree_id);
496 if (err)
497 goto done;
498 if (asprintf(&tree_str, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str)
499 == -1) {
500 err = got_error_from_errno("asprintf");
501 goto done;
503 len = strlen(tree_str);
504 SHA1Update(&sha1_ctx, tree_str, len);
505 n = fwrite(tree_str, 1, len, commitfile);
506 if (n != len) {
507 err = got_ferror(commitfile, GOT_ERR_IO);
508 goto done;
511 if (parent_ids) {
512 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
513 char *parent_str = NULL;
515 free(id_str);
517 err = got_object_id_str(&id_str, qid->id);
518 if (err)
519 goto done;
520 if (asprintf(&parent_str, "%s%s\n",
521 GOT_COMMIT_LABEL_PARENT, id_str) == -1) {
522 err = got_error_from_errno("asprintf");
523 goto done;
525 len = strlen(parent_str);
526 SHA1Update(&sha1_ctx, parent_str, len);
527 n = fwrite(parent_str, 1, len, commitfile);
528 if (n != len) {
529 err = got_ferror(commitfile, GOT_ERR_IO);
530 free(parent_str);
531 goto done;
533 free(parent_str);
537 len = strlen(author_str);
538 SHA1Update(&sha1_ctx, author_str, len);
539 n = fwrite(author_str, 1, len, commitfile);
540 if (n != len) {
541 err = got_ferror(commitfile, GOT_ERR_IO);
542 goto done;
545 len = strlen(committer_str);
546 SHA1Update(&sha1_ctx, committer_str, len);
547 n = fwrite(committer_str, 1, len, commitfile);
548 if (n != len) {
549 err = got_ferror(commitfile, GOT_ERR_IO);
550 goto done;
553 SHA1Update(&sha1_ctx, "\n", 1);
554 n = fwrite("\n", 1, 1, commitfile);
555 if (n != 1) {
556 err = got_ferror(commitfile, GOT_ERR_IO);
557 goto done;
560 len = strlen(msg);
561 SHA1Update(&sha1_ctx, msg, len);
562 n = fwrite(msg, 1, len, commitfile);
563 if (n != len) {
564 err = got_ferror(commitfile, GOT_ERR_IO);
565 goto done;
568 SHA1Update(&sha1_ctx, "\n", 1);
569 n = fwrite("\n", 1, 1, commitfile);
570 if (n != 1) {
571 err = got_ferror(commitfile, GOT_ERR_IO);
572 goto done;
575 *id = malloc(sizeof(**id));
576 if (*id == NULL) {
577 err = got_error_from_errno("malloc");
578 goto done;
580 SHA1Final((*id)->sha1, &sha1_ctx);
582 if (fflush(commitfile) != 0) {
583 err = got_error_from_errno("fflush");
584 goto done;
586 rewind(commitfile);
588 err = create_object_file(*id, commitfile, repo);
589 done:
590 free(msg0);
591 free(header);
592 free(tree_str);
593 free(author_str);
594 free(committer_str);
595 if (commitfile && fclose(commitfile) != 0 && err == NULL)
596 err = got_error_from_errno("fclose");
597 if (err) {
598 free(*id);
599 *id = NULL;
601 return err;
604 const struct got_error *
605 got_object_tag_create(struct got_object_id **id,
606 const char *tag_name, struct got_object_id *object_id, const char *tagger,
607 time_t tagger_time, const char *tagmsg, struct got_repository *repo)
609 const struct got_error *err = NULL;
610 SHA1_CTX sha1_ctx;
611 char *header = NULL;
612 char *tag_str = NULL, *tagger_str = NULL;
613 char *id_str = NULL, *obj_str = NULL, *type_str = NULL;
614 size_t headerlen, len = 0, n;
615 FILE *tagfile = NULL;
616 char *msg0 = NULL, *msg;
617 const char *obj_type_str;
618 int obj_type;
620 *id = NULL;
622 SHA1Init(&sha1_ctx);
624 err = got_object_id_str(&id_str, object_id);
625 if (err)
626 goto done;
627 if (asprintf(&obj_str, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str) == -1) {
628 err = got_error_from_errno("asprintf");
629 goto done;
632 err = got_object_get_type(&obj_type, repo, object_id);
633 if (err)
634 goto done;
636 switch (obj_type) {
637 case GOT_OBJ_TYPE_BLOB:
638 obj_type_str = GOT_OBJ_LABEL_BLOB;
639 break;
640 case GOT_OBJ_TYPE_TREE:
641 obj_type_str = GOT_OBJ_LABEL_TREE;
642 break;
643 case GOT_OBJ_TYPE_COMMIT:
644 obj_type_str = GOT_OBJ_LABEL_COMMIT;
645 break;
646 case GOT_OBJ_TYPE_TAG:
647 obj_type_str = GOT_OBJ_LABEL_TAG;
648 break;
649 default:
650 err = got_error(GOT_ERR_OBJ_TYPE);
651 goto done;
654 if (asprintf(&type_str, "%s%s\n", GOT_TAG_LABEL_TYPE,
655 obj_type_str) == -1) {
656 err = got_error_from_errno("asprintf");
657 goto done;
660 if (asprintf(&tag_str, "%s%s\n", GOT_TAG_LABEL_TAG, tag_name) == -1) {
661 err = got_error_from_errno("asprintf");
662 goto done;
665 if (asprintf(&tagger_str, "%s%s %lld +0000\n",
666 GOT_TAG_LABEL_TAGGER, tagger, tagger_time) == -1)
667 return got_error_from_errno("asprintf");
669 msg0 = strdup(tagmsg);
670 if (msg0 == NULL) {
671 err = got_error_from_errno("strdup");
672 goto done;
674 msg = msg0;
676 while (isspace((unsigned char)msg[0]))
677 msg++;
679 len = strlen(obj_str) + strlen(type_str) + strlen(tag_str) +
680 strlen(tagger_str) + 1 + strlen(msg) + 1;
682 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TAG, len) == -1) {
683 err = got_error_from_errno("asprintf");
684 goto done;
687 headerlen = strlen(header) + 1;
688 SHA1Update(&sha1_ctx, header, headerlen);
690 tagfile = got_opentemp();
691 if (tagfile == NULL) {
692 err = got_error_from_errno("got_opentemp");
693 goto done;
696 n = fwrite(header, 1, headerlen, tagfile);
697 if (n != headerlen) {
698 err = got_ferror(tagfile, GOT_ERR_IO);
699 goto done;
701 len = strlen(obj_str);
702 SHA1Update(&sha1_ctx, obj_str, len);
703 n = fwrite(obj_str, 1, len, tagfile);
704 if (n != len) {
705 err = got_ferror(tagfile, GOT_ERR_IO);
706 goto done;
708 len = strlen(type_str);
709 SHA1Update(&sha1_ctx, type_str, len);
710 n = fwrite(type_str, 1, len, tagfile);
711 if (n != len) {
712 err = got_ferror(tagfile, GOT_ERR_IO);
713 goto done;
716 len = strlen(tag_str);
717 SHA1Update(&sha1_ctx, tag_str, len);
718 n = fwrite(tag_str, 1, len, tagfile);
719 if (n != len) {
720 err = got_ferror(tagfile, GOT_ERR_IO);
721 goto done;
724 len = strlen(tagger_str);
725 SHA1Update(&sha1_ctx, tagger_str, len);
726 n = fwrite(tagger_str, 1, len, tagfile);
727 if (n != len) {
728 err = got_ferror(tagfile, GOT_ERR_IO);
729 goto done;
732 SHA1Update(&sha1_ctx, "\n", 1);
733 n = fwrite("\n", 1, 1, tagfile);
734 if (n != 1) {
735 err = got_ferror(tagfile, GOT_ERR_IO);
736 goto done;
739 len = strlen(msg);
740 SHA1Update(&sha1_ctx, msg, len);
741 n = fwrite(msg, 1, len, tagfile);
742 if (n != len) {
743 err = got_ferror(tagfile, GOT_ERR_IO);
744 goto done;
747 SHA1Update(&sha1_ctx, "\n", 1);
748 n = fwrite("\n", 1, 1, tagfile);
749 if (n != 1) {
750 err = got_ferror(tagfile, GOT_ERR_IO);
751 goto done;
754 *id = malloc(sizeof(**id));
755 if (*id == NULL) {
756 err = got_error_from_errno("malloc");
757 goto done;
759 SHA1Final((*id)->sha1, &sha1_ctx);
761 if (fflush(tagfile) != 0) {
762 err = got_error_from_errno("fflush");
763 goto done;
765 rewind(tagfile);
767 err = create_object_file(*id, tagfile, repo);
768 done:
769 free(msg0);
770 free(header);
771 free(obj_str);
772 free(tagger_str);
773 if (tagfile && fclose(tagfile) != 0 && err == NULL)
774 err = got_error_from_errno("fclose");
775 if (err) {
776 free(*id);
777 *id = NULL;
779 return err;