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, struct got_tree_entry *te)
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.
215 if (S_ISREG(te->mode)) {
216 mode = GOT_DEFAULT_FILE_MODE;
217 if (te->mode & (S_IXUSR | S_IXGRP | S_IXOTH))
218 mode |= S_IXUSR | S_IXGRP | S_IXOTH;
219 } else if (got_object_tree_entry_is_submodule(te))
220 mode = S_IFDIR | S_IFLNK;
221 else if (S_ISDIR(te->mode))
222 mode = S_IFDIR; /* Git leaves all the other bits unset. */
224 return got_error(GOT_ERR_BAD_FILETYPE);
226 ret = snprintf(buf, len, "%o ", mode);
227 if (ret == -1 || ret >= len)
228 return got_error(GOT_ERR_NO_SPACE);
233 * Git expects directory tree entries to be sorted with an imaginary slash
234 * appended to their name, and will break otherwise. Let's be nice.
235 * This function is intended to be used with mergesort(3) to sort an
236 * array of pointers to struct got_tree_entry objects.
239 sort_tree_entries_the_way_git_likes_it(const void *arg1, const void *arg2)
241 struct got_tree_entry * const *te1 = arg1;
242 struct got_tree_entry * const *te2 = arg2;
243 char name1[NAME_MAX + 2];
244 char name2[NAME_MAX + 2];
246 strlcpy(name1, (*te1)->name, sizeof(name1));
247 strlcpy(name2, (*te2)->name, sizeof(name2));
248 if (S_ISDIR((*te1)->mode))
249 strlcat(name1, "/", sizeof(name1));
250 if (S_ISDIR((*te2)->mode))
251 strlcat(name2, "/", sizeof(name2));
252 return strcmp(name1, name2);
255 const struct got_error *
256 got_object_tree_create(struct got_object_id **id,
257 struct got_pathlist_head *paths, int nentries, struct got_repository *repo)
259 const struct got_error *err = NULL;
260 char modebuf[sizeof("100644 ")];
263 size_t headerlen, len = 0, n;
264 FILE *treefile = NULL;
265 struct got_pathlist_entry *pe;
266 struct got_tree_entry **sorted_entries;
267 struct got_tree_entry *te;
274 sorted_entries = calloc(nentries, sizeof(struct got_tree_entry *));
275 if (sorted_entries == NULL)
276 return got_error_from_errno("calloc");
279 TAILQ_FOREACH(pe, paths, entry)
280 sorted_entries[i++] = pe->data;
281 mergesort(sorted_entries, nentries, sizeof(struct got_tree_entry *),
282 sort_tree_entries_the_way_git_likes_it);
284 for (i = 0; i < nentries; i++) {
285 te = sorted_entries[i];
286 err = te_mode2str(modebuf, sizeof(modebuf), te);
289 len += strlen(modebuf) + strlen(te->name) + 1 +
293 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TREE, len) == -1) {
294 err = got_error_from_errno("asprintf");
297 headerlen = strlen(header) + 1;
298 SHA1Update(&sha1_ctx, header, headerlen);
300 treefile = got_opentemp();
301 if (treefile == NULL) {
302 err = got_error_from_errno("got_opentemp");
306 n = fwrite(header, 1, headerlen, treefile);
307 if (n != headerlen) {
308 err = got_ferror(treefile, GOT_ERR_IO);
312 for (i = 0; i < nentries; i++) {
313 te = sorted_entries[i];
314 err = te_mode2str(modebuf, sizeof(modebuf), te);
317 len = strlen(modebuf);
318 n = fwrite(modebuf, 1, len, treefile);
320 err = got_ferror(treefile, GOT_ERR_IO);
323 SHA1Update(&sha1_ctx, modebuf, len);
325 len = strlen(te->name) + 1; /* must include NUL */
326 n = fwrite(te->name, 1, len, treefile);
328 err = got_ferror(treefile, GOT_ERR_IO);
331 SHA1Update(&sha1_ctx, te->name, len);
333 len = SHA1_DIGEST_LENGTH;
334 n = fwrite(te->id.sha1, 1, len, treefile);
336 err = got_ferror(treefile, GOT_ERR_IO);
339 SHA1Update(&sha1_ctx, te->id.sha1, len);
342 *id = malloc(sizeof(**id));
344 err = got_error_from_errno("malloc");
347 SHA1Final((*id)->sha1, &sha1_ctx);
349 if (fflush(treefile) != 0) {
350 err = got_error_from_errno("fflush");
355 err = create_object_file(*id, treefile, repo);
358 free(sorted_entries);
359 if (treefile && fclose(treefile) != 0 && err == NULL)
360 err = got_error_from_errno("fclose");
368 const struct got_error *
369 got_object_commit_create(struct got_object_id **id,
370 struct got_object_id *tree_id, struct got_object_id_queue *parent_ids,
371 int nparents, const char *author, time_t author_time,
372 const char *committer, time_t committer_time,
373 const char *logmsg, struct got_repository *repo)
375 const struct got_error *err = NULL;
377 char *header = NULL, *tree_str = NULL;
378 char *author_str = NULL, *committer_str = NULL;
380 size_t headerlen, len = 0, n;
381 FILE *commitfile = NULL;
382 struct got_object_qid *qid;
389 msg0 = strdup(logmsg);
391 return got_error_from_errno("strdup");
394 while (isspace((unsigned char)msg[0]))
397 while (len > 0 && isspace((unsigned char)msg[len - 1])) {
402 if (asprintf(&author_str, "%s%s %lld +0000\n",
403 GOT_COMMIT_LABEL_AUTHOR, author, author_time) == -1)
404 return got_error_from_errno("asprintf");
406 if (asprintf(&committer_str, "%s%s %lld +0000\n",
407 GOT_COMMIT_LABEL_COMMITTER, committer ? committer : author,
408 committer ? committer_time : author_time)
410 err = got_error_from_errno("asprintf");
414 len = strlen(GOT_COMMIT_LABEL_TREE) + SHA1_DIGEST_STRING_LENGTH +
416 (strlen(GOT_COMMIT_LABEL_PARENT) + SHA1_DIGEST_STRING_LENGTH) +
417 + strlen(author_str) + strlen(committer_str) + 2 + strlen(msg);
419 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_COMMIT, len) == -1) {
420 err = got_error_from_errno("asprintf");
423 headerlen = strlen(header) + 1;
424 SHA1Update(&sha1_ctx, header, headerlen);
426 commitfile = got_opentemp();
427 if (commitfile == NULL) {
428 err = got_error_from_errno("got_opentemp");
432 n = fwrite(header, 1, headerlen, commitfile);
433 if (n != headerlen) {
434 err = got_ferror(commitfile, GOT_ERR_IO);
438 err = got_object_id_str(&id_str, tree_id);
441 if (asprintf(&tree_str, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str)
443 err = got_error_from_errno("asprintf");
446 len = strlen(tree_str);
447 SHA1Update(&sha1_ctx, tree_str, len);
448 n = fwrite(tree_str, 1, len, commitfile);
450 err = got_ferror(commitfile, GOT_ERR_IO);
455 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
456 char *parent_str = NULL;
460 err = got_object_id_str(&id_str, qid->id);
463 if (asprintf(&parent_str, "%s%s\n",
464 GOT_COMMIT_LABEL_PARENT, id_str) == -1) {
465 err = got_error_from_errno("asprintf");
468 len = strlen(parent_str);
469 SHA1Update(&sha1_ctx, parent_str, len);
470 n = fwrite(parent_str, 1, len, commitfile);
472 err = got_ferror(commitfile, GOT_ERR_IO);
480 len = strlen(author_str);
481 SHA1Update(&sha1_ctx, author_str, len);
482 n = fwrite(author_str, 1, len, commitfile);
484 err = got_ferror(commitfile, GOT_ERR_IO);
488 len = strlen(committer_str);
489 SHA1Update(&sha1_ctx, committer_str, len);
490 n = fwrite(committer_str, 1, len, commitfile);
492 err = got_ferror(commitfile, GOT_ERR_IO);
496 SHA1Update(&sha1_ctx, "\n", 1);
497 n = fwrite("\n", 1, 1, commitfile);
499 err = got_ferror(commitfile, GOT_ERR_IO);
504 SHA1Update(&sha1_ctx, msg, len);
505 n = fwrite(msg, 1, len, commitfile);
507 err = got_ferror(commitfile, GOT_ERR_IO);
511 SHA1Update(&sha1_ctx, "\n", 1);
512 n = fwrite("\n", 1, 1, commitfile);
514 err = got_ferror(commitfile, GOT_ERR_IO);
518 *id = malloc(sizeof(**id));
520 err = got_error_from_errno("malloc");
523 SHA1Final((*id)->sha1, &sha1_ctx);
525 if (fflush(commitfile) != 0) {
526 err = got_error_from_errno("fflush");
531 err = create_object_file(*id, commitfile, repo);
538 if (commitfile && fclose(commitfile) != 0 && err == NULL)
539 err = got_error_from_errno("fclose");
547 const struct got_error *
548 got_object_tag_create(struct got_object_id **id,
549 const char *tag_name, struct got_object_id *object_id, const char *tagger,
550 time_t tagger_time, const char *tagmsg, struct got_repository *repo)
552 const struct got_error *err = NULL;
555 char *tag_str = NULL, *tagger_str = NULL;
556 char *id_str = NULL, *obj_str = NULL, *type_str = NULL;
557 size_t headerlen, len = 0, n;
558 FILE *tagfile = NULL;
559 char *msg0 = NULL, *msg;
560 const char *obj_type_str;
567 err = got_object_id_str(&id_str, object_id);
570 if (asprintf(&obj_str, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str) == -1) {
571 err = got_error_from_errno("asprintf");
575 err = got_object_get_type(&obj_type, repo, object_id);
580 case GOT_OBJ_TYPE_BLOB:
581 obj_type_str = GOT_OBJ_LABEL_BLOB;
583 case GOT_OBJ_TYPE_TREE:
584 obj_type_str = GOT_OBJ_LABEL_TREE;
586 case GOT_OBJ_TYPE_COMMIT:
587 obj_type_str = GOT_OBJ_LABEL_COMMIT;
589 case GOT_OBJ_TYPE_TAG:
590 obj_type_str = GOT_OBJ_LABEL_TAG;
593 err = got_error(GOT_ERR_OBJ_TYPE);
597 if (asprintf(&type_str, "%s%s\n", GOT_TAG_LABEL_TYPE,
598 obj_type_str) == -1) {
599 err = got_error_from_errno("asprintf");
603 if (asprintf(&tag_str, "%s%s\n", GOT_TAG_LABEL_TAG, tag_name) == -1) {
604 err = got_error_from_errno("asprintf");
608 if (asprintf(&tagger_str, "%s%s %lld +0000\n",
609 GOT_TAG_LABEL_TAGGER, tagger, tagger_time) == -1)
610 return got_error_from_errno("asprintf");
612 msg0 = strdup(tagmsg);
614 err = got_error_from_errno("strdup");
619 while (isspace((unsigned char)msg[0]))
622 len = strlen(obj_str) + strlen(type_str) + strlen(tag_str) +
623 strlen(tagger_str) + 1 + strlen(msg) + 1;
625 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TAG, len) == -1) {
626 err = got_error_from_errno("asprintf");
630 headerlen = strlen(header) + 1;
631 SHA1Update(&sha1_ctx, header, headerlen);
633 tagfile = got_opentemp();
634 if (tagfile == NULL) {
635 err = got_error_from_errno("got_opentemp");
639 n = fwrite(header, 1, headerlen, tagfile);
640 if (n != headerlen) {
641 err = got_ferror(tagfile, GOT_ERR_IO);
644 len = strlen(obj_str);
645 SHA1Update(&sha1_ctx, obj_str, len);
646 n = fwrite(obj_str, 1, len, tagfile);
648 err = got_ferror(tagfile, GOT_ERR_IO);
651 len = strlen(type_str);
652 SHA1Update(&sha1_ctx, type_str, len);
653 n = fwrite(type_str, 1, len, tagfile);
655 err = got_ferror(tagfile, GOT_ERR_IO);
659 len = strlen(tag_str);
660 SHA1Update(&sha1_ctx, tag_str, len);
661 n = fwrite(tag_str, 1, len, tagfile);
663 err = got_ferror(tagfile, GOT_ERR_IO);
667 len = strlen(tagger_str);
668 SHA1Update(&sha1_ctx, tagger_str, len);
669 n = fwrite(tagger_str, 1, len, tagfile);
671 err = got_ferror(tagfile, GOT_ERR_IO);
675 SHA1Update(&sha1_ctx, "\n", 1);
676 n = fwrite("\n", 1, 1, tagfile);
678 err = got_ferror(tagfile, GOT_ERR_IO);
683 SHA1Update(&sha1_ctx, msg, len);
684 n = fwrite(msg, 1, len, tagfile);
686 err = got_ferror(tagfile, GOT_ERR_IO);
690 SHA1Update(&sha1_ctx, "\n", 1);
691 n = fwrite("\n", 1, 1, tagfile);
693 err = got_ferror(tagfile, GOT_ERR_IO);
697 *id = malloc(sizeof(**id));
699 err = got_error_from_errno("malloc");
702 SHA1Final((*id)->sha1, &sha1_ctx);
704 if (fflush(tagfile) != 0) {
705 err = got_error_from_errno("fflush");
710 err = create_object_file(*id, tagfile, repo);
716 if (tagfile && fclose(tagfile) != 0 && err == NULL)
717 err = got_error_from_errno("fclose");