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>
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 <sha1.h>
30 #include <unistd.h>
31 #include <zlib.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 #ifndef nitems
47 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
48 #endif
50 static const struct got_error *
51 create_object_file(struct got_object_id *id, FILE *content,
52 off_t content_len, struct got_repository *repo)
53 {
54 const struct got_error *err = NULL, *unlock_err = NULL;
55 char *objpath = NULL, *tmppath = NULL;
56 FILE *tmpfile = NULL;
57 struct got_lockfile *lf = NULL;
58 off_t tmplen = 0;
60 err = got_object_get_path(&objpath, id, repo);
61 if (err)
62 return err;
64 err = got_opentemp_named(&tmppath, &tmpfile, objpath);
65 if (err) {
66 char *parent_path;
67 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
68 goto done;
69 err = got_path_dirname(&parent_path, objpath);
70 if (err)
71 goto done;
72 err = got_path_mkdir(parent_path);
73 free(parent_path);
74 if (err)
75 goto done;
76 err = got_opentemp_named(&tmppath, &tmpfile, objpath);
77 if (err)
78 goto done;
79 }
81 if (fchmod(fileno(tmpfile), GOT_DEFAULT_FILE_MODE) != 0) {
82 err = got_error_from_errno2("fchmod", tmppath);
83 goto done;
84 }
86 err = got_deflate_to_file(&tmplen, content, content_len, tmpfile, NULL);
87 if (err)
88 goto done;
90 err = got_lockfile_lock(&lf, objpath, -1);
91 if (err)
92 goto done;
94 if (rename(tmppath, objpath) != 0) {
95 err = got_error_from_errno3("rename", tmppath, objpath);
96 goto done;
97 }
98 free(tmppath);
99 tmppath = NULL;
100 done:
101 free(objpath);
102 if (tmppath) {
103 if (unlink(tmppath) != 0 && err == NULL)
104 err = got_error_from_errno2("unlink", tmppath);
105 free(tmppath);
107 if (tmpfile && fclose(tmpfile) == EOF && err == NULL)
108 err = got_error_from_errno("fclose");
109 if (lf)
110 unlock_err = got_lockfile_unlock(lf, -1);
111 return err ? err : unlock_err;
114 const struct got_error *
115 got_object_blob_file_create(struct got_object_id **id, FILE **blobfile,
116 off_t *blobsize, const char *ondisk_path)
118 const struct got_error *err = NULL;
119 char *header = NULL;
120 int fd = -1;
121 struct stat sb;
122 SHA1_CTX sha1_ctx;
123 size_t headerlen = 0, n;
125 *id = NULL;
126 *blobfile = NULL;
127 *blobsize = 0;
129 SHA1Init(&sha1_ctx);
131 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
132 if (fd == -1) {
133 if (!got_err_open_nofollow_on_symlink())
134 return got_error_from_errno2("open", ondisk_path);
136 if (lstat(ondisk_path, &sb) == -1) {
137 err = got_error_from_errno2("lstat", ondisk_path);
138 goto done;
140 } else if (fstat(fd, &sb) == -1) {
141 err = got_error_from_errno2("fstat", ondisk_path);
142 goto done;
145 if (asprintf(&header, "%s %lld", GOT_OBJ_LABEL_BLOB,
146 (long long)sb.st_size) == -1) {
147 err = got_error_from_errno("asprintf");
148 goto done;
150 headerlen = strlen(header) + 1;
151 SHA1Update(&sha1_ctx, header, headerlen);
153 *blobfile = got_opentemp();
154 if (*blobfile == NULL) {
155 err = got_error_from_errno("got_opentemp");
156 goto done;
159 n = fwrite(header, 1, headerlen, *blobfile);
160 if (n != headerlen) {
161 err = got_ferror(*blobfile, GOT_ERR_IO);
162 goto done;
164 *blobsize += headerlen;
165 for (;;) {
166 char buf[PATH_MAX * 8];
167 ssize_t inlen;
169 if (S_ISLNK(sb.st_mode)) {
170 inlen = readlink(ondisk_path, buf, sizeof(buf));
171 if (inlen == -1) {
172 err = got_error_from_errno("readlink");
173 goto done;
175 } else {
176 inlen = read(fd, buf, sizeof(buf));
177 if (inlen == -1) {
178 err = got_error_from_errno("read");
179 goto done;
182 if (inlen == 0)
183 break; /* EOF */
184 SHA1Update(&sha1_ctx, buf, inlen);
185 n = fwrite(buf, 1, inlen, *blobfile);
186 if (n != inlen) {
187 err = got_ferror(*blobfile, GOT_ERR_IO);
188 goto done;
190 *blobsize += n;
191 if (S_ISLNK(sb.st_mode))
192 break;
195 *id = malloc(sizeof(**id));
196 if (*id == NULL) {
197 err = got_error_from_errno("malloc");
198 goto done;
200 SHA1Final((*id)->sha1, &sha1_ctx);
202 if (fflush(*blobfile) != 0) {
203 err = got_error_from_errno("fflush");
204 goto done;
206 rewind(*blobfile);
207 done:
208 free(header);
209 if (fd != -1 && close(fd) == -1 && err == NULL)
210 err = got_error_from_errno("close");
211 if (err) {
212 free(*id);
213 *id = NULL;
214 if (*blobfile) {
215 fclose(*blobfile);
216 *blobfile = NULL;
219 return err;
222 const struct got_error *
223 got_object_blob_create(struct got_object_id **id, const char *ondisk_path,
224 struct got_repository *repo)
226 const struct got_error *err = NULL;
227 FILE *blobfile = NULL;
228 off_t blobsize;
230 err = got_object_blob_file_create(id, &blobfile, &blobsize,
231 ondisk_path);
232 if (err)
233 return err;
235 err = create_object_file(*id, blobfile, blobsize, repo);
236 if (fclose(blobfile) == EOF && err == NULL)
237 err = got_error_from_errno("fclose");
238 if (err) {
239 free(*id);
240 *id = NULL;
242 return err;
245 static const struct got_error *
246 te_mode2str(char *buf, size_t len, struct got_tree_entry *te)
248 int ret;
249 mode_t mode;
251 /*
252 * Some Git implementations are picky about modes seen in tree entries.
253 * For best compatibility we normalize the file/directory mode here.
254 */
255 if (S_ISREG(te->mode)) {
256 mode = GOT_DEFAULT_FILE_MODE;
257 if (te->mode & (S_IXUSR | S_IXGRP | S_IXOTH))
258 mode |= S_IXUSR | S_IXGRP | S_IXOTH;
259 } else if (got_object_tree_entry_is_submodule(te))
260 mode = S_IFDIR | S_IFLNK;
261 else if (S_ISLNK(te->mode))
262 mode = S_IFLNK; /* Git leaves all the other bits unset. */
263 else if (S_ISDIR(te->mode))
264 mode = S_IFDIR; /* Git leaves all the other bits unset. */
265 else
266 return got_error(GOT_ERR_BAD_FILETYPE);
268 ret = snprintf(buf, len, "%o ", mode);
269 if (ret == -1 || ret >= len)
270 return got_error(GOT_ERR_NO_SPACE);
271 return NULL;
274 /*
275 * Git expects directory tree entries to be sorted with an imaginary slash
276 * appended to their name, and will break otherwise. Let's be nice.
277 * This function is intended to be used with mergesort(3) to sort an
278 * array of pointers to struct got_tree_entry objects.
279 */
280 static int
281 sort_tree_entries_the_way_git_likes_it(const void *arg1, const void *arg2)
283 struct got_tree_entry * const *te1 = arg1;
284 struct got_tree_entry * const *te2 = arg2;
285 char name1[NAME_MAX + 2];
286 char name2[NAME_MAX + 2];
288 strlcpy(name1, (*te1)->name, sizeof(name1));
289 strlcpy(name2, (*te2)->name, sizeof(name2));
290 if (S_ISDIR((*te1)->mode))
291 strlcat(name1, "/", sizeof(name1));
292 if (S_ISDIR((*te2)->mode))
293 strlcat(name2, "/", sizeof(name2));
294 return strcmp(name1, name2);
297 const struct got_error *
298 got_object_tree_create(struct got_object_id **id,
299 struct got_pathlist_head *paths, int nentries, struct got_repository *repo)
301 const struct got_error *err = NULL;
302 char modebuf[sizeof("100644 ")];
303 SHA1_CTX sha1_ctx;
304 char *header = NULL;
305 size_t headerlen, len = 0, n;
306 FILE *treefile = NULL;
307 off_t treesize = 0;
308 struct got_pathlist_entry *pe;
309 struct got_tree_entry **sorted_entries;
310 struct got_tree_entry *te;
311 int i;
313 *id = NULL;
315 SHA1Init(&sha1_ctx);
317 sorted_entries = calloc(nentries, sizeof(struct got_tree_entry *));
318 if (sorted_entries == NULL)
319 return got_error_from_errno("calloc");
321 i = 0;
322 TAILQ_FOREACH(pe, paths, entry)
323 sorted_entries[i++] = pe->data;
324 mergesort(sorted_entries, nentries, sizeof(struct got_tree_entry *),
325 sort_tree_entries_the_way_git_likes_it);
327 for (i = 0; i < nentries; i++) {
328 te = sorted_entries[i];
329 err = te_mode2str(modebuf, sizeof(modebuf), te);
330 if (err)
331 goto done;
332 len += strlen(modebuf) + strlen(te->name) + 1 +
333 SHA1_DIGEST_LENGTH;
336 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TREE, len) == -1) {
337 err = got_error_from_errno("asprintf");
338 goto done;
340 headerlen = strlen(header) + 1;
341 SHA1Update(&sha1_ctx, header, headerlen);
343 treefile = got_opentemp();
344 if (treefile == NULL) {
345 err = got_error_from_errno("got_opentemp");
346 goto done;
349 n = fwrite(header, 1, headerlen, treefile);
350 if (n != headerlen) {
351 err = got_ferror(treefile, GOT_ERR_IO);
352 goto done;
354 treesize += headerlen;
356 for (i = 0; i < nentries; i++) {
357 te = sorted_entries[i];
358 err = te_mode2str(modebuf, sizeof(modebuf), te);
359 if (err)
360 goto done;
361 len = strlen(modebuf);
362 n = fwrite(modebuf, 1, len, treefile);
363 if (n != len) {
364 err = got_ferror(treefile, GOT_ERR_IO);
365 goto done;
367 SHA1Update(&sha1_ctx, modebuf, len);
368 treesize += n;
370 len = strlen(te->name) + 1; /* must include NUL */
371 n = fwrite(te->name, 1, len, treefile);
372 if (n != len) {
373 err = got_ferror(treefile, GOT_ERR_IO);
374 goto done;
376 SHA1Update(&sha1_ctx, te->name, len);
377 treesize += n;
379 len = SHA1_DIGEST_LENGTH;
380 n = fwrite(te->id.sha1, 1, len, treefile);
381 if (n != len) {
382 err = got_ferror(treefile, GOT_ERR_IO);
383 goto done;
385 SHA1Update(&sha1_ctx, te->id.sha1, len);
386 treesize += n;
389 *id = malloc(sizeof(**id));
390 if (*id == NULL) {
391 err = got_error_from_errno("malloc");
392 goto done;
394 SHA1Final((*id)->sha1, &sha1_ctx);
396 if (fflush(treefile) != 0) {
397 err = got_error_from_errno("fflush");
398 goto done;
400 rewind(treefile);
402 err = create_object_file(*id, treefile, treesize, repo);
403 done:
404 free(header);
405 free(sorted_entries);
406 if (treefile && fclose(treefile) == EOF && err == NULL)
407 err = got_error_from_errno("fclose");
408 if (err) {
409 free(*id);
410 *id = NULL;
412 return err;
415 const struct got_error *
416 got_object_commit_create(struct got_object_id **id,
417 struct got_object_id *tree_id, struct got_object_id_queue *parent_ids,
418 int nparents, const char *author, time_t author_time,
419 const char *committer, time_t committer_time,
420 const char *logmsg, struct got_repository *repo)
422 const struct got_error *err = NULL;
423 SHA1_CTX sha1_ctx;
424 char *header = NULL, *tree_str = NULL;
425 char *author_str = NULL, *committer_str = NULL;
426 char *id_str = NULL;
427 size_t headerlen, len = 0, n;
428 FILE *commitfile = NULL;
429 off_t commitsize = 0;
430 struct got_object_qid *qid;
431 char *msg0, *msg;
433 *id = NULL;
435 SHA1Init(&sha1_ctx);
437 msg0 = strdup(logmsg);
438 if (msg0 == NULL)
439 return got_error_from_errno("strdup");
440 msg = msg0;
442 while (isspace((unsigned char)msg[0]))
443 msg++;
444 len = strlen(msg);
445 while (len > 0 && isspace((unsigned char)msg[len - 1])) {
446 msg[len - 1] = '\0';
447 len--;
450 if (asprintf(&author_str, "%s%s %lld +0000\n",
451 GOT_COMMIT_LABEL_AUTHOR, author, (long long)author_time) == -1)
452 return got_error_from_errno("asprintf");
454 if (asprintf(&committer_str, "%s%s %lld +0000\n",
455 GOT_COMMIT_LABEL_COMMITTER, committer ? committer : author,
456 (long long)(committer ? committer_time : author_time))
457 == -1) {
458 err = got_error_from_errno("asprintf");
459 goto done;
462 len = strlen(GOT_COMMIT_LABEL_TREE) + SHA1_DIGEST_STRING_LENGTH +
463 nparents *
464 (strlen(GOT_COMMIT_LABEL_PARENT) + SHA1_DIGEST_STRING_LENGTH) +
465 + strlen(author_str) + strlen(committer_str) + 2 + strlen(msg);
467 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_COMMIT, len) == -1) {
468 err = got_error_from_errno("asprintf");
469 goto done;
471 headerlen = strlen(header) + 1;
472 SHA1Update(&sha1_ctx, header, headerlen);
474 commitfile = got_opentemp();
475 if (commitfile == NULL) {
476 err = got_error_from_errno("got_opentemp");
477 goto done;
480 n = fwrite(header, 1, headerlen, commitfile);
481 if (n != headerlen) {
482 err = got_ferror(commitfile, GOT_ERR_IO);
483 goto done;
485 commitsize += headerlen;
487 err = got_object_id_str(&id_str, tree_id);
488 if (err)
489 goto done;
490 if (asprintf(&tree_str, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str)
491 == -1) {
492 err = got_error_from_errno("asprintf");
493 goto done;
495 len = strlen(tree_str);
496 SHA1Update(&sha1_ctx, tree_str, len);
497 n = fwrite(tree_str, 1, len, commitfile);
498 if (n != len) {
499 err = got_ferror(commitfile, GOT_ERR_IO);
500 goto done;
502 commitsize += n;
504 if (parent_ids) {
505 free(id_str);
506 id_str = NULL;
507 STAILQ_FOREACH(qid, parent_ids, entry) {
508 char *parent_str = NULL;
510 err = got_object_id_str(&id_str, &qid->id);
511 if (err)
512 goto done;
513 if (asprintf(&parent_str, "%s%s\n",
514 GOT_COMMIT_LABEL_PARENT, id_str) == -1) {
515 err = got_error_from_errno("asprintf");
516 goto done;
518 len = strlen(parent_str);
519 SHA1Update(&sha1_ctx, parent_str, len);
520 n = fwrite(parent_str, 1, len, commitfile);
521 if (n != len) {
522 err = got_ferror(commitfile, GOT_ERR_IO);
523 free(parent_str);
524 goto done;
526 commitsize += n;
527 free(parent_str);
528 free(id_str);
529 id_str = NULL;
533 len = strlen(author_str);
534 SHA1Update(&sha1_ctx, author_str, len);
535 n = fwrite(author_str, 1, len, commitfile);
536 if (n != len) {
537 err = got_ferror(commitfile, GOT_ERR_IO);
538 goto done;
540 commitsize += n;
542 len = strlen(committer_str);
543 SHA1Update(&sha1_ctx, committer_str, len);
544 n = fwrite(committer_str, 1, len, commitfile);
545 if (n != len) {
546 err = got_ferror(commitfile, GOT_ERR_IO);
547 goto done;
549 commitsize += n;
551 SHA1Update(&sha1_ctx, "\n", 1);
552 n = fwrite("\n", 1, 1, commitfile);
553 if (n != 1) {
554 err = got_ferror(commitfile, GOT_ERR_IO);
555 goto done;
557 commitsize += n;
559 len = strlen(msg);
560 SHA1Update(&sha1_ctx, msg, len);
561 n = fwrite(msg, 1, len, commitfile);
562 if (n != len) {
563 err = got_ferror(commitfile, GOT_ERR_IO);
564 goto done;
566 commitsize += n;
568 SHA1Update(&sha1_ctx, "\n", 1);
569 n = fwrite("\n", 1, 1, commitfile);
570 if (n != 1) {
571 err = got_ferror(commitfile, GOT_ERR_IO);
572 goto done;
574 commitsize += n;
576 *id = malloc(sizeof(**id));
577 if (*id == NULL) {
578 err = got_error_from_errno("malloc");
579 goto done;
581 SHA1Final((*id)->sha1, &sha1_ctx);
583 if (fflush(commitfile) != 0) {
584 err = got_error_from_errno("fflush");
585 goto done;
587 rewind(commitfile);
589 err = create_object_file(*id, commitfile, commitsize, repo);
590 done:
591 free(id_str);
592 free(msg0);
593 free(header);
594 free(tree_str);
595 free(author_str);
596 free(committer_str);
597 if (commitfile && fclose(commitfile) == EOF && err == NULL)
598 err = got_error_from_errno("fclose");
599 if (err) {
600 free(*id);
601 *id = NULL;
603 return err;
606 const struct got_error *
607 got_object_tag_create(struct got_object_id **id,
608 const char *tag_name, struct got_object_id *object_id, const char *tagger,
609 time_t tagger_time, const char *tagmsg, struct got_repository *repo)
611 const struct got_error *err = NULL;
612 SHA1_CTX sha1_ctx;
613 char *header = NULL;
614 char *tag_str = NULL, *tagger_str = NULL;
615 char *id_str = NULL, *obj_str = NULL, *type_str = NULL;
616 size_t headerlen, len = 0, n;
617 FILE *tagfile = NULL;
618 off_t tagsize = 0;
619 char *msg0 = NULL, *msg;
620 const char *obj_type_str;
621 int obj_type;
623 *id = NULL;
625 SHA1Init(&sha1_ctx);
627 err = got_object_id_str(&id_str, object_id);
628 if (err)
629 goto done;
630 if (asprintf(&obj_str, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str) == -1) {
631 err = got_error_from_errno("asprintf");
632 goto done;
635 err = got_object_get_type(&obj_type, repo, object_id);
636 if (err)
637 goto done;
639 switch (obj_type) {
640 case GOT_OBJ_TYPE_BLOB:
641 obj_type_str = GOT_OBJ_LABEL_BLOB;
642 break;
643 case GOT_OBJ_TYPE_TREE:
644 obj_type_str = GOT_OBJ_LABEL_TREE;
645 break;
646 case GOT_OBJ_TYPE_COMMIT:
647 obj_type_str = GOT_OBJ_LABEL_COMMIT;
648 break;
649 case GOT_OBJ_TYPE_TAG:
650 obj_type_str = GOT_OBJ_LABEL_TAG;
651 break;
652 default:
653 err = got_error(GOT_ERR_OBJ_TYPE);
654 goto done;
657 if (asprintf(&type_str, "%s%s\n", GOT_TAG_LABEL_TYPE,
658 obj_type_str) == -1) {
659 err = got_error_from_errno("asprintf");
660 goto done;
663 if (asprintf(&tag_str, "%s%s\n", GOT_TAG_LABEL_TAG, tag_name) == -1) {
664 err = got_error_from_errno("asprintf");
665 goto done;
668 if (asprintf(&tagger_str, "%s%s %lld +0000\n",
669 GOT_TAG_LABEL_TAGGER, tagger, (long long)tagger_time) == -1)
670 return got_error_from_errno("asprintf");
672 msg0 = strdup(tagmsg);
673 if (msg0 == NULL) {
674 err = got_error_from_errno("strdup");
675 goto done;
677 msg = msg0;
679 while (isspace((unsigned char)msg[0]))
680 msg++;
682 len = strlen(obj_str) + strlen(type_str) + strlen(tag_str) +
683 strlen(tagger_str) + 1 + strlen(msg) + 1;
685 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TAG, len) == -1) {
686 err = got_error_from_errno("asprintf");
687 goto done;
690 headerlen = strlen(header) + 1;
691 SHA1Update(&sha1_ctx, header, headerlen);
693 tagfile = got_opentemp();
694 if (tagfile == NULL) {
695 err = got_error_from_errno("got_opentemp");
696 goto done;
699 n = fwrite(header, 1, headerlen, tagfile);
700 if (n != headerlen) {
701 err = got_ferror(tagfile, GOT_ERR_IO);
702 goto done;
704 tagsize += headerlen;
705 len = strlen(obj_str);
706 SHA1Update(&sha1_ctx, obj_str, len);
707 n = fwrite(obj_str, 1, len, tagfile);
708 if (n != len) {
709 err = got_ferror(tagfile, GOT_ERR_IO);
710 goto done;
712 tagsize += n;
713 len = strlen(type_str);
714 SHA1Update(&sha1_ctx, type_str, len);
715 n = fwrite(type_str, 1, len, tagfile);
716 if (n != len) {
717 err = got_ferror(tagfile, GOT_ERR_IO);
718 goto done;
720 tagsize += n;
722 len = strlen(tag_str);
723 SHA1Update(&sha1_ctx, tag_str, len);
724 n = fwrite(tag_str, 1, len, tagfile);
725 if (n != len) {
726 err = got_ferror(tagfile, GOT_ERR_IO);
727 goto done;
729 tagsize += n;
731 len = strlen(tagger_str);
732 SHA1Update(&sha1_ctx, tagger_str, len);
733 n = fwrite(tagger_str, 1, len, tagfile);
734 if (n != len) {
735 err = got_ferror(tagfile, GOT_ERR_IO);
736 goto done;
738 tagsize += n;
740 SHA1Update(&sha1_ctx, "\n", 1);
741 n = fwrite("\n", 1, 1, tagfile);
742 if (n != 1) {
743 err = got_ferror(tagfile, GOT_ERR_IO);
744 goto done;
746 tagsize += n;
748 len = strlen(msg);
749 SHA1Update(&sha1_ctx, msg, len);
750 n = fwrite(msg, 1, len, tagfile);
751 if (n != len) {
752 err = got_ferror(tagfile, GOT_ERR_IO);
753 goto done;
755 tagsize += n;
757 SHA1Update(&sha1_ctx, "\n", 1);
758 n = fwrite("\n", 1, 1, tagfile);
759 if (n != 1) {
760 err = got_ferror(tagfile, GOT_ERR_IO);
761 goto done;
763 tagsize += n;
765 *id = malloc(sizeof(**id));
766 if (*id == NULL) {
767 err = got_error_from_errno("malloc");
768 goto done;
770 SHA1Final((*id)->sha1, &sha1_ctx);
772 if (fflush(tagfile) != 0) {
773 err = got_error_from_errno("fflush");
774 goto done;
776 rewind(tagfile);
778 err = create_object_file(*id, tagfile, tagsize, repo);
779 done:
780 free(msg0);
781 free(header);
782 free(obj_str);
783 free(tagger_str);
784 if (tagfile && fclose(tagfile) == EOF && err == NULL)
785 err = got_error_from_errno("fclose");
786 if (err) {
787 free(*id);
788 *id = NULL;
790 return err;