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"
47 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
50 static const struct got_error *
51 create_object_file(struct got_object_id *id, FILE *content,
52 struct got_repository *repo)
54 const struct got_error *err = NULL, *unlock_err = NULL;
55 char *objpath = NULL, *tmppath = NULL;
57 struct got_lockfile *lf = NULL;
60 err = got_object_get_path(&objpath, id, repo);
64 err = got_opentemp_named(&tmppath, &tmpfile, objpath);
67 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
69 err = got_path_dirname(&parent_path, objpath);
72 err = got_path_mkdir(parent_path);
76 err = got_opentemp_named(&tmppath, &tmpfile, objpath);
81 err = got_deflate_to_file(&tmplen, content, tmpfile);
85 err = got_lockfile_lock(&lf, objpath);
89 if (rename(tmppath, objpath) != 0) {
90 err = got_error_from_errno3("rename", tmppath, objpath);
96 if (chmod(objpath, GOT_DEFAULT_FILE_MODE) != 0) {
97 err = got_error_from_errno2("chmod", objpath);
103 if (unlink(tmppath) != 0 && err == NULL)
104 err = got_error_from_errno2("unlink", tmppath);
107 if (tmpfile && fclose(tmpfile) != 0 && err == NULL)
108 err = got_error_from_errno("fclose");
110 unlock_err = got_lockfile_unlock(lf);
111 return err ? err : unlock_err;
114 const struct got_error *
115 got_object_blob_file_create(struct got_object_id **id, FILE **blobfile,
116 const char *ondisk_path)
118 const struct got_error *err = NULL;
123 size_t headerlen = 0, n;
130 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
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);
139 } else if (fstat(fd, &sb) == -1) {
140 err = got_error_from_errno2("fstat", ondisk_path);
144 if (asprintf(&header, "%s %lld", GOT_OBJ_LABEL_BLOB,
146 err = got_error_from_errno("asprintf");
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");
158 n = fwrite(header, 1, headerlen, *blobfile);
159 if (n != headerlen) {
160 err = got_ferror(*blobfile, GOT_ERR_IO);
164 char buf[PATH_MAX * 8];
167 if (S_ISLNK(sb.st_mode)) {
168 inlen = readlink(ondisk_path, buf, sizeof(buf));
170 err = got_error_from_errno("readlink");
174 inlen = read(fd, buf, sizeof(buf));
176 err = got_error_from_errno("read");
182 SHA1Update(&sha1_ctx, buf, inlen);
183 n = fwrite(buf, 1, inlen, *blobfile);
185 err = got_ferror(*blobfile, GOT_ERR_IO);
188 if (S_ISLNK(sb.st_mode))
192 *id = malloc(sizeof(**id));
194 err = got_error_from_errno("malloc");
197 SHA1Final((*id)->sha1, &sha1_ctx);
199 if (fflush(*blobfile) != 0) {
200 err = got_error_from_errno("fflush");
206 if (fd != -1 && close(fd) != 0 && err == NULL)
207 err = got_error_from_errno("close");
219 const struct got_error *
220 got_object_blob_create(struct got_object_id **id, const char *ondisk_path,
221 struct got_repository *repo)
223 const struct got_error *err = NULL;
224 FILE *blobfile = NULL;
226 err = got_object_blob_file_create(id, &blobfile, ondisk_path);
230 err = create_object_file(*id, blobfile, repo);
231 if (fclose(blobfile) == EOF && err == NULL)
232 err = got_error_from_errno("fclose");
240 static const struct got_error *
241 te_mode2str(char *buf, size_t len, struct got_tree_entry *te)
247 * Some Git implementations are picky about modes seen in tree entries.
248 * For best compatibility we normalize the file/directory mode here.
249 * Note that we do not support committing symlinks.
251 if (S_ISREG(te->mode)) {
252 mode = GOT_DEFAULT_FILE_MODE;
253 if (te->mode & (S_IXUSR | S_IXGRP | S_IXOTH))
254 mode |= S_IXUSR | S_IXGRP | S_IXOTH;
255 } else if (got_object_tree_entry_is_submodule(te))
256 mode = S_IFDIR | S_IFLNK;
257 else if (S_ISLNK(te->mode))
258 mode = S_IFLNK; /* Git leaves all the other bits unset. */
259 else if (S_ISDIR(te->mode))
260 mode = S_IFDIR; /* Git leaves all the other bits unset. */
262 return got_error(GOT_ERR_BAD_FILETYPE);
264 ret = snprintf(buf, len, "%o ", mode);
265 if (ret == -1 || ret >= len)
266 return got_error(GOT_ERR_NO_SPACE);
271 * Git expects directory tree entries to be sorted with an imaginary slash
272 * appended to their name, and will break otherwise. Let's be nice.
273 * This function is intended to be used with mergesort(3) to sort an
274 * array of pointers to struct got_tree_entry objects.
277 sort_tree_entries_the_way_git_likes_it(const void *arg1, const void *arg2)
279 struct got_tree_entry * const *te1 = arg1;
280 struct got_tree_entry * const *te2 = arg2;
281 char name1[NAME_MAX + 2];
282 char name2[NAME_MAX + 2];
284 strlcpy(name1, (*te1)->name, sizeof(name1));
285 strlcpy(name2, (*te2)->name, sizeof(name2));
286 if (S_ISDIR((*te1)->mode))
287 strlcat(name1, "/", sizeof(name1));
288 if (S_ISDIR((*te2)->mode))
289 strlcat(name2, "/", sizeof(name2));
290 return strcmp(name1, name2);
293 const struct got_error *
294 got_object_tree_create(struct got_object_id **id,
295 struct got_pathlist_head *paths, int nentries, struct got_repository *repo)
297 const struct got_error *err = NULL;
298 char modebuf[sizeof("100644 ")];
301 size_t headerlen, len = 0, n;
302 FILE *treefile = NULL;
303 struct got_pathlist_entry *pe;
304 struct got_tree_entry **sorted_entries;
305 struct got_tree_entry *te;
312 sorted_entries = calloc(nentries, sizeof(struct got_tree_entry *));
313 if (sorted_entries == NULL)
314 return got_error_from_errno("calloc");
317 TAILQ_FOREACH(pe, paths, entry)
318 sorted_entries[i++] = pe->data;
319 mergesort(sorted_entries, nentries, sizeof(struct got_tree_entry *),
320 sort_tree_entries_the_way_git_likes_it);
322 for (i = 0; i < nentries; i++) {
323 te = sorted_entries[i];
324 err = te_mode2str(modebuf, sizeof(modebuf), te);
327 len += strlen(modebuf) + strlen(te->name) + 1 +
331 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TREE, len) == -1) {
332 err = got_error_from_errno("asprintf");
335 headerlen = strlen(header) + 1;
336 SHA1Update(&sha1_ctx, header, headerlen);
338 treefile = got_opentemp();
339 if (treefile == NULL) {
340 err = got_error_from_errno("got_opentemp");
344 n = fwrite(header, 1, headerlen, treefile);
345 if (n != headerlen) {
346 err = got_ferror(treefile, GOT_ERR_IO);
350 for (i = 0; i < nentries; i++) {
351 te = sorted_entries[i];
352 err = te_mode2str(modebuf, sizeof(modebuf), te);
355 len = strlen(modebuf);
356 n = fwrite(modebuf, 1, len, treefile);
358 err = got_ferror(treefile, GOT_ERR_IO);
361 SHA1Update(&sha1_ctx, modebuf, len);
363 len = strlen(te->name) + 1; /* must include NUL */
364 n = fwrite(te->name, 1, len, treefile);
366 err = got_ferror(treefile, GOT_ERR_IO);
369 SHA1Update(&sha1_ctx, te->name, len);
371 len = SHA1_DIGEST_LENGTH;
372 n = fwrite(te->id.sha1, 1, len, treefile);
374 err = got_ferror(treefile, GOT_ERR_IO);
377 SHA1Update(&sha1_ctx, te->id.sha1, len);
380 *id = malloc(sizeof(**id));
382 err = got_error_from_errno("malloc");
385 SHA1Final((*id)->sha1, &sha1_ctx);
387 if (fflush(treefile) != 0) {
388 err = got_error_from_errno("fflush");
393 err = create_object_file(*id, treefile, repo);
396 free(sorted_entries);
397 if (treefile && fclose(treefile) != 0 && err == NULL)
398 err = got_error_from_errno("fclose");
406 const struct got_error *
407 got_object_commit_create(struct got_object_id **id,
408 struct got_object_id *tree_id, struct got_object_id_queue *parent_ids,
409 int nparents, const char *author, time_t author_time,
410 const char *committer, time_t committer_time,
411 const char *logmsg, struct got_repository *repo)
413 const struct got_error *err = NULL;
415 char *header = NULL, *tree_str = NULL;
416 char *author_str = NULL, *committer_str = NULL;
418 size_t headerlen, len = 0, n;
419 FILE *commitfile = NULL;
420 struct got_object_qid *qid;
427 msg0 = strdup(logmsg);
429 return got_error_from_errno("strdup");
432 while (isspace((unsigned char)msg[0]))
435 while (len > 0 && isspace((unsigned char)msg[len - 1])) {
440 if (asprintf(&author_str, "%s%s %lld +0000\n",
441 GOT_COMMIT_LABEL_AUTHOR, author, author_time) == -1)
442 return got_error_from_errno("asprintf");
444 if (asprintf(&committer_str, "%s%s %lld +0000\n",
445 GOT_COMMIT_LABEL_COMMITTER, committer ? committer : author,
446 committer ? committer_time : author_time)
448 err = got_error_from_errno("asprintf");
452 len = strlen(GOT_COMMIT_LABEL_TREE) + SHA1_DIGEST_STRING_LENGTH +
454 (strlen(GOT_COMMIT_LABEL_PARENT) + SHA1_DIGEST_STRING_LENGTH) +
455 + strlen(author_str) + strlen(committer_str) + 2 + strlen(msg);
457 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_COMMIT, len) == -1) {
458 err = got_error_from_errno("asprintf");
461 headerlen = strlen(header) + 1;
462 SHA1Update(&sha1_ctx, header, headerlen);
464 commitfile = got_opentemp();
465 if (commitfile == NULL) {
466 err = got_error_from_errno("got_opentemp");
470 n = fwrite(header, 1, headerlen, commitfile);
471 if (n != headerlen) {
472 err = got_ferror(commitfile, GOT_ERR_IO);
476 err = got_object_id_str(&id_str, tree_id);
479 if (asprintf(&tree_str, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str)
481 err = got_error_from_errno("asprintf");
484 len = strlen(tree_str);
485 SHA1Update(&sha1_ctx, tree_str, len);
486 n = fwrite(tree_str, 1, len, commitfile);
488 err = got_ferror(commitfile, GOT_ERR_IO);
493 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
494 char *parent_str = NULL;
498 err = got_object_id_str(&id_str, qid->id);
501 if (asprintf(&parent_str, "%s%s\n",
502 GOT_COMMIT_LABEL_PARENT, id_str) == -1) {
503 err = got_error_from_errno("asprintf");
506 len = strlen(parent_str);
507 SHA1Update(&sha1_ctx, parent_str, len);
508 n = fwrite(parent_str, 1, len, commitfile);
510 err = got_ferror(commitfile, GOT_ERR_IO);
518 len = strlen(author_str);
519 SHA1Update(&sha1_ctx, author_str, len);
520 n = fwrite(author_str, 1, len, commitfile);
522 err = got_ferror(commitfile, GOT_ERR_IO);
526 len = strlen(committer_str);
527 SHA1Update(&sha1_ctx, committer_str, len);
528 n = fwrite(committer_str, 1, len, commitfile);
530 err = got_ferror(commitfile, GOT_ERR_IO);
534 SHA1Update(&sha1_ctx, "\n", 1);
535 n = fwrite("\n", 1, 1, commitfile);
537 err = got_ferror(commitfile, GOT_ERR_IO);
542 SHA1Update(&sha1_ctx, msg, len);
543 n = fwrite(msg, 1, len, commitfile);
545 err = got_ferror(commitfile, GOT_ERR_IO);
549 SHA1Update(&sha1_ctx, "\n", 1);
550 n = fwrite("\n", 1, 1, commitfile);
552 err = got_ferror(commitfile, GOT_ERR_IO);
556 *id = malloc(sizeof(**id));
558 err = got_error_from_errno("malloc");
561 SHA1Final((*id)->sha1, &sha1_ctx);
563 if (fflush(commitfile) != 0) {
564 err = got_error_from_errno("fflush");
569 err = create_object_file(*id, commitfile, repo);
576 if (commitfile && fclose(commitfile) != 0 && err == NULL)
577 err = got_error_from_errno("fclose");
585 const struct got_error *
586 got_object_tag_create(struct got_object_id **id,
587 const char *tag_name, struct got_object_id *object_id, const char *tagger,
588 time_t tagger_time, const char *tagmsg, struct got_repository *repo)
590 const struct got_error *err = NULL;
593 char *tag_str = NULL, *tagger_str = NULL;
594 char *id_str = NULL, *obj_str = NULL, *type_str = NULL;
595 size_t headerlen, len = 0, n;
596 FILE *tagfile = NULL;
597 char *msg0 = NULL, *msg;
598 const char *obj_type_str;
605 err = got_object_id_str(&id_str, object_id);
608 if (asprintf(&obj_str, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str) == -1) {
609 err = got_error_from_errno("asprintf");
613 err = got_object_get_type(&obj_type, repo, object_id);
618 case GOT_OBJ_TYPE_BLOB:
619 obj_type_str = GOT_OBJ_LABEL_BLOB;
621 case GOT_OBJ_TYPE_TREE:
622 obj_type_str = GOT_OBJ_LABEL_TREE;
624 case GOT_OBJ_TYPE_COMMIT:
625 obj_type_str = GOT_OBJ_LABEL_COMMIT;
627 case GOT_OBJ_TYPE_TAG:
628 obj_type_str = GOT_OBJ_LABEL_TAG;
631 err = got_error(GOT_ERR_OBJ_TYPE);
635 if (asprintf(&type_str, "%s%s\n", GOT_TAG_LABEL_TYPE,
636 obj_type_str) == -1) {
637 err = got_error_from_errno("asprintf");
641 if (asprintf(&tag_str, "%s%s\n", GOT_TAG_LABEL_TAG, tag_name) == -1) {
642 err = got_error_from_errno("asprintf");
646 if (asprintf(&tagger_str, "%s%s %lld +0000\n",
647 GOT_TAG_LABEL_TAGGER, tagger, tagger_time) == -1)
648 return got_error_from_errno("asprintf");
650 msg0 = strdup(tagmsg);
652 err = got_error_from_errno("strdup");
657 while (isspace((unsigned char)msg[0]))
660 len = strlen(obj_str) + strlen(type_str) + strlen(tag_str) +
661 strlen(tagger_str) + 1 + strlen(msg) + 1;
663 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TAG, len) == -1) {
664 err = got_error_from_errno("asprintf");
668 headerlen = strlen(header) + 1;
669 SHA1Update(&sha1_ctx, header, headerlen);
671 tagfile = got_opentemp();
672 if (tagfile == NULL) {
673 err = got_error_from_errno("got_opentemp");
677 n = fwrite(header, 1, headerlen, tagfile);
678 if (n != headerlen) {
679 err = got_ferror(tagfile, GOT_ERR_IO);
682 len = strlen(obj_str);
683 SHA1Update(&sha1_ctx, obj_str, len);
684 n = fwrite(obj_str, 1, len, tagfile);
686 err = got_ferror(tagfile, GOT_ERR_IO);
689 len = strlen(type_str);
690 SHA1Update(&sha1_ctx, type_str, len);
691 n = fwrite(type_str, 1, len, tagfile);
693 err = got_ferror(tagfile, GOT_ERR_IO);
697 len = strlen(tag_str);
698 SHA1Update(&sha1_ctx, tag_str, len);
699 n = fwrite(tag_str, 1, len, tagfile);
701 err = got_ferror(tagfile, GOT_ERR_IO);
705 len = strlen(tagger_str);
706 SHA1Update(&sha1_ctx, tagger_str, len);
707 n = fwrite(tagger_str, 1, len, tagfile);
709 err = got_ferror(tagfile, GOT_ERR_IO);
713 SHA1Update(&sha1_ctx, "\n", 1);
714 n = fwrite("\n", 1, 1, tagfile);
716 err = got_ferror(tagfile, GOT_ERR_IO);
721 SHA1Update(&sha1_ctx, msg, len);
722 n = fwrite(msg, 1, len, tagfile);
724 err = got_ferror(tagfile, GOT_ERR_IO);
728 SHA1Update(&sha1_ctx, "\n", 1);
729 n = fwrite("\n", 1, 1, tagfile);
731 err = got_ferror(tagfile, GOT_ERR_IO);
735 *id = malloc(sizeof(**id));
737 err = got_error_from_errno("malloc");
740 SHA1Final((*id)->sha1, &sha1_ctx);
742 if (fflush(tagfile) != 0) {
743 err = got_error_from_errno("fflush");
748 err = create_object_file(*id, tagfile, repo);
754 if (tagfile && fclose(tagfile) != 0 && err == NULL)
755 err = got_error_from_errno("fclose");