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 <limits.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdint.h>
29 #include <sha1.h>
30 #include <unistd.h>
31 #include <zlib.h>
33 #include "got_error.h"
34 #include "got_object.h"
35 #include "got_repository.h"
36 #include "got_opentemp.h"
37 #include "got_path.h"
39 #include "got_lib_sha1.h"
40 #include "got_lib_deflate.h"
41 #include "got_lib_delta.h"
42 #include "got_lib_object.h"
43 #include "got_lib_object_parse.h"
44 #include "got_lib_lockfile.h"
46 #ifndef nitems
47 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
48 #endif
50 static const struct got_error *
51 create_object_file(struct got_object_id *id, FILE *content,
52 struct got_repository *repo)
53 {
54 const struct got_error *err = NULL, *unlock_err = NULL;
55 char *objpath = NULL, *tmppath = NULL;
56 FILE *tmpfile = NULL;
57 struct got_lockfile *lf = NULL;
58 size_t tmplen = 0;
60 err = got_object_get_path(&objpath, id, repo);
61 if (err)
62 return err;
64 err = got_opentemp_named(&tmppath, &tmpfile, objpath);
65 if (err) {
66 char *parent_path;
67 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
68 goto done;
69 err = got_path_dirname(&parent_path, objpath);
70 if (err)
71 goto done;
72 err = got_path_mkdir(parent_path);
73 free(parent_path);
74 if (err)
75 goto done;
76 err = got_opentemp_named(&tmppath, &tmpfile, objpath);
77 if (err)
78 goto done;
79 }
81 err = got_deflate_to_file(&tmplen, content, tmpfile);
82 if (err)
83 goto done;
85 err = got_lockfile_lock(&lf, objpath);
86 if (err)
87 goto done;
89 if (rename(tmppath, objpath) != 0) {
90 err = got_error_from_errno3("rename", tmppath, objpath);
91 goto done;
92 }
93 free(tmppath);
94 tmppath = NULL;
96 if (chmod(objpath, GOT_DEFAULT_FILE_MODE) != 0) {
97 err = got_error_from_errno2("chmod", objpath);
98 goto done;
99 }
100 done:
101 free(objpath);
102 if (tmppath) {
103 if (unlink(tmppath) != 0 && err == NULL)
104 err = got_error_from_errno2("unlink", tmppath);
105 free(tmppath);
107 if (tmpfile && fclose(tmpfile) != 0 && err == NULL)
108 err = got_error_from_errno("fclose");
109 if (lf)
110 unlock_err = got_lockfile_unlock(lf);
111 return err ? err : unlock_err;
114 const struct got_error *
115 got_object_blob_create(struct got_object_id **id, const char *ondisk_path,
116 struct got_repository *repo)
118 const struct got_error *err = NULL;
119 char *header = NULL;
120 FILE *blobfile = NULL;
121 int fd = -1;
122 struct stat sb;
123 SHA1_CTX sha1_ctx;
124 size_t headerlen = 0, n;
126 *id = NULL;
128 SHA1Init(&sha1_ctx);
130 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
131 if (fd == -1) {
132 if (errno != ELOOP)
133 return got_error_from_errno2("open", ondisk_path);
135 if (lstat(ondisk_path, &sb) == -1) {
136 err = got_error_from_errno2("lstat", ondisk_path);
137 goto done;
139 } else if (fstat(fd, &sb) == -1) {
140 err = got_error_from_errno2("fstat", ondisk_path);
141 goto done;
144 if (asprintf(&header, "%s %lld", GOT_OBJ_LABEL_BLOB,
145 sb.st_size) == -1) {
146 err = got_error_from_errno("asprintf");
147 goto done;
149 headerlen = strlen(header) + 1;
150 SHA1Update(&sha1_ctx, header, headerlen);
152 blobfile = got_opentemp();
153 if (blobfile == NULL) {
154 err = got_error_from_errno("got_opentemp");
155 goto done;
158 n = fwrite(header, 1, headerlen, blobfile);
159 if (n != headerlen) {
160 err = got_ferror(blobfile, GOT_ERR_IO);
161 goto done;
163 for (;;) {
164 char buf[PATH_MAX * 8];
165 ssize_t inlen;
167 if (S_ISLNK(sb.st_mode)) {
168 inlen = readlink(ondisk_path, buf, sizeof(buf));
169 if (inlen == -1) {
170 err = got_error_from_errno("readlink");
171 goto done;
173 } else {
174 inlen = read(fd, buf, sizeof(buf));
175 if (inlen == -1) {
176 err = got_error_from_errno("read");
177 goto done;
180 if (inlen == 0)
181 break; /* EOF */
182 SHA1Update(&sha1_ctx, buf, inlen);
183 n = fwrite(buf, 1, inlen, blobfile);
184 if (n != inlen) {
185 err = got_ferror(blobfile, GOT_ERR_IO);
186 goto done;
188 if (S_ISLNK(sb.st_mode))
189 break;
192 *id = malloc(sizeof(**id));
193 if (*id == NULL) {
194 err = got_error_from_errno("malloc");
195 goto done;
197 SHA1Final((*id)->sha1, &sha1_ctx);
199 if (fflush(blobfile) != 0) {
200 err = got_error_from_errno("fflush");
201 goto done;
203 rewind(blobfile);
205 err = create_object_file(*id, blobfile, repo);
206 done:
207 free(header);
208 if (fd != -1 && close(fd) != 0 && err == NULL)
209 err = got_error_from_errno("close");
210 if (blobfile && fclose(blobfile) != 0 && err == NULL)
211 err = got_error_from_errno("fclose");
212 if (err) {
213 free(*id);
214 *id = NULL;
216 return err;
219 static const struct got_error *
220 te_mode2str(char *buf, size_t len, struct got_tree_entry *te)
222 int ret;
223 mode_t mode;
225 /*
226 * Some Git implementations are picky about modes seen in tree entries.
227 * For best compatibility we normalize the file/directory mode here.
228 * Note that we do not support committing symlinks.
229 */
230 if (S_ISREG(te->mode)) {
231 mode = GOT_DEFAULT_FILE_MODE;
232 if (te->mode & (S_IXUSR | S_IXGRP | S_IXOTH))
233 mode |= S_IXUSR | S_IXGRP | S_IXOTH;
234 } else if (got_object_tree_entry_is_submodule(te))
235 mode = S_IFDIR | S_IFLNK;
236 else if (S_ISLNK(te->mode))
237 mode = S_IFLNK; /* Git leaves all the other bits unset. */
238 else if (S_ISDIR(te->mode))
239 mode = S_IFDIR; /* Git leaves all the other bits unset. */
240 else
241 return got_error(GOT_ERR_BAD_FILETYPE);
243 ret = snprintf(buf, len, "%o ", mode);
244 if (ret == -1 || ret >= len)
245 return got_error(GOT_ERR_NO_SPACE);
246 return NULL;
249 /*
250 * Git expects directory tree entries to be sorted with an imaginary slash
251 * appended to their name, and will break otherwise. Let's be nice.
252 * This function is intended to be used with mergesort(3) to sort an
253 * array of pointers to struct got_tree_entry objects.
254 */
255 static int
256 sort_tree_entries_the_way_git_likes_it(const void *arg1, const void *arg2)
258 struct got_tree_entry * const *te1 = arg1;
259 struct got_tree_entry * const *te2 = arg2;
260 char name1[NAME_MAX + 2];
261 char name2[NAME_MAX + 2];
263 strlcpy(name1, (*te1)->name, sizeof(name1));
264 strlcpy(name2, (*te2)->name, sizeof(name2));
265 if (S_ISDIR((*te1)->mode))
266 strlcat(name1, "/", sizeof(name1));
267 if (S_ISDIR((*te2)->mode))
268 strlcat(name2, "/", sizeof(name2));
269 return strcmp(name1, name2);
272 const struct got_error *
273 got_object_tree_create(struct got_object_id **id,
274 struct got_pathlist_head *paths, int nentries, struct got_repository *repo)
276 const struct got_error *err = NULL;
277 char modebuf[sizeof("100644 ")];
278 SHA1_CTX sha1_ctx;
279 char *header = NULL;
280 size_t headerlen, len = 0, n;
281 FILE *treefile = NULL;
282 struct got_pathlist_entry *pe;
283 struct got_tree_entry **sorted_entries;
284 struct got_tree_entry *te;
285 int i;
287 *id = NULL;
289 SHA1Init(&sha1_ctx);
291 sorted_entries = calloc(nentries, sizeof(struct got_tree_entry *));
292 if (sorted_entries == NULL)
293 return got_error_from_errno("calloc");
295 i = 0;
296 TAILQ_FOREACH(pe, paths, entry)
297 sorted_entries[i++] = pe->data;
298 mergesort(sorted_entries, nentries, sizeof(struct got_tree_entry *),
299 sort_tree_entries_the_way_git_likes_it);
301 for (i = 0; i < nentries; i++) {
302 te = sorted_entries[i];
303 err = te_mode2str(modebuf, sizeof(modebuf), te);
304 if (err)
305 goto done;
306 len += strlen(modebuf) + strlen(te->name) + 1 +
307 SHA1_DIGEST_LENGTH;
310 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TREE, len) == -1) {
311 err = got_error_from_errno("asprintf");
312 goto done;
314 headerlen = strlen(header) + 1;
315 SHA1Update(&sha1_ctx, header, headerlen);
317 treefile = got_opentemp();
318 if (treefile == NULL) {
319 err = got_error_from_errno("got_opentemp");
320 goto done;
323 n = fwrite(header, 1, headerlen, treefile);
324 if (n != headerlen) {
325 err = got_ferror(treefile, GOT_ERR_IO);
326 goto done;
329 for (i = 0; i < nentries; i++) {
330 te = sorted_entries[i];
331 err = te_mode2str(modebuf, sizeof(modebuf), te);
332 if (err)
333 goto done;
334 len = strlen(modebuf);
335 n = fwrite(modebuf, 1, len, treefile);
336 if (n != len) {
337 err = got_ferror(treefile, GOT_ERR_IO);
338 goto done;
340 SHA1Update(&sha1_ctx, modebuf, len);
342 len = strlen(te->name) + 1; /* must include NUL */
343 n = fwrite(te->name, 1, len, treefile);
344 if (n != len) {
345 err = got_ferror(treefile, GOT_ERR_IO);
346 goto done;
348 SHA1Update(&sha1_ctx, te->name, len);
350 len = SHA1_DIGEST_LENGTH;
351 n = fwrite(te->id.sha1, 1, len, treefile);
352 if (n != len) {
353 err = got_ferror(treefile, GOT_ERR_IO);
354 goto done;
356 SHA1Update(&sha1_ctx, te->id.sha1, len);
359 *id = malloc(sizeof(**id));
360 if (*id == NULL) {
361 err = got_error_from_errno("malloc");
362 goto done;
364 SHA1Final((*id)->sha1, &sha1_ctx);
366 if (fflush(treefile) != 0) {
367 err = got_error_from_errno("fflush");
368 goto done;
370 rewind(treefile);
372 err = create_object_file(*id, treefile, repo);
373 done:
374 free(header);
375 free(sorted_entries);
376 if (treefile && fclose(treefile) != 0 && err == NULL)
377 err = got_error_from_errno("fclose");
378 if (err) {
379 free(*id);
380 *id = NULL;
382 return err;
385 const struct got_error *
386 got_object_commit_create(struct got_object_id **id,
387 struct got_object_id *tree_id, struct got_object_id_queue *parent_ids,
388 int nparents, const char *author, time_t author_time,
389 const char *committer, time_t committer_time,
390 const char *logmsg, struct got_repository *repo)
392 const struct got_error *err = NULL;
393 SHA1_CTX sha1_ctx;
394 char *header = NULL, *tree_str = NULL;
395 char *author_str = NULL, *committer_str = NULL;
396 char *id_str = NULL;
397 size_t headerlen, len = 0, n;
398 FILE *commitfile = NULL;
399 struct got_object_qid *qid;
400 char *msg0, *msg;
402 *id = NULL;
404 SHA1Init(&sha1_ctx);
406 msg0 = strdup(logmsg);
407 if (msg0 == NULL)
408 return got_error_from_errno("strdup");
409 msg = msg0;
411 while (isspace((unsigned char)msg[0]))
412 msg++;
413 len = strlen(msg);
414 while (len > 0 && isspace((unsigned char)msg[len - 1])) {
415 msg[len - 1] = '\0';
416 len--;
419 if (asprintf(&author_str, "%s%s %lld +0000\n",
420 GOT_COMMIT_LABEL_AUTHOR, author, author_time) == -1)
421 return got_error_from_errno("asprintf");
423 if (asprintf(&committer_str, "%s%s %lld +0000\n",
424 GOT_COMMIT_LABEL_COMMITTER, committer ? committer : author,
425 committer ? committer_time : author_time)
426 == -1) {
427 err = got_error_from_errno("asprintf");
428 goto done;
431 len = strlen(GOT_COMMIT_LABEL_TREE) + SHA1_DIGEST_STRING_LENGTH +
432 nparents *
433 (strlen(GOT_COMMIT_LABEL_PARENT) + SHA1_DIGEST_STRING_LENGTH) +
434 + strlen(author_str) + strlen(committer_str) + 2 + strlen(msg);
436 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_COMMIT, len) == -1) {
437 err = got_error_from_errno("asprintf");
438 goto done;
440 headerlen = strlen(header) + 1;
441 SHA1Update(&sha1_ctx, header, headerlen);
443 commitfile = got_opentemp();
444 if (commitfile == NULL) {
445 err = got_error_from_errno("got_opentemp");
446 goto done;
449 n = fwrite(header, 1, headerlen, commitfile);
450 if (n != headerlen) {
451 err = got_ferror(commitfile, GOT_ERR_IO);
452 goto done;
455 err = got_object_id_str(&id_str, tree_id);
456 if (err)
457 goto done;
458 if (asprintf(&tree_str, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str)
459 == -1) {
460 err = got_error_from_errno("asprintf");
461 goto done;
463 len = strlen(tree_str);
464 SHA1Update(&sha1_ctx, tree_str, len);
465 n = fwrite(tree_str, 1, len, commitfile);
466 if (n != len) {
467 err = got_ferror(commitfile, GOT_ERR_IO);
468 goto done;
471 if (parent_ids) {
472 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
473 char *parent_str = NULL;
475 free(id_str);
477 err = got_object_id_str(&id_str, qid->id);
478 if (err)
479 goto done;
480 if (asprintf(&parent_str, "%s%s\n",
481 GOT_COMMIT_LABEL_PARENT, id_str) == -1) {
482 err = got_error_from_errno("asprintf");
483 goto done;
485 len = strlen(parent_str);
486 SHA1Update(&sha1_ctx, parent_str, len);
487 n = fwrite(parent_str, 1, len, commitfile);
488 if (n != len) {
489 err = got_ferror(commitfile, GOT_ERR_IO);
490 free(parent_str);
491 goto done;
493 free(parent_str);
497 len = strlen(author_str);
498 SHA1Update(&sha1_ctx, author_str, len);
499 n = fwrite(author_str, 1, len, commitfile);
500 if (n != len) {
501 err = got_ferror(commitfile, GOT_ERR_IO);
502 goto done;
505 len = strlen(committer_str);
506 SHA1Update(&sha1_ctx, committer_str, len);
507 n = fwrite(committer_str, 1, len, commitfile);
508 if (n != len) {
509 err = got_ferror(commitfile, GOT_ERR_IO);
510 goto done;
513 SHA1Update(&sha1_ctx, "\n", 1);
514 n = fwrite("\n", 1, 1, commitfile);
515 if (n != 1) {
516 err = got_ferror(commitfile, GOT_ERR_IO);
517 goto done;
520 len = strlen(msg);
521 SHA1Update(&sha1_ctx, msg, len);
522 n = fwrite(msg, 1, len, commitfile);
523 if (n != len) {
524 err = got_ferror(commitfile, GOT_ERR_IO);
525 goto done;
528 SHA1Update(&sha1_ctx, "\n", 1);
529 n = fwrite("\n", 1, 1, commitfile);
530 if (n != 1) {
531 err = got_ferror(commitfile, GOT_ERR_IO);
532 goto done;
535 *id = malloc(sizeof(**id));
536 if (*id == NULL) {
537 err = got_error_from_errno("malloc");
538 goto done;
540 SHA1Final((*id)->sha1, &sha1_ctx);
542 if (fflush(commitfile) != 0) {
543 err = got_error_from_errno("fflush");
544 goto done;
546 rewind(commitfile);
548 err = create_object_file(*id, commitfile, repo);
549 done:
550 free(msg0);
551 free(header);
552 free(tree_str);
553 free(author_str);
554 free(committer_str);
555 if (commitfile && fclose(commitfile) != 0 && err == NULL)
556 err = got_error_from_errno("fclose");
557 if (err) {
558 free(*id);
559 *id = NULL;
561 return err;
564 const struct got_error *
565 got_object_tag_create(struct got_object_id **id,
566 const char *tag_name, struct got_object_id *object_id, const char *tagger,
567 time_t tagger_time, const char *tagmsg, struct got_repository *repo)
569 const struct got_error *err = NULL;
570 SHA1_CTX sha1_ctx;
571 char *header = NULL;
572 char *tag_str = NULL, *tagger_str = NULL;
573 char *id_str = NULL, *obj_str = NULL, *type_str = NULL;
574 size_t headerlen, len = 0, n;
575 FILE *tagfile = NULL;
576 char *msg0 = NULL, *msg;
577 const char *obj_type_str;
578 int obj_type;
580 *id = NULL;
582 SHA1Init(&sha1_ctx);
584 err = got_object_id_str(&id_str, object_id);
585 if (err)
586 goto done;
587 if (asprintf(&obj_str, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str) == -1) {
588 err = got_error_from_errno("asprintf");
589 goto done;
592 err = got_object_get_type(&obj_type, repo, object_id);
593 if (err)
594 goto done;
596 switch (obj_type) {
597 case GOT_OBJ_TYPE_BLOB:
598 obj_type_str = GOT_OBJ_LABEL_BLOB;
599 break;
600 case GOT_OBJ_TYPE_TREE:
601 obj_type_str = GOT_OBJ_LABEL_TREE;
602 break;
603 case GOT_OBJ_TYPE_COMMIT:
604 obj_type_str = GOT_OBJ_LABEL_COMMIT;
605 break;
606 case GOT_OBJ_TYPE_TAG:
607 obj_type_str = GOT_OBJ_LABEL_TAG;
608 break;
609 default:
610 err = got_error(GOT_ERR_OBJ_TYPE);
611 goto done;
614 if (asprintf(&type_str, "%s%s\n", GOT_TAG_LABEL_TYPE,
615 obj_type_str) == -1) {
616 err = got_error_from_errno("asprintf");
617 goto done;
620 if (asprintf(&tag_str, "%s%s\n", GOT_TAG_LABEL_TAG, tag_name) == -1) {
621 err = got_error_from_errno("asprintf");
622 goto done;
625 if (asprintf(&tagger_str, "%s%s %lld +0000\n",
626 GOT_TAG_LABEL_TAGGER, tagger, tagger_time) == -1)
627 return got_error_from_errno("asprintf");
629 msg0 = strdup(tagmsg);
630 if (msg0 == NULL) {
631 err = got_error_from_errno("strdup");
632 goto done;
634 msg = msg0;
636 while (isspace((unsigned char)msg[0]))
637 msg++;
639 len = strlen(obj_str) + strlen(type_str) + strlen(tag_str) +
640 strlen(tagger_str) + 1 + strlen(msg) + 1;
642 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TAG, len) == -1) {
643 err = got_error_from_errno("asprintf");
644 goto done;
647 headerlen = strlen(header) + 1;
648 SHA1Update(&sha1_ctx, header, headerlen);
650 tagfile = got_opentemp();
651 if (tagfile == NULL) {
652 err = got_error_from_errno("got_opentemp");
653 goto done;
656 n = fwrite(header, 1, headerlen, tagfile);
657 if (n != headerlen) {
658 err = got_ferror(tagfile, GOT_ERR_IO);
659 goto done;
661 len = strlen(obj_str);
662 SHA1Update(&sha1_ctx, obj_str, len);
663 n = fwrite(obj_str, 1, len, tagfile);
664 if (n != len) {
665 err = got_ferror(tagfile, GOT_ERR_IO);
666 goto done;
668 len = strlen(type_str);
669 SHA1Update(&sha1_ctx, type_str, len);
670 n = fwrite(type_str, 1, len, tagfile);
671 if (n != len) {
672 err = got_ferror(tagfile, GOT_ERR_IO);
673 goto done;
676 len = strlen(tag_str);
677 SHA1Update(&sha1_ctx, tag_str, len);
678 n = fwrite(tag_str, 1, len, tagfile);
679 if (n != len) {
680 err = got_ferror(tagfile, GOT_ERR_IO);
681 goto done;
684 len = strlen(tagger_str);
685 SHA1Update(&sha1_ctx, tagger_str, len);
686 n = fwrite(tagger_str, 1, len, tagfile);
687 if (n != len) {
688 err = got_ferror(tagfile, GOT_ERR_IO);
689 goto done;
692 SHA1Update(&sha1_ctx, "\n", 1);
693 n = fwrite("\n", 1, 1, tagfile);
694 if (n != 1) {
695 err = got_ferror(tagfile, GOT_ERR_IO);
696 goto done;
699 len = strlen(msg);
700 SHA1Update(&sha1_ctx, msg, len);
701 n = fwrite(msg, 1, len, tagfile);
702 if (n != len) {
703 err = got_ferror(tagfile, GOT_ERR_IO);
704 goto done;
707 SHA1Update(&sha1_ctx, "\n", 1);
708 n = fwrite("\n", 1, 1, tagfile);
709 if (n != 1) {
710 err = got_ferror(tagfile, GOT_ERR_IO);
711 goto done;
714 *id = malloc(sizeof(**id));
715 if (*id == NULL) {
716 err = got_error_from_errno("malloc");
717 goto done;
719 SHA1Final((*id)->sha1, &sha1_ctx);
721 if (fflush(tagfile) != 0) {
722 err = got_error_from_errno("fflush");
723 goto done;
725 rewind(tagfile);
727 err = create_object_file(*id, tagfile, repo);
728 done:
729 free(msg0);
730 free(header);
731 free(obj_str);
732 free(tagger_str);
733 if (tagfile && fclose(tagfile) != 0 && err == NULL)
734 err = got_error_from_errno("fclose");
735 if (err) {
736 free(*id);
737 *id = NULL;
739 return err;