tog man page: tweak documentation of vertical/horizontal split for clarity
[got-portable.git] / lib / object_create.c
blobbf1781172c3214c0f7cefd4f0294c39c9e986cc0
1 /*
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>
18 #include <sys/stat.h>
19 #include <sys/wait.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <limits.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdint.h>
29 #include <unistd.h>
30 #include <zlib.h>
32 #include "got_compat.h"
34 #include "got_error.h"
35 #include "got_object.h"
36 #include "got_repository.h"
37 #include "got_opentemp.h"
38 #include "got_path.h"
39 #include "got_sigs.h"
41 #include "got_lib_sha1.h"
42 #include "got_lib_deflate.h"
43 #include "got_lib_delta.h"
44 #include "got_lib_object.h"
45 #include "got_lib_object_parse.h"
46 #include "got_lib_lockfile.h"
48 #include "got_lib_object_create.h"
50 #include "buf.h"
52 #ifndef nitems
53 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
54 #endif
56 static const struct got_error *
57 create_object_file(struct got_object_id *id, FILE *content,
58 off_t content_len, struct got_repository *repo)
60 const struct got_error *err = NULL, *unlock_err = NULL;
61 char *objpath = NULL, *tmppath = NULL;
62 FILE *tmpfile = NULL;
63 struct got_lockfile *lf = NULL;
64 off_t tmplen = 0;
66 err = got_object_get_path(&objpath, id, repo);
67 if (err)
68 return err;
70 err = got_opentemp_named(&tmppath, &tmpfile, objpath);
71 if (err) {
72 char *parent_path;
73 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
74 goto done;
75 err = got_path_dirname(&parent_path, objpath);
76 if (err)
77 goto done;
78 err = got_path_mkdir(parent_path);
79 free(parent_path);
80 if (err)
81 goto done;
82 err = got_opentemp_named(&tmppath, &tmpfile, objpath);
83 if (err)
84 goto done;
87 if (fchmod(fileno(tmpfile), GOT_DEFAULT_FILE_MODE) != 0) {
88 err = got_error_from_errno2("fchmod", tmppath);
89 goto done;
92 err = got_deflate_to_file(&tmplen, content, content_len, tmpfile, NULL);
93 if (err)
94 goto done;
96 err = got_lockfile_lock(&lf, objpath, -1);
97 if (err)
98 goto done;
100 if (rename(tmppath, objpath) != 0) {
101 err = got_error_from_errno3("rename", tmppath, objpath);
102 goto done;
104 free(tmppath);
105 tmppath = NULL;
106 done:
107 free(objpath);
108 if (tmppath) {
109 if (unlink(tmppath) != 0 && err == NULL)
110 err = got_error_from_errno2("unlink", tmppath);
111 free(tmppath);
113 if (tmpfile && fclose(tmpfile) == EOF && err == NULL)
114 err = got_error_from_errno("fclose");
115 if (lf)
116 unlock_err = got_lockfile_unlock(lf, -1);
117 return err ? err : unlock_err;
120 const struct got_error *
121 got_object_blob_file_create(struct got_object_id **id, FILE **blobfile,
122 off_t *blobsize, const char *ondisk_path)
124 const struct got_error *err = NULL;
125 char *header = NULL;
126 int fd = -1;
127 struct stat sb;
128 SHA1_CTX sha1_ctx;
129 size_t headerlen = 0, n;
131 *id = NULL;
132 *blobfile = NULL;
133 *blobsize = 0;
135 SHA1Init(&sha1_ctx);
137 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
138 if (fd == -1) {
139 if (!got_err_open_nofollow_on_symlink())
140 return got_error_from_errno2("open", ondisk_path);
142 if (lstat(ondisk_path, &sb) == -1) {
143 err = got_error_from_errno2("lstat", ondisk_path);
144 goto done;
146 } else if (fstat(fd, &sb) == -1) {
147 err = got_error_from_errno2("fstat", ondisk_path);
148 goto done;
151 if (asprintf(&header, "%s %lld", GOT_OBJ_LABEL_BLOB,
152 (long long)sb.st_size) == -1) {
153 err = got_error_from_errno("asprintf");
154 goto done;
156 headerlen = strlen(header) + 1;
157 SHA1Update(&sha1_ctx, header, headerlen);
159 *blobfile = got_opentemp();
160 if (*blobfile == NULL) {
161 err = got_error_from_errno("got_opentemp");
162 goto done;
165 n = fwrite(header, 1, headerlen, *blobfile);
166 if (n != headerlen) {
167 err = got_ferror(*blobfile, GOT_ERR_IO);
168 goto done;
170 *blobsize += headerlen;
171 for (;;) {
172 char buf[PATH_MAX * 8];
173 ssize_t inlen;
175 if (S_ISLNK(sb.st_mode)) {
176 inlen = readlink(ondisk_path, buf, sizeof(buf));
177 if (inlen == -1) {
178 err = got_error_from_errno("readlink");
179 goto done;
181 } else {
182 inlen = read(fd, buf, sizeof(buf));
183 if (inlen == -1) {
184 err = got_error_from_errno("read");
185 goto done;
188 if (inlen == 0)
189 break; /* EOF */
190 SHA1Update(&sha1_ctx, buf, inlen);
191 n = fwrite(buf, 1, inlen, *blobfile);
192 if (n != inlen) {
193 err = got_ferror(*blobfile, GOT_ERR_IO);
194 goto done;
196 *blobsize += n;
197 if (S_ISLNK(sb.st_mode))
198 break;
201 *id = malloc(sizeof(**id));
202 if (*id == NULL) {
203 err = got_error_from_errno("malloc");
204 goto done;
206 SHA1Final((*id)->sha1, &sha1_ctx);
208 if (fflush(*blobfile) != 0) {
209 err = got_error_from_errno("fflush");
210 goto done;
212 rewind(*blobfile);
213 done:
214 free(header);
215 if (fd != -1 && close(fd) == -1 && err == NULL)
216 err = got_error_from_errno("close");
217 if (err) {
218 free(*id);
219 *id = NULL;
220 if (*blobfile) {
221 fclose(*blobfile);
222 *blobfile = NULL;
225 return err;
228 const struct got_error *
229 got_object_blob_create(struct got_object_id **id, const char *ondisk_path,
230 struct got_repository *repo)
232 const struct got_error *err = NULL;
233 FILE *blobfile = NULL;
234 off_t blobsize;
236 err = got_object_blob_file_create(id, &blobfile, &blobsize,
237 ondisk_path);
238 if (err)
239 return err;
241 err = create_object_file(*id, blobfile, blobsize, repo);
242 if (fclose(blobfile) == EOF && err == NULL)
243 err = got_error_from_errno("fclose");
244 if (err) {
245 free(*id);
246 *id = NULL;
248 return err;
251 static const struct got_error *
252 te_mode2str(char *buf, size_t len, struct got_tree_entry *te)
254 int ret;
255 mode_t mode;
258 * Some Git implementations are picky about modes seen in tree entries.
259 * For best compatibility we normalize the file/directory mode here.
261 if (S_ISREG(te->mode)) {
262 mode = GOT_DEFAULT_FILE_MODE;
263 if (te->mode & (S_IXUSR | S_IXGRP | S_IXOTH))
264 mode |= S_IXUSR | S_IXGRP | S_IXOTH;
265 } else if (got_object_tree_entry_is_submodule(te))
266 mode = S_IFDIR | S_IFLNK;
267 else if (S_ISLNK(te->mode))
268 mode = S_IFLNK; /* Git leaves all the other bits unset. */
269 else if (S_ISDIR(te->mode))
270 mode = S_IFDIR; /* Git leaves all the other bits unset. */
271 else
272 return got_error(GOT_ERR_BAD_FILETYPE);
274 ret = snprintf(buf, len, "%o ", mode);
275 if (ret == -1 || ret >= len)
276 return got_error(GOT_ERR_NO_SPACE);
277 return NULL;
281 * Git expects directory tree entries to be sorted with an imaginary slash
282 * appended to their name, and will break otherwise. Let's be nice.
283 * This function is intended to be used with mergesort(3) to sort an
284 * array of pointers to struct got_tree_entry objects.
286 static int
287 sort_tree_entries_the_way_git_likes_it(const void *arg1, const void *arg2)
289 struct got_tree_entry * const *te1 = arg1;
290 struct got_tree_entry * const *te2 = arg2;
291 char name1[NAME_MAX + 2];
292 char name2[NAME_MAX + 2];
294 strlcpy(name1, (*te1)->name, sizeof(name1));
295 strlcpy(name2, (*te2)->name, sizeof(name2));
296 if (S_ISDIR((*te1)->mode))
297 strlcat(name1, "/", sizeof(name1));
298 if (S_ISDIR((*te2)->mode))
299 strlcat(name2, "/", sizeof(name2));
300 return strcmp(name1, name2);
303 const struct got_error *
304 got_object_tree_create(struct got_object_id **id,
305 struct got_pathlist_head *paths, int nentries, struct got_repository *repo)
307 const struct got_error *err = NULL;
308 char modebuf[sizeof("100644 ")];
309 SHA1_CTX sha1_ctx;
310 char *header = NULL;
311 size_t headerlen, len = 0, n;
312 FILE *treefile = NULL;
313 off_t treesize = 0;
314 struct got_pathlist_entry *pe;
315 struct got_tree_entry **sorted_entries;
316 struct got_tree_entry *te;
317 int i;
319 *id = NULL;
321 SHA1Init(&sha1_ctx);
323 sorted_entries = calloc(nentries, sizeof(struct got_tree_entry *));
324 if (sorted_entries == NULL)
325 return got_error_from_errno("calloc");
327 i = 0;
328 TAILQ_FOREACH(pe, paths, entry)
329 sorted_entries[i++] = pe->data;
330 mergesort(sorted_entries, nentries, sizeof(struct got_tree_entry *),
331 sort_tree_entries_the_way_git_likes_it);
333 for (i = 0; i < nentries; i++) {
334 te = sorted_entries[i];
335 err = te_mode2str(modebuf, sizeof(modebuf), te);
336 if (err)
337 goto done;
338 len += strlen(modebuf) + strlen(te->name) + 1 +
339 SHA1_DIGEST_LENGTH;
342 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TREE, len) == -1) {
343 err = got_error_from_errno("asprintf");
344 goto done;
346 headerlen = strlen(header) + 1;
347 SHA1Update(&sha1_ctx, header, headerlen);
349 treefile = got_opentemp();
350 if (treefile == NULL) {
351 err = got_error_from_errno("got_opentemp");
352 goto done;
355 n = fwrite(header, 1, headerlen, treefile);
356 if (n != headerlen) {
357 err = got_ferror(treefile, GOT_ERR_IO);
358 goto done;
360 treesize += headerlen;
362 for (i = 0; i < nentries; i++) {
363 te = sorted_entries[i];
364 err = te_mode2str(modebuf, sizeof(modebuf), te);
365 if (err)
366 goto done;
367 len = strlen(modebuf);
368 n = fwrite(modebuf, 1, len, treefile);
369 if (n != len) {
370 err = got_ferror(treefile, GOT_ERR_IO);
371 goto done;
373 SHA1Update(&sha1_ctx, modebuf, len);
374 treesize += n;
376 len = strlen(te->name) + 1; /* must include NUL */
377 n = fwrite(te->name, 1, len, treefile);
378 if (n != len) {
379 err = got_ferror(treefile, GOT_ERR_IO);
380 goto done;
382 SHA1Update(&sha1_ctx, te->name, len);
383 treesize += n;
385 len = SHA1_DIGEST_LENGTH;
386 n = fwrite(te->id.sha1, 1, len, treefile);
387 if (n != len) {
388 err = got_ferror(treefile, GOT_ERR_IO);
389 goto done;
391 SHA1Update(&sha1_ctx, te->id.sha1, len);
392 treesize += n;
395 *id = malloc(sizeof(**id));
396 if (*id == NULL) {
397 err = got_error_from_errno("malloc");
398 goto done;
400 SHA1Final((*id)->sha1, &sha1_ctx);
402 if (fflush(treefile) != 0) {
403 err = got_error_from_errno("fflush");
404 goto done;
406 rewind(treefile);
408 err = create_object_file(*id, treefile, treesize, repo);
409 done:
410 free(header);
411 free(sorted_entries);
412 if (treefile && fclose(treefile) == EOF && err == NULL)
413 err = got_error_from_errno("fclose");
414 if (err) {
415 free(*id);
416 *id = NULL;
418 return err;
421 const struct got_error *
422 got_object_commit_create(struct got_object_id **id,
423 struct got_object_id *tree_id, struct got_object_id_queue *parent_ids,
424 int nparents, const char *author, time_t author_time,
425 const char *committer, time_t committer_time,
426 const char *logmsg, struct got_repository *repo)
428 const struct got_error *err = NULL;
429 SHA1_CTX sha1_ctx;
430 char *header = NULL, *tree_str = NULL;
431 char *author_str = NULL, *committer_str = NULL;
432 char *id_str = NULL;
433 size_t headerlen, len = 0, n;
434 FILE *commitfile = NULL;
435 off_t commitsize = 0;
436 struct got_object_qid *qid;
437 char *msg0, *msg;
439 *id = NULL;
441 SHA1Init(&sha1_ctx);
443 msg0 = strdup(logmsg);
444 if (msg0 == NULL)
445 return got_error_from_errno("strdup");
446 msg = msg0;
448 while (isspace((unsigned char)msg[0]))
449 msg++;
450 len = strlen(msg);
451 while (len > 0 && isspace((unsigned char)msg[len - 1])) {
452 msg[len - 1] = '\0';
453 len--;
456 if (asprintf(&author_str, "%s%s %lld +0000\n",
457 GOT_COMMIT_LABEL_AUTHOR, author, (long long)author_time) == -1)
458 return got_error_from_errno("asprintf");
460 if (asprintf(&committer_str, "%s%s %lld +0000\n",
461 GOT_COMMIT_LABEL_COMMITTER, committer ? committer : author,
462 (long long)(committer ? committer_time : author_time))
463 == -1) {
464 err = got_error_from_errno("asprintf");
465 goto done;
468 len = strlen(GOT_COMMIT_LABEL_TREE) + SHA1_DIGEST_STRING_LENGTH +
469 nparents *
470 (strlen(GOT_COMMIT_LABEL_PARENT) + SHA1_DIGEST_STRING_LENGTH) +
471 + strlen(author_str) + strlen(committer_str) + 2 + strlen(msg);
473 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_COMMIT, len) == -1) {
474 err = got_error_from_errno("asprintf");
475 goto done;
477 headerlen = strlen(header) + 1;
478 SHA1Update(&sha1_ctx, header, headerlen);
480 commitfile = got_opentemp();
481 if (commitfile == NULL) {
482 err = got_error_from_errno("got_opentemp");
483 goto done;
486 n = fwrite(header, 1, headerlen, commitfile);
487 if (n != headerlen) {
488 err = got_ferror(commitfile, GOT_ERR_IO);
489 goto done;
491 commitsize += headerlen;
493 err = got_object_id_str(&id_str, tree_id);
494 if (err)
495 goto done;
496 if (asprintf(&tree_str, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str)
497 == -1) {
498 err = got_error_from_errno("asprintf");
499 goto done;
501 len = strlen(tree_str);
502 SHA1Update(&sha1_ctx, tree_str, len);
503 n = fwrite(tree_str, 1, len, commitfile);
504 if (n != len) {
505 err = got_ferror(commitfile, GOT_ERR_IO);
506 goto done;
508 commitsize += n;
510 if (parent_ids) {
511 free(id_str);
512 id_str = NULL;
513 STAILQ_FOREACH(qid, parent_ids, entry) {
514 char *parent_str = NULL;
516 err = got_object_id_str(&id_str, &qid->id);
517 if (err)
518 goto done;
519 if (asprintf(&parent_str, "%s%s\n",
520 GOT_COMMIT_LABEL_PARENT, id_str) == -1) {
521 err = got_error_from_errno("asprintf");
522 goto done;
524 len = strlen(parent_str);
525 SHA1Update(&sha1_ctx, parent_str, len);
526 n = fwrite(parent_str, 1, len, commitfile);
527 if (n != len) {
528 err = got_ferror(commitfile, GOT_ERR_IO);
529 free(parent_str);
530 goto done;
532 commitsize += n;
533 free(parent_str);
534 free(id_str);
535 id_str = NULL;
539 len = strlen(author_str);
540 SHA1Update(&sha1_ctx, author_str, len);
541 n = fwrite(author_str, 1, len, commitfile);
542 if (n != len) {
543 err = got_ferror(commitfile, GOT_ERR_IO);
544 goto done;
546 commitsize += n;
548 len = strlen(committer_str);
549 SHA1Update(&sha1_ctx, committer_str, len);
550 n = fwrite(committer_str, 1, len, commitfile);
551 if (n != len) {
552 err = got_ferror(commitfile, GOT_ERR_IO);
553 goto done;
555 commitsize += n;
557 SHA1Update(&sha1_ctx, "\n", 1);
558 n = fwrite("\n", 1, 1, commitfile);
559 if (n != 1) {
560 err = got_ferror(commitfile, GOT_ERR_IO);
561 goto done;
563 commitsize += n;
565 len = strlen(msg);
566 SHA1Update(&sha1_ctx, msg, len);
567 n = fwrite(msg, 1, len, commitfile);
568 if (n != len) {
569 err = got_ferror(commitfile, GOT_ERR_IO);
570 goto done;
572 commitsize += n;
574 SHA1Update(&sha1_ctx, "\n", 1);
575 n = fwrite("\n", 1, 1, commitfile);
576 if (n != 1) {
577 err = got_ferror(commitfile, GOT_ERR_IO);
578 goto done;
580 commitsize += n;
582 *id = malloc(sizeof(**id));
583 if (*id == NULL) {
584 err = got_error_from_errno("malloc");
585 goto done;
587 SHA1Final((*id)->sha1, &sha1_ctx);
589 if (fflush(commitfile) != 0) {
590 err = got_error_from_errno("fflush");
591 goto done;
593 rewind(commitfile);
595 err = create_object_file(*id, commitfile, commitsize, repo);
596 done:
597 free(id_str);
598 free(msg0);
599 free(header);
600 free(tree_str);
601 free(author_str);
602 free(committer_str);
603 if (commitfile && fclose(commitfile) == EOF && err == NULL)
604 err = got_error_from_errno("fclose");
605 if (err) {
606 free(*id);
607 *id = NULL;
609 return err;
612 const struct got_error *
613 got_object_tag_create(struct got_object_id **id,
614 const char *tag_name, struct got_object_id *object_id, const char *tagger,
615 time_t tagger_time, const char *tagmsg, const char *signer_id,
616 struct got_repository *repo, int verbosity)
618 const struct got_error *err = NULL;
619 SHA1_CTX sha1_ctx;
620 char *header = NULL;
621 char *tag_str = NULL, *tagger_str = NULL;
622 char *id_str = NULL, *obj_str = NULL, *type_str = NULL;
623 size_t headerlen, len = 0, sig_len = 0, n;
624 FILE *tagfile = NULL;
625 off_t tagsize = 0;
626 char *msg0 = NULL, *msg;
627 const char *obj_type_str;
628 int obj_type;
629 BUF *buf = NULL;
631 *id = NULL;
633 SHA1Init(&sha1_ctx);
635 err = got_object_id_str(&id_str, object_id);
636 if (err)
637 goto done;
638 if (asprintf(&obj_str, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str) == -1) {
639 err = got_error_from_errno("asprintf");
640 goto done;
643 err = got_object_get_type(&obj_type, repo, object_id);
644 if (err)
645 goto done;
647 switch (obj_type) {
648 case GOT_OBJ_TYPE_BLOB:
649 obj_type_str = GOT_OBJ_LABEL_BLOB;
650 break;
651 case GOT_OBJ_TYPE_TREE:
652 obj_type_str = GOT_OBJ_LABEL_TREE;
653 break;
654 case GOT_OBJ_TYPE_COMMIT:
655 obj_type_str = GOT_OBJ_LABEL_COMMIT;
656 break;
657 case GOT_OBJ_TYPE_TAG:
658 obj_type_str = GOT_OBJ_LABEL_TAG;
659 break;
660 default:
661 err = got_error(GOT_ERR_OBJ_TYPE);
662 goto done;
665 if (asprintf(&type_str, "%s%s\n", GOT_TAG_LABEL_TYPE,
666 obj_type_str) == -1) {
667 err = got_error_from_errno("asprintf");
668 goto done;
671 if (asprintf(&tag_str, "%s%s\n", GOT_TAG_LABEL_TAG, tag_name) == -1) {
672 err = got_error_from_errno("asprintf");
673 goto done;
676 if (asprintf(&tagger_str, "%s%s %lld +0000\n",
677 GOT_TAG_LABEL_TAGGER, tagger, (long long)tagger_time) == -1)
678 return got_error_from_errno("asprintf");
680 msg0 = strdup(tagmsg);
681 if (msg0 == NULL) {
682 err = got_error_from_errno("strdup");
683 goto done;
685 msg = msg0;
687 while (isspace((unsigned char)msg[0]))
688 msg++;
690 if (signer_id) {
691 FILE *out;
692 pid_t pid;
693 size_t len;
694 int in_fd, out_fd;
695 int status;
697 err = buf_alloc(&buf, 0);
698 if (err)
699 goto done;
701 /* signed message */
702 err = buf_puts(&len, buf, obj_str);
703 if (err)
704 goto done;
705 err = buf_puts(&len, buf, type_str);
706 if (err)
707 goto done;
708 err = buf_puts(&len, buf, tag_str);
709 if (err)
710 goto done;
711 err = buf_puts(&len, buf, tagger_str);
712 if (err)
713 goto done;
714 err = buf_putc(buf, '\n');
715 if (err)
716 goto done;
717 err = buf_puts(&len, buf, msg);
718 if (err)
719 goto done;
720 err = buf_putc(buf, '\n');
721 if (err)
722 goto done;
724 err = got_sigs_sign_tag_ssh(&pid, &in_fd, &out_fd, signer_id,
725 verbosity);
726 if (err)
727 goto done;
728 if (buf_write_fd(buf, in_fd) == -1) {
729 err = got_error_from_errno("write");
730 goto done;
732 if (close(in_fd) == -1) {
733 err = got_error_from_errno("close");
734 goto done;
737 if (waitpid(pid, &status, 0) == -1) {
738 err = got_error_from_errno("waitpid");
739 goto done;
741 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
742 err = got_error(GOT_ERR_SIGNING_TAG);
743 goto done;
746 out = fdopen(out_fd, "r");
747 if (out == NULL) {
748 err = got_error_from_errno("fdopen");
749 goto done;
751 buf_empty(buf);
752 err = buf_load(&buf, out);
753 if (err)
754 goto done;
755 sig_len = buf_len(buf) + 1;
756 err = buf_putc(buf, '\0');
757 if (err)
758 goto done;
759 if (close(out_fd) == -1) {
760 err = got_error_from_errno("close");
761 goto done;
765 len = strlen(obj_str) + strlen(type_str) + strlen(tag_str) +
766 strlen(tagger_str) + 1 + strlen(msg) + 1 + sig_len;
767 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TAG, len) == -1) {
768 err = got_error_from_errno("asprintf");
769 goto done;
772 headerlen = strlen(header) + 1;
773 SHA1Update(&sha1_ctx, header, headerlen);
775 tagfile = got_opentemp();
776 if (tagfile == NULL) {
777 err = got_error_from_errno("got_opentemp");
778 goto done;
781 n = fwrite(header, 1, headerlen, tagfile);
782 if (n != headerlen) {
783 err = got_ferror(tagfile, GOT_ERR_IO);
784 goto done;
786 tagsize += headerlen;
787 len = strlen(obj_str);
788 SHA1Update(&sha1_ctx, obj_str, len);
789 n = fwrite(obj_str, 1, len, tagfile);
790 if (n != len) {
791 err = got_ferror(tagfile, GOT_ERR_IO);
792 goto done;
794 tagsize += n;
795 len = strlen(type_str);
796 SHA1Update(&sha1_ctx, type_str, len);
797 n = fwrite(type_str, 1, len, tagfile);
798 if (n != len) {
799 err = got_ferror(tagfile, GOT_ERR_IO);
800 goto done;
802 tagsize += n;
804 len = strlen(tag_str);
805 SHA1Update(&sha1_ctx, tag_str, len);
806 n = fwrite(tag_str, 1, len, tagfile);
807 if (n != len) {
808 err = got_ferror(tagfile, GOT_ERR_IO);
809 goto done;
811 tagsize += n;
813 len = strlen(tagger_str);
814 SHA1Update(&sha1_ctx, tagger_str, len);
815 n = fwrite(tagger_str, 1, len, tagfile);
816 if (n != len) {
817 err = got_ferror(tagfile, GOT_ERR_IO);
818 goto done;
820 tagsize += n;
822 SHA1Update(&sha1_ctx, "\n", 1);
823 n = fwrite("\n", 1, 1, tagfile);
824 if (n != 1) {
825 err = got_ferror(tagfile, GOT_ERR_IO);
826 goto done;
828 tagsize += n;
830 len = strlen(msg);
831 SHA1Update(&sha1_ctx, msg, len);
832 n = fwrite(msg, 1, len, tagfile);
833 if (n != len) {
834 err = got_ferror(tagfile, GOT_ERR_IO);
835 goto done;
837 tagsize += n;
839 SHA1Update(&sha1_ctx, "\n", 1);
840 n = fwrite("\n", 1, 1, tagfile);
841 if (n != 1) {
842 err = got_ferror(tagfile, GOT_ERR_IO);
843 goto done;
845 tagsize += n;
847 if (signer_id && buf_len(buf) > 0) {
848 len = buf_len(buf);
849 SHA1Update(&sha1_ctx, buf_get(buf), len);
850 n = fwrite(buf_get(buf), 1, len, tagfile);
851 if (n != len) {
852 err = got_ferror(tagfile, GOT_ERR_IO);
853 goto done;
855 tagsize += n;
858 *id = malloc(sizeof(**id));
859 if (*id == NULL) {
860 err = got_error_from_errno("malloc");
861 goto done;
863 SHA1Final((*id)->sha1, &sha1_ctx);
865 if (fflush(tagfile) != 0) {
866 err = got_error_from_errno("fflush");
867 goto done;
869 rewind(tagfile);
871 err = create_object_file(*id, tagfile, tagsize, repo);
872 done:
873 free(msg0);
874 free(header);
875 free(obj_str);
876 free(tagger_str);
877 if (buf)
878 buf_release(buf);
879 if (tagfile && fclose(tagfile) == EOF && err == NULL)
880 err = got_error_from_errno("fclose");
881 if (err) {
882 free(*id);
883 *id = NULL;
885 return err;