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 mode2str(char *buf, size_t len, mode_t mode)
208 ret = snprintf(buf, len, "%o ", mode);
209 if (ret == -1 || ret >= len)
210 return got_error(GOT_ERR_NO_SPACE);
215 * Git expects directory tree entries to be sorted with an imaginary slash
216 * appended to their name, and will break otherwise. Let's be nice.
217 * This function is intended to be used with mergesort(3) to sort an
218 * array of pointers to struct got_tree_entry objects.
221 sort_tree_entries_the_way_git_likes_it(const void *arg1, const void *arg2)
223 struct got_tree_entry * const *te1 = arg1;
224 struct got_tree_entry * const *te2 = arg2;
225 char name1[NAME_MAX + 2];
226 char name2[NAME_MAX + 2];
228 strlcpy(name1, (*te1)->name, sizeof(name1));
229 strlcpy(name2, (*te2)->name, sizeof(name2));
230 if (S_ISDIR((*te1)->mode))
231 strlcat(name1, "/", sizeof(name1));
232 if (S_ISDIR((*te2)->mode))
233 strlcat(name2, "/", sizeof(name2));
234 return strcmp(name1, name2);
237 const struct got_error *
238 got_object_tree_create(struct got_object_id **id,
239 struct got_pathlist_head *paths, int nentries, struct got_repository *repo)
241 const struct got_error *err = NULL;
242 char modebuf[sizeof("100644 ")];
245 size_t headerlen, len = 0, n;
246 FILE *treefile = NULL;
247 struct got_pathlist_entry *pe;
248 struct got_tree_entry **sorted_entries;
249 struct got_tree_entry *te;
256 sorted_entries = calloc(nentries, sizeof(struct got_tree_entry *));
257 if (sorted_entries == NULL)
258 return got_error_from_errno("calloc");
261 TAILQ_FOREACH(pe, paths, entry)
262 sorted_entries[i++] = pe->data;
263 mergesort(sorted_entries, nentries, sizeof(struct got_tree_entry *),
264 sort_tree_entries_the_way_git_likes_it);
266 for (i = 0; i < nentries; i++) {
267 te = sorted_entries[i];
268 err = mode2str(modebuf, sizeof(modebuf), te->mode);
271 len += strlen(modebuf) + strlen(te->name) + 1 +
275 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TREE, len) == -1) {
276 err = got_error_from_errno("asprintf");
279 headerlen = strlen(header) + 1;
280 SHA1Update(&sha1_ctx, header, headerlen);
282 treefile = got_opentemp();
283 if (treefile == NULL) {
284 err = got_error_from_errno("got_opentemp");
288 n = fwrite(header, 1, headerlen, treefile);
289 if (n != headerlen) {
290 err = got_ferror(treefile, GOT_ERR_IO);
294 for (i = 0; i < nentries; i++) {
295 te = sorted_entries[i];
296 err = mode2str(modebuf, sizeof(modebuf), te->mode);
299 len = strlen(modebuf);
300 n = fwrite(modebuf, 1, len, treefile);
302 err = got_ferror(treefile, GOT_ERR_IO);
305 SHA1Update(&sha1_ctx, modebuf, len);
307 len = strlen(te->name) + 1; /* must include NUL */
308 n = fwrite(te->name, 1, len, treefile);
310 err = got_ferror(treefile, GOT_ERR_IO);
313 SHA1Update(&sha1_ctx, te->name, len);
315 len = SHA1_DIGEST_LENGTH;
316 n = fwrite(te->id.sha1, 1, len, treefile);
318 err = got_ferror(treefile, GOT_ERR_IO);
321 SHA1Update(&sha1_ctx, te->id.sha1, len);
324 *id = malloc(sizeof(**id));
326 err = got_error_from_errno("malloc");
329 SHA1Final((*id)->sha1, &sha1_ctx);
331 if (fflush(treefile) != 0) {
332 err = got_error_from_errno("fflush");
337 err = create_object_file(*id, treefile, repo);
340 free(sorted_entries);
341 if (treefile && fclose(treefile) != 0 && err == NULL)
342 err = got_error_from_errno("fclose");
350 const struct got_error *
351 got_object_commit_create(struct got_object_id **id,
352 struct got_object_id *tree_id, struct got_object_id_queue *parent_ids,
353 int nparents, const char *author, time_t author_time,
354 const char *committer, time_t committer_time,
355 const char *logmsg, struct got_repository *repo)
357 const struct got_error *err = NULL;
359 char *header = NULL, *tree_str = NULL;
360 char *author_str = NULL, *committer_str = NULL;
362 size_t headerlen, len = 0, n;
363 FILE *commitfile = NULL;
364 struct got_object_qid *qid;
371 msg0 = strdup(logmsg);
373 return got_error_from_errno("strdup");
376 while (isspace((unsigned char)msg[0]))
379 while (len > 0 && isspace((unsigned char)msg[len - 1])) {
384 if (asprintf(&author_str, "%s%s %lld +0000\n",
385 GOT_COMMIT_LABEL_AUTHOR, author, author_time) == -1)
386 return got_error_from_errno("asprintf");
388 if (asprintf(&committer_str, "%s%s %lld +0000\n",
389 GOT_COMMIT_LABEL_COMMITTER, committer ? committer : author,
390 committer ? committer_time : author_time)
392 err = got_error_from_errno("asprintf");
396 len = strlen(GOT_COMMIT_LABEL_TREE) + SHA1_DIGEST_STRING_LENGTH +
398 (strlen(GOT_COMMIT_LABEL_PARENT) + SHA1_DIGEST_STRING_LENGTH) +
399 + strlen(author_str) + strlen(committer_str) + 2 + strlen(msg);
401 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_COMMIT, len) == -1) {
402 err = got_error_from_errno("asprintf");
405 headerlen = strlen(header) + 1;
406 SHA1Update(&sha1_ctx, header, headerlen);
408 commitfile = got_opentemp();
409 if (commitfile == NULL) {
410 err = got_error_from_errno("got_opentemp");
414 n = fwrite(header, 1, headerlen, commitfile);
415 if (n != headerlen) {
416 err = got_ferror(commitfile, GOT_ERR_IO);
420 err = got_object_id_str(&id_str, tree_id);
423 if (asprintf(&tree_str, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str)
425 err = got_error_from_errno("asprintf");
428 len = strlen(tree_str);
429 SHA1Update(&sha1_ctx, tree_str, len);
430 n = fwrite(tree_str, 1, len, commitfile);
432 err = got_ferror(commitfile, GOT_ERR_IO);
437 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
438 char *parent_str = NULL;
442 err = got_object_id_str(&id_str, qid->id);
445 if (asprintf(&parent_str, "%s%s\n",
446 GOT_COMMIT_LABEL_PARENT, id_str) == -1) {
447 err = got_error_from_errno("asprintf");
450 len = strlen(parent_str);
451 SHA1Update(&sha1_ctx, parent_str, len);
452 n = fwrite(parent_str, 1, len, commitfile);
454 err = got_ferror(commitfile, GOT_ERR_IO);
462 len = strlen(author_str);
463 SHA1Update(&sha1_ctx, author_str, len);
464 n = fwrite(author_str, 1, len, commitfile);
466 err = got_ferror(commitfile, GOT_ERR_IO);
470 len = strlen(committer_str);
471 SHA1Update(&sha1_ctx, committer_str, len);
472 n = fwrite(committer_str, 1, len, commitfile);
474 err = got_ferror(commitfile, GOT_ERR_IO);
478 SHA1Update(&sha1_ctx, "\n", 1);
479 n = fwrite("\n", 1, 1, commitfile);
481 err = got_ferror(commitfile, GOT_ERR_IO);
486 SHA1Update(&sha1_ctx, msg, len);
487 n = fwrite(msg, 1, len, commitfile);
489 err = got_ferror(commitfile, GOT_ERR_IO);
493 SHA1Update(&sha1_ctx, "\n", 1);
494 n = fwrite("\n", 1, 1, commitfile);
496 err = got_ferror(commitfile, GOT_ERR_IO);
500 *id = malloc(sizeof(**id));
502 err = got_error_from_errno("malloc");
505 SHA1Final((*id)->sha1, &sha1_ctx);
507 if (fflush(commitfile) != 0) {
508 err = got_error_from_errno("fflush");
513 err = create_object_file(*id, commitfile, repo);
520 if (commitfile && fclose(commitfile) != 0 && err == NULL)
521 err = got_error_from_errno("fclose");
529 const struct got_error *
530 got_object_tag_create(struct got_object_id **id,
531 const char *tag_name, struct got_object_id *object_id, const char *tagger,
532 time_t tagger_time, const char *tagmsg, struct got_repository *repo)
534 const struct got_error *err = NULL;
537 char *tag_str = NULL, *tagger_str = NULL;
538 char *id_str = NULL, *obj_str = NULL, *type_str = NULL;
539 size_t headerlen, len = 0, n;
540 FILE *tagfile = NULL;
541 char *msg0 = NULL, *msg;
542 const char *obj_type_str;
549 err = got_object_id_str(&id_str, object_id);
552 if (asprintf(&obj_str, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str) == -1) {
553 err = got_error_from_errno("asprintf");
557 err = got_object_get_type(&obj_type, repo, object_id);
562 case GOT_OBJ_TYPE_BLOB:
563 obj_type_str = GOT_OBJ_LABEL_BLOB;
565 case GOT_OBJ_TYPE_TREE:
566 obj_type_str = GOT_OBJ_LABEL_TREE;
568 case GOT_OBJ_TYPE_COMMIT:
569 obj_type_str = GOT_OBJ_LABEL_COMMIT;
571 case GOT_OBJ_TYPE_TAG:
572 obj_type_str = GOT_OBJ_LABEL_TAG;
575 err = got_error(GOT_ERR_OBJ_TYPE);
579 if (asprintf(&type_str, "%s%s\n", GOT_TAG_LABEL_TYPE,
580 obj_type_str) == -1) {
581 err = got_error_from_errno("asprintf");
585 if (asprintf(&tag_str, "%s%s\n", GOT_TAG_LABEL_TAG, tag_name) == -1) {
586 err = got_error_from_errno("asprintf");
590 if (asprintf(&tagger_str, "%s%s %lld +0000\n",
591 GOT_TAG_LABEL_TAGGER, tagger, tagger_time) == -1)
592 return got_error_from_errno("asprintf");
594 msg0 = strdup(tagmsg);
596 err = got_error_from_errno("strdup");
601 while (isspace((unsigned char)msg[0]))
604 len = strlen(obj_str) + strlen(type_str) + strlen(tag_str) +
605 strlen(tagger_str) + 1 + strlen(msg) + 1;
607 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TAG, len) == -1) {
608 err = got_error_from_errno("asprintf");
612 headerlen = strlen(header) + 1;
613 SHA1Update(&sha1_ctx, header, headerlen);
615 tagfile = got_opentemp();
616 if (tagfile == NULL) {
617 err = got_error_from_errno("got_opentemp");
621 n = fwrite(header, 1, headerlen, tagfile);
622 if (n != headerlen) {
623 err = got_ferror(tagfile, GOT_ERR_IO);
626 len = strlen(obj_str);
627 SHA1Update(&sha1_ctx, obj_str, len);
628 n = fwrite(obj_str, 1, len, tagfile);
630 err = got_ferror(tagfile, GOT_ERR_IO);
633 len = strlen(type_str);
634 SHA1Update(&sha1_ctx, type_str, len);
635 n = fwrite(type_str, 1, len, tagfile);
637 err = got_ferror(tagfile, GOT_ERR_IO);
641 len = strlen(tag_str);
642 SHA1Update(&sha1_ctx, tag_str, len);
643 n = fwrite(tag_str, 1, len, tagfile);
645 err = got_ferror(tagfile, GOT_ERR_IO);
649 len = strlen(tagger_str);
650 SHA1Update(&sha1_ctx, tagger_str, len);
651 n = fwrite(tagger_str, 1, len, tagfile);
653 err = got_ferror(tagfile, GOT_ERR_IO);
657 SHA1Update(&sha1_ctx, "\n", 1);
658 n = fwrite("\n", 1, 1, tagfile);
660 err = got_ferror(tagfile, GOT_ERR_IO);
665 SHA1Update(&sha1_ctx, msg, len);
666 n = fwrite(msg, 1, len, tagfile);
668 err = got_ferror(tagfile, GOT_ERR_IO);
672 SHA1Update(&sha1_ctx, "\n", 1);
673 n = fwrite("\n", 1, 1, tagfile);
675 err = got_ferror(tagfile, GOT_ERR_IO);
679 *id = malloc(sizeof(**id));
681 err = got_error_from_errno("malloc");
684 SHA1Final((*id)->sha1, &sha1_ctx);
686 if (fflush(tagfile) != 0) {
687 err = got_error_from_errno("fflush");
692 err = create_object_file(*id, tagfile, repo);
698 if (tagfile && fclose(tagfile) != 0 && err == NULL)
699 err = got_error_from_errno("fclose");