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 <sha2.h>
32 #include <unistd.h>
33 #include <zlib.h>
35 #include "got_error.h"
36 #include "got_object.h"
37 #include "got_repository.h"
38 #include "got_opentemp.h"
39 #include "got_path.h"
40 #include "got_sigs.h"
42 #include "got_lib_hash.h"
43 #include "got_lib_deflate.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_object.h"
46 #include "got_lib_object_parse.h"
47 #include "got_lib_lockfile.h"
49 #include "got_lib_object_create.h"
51 #include "buf.h"
53 #ifndef nitems
54 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
55 #endif
57 static const struct got_error *
58 create_object_file(struct got_object_id *id, FILE *content,
59 off_t content_len, struct got_repository *repo)
60 {
61 const struct got_error *err = NULL, *unlock_err = NULL;
62 char *objpath = NULL, *tmppath = NULL;
63 FILE *tmpfile = NULL;
64 struct got_lockfile *lf = NULL;
65 off_t tmplen = 0;
67 err = got_object_get_path(&objpath, id, repo);
68 if (err)
69 return err;
71 err = got_opentemp_named(&tmppath, &tmpfile, objpath, "");
72 if (err) {
73 char *parent_path;
74 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
75 goto done;
76 err = got_path_dirname(&parent_path, objpath);
77 if (err)
78 goto done;
79 err = got_path_mkdir(parent_path);
80 free(parent_path);
81 if (err)
82 goto done;
83 err = got_opentemp_named(&tmppath, &tmpfile, objpath, "");
84 if (err)
85 goto done;
86 }
88 if (fchmod(fileno(tmpfile), GOT_DEFAULT_FILE_MODE) != 0) {
89 err = got_error_from_errno2("fchmod", tmppath);
90 goto done;
91 }
93 err = got_deflate_to_file(&tmplen, content, content_len, tmpfile, NULL);
94 if (err)
95 goto done;
97 err = got_lockfile_lock(&lf, objpath, -1);
98 if (err)
99 goto done;
101 if (rename(tmppath, objpath) != 0) {
102 err = got_error_from_errno3("rename", tmppath, objpath);
103 goto done;
105 free(tmppath);
106 tmppath = NULL;
107 done:
108 free(objpath);
109 if (tmppath) {
110 if (unlink(tmppath) != 0 && err == NULL)
111 err = got_error_from_errno2("unlink", tmppath);
112 free(tmppath);
114 if (tmpfile && fclose(tmpfile) == EOF && err == NULL)
115 err = got_error_from_errno("fclose");
116 if (lf)
117 unlock_err = got_lockfile_unlock(lf, -1);
118 return err ? err : unlock_err;
121 const struct got_error *
122 got_object_blob_file_create(struct got_object_id **id, FILE **blobfile,
123 off_t *blobsize, const char *ondisk_path)
125 const struct got_error *err = NULL;
126 char *header = NULL;
127 int fd = -1;
128 struct stat sb;
129 struct got_hash ctx;
130 size_t headerlen = 0, n;
132 *id = NULL;
133 *blobfile = NULL;
134 *blobsize = 0;
136 got_hash_init(&ctx, GOT_HASH_SHA1);
138 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
139 if (fd == -1) {
140 if (!got_err_open_nofollow_on_symlink())
141 return got_error_from_errno2("open", ondisk_path);
143 if (lstat(ondisk_path, &sb) == -1) {
144 err = got_error_from_errno2("lstat", ondisk_path);
145 goto done;
147 } else if (fstat(fd, &sb) == -1) {
148 err = got_error_from_errno2("fstat", ondisk_path);
149 goto done;
152 if (asprintf(&header, "%s %lld", GOT_OBJ_LABEL_BLOB,
153 (long long)sb.st_size) == -1) {
154 err = got_error_from_errno("asprintf");
155 goto done;
157 headerlen = strlen(header) + 1;
158 got_hash_update(&ctx, header, headerlen);
160 *blobfile = got_opentemp();
161 if (*blobfile == NULL) {
162 err = got_error_from_errno("got_opentemp");
163 goto done;
166 n = fwrite(header, 1, headerlen, *blobfile);
167 if (n != headerlen) {
168 err = got_ferror(*blobfile, GOT_ERR_IO);
169 goto done;
171 *blobsize += headerlen;
172 for (;;) {
173 char buf[PATH_MAX * 8];
174 ssize_t inlen;
176 if (S_ISLNK(sb.st_mode)) {
177 inlen = readlink(ondisk_path, buf, sizeof(buf));
178 if (inlen == -1) {
179 err = got_error_from_errno("readlink");
180 goto done;
182 } else {
183 inlen = read(fd, buf, sizeof(buf));
184 if (inlen == -1) {
185 err = got_error_from_errno("read");
186 goto done;
189 if (inlen == 0)
190 break; /* EOF */
191 got_hash_update(&ctx, buf, inlen);
192 n = fwrite(buf, 1, inlen, *blobfile);
193 if (n != inlen) {
194 err = got_ferror(*blobfile, GOT_ERR_IO);
195 goto done;
197 *blobsize += n;
198 if (S_ISLNK(sb.st_mode))
199 break;
202 *id = calloc(1, sizeof(**id));
203 if (*id == NULL) {
204 err = got_error_from_errno("calloc");
205 goto done;
207 got_hash_final_object_id(&ctx, *id);
209 if (fflush(*blobfile) != 0) {
210 err = got_error_from_errno("fflush");
211 goto done;
213 rewind(*blobfile);
214 done:
215 free(header);
216 if (fd != -1 && close(fd) == -1 && err == NULL)
217 err = got_error_from_errno("close");
218 if (err) {
219 free(*id);
220 *id = NULL;
221 if (*blobfile) {
222 fclose(*blobfile);
223 *blobfile = NULL;
226 return err;
229 const struct got_error *
230 got_object_blob_create(struct got_object_id **id, const char *ondisk_path,
231 struct got_repository *repo)
233 const struct got_error *err = NULL;
234 FILE *blobfile = NULL;
235 off_t blobsize;
237 err = got_object_blob_file_create(id, &blobfile, &blobsize,
238 ondisk_path);
239 if (err)
240 return err;
242 err = create_object_file(*id, blobfile, blobsize, repo);
243 if (fclose(blobfile) == EOF && err == NULL)
244 err = got_error_from_errno("fclose");
245 if (err) {
246 free(*id);
247 *id = NULL;
249 return err;
252 static const struct got_error *
253 te_mode2str(char *buf, size_t len, struct got_tree_entry *te)
255 int ret;
256 mode_t mode;
258 /*
259 * Some Git implementations are picky about modes seen in tree entries.
260 * For best compatibility we normalize the file/directory mode here.
261 */
262 if (S_ISREG(te->mode)) {
263 mode = GOT_DEFAULT_FILE_MODE;
264 if (te->mode & (S_IXUSR | S_IXGRP | S_IXOTH))
265 mode |= S_IXUSR | S_IXGRP | S_IXOTH;
266 } else if (got_object_tree_entry_is_submodule(te))
267 mode = S_IFDIR | S_IFLNK;
268 else if (S_ISLNK(te->mode))
269 mode = S_IFLNK; /* Git leaves all the other bits unset. */
270 else if (S_ISDIR(te->mode))
271 mode = S_IFDIR; /* Git leaves all the other bits unset. */
272 else
273 return got_error(GOT_ERR_BAD_FILETYPE);
275 ret = snprintf(buf, len, "%o ", mode);
276 if (ret < 0 || (size_t)ret >= len)
277 return got_error(GOT_ERR_NO_SPACE);
278 return NULL;
281 /*
282 * Git expects directory tree entries to be sorted with an imaginary slash
283 * appended to their name, and will break otherwise. Let's be nice.
284 * This function is intended to be used with mergesort(3) to sort an
285 * array of pointers to struct got_tree_entry objects.
286 */
287 static int
288 sort_tree_entries_the_way_git_likes_it(const void *arg1, const void *arg2)
290 struct got_tree_entry * const *te1 = arg1;
291 struct got_tree_entry * const *te2 = arg2;
292 char name1[NAME_MAX + 2];
293 char name2[NAME_MAX + 2];
295 strlcpy(name1, (*te1)->name, sizeof(name1));
296 strlcpy(name2, (*te2)->name, sizeof(name2));
297 if (S_ISDIR((*te1)->mode))
298 strlcat(name1, "/", sizeof(name1));
299 if (S_ISDIR((*te2)->mode))
300 strlcat(name2, "/", sizeof(name2));
301 return strcmp(name1, name2);
304 const struct got_error *
305 got_object_tree_create(struct got_object_id **id,
306 struct got_pathlist_head *paths, int nentries, struct got_repository *repo)
308 const struct got_error *err = NULL;
309 char modebuf[sizeof("100644 ")];
310 struct got_hash ctx;
311 char *header = NULL;
312 size_t headerlen, len = 0, n;
313 FILE *treefile = NULL;
314 off_t treesize = 0;
315 struct got_pathlist_entry *pe;
316 struct got_tree_entry **sorted_entries;
317 struct got_tree_entry *te;
318 int i;
320 *id = NULL;
322 got_hash_init(&ctx, GOT_HASH_SHA1);
324 sorted_entries = calloc(nentries, sizeof(struct got_tree_entry *));
325 if (sorted_entries == NULL)
326 return got_error_from_errno("calloc");
328 i = 0;
329 TAILQ_FOREACH(pe, paths, entry)
330 sorted_entries[i++] = pe->data;
331 mergesort(sorted_entries, nentries, sizeof(struct got_tree_entry *),
332 sort_tree_entries_the_way_git_likes_it);
334 for (i = 0; i < nentries; i++) {
335 te = sorted_entries[i];
336 err = te_mode2str(modebuf, sizeof(modebuf), te);
337 if (err)
338 goto done;
339 len += strlen(modebuf) + strlen(te->name) + 1 +
340 SHA1_DIGEST_LENGTH;
343 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TREE, len) == -1) {
344 err = got_error_from_errno("asprintf");
345 goto done;
347 headerlen = strlen(header) + 1;
348 got_hash_update(&ctx, header, headerlen);
350 treefile = got_opentemp();
351 if (treefile == NULL) {
352 err = got_error_from_errno("got_opentemp");
353 goto done;
356 n = fwrite(header, 1, headerlen, treefile);
357 if (n != headerlen) {
358 err = got_ferror(treefile, GOT_ERR_IO);
359 goto done;
361 treesize += headerlen;
363 for (i = 0; i < nentries; i++) {
364 te = sorted_entries[i];
365 err = te_mode2str(modebuf, sizeof(modebuf), te);
366 if (err)
367 goto done;
368 len = strlen(modebuf);
369 n = fwrite(modebuf, 1, len, treefile);
370 if (n != len) {
371 err = got_ferror(treefile, GOT_ERR_IO);
372 goto done;
374 got_hash_update(&ctx, modebuf, len);
375 treesize += n;
377 len = strlen(te->name) + 1; /* must include NUL */
378 n = fwrite(te->name, 1, len, treefile);
379 if (n != len) {
380 err = got_ferror(treefile, GOT_ERR_IO);
381 goto done;
383 got_hash_update(&ctx, te->name, len);
384 treesize += n;
386 len = SHA1_DIGEST_LENGTH;
387 n = fwrite(te->id.sha1, 1, len, treefile);
388 if (n != len) {
389 err = got_ferror(treefile, GOT_ERR_IO);
390 goto done;
392 got_hash_update(&ctx, te->id.sha1, len);
393 treesize += n;
396 *id = calloc(1, sizeof(**id));
397 if (*id == NULL) {
398 err = got_error_from_errno("calloc");
399 goto done;
401 got_hash_final_object_id(&ctx, *id);
403 if (fflush(treefile) != 0) {
404 err = got_error_from_errno("fflush");
405 goto done;
407 rewind(treefile);
409 err = create_object_file(*id, treefile, treesize, repo);
410 done:
411 free(header);
412 free(sorted_entries);
413 if (treefile && fclose(treefile) == EOF && err == NULL)
414 err = got_error_from_errno("fclose");
415 if (err) {
416 free(*id);
417 *id = NULL;
419 return err;
422 const struct got_error *
423 got_object_commit_create(struct got_object_id **id,
424 struct got_object_id *tree_id, struct got_object_id_queue *parent_ids,
425 int nparents, const char *author, time_t author_time,
426 const char *committer, time_t committer_time,
427 const char *logmsg, struct got_repository *repo)
429 const struct got_error *err = NULL;
430 struct got_hash ctx;
431 char *header = NULL, *tree_str = NULL;
432 char *author_str = NULL, *committer_str = NULL;
433 char *id_str = NULL;
434 size_t headerlen, len = 0, n;
435 FILE *commitfile = NULL;
436 off_t commitsize = 0;
437 struct got_object_qid *qid;
438 char *msg0, *msg;
440 *id = NULL;
442 got_hash_init(&ctx, GOT_HASH_SHA1);
444 msg0 = strdup(logmsg);
445 if (msg0 == NULL)
446 return got_error_from_errno("strdup");
447 msg = msg0;
449 while (isspace((unsigned char)msg[0]))
450 msg++;
451 len = strlen(msg);
452 while (len > 0 && isspace((unsigned char)msg[len - 1])) {
453 msg[len - 1] = '\0';
454 len--;
457 if (asprintf(&author_str, "%s%s %lld +0000\n",
458 GOT_COMMIT_LABEL_AUTHOR, author, (long long)author_time) == -1) {
459 err = got_error_from_errno("asprintf");
460 goto done;
463 if (asprintf(&committer_str, "%s%s %lld +0000\n",
464 GOT_COMMIT_LABEL_COMMITTER, committer ? committer : author,
465 (long long)(committer ? committer_time : author_time))
466 == -1) {
467 err = got_error_from_errno("asprintf");
468 goto done;
471 len = strlen(GOT_COMMIT_LABEL_TREE) + SHA1_DIGEST_STRING_LENGTH +
472 nparents *
473 (strlen(GOT_COMMIT_LABEL_PARENT) + SHA1_DIGEST_STRING_LENGTH) +
474 + strlen(author_str) + strlen(committer_str) + 2 + strlen(msg);
476 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_COMMIT, len) == -1) {
477 err = got_error_from_errno("asprintf");
478 goto done;
480 headerlen = strlen(header) + 1;
481 got_hash_update(&ctx, header, headerlen);
483 commitfile = got_opentemp();
484 if (commitfile == NULL) {
485 err = got_error_from_errno("got_opentemp");
486 goto done;
489 n = fwrite(header, 1, headerlen, commitfile);
490 if (n != headerlen) {
491 err = got_ferror(commitfile, GOT_ERR_IO);
492 goto done;
494 commitsize += headerlen;
496 err = got_object_id_str(&id_str, tree_id);
497 if (err)
498 goto done;
499 if (asprintf(&tree_str, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str)
500 == -1) {
501 err = got_error_from_errno("asprintf");
502 goto done;
504 len = strlen(tree_str);
505 got_hash_update(&ctx, tree_str, len);
506 n = fwrite(tree_str, 1, len, commitfile);
507 if (n != len) {
508 err = got_ferror(commitfile, GOT_ERR_IO);
509 goto done;
511 commitsize += n;
513 if (parent_ids) {
514 free(id_str);
515 id_str = NULL;
516 STAILQ_FOREACH(qid, parent_ids, entry) {
517 char *parent_str = NULL;
519 err = got_object_id_str(&id_str, &qid->id);
520 if (err)
521 goto done;
522 if (asprintf(&parent_str, "%s%s\n",
523 GOT_COMMIT_LABEL_PARENT, id_str) == -1) {
524 err = got_error_from_errno("asprintf");
525 goto done;
527 len = strlen(parent_str);
528 got_hash_update(&ctx, parent_str, len);
529 n = fwrite(parent_str, 1, len, commitfile);
530 if (n != len) {
531 err = got_ferror(commitfile, GOT_ERR_IO);
532 free(parent_str);
533 goto done;
535 commitsize += n;
536 free(parent_str);
537 free(id_str);
538 id_str = NULL;
542 len = strlen(author_str);
543 got_hash_update(&ctx, author_str, len);
544 n = fwrite(author_str, 1, len, commitfile);
545 if (n != len) {
546 err = got_ferror(commitfile, GOT_ERR_IO);
547 goto done;
549 commitsize += n;
551 len = strlen(committer_str);
552 got_hash_update(&ctx, committer_str, len);
553 n = fwrite(committer_str, 1, len, commitfile);
554 if (n != len) {
555 err = got_ferror(commitfile, GOT_ERR_IO);
556 goto done;
558 commitsize += n;
560 got_hash_update(&ctx, "\n", 1);
561 n = fwrite("\n", 1, 1, commitfile);
562 if (n != 1) {
563 err = got_ferror(commitfile, GOT_ERR_IO);
564 goto done;
566 commitsize += n;
568 len = strlen(msg);
569 got_hash_update(&ctx, msg, len);
570 n = fwrite(msg, 1, len, commitfile);
571 if (n != len) {
572 err = got_ferror(commitfile, GOT_ERR_IO);
573 goto done;
575 commitsize += n;
577 got_hash_update(&ctx, "\n", 1);
578 n = fwrite("\n", 1, 1, commitfile);
579 if (n != 1) {
580 err = got_ferror(commitfile, GOT_ERR_IO);
581 goto done;
583 commitsize += n;
585 *id = calloc(1, sizeof(**id));
586 if (*id == NULL) {
587 err = got_error_from_errno("calloc");
588 goto done;
590 got_hash_final_object_id(&ctx, *id);
592 if (fflush(commitfile) != 0) {
593 err = got_error_from_errno("fflush");
594 goto done;
596 rewind(commitfile);
598 err = create_object_file(*id, commitfile, commitsize, repo);
599 done:
600 free(id_str);
601 free(msg0);
602 free(header);
603 free(tree_str);
604 free(author_str);
605 free(committer_str);
606 if (commitfile && fclose(commitfile) == EOF && err == NULL)
607 err = got_error_from_errno("fclose");
608 if (err) {
609 free(*id);
610 *id = NULL;
612 return err;
615 const struct got_error *
616 got_object_tag_create(struct got_object_id **id,
617 const char *tag_name, struct got_object_id *object_id, const char *tagger,
618 time_t tagger_time, const char *tagmsg, const char *signer_id,
619 struct got_repository *repo, int verbosity)
621 const struct got_error *err = NULL;
622 struct got_hash ctx;
623 char *header = NULL;
624 char *tag_str = NULL, *tagger_str = NULL;
625 char *id_str = NULL, *obj_str = NULL, *type_str = NULL;
626 size_t headerlen, len = 0, sig_len = 0, n;
627 FILE *tagfile = NULL;
628 off_t tagsize = 0;
629 char *msg0 = NULL, *msg;
630 const char *obj_type_str;
631 int obj_type;
632 BUF *buf = NULL;
634 *id = NULL;
636 got_hash_init(&ctx, GOT_HASH_SHA1);
638 err = got_object_id_str(&id_str, object_id);
639 if (err)
640 goto done;
641 if (asprintf(&obj_str, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str) == -1) {
642 err = got_error_from_errno("asprintf");
643 goto done;
646 err = got_object_get_type(&obj_type, repo, object_id);
647 if (err)
648 goto done;
650 switch (obj_type) {
651 case GOT_OBJ_TYPE_BLOB:
652 obj_type_str = GOT_OBJ_LABEL_BLOB;
653 break;
654 case GOT_OBJ_TYPE_TREE:
655 obj_type_str = GOT_OBJ_LABEL_TREE;
656 break;
657 case GOT_OBJ_TYPE_COMMIT:
658 obj_type_str = GOT_OBJ_LABEL_COMMIT;
659 break;
660 case GOT_OBJ_TYPE_TAG:
661 obj_type_str = GOT_OBJ_LABEL_TAG;
662 break;
663 default:
664 err = got_error(GOT_ERR_OBJ_TYPE);
665 goto done;
668 if (asprintf(&type_str, "%s%s\n", GOT_TAG_LABEL_TYPE,
669 obj_type_str) == -1) {
670 err = got_error_from_errno("asprintf");
671 goto done;
674 if (asprintf(&tag_str, "%s%s\n", GOT_TAG_LABEL_TAG, tag_name) == -1) {
675 err = got_error_from_errno("asprintf");
676 goto done;
679 if (asprintf(&tagger_str, "%s%s %lld +0000\n",
680 GOT_TAG_LABEL_TAGGER, tagger, (long long)tagger_time) == -1)
681 return got_error_from_errno("asprintf");
683 msg0 = strdup(tagmsg);
684 if (msg0 == NULL) {
685 err = got_error_from_errno("strdup");
686 goto done;
688 msg = msg0;
690 while (isspace((unsigned char)msg[0]))
691 msg++;
693 if (signer_id) {
694 pid_t pid;
695 size_t len;
696 int in_fd, out_fd;
697 int status;
699 err = buf_alloc(&buf, 0);
700 if (err)
701 goto done;
703 /* signed message */
704 err = buf_puts(&len, buf, obj_str);
705 if (err)
706 goto done;
707 err = buf_puts(&len, buf, type_str);
708 if (err)
709 goto done;
710 err = buf_puts(&len, buf, tag_str);
711 if (err)
712 goto done;
713 err = buf_puts(&len, buf, tagger_str);
714 if (err)
715 goto done;
716 err = buf_putc(buf, '\n');
717 if (err)
718 goto done;
719 err = buf_puts(&len, buf, msg);
720 if (err)
721 goto done;
722 err = buf_putc(buf, '\n');
723 if (err)
724 goto done;
726 err = got_sigs_sign_tag_ssh(&pid, &in_fd, &out_fd, signer_id,
727 verbosity);
728 if (err)
729 goto done;
730 if (buf_write_fd(buf, in_fd) == -1) {
731 err = got_error_from_errno("write");
732 goto done;
734 if (close(in_fd) == -1) {
735 err = got_error_from_errno("close");
736 goto done;
739 if (waitpid(pid, &status, 0) == -1) {
740 err = got_error_from_errno("waitpid");
741 goto done;
743 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
744 err = got_error(GOT_ERR_SIGNING_TAG);
745 goto done;
748 buf_empty(buf);
749 err = buf_load_fd(&buf, out_fd);
750 if (err)
751 goto done;
752 sig_len = buf_len(buf);
753 if (close(out_fd) == -1) {
754 err = got_error_from_errno("close");
755 goto done;
759 len = strlen(obj_str) + strlen(type_str) + strlen(tag_str) +
760 strlen(tagger_str) + 1 + strlen(msg) + 1 + sig_len;
761 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TAG, len) == -1) {
762 err = got_error_from_errno("asprintf");
763 goto done;
766 headerlen = strlen(header) + 1;
767 got_hash_update(&ctx, header, headerlen);
769 tagfile = got_opentemp();
770 if (tagfile == NULL) {
771 err = got_error_from_errno("got_opentemp");
772 goto done;
775 n = fwrite(header, 1, headerlen, tagfile);
776 if (n != headerlen) {
777 err = got_ferror(tagfile, GOT_ERR_IO);
778 goto done;
780 tagsize += headerlen;
781 len = strlen(obj_str);
782 got_hash_update(&ctx, obj_str, len);
783 n = fwrite(obj_str, 1, len, tagfile);
784 if (n != len) {
785 err = got_ferror(tagfile, GOT_ERR_IO);
786 goto done;
788 tagsize += n;
789 len = strlen(type_str);
790 got_hash_update(&ctx, type_str, len);
791 n = fwrite(type_str, 1, len, tagfile);
792 if (n != len) {
793 err = got_ferror(tagfile, GOT_ERR_IO);
794 goto done;
796 tagsize += n;
798 len = strlen(tag_str);
799 got_hash_update(&ctx, tag_str, len);
800 n = fwrite(tag_str, 1, len, tagfile);
801 if (n != len) {
802 err = got_ferror(tagfile, GOT_ERR_IO);
803 goto done;
805 tagsize += n;
807 len = strlen(tagger_str);
808 got_hash_update(&ctx, tagger_str, len);
809 n = fwrite(tagger_str, 1, len, tagfile);
810 if (n != len) {
811 err = got_ferror(tagfile, GOT_ERR_IO);
812 goto done;
814 tagsize += n;
816 got_hash_update(&ctx, "\n", 1);
817 n = fwrite("\n", 1, 1, tagfile);
818 if (n != 1) {
819 err = got_ferror(tagfile, GOT_ERR_IO);
820 goto done;
822 tagsize += n;
824 len = strlen(msg);
825 got_hash_update(&ctx, msg, len);
826 n = fwrite(msg, 1, len, tagfile);
827 if (n != len) {
828 err = got_ferror(tagfile, GOT_ERR_IO);
829 goto done;
831 tagsize += n;
833 got_hash_update(&ctx, "\n", 1);
834 n = fwrite("\n", 1, 1, tagfile);
835 if (n != 1) {
836 err = got_ferror(tagfile, GOT_ERR_IO);
837 goto done;
839 tagsize += n;
841 if (signer_id && buf_len(buf) > 0) {
842 len = buf_len(buf);
843 got_hash_update(&ctx, buf_get(buf), len);
844 n = fwrite(buf_get(buf), 1, len, tagfile);
845 if (n != len) {
846 err = got_ferror(tagfile, GOT_ERR_IO);
847 goto done;
849 tagsize += n;
852 *id = calloc(1, sizeof(**id));
853 if (*id == NULL) {
854 err = got_error_from_errno("calloc");
855 goto done;
857 got_hash_final_object_id(&ctx, *id);
859 if (fflush(tagfile) != 0) {
860 err = got_error_from_errno("fflush");
861 goto done;
863 rewind(tagfile);
865 err = create_object_file(*id, tagfile, tagsize, repo);
866 done:
867 free(msg0);
868 free(header);
869 free(obj_str);
870 free(tagger_str);
871 if (buf)
872 buf_release(buf);
873 if (tagfile && fclose(tagfile) == EOF && err == NULL)
874 err = got_error_from_errno("fclose");
875 if (err) {
876 free(*id);
877 *id = NULL;
879 return err;