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);
132 return got_error_from_errno2("open", ondisk_path);
134 if (fstat(fd, &sb) == -1) {
135 err = got_error_from_errno2("fstat", ondisk_path);
139 if (asprintf(&header, "%s %lld", GOT_OBJ_LABEL_BLOB,
141 err = got_error_from_errno("asprintf");
144 headerlen = strlen(header) + 1;
145 SHA1Update(&sha1_ctx, header, headerlen);
147 blobfile = got_opentemp();
148 if (blobfile == NULL) {
149 err = got_error_from_errno("got_opentemp");
153 n = fwrite(header, 1, headerlen, blobfile);
154 if (n != headerlen) {
155 err = got_ferror(blobfile, GOT_ERR_IO);
162 inlen = read(fd, buf, sizeof(buf));
164 err = got_error_from_errno("read");
169 SHA1Update(&sha1_ctx, buf, inlen);
170 n = fwrite(buf, 1, inlen, blobfile);
172 err = got_ferror(blobfile, GOT_ERR_IO);
177 *id = malloc(sizeof(**id));
179 err = got_error_from_errno("malloc");
182 SHA1Final((*id)->sha1, &sha1_ctx);
184 if (fflush(blobfile) != 0) {
185 err = got_error_from_errno("fflush");
190 err = create_object_file(*id, blobfile, repo);
193 if (fd != -1 && close(fd) != 0 && err == NULL)
194 err = got_error_from_errno("close");
195 if (blobfile && fclose(blobfile) != 0 && err == NULL)
196 err = got_error_from_errno("fclose");
204 static const struct got_error *
205 te_mode2str(char *buf, size_t len, mode_t te_mode)
211 * Some Git implementations are picky about modes seen in tree entries.
212 * For best compatibility we normalize the file/directory mode here.
213 * Note that we do not support committing symlinks or submodules.
215 if (S_ISREG(te_mode))
216 mode = GOT_DEFAULT_FILE_MODE;
217 else if (S_ISDIR(te_mode))
218 mode = GOT_DEFAULT_DIR_MODE;
220 return got_error(GOT_ERR_BAD_FILETYPE);
221 if (te_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
222 mode |= S_IXUSR | S_IXGRP | S_IXOTH;
224 ret = snprintf(buf, len, "%o ", mode);
225 if (ret == -1 || ret >= len)
226 return got_error(GOT_ERR_NO_SPACE);
231 * Git expects directory tree entries to be sorted with an imaginary slash
232 * appended to their name, and will break otherwise. Let's be nice.
233 * This function is intended to be used with mergesort(3) to sort an
234 * array of pointers to struct got_tree_entry objects.
237 sort_tree_entries_the_way_git_likes_it(const void *arg1, const void *arg2)
239 struct got_tree_entry * const *te1 = arg1;
240 struct got_tree_entry * const *te2 = arg2;
241 char name1[NAME_MAX + 2];
242 char name2[NAME_MAX + 2];
244 strlcpy(name1, (*te1)->name, sizeof(name1));
245 strlcpy(name2, (*te2)->name, sizeof(name2));
246 if (S_ISDIR((*te1)->mode))
247 strlcat(name1, "/", sizeof(name1));
248 if (S_ISDIR((*te2)->mode))
249 strlcat(name2, "/", sizeof(name2));
250 return strcmp(name1, name2);
253 const struct got_error *
254 got_object_tree_create(struct got_object_id **id,
255 struct got_pathlist_head *paths, int nentries, struct got_repository *repo)
257 const struct got_error *err = NULL;
258 char modebuf[sizeof("100644 ")];
261 size_t headerlen, len = 0, n;
262 FILE *treefile = NULL;
263 struct got_pathlist_entry *pe;
264 struct got_tree_entry **sorted_entries;
265 struct got_tree_entry *te;
272 sorted_entries = calloc(nentries, sizeof(struct got_tree_entry *));
273 if (sorted_entries == NULL)
274 return got_error_from_errno("calloc");
277 TAILQ_FOREACH(pe, paths, entry)
278 sorted_entries[i++] = pe->data;
279 mergesort(sorted_entries, nentries, sizeof(struct got_tree_entry *),
280 sort_tree_entries_the_way_git_likes_it);
282 for (i = 0; i < nentries; i++) {
283 te = sorted_entries[i];
284 err = te_mode2str(modebuf, sizeof(modebuf), te->mode);
287 len += strlen(modebuf) + strlen(te->name) + 1 +
291 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TREE, len) == -1) {
292 err = got_error_from_errno("asprintf");
295 headerlen = strlen(header) + 1;
296 SHA1Update(&sha1_ctx, header, headerlen);
298 treefile = got_opentemp();
299 if (treefile == NULL) {
300 err = got_error_from_errno("got_opentemp");
304 n = fwrite(header, 1, headerlen, treefile);
305 if (n != headerlen) {
306 err = got_ferror(treefile, GOT_ERR_IO);
310 for (i = 0; i < nentries; i++) {
311 te = sorted_entries[i];
312 err = te_mode2str(modebuf, sizeof(modebuf), te->mode);
315 len = strlen(modebuf);
316 n = fwrite(modebuf, 1, len, treefile);
318 err = got_ferror(treefile, GOT_ERR_IO);
321 SHA1Update(&sha1_ctx, modebuf, len);
323 len = strlen(te->name) + 1; /* must include NUL */
324 n = fwrite(te->name, 1, len, treefile);
326 err = got_ferror(treefile, GOT_ERR_IO);
329 SHA1Update(&sha1_ctx, te->name, len);
331 len = SHA1_DIGEST_LENGTH;
332 n = fwrite(te->id.sha1, 1, len, treefile);
334 err = got_ferror(treefile, GOT_ERR_IO);
337 SHA1Update(&sha1_ctx, te->id.sha1, len);
340 *id = malloc(sizeof(**id));
342 err = got_error_from_errno("malloc");
345 SHA1Final((*id)->sha1, &sha1_ctx);
347 if (fflush(treefile) != 0) {
348 err = got_error_from_errno("fflush");
353 err = create_object_file(*id, treefile, repo);
356 free(sorted_entries);
357 if (treefile && fclose(treefile) != 0 && err == NULL)
358 err = got_error_from_errno("fclose");
366 const struct got_error *
367 got_object_commit_create(struct got_object_id **id,
368 struct got_object_id *tree_id, struct got_object_id_queue *parent_ids,
369 int nparents, const char *author, time_t author_time,
370 const char *committer, time_t committer_time,
371 const char *logmsg, struct got_repository *repo)
373 const struct got_error *err = NULL;
375 char *header = NULL, *tree_str = NULL;
376 char *author_str = NULL, *committer_str = NULL;
378 size_t headerlen, len = 0, n;
379 FILE *commitfile = NULL;
380 struct got_object_qid *qid;
387 msg0 = strdup(logmsg);
389 return got_error_from_errno("strdup");
392 while (isspace((unsigned char)msg[0]))
395 while (len > 0 && isspace((unsigned char)msg[len - 1])) {
400 if (asprintf(&author_str, "%s%s %lld +0000\n",
401 GOT_COMMIT_LABEL_AUTHOR, author, author_time) == -1)
402 return got_error_from_errno("asprintf");
404 if (asprintf(&committer_str, "%s%s %lld +0000\n",
405 GOT_COMMIT_LABEL_COMMITTER, committer ? committer : author,
406 committer ? committer_time : author_time)
408 err = got_error_from_errno("asprintf");
412 len = strlen(GOT_COMMIT_LABEL_TREE) + SHA1_DIGEST_STRING_LENGTH +
414 (strlen(GOT_COMMIT_LABEL_PARENT) + SHA1_DIGEST_STRING_LENGTH) +
415 + strlen(author_str) + strlen(committer_str) + 2 + strlen(msg);
417 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_COMMIT, len) == -1) {
418 err = got_error_from_errno("asprintf");
421 headerlen = strlen(header) + 1;
422 SHA1Update(&sha1_ctx, header, headerlen);
424 commitfile = got_opentemp();
425 if (commitfile == NULL) {
426 err = got_error_from_errno("got_opentemp");
430 n = fwrite(header, 1, headerlen, commitfile);
431 if (n != headerlen) {
432 err = got_ferror(commitfile, GOT_ERR_IO);
436 err = got_object_id_str(&id_str, tree_id);
439 if (asprintf(&tree_str, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str)
441 err = got_error_from_errno("asprintf");
444 len = strlen(tree_str);
445 SHA1Update(&sha1_ctx, tree_str, len);
446 n = fwrite(tree_str, 1, len, commitfile);
448 err = got_ferror(commitfile, GOT_ERR_IO);
453 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
454 char *parent_str = NULL;
458 err = got_object_id_str(&id_str, qid->id);
461 if (asprintf(&parent_str, "%s%s\n",
462 GOT_COMMIT_LABEL_PARENT, id_str) == -1) {
463 err = got_error_from_errno("asprintf");
466 len = strlen(parent_str);
467 SHA1Update(&sha1_ctx, parent_str, len);
468 n = fwrite(parent_str, 1, len, commitfile);
470 err = got_ferror(commitfile, GOT_ERR_IO);
478 len = strlen(author_str);
479 SHA1Update(&sha1_ctx, author_str, len);
480 n = fwrite(author_str, 1, len, commitfile);
482 err = got_ferror(commitfile, GOT_ERR_IO);
486 len = strlen(committer_str);
487 SHA1Update(&sha1_ctx, committer_str, len);
488 n = fwrite(committer_str, 1, len, commitfile);
490 err = got_ferror(commitfile, GOT_ERR_IO);
494 SHA1Update(&sha1_ctx, "\n", 1);
495 n = fwrite("\n", 1, 1, commitfile);
497 err = got_ferror(commitfile, GOT_ERR_IO);
502 SHA1Update(&sha1_ctx, msg, len);
503 n = fwrite(msg, 1, len, commitfile);
505 err = got_ferror(commitfile, GOT_ERR_IO);
509 SHA1Update(&sha1_ctx, "\n", 1);
510 n = fwrite("\n", 1, 1, commitfile);
512 err = got_ferror(commitfile, GOT_ERR_IO);
516 *id = malloc(sizeof(**id));
518 err = got_error_from_errno("malloc");
521 SHA1Final((*id)->sha1, &sha1_ctx);
523 if (fflush(commitfile) != 0) {
524 err = got_error_from_errno("fflush");
529 err = create_object_file(*id, commitfile, repo);
536 if (commitfile && fclose(commitfile) != 0 && err == NULL)
537 err = got_error_from_errno("fclose");
545 const struct got_error *
546 got_object_tag_create(struct got_object_id **id,
547 const char *tag_name, struct got_object_id *object_id, const char *tagger,
548 time_t tagger_time, const char *tagmsg, struct got_repository *repo)
550 const struct got_error *err = NULL;
553 char *tag_str = NULL, *tagger_str = NULL;
554 char *id_str = NULL, *obj_str = NULL, *type_str = NULL;
555 size_t headerlen, len = 0, n;
556 FILE *tagfile = NULL;
557 char *msg0 = NULL, *msg;
558 const char *obj_type_str;
565 err = got_object_id_str(&id_str, object_id);
568 if (asprintf(&obj_str, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str) == -1) {
569 err = got_error_from_errno("asprintf");
573 err = got_object_get_type(&obj_type, repo, object_id);
578 case GOT_OBJ_TYPE_BLOB:
579 obj_type_str = GOT_OBJ_LABEL_BLOB;
581 case GOT_OBJ_TYPE_TREE:
582 obj_type_str = GOT_OBJ_LABEL_TREE;
584 case GOT_OBJ_TYPE_COMMIT:
585 obj_type_str = GOT_OBJ_LABEL_COMMIT;
587 case GOT_OBJ_TYPE_TAG:
588 obj_type_str = GOT_OBJ_LABEL_TAG;
591 err = got_error(GOT_ERR_OBJ_TYPE);
595 if (asprintf(&type_str, "%s%s\n", GOT_TAG_LABEL_TYPE,
596 obj_type_str) == -1) {
597 err = got_error_from_errno("asprintf");
601 if (asprintf(&tag_str, "%s%s\n", GOT_TAG_LABEL_TAG, tag_name) == -1) {
602 err = got_error_from_errno("asprintf");
606 if (asprintf(&tagger_str, "%s%s %lld +0000\n",
607 GOT_TAG_LABEL_TAGGER, tagger, tagger_time) == -1)
608 return got_error_from_errno("asprintf");
610 msg0 = strdup(tagmsg);
612 err = got_error_from_errno("strdup");
617 while (isspace((unsigned char)msg[0]))
620 len = strlen(obj_str) + strlen(type_str) + strlen(tag_str) +
621 strlen(tagger_str) + 1 + strlen(msg) + 1;
623 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TAG, len) == -1) {
624 err = got_error_from_errno("asprintf");
628 headerlen = strlen(header) + 1;
629 SHA1Update(&sha1_ctx, header, headerlen);
631 tagfile = got_opentemp();
632 if (tagfile == NULL) {
633 err = got_error_from_errno("got_opentemp");
637 n = fwrite(header, 1, headerlen, tagfile);
638 if (n != headerlen) {
639 err = got_ferror(tagfile, GOT_ERR_IO);
642 len = strlen(obj_str);
643 SHA1Update(&sha1_ctx, obj_str, len);
644 n = fwrite(obj_str, 1, len, tagfile);
646 err = got_ferror(tagfile, GOT_ERR_IO);
649 len = strlen(type_str);
650 SHA1Update(&sha1_ctx, type_str, len);
651 n = fwrite(type_str, 1, len, tagfile);
653 err = got_ferror(tagfile, GOT_ERR_IO);
657 len = strlen(tag_str);
658 SHA1Update(&sha1_ctx, tag_str, len);
659 n = fwrite(tag_str, 1, len, tagfile);
661 err = got_ferror(tagfile, GOT_ERR_IO);
665 len = strlen(tagger_str);
666 SHA1Update(&sha1_ctx, tagger_str, len);
667 n = fwrite(tagger_str, 1, len, tagfile);
669 err = got_ferror(tagfile, GOT_ERR_IO);
673 SHA1Update(&sha1_ctx, "\n", 1);
674 n = fwrite("\n", 1, 1, tagfile);
676 err = got_ferror(tagfile, GOT_ERR_IO);
681 SHA1Update(&sha1_ctx, msg, len);
682 n = fwrite(msg, 1, len, tagfile);
684 err = got_ferror(tagfile, GOT_ERR_IO);
688 SHA1Update(&sha1_ctx, "\n", 1);
689 n = fwrite("\n", 1, 1, tagfile);
691 err = got_ferror(tagfile, GOT_ERR_IO);
695 *id = malloc(sizeof(**id));
697 err = got_error_from_errno("malloc");
700 SHA1Final((*id)->sha1, &sha1_ctx);
702 if (fflush(tagfile) != 0) {
703 err = got_error_from_errno("fflush");
708 err = create_object_file(*id, tagfile, repo);
714 if (tagfile && fclose(tagfile) != 0 && err == NULL)
715 err = got_error_from_errno("fclose");