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_create(struct got_object_id **id, const char *ondisk_path,
116 struct got_repository *repo)
118 const struct got_error *err = NULL;
120 FILE *blobfile = NULL;
124 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");
205 err = create_object_file(*id, blobfile, repo);
208 if (fd != -1 && close(fd) != 0 && err == NULL)
209 err = got_error_from_errno("close");
210 if (blobfile && fclose(blobfile) != 0 && err == NULL)
211 err = got_error_from_errno("fclose");
219 static const struct got_error *
220 te_mode2str(char *buf, size_t len, struct got_tree_entry *te)
226 * Some Git implementations are picky about modes seen in tree entries.
227 * For best compatibility we normalize the file/directory mode here.
228 * Note that we do not support committing symlinks.
230 if (S_ISREG(te->mode)) {
231 mode = GOT_DEFAULT_FILE_MODE;
232 if (te->mode & (S_IXUSR | S_IXGRP | S_IXOTH))
233 mode |= S_IXUSR | S_IXGRP | S_IXOTH;
234 } else if (got_object_tree_entry_is_submodule(te))
235 mode = S_IFDIR | S_IFLNK;
236 else if (S_ISLNK(te->mode))
237 mode = S_IFLNK; /* Git leaves all the other bits unset. */
238 else if (S_ISDIR(te->mode))
239 mode = S_IFDIR; /* Git leaves all the other bits unset. */
241 return got_error(GOT_ERR_BAD_FILETYPE);
243 ret = snprintf(buf, len, "%o ", mode);
244 if (ret == -1 || ret >= len)
245 return got_error(GOT_ERR_NO_SPACE);
250 * Git expects directory tree entries to be sorted with an imaginary slash
251 * appended to their name, and will break otherwise. Let's be nice.
252 * This function is intended to be used with mergesort(3) to sort an
253 * array of pointers to struct got_tree_entry objects.
256 sort_tree_entries_the_way_git_likes_it(const void *arg1, const void *arg2)
258 struct got_tree_entry * const *te1 = arg1;
259 struct got_tree_entry * const *te2 = arg2;
260 char name1[NAME_MAX + 2];
261 char name2[NAME_MAX + 2];
263 strlcpy(name1, (*te1)->name, sizeof(name1));
264 strlcpy(name2, (*te2)->name, sizeof(name2));
265 if (S_ISDIR((*te1)->mode))
266 strlcat(name1, "/", sizeof(name1));
267 if (S_ISDIR((*te2)->mode))
268 strlcat(name2, "/", sizeof(name2));
269 return strcmp(name1, name2);
272 const struct got_error *
273 got_object_tree_create(struct got_object_id **id,
274 struct got_pathlist_head *paths, int nentries, struct got_repository *repo)
276 const struct got_error *err = NULL;
277 char modebuf[sizeof("100644 ")];
280 size_t headerlen, len = 0, n;
281 FILE *treefile = NULL;
282 struct got_pathlist_entry *pe;
283 struct got_tree_entry **sorted_entries;
284 struct got_tree_entry *te;
291 sorted_entries = calloc(nentries, sizeof(struct got_tree_entry *));
292 if (sorted_entries == NULL)
293 return got_error_from_errno("calloc");
296 TAILQ_FOREACH(pe, paths, entry)
297 sorted_entries[i++] = pe->data;
298 mergesort(sorted_entries, nentries, sizeof(struct got_tree_entry *),
299 sort_tree_entries_the_way_git_likes_it);
301 for (i = 0; i < nentries; i++) {
302 te = sorted_entries[i];
303 err = te_mode2str(modebuf, sizeof(modebuf), te);
306 len += strlen(modebuf) + strlen(te->name) + 1 +
310 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TREE, len) == -1) {
311 err = got_error_from_errno("asprintf");
314 headerlen = strlen(header) + 1;
315 SHA1Update(&sha1_ctx, header, headerlen);
317 treefile = got_opentemp();
318 if (treefile == NULL) {
319 err = got_error_from_errno("got_opentemp");
323 n = fwrite(header, 1, headerlen, treefile);
324 if (n != headerlen) {
325 err = got_ferror(treefile, GOT_ERR_IO);
329 for (i = 0; i < nentries; i++) {
330 te = sorted_entries[i];
331 err = te_mode2str(modebuf, sizeof(modebuf), te);
334 len = strlen(modebuf);
335 n = fwrite(modebuf, 1, len, treefile);
337 err = got_ferror(treefile, GOT_ERR_IO);
340 SHA1Update(&sha1_ctx, modebuf, len);
342 len = strlen(te->name) + 1; /* must include NUL */
343 n = fwrite(te->name, 1, len, treefile);
345 err = got_ferror(treefile, GOT_ERR_IO);
348 SHA1Update(&sha1_ctx, te->name, len);
350 len = SHA1_DIGEST_LENGTH;
351 n = fwrite(te->id.sha1, 1, len, treefile);
353 err = got_ferror(treefile, GOT_ERR_IO);
356 SHA1Update(&sha1_ctx, te->id.sha1, len);
359 *id = malloc(sizeof(**id));
361 err = got_error_from_errno("malloc");
364 SHA1Final((*id)->sha1, &sha1_ctx);
366 if (fflush(treefile) != 0) {
367 err = got_error_from_errno("fflush");
372 err = create_object_file(*id, treefile, repo);
375 free(sorted_entries);
376 if (treefile && fclose(treefile) != 0 && err == NULL)
377 err = got_error_from_errno("fclose");
385 const struct got_error *
386 got_object_commit_create(struct got_object_id **id,
387 struct got_object_id *tree_id, struct got_object_id_queue *parent_ids,
388 int nparents, const char *author, time_t author_time,
389 const char *committer, time_t committer_time,
390 const char *logmsg, struct got_repository *repo)
392 const struct got_error *err = NULL;
394 char *header = NULL, *tree_str = NULL;
395 char *author_str = NULL, *committer_str = NULL;
397 size_t headerlen, len = 0, n;
398 FILE *commitfile = NULL;
399 struct got_object_qid *qid;
406 msg0 = strdup(logmsg);
408 return got_error_from_errno("strdup");
411 while (isspace((unsigned char)msg[0]))
414 while (len > 0 && isspace((unsigned char)msg[len - 1])) {
419 if (asprintf(&author_str, "%s%s %lld +0000\n",
420 GOT_COMMIT_LABEL_AUTHOR, author, author_time) == -1)
421 return got_error_from_errno("asprintf");
423 if (asprintf(&committer_str, "%s%s %lld +0000\n",
424 GOT_COMMIT_LABEL_COMMITTER, committer ? committer : author,
425 committer ? committer_time : author_time)
427 err = got_error_from_errno("asprintf");
431 len = strlen(GOT_COMMIT_LABEL_TREE) + SHA1_DIGEST_STRING_LENGTH +
433 (strlen(GOT_COMMIT_LABEL_PARENT) + SHA1_DIGEST_STRING_LENGTH) +
434 + strlen(author_str) + strlen(committer_str) + 2 + strlen(msg);
436 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_COMMIT, len) == -1) {
437 err = got_error_from_errno("asprintf");
440 headerlen = strlen(header) + 1;
441 SHA1Update(&sha1_ctx, header, headerlen);
443 commitfile = got_opentemp();
444 if (commitfile == NULL) {
445 err = got_error_from_errno("got_opentemp");
449 n = fwrite(header, 1, headerlen, commitfile);
450 if (n != headerlen) {
451 err = got_ferror(commitfile, GOT_ERR_IO);
455 err = got_object_id_str(&id_str, tree_id);
458 if (asprintf(&tree_str, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str)
460 err = got_error_from_errno("asprintf");
463 len = strlen(tree_str);
464 SHA1Update(&sha1_ctx, tree_str, len);
465 n = fwrite(tree_str, 1, len, commitfile);
467 err = got_ferror(commitfile, GOT_ERR_IO);
472 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
473 char *parent_str = NULL;
477 err = got_object_id_str(&id_str, qid->id);
480 if (asprintf(&parent_str, "%s%s\n",
481 GOT_COMMIT_LABEL_PARENT, id_str) == -1) {
482 err = got_error_from_errno("asprintf");
485 len = strlen(parent_str);
486 SHA1Update(&sha1_ctx, parent_str, len);
487 n = fwrite(parent_str, 1, len, commitfile);
489 err = got_ferror(commitfile, GOT_ERR_IO);
497 len = strlen(author_str);
498 SHA1Update(&sha1_ctx, author_str, len);
499 n = fwrite(author_str, 1, len, commitfile);
501 err = got_ferror(commitfile, GOT_ERR_IO);
505 len = strlen(committer_str);
506 SHA1Update(&sha1_ctx, committer_str, len);
507 n = fwrite(committer_str, 1, len, commitfile);
509 err = got_ferror(commitfile, GOT_ERR_IO);
513 SHA1Update(&sha1_ctx, "\n", 1);
514 n = fwrite("\n", 1, 1, commitfile);
516 err = got_ferror(commitfile, GOT_ERR_IO);
521 SHA1Update(&sha1_ctx, msg, len);
522 n = fwrite(msg, 1, len, commitfile);
524 err = got_ferror(commitfile, GOT_ERR_IO);
528 SHA1Update(&sha1_ctx, "\n", 1);
529 n = fwrite("\n", 1, 1, commitfile);
531 err = got_ferror(commitfile, GOT_ERR_IO);
535 *id = malloc(sizeof(**id));
537 err = got_error_from_errno("malloc");
540 SHA1Final((*id)->sha1, &sha1_ctx);
542 if (fflush(commitfile) != 0) {
543 err = got_error_from_errno("fflush");
548 err = create_object_file(*id, commitfile, repo);
555 if (commitfile && fclose(commitfile) != 0 && err == NULL)
556 err = got_error_from_errno("fclose");
564 const struct got_error *
565 got_object_tag_create(struct got_object_id **id,
566 const char *tag_name, struct got_object_id *object_id, const char *tagger,
567 time_t tagger_time, const char *tagmsg, struct got_repository *repo)
569 const struct got_error *err = NULL;
572 char *tag_str = NULL, *tagger_str = NULL;
573 char *id_str = NULL, *obj_str = NULL, *type_str = NULL;
574 size_t headerlen, len = 0, n;
575 FILE *tagfile = NULL;
576 char *msg0 = NULL, *msg;
577 const char *obj_type_str;
584 err = got_object_id_str(&id_str, object_id);
587 if (asprintf(&obj_str, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str) == -1) {
588 err = got_error_from_errno("asprintf");
592 err = got_object_get_type(&obj_type, repo, object_id);
597 case GOT_OBJ_TYPE_BLOB:
598 obj_type_str = GOT_OBJ_LABEL_BLOB;
600 case GOT_OBJ_TYPE_TREE:
601 obj_type_str = GOT_OBJ_LABEL_TREE;
603 case GOT_OBJ_TYPE_COMMIT:
604 obj_type_str = GOT_OBJ_LABEL_COMMIT;
606 case GOT_OBJ_TYPE_TAG:
607 obj_type_str = GOT_OBJ_LABEL_TAG;
610 err = got_error(GOT_ERR_OBJ_TYPE);
614 if (asprintf(&type_str, "%s%s\n", GOT_TAG_LABEL_TYPE,
615 obj_type_str) == -1) {
616 err = got_error_from_errno("asprintf");
620 if (asprintf(&tag_str, "%s%s\n", GOT_TAG_LABEL_TAG, tag_name) == -1) {
621 err = got_error_from_errno("asprintf");
625 if (asprintf(&tagger_str, "%s%s %lld +0000\n",
626 GOT_TAG_LABEL_TAGGER, tagger, tagger_time) == -1)
627 return got_error_from_errno("asprintf");
629 msg0 = strdup(tagmsg);
631 err = got_error_from_errno("strdup");
636 while (isspace((unsigned char)msg[0]))
639 len = strlen(obj_str) + strlen(type_str) + strlen(tag_str) +
640 strlen(tagger_str) + 1 + strlen(msg) + 1;
642 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TAG, len) == -1) {
643 err = got_error_from_errno("asprintf");
647 headerlen = strlen(header) + 1;
648 SHA1Update(&sha1_ctx, header, headerlen);
650 tagfile = got_opentemp();
651 if (tagfile == NULL) {
652 err = got_error_from_errno("got_opentemp");
656 n = fwrite(header, 1, headerlen, tagfile);
657 if (n != headerlen) {
658 err = got_ferror(tagfile, GOT_ERR_IO);
661 len = strlen(obj_str);
662 SHA1Update(&sha1_ctx, obj_str, len);
663 n = fwrite(obj_str, 1, len, tagfile);
665 err = got_ferror(tagfile, GOT_ERR_IO);
668 len = strlen(type_str);
669 SHA1Update(&sha1_ctx, type_str, len);
670 n = fwrite(type_str, 1, len, tagfile);
672 err = got_ferror(tagfile, GOT_ERR_IO);
676 len = strlen(tag_str);
677 SHA1Update(&sha1_ctx, tag_str, len);
678 n = fwrite(tag_str, 1, len, tagfile);
680 err = got_ferror(tagfile, GOT_ERR_IO);
684 len = strlen(tagger_str);
685 SHA1Update(&sha1_ctx, tagger_str, len);
686 n = fwrite(tagger_str, 1, len, tagfile);
688 err = got_ferror(tagfile, GOT_ERR_IO);
692 SHA1Update(&sha1_ctx, "\n", 1);
693 n = fwrite("\n", 1, 1, tagfile);
695 err = got_ferror(tagfile, GOT_ERR_IO);
700 SHA1Update(&sha1_ctx, msg, len);
701 n = fwrite(msg, 1, len, tagfile);
703 err = got_ferror(tagfile, GOT_ERR_IO);
707 SHA1Update(&sha1_ctx, "\n", 1);
708 n = fwrite("\n", 1, 1, tagfile);
710 err = got_ferror(tagfile, GOT_ERR_IO);
714 *id = malloc(sizeof(**id));
716 err = got_error_from_errno("malloc");
719 SHA1Final((*id)->sha1, &sha1_ctx);
721 if (fflush(tagfile) != 0) {
722 err = got_error_from_errno("fflush");
727 err = create_object_file(*id, tagfile, repo);
733 if (tagfile && fclose(tagfile) != 0 && err == NULL)
734 err = got_error_from_errno("fclose");