2 * Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/types.h>
19 #include <sys/queue.h>
34 #include "got_error.h"
35 #include "got_object.h"
36 #include "got_repository.h"
37 #include "got_opentemp.h"
41 #include "got_lib_sha1.h"
42 #include "got_lib_deflate.h"
43 #include "got_lib_delta.h"
44 #include "got_lib_object.h"
45 #include "got_lib_object_parse.h"
46 #include "got_lib_lockfile.h"
48 #include "got_lib_object_create.h"
53 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
56 static const struct got_error *
57 create_object_file(struct got_object_id *id, FILE *content,
58 off_t content_len, struct got_repository *repo)
60 const struct got_error *err = NULL, *unlock_err = NULL;
61 char *objpath = NULL, *tmppath = NULL;
63 struct got_lockfile *lf = NULL;
66 err = got_object_get_path(&objpath, id, repo);
70 err = got_opentemp_named(&tmppath, &tmpfile, objpath);
73 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
75 err = got_path_dirname(&parent_path, objpath);
78 err = got_path_mkdir(parent_path);
82 err = got_opentemp_named(&tmppath, &tmpfile, objpath);
87 if (fchmod(fileno(tmpfile), GOT_DEFAULT_FILE_MODE) != 0) {
88 err = got_error_from_errno2("fchmod", tmppath);
92 err = got_deflate_to_file(&tmplen, content, content_len, tmpfile, NULL);
96 err = got_lockfile_lock(&lf, objpath, -1);
100 if (rename(tmppath, objpath) != 0) {
101 err = got_error_from_errno3("rename", tmppath, objpath);
109 if (unlink(tmppath) != 0 && err == NULL)
110 err = got_error_from_errno2("unlink", tmppath);
113 if (tmpfile && fclose(tmpfile) == EOF && err == NULL)
114 err = got_error_from_errno("fclose");
116 unlock_err = got_lockfile_unlock(lf, -1);
117 return err ? err : unlock_err;
120 const struct got_error *
121 got_object_blob_file_create(struct got_object_id **id, FILE **blobfile,
122 off_t *blobsize, const char *ondisk_path)
124 const struct got_error *err = NULL;
129 size_t headerlen = 0, n;
137 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
139 if (!got_err_open_nofollow_on_symlink())
140 return got_error_from_errno2("open", ondisk_path);
142 if (lstat(ondisk_path, &sb) == -1) {
143 err = got_error_from_errno2("lstat", ondisk_path);
146 } else if (fstat(fd, &sb) == -1) {
147 err = got_error_from_errno2("fstat", ondisk_path);
151 if (asprintf(&header, "%s %lld", GOT_OBJ_LABEL_BLOB,
152 (long long)sb.st_size) == -1) {
153 err = got_error_from_errno("asprintf");
156 headerlen = strlen(header) + 1;
157 SHA1Update(&sha1_ctx, header, headerlen);
159 *blobfile = got_opentemp();
160 if (*blobfile == NULL) {
161 err = got_error_from_errno("got_opentemp");
165 n = fwrite(header, 1, headerlen, *blobfile);
166 if (n != headerlen) {
167 err = got_ferror(*blobfile, GOT_ERR_IO);
170 *blobsize += headerlen;
172 char buf[PATH_MAX * 8];
175 if (S_ISLNK(sb.st_mode)) {
176 inlen = readlink(ondisk_path, buf, sizeof(buf));
178 err = got_error_from_errno("readlink");
182 inlen = read(fd, buf, sizeof(buf));
184 err = got_error_from_errno("read");
190 SHA1Update(&sha1_ctx, buf, inlen);
191 n = fwrite(buf, 1, inlen, *blobfile);
193 err = got_ferror(*blobfile, GOT_ERR_IO);
197 if (S_ISLNK(sb.st_mode))
201 *id = malloc(sizeof(**id));
203 err = got_error_from_errno("malloc");
206 SHA1Final((*id)->sha1, &sha1_ctx);
208 if (fflush(*blobfile) != 0) {
209 err = got_error_from_errno("fflush");
215 if (fd != -1 && close(fd) == -1 && err == NULL)
216 err = got_error_from_errno("close");
228 const struct got_error *
229 got_object_blob_create(struct got_object_id **id, const char *ondisk_path,
230 struct got_repository *repo)
232 const struct got_error *err = NULL;
233 FILE *blobfile = NULL;
236 err = got_object_blob_file_create(id, &blobfile, &blobsize,
241 err = create_object_file(*id, blobfile, blobsize, repo);
242 if (fclose(blobfile) == EOF && err == NULL)
243 err = got_error_from_errno("fclose");
251 static const struct got_error *
252 te_mode2str(char *buf, size_t len, struct got_tree_entry *te)
258 * Some Git implementations are picky about modes seen in tree entries.
259 * For best compatibility we normalize the file/directory mode here.
261 if (S_ISREG(te->mode)) {
262 mode = GOT_DEFAULT_FILE_MODE;
263 if (te->mode & (S_IXUSR | S_IXGRP | S_IXOTH))
264 mode |= S_IXUSR | S_IXGRP | S_IXOTH;
265 } else if (got_object_tree_entry_is_submodule(te))
266 mode = S_IFDIR | S_IFLNK;
267 else if (S_ISLNK(te->mode))
268 mode = S_IFLNK; /* Git leaves all the other bits unset. */
269 else if (S_ISDIR(te->mode))
270 mode = S_IFDIR; /* Git leaves all the other bits unset. */
272 return got_error(GOT_ERR_BAD_FILETYPE);
274 ret = snprintf(buf, len, "%o ", mode);
275 if (ret < 0 || (size_t)ret >= len)
276 return got_error(GOT_ERR_NO_SPACE);
281 * Git expects directory tree entries to be sorted with an imaginary slash
282 * appended to their name, and will break otherwise. Let's be nice.
283 * This function is intended to be used with mergesort(3) to sort an
284 * array of pointers to struct got_tree_entry objects.
287 sort_tree_entries_the_way_git_likes_it(const void *arg1, const void *arg2)
289 struct got_tree_entry * const *te1 = arg1;
290 struct got_tree_entry * const *te2 = arg2;
291 char name1[NAME_MAX + 2];
292 char name2[NAME_MAX + 2];
294 strlcpy(name1, (*te1)->name, sizeof(name1));
295 strlcpy(name2, (*te2)->name, sizeof(name2));
296 if (S_ISDIR((*te1)->mode))
297 strlcat(name1, "/", sizeof(name1));
298 if (S_ISDIR((*te2)->mode))
299 strlcat(name2, "/", sizeof(name2));
300 return strcmp(name1, name2);
303 const struct got_error *
304 got_object_tree_create(struct got_object_id **id,
305 struct got_pathlist_head *paths, int nentries, struct got_repository *repo)
307 const struct got_error *err = NULL;
308 char modebuf[sizeof("100644 ")];
311 size_t headerlen, len = 0, n;
312 FILE *treefile = NULL;
314 struct got_pathlist_entry *pe;
315 struct got_tree_entry **sorted_entries;
316 struct got_tree_entry *te;
323 sorted_entries = calloc(nentries, sizeof(struct got_tree_entry *));
324 if (sorted_entries == NULL)
325 return got_error_from_errno("calloc");
328 TAILQ_FOREACH(pe, paths, entry)
329 sorted_entries[i++] = pe->data;
330 mergesort(sorted_entries, nentries, sizeof(struct got_tree_entry *),
331 sort_tree_entries_the_way_git_likes_it);
333 for (i = 0; i < nentries; i++) {
334 te = sorted_entries[i];
335 err = te_mode2str(modebuf, sizeof(modebuf), te);
338 len += strlen(modebuf) + strlen(te->name) + 1 +
342 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TREE, len) == -1) {
343 err = got_error_from_errno("asprintf");
346 headerlen = strlen(header) + 1;
347 SHA1Update(&sha1_ctx, header, headerlen);
349 treefile = got_opentemp();
350 if (treefile == NULL) {
351 err = got_error_from_errno("got_opentemp");
355 n = fwrite(header, 1, headerlen, treefile);
356 if (n != headerlen) {
357 err = got_ferror(treefile, GOT_ERR_IO);
360 treesize += headerlen;
362 for (i = 0; i < nentries; i++) {
363 te = sorted_entries[i];
364 err = te_mode2str(modebuf, sizeof(modebuf), te);
367 len = strlen(modebuf);
368 n = fwrite(modebuf, 1, len, treefile);
370 err = got_ferror(treefile, GOT_ERR_IO);
373 SHA1Update(&sha1_ctx, modebuf, len);
376 len = strlen(te->name) + 1; /* must include NUL */
377 n = fwrite(te->name, 1, len, treefile);
379 err = got_ferror(treefile, GOT_ERR_IO);
382 SHA1Update(&sha1_ctx, te->name, len);
385 len = SHA1_DIGEST_LENGTH;
386 n = fwrite(te->id.sha1, 1, len, treefile);
388 err = got_ferror(treefile, GOT_ERR_IO);
391 SHA1Update(&sha1_ctx, te->id.sha1, len);
395 *id = malloc(sizeof(**id));
397 err = got_error_from_errno("malloc");
400 SHA1Final((*id)->sha1, &sha1_ctx);
402 if (fflush(treefile) != 0) {
403 err = got_error_from_errno("fflush");
408 err = create_object_file(*id, treefile, treesize, repo);
411 free(sorted_entries);
412 if (treefile && fclose(treefile) == EOF && err == NULL)
413 err = got_error_from_errno("fclose");
421 const struct got_error *
422 got_object_commit_create(struct got_object_id **id,
423 struct got_object_id *tree_id, struct got_object_id_queue *parent_ids,
424 int nparents, const char *author, time_t author_time,
425 const char *committer, time_t committer_time,
426 const char *logmsg, struct got_repository *repo)
428 const struct got_error *err = NULL;
430 char *header = NULL, *tree_str = NULL;
431 char *author_str = NULL, *committer_str = NULL;
433 size_t headerlen, len = 0, n;
434 FILE *commitfile = NULL;
435 off_t commitsize = 0;
436 struct got_object_qid *qid;
443 msg0 = strdup(logmsg);
445 return got_error_from_errno("strdup");
448 while (isspace((unsigned char)msg[0]))
451 while (len > 0 && isspace((unsigned char)msg[len - 1])) {
456 if (asprintf(&author_str, "%s%s %lld +0000\n",
457 GOT_COMMIT_LABEL_AUTHOR, author, (long long)author_time) == -1) {
458 err = got_error_from_errno("asprintf");
462 if (asprintf(&committer_str, "%s%s %lld +0000\n",
463 GOT_COMMIT_LABEL_COMMITTER, committer ? committer : author,
464 (long long)(committer ? committer_time : author_time))
466 err = got_error_from_errno("asprintf");
470 len = strlen(GOT_COMMIT_LABEL_TREE) + SHA1_DIGEST_STRING_LENGTH +
472 (strlen(GOT_COMMIT_LABEL_PARENT) + SHA1_DIGEST_STRING_LENGTH) +
473 + strlen(author_str) + strlen(committer_str) + 2 + strlen(msg);
475 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_COMMIT, len) == -1) {
476 err = got_error_from_errno("asprintf");
479 headerlen = strlen(header) + 1;
480 SHA1Update(&sha1_ctx, header, headerlen);
482 commitfile = got_opentemp();
483 if (commitfile == NULL) {
484 err = got_error_from_errno("got_opentemp");
488 n = fwrite(header, 1, headerlen, commitfile);
489 if (n != headerlen) {
490 err = got_ferror(commitfile, GOT_ERR_IO);
493 commitsize += headerlen;
495 err = got_object_id_str(&id_str, tree_id);
498 if (asprintf(&tree_str, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str)
500 err = got_error_from_errno("asprintf");
503 len = strlen(tree_str);
504 SHA1Update(&sha1_ctx, tree_str, len);
505 n = fwrite(tree_str, 1, len, commitfile);
507 err = got_ferror(commitfile, GOT_ERR_IO);
515 STAILQ_FOREACH(qid, parent_ids, entry) {
516 char *parent_str = NULL;
518 err = got_object_id_str(&id_str, &qid->id);
521 if (asprintf(&parent_str, "%s%s\n",
522 GOT_COMMIT_LABEL_PARENT, id_str) == -1) {
523 err = got_error_from_errno("asprintf");
526 len = strlen(parent_str);
527 SHA1Update(&sha1_ctx, parent_str, len);
528 n = fwrite(parent_str, 1, len, commitfile);
530 err = got_ferror(commitfile, GOT_ERR_IO);
541 len = strlen(author_str);
542 SHA1Update(&sha1_ctx, author_str, len);
543 n = fwrite(author_str, 1, len, commitfile);
545 err = got_ferror(commitfile, GOT_ERR_IO);
550 len = strlen(committer_str);
551 SHA1Update(&sha1_ctx, committer_str, len);
552 n = fwrite(committer_str, 1, len, commitfile);
554 err = got_ferror(commitfile, GOT_ERR_IO);
559 SHA1Update(&sha1_ctx, "\n", 1);
560 n = fwrite("\n", 1, 1, commitfile);
562 err = got_ferror(commitfile, GOT_ERR_IO);
568 SHA1Update(&sha1_ctx, msg, len);
569 n = fwrite(msg, 1, len, commitfile);
571 err = got_ferror(commitfile, GOT_ERR_IO);
576 SHA1Update(&sha1_ctx, "\n", 1);
577 n = fwrite("\n", 1, 1, commitfile);
579 err = got_ferror(commitfile, GOT_ERR_IO);
584 *id = malloc(sizeof(**id));
586 err = got_error_from_errno("malloc");
589 SHA1Final((*id)->sha1, &sha1_ctx);
591 if (fflush(commitfile) != 0) {
592 err = got_error_from_errno("fflush");
597 err = create_object_file(*id, commitfile, commitsize, repo);
605 if (commitfile && fclose(commitfile) == EOF && err == NULL)
606 err = got_error_from_errno("fclose");
614 const struct got_error *
615 got_object_tag_create(struct got_object_id **id,
616 const char *tag_name, struct got_object_id *object_id, const char *tagger,
617 time_t tagger_time, const char *tagmsg, const char *signer_id,
618 struct got_repository *repo, int verbosity)
620 const struct got_error *err = NULL;
623 char *tag_str = NULL, *tagger_str = NULL;
624 char *id_str = NULL, *obj_str = NULL, *type_str = NULL;
625 size_t headerlen, len = 0, sig_len = 0, n;
626 FILE *tagfile = NULL;
628 char *msg0 = NULL, *msg;
629 const char *obj_type_str;
637 err = got_object_id_str(&id_str, object_id);
640 if (asprintf(&obj_str, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str) == -1) {
641 err = got_error_from_errno("asprintf");
645 err = got_object_get_type(&obj_type, repo, object_id);
650 case GOT_OBJ_TYPE_BLOB:
651 obj_type_str = GOT_OBJ_LABEL_BLOB;
653 case GOT_OBJ_TYPE_TREE:
654 obj_type_str = GOT_OBJ_LABEL_TREE;
656 case GOT_OBJ_TYPE_COMMIT:
657 obj_type_str = GOT_OBJ_LABEL_COMMIT;
659 case GOT_OBJ_TYPE_TAG:
660 obj_type_str = GOT_OBJ_LABEL_TAG;
663 err = got_error(GOT_ERR_OBJ_TYPE);
667 if (asprintf(&type_str, "%s%s\n", GOT_TAG_LABEL_TYPE,
668 obj_type_str) == -1) {
669 err = got_error_from_errno("asprintf");
673 if (asprintf(&tag_str, "%s%s\n", GOT_TAG_LABEL_TAG, tag_name) == -1) {
674 err = got_error_from_errno("asprintf");
678 if (asprintf(&tagger_str, "%s%s %lld +0000\n",
679 GOT_TAG_LABEL_TAGGER, tagger, (long long)tagger_time) == -1)
680 return got_error_from_errno("asprintf");
682 msg0 = strdup(tagmsg);
684 err = got_error_from_errno("strdup");
689 while (isspace((unsigned char)msg[0]))
698 err = buf_alloc(&buf, 0);
703 err = buf_puts(&len, buf, obj_str);
706 err = buf_puts(&len, buf, type_str);
709 err = buf_puts(&len, buf, tag_str);
712 err = buf_puts(&len, buf, tagger_str);
715 err = buf_putc(buf, '\n');
718 err = buf_puts(&len, buf, msg);
721 err = buf_putc(buf, '\n');
725 err = got_sigs_sign_tag_ssh(&pid, &in_fd, &out_fd, signer_id,
729 if (buf_write_fd(buf, in_fd) == -1) {
730 err = got_error_from_errno("write");
733 if (close(in_fd) == -1) {
734 err = got_error_from_errno("close");
738 if (waitpid(pid, &status, 0) == -1) {
739 err = got_error_from_errno("waitpid");
742 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
743 err = got_error(GOT_ERR_SIGNING_TAG);
748 err = buf_load_fd(&buf, out_fd);
751 sig_len = buf_len(buf);
752 if (close(out_fd) == -1) {
753 err = got_error_from_errno("close");
758 len = strlen(obj_str) + strlen(type_str) + strlen(tag_str) +
759 strlen(tagger_str) + 1 + strlen(msg) + 1 + sig_len;
760 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TAG, len) == -1) {
761 err = got_error_from_errno("asprintf");
765 headerlen = strlen(header) + 1;
766 SHA1Update(&sha1_ctx, header, headerlen);
768 tagfile = got_opentemp();
769 if (tagfile == NULL) {
770 err = got_error_from_errno("got_opentemp");
774 n = fwrite(header, 1, headerlen, tagfile);
775 if (n != headerlen) {
776 err = got_ferror(tagfile, GOT_ERR_IO);
779 tagsize += headerlen;
780 len = strlen(obj_str);
781 SHA1Update(&sha1_ctx, obj_str, len);
782 n = fwrite(obj_str, 1, len, tagfile);
784 err = got_ferror(tagfile, GOT_ERR_IO);
788 len = strlen(type_str);
789 SHA1Update(&sha1_ctx, type_str, len);
790 n = fwrite(type_str, 1, len, tagfile);
792 err = got_ferror(tagfile, GOT_ERR_IO);
797 len = strlen(tag_str);
798 SHA1Update(&sha1_ctx, tag_str, len);
799 n = fwrite(tag_str, 1, len, tagfile);
801 err = got_ferror(tagfile, GOT_ERR_IO);
806 len = strlen(tagger_str);
807 SHA1Update(&sha1_ctx, tagger_str, len);
808 n = fwrite(tagger_str, 1, len, tagfile);
810 err = got_ferror(tagfile, GOT_ERR_IO);
815 SHA1Update(&sha1_ctx, "\n", 1);
816 n = fwrite("\n", 1, 1, tagfile);
818 err = got_ferror(tagfile, GOT_ERR_IO);
824 SHA1Update(&sha1_ctx, msg, len);
825 n = fwrite(msg, 1, len, tagfile);
827 err = got_ferror(tagfile, GOT_ERR_IO);
832 SHA1Update(&sha1_ctx, "\n", 1);
833 n = fwrite("\n", 1, 1, tagfile);
835 err = got_ferror(tagfile, GOT_ERR_IO);
840 if (signer_id && buf_len(buf) > 0) {
842 SHA1Update(&sha1_ctx, buf_get(buf), len);
843 n = fwrite(buf_get(buf), 1, len, tagfile);
845 err = got_ferror(tagfile, GOT_ERR_IO);
851 *id = malloc(sizeof(**id));
853 err = got_error_from_errno("malloc");
856 SHA1Final((*id)->sha1, &sha1_ctx);
858 if (fflush(tagfile) != 0) {
859 err = got_error_from_errno("fflush");
864 err = create_object_file(*id, tagfile, tagsize, repo);
872 if (tagfile && fclose(tagfile) == EOF && err == NULL)
873 err = got_error_from_errno("fclose");