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 SHA1_CTX hash_ctx;
130 size_t headerlen = 0, n;
132 *id = NULL;
133 *blobfile = NULL;
134 *blobsize = 0;
136 SHA1Init(&hash_ctx);
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 SHA1Update(&hash_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 SHA1Update(&hash_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 = malloc(sizeof(**id));
203 if (*id == NULL) {
204 err = got_error_from_errno("malloc");
205 goto done;
207 SHA1Final((*id)->hash, &hash_ctx);
208 (*id)->algo = GOT_HASH_SHA1;
210 if (fflush(*blobfile) != 0) {
211 err = got_error_from_errno("fflush");
212 goto done;
214 rewind(*blobfile);
215 done:
216 free(header);
217 if (fd != -1 && close(fd) == -1 && err == NULL)
218 err = got_error_from_errno("close");
219 if (err) {
220 free(*id);
221 *id = NULL;
222 if (*blobfile) {
223 fclose(*blobfile);
224 *blobfile = NULL;
227 return err;
230 const struct got_error *
231 got_object_blob_create(struct got_object_id **id, const char *ondisk_path,
232 struct got_repository *repo)
234 const struct got_error *err = NULL;
235 FILE *blobfile = NULL;
236 off_t blobsize;
238 err = got_object_blob_file_create(id, &blobfile, &blobsize,
239 ondisk_path);
240 if (err)
241 return err;
243 err = create_object_file(*id, blobfile, blobsize, repo);
244 if (fclose(blobfile) == EOF && err == NULL)
245 err = got_error_from_errno("fclose");
246 if (err) {
247 free(*id);
248 *id = NULL;
250 return err;
253 static const struct got_error *
254 te_mode2str(char *buf, size_t len, struct got_tree_entry *te)
256 int ret;
257 mode_t mode;
259 /*
260 * Some Git implementations are picky about modes seen in tree entries.
261 * For best compatibility we normalize the file/directory mode here.
262 */
263 if (S_ISREG(te->mode)) {
264 mode = GOT_DEFAULT_FILE_MODE;
265 if (te->mode & (S_IXUSR | S_IXGRP | S_IXOTH))
266 mode |= S_IXUSR | S_IXGRP | S_IXOTH;
267 } else if (got_object_tree_entry_is_submodule(te))
268 mode = S_IFDIR | S_IFLNK;
269 else if (S_ISLNK(te->mode))
270 mode = S_IFLNK; /* Git leaves all the other bits unset. */
271 else if (S_ISDIR(te->mode))
272 mode = S_IFDIR; /* Git leaves all the other bits unset. */
273 else
274 return got_error(GOT_ERR_BAD_FILETYPE);
276 ret = snprintf(buf, len, "%o ", mode);
277 if (ret < 0 || (size_t)ret >= len)
278 return got_error(GOT_ERR_NO_SPACE);
279 return NULL;
282 /*
283 * Git expects directory tree entries to be sorted with an imaginary slash
284 * appended to their name, and will break otherwise. Let's be nice.
285 * This function is intended to be used with mergesort(3) to sort an
286 * array of pointers to struct got_tree_entry objects.
287 */
288 static int
289 sort_tree_entries_the_way_git_likes_it(const void *arg1, const void *arg2)
291 struct got_tree_entry * const *te1 = arg1;
292 struct got_tree_entry * const *te2 = arg2;
293 char name1[NAME_MAX + 2];
294 char name2[NAME_MAX + 2];
296 strlcpy(name1, (*te1)->name, sizeof(name1));
297 strlcpy(name2, (*te2)->name, sizeof(name2));
298 if (S_ISDIR((*te1)->mode))
299 strlcat(name1, "/", sizeof(name1));
300 if (S_ISDIR((*te2)->mode))
301 strlcat(name2, "/", sizeof(name2));
302 return strcmp(name1, name2);
305 const struct got_error *
306 got_object_tree_create(struct got_object_id **id,
307 struct got_pathlist_head *paths, int nentries, struct got_repository *repo)
309 const struct got_error *err = NULL;
310 char modebuf[sizeof("100644 ")];
311 SHA1_CTX hash_ctx;
312 char *header = NULL;
313 size_t headerlen, len = 0, n;
314 FILE *treefile = NULL;
315 off_t treesize = 0;
316 struct got_pathlist_entry *pe;
317 struct got_tree_entry **sorted_entries;
318 struct got_tree_entry *te;
319 int i;
321 *id = NULL;
323 SHA1Init(&hash_ctx);
325 sorted_entries = calloc(nentries, sizeof(struct got_tree_entry *));
326 if (sorted_entries == NULL)
327 return got_error_from_errno("calloc");
329 i = 0;
330 TAILQ_FOREACH(pe, paths, entry)
331 sorted_entries[i++] = pe->data;
332 mergesort(sorted_entries, nentries, sizeof(struct got_tree_entry *),
333 sort_tree_entries_the_way_git_likes_it);
335 for (i = 0; i < nentries; i++) {
336 te = sorted_entries[i];
337 err = te_mode2str(modebuf, sizeof(modebuf), te);
338 if (err)
339 goto done;
340 len += strlen(modebuf) + strlen(te->name) + 1 +
341 SHA1_DIGEST_LENGTH;
344 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TREE, len) == -1) {
345 err = got_error_from_errno("asprintf");
346 goto done;
348 headerlen = strlen(header) + 1;
349 SHA1Update(&hash_ctx, header, headerlen);
351 treefile = got_opentemp();
352 if (treefile == NULL) {
353 err = got_error_from_errno("got_opentemp");
354 goto done;
357 n = fwrite(header, 1, headerlen, treefile);
358 if (n != headerlen) {
359 err = got_ferror(treefile, GOT_ERR_IO);
360 goto done;
362 treesize += headerlen;
364 for (i = 0; i < nentries; i++) {
365 te = sorted_entries[i];
366 err = te_mode2str(modebuf, sizeof(modebuf), te);
367 if (err)
368 goto done;
369 len = strlen(modebuf);
370 n = fwrite(modebuf, 1, len, treefile);
371 if (n != len) {
372 err = got_ferror(treefile, GOT_ERR_IO);
373 goto done;
375 SHA1Update(&hash_ctx, modebuf, len);
376 treesize += n;
378 len = strlen(te->name) + 1; /* must include NUL */
379 n = fwrite(te->name, 1, len, treefile);
380 if (n != len) {
381 err = got_ferror(treefile, GOT_ERR_IO);
382 goto done;
384 SHA1Update(&hash_ctx, te->name, len);
385 treesize += n;
387 len = SHA1_DIGEST_LENGTH;
388 n = fwrite(te->id.hash, 1, len, treefile);
389 if (n != len) {
390 err = got_ferror(treefile, GOT_ERR_IO);
391 goto done;
393 SHA1Update(&hash_ctx, te->id.hash, len);
394 treesize += n;
397 *id = malloc(sizeof(**id));
398 if (*id == NULL) {
399 err = got_error_from_errno("malloc");
400 goto done;
402 SHA1Final((*id)->hash, &hash_ctx);
403 (*id)->algo = GOT_HASH_SHA1;
405 if (fflush(treefile) != 0) {
406 err = got_error_from_errno("fflush");
407 goto done;
409 rewind(treefile);
411 err = create_object_file(*id, treefile, treesize, repo);
412 done:
413 free(header);
414 free(sorted_entries);
415 if (treefile && fclose(treefile) == EOF && err == NULL)
416 err = got_error_from_errno("fclose");
417 if (err) {
418 free(*id);
419 *id = NULL;
421 return err;
424 const struct got_error *
425 got_object_commit_create(struct got_object_id **id,
426 struct got_object_id *tree_id, struct got_object_id_queue *parent_ids,
427 int nparents, const char *author, time_t author_time,
428 const char *committer, time_t committer_time,
429 const char *logmsg, struct got_repository *repo)
431 const struct got_error *err = NULL;
432 SHA1_CTX hash_ctx;
433 char *header = NULL, *tree_str = NULL;
434 char *author_str = NULL, *committer_str = NULL;
435 char *id_str = NULL;
436 size_t headerlen, len = 0, n;
437 FILE *commitfile = NULL;
438 off_t commitsize = 0;
439 struct got_object_qid *qid;
440 char *msg0, *msg;
442 *id = NULL;
444 SHA1Init(&hash_ctx);
446 msg0 = strdup(logmsg);
447 if (msg0 == NULL)
448 return got_error_from_errno("strdup");
449 msg = msg0;
451 while (isspace((unsigned char)msg[0]))
452 msg++;
453 len = strlen(msg);
454 while (len > 0 && isspace((unsigned char)msg[len - 1])) {
455 msg[len - 1] = '\0';
456 len--;
459 if (asprintf(&author_str, "%s%s %lld +0000\n",
460 GOT_COMMIT_LABEL_AUTHOR, author, (long long)author_time) == -1) {
461 err = got_error_from_errno("asprintf");
462 goto done;
465 if (asprintf(&committer_str, "%s%s %lld +0000\n",
466 GOT_COMMIT_LABEL_COMMITTER, committer ? committer : author,
467 (long long)(committer ? committer_time : author_time))
468 == -1) {
469 err = got_error_from_errno("asprintf");
470 goto done;
473 len = strlen(GOT_COMMIT_LABEL_TREE) + SHA1_DIGEST_STRING_LENGTH +
474 nparents *
475 (strlen(GOT_COMMIT_LABEL_PARENT) + SHA1_DIGEST_STRING_LENGTH) +
476 + strlen(author_str) + strlen(committer_str) + 2 + strlen(msg);
478 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_COMMIT, len) == -1) {
479 err = got_error_from_errno("asprintf");
480 goto done;
482 headerlen = strlen(header) + 1;
483 SHA1Update(&hash_ctx, header, headerlen);
485 commitfile = got_opentemp();
486 if (commitfile == NULL) {
487 err = got_error_from_errno("got_opentemp");
488 goto done;
491 n = fwrite(header, 1, headerlen, commitfile);
492 if (n != headerlen) {
493 err = got_ferror(commitfile, GOT_ERR_IO);
494 goto done;
496 commitsize += headerlen;
498 err = got_object_id_str(&id_str, tree_id);
499 if (err)
500 goto done;
501 if (asprintf(&tree_str, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str)
502 == -1) {
503 err = got_error_from_errno("asprintf");
504 goto done;
506 len = strlen(tree_str);
507 SHA1Update(&hash_ctx, tree_str, len);
508 n = fwrite(tree_str, 1, len, commitfile);
509 if (n != len) {
510 err = got_ferror(commitfile, GOT_ERR_IO);
511 goto done;
513 commitsize += n;
515 if (parent_ids) {
516 free(id_str);
517 id_str = NULL;
518 STAILQ_FOREACH(qid, parent_ids, entry) {
519 char *parent_str = NULL;
521 err = got_object_id_str(&id_str, &qid->id);
522 if (err)
523 goto done;
524 if (asprintf(&parent_str, "%s%s\n",
525 GOT_COMMIT_LABEL_PARENT, id_str) == -1) {
526 err = got_error_from_errno("asprintf");
527 goto done;
529 len = strlen(parent_str);
530 SHA1Update(&hash_ctx, parent_str, len);
531 n = fwrite(parent_str, 1, len, commitfile);
532 if (n != len) {
533 err = got_ferror(commitfile, GOT_ERR_IO);
534 free(parent_str);
535 goto done;
537 commitsize += n;
538 free(parent_str);
539 free(id_str);
540 id_str = NULL;
544 len = strlen(author_str);
545 SHA1Update(&hash_ctx, author_str, len);
546 n = fwrite(author_str, 1, len, commitfile);
547 if (n != len) {
548 err = got_ferror(commitfile, GOT_ERR_IO);
549 goto done;
551 commitsize += n;
553 len = strlen(committer_str);
554 SHA1Update(&hash_ctx, committer_str, len);
555 n = fwrite(committer_str, 1, len, commitfile);
556 if (n != len) {
557 err = got_ferror(commitfile, GOT_ERR_IO);
558 goto done;
560 commitsize += n;
562 SHA1Update(&hash_ctx, "\n", 1);
563 n = fwrite("\n", 1, 1, commitfile);
564 if (n != 1) {
565 err = got_ferror(commitfile, GOT_ERR_IO);
566 goto done;
568 commitsize += n;
570 len = strlen(msg);
571 SHA1Update(&hash_ctx, msg, len);
572 n = fwrite(msg, 1, len, commitfile);
573 if (n != len) {
574 err = got_ferror(commitfile, GOT_ERR_IO);
575 goto done;
577 commitsize += n;
579 SHA1Update(&hash_ctx, "\n", 1);
580 n = fwrite("\n", 1, 1, commitfile);
581 if (n != 1) {
582 err = got_ferror(commitfile, GOT_ERR_IO);
583 goto done;
585 commitsize += n;
587 *id = malloc(sizeof(**id));
588 if (*id == NULL) {
589 err = got_error_from_errno("malloc");
590 goto done;
592 SHA1Final((*id)->hash, &hash_ctx);
593 (*id)->algo = GOT_HASH_SHA1;
595 if (fflush(commitfile) != 0) {
596 err = got_error_from_errno("fflush");
597 goto done;
599 rewind(commitfile);
601 err = create_object_file(*id, commitfile, commitsize, repo);
602 done:
603 free(id_str);
604 free(msg0);
605 free(header);
606 free(tree_str);
607 free(author_str);
608 free(committer_str);
609 if (commitfile && fclose(commitfile) == EOF && err == NULL)
610 err = got_error_from_errno("fclose");
611 if (err) {
612 free(*id);
613 *id = NULL;
615 return err;
618 const struct got_error *
619 got_object_tag_create(struct got_object_id **id,
620 const char *tag_name, struct got_object_id *object_id, const char *tagger,
621 time_t tagger_time, const char *tagmsg, const char *signer_id,
622 struct got_repository *repo, int verbosity)
624 const struct got_error *err = NULL;
625 SHA1_CTX hash_ctx;
626 char *header = NULL;
627 char *tag_str = NULL, *tagger_str = NULL;
628 char *id_str = NULL, *obj_str = NULL, *type_str = NULL;
629 size_t headerlen, len = 0, sig_len = 0, n;
630 FILE *tagfile = NULL;
631 off_t tagsize = 0;
632 char *msg0 = NULL, *msg;
633 const char *obj_type_str;
634 int obj_type;
635 BUF *buf = NULL;
637 *id = NULL;
639 SHA1Init(&hash_ctx);
641 err = got_object_id_str(&id_str, object_id);
642 if (err)
643 goto done;
644 if (asprintf(&obj_str, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str) == -1) {
645 err = got_error_from_errno("asprintf");
646 goto done;
649 err = got_object_get_type(&obj_type, repo, object_id);
650 if (err)
651 goto done;
653 switch (obj_type) {
654 case GOT_OBJ_TYPE_BLOB:
655 obj_type_str = GOT_OBJ_LABEL_BLOB;
656 break;
657 case GOT_OBJ_TYPE_TREE:
658 obj_type_str = GOT_OBJ_LABEL_TREE;
659 break;
660 case GOT_OBJ_TYPE_COMMIT:
661 obj_type_str = GOT_OBJ_LABEL_COMMIT;
662 break;
663 case GOT_OBJ_TYPE_TAG:
664 obj_type_str = GOT_OBJ_LABEL_TAG;
665 break;
666 default:
667 err = got_error(GOT_ERR_OBJ_TYPE);
668 goto done;
671 if (asprintf(&type_str, "%s%s\n", GOT_TAG_LABEL_TYPE,
672 obj_type_str) == -1) {
673 err = got_error_from_errno("asprintf");
674 goto done;
677 if (asprintf(&tag_str, "%s%s\n", GOT_TAG_LABEL_TAG, tag_name) == -1) {
678 err = got_error_from_errno("asprintf");
679 goto done;
682 if (asprintf(&tagger_str, "%s%s %lld +0000\n",
683 GOT_TAG_LABEL_TAGGER, tagger, (long long)tagger_time) == -1)
684 return got_error_from_errno("asprintf");
686 msg0 = strdup(tagmsg);
687 if (msg0 == NULL) {
688 err = got_error_from_errno("strdup");
689 goto done;
691 msg = msg0;
693 while (isspace((unsigned char)msg[0]))
694 msg++;
696 if (signer_id) {
697 pid_t pid;
698 size_t len;
699 int in_fd, out_fd;
700 int status;
702 err = buf_alloc(&buf, 0);
703 if (err)
704 goto done;
706 /* signed message */
707 err = buf_puts(&len, buf, obj_str);
708 if (err)
709 goto done;
710 err = buf_puts(&len, buf, type_str);
711 if (err)
712 goto done;
713 err = buf_puts(&len, buf, tag_str);
714 if (err)
715 goto done;
716 err = buf_puts(&len, buf, tagger_str);
717 if (err)
718 goto done;
719 err = buf_putc(buf, '\n');
720 if (err)
721 goto done;
722 err = buf_puts(&len, buf, msg);
723 if (err)
724 goto done;
725 err = buf_putc(buf, '\n');
726 if (err)
727 goto done;
729 err = got_sigs_sign_tag_ssh(&pid, &in_fd, &out_fd, signer_id,
730 verbosity);
731 if (err)
732 goto done;
733 if (buf_write_fd(buf, in_fd) == -1) {
734 err = got_error_from_errno("write");
735 goto done;
737 if (close(in_fd) == -1) {
738 err = got_error_from_errno("close");
739 goto done;
742 if (waitpid(pid, &status, 0) == -1) {
743 err = got_error_from_errno("waitpid");
744 goto done;
746 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
747 err = got_error(GOT_ERR_SIGNING_TAG);
748 goto done;
751 buf_empty(buf);
752 err = buf_load_fd(&buf, out_fd);
753 if (err)
754 goto done;
755 sig_len = buf_len(buf);
756 if (close(out_fd) == -1) {
757 err = got_error_from_errno("close");
758 goto done;
762 len = strlen(obj_str) + strlen(type_str) + strlen(tag_str) +
763 strlen(tagger_str) + 1 + strlen(msg) + 1 + sig_len;
764 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TAG, len) == -1) {
765 err = got_error_from_errno("asprintf");
766 goto done;
769 headerlen = strlen(header) + 1;
770 SHA1Update(&hash_ctx, header, headerlen);
772 tagfile = got_opentemp();
773 if (tagfile == NULL) {
774 err = got_error_from_errno("got_opentemp");
775 goto done;
778 n = fwrite(header, 1, headerlen, tagfile);
779 if (n != headerlen) {
780 err = got_ferror(tagfile, GOT_ERR_IO);
781 goto done;
783 tagsize += headerlen;
784 len = strlen(obj_str);
785 SHA1Update(&hash_ctx, obj_str, len);
786 n = fwrite(obj_str, 1, len, tagfile);
787 if (n != len) {
788 err = got_ferror(tagfile, GOT_ERR_IO);
789 goto done;
791 tagsize += n;
792 len = strlen(type_str);
793 SHA1Update(&hash_ctx, type_str, len);
794 n = fwrite(type_str, 1, len, tagfile);
795 if (n != len) {
796 err = got_ferror(tagfile, GOT_ERR_IO);
797 goto done;
799 tagsize += n;
801 len = strlen(tag_str);
802 SHA1Update(&hash_ctx, tag_str, len);
803 n = fwrite(tag_str, 1, len, tagfile);
804 if (n != len) {
805 err = got_ferror(tagfile, GOT_ERR_IO);
806 goto done;
808 tagsize += n;
810 len = strlen(tagger_str);
811 SHA1Update(&hash_ctx, tagger_str, len);
812 n = fwrite(tagger_str, 1, len, tagfile);
813 if (n != len) {
814 err = got_ferror(tagfile, GOT_ERR_IO);
815 goto done;
817 tagsize += n;
819 SHA1Update(&hash_ctx, "\n", 1);
820 n = fwrite("\n", 1, 1, tagfile);
821 if (n != 1) {
822 err = got_ferror(tagfile, GOT_ERR_IO);
823 goto done;
825 tagsize += n;
827 len = strlen(msg);
828 SHA1Update(&hash_ctx, msg, len);
829 n = fwrite(msg, 1, len, tagfile);
830 if (n != len) {
831 err = got_ferror(tagfile, GOT_ERR_IO);
832 goto done;
834 tagsize += n;
836 SHA1Update(&hash_ctx, "\n", 1);
837 n = fwrite("\n", 1, 1, tagfile);
838 if (n != 1) {
839 err = got_ferror(tagfile, GOT_ERR_IO);
840 goto done;
842 tagsize += n;
844 if (signer_id && buf_len(buf) > 0) {
845 len = buf_len(buf);
846 SHA1Update(&hash_ctx, buf_get(buf), len);
847 n = fwrite(buf_get(buf), 1, len, tagfile);
848 if (n != len) {
849 err = got_ferror(tagfile, GOT_ERR_IO);
850 goto done;
852 tagsize += n;
855 *id = malloc(sizeof(**id));
856 if (*id == NULL) {
857 err = got_error_from_errno("malloc");
858 goto done;
860 SHA1Final((*id)->hash, &hash_ctx);
861 (*id)->algo = GOT_HASH_SHA1;
863 if (fflush(tagfile) != 0) {
864 err = got_error_from_errno("fflush");
865 goto done;
867 rewind(tagfile);
869 err = create_object_file(*id, tagfile, tagsize, repo);
870 done:
871 free(msg0);
872 free(header);
873 free(obj_str);
874 free(tagger_str);
875 if (buf)
876 buf_release(buf);
877 if (tagfile && fclose(tagfile) == EOF && err == NULL)
878 err = got_error_from_errno("fclose");
879 if (err) {
880 free(*id);
881 *id = NULL;
883 return err;