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>
31 #include "got_compat.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 off_t content_len, 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 if (fchmod(fileno(tmpfile), GOT_DEFAULT_FILE_MODE) != 0) {
82 err = got_error_from_errno2("fchmod", tmppath);
86 err = got_deflate_to_file(&tmplen, content, content_len, tmpfile, NULL);
90 err = got_lockfile_lock(&lf, objpath, -1);
94 if (rename(tmppath, objpath) != 0) {
95 err = got_error_from_errno3("rename", tmppath, objpath);
103 if (unlink(tmppath) != 0 && err == NULL)
104 err = got_error_from_errno2("unlink", tmppath);
107 if (tmpfile && fclose(tmpfile) == EOF && err == NULL)
108 err = got_error_from_errno("fclose");
110 unlock_err = got_lockfile_unlock(lf, -1);
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 off_t *blobsize, const char *ondisk_path)
118 const struct got_error *err = NULL;
123 size_t headerlen = 0, n;
131 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
133 if (!got_err_open_nofollow_on_symlink())
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);
164 *blobsize += headerlen;
166 char buf[PATH_MAX * 8];
169 if (S_ISLNK(sb.st_mode)) {
170 inlen = readlink(ondisk_path, buf, sizeof(buf));
172 err = got_error_from_errno("readlink");
176 inlen = read(fd, buf, sizeof(buf));
178 err = got_error_from_errno("read");
184 SHA1Update(&sha1_ctx, buf, inlen);
185 n = fwrite(buf, 1, inlen, *blobfile);
187 err = got_ferror(*blobfile, GOT_ERR_IO);
191 if (S_ISLNK(sb.st_mode))
195 *id = malloc(sizeof(**id));
197 err = got_error_from_errno("malloc");
200 SHA1Final((*id)->sha1, &sha1_ctx);
202 if (fflush(*blobfile) != 0) {
203 err = got_error_from_errno("fflush");
209 if (fd != -1 && close(fd) == -1 && err == NULL)
210 err = got_error_from_errno("close");
222 const struct got_error *
223 got_object_blob_create(struct got_object_id **id, const char *ondisk_path,
224 struct got_repository *repo)
226 const struct got_error *err = NULL;
227 FILE *blobfile = NULL;
230 err = got_object_blob_file_create(id, &blobfile, &blobsize,
235 err = create_object_file(*id, blobfile, blobsize, repo);
236 if (fclose(blobfile) == EOF && err == NULL)
237 err = got_error_from_errno("fclose");
245 static const struct got_error *
246 te_mode2str(char *buf, size_t len, struct got_tree_entry *te)
252 * Some Git implementations are picky about modes seen in tree entries.
253 * For best compatibility we normalize the file/directory mode here.
255 if (S_ISREG(te->mode)) {
256 mode = GOT_DEFAULT_FILE_MODE;
257 if (te->mode & (S_IXUSR | S_IXGRP | S_IXOTH))
258 mode |= S_IXUSR | S_IXGRP | S_IXOTH;
259 } else if (got_object_tree_entry_is_submodule(te))
260 mode = S_IFDIR | S_IFLNK;
261 else if (S_ISLNK(te->mode))
262 mode = S_IFLNK; /* Git leaves all the other bits unset. */
263 else if (S_ISDIR(te->mode))
264 mode = S_IFDIR; /* Git leaves all the other bits unset. */
266 return got_error(GOT_ERR_BAD_FILETYPE);
268 ret = snprintf(buf, len, "%o ", mode);
269 if (ret == -1 || ret >= len)
270 return got_error(GOT_ERR_NO_SPACE);
275 * Git expects directory tree entries to be sorted with an imaginary slash
276 * appended to their name, and will break otherwise. Let's be nice.
277 * This function is intended to be used with mergesort(3) to sort an
278 * array of pointers to struct got_tree_entry objects.
281 sort_tree_entries_the_way_git_likes_it(const void *arg1, const void *arg2)
283 struct got_tree_entry * const *te1 = arg1;
284 struct got_tree_entry * const *te2 = arg2;
285 char name1[NAME_MAX + 2];
286 char name2[NAME_MAX + 2];
288 strlcpy(name1, (*te1)->name, sizeof(name1));
289 strlcpy(name2, (*te2)->name, sizeof(name2));
290 if (S_ISDIR((*te1)->mode))
291 strlcat(name1, "/", sizeof(name1));
292 if (S_ISDIR((*te2)->mode))
293 strlcat(name2, "/", sizeof(name2));
294 return strcmp(name1, name2);
297 const struct got_error *
298 got_object_tree_create(struct got_object_id **id,
299 struct got_pathlist_head *paths, int nentries, struct got_repository *repo)
301 const struct got_error *err = NULL;
302 char modebuf[sizeof("100644 ")];
305 size_t headerlen, len = 0, n;
306 FILE *treefile = NULL;
308 struct got_pathlist_entry *pe;
309 struct got_tree_entry **sorted_entries;
310 struct got_tree_entry *te;
317 sorted_entries = calloc(nentries, sizeof(struct got_tree_entry *));
318 if (sorted_entries == NULL)
319 return got_error_from_errno("calloc");
322 TAILQ_FOREACH(pe, paths, entry)
323 sorted_entries[i++] = pe->data;
324 mergesort(sorted_entries, nentries, sizeof(struct got_tree_entry *),
325 sort_tree_entries_the_way_git_likes_it);
327 for (i = 0; i < nentries; i++) {
328 te = sorted_entries[i];
329 err = te_mode2str(modebuf, sizeof(modebuf), te);
332 len += strlen(modebuf) + strlen(te->name) + 1 +
336 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TREE, len) == -1) {
337 err = got_error_from_errno("asprintf");
340 headerlen = strlen(header) + 1;
341 SHA1Update(&sha1_ctx, header, headerlen);
343 treefile = got_opentemp();
344 if (treefile == NULL) {
345 err = got_error_from_errno("got_opentemp");
349 n = fwrite(header, 1, headerlen, treefile);
350 if (n != headerlen) {
351 err = got_ferror(treefile, GOT_ERR_IO);
354 treesize += headerlen;
356 for (i = 0; i < nentries; i++) {
357 te = sorted_entries[i];
358 err = te_mode2str(modebuf, sizeof(modebuf), te);
361 len = strlen(modebuf);
362 n = fwrite(modebuf, 1, len, treefile);
364 err = got_ferror(treefile, GOT_ERR_IO);
367 SHA1Update(&sha1_ctx, modebuf, len);
370 len = strlen(te->name) + 1; /* must include NUL */
371 n = fwrite(te->name, 1, len, treefile);
373 err = got_ferror(treefile, GOT_ERR_IO);
376 SHA1Update(&sha1_ctx, te->name, len);
379 len = SHA1_DIGEST_LENGTH;
380 n = fwrite(te->id.sha1, 1, len, treefile);
382 err = got_ferror(treefile, GOT_ERR_IO);
385 SHA1Update(&sha1_ctx, te->id.sha1, len);
389 *id = malloc(sizeof(**id));
391 err = got_error_from_errno("malloc");
394 SHA1Final((*id)->sha1, &sha1_ctx);
396 if (fflush(treefile) != 0) {
397 err = got_error_from_errno("fflush");
402 err = create_object_file(*id, treefile, treesize, repo);
405 free(sorted_entries);
406 if (treefile && fclose(treefile) == EOF && err == NULL)
407 err = got_error_from_errno("fclose");
415 const struct got_error *
416 got_object_commit_create(struct got_object_id **id,
417 struct got_object_id *tree_id, struct got_object_id_queue *parent_ids,
418 int nparents, const char *author, time_t author_time,
419 const char *committer, time_t committer_time,
420 const char *logmsg, struct got_repository *repo)
422 const struct got_error *err = NULL;
424 char *header = NULL, *tree_str = NULL;
425 char *author_str = NULL, *committer_str = NULL;
427 size_t headerlen, len = 0, n;
428 FILE *commitfile = NULL;
429 off_t commitsize = 0;
430 struct got_object_qid *qid;
437 msg0 = strdup(logmsg);
439 return got_error_from_errno("strdup");
442 while (isspace((unsigned char)msg[0]))
445 while (len > 0 && isspace((unsigned char)msg[len - 1])) {
450 if (asprintf(&author_str, "%s%s %lld +0000\n",
451 GOT_COMMIT_LABEL_AUTHOR, author, (long long)author_time) == -1)
452 return got_error_from_errno("asprintf");
454 if (asprintf(&committer_str, "%s%s %lld +0000\n",
455 GOT_COMMIT_LABEL_COMMITTER, committer ? committer : author,
456 (long long)(committer ? committer_time : author_time))
458 err = got_error_from_errno("asprintf");
462 len = strlen(GOT_COMMIT_LABEL_TREE) + SHA1_DIGEST_STRING_LENGTH +
464 (strlen(GOT_COMMIT_LABEL_PARENT) + SHA1_DIGEST_STRING_LENGTH) +
465 + strlen(author_str) + strlen(committer_str) + 2 + strlen(msg);
467 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_COMMIT, len) == -1) {
468 err = got_error_from_errno("asprintf");
471 headerlen = strlen(header) + 1;
472 SHA1Update(&sha1_ctx, header, headerlen);
474 commitfile = got_opentemp();
475 if (commitfile == NULL) {
476 err = got_error_from_errno("got_opentemp");
480 n = fwrite(header, 1, headerlen, commitfile);
481 if (n != headerlen) {
482 err = got_ferror(commitfile, GOT_ERR_IO);
485 commitsize += headerlen;
487 err = got_object_id_str(&id_str, tree_id);
490 if (asprintf(&tree_str, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str)
492 err = got_error_from_errno("asprintf");
495 len = strlen(tree_str);
496 SHA1Update(&sha1_ctx, tree_str, len);
497 n = fwrite(tree_str, 1, len, commitfile);
499 err = got_ferror(commitfile, GOT_ERR_IO);
507 STAILQ_FOREACH(qid, parent_ids, entry) {
508 char *parent_str = NULL;
510 err = got_object_id_str(&id_str, qid->id);
513 if (asprintf(&parent_str, "%s%s\n",
514 GOT_COMMIT_LABEL_PARENT, id_str) == -1) {
515 err = got_error_from_errno("asprintf");
518 len = strlen(parent_str);
519 SHA1Update(&sha1_ctx, parent_str, len);
520 n = fwrite(parent_str, 1, len, commitfile);
522 err = got_ferror(commitfile, GOT_ERR_IO);
533 len = strlen(author_str);
534 SHA1Update(&sha1_ctx, author_str, len);
535 n = fwrite(author_str, 1, len, commitfile);
537 err = got_ferror(commitfile, GOT_ERR_IO);
542 len = strlen(committer_str);
543 SHA1Update(&sha1_ctx, committer_str, len);
544 n = fwrite(committer_str, 1, len, commitfile);
546 err = got_ferror(commitfile, GOT_ERR_IO);
551 SHA1Update(&sha1_ctx, "\n", 1);
552 n = fwrite("\n", 1, 1, commitfile);
554 err = got_ferror(commitfile, GOT_ERR_IO);
560 SHA1Update(&sha1_ctx, msg, len);
561 n = fwrite(msg, 1, len, commitfile);
563 err = got_ferror(commitfile, GOT_ERR_IO);
568 SHA1Update(&sha1_ctx, "\n", 1);
569 n = fwrite("\n", 1, 1, commitfile);
571 err = got_ferror(commitfile, GOT_ERR_IO);
576 *id = malloc(sizeof(**id));
578 err = got_error_from_errno("malloc");
581 SHA1Final((*id)->sha1, &sha1_ctx);
583 if (fflush(commitfile) != 0) {
584 err = got_error_from_errno("fflush");
589 err = create_object_file(*id, commitfile, commitsize, repo);
597 if (commitfile && fclose(commitfile) == EOF && err == NULL)
598 err = got_error_from_errno("fclose");
606 const struct got_error *
607 got_object_tag_create(struct got_object_id **id,
608 const char *tag_name, struct got_object_id *object_id, const char *tagger,
609 time_t tagger_time, const char *tagmsg, struct got_repository *repo)
611 const struct got_error *err = NULL;
614 char *tag_str = NULL, *tagger_str = NULL;
615 char *id_str = NULL, *obj_str = NULL, *type_str = NULL;
616 size_t headerlen, len = 0, n;
617 FILE *tagfile = NULL;
619 char *msg0 = NULL, *msg;
620 const char *obj_type_str;
627 err = got_object_id_str(&id_str, object_id);
630 if (asprintf(&obj_str, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str) == -1) {
631 err = got_error_from_errno("asprintf");
635 err = got_object_get_type(&obj_type, repo, object_id);
640 case GOT_OBJ_TYPE_BLOB:
641 obj_type_str = GOT_OBJ_LABEL_BLOB;
643 case GOT_OBJ_TYPE_TREE:
644 obj_type_str = GOT_OBJ_LABEL_TREE;
646 case GOT_OBJ_TYPE_COMMIT:
647 obj_type_str = GOT_OBJ_LABEL_COMMIT;
649 case GOT_OBJ_TYPE_TAG:
650 obj_type_str = GOT_OBJ_LABEL_TAG;
653 err = got_error(GOT_ERR_OBJ_TYPE);
657 if (asprintf(&type_str, "%s%s\n", GOT_TAG_LABEL_TYPE,
658 obj_type_str) == -1) {
659 err = got_error_from_errno("asprintf");
663 if (asprintf(&tag_str, "%s%s\n", GOT_TAG_LABEL_TAG, tag_name) == -1) {
664 err = got_error_from_errno("asprintf");
668 if (asprintf(&tagger_str, "%s%s %lld +0000\n",
669 GOT_TAG_LABEL_TAGGER, tagger, (long long)tagger_time) == -1)
670 return got_error_from_errno("asprintf");
672 msg0 = strdup(tagmsg);
674 err = got_error_from_errno("strdup");
679 while (isspace((unsigned char)msg[0]))
682 len = strlen(obj_str) + strlen(type_str) + strlen(tag_str) +
683 strlen(tagger_str) + 1 + strlen(msg) + 1;
685 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TAG, len) == -1) {
686 err = got_error_from_errno("asprintf");
690 headerlen = strlen(header) + 1;
691 SHA1Update(&sha1_ctx, header, headerlen);
693 tagfile = got_opentemp();
694 if (tagfile == NULL) {
695 err = got_error_from_errno("got_opentemp");
699 n = fwrite(header, 1, headerlen, tagfile);
700 if (n != headerlen) {
701 err = got_ferror(tagfile, GOT_ERR_IO);
704 tagsize += headerlen;
705 len = strlen(obj_str);
706 SHA1Update(&sha1_ctx, obj_str, len);
707 n = fwrite(obj_str, 1, len, tagfile);
709 err = got_ferror(tagfile, GOT_ERR_IO);
713 len = strlen(type_str);
714 SHA1Update(&sha1_ctx, type_str, len);
715 n = fwrite(type_str, 1, len, tagfile);
717 err = got_ferror(tagfile, GOT_ERR_IO);
722 len = strlen(tag_str);
723 SHA1Update(&sha1_ctx, tag_str, len);
724 n = fwrite(tag_str, 1, len, tagfile);
726 err = got_ferror(tagfile, GOT_ERR_IO);
731 len = strlen(tagger_str);
732 SHA1Update(&sha1_ctx, tagger_str, len);
733 n = fwrite(tagger_str, 1, len, tagfile);
735 err = got_ferror(tagfile, GOT_ERR_IO);
740 SHA1Update(&sha1_ctx, "\n", 1);
741 n = fwrite("\n", 1, 1, tagfile);
743 err = got_ferror(tagfile, GOT_ERR_IO);
749 SHA1Update(&sha1_ctx, msg, len);
750 n = fwrite(msg, 1, len, tagfile);
752 err = got_ferror(tagfile, GOT_ERR_IO);
757 SHA1Update(&sha1_ctx, "\n", 1);
758 n = fwrite("\n", 1, 1, tagfile);
760 err = got_ferror(tagfile, GOT_ERR_IO);
765 *id = malloc(sizeof(**id));
767 err = got_error_from_errno("malloc");
770 SHA1Final((*id)->sha1, &sha1_ctx);
772 if (fflush(tagfile) != 0) {
773 err = got_error_from_errno("fflush");
778 err = create_object_file(*id, tagfile, tagsize, repo);
784 if (tagfile && fclose(tagfile) == EOF && err == NULL)
785 err = got_error_from_errno("fclose");