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>
20 #include <ctype.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdint.h>
28 #include <unistd.h>
29 #include <zlib.h>
31 #include "got_compat.h"
33 #include "got_error.h"
34 #include "got_object.h"
35 #include "got_repository.h"
36 #include "got_opentemp.h"
37 #include "got_path.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"
46 #include "got_lib_object_create.h"
48 #ifndef nitems
49 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
50 #endif
52 static const struct got_error *
53 create_object_file(struct got_object_id *id, FILE *content,
54 off_t content_len, struct got_repository *repo)
55 {
56 const struct got_error *err = NULL, *unlock_err = NULL;
57 char *objpath = NULL, *tmppath = NULL;
58 FILE *tmpfile = NULL;
59 struct got_lockfile *lf = NULL;
60 off_t tmplen = 0;
62 err = got_object_get_path(&objpath, id, repo);
63 if (err)
64 return err;
66 err = got_opentemp_named(&tmppath, &tmpfile, objpath);
67 if (err) {
68 char *parent_path;
69 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
70 goto done;
71 err = got_path_dirname(&parent_path, objpath);
72 if (err)
73 goto done;
74 err = got_path_mkdir(parent_path);
75 free(parent_path);
76 if (err)
77 goto done;
78 err = got_opentemp_named(&tmppath, &tmpfile, objpath);
79 if (err)
80 goto done;
81 }
83 if (fchmod(fileno(tmpfile), GOT_DEFAULT_FILE_MODE) != 0) {
84 err = got_error_from_errno2("fchmod", tmppath);
85 goto done;
86 }
88 err = got_deflate_to_file(&tmplen, content, content_len, tmpfile, NULL);
89 if (err)
90 goto done;
92 err = got_lockfile_lock(&lf, objpath, -1);
93 if (err)
94 goto done;
96 if (rename(tmppath, objpath) != 0) {
97 err = got_error_from_errno3("rename", tmppath, objpath);
98 goto done;
99 }
100 free(tmppath);
101 tmppath = NULL;
102 done:
103 free(objpath);
104 if (tmppath) {
105 if (unlink(tmppath) != 0 && err == NULL)
106 err = got_error_from_errno2("unlink", tmppath);
107 free(tmppath);
109 if (tmpfile && fclose(tmpfile) == EOF && err == NULL)
110 err = got_error_from_errno("fclose");
111 if (lf)
112 unlock_err = got_lockfile_unlock(lf, -1);
113 return err ? err : unlock_err;
116 const struct got_error *
117 got_object_blob_file_create(struct got_object_id **id, FILE **blobfile,
118 off_t *blobsize, const char *ondisk_path)
120 const struct got_error *err = NULL;
121 char *header = NULL;
122 int fd = -1;
123 struct stat sb;
124 SHA1_CTX sha1_ctx;
125 size_t headerlen = 0, n;
127 *id = NULL;
128 *blobfile = NULL;
129 *blobsize = 0;
131 SHA1Init(&sha1_ctx);
133 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
134 if (fd == -1) {
135 if (!got_err_open_nofollow_on_symlink())
136 return got_error_from_errno2("open", ondisk_path);
138 if (lstat(ondisk_path, &sb) == -1) {
139 err = got_error_from_errno2("lstat", ondisk_path);
140 goto done;
142 } else if (fstat(fd, &sb) == -1) {
143 err = got_error_from_errno2("fstat", ondisk_path);
144 goto done;
147 if (asprintf(&header, "%s %lld", GOT_OBJ_LABEL_BLOB,
148 (long long)sb.st_size) == -1) {
149 err = got_error_from_errno("asprintf");
150 goto done;
152 headerlen = strlen(header) + 1;
153 SHA1Update(&sha1_ctx, header, headerlen);
155 *blobfile = got_opentemp();
156 if (*blobfile == NULL) {
157 err = got_error_from_errno("got_opentemp");
158 goto done;
161 n = fwrite(header, 1, headerlen, *blobfile);
162 if (n != headerlen) {
163 err = got_ferror(*blobfile, GOT_ERR_IO);
164 goto done;
166 *blobsize += headerlen;
167 for (;;) {
168 char buf[PATH_MAX * 8];
169 ssize_t inlen;
171 if (S_ISLNK(sb.st_mode)) {
172 inlen = readlink(ondisk_path, buf, sizeof(buf));
173 if (inlen == -1) {
174 err = got_error_from_errno("readlink");
175 goto done;
177 } else {
178 inlen = read(fd, buf, sizeof(buf));
179 if (inlen == -1) {
180 err = got_error_from_errno("read");
181 goto done;
184 if (inlen == 0)
185 break; /* EOF */
186 SHA1Update(&sha1_ctx, buf, inlen);
187 n = fwrite(buf, 1, inlen, *blobfile);
188 if (n != inlen) {
189 err = got_ferror(*blobfile, GOT_ERR_IO);
190 goto done;
192 *blobsize += n;
193 if (S_ISLNK(sb.st_mode))
194 break;
197 *id = malloc(sizeof(**id));
198 if (*id == NULL) {
199 err = got_error_from_errno("malloc");
200 goto done;
202 SHA1Final((*id)->sha1, &sha1_ctx);
204 if (fflush(*blobfile) != 0) {
205 err = got_error_from_errno("fflush");
206 goto done;
208 rewind(*blobfile);
209 done:
210 free(header);
211 if (fd != -1 && close(fd) == -1 && err == NULL)
212 err = got_error_from_errno("close");
213 if (err) {
214 free(*id);
215 *id = NULL;
216 if (*blobfile) {
217 fclose(*blobfile);
218 *blobfile = NULL;
221 return err;
224 const struct got_error *
225 got_object_blob_create(struct got_object_id **id, const char *ondisk_path,
226 struct got_repository *repo)
228 const struct got_error *err = NULL;
229 FILE *blobfile = NULL;
230 off_t blobsize;
232 err = got_object_blob_file_create(id, &blobfile, &blobsize,
233 ondisk_path);
234 if (err)
235 return err;
237 err = create_object_file(*id, blobfile, blobsize, repo);
238 if (fclose(blobfile) == EOF && err == NULL)
239 err = got_error_from_errno("fclose");
240 if (err) {
241 free(*id);
242 *id = NULL;
244 return err;
247 static const struct got_error *
248 te_mode2str(char *buf, size_t len, struct got_tree_entry *te)
250 int ret;
251 mode_t mode;
253 /*
254 * Some Git implementations are picky about modes seen in tree entries.
255 * For best compatibility we normalize the file/directory mode here.
256 */
257 if (S_ISREG(te->mode)) {
258 mode = GOT_DEFAULT_FILE_MODE;
259 if (te->mode & (S_IXUSR | S_IXGRP | S_IXOTH))
260 mode |= S_IXUSR | S_IXGRP | S_IXOTH;
261 } else if (got_object_tree_entry_is_submodule(te))
262 mode = S_IFDIR | S_IFLNK;
263 else if (S_ISLNK(te->mode))
264 mode = S_IFLNK; /* Git leaves all the other bits unset. */
265 else if (S_ISDIR(te->mode))
266 mode = S_IFDIR; /* Git leaves all the other bits unset. */
267 else
268 return got_error(GOT_ERR_BAD_FILETYPE);
270 ret = snprintf(buf, len, "%o ", mode);
271 if (ret == -1 || ret >= len)
272 return got_error(GOT_ERR_NO_SPACE);
273 return NULL;
276 /*
277 * Git expects directory tree entries to be sorted with an imaginary slash
278 * appended to their name, and will break otherwise. Let's be nice.
279 * This function is intended to be used with mergesort(3) to sort an
280 * array of pointers to struct got_tree_entry objects.
281 */
282 static int
283 sort_tree_entries_the_way_git_likes_it(const void *arg1, const void *arg2)
285 struct got_tree_entry * const *te1 = arg1;
286 struct got_tree_entry * const *te2 = arg2;
287 char name1[NAME_MAX + 2];
288 char name2[NAME_MAX + 2];
290 strlcpy(name1, (*te1)->name, sizeof(name1));
291 strlcpy(name2, (*te2)->name, sizeof(name2));
292 if (S_ISDIR((*te1)->mode))
293 strlcat(name1, "/", sizeof(name1));
294 if (S_ISDIR((*te2)->mode))
295 strlcat(name2, "/", sizeof(name2));
296 return strcmp(name1, name2);
299 const struct got_error *
300 got_object_tree_create(struct got_object_id **id,
301 struct got_pathlist_head *paths, int nentries, struct got_repository *repo)
303 const struct got_error *err = NULL;
304 char modebuf[sizeof("100644 ")];
305 SHA1_CTX sha1_ctx;
306 char *header = NULL;
307 size_t headerlen, len = 0, n;
308 FILE *treefile = NULL;
309 off_t treesize = 0;
310 struct got_pathlist_entry *pe;
311 struct got_tree_entry **sorted_entries;
312 struct got_tree_entry *te;
313 int i;
315 *id = NULL;
317 SHA1Init(&sha1_ctx);
319 sorted_entries = calloc(nentries, sizeof(struct got_tree_entry *));
320 if (sorted_entries == NULL)
321 return got_error_from_errno("calloc");
323 i = 0;
324 TAILQ_FOREACH(pe, paths, entry)
325 sorted_entries[i++] = pe->data;
326 mergesort(sorted_entries, nentries, sizeof(struct got_tree_entry *),
327 sort_tree_entries_the_way_git_likes_it);
329 for (i = 0; i < nentries; i++) {
330 te = sorted_entries[i];
331 err = te_mode2str(modebuf, sizeof(modebuf), te);
332 if (err)
333 goto done;
334 len += strlen(modebuf) + strlen(te->name) + 1 +
335 SHA1_DIGEST_LENGTH;
338 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TREE, len) == -1) {
339 err = got_error_from_errno("asprintf");
340 goto done;
342 headerlen = strlen(header) + 1;
343 SHA1Update(&sha1_ctx, header, headerlen);
345 treefile = got_opentemp();
346 if (treefile == NULL) {
347 err = got_error_from_errno("got_opentemp");
348 goto done;
351 n = fwrite(header, 1, headerlen, treefile);
352 if (n != headerlen) {
353 err = got_ferror(treefile, GOT_ERR_IO);
354 goto done;
356 treesize += headerlen;
358 for (i = 0; i < nentries; i++) {
359 te = sorted_entries[i];
360 err = te_mode2str(modebuf, sizeof(modebuf), te);
361 if (err)
362 goto done;
363 len = strlen(modebuf);
364 n = fwrite(modebuf, 1, len, treefile);
365 if (n != len) {
366 err = got_ferror(treefile, GOT_ERR_IO);
367 goto done;
369 SHA1Update(&sha1_ctx, modebuf, len);
370 treesize += n;
372 len = strlen(te->name) + 1; /* must include NUL */
373 n = fwrite(te->name, 1, len, treefile);
374 if (n != len) {
375 err = got_ferror(treefile, GOT_ERR_IO);
376 goto done;
378 SHA1Update(&sha1_ctx, te->name, len);
379 treesize += n;
381 len = SHA1_DIGEST_LENGTH;
382 n = fwrite(te->id.sha1, 1, len, treefile);
383 if (n != len) {
384 err = got_ferror(treefile, GOT_ERR_IO);
385 goto done;
387 SHA1Update(&sha1_ctx, te->id.sha1, len);
388 treesize += n;
391 *id = malloc(sizeof(**id));
392 if (*id == NULL) {
393 err = got_error_from_errno("malloc");
394 goto done;
396 SHA1Final((*id)->sha1, &sha1_ctx);
398 if (fflush(treefile) != 0) {
399 err = got_error_from_errno("fflush");
400 goto done;
402 rewind(treefile);
404 err = create_object_file(*id, treefile, treesize, repo);
405 done:
406 free(header);
407 free(sorted_entries);
408 if (treefile && fclose(treefile) == EOF && err == NULL)
409 err = got_error_from_errno("fclose");
410 if (err) {
411 free(*id);
412 *id = NULL;
414 return err;
417 const struct got_error *
418 got_object_commit_create(struct got_object_id **id,
419 struct got_object_id *tree_id, struct got_object_id_queue *parent_ids,
420 int nparents, const char *author, time_t author_time,
421 const char *committer, time_t committer_time,
422 const char *logmsg, struct got_repository *repo)
424 const struct got_error *err = NULL;
425 SHA1_CTX sha1_ctx;
426 char *header = NULL, *tree_str = NULL;
427 char *author_str = NULL, *committer_str = NULL;
428 char *id_str = NULL;
429 size_t headerlen, len = 0, n;
430 FILE *commitfile = NULL;
431 off_t commitsize = 0;
432 struct got_object_qid *qid;
433 char *msg0, *msg;
435 *id = NULL;
437 SHA1Init(&sha1_ctx);
439 msg0 = strdup(logmsg);
440 if (msg0 == NULL)
441 return got_error_from_errno("strdup");
442 msg = msg0;
444 while (isspace((unsigned char)msg[0]))
445 msg++;
446 len = strlen(msg);
447 while (len > 0 && isspace((unsigned char)msg[len - 1])) {
448 msg[len - 1] = '\0';
449 len--;
452 if (asprintf(&author_str, "%s%s %lld +0000\n",
453 GOT_COMMIT_LABEL_AUTHOR, author, (long long)author_time) == -1)
454 return got_error_from_errno("asprintf");
456 if (asprintf(&committer_str, "%s%s %lld +0000\n",
457 GOT_COMMIT_LABEL_COMMITTER, committer ? committer : author,
458 (long long)(committer ? committer_time : author_time))
459 == -1) {
460 err = got_error_from_errno("asprintf");
461 goto done;
464 len = strlen(GOT_COMMIT_LABEL_TREE) + SHA1_DIGEST_STRING_LENGTH +
465 nparents *
466 (strlen(GOT_COMMIT_LABEL_PARENT) + SHA1_DIGEST_STRING_LENGTH) +
467 + strlen(author_str) + strlen(committer_str) + 2 + strlen(msg);
469 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_COMMIT, len) == -1) {
470 err = got_error_from_errno("asprintf");
471 goto done;
473 headerlen = strlen(header) + 1;
474 SHA1Update(&sha1_ctx, header, headerlen);
476 commitfile = got_opentemp();
477 if (commitfile == NULL) {
478 err = got_error_from_errno("got_opentemp");
479 goto done;
482 n = fwrite(header, 1, headerlen, commitfile);
483 if (n != headerlen) {
484 err = got_ferror(commitfile, GOT_ERR_IO);
485 goto done;
487 commitsize += headerlen;
489 err = got_object_id_str(&id_str, tree_id);
490 if (err)
491 goto done;
492 if (asprintf(&tree_str, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str)
493 == -1) {
494 err = got_error_from_errno("asprintf");
495 goto done;
497 len = strlen(tree_str);
498 SHA1Update(&sha1_ctx, tree_str, len);
499 n = fwrite(tree_str, 1, len, commitfile);
500 if (n != len) {
501 err = got_ferror(commitfile, GOT_ERR_IO);
502 goto done;
504 commitsize += n;
506 if (parent_ids) {
507 free(id_str);
508 id_str = NULL;
509 STAILQ_FOREACH(qid, parent_ids, entry) {
510 char *parent_str = NULL;
512 err = got_object_id_str(&id_str, &qid->id);
513 if (err)
514 goto done;
515 if (asprintf(&parent_str, "%s%s\n",
516 GOT_COMMIT_LABEL_PARENT, id_str) == -1) {
517 err = got_error_from_errno("asprintf");
518 goto done;
520 len = strlen(parent_str);
521 SHA1Update(&sha1_ctx, parent_str, len);
522 n = fwrite(parent_str, 1, len, commitfile);
523 if (n != len) {
524 err = got_ferror(commitfile, GOT_ERR_IO);
525 free(parent_str);
526 goto done;
528 commitsize += n;
529 free(parent_str);
530 free(id_str);
531 id_str = NULL;
535 len = strlen(author_str);
536 SHA1Update(&sha1_ctx, author_str, len);
537 n = fwrite(author_str, 1, len, commitfile);
538 if (n != len) {
539 err = got_ferror(commitfile, GOT_ERR_IO);
540 goto done;
542 commitsize += n;
544 len = strlen(committer_str);
545 SHA1Update(&sha1_ctx, committer_str, len);
546 n = fwrite(committer_str, 1, len, commitfile);
547 if (n != len) {
548 err = got_ferror(commitfile, GOT_ERR_IO);
549 goto done;
551 commitsize += n;
553 SHA1Update(&sha1_ctx, "\n", 1);
554 n = fwrite("\n", 1, 1, commitfile);
555 if (n != 1) {
556 err = got_ferror(commitfile, GOT_ERR_IO);
557 goto done;
559 commitsize += n;
561 len = strlen(msg);
562 SHA1Update(&sha1_ctx, msg, len);
563 n = fwrite(msg, 1, len, commitfile);
564 if (n != len) {
565 err = got_ferror(commitfile, GOT_ERR_IO);
566 goto done;
568 commitsize += n;
570 SHA1Update(&sha1_ctx, "\n", 1);
571 n = fwrite("\n", 1, 1, commitfile);
572 if (n != 1) {
573 err = got_ferror(commitfile, GOT_ERR_IO);
574 goto done;
576 commitsize += n;
578 *id = malloc(sizeof(**id));
579 if (*id == NULL) {
580 err = got_error_from_errno("malloc");
581 goto done;
583 SHA1Final((*id)->sha1, &sha1_ctx);
585 if (fflush(commitfile) != 0) {
586 err = got_error_from_errno("fflush");
587 goto done;
589 rewind(commitfile);
591 err = create_object_file(*id, commitfile, commitsize, repo);
592 done:
593 free(id_str);
594 free(msg0);
595 free(header);
596 free(tree_str);
597 free(author_str);
598 free(committer_str);
599 if (commitfile && fclose(commitfile) == EOF && err == NULL)
600 err = got_error_from_errno("fclose");
601 if (err) {
602 free(*id);
603 *id = NULL;
605 return err;
608 const struct got_error *
609 got_object_tag_create(struct got_object_id **id,
610 const char *tag_name, struct got_object_id *object_id, const char *tagger,
611 time_t tagger_time, const char *tagmsg, struct got_repository *repo)
613 const struct got_error *err = NULL;
614 SHA1_CTX sha1_ctx;
615 char *header = NULL;
616 char *tag_str = NULL, *tagger_str = NULL;
617 char *id_str = NULL, *obj_str = NULL, *type_str = NULL;
618 size_t headerlen, len = 0, n;
619 FILE *tagfile = NULL;
620 off_t tagsize = 0;
621 char *msg0 = NULL, *msg;
622 const char *obj_type_str;
623 int obj_type;
625 *id = NULL;
627 SHA1Init(&sha1_ctx);
629 err = got_object_id_str(&id_str, object_id);
630 if (err)
631 goto done;
632 if (asprintf(&obj_str, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str) == -1) {
633 err = got_error_from_errno("asprintf");
634 goto done;
637 err = got_object_get_type(&obj_type, repo, object_id);
638 if (err)
639 goto done;
641 switch (obj_type) {
642 case GOT_OBJ_TYPE_BLOB:
643 obj_type_str = GOT_OBJ_LABEL_BLOB;
644 break;
645 case GOT_OBJ_TYPE_TREE:
646 obj_type_str = GOT_OBJ_LABEL_TREE;
647 break;
648 case GOT_OBJ_TYPE_COMMIT:
649 obj_type_str = GOT_OBJ_LABEL_COMMIT;
650 break;
651 case GOT_OBJ_TYPE_TAG:
652 obj_type_str = GOT_OBJ_LABEL_TAG;
653 break;
654 default:
655 err = got_error(GOT_ERR_OBJ_TYPE);
656 goto done;
659 if (asprintf(&type_str, "%s%s\n", GOT_TAG_LABEL_TYPE,
660 obj_type_str) == -1) {
661 err = got_error_from_errno("asprintf");
662 goto done;
665 if (asprintf(&tag_str, "%s%s\n", GOT_TAG_LABEL_TAG, tag_name) == -1) {
666 err = got_error_from_errno("asprintf");
667 goto done;
670 if (asprintf(&tagger_str, "%s%s %lld +0000\n",
671 GOT_TAG_LABEL_TAGGER, tagger, (long long)tagger_time) == -1)
672 return got_error_from_errno("asprintf");
674 msg0 = strdup(tagmsg);
675 if (msg0 == NULL) {
676 err = got_error_from_errno("strdup");
677 goto done;
679 msg = msg0;
681 while (isspace((unsigned char)msg[0]))
682 msg++;
684 len = strlen(obj_str) + strlen(type_str) + strlen(tag_str) +
685 strlen(tagger_str) + 1 + strlen(msg) + 1;
687 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TAG, len) == -1) {
688 err = got_error_from_errno("asprintf");
689 goto done;
692 headerlen = strlen(header) + 1;
693 SHA1Update(&sha1_ctx, header, headerlen);
695 tagfile = got_opentemp();
696 if (tagfile == NULL) {
697 err = got_error_from_errno("got_opentemp");
698 goto done;
701 n = fwrite(header, 1, headerlen, tagfile);
702 if (n != headerlen) {
703 err = got_ferror(tagfile, GOT_ERR_IO);
704 goto done;
706 tagsize += headerlen;
707 len = strlen(obj_str);
708 SHA1Update(&sha1_ctx, obj_str, len);
709 n = fwrite(obj_str, 1, len, tagfile);
710 if (n != len) {
711 err = got_ferror(tagfile, GOT_ERR_IO);
712 goto done;
714 tagsize += n;
715 len = strlen(type_str);
716 SHA1Update(&sha1_ctx, type_str, len);
717 n = fwrite(type_str, 1, len, tagfile);
718 if (n != len) {
719 err = got_ferror(tagfile, GOT_ERR_IO);
720 goto done;
722 tagsize += n;
724 len = strlen(tag_str);
725 SHA1Update(&sha1_ctx, tag_str, len);
726 n = fwrite(tag_str, 1, len, tagfile);
727 if (n != len) {
728 err = got_ferror(tagfile, GOT_ERR_IO);
729 goto done;
731 tagsize += n;
733 len = strlen(tagger_str);
734 SHA1Update(&sha1_ctx, tagger_str, len);
735 n = fwrite(tagger_str, 1, len, tagfile);
736 if (n != len) {
737 err = got_ferror(tagfile, GOT_ERR_IO);
738 goto done;
740 tagsize += n;
742 SHA1Update(&sha1_ctx, "\n", 1);
743 n = fwrite("\n", 1, 1, tagfile);
744 if (n != 1) {
745 err = got_ferror(tagfile, GOT_ERR_IO);
746 goto done;
748 tagsize += n;
750 len = strlen(msg);
751 SHA1Update(&sha1_ctx, msg, len);
752 n = fwrite(msg, 1, len, tagfile);
753 if (n != len) {
754 err = got_ferror(tagfile, GOT_ERR_IO);
755 goto done;
757 tagsize += n;
759 SHA1Update(&sha1_ctx, "\n", 1);
760 n = fwrite("\n", 1, 1, tagfile);
761 if (n != 1) {
762 err = got_ferror(tagfile, GOT_ERR_IO);
763 goto done;
765 tagsize += n;
767 *id = malloc(sizeof(**id));
768 if (*id == NULL) {
769 err = got_error_from_errno("malloc");
770 goto done;
772 SHA1Final((*id)->sha1, &sha1_ctx);
774 if (fflush(tagfile) != 0) {
775 err = got_error_from_errno("fflush");
776 goto done;
778 rewind(tagfile);
780 err = create_object_file(*id, tagfile, tagsize, repo);
781 done:
782 free(msg0);
783 free(header);
784 free(obj_str);
785 free(tagger_str);
786 if (tagfile && fclose(tagfile) == EOF && err == NULL)
787 err = got_error_from_errno("fclose");
788 if (err) {
789 free(*id);
790 *id = NULL;
792 return err;