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>
33 #include "got_error.h"
34 #include "got_object.h"
35 #include "got_repository.h"
36 #include "got_opentemp.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 #include "got_lib_object_create.h"
49 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
52 static const struct got_error *
53 create_object_file(struct got_object_id *id, FILE *content,
54 off_t content_len, struct got_repository *repo)
56 const struct got_error *err = NULL, *unlock_err = NULL;
57 char *objpath = NULL, *tmppath = NULL;
59 struct got_lockfile *lf = NULL;
62 err = got_object_get_path(&objpath, id, repo);
66 err = got_opentemp_named(&tmppath, &tmpfile, objpath);
69 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
71 err = got_path_dirname(&parent_path, objpath);
74 err = got_path_mkdir(parent_path);
78 err = got_opentemp_named(&tmppath, &tmpfile, objpath);
83 if (fchmod(fileno(tmpfile), GOT_DEFAULT_FILE_MODE) != 0) {
84 err = got_error_from_errno2("fchmod", tmppath);
88 err = got_deflate_to_file(&tmplen, content, content_len, tmpfile, NULL);
92 err = got_lockfile_lock(&lf, objpath, -1);
96 if (rename(tmppath, objpath) != 0) {
97 err = got_error_from_errno3("rename", tmppath, objpath);
105 if (unlink(tmppath) != 0 && err == NULL)
106 err = got_error_from_errno2("unlink", tmppath);
109 if (tmpfile && fclose(tmpfile) == EOF && err == NULL)
110 err = got_error_from_errno("fclose");
112 unlock_err = got_lockfile_unlock(lf, -1);
113 return err ? err : unlock_err;
116 const struct got_error *
117 got_object_blob_file_create(struct got_object_id **id, FILE **blobfile,
118 off_t *blobsize, const char *ondisk_path)
120 const struct got_error *err = NULL;
125 size_t headerlen = 0, n;
133 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
135 if (!got_err_open_nofollow_on_symlink())
136 return got_error_from_errno2("open", ondisk_path);
138 if (lstat(ondisk_path, &sb) == -1) {
139 err = got_error_from_errno2("lstat", ondisk_path);
142 } else if (fstat(fd, &sb) == -1) {
143 err = got_error_from_errno2("fstat", ondisk_path);
147 if (asprintf(&header, "%s %lld", GOT_OBJ_LABEL_BLOB,
148 (long long)sb.st_size) == -1) {
149 err = got_error_from_errno("asprintf");
152 headerlen = strlen(header) + 1;
153 SHA1Update(&sha1_ctx, header, headerlen);
155 *blobfile = got_opentemp();
156 if (*blobfile == NULL) {
157 err = got_error_from_errno("got_opentemp");
161 n = fwrite(header, 1, headerlen, *blobfile);
162 if (n != headerlen) {
163 err = got_ferror(*blobfile, GOT_ERR_IO);
166 *blobsize += headerlen;
168 char buf[PATH_MAX * 8];
171 if (S_ISLNK(sb.st_mode)) {
172 inlen = readlink(ondisk_path, buf, sizeof(buf));
174 err = got_error_from_errno("readlink");
178 inlen = read(fd, buf, sizeof(buf));
180 err = got_error_from_errno("read");
186 SHA1Update(&sha1_ctx, buf, inlen);
187 n = fwrite(buf, 1, inlen, *blobfile);
189 err = got_ferror(*blobfile, GOT_ERR_IO);
193 if (S_ISLNK(sb.st_mode))
197 *id = malloc(sizeof(**id));
199 err = got_error_from_errno("malloc");
202 SHA1Final((*id)->sha1, &sha1_ctx);
204 if (fflush(*blobfile) != 0) {
205 err = got_error_from_errno("fflush");
211 if (fd != -1 && close(fd) == -1 && err == NULL)
212 err = got_error_from_errno("close");
224 const struct got_error *
225 got_object_blob_create(struct got_object_id **id, const char *ondisk_path,
226 struct got_repository *repo)
228 const struct got_error *err = NULL;
229 FILE *blobfile = NULL;
232 err = got_object_blob_file_create(id, &blobfile, &blobsize,
237 err = create_object_file(*id, blobfile, blobsize, repo);
238 if (fclose(blobfile) == EOF && err == NULL)
239 err = got_error_from_errno("fclose");
247 static const struct got_error *
248 te_mode2str(char *buf, size_t len, struct got_tree_entry *te)
254 * Some Git implementations are picky about modes seen in tree entries.
255 * For best compatibility we normalize the file/directory mode here.
257 if (S_ISREG(te->mode)) {
258 mode = GOT_DEFAULT_FILE_MODE;
259 if (te->mode & (S_IXUSR | S_IXGRP | S_IXOTH))
260 mode |= S_IXUSR | S_IXGRP | S_IXOTH;
261 } else if (got_object_tree_entry_is_submodule(te))
262 mode = S_IFDIR | S_IFLNK;
263 else if (S_ISLNK(te->mode))
264 mode = S_IFLNK; /* Git leaves all the other bits unset. */
265 else if (S_ISDIR(te->mode))
266 mode = S_IFDIR; /* Git leaves all the other bits unset. */
268 return got_error(GOT_ERR_BAD_FILETYPE);
270 ret = snprintf(buf, len, "%o ", mode);
271 if (ret == -1 || ret >= len)
272 return got_error(GOT_ERR_NO_SPACE);
277 * Git expects directory tree entries to be sorted with an imaginary slash
278 * appended to their name, and will break otherwise. Let's be nice.
279 * This function is intended to be used with mergesort(3) to sort an
280 * array of pointers to struct got_tree_entry objects.
283 sort_tree_entries_the_way_git_likes_it(const void *arg1, const void *arg2)
285 struct got_tree_entry * const *te1 = arg1;
286 struct got_tree_entry * const *te2 = arg2;
287 char name1[NAME_MAX + 2];
288 char name2[NAME_MAX + 2];
290 strlcpy(name1, (*te1)->name, sizeof(name1));
291 strlcpy(name2, (*te2)->name, sizeof(name2));
292 if (S_ISDIR((*te1)->mode))
293 strlcat(name1, "/", sizeof(name1));
294 if (S_ISDIR((*te2)->mode))
295 strlcat(name2, "/", sizeof(name2));
296 return strcmp(name1, name2);
299 const struct got_error *
300 got_object_tree_create(struct got_object_id **id,
301 struct got_pathlist_head *paths, int nentries, struct got_repository *repo)
303 const struct got_error *err = NULL;
304 char modebuf[sizeof("100644 ")];
307 size_t headerlen, len = 0, n;
308 FILE *treefile = NULL;
310 struct got_pathlist_entry *pe;
311 struct got_tree_entry **sorted_entries;
312 struct got_tree_entry *te;
319 sorted_entries = calloc(nentries, sizeof(struct got_tree_entry *));
320 if (sorted_entries == NULL)
321 return got_error_from_errno("calloc");
324 TAILQ_FOREACH(pe, paths, entry)
325 sorted_entries[i++] = pe->data;
326 mergesort(sorted_entries, nentries, sizeof(struct got_tree_entry *),
327 sort_tree_entries_the_way_git_likes_it);
329 for (i = 0; i < nentries; i++) {
330 te = sorted_entries[i];
331 err = te_mode2str(modebuf, sizeof(modebuf), te);
334 len += strlen(modebuf) + strlen(te->name) + 1 +
338 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TREE, len) == -1) {
339 err = got_error_from_errno("asprintf");
342 headerlen = strlen(header) + 1;
343 SHA1Update(&sha1_ctx, header, headerlen);
345 treefile = got_opentemp();
346 if (treefile == NULL) {
347 err = got_error_from_errno("got_opentemp");
351 n = fwrite(header, 1, headerlen, treefile);
352 if (n != headerlen) {
353 err = got_ferror(treefile, GOT_ERR_IO);
356 treesize += headerlen;
358 for (i = 0; i < nentries; i++) {
359 te = sorted_entries[i];
360 err = te_mode2str(modebuf, sizeof(modebuf), te);
363 len = strlen(modebuf);
364 n = fwrite(modebuf, 1, len, treefile);
366 err = got_ferror(treefile, GOT_ERR_IO);
369 SHA1Update(&sha1_ctx, modebuf, len);
372 len = strlen(te->name) + 1; /* must include NUL */
373 n = fwrite(te->name, 1, len, treefile);
375 err = got_ferror(treefile, GOT_ERR_IO);
378 SHA1Update(&sha1_ctx, te->name, len);
381 len = SHA1_DIGEST_LENGTH;
382 n = fwrite(te->id.sha1, 1, len, treefile);
384 err = got_ferror(treefile, GOT_ERR_IO);
387 SHA1Update(&sha1_ctx, te->id.sha1, len);
391 *id = malloc(sizeof(**id));
393 err = got_error_from_errno("malloc");
396 SHA1Final((*id)->sha1, &sha1_ctx);
398 if (fflush(treefile) != 0) {
399 err = got_error_from_errno("fflush");
404 err = create_object_file(*id, treefile, treesize, repo);
407 free(sorted_entries);
408 if (treefile && fclose(treefile) == EOF && err == NULL)
409 err = got_error_from_errno("fclose");
417 const struct got_error *
418 got_object_commit_create(struct got_object_id **id,
419 struct got_object_id *tree_id, struct got_object_id_queue *parent_ids,
420 int nparents, const char *author, time_t author_time,
421 const char *committer, time_t committer_time,
422 const char *logmsg, struct got_repository *repo)
424 const struct got_error *err = NULL;
426 char *header = NULL, *tree_str = NULL;
427 char *author_str = NULL, *committer_str = NULL;
429 size_t headerlen, len = 0, n;
430 FILE *commitfile = NULL;
431 off_t commitsize = 0;
432 struct got_object_qid *qid;
439 msg0 = strdup(logmsg);
441 return got_error_from_errno("strdup");
444 while (isspace((unsigned char)msg[0]))
447 while (len > 0 && isspace((unsigned char)msg[len - 1])) {
452 if (asprintf(&author_str, "%s%s %lld +0000\n",
453 GOT_COMMIT_LABEL_AUTHOR, author, (long long)author_time) == -1)
454 return got_error_from_errno("asprintf");
456 if (asprintf(&committer_str, "%s%s %lld +0000\n",
457 GOT_COMMIT_LABEL_COMMITTER, committer ? committer : author,
458 (long long)(committer ? committer_time : author_time))
460 err = got_error_from_errno("asprintf");
464 len = strlen(GOT_COMMIT_LABEL_TREE) + SHA1_DIGEST_STRING_LENGTH +
466 (strlen(GOT_COMMIT_LABEL_PARENT) + SHA1_DIGEST_STRING_LENGTH) +
467 + strlen(author_str) + strlen(committer_str) + 2 + strlen(msg);
469 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_COMMIT, len) == -1) {
470 err = got_error_from_errno("asprintf");
473 headerlen = strlen(header) + 1;
474 SHA1Update(&sha1_ctx, header, headerlen);
476 commitfile = got_opentemp();
477 if (commitfile == NULL) {
478 err = got_error_from_errno("got_opentemp");
482 n = fwrite(header, 1, headerlen, commitfile);
483 if (n != headerlen) {
484 err = got_ferror(commitfile, GOT_ERR_IO);
487 commitsize += headerlen;
489 err = got_object_id_str(&id_str, tree_id);
492 if (asprintf(&tree_str, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str)
494 err = got_error_from_errno("asprintf");
497 len = strlen(tree_str);
498 SHA1Update(&sha1_ctx, tree_str, len);
499 n = fwrite(tree_str, 1, len, commitfile);
501 err = got_ferror(commitfile, GOT_ERR_IO);
509 STAILQ_FOREACH(qid, parent_ids, entry) {
510 char *parent_str = NULL;
512 err = got_object_id_str(&id_str, &qid->id);
515 if (asprintf(&parent_str, "%s%s\n",
516 GOT_COMMIT_LABEL_PARENT, id_str) == -1) {
517 err = got_error_from_errno("asprintf");
520 len = strlen(parent_str);
521 SHA1Update(&sha1_ctx, parent_str, len);
522 n = fwrite(parent_str, 1, len, commitfile);
524 err = got_ferror(commitfile, GOT_ERR_IO);
535 len = strlen(author_str);
536 SHA1Update(&sha1_ctx, author_str, len);
537 n = fwrite(author_str, 1, len, commitfile);
539 err = got_ferror(commitfile, GOT_ERR_IO);
544 len = strlen(committer_str);
545 SHA1Update(&sha1_ctx, committer_str, len);
546 n = fwrite(committer_str, 1, len, commitfile);
548 err = got_ferror(commitfile, GOT_ERR_IO);
553 SHA1Update(&sha1_ctx, "\n", 1);
554 n = fwrite("\n", 1, 1, commitfile);
556 err = got_ferror(commitfile, GOT_ERR_IO);
562 SHA1Update(&sha1_ctx, msg, len);
563 n = fwrite(msg, 1, len, commitfile);
565 err = got_ferror(commitfile, GOT_ERR_IO);
570 SHA1Update(&sha1_ctx, "\n", 1);
571 n = fwrite("\n", 1, 1, commitfile);
573 err = got_ferror(commitfile, GOT_ERR_IO);
578 *id = malloc(sizeof(**id));
580 err = got_error_from_errno("malloc");
583 SHA1Final((*id)->sha1, &sha1_ctx);
585 if (fflush(commitfile) != 0) {
586 err = got_error_from_errno("fflush");
591 err = create_object_file(*id, commitfile, commitsize, repo);
599 if (commitfile && fclose(commitfile) == EOF && err == NULL)
600 err = got_error_from_errno("fclose");
608 const struct got_error *
609 got_object_tag_create(struct got_object_id **id,
610 const char *tag_name, struct got_object_id *object_id, const char *tagger,
611 time_t tagger_time, const char *tagmsg, struct got_repository *repo)
613 const struct got_error *err = NULL;
616 char *tag_str = NULL, *tagger_str = NULL;
617 char *id_str = NULL, *obj_str = NULL, *type_str = NULL;
618 size_t headerlen, len = 0, n;
619 FILE *tagfile = NULL;
621 char *msg0 = NULL, *msg;
622 const char *obj_type_str;
629 err = got_object_id_str(&id_str, object_id);
632 if (asprintf(&obj_str, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str) == -1) {
633 err = got_error_from_errno("asprintf");
637 err = got_object_get_type(&obj_type, repo, object_id);
642 case GOT_OBJ_TYPE_BLOB:
643 obj_type_str = GOT_OBJ_LABEL_BLOB;
645 case GOT_OBJ_TYPE_TREE:
646 obj_type_str = GOT_OBJ_LABEL_TREE;
648 case GOT_OBJ_TYPE_COMMIT:
649 obj_type_str = GOT_OBJ_LABEL_COMMIT;
651 case GOT_OBJ_TYPE_TAG:
652 obj_type_str = GOT_OBJ_LABEL_TAG;
655 err = got_error(GOT_ERR_OBJ_TYPE);
659 if (asprintf(&type_str, "%s%s\n", GOT_TAG_LABEL_TYPE,
660 obj_type_str) == -1) {
661 err = got_error_from_errno("asprintf");
665 if (asprintf(&tag_str, "%s%s\n", GOT_TAG_LABEL_TAG, tag_name) == -1) {
666 err = got_error_from_errno("asprintf");
670 if (asprintf(&tagger_str, "%s%s %lld +0000\n",
671 GOT_TAG_LABEL_TAGGER, tagger, (long long)tagger_time) == -1)
672 return got_error_from_errno("asprintf");
674 msg0 = strdup(tagmsg);
676 err = got_error_from_errno("strdup");
681 while (isspace((unsigned char)msg[0]))
684 len = strlen(obj_str) + strlen(type_str) + strlen(tag_str) +
685 strlen(tagger_str) + 1 + strlen(msg) + 1;
687 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TAG, len) == -1) {
688 err = got_error_from_errno("asprintf");
692 headerlen = strlen(header) + 1;
693 SHA1Update(&sha1_ctx, header, headerlen);
695 tagfile = got_opentemp();
696 if (tagfile == NULL) {
697 err = got_error_from_errno("got_opentemp");
701 n = fwrite(header, 1, headerlen, tagfile);
702 if (n != headerlen) {
703 err = got_ferror(tagfile, GOT_ERR_IO);
706 tagsize += headerlen;
707 len = strlen(obj_str);
708 SHA1Update(&sha1_ctx, obj_str, len);
709 n = fwrite(obj_str, 1, len, tagfile);
711 err = got_ferror(tagfile, GOT_ERR_IO);
715 len = strlen(type_str);
716 SHA1Update(&sha1_ctx, type_str, len);
717 n = fwrite(type_str, 1, len, tagfile);
719 err = got_ferror(tagfile, GOT_ERR_IO);
724 len = strlen(tag_str);
725 SHA1Update(&sha1_ctx, tag_str, len);
726 n = fwrite(tag_str, 1, len, tagfile);
728 err = got_ferror(tagfile, GOT_ERR_IO);
733 len = strlen(tagger_str);
734 SHA1Update(&sha1_ctx, tagger_str, len);
735 n = fwrite(tagger_str, 1, len, tagfile);
737 err = got_ferror(tagfile, GOT_ERR_IO);
742 SHA1Update(&sha1_ctx, "\n", 1);
743 n = fwrite("\n", 1, 1, tagfile);
745 err = got_ferror(tagfile, GOT_ERR_IO);
751 SHA1Update(&sha1_ctx, msg, len);
752 n = fwrite(msg, 1, len, tagfile);
754 err = got_ferror(tagfile, GOT_ERR_IO);
759 SHA1Update(&sha1_ctx, "\n", 1);
760 n = fwrite("\n", 1, 1, tagfile);
762 err = got_ferror(tagfile, GOT_ERR_IO);
767 *id = malloc(sizeof(**id));
769 err = got_error_from_errno("malloc");
772 SHA1Final((*id)->sha1, &sha1_ctx);
774 if (fflush(tagfile) != 0) {
775 err = got_error_from_errno("fflush");
780 err = create_object_file(*id, tagfile, tagsize, repo);
786 if (tagfile && fclose(tagfile) == EOF && err == NULL)
787 err = got_error_from_errno("fclose");