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>
32 #include "got_compat.h"
34 #include "got_error.h"
35 #include "got_object.h"
36 #include "got_repository.h"
37 #include "got_opentemp.h"
40 #include "got_lib_sha1.h"
41 #include "got_lib_deflate.h"
42 #include "got_lib_delta.h"
43 #include "got_lib_object.h"
44 #include "got_lib_object_parse.h"
45 #include "got_lib_lockfile.h"
48 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
51 static const struct got_error *
52 create_object_file(struct got_object_id *id, FILE *content,
53 struct got_repository *repo)
55 const struct got_error *err = NULL, *unlock_err = NULL;
56 char *objpath = NULL, *tmppath = NULL;
58 struct got_lockfile *lf = NULL;
61 err = got_object_get_path(&objpath, id, repo);
65 err = got_opentemp_named(&tmppath, &tmpfile, objpath);
68 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
70 err = got_path_dirname(&parent_path, objpath);
73 err = got_path_mkdir(parent_path);
77 err = got_opentemp_named(&tmppath, &tmpfile, objpath);
82 if (fchmod(fileno(tmpfile), GOT_DEFAULT_FILE_MODE) != 0) {
83 err = got_error_from_errno2("fchmod", tmppath);
87 err = got_deflate_to_file(&tmplen, content, tmpfile, NULL);
91 err = got_lockfile_lock(&lf, objpath, -1);
95 if (rename(tmppath, objpath) != 0) {
96 err = got_error_from_errno3("rename", tmppath, objpath);
104 if (unlink(tmppath) != 0 && err == NULL)
105 err = got_error_from_errno2("unlink", tmppath);
108 if (tmpfile && fclose(tmpfile) == EOF && err == NULL)
109 err = got_error_from_errno("fclose");
111 unlock_err = got_lockfile_unlock(lf, -1);
112 return err ? err : unlock_err;
115 const struct got_error *
116 got_object_blob_file_create(struct got_object_id **id, FILE **blobfile,
117 const char *ondisk_path)
119 const struct got_error *err = NULL;
124 size_t headerlen = 0, n;
131 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
134 return got_error_from_errno2("open", ondisk_path);
136 if (lstat(ondisk_path, &sb) == -1) {
137 err = got_error_from_errno2("lstat", ondisk_path);
140 } else if (fstat(fd, &sb) == -1) {
141 err = got_error_from_errno2("fstat", ondisk_path);
145 if (asprintf(&header, "%s %lld", GOT_OBJ_LABEL_BLOB,
146 (long long)sb.st_size) == -1) {
147 err = got_error_from_errno("asprintf");
150 headerlen = strlen(header) + 1;
151 SHA1Update(&sha1_ctx, header, headerlen);
153 *blobfile = got_opentemp();
154 if (*blobfile == NULL) {
155 err = got_error_from_errno("got_opentemp");
159 n = fwrite(header, 1, headerlen, *blobfile);
160 if (n != headerlen) {
161 err = got_ferror(*blobfile, GOT_ERR_IO);
165 char buf[PATH_MAX * 8];
168 if (S_ISLNK(sb.st_mode)) {
169 inlen = readlink(ondisk_path, buf, sizeof(buf));
171 err = got_error_from_errno("readlink");
175 inlen = read(fd, buf, sizeof(buf));
177 err = got_error_from_errno("read");
183 SHA1Update(&sha1_ctx, buf, inlen);
184 n = fwrite(buf, 1, inlen, *blobfile);
186 err = got_ferror(*blobfile, GOT_ERR_IO);
189 if (S_ISLNK(sb.st_mode))
193 *id = malloc(sizeof(**id));
195 err = got_error_from_errno("malloc");
198 SHA1Final((*id)->sha1, &sha1_ctx);
200 if (fflush(*blobfile) != 0) {
201 err = got_error_from_errno("fflush");
207 if (fd != -1 && close(fd) == -1 && err == NULL)
208 err = got_error_from_errno("close");
220 const struct got_error *
221 got_object_blob_create(struct got_object_id **id, const char *ondisk_path,
222 struct got_repository *repo)
224 const struct got_error *err = NULL;
225 FILE *blobfile = NULL;
227 err = got_object_blob_file_create(id, &blobfile, ondisk_path);
231 err = create_object_file(*id, blobfile, repo);
232 if (fclose(blobfile) == EOF && err == NULL)
233 err = got_error_from_errno("fclose");
241 static const struct got_error *
242 te_mode2str(char *buf, size_t len, struct got_tree_entry *te)
248 * Some Git implementations are picky about modes seen in tree entries.
249 * For best compatibility we normalize the file/directory mode here.
250 * Note that we do not support committing symlinks.
252 if (S_ISREG(te->mode)) {
253 mode = GOT_DEFAULT_FILE_MODE;
254 if (te->mode & (S_IXUSR | S_IXGRP | S_IXOTH))
255 mode |= S_IXUSR | S_IXGRP | S_IXOTH;
256 } else if (got_object_tree_entry_is_submodule(te))
257 mode = S_IFDIR | S_IFLNK;
258 else if (S_ISLNK(te->mode))
259 mode = S_IFLNK; /* Git leaves all the other bits unset. */
260 else if (S_ISDIR(te->mode))
261 mode = S_IFDIR; /* Git leaves all the other bits unset. */
263 return got_error(GOT_ERR_BAD_FILETYPE);
265 ret = snprintf(buf, len, "%o ", mode);
266 if (ret == -1 || ret >= len)
267 return got_error(GOT_ERR_NO_SPACE);
272 * Git expects directory tree entries to be sorted with an imaginary slash
273 * appended to their name, and will break otherwise. Let's be nice.
274 * This function is intended to be used with mergesort(3) to sort an
275 * array of pointers to struct got_tree_entry objects.
278 sort_tree_entries_the_way_git_likes_it(const void *arg1, const void *arg2)
280 struct got_tree_entry * const *te1 = arg1;
281 struct got_tree_entry * const *te2 = arg2;
282 char name1[NAME_MAX + 2];
283 char name2[NAME_MAX + 2];
285 strlcpy(name1, (*te1)->name, sizeof(name1));
286 strlcpy(name2, (*te2)->name, sizeof(name2));
287 if (S_ISDIR((*te1)->mode))
288 strlcat(name1, "/", sizeof(name1));
289 if (S_ISDIR((*te2)->mode))
290 strlcat(name2, "/", sizeof(name2));
291 return strcmp(name1, name2);
294 const struct got_error *
295 got_object_tree_create(struct got_object_id **id,
296 struct got_pathlist_head *paths, int nentries, struct got_repository *repo)
298 const struct got_error *err = NULL;
299 char modebuf[sizeof("100644 ")];
302 size_t headerlen, len = 0, n;
303 FILE *treefile = NULL;
304 struct got_pathlist_entry *pe;
305 struct got_tree_entry **sorted_entries;
306 struct got_tree_entry *te;
313 sorted_entries = calloc(nentries, sizeof(struct got_tree_entry *));
314 if (sorted_entries == NULL)
315 return got_error_from_errno("calloc");
318 TAILQ_FOREACH(pe, paths, entry)
319 sorted_entries[i++] = pe->data;
320 mergesort(sorted_entries, nentries, sizeof(struct got_tree_entry *),
321 sort_tree_entries_the_way_git_likes_it);
323 for (i = 0; i < nentries; i++) {
324 te = sorted_entries[i];
325 err = te_mode2str(modebuf, sizeof(modebuf), te);
328 len += strlen(modebuf) + strlen(te->name) + 1 +
332 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TREE, len) == -1) {
333 err = got_error_from_errno("asprintf");
336 headerlen = strlen(header) + 1;
337 SHA1Update(&sha1_ctx, header, headerlen);
339 treefile = got_opentemp();
340 if (treefile == NULL) {
341 err = got_error_from_errno("got_opentemp");
345 n = fwrite(header, 1, headerlen, treefile);
346 if (n != headerlen) {
347 err = got_ferror(treefile, GOT_ERR_IO);
351 for (i = 0; i < nentries; i++) {
352 te = sorted_entries[i];
353 err = te_mode2str(modebuf, sizeof(modebuf), te);
356 len = strlen(modebuf);
357 n = fwrite(modebuf, 1, len, treefile);
359 err = got_ferror(treefile, GOT_ERR_IO);
362 SHA1Update(&sha1_ctx, modebuf, len);
364 len = strlen(te->name) + 1; /* must include NUL */
365 n = fwrite(te->name, 1, len, treefile);
367 err = got_ferror(treefile, GOT_ERR_IO);
370 SHA1Update(&sha1_ctx, te->name, len);
372 len = SHA1_DIGEST_LENGTH;
373 n = fwrite(te->id.sha1, 1, len, treefile);
375 err = got_ferror(treefile, GOT_ERR_IO);
378 SHA1Update(&sha1_ctx, te->id.sha1, len);
381 *id = malloc(sizeof(**id));
383 err = got_error_from_errno("malloc");
386 SHA1Final((*id)->sha1, &sha1_ctx);
388 if (fflush(treefile) != 0) {
389 err = got_error_from_errno("fflush");
394 err = create_object_file(*id, treefile, repo);
397 free(sorted_entries);
398 if (treefile && fclose(treefile) == EOF && err == NULL)
399 err = got_error_from_errno("fclose");
407 const struct got_error *
408 got_object_commit_create(struct got_object_id **id,
409 struct got_object_id *tree_id, struct got_object_id_queue *parent_ids,
410 int nparents, const char *author, time_t author_time,
411 const char *committer, time_t committer_time,
412 const char *logmsg, struct got_repository *repo)
414 const struct got_error *err = NULL;
416 char *header = NULL, *tree_str = NULL;
417 char *author_str = NULL, *committer_str = NULL;
419 size_t headerlen, len = 0, n;
420 FILE *commitfile = NULL;
421 struct got_object_qid *qid;
428 msg0 = strdup(logmsg);
430 return got_error_from_errno("strdup");
433 while (isspace((unsigned char)msg[0]))
436 while (len > 0 && isspace((unsigned char)msg[len - 1])) {
441 if (asprintf(&author_str, "%s%s %lld +0000\n",
442 GOT_COMMIT_LABEL_AUTHOR, author, (long long)author_time) == -1)
443 return got_error_from_errno("asprintf");
445 if (asprintf(&committer_str, "%s%s %lld +0000\n",
446 GOT_COMMIT_LABEL_COMMITTER, committer ? committer : author,
447 (long long)(committer ? committer_time : author_time))
449 err = got_error_from_errno("asprintf");
453 len = strlen(GOT_COMMIT_LABEL_TREE) + SHA1_DIGEST_STRING_LENGTH +
455 (strlen(GOT_COMMIT_LABEL_PARENT) + SHA1_DIGEST_STRING_LENGTH) +
456 + strlen(author_str) + strlen(committer_str) + 2 + strlen(msg);
458 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_COMMIT, len) == -1) {
459 err = got_error_from_errno("asprintf");
462 headerlen = strlen(header) + 1;
463 SHA1Update(&sha1_ctx, header, headerlen);
465 commitfile = got_opentemp();
466 if (commitfile == NULL) {
467 err = got_error_from_errno("got_opentemp");
471 n = fwrite(header, 1, headerlen, commitfile);
472 if (n != headerlen) {
473 err = got_ferror(commitfile, GOT_ERR_IO);
477 err = got_object_id_str(&id_str, tree_id);
480 if (asprintf(&tree_str, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str)
482 err = got_error_from_errno("asprintf");
485 len = strlen(tree_str);
486 SHA1Update(&sha1_ctx, tree_str, len);
487 n = fwrite(tree_str, 1, len, commitfile);
489 err = got_ferror(commitfile, GOT_ERR_IO);
494 STAILQ_FOREACH(qid, parent_ids, entry) {
495 char *parent_str = NULL;
499 err = got_object_id_str(&id_str, qid->id);
502 if (asprintf(&parent_str, "%s%s\n",
503 GOT_COMMIT_LABEL_PARENT, id_str) == -1) {
504 err = got_error_from_errno("asprintf");
507 len = strlen(parent_str);
508 SHA1Update(&sha1_ctx, parent_str, len);
509 n = fwrite(parent_str, 1, len, commitfile);
511 err = got_ferror(commitfile, GOT_ERR_IO);
519 len = strlen(author_str);
520 SHA1Update(&sha1_ctx, author_str, len);
521 n = fwrite(author_str, 1, len, commitfile);
523 err = got_ferror(commitfile, GOT_ERR_IO);
527 len = strlen(committer_str);
528 SHA1Update(&sha1_ctx, committer_str, len);
529 n = fwrite(committer_str, 1, len, commitfile);
531 err = got_ferror(commitfile, GOT_ERR_IO);
535 SHA1Update(&sha1_ctx, "\n", 1);
536 n = fwrite("\n", 1, 1, commitfile);
538 err = got_ferror(commitfile, GOT_ERR_IO);
543 SHA1Update(&sha1_ctx, msg, len);
544 n = fwrite(msg, 1, len, commitfile);
546 err = got_ferror(commitfile, GOT_ERR_IO);
550 SHA1Update(&sha1_ctx, "\n", 1);
551 n = fwrite("\n", 1, 1, commitfile);
553 err = got_ferror(commitfile, GOT_ERR_IO);
557 *id = malloc(sizeof(**id));
559 err = got_error_from_errno("malloc");
562 SHA1Final((*id)->sha1, &sha1_ctx);
564 if (fflush(commitfile) != 0) {
565 err = got_error_from_errno("fflush");
570 err = create_object_file(*id, commitfile, repo);
577 if (commitfile && fclose(commitfile) == EOF && err == NULL)
578 err = got_error_from_errno("fclose");
586 const struct got_error *
587 got_object_tag_create(struct got_object_id **id,
588 const char *tag_name, struct got_object_id *object_id, const char *tagger,
589 time_t tagger_time, const char *tagmsg, struct got_repository *repo)
591 const struct got_error *err = NULL;
594 char *tag_str = NULL, *tagger_str = NULL;
595 char *id_str = NULL, *obj_str = NULL, *type_str = NULL;
596 size_t headerlen, len = 0, n;
597 FILE *tagfile = NULL;
598 char *msg0 = NULL, *msg;
599 const char *obj_type_str;
606 err = got_object_id_str(&id_str, object_id);
609 if (asprintf(&obj_str, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str) == -1) {
610 err = got_error_from_errno("asprintf");
614 err = got_object_get_type(&obj_type, repo, object_id);
619 case GOT_OBJ_TYPE_BLOB:
620 obj_type_str = GOT_OBJ_LABEL_BLOB;
622 case GOT_OBJ_TYPE_TREE:
623 obj_type_str = GOT_OBJ_LABEL_TREE;
625 case GOT_OBJ_TYPE_COMMIT:
626 obj_type_str = GOT_OBJ_LABEL_COMMIT;
628 case GOT_OBJ_TYPE_TAG:
629 obj_type_str = GOT_OBJ_LABEL_TAG;
632 err = got_error(GOT_ERR_OBJ_TYPE);
636 if (asprintf(&type_str, "%s%s\n", GOT_TAG_LABEL_TYPE,
637 obj_type_str) == -1) {
638 err = got_error_from_errno("asprintf");
642 if (asprintf(&tag_str, "%s%s\n", GOT_TAG_LABEL_TAG, tag_name) == -1) {
643 err = got_error_from_errno("asprintf");
647 if (asprintf(&tagger_str, "%s%s %lld +0000\n",
648 GOT_TAG_LABEL_TAGGER, tagger, (long long)tagger_time) == -1)
649 return got_error_from_errno("asprintf");
651 msg0 = strdup(tagmsg);
653 err = got_error_from_errno("strdup");
658 while (isspace((unsigned char)msg[0]))
661 len = strlen(obj_str) + strlen(type_str) + strlen(tag_str) +
662 strlen(tagger_str) + 1 + strlen(msg) + 1;
664 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TAG, len) == -1) {
665 err = got_error_from_errno("asprintf");
669 headerlen = strlen(header) + 1;
670 SHA1Update(&sha1_ctx, header, headerlen);
672 tagfile = got_opentemp();
673 if (tagfile == NULL) {
674 err = got_error_from_errno("got_opentemp");
678 n = fwrite(header, 1, headerlen, tagfile);
679 if (n != headerlen) {
680 err = got_ferror(tagfile, GOT_ERR_IO);
683 len = strlen(obj_str);
684 SHA1Update(&sha1_ctx, obj_str, len);
685 n = fwrite(obj_str, 1, len, tagfile);
687 err = got_ferror(tagfile, GOT_ERR_IO);
690 len = strlen(type_str);
691 SHA1Update(&sha1_ctx, type_str, len);
692 n = fwrite(type_str, 1, len, tagfile);
694 err = got_ferror(tagfile, GOT_ERR_IO);
698 len = strlen(tag_str);
699 SHA1Update(&sha1_ctx, tag_str, len);
700 n = fwrite(tag_str, 1, len, tagfile);
702 err = got_ferror(tagfile, GOT_ERR_IO);
706 len = strlen(tagger_str);
707 SHA1Update(&sha1_ctx, tagger_str, len);
708 n = fwrite(tagger_str, 1, len, tagfile);
710 err = got_ferror(tagfile, GOT_ERR_IO);
714 SHA1Update(&sha1_ctx, "\n", 1);
715 n = fwrite("\n", 1, 1, tagfile);
717 err = got_ferror(tagfile, GOT_ERR_IO);
722 SHA1Update(&sha1_ctx, msg, len);
723 n = fwrite(msg, 1, len, tagfile);
725 err = got_ferror(tagfile, GOT_ERR_IO);
729 SHA1Update(&sha1_ctx, "\n", 1);
730 n = fwrite("\n", 1, 1, tagfile);
732 err = got_ferror(tagfile, GOT_ERR_IO);
736 *id = malloc(sizeof(**id));
738 err = got_error_from_errno("malloc");
741 SHA1Final((*id)->sha1, &sha1_ctx);
743 if (fflush(tagfile) != 0) {
744 err = got_error_from_errno("fflush");
749 err = create_object_file(*id, tagfile, repo);
755 if (tagfile && fclose(tagfile) == EOF && err == NULL)
756 err = got_error_from_errno("fclose");