Blob


1 /*
2 * Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/queue.h>
20 #include <sys/wait.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <limits.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdint.h>
30 #include <sha1.h>
31 #include <unistd.h>
32 #include <zlib.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)
59 {
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;
85 }
87 if (fchmod(fileno(tmpfile), GOT_DEFAULT_FILE_MODE) != 0) {
88 err = got_error_from_errno2("fchmod", tmppath);
89 goto done;
90 }
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;
257 /*
258 * Some Git implementations are picky about modes seen in tree entries.
259 * For best compatibility we normalize the file/directory mode here.
260 */
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 < 0 || (size_t)ret >= len)
276 return got_error(GOT_ERR_NO_SPACE);
277 return NULL;
280 /*
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.
285 */
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 err = got_error_from_errno("asprintf");
459 goto done;
462 if (asprintf(&committer_str, "%s%s %lld +0000\n",
463 GOT_COMMIT_LABEL_COMMITTER, committer ? committer : author,
464 (long long)(committer ? committer_time : author_time))
465 == -1) {
466 err = got_error_from_errno("asprintf");
467 goto done;
470 len = strlen(GOT_COMMIT_LABEL_TREE) + SHA1_DIGEST_STRING_LENGTH +
471 nparents *
472 (strlen(GOT_COMMIT_LABEL_PARENT) + SHA1_DIGEST_STRING_LENGTH) +
473 + strlen(author_str) + strlen(committer_str) + 2 + strlen(msg);
475 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_COMMIT, len) == -1) {
476 err = got_error_from_errno("asprintf");
477 goto done;
479 headerlen = strlen(header) + 1;
480 SHA1Update(&sha1_ctx, header, headerlen);
482 commitfile = got_opentemp();
483 if (commitfile == NULL) {
484 err = got_error_from_errno("got_opentemp");
485 goto done;
488 n = fwrite(header, 1, headerlen, commitfile);
489 if (n != headerlen) {
490 err = got_ferror(commitfile, GOT_ERR_IO);
491 goto done;
493 commitsize += headerlen;
495 err = got_object_id_str(&id_str, tree_id);
496 if (err)
497 goto done;
498 if (asprintf(&tree_str, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str)
499 == -1) {
500 err = got_error_from_errno("asprintf");
501 goto done;
503 len = strlen(tree_str);
504 SHA1Update(&sha1_ctx, tree_str, len);
505 n = fwrite(tree_str, 1, len, commitfile);
506 if (n != len) {
507 err = got_ferror(commitfile, GOT_ERR_IO);
508 goto done;
510 commitsize += n;
512 if (parent_ids) {
513 free(id_str);
514 id_str = NULL;
515 STAILQ_FOREACH(qid, parent_ids, entry) {
516 char *parent_str = NULL;
518 err = got_object_id_str(&id_str, &qid->id);
519 if (err)
520 goto done;
521 if (asprintf(&parent_str, "%s%s\n",
522 GOT_COMMIT_LABEL_PARENT, id_str) == -1) {
523 err = got_error_from_errno("asprintf");
524 goto done;
526 len = strlen(parent_str);
527 SHA1Update(&sha1_ctx, parent_str, len);
528 n = fwrite(parent_str, 1, len, commitfile);
529 if (n != len) {
530 err = got_ferror(commitfile, GOT_ERR_IO);
531 free(parent_str);
532 goto done;
534 commitsize += n;
535 free(parent_str);
536 free(id_str);
537 id_str = NULL;
541 len = strlen(author_str);
542 SHA1Update(&sha1_ctx, author_str, len);
543 n = fwrite(author_str, 1, len, commitfile);
544 if (n != len) {
545 err = got_ferror(commitfile, GOT_ERR_IO);
546 goto done;
548 commitsize += n;
550 len = strlen(committer_str);
551 SHA1Update(&sha1_ctx, committer_str, len);
552 n = fwrite(committer_str, 1, len, commitfile);
553 if (n != len) {
554 err = got_ferror(commitfile, GOT_ERR_IO);
555 goto done;
557 commitsize += n;
559 SHA1Update(&sha1_ctx, "\n", 1);
560 n = fwrite("\n", 1, 1, commitfile);
561 if (n != 1) {
562 err = got_ferror(commitfile, GOT_ERR_IO);
563 goto done;
565 commitsize += n;
567 len = strlen(msg);
568 SHA1Update(&sha1_ctx, msg, len);
569 n = fwrite(msg, 1, len, commitfile);
570 if (n != len) {
571 err = got_ferror(commitfile, GOT_ERR_IO);
572 goto done;
574 commitsize += n;
576 SHA1Update(&sha1_ctx, "\n", 1);
577 n = fwrite("\n", 1, 1, commitfile);
578 if (n != 1) {
579 err = got_ferror(commitfile, GOT_ERR_IO);
580 goto done;
582 commitsize += n;
584 *id = malloc(sizeof(**id));
585 if (*id == NULL) {
586 err = got_error_from_errno("malloc");
587 goto done;
589 SHA1Final((*id)->sha1, &sha1_ctx);
591 if (fflush(commitfile) != 0) {
592 err = got_error_from_errno("fflush");
593 goto done;
595 rewind(commitfile);
597 err = create_object_file(*id, commitfile, commitsize, repo);
598 done:
599 free(id_str);
600 free(msg0);
601 free(header);
602 free(tree_str);
603 free(author_str);
604 free(committer_str);
605 if (commitfile && fclose(commitfile) == EOF && err == NULL)
606 err = got_error_from_errno("fclose");
607 if (err) {
608 free(*id);
609 *id = NULL;
611 return err;
614 const struct got_error *
615 got_object_tag_create(struct got_object_id **id,
616 const char *tag_name, struct got_object_id *object_id, const char *tagger,
617 time_t tagger_time, const char *tagmsg, const char *signer_id,
618 struct got_repository *repo, int verbosity)
620 const struct got_error *err = NULL;
621 SHA1_CTX sha1_ctx;
622 char *header = NULL;
623 char *tag_str = NULL, *tagger_str = NULL;
624 char *id_str = NULL, *obj_str = NULL, *type_str = NULL;
625 size_t headerlen, len = 0, sig_len = 0, n;
626 FILE *tagfile = NULL;
627 off_t tagsize = 0;
628 char *msg0 = NULL, *msg;
629 const char *obj_type_str;
630 int obj_type;
631 BUF *buf = NULL;
633 *id = NULL;
635 SHA1Init(&sha1_ctx);
637 err = got_object_id_str(&id_str, object_id);
638 if (err)
639 goto done;
640 if (asprintf(&obj_str, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str) == -1) {
641 err = got_error_from_errno("asprintf");
642 goto done;
645 err = got_object_get_type(&obj_type, repo, object_id);
646 if (err)
647 goto done;
649 switch (obj_type) {
650 case GOT_OBJ_TYPE_BLOB:
651 obj_type_str = GOT_OBJ_LABEL_BLOB;
652 break;
653 case GOT_OBJ_TYPE_TREE:
654 obj_type_str = GOT_OBJ_LABEL_TREE;
655 break;
656 case GOT_OBJ_TYPE_COMMIT:
657 obj_type_str = GOT_OBJ_LABEL_COMMIT;
658 break;
659 case GOT_OBJ_TYPE_TAG:
660 obj_type_str = GOT_OBJ_LABEL_TAG;
661 break;
662 default:
663 err = got_error(GOT_ERR_OBJ_TYPE);
664 goto done;
667 if (asprintf(&type_str, "%s%s\n", GOT_TAG_LABEL_TYPE,
668 obj_type_str) == -1) {
669 err = got_error_from_errno("asprintf");
670 goto done;
673 if (asprintf(&tag_str, "%s%s\n", GOT_TAG_LABEL_TAG, tag_name) == -1) {
674 err = got_error_from_errno("asprintf");
675 goto done;
678 if (asprintf(&tagger_str, "%s%s %lld +0000\n",
679 GOT_TAG_LABEL_TAGGER, tagger, (long long)tagger_time) == -1)
680 return got_error_from_errno("asprintf");
682 msg0 = strdup(tagmsg);
683 if (msg0 == NULL) {
684 err = got_error_from_errno("strdup");
685 goto done;
687 msg = msg0;
689 while (isspace((unsigned char)msg[0]))
690 msg++;
692 if (signer_id) {
693 pid_t pid;
694 size_t len;
695 int in_fd, out_fd;
696 int status;
698 err = buf_alloc(&buf, 0);
699 if (err)
700 goto done;
702 /* signed message */
703 err = buf_puts(&len, buf, obj_str);
704 if (err)
705 goto done;
706 err = buf_puts(&len, buf, type_str);
707 if (err)
708 goto done;
709 err = buf_puts(&len, buf, tag_str);
710 if (err)
711 goto done;
712 err = buf_puts(&len, buf, tagger_str);
713 if (err)
714 goto done;
715 err = buf_putc(buf, '\n');
716 if (err)
717 goto done;
718 err = buf_puts(&len, buf, msg);
719 if (err)
720 goto done;
721 err = buf_putc(buf, '\n');
722 if (err)
723 goto done;
725 err = got_sigs_sign_tag_ssh(&pid, &in_fd, &out_fd, signer_id,
726 verbosity);
727 if (err)
728 goto done;
729 if (buf_write_fd(buf, in_fd) == -1) {
730 err = got_error_from_errno("write");
731 goto done;
733 if (close(in_fd) == -1) {
734 err = got_error_from_errno("close");
735 goto done;
738 if (waitpid(pid, &status, 0) == -1) {
739 err = got_error_from_errno("waitpid");
740 goto done;
742 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
743 err = got_error(GOT_ERR_SIGNING_TAG);
744 goto done;
747 buf_empty(buf);
748 err = buf_load_fd(&buf, out_fd);
749 if (err)
750 goto done;
751 sig_len = buf_len(buf);
752 if (close(out_fd) == -1) {
753 err = got_error_from_errno("close");
754 goto done;
758 len = strlen(obj_str) + strlen(type_str) + strlen(tag_str) +
759 strlen(tagger_str) + 1 + strlen(msg) + 1 + sig_len;
760 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TAG, len) == -1) {
761 err = got_error_from_errno("asprintf");
762 goto done;
765 headerlen = strlen(header) + 1;
766 SHA1Update(&sha1_ctx, header, headerlen);
768 tagfile = got_opentemp();
769 if (tagfile == NULL) {
770 err = got_error_from_errno("got_opentemp");
771 goto done;
774 n = fwrite(header, 1, headerlen, tagfile);
775 if (n != headerlen) {
776 err = got_ferror(tagfile, GOT_ERR_IO);
777 goto done;
779 tagsize += headerlen;
780 len = strlen(obj_str);
781 SHA1Update(&sha1_ctx, obj_str, len);
782 n = fwrite(obj_str, 1, len, tagfile);
783 if (n != len) {
784 err = got_ferror(tagfile, GOT_ERR_IO);
785 goto done;
787 tagsize += n;
788 len = strlen(type_str);
789 SHA1Update(&sha1_ctx, type_str, len);
790 n = fwrite(type_str, 1, len, tagfile);
791 if (n != len) {
792 err = got_ferror(tagfile, GOT_ERR_IO);
793 goto done;
795 tagsize += n;
797 len = strlen(tag_str);
798 SHA1Update(&sha1_ctx, tag_str, len);
799 n = fwrite(tag_str, 1, len, tagfile);
800 if (n != len) {
801 err = got_ferror(tagfile, GOT_ERR_IO);
802 goto done;
804 tagsize += n;
806 len = strlen(tagger_str);
807 SHA1Update(&sha1_ctx, tagger_str, len);
808 n = fwrite(tagger_str, 1, len, tagfile);
809 if (n != len) {
810 err = got_ferror(tagfile, GOT_ERR_IO);
811 goto done;
813 tagsize += n;
815 SHA1Update(&sha1_ctx, "\n", 1);
816 n = fwrite("\n", 1, 1, tagfile);
817 if (n != 1) {
818 err = got_ferror(tagfile, GOT_ERR_IO);
819 goto done;
821 tagsize += n;
823 len = strlen(msg);
824 SHA1Update(&sha1_ctx, msg, len);
825 n = fwrite(msg, 1, len, tagfile);
826 if (n != len) {
827 err = got_ferror(tagfile, GOT_ERR_IO);
828 goto done;
830 tagsize += n;
832 SHA1Update(&sha1_ctx, "\n", 1);
833 n = fwrite("\n", 1, 1, tagfile);
834 if (n != 1) {
835 err = got_ferror(tagfile, GOT_ERR_IO);
836 goto done;
838 tagsize += n;
840 if (signer_id && buf_len(buf) > 0) {
841 len = buf_len(buf);
842 SHA1Update(&sha1_ctx, buf_get(buf), len);
843 n = fwrite(buf_get(buf), 1, len, tagfile);
844 if (n != len) {
845 err = got_ferror(tagfile, GOT_ERR_IO);
846 goto done;
848 tagsize += n;
851 *id = malloc(sizeof(**id));
852 if (*id == NULL) {
853 err = got_error_from_errno("malloc");
854 goto done;
856 SHA1Final((*id)->sha1, &sha1_ctx);
858 if (fflush(tagfile) != 0) {
859 err = got_error_from_errno("fflush");
860 goto done;
862 rewind(tagfile);
864 err = create_object_file(*id, tagfile, tagsize, repo);
865 done:
866 free(msg0);
867 free(header);
868 free(obj_str);
869 free(tagger_str);
870 if (buf)
871 buf_release(buf);
872 if (tagfile && fclose(tagfile) == EOF && err == NULL)
873 err = got_error_from_errno("fclose");
874 if (err) {
875 free(*id);
876 *id = NULL;
878 return err;