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 #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 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 size_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, 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 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;
128 SHA1Init(&sha1_ctx);
130 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
131 if (fd == -1) {
132 if (!got_err_open_nofollow_on_symlink())
133 return got_error_from_errno2("open", ondisk_path);
135 if (lstat(ondisk_path, &sb) == -1) {
136 err = got_error_from_errno2("lstat", ondisk_path);
137 goto done;
139 } else if (fstat(fd, &sb) == -1) {
140 err = got_error_from_errno2("fstat", ondisk_path);
141 goto done;
144 if (asprintf(&header, "%s %lld", GOT_OBJ_LABEL_BLOB,
145 (long long)sb.st_size) == -1) {
146 err = got_error_from_errno("asprintf");
147 goto done;
149 headerlen = strlen(header) + 1;
150 SHA1Update(&sha1_ctx, header, headerlen);
152 *blobfile = got_opentemp();
153 if (*blobfile == NULL) {
154 err = got_error_from_errno("got_opentemp");
155 goto done;
158 n = fwrite(header, 1, headerlen, *blobfile);
159 if (n != headerlen) {
160 err = got_ferror(*blobfile, GOT_ERR_IO);
161 goto done;
163 for (;;) {
164 char buf[PATH_MAX * 8];
165 ssize_t inlen;
167 if (S_ISLNK(sb.st_mode)) {
168 inlen = readlink(ondisk_path, buf, sizeof(buf));
169 if (inlen == -1) {
170 err = got_error_from_errno("readlink");
171 goto done;
173 } else {
174 inlen = read(fd, buf, sizeof(buf));
175 if (inlen == -1) {
176 err = got_error_from_errno("read");
177 goto done;
180 if (inlen == 0)
181 break; /* EOF */
182 SHA1Update(&sha1_ctx, buf, inlen);
183 n = fwrite(buf, 1, inlen, *blobfile);
184 if (n != inlen) {
185 err = got_ferror(*blobfile, GOT_ERR_IO);
186 goto done;
188 if (S_ISLNK(sb.st_mode))
189 break;
192 *id = malloc(sizeof(**id));
193 if (*id == NULL) {
194 err = got_error_from_errno("malloc");
195 goto done;
197 SHA1Final((*id)->sha1, &sha1_ctx);
199 if (fflush(*blobfile) != 0) {
200 err = got_error_from_errno("fflush");
201 goto done;
203 rewind(*blobfile);
204 done:
205 free(header);
206 if (fd != -1 && close(fd) == -1 && err == NULL)
207 err = got_error_from_errno("close");
208 if (err) {
209 free(*id);
210 *id = NULL;
211 if (*blobfile) {
212 fclose(*blobfile);
213 *blobfile = NULL;
216 return err;
219 const struct got_error *
220 got_object_blob_create(struct got_object_id **id, const char *ondisk_path,
221 struct got_repository *repo)
223 const struct got_error *err = NULL;
224 FILE *blobfile = NULL;
226 err = got_object_blob_file_create(id, &blobfile, ondisk_path);
227 if (err)
228 return err;
230 err = create_object_file(*id, blobfile, repo);
231 if (fclose(blobfile) == EOF && err == NULL)
232 err = got_error_from_errno("fclose");
233 if (err) {
234 free(*id);
235 *id = NULL;
237 return err;
240 static const struct got_error *
241 te_mode2str(char *buf, size_t len, struct got_tree_entry *te)
243 int ret;
244 mode_t mode;
246 /*
247 * Some Git implementations are picky about modes seen in tree entries.
248 * For best compatibility we normalize the file/directory mode here.
249 * Note that we do not support committing symlinks.
250 */
251 if (S_ISREG(te->mode)) {
252 mode = GOT_DEFAULT_FILE_MODE;
253 if (te->mode & (S_IXUSR | S_IXGRP | S_IXOTH))
254 mode |= S_IXUSR | S_IXGRP | S_IXOTH;
255 } else if (got_object_tree_entry_is_submodule(te))
256 mode = S_IFDIR | S_IFLNK;
257 else if (S_ISLNK(te->mode))
258 mode = S_IFLNK; /* Git leaves all the other bits unset. */
259 else if (S_ISDIR(te->mode))
260 mode = S_IFDIR; /* Git leaves all the other bits unset. */
261 else
262 return got_error(GOT_ERR_BAD_FILETYPE);
264 ret = snprintf(buf, len, "%o ", mode);
265 if (ret == -1 || ret >= len)
266 return got_error(GOT_ERR_NO_SPACE);
267 return NULL;
270 /*
271 * Git expects directory tree entries to be sorted with an imaginary slash
272 * appended to their name, and will break otherwise. Let's be nice.
273 * This function is intended to be used with mergesort(3) to sort an
274 * array of pointers to struct got_tree_entry objects.
275 */
276 static int
277 sort_tree_entries_the_way_git_likes_it(const void *arg1, const void *arg2)
279 struct got_tree_entry * const *te1 = arg1;
280 struct got_tree_entry * const *te2 = arg2;
281 char name1[NAME_MAX + 2];
282 char name2[NAME_MAX + 2];
284 strlcpy(name1, (*te1)->name, sizeof(name1));
285 strlcpy(name2, (*te2)->name, sizeof(name2));
286 if (S_ISDIR((*te1)->mode))
287 strlcat(name1, "/", sizeof(name1));
288 if (S_ISDIR((*te2)->mode))
289 strlcat(name2, "/", sizeof(name2));
290 return strcmp(name1, name2);
293 const struct got_error *
294 got_object_tree_create(struct got_object_id **id,
295 struct got_pathlist_head *paths, int nentries, struct got_repository *repo)
297 const struct got_error *err = NULL;
298 char modebuf[sizeof("100644 ")];
299 SHA1_CTX sha1_ctx;
300 char *header = NULL;
301 size_t headerlen, len = 0, n;
302 FILE *treefile = NULL;
303 struct got_pathlist_entry *pe;
304 struct got_tree_entry **sorted_entries;
305 struct got_tree_entry *te;
306 int i;
308 *id = NULL;
310 SHA1Init(&sha1_ctx);
312 sorted_entries = calloc(nentries, sizeof(struct got_tree_entry *));
313 if (sorted_entries == NULL)
314 return got_error_from_errno("calloc");
316 i = 0;
317 TAILQ_FOREACH(pe, paths, entry)
318 sorted_entries[i++] = pe->data;
319 mergesort(sorted_entries, nentries, sizeof(struct got_tree_entry *),
320 sort_tree_entries_the_way_git_likes_it);
322 for (i = 0; i < nentries; i++) {
323 te = sorted_entries[i];
324 err = te_mode2str(modebuf, sizeof(modebuf), te);
325 if (err)
326 goto done;
327 len += strlen(modebuf) + strlen(te->name) + 1 +
328 SHA1_DIGEST_LENGTH;
331 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TREE, len) == -1) {
332 err = got_error_from_errno("asprintf");
333 goto done;
335 headerlen = strlen(header) + 1;
336 SHA1Update(&sha1_ctx, header, headerlen);
338 treefile = got_opentemp();
339 if (treefile == NULL) {
340 err = got_error_from_errno("got_opentemp");
341 goto done;
344 n = fwrite(header, 1, headerlen, treefile);
345 if (n != headerlen) {
346 err = got_ferror(treefile, GOT_ERR_IO);
347 goto done;
350 for (i = 0; i < nentries; i++) {
351 te = sorted_entries[i];
352 err = te_mode2str(modebuf, sizeof(modebuf), te);
353 if (err)
354 goto done;
355 len = strlen(modebuf);
356 n = fwrite(modebuf, 1, len, treefile);
357 if (n != len) {
358 err = got_ferror(treefile, GOT_ERR_IO);
359 goto done;
361 SHA1Update(&sha1_ctx, modebuf, len);
363 len = strlen(te->name) + 1; /* must include NUL */
364 n = fwrite(te->name, 1, len, treefile);
365 if (n != len) {
366 err = got_ferror(treefile, GOT_ERR_IO);
367 goto done;
369 SHA1Update(&sha1_ctx, te->name, len);
371 len = SHA1_DIGEST_LENGTH;
372 n = fwrite(te->id.sha1, 1, len, treefile);
373 if (n != len) {
374 err = got_ferror(treefile, GOT_ERR_IO);
375 goto done;
377 SHA1Update(&sha1_ctx, te->id.sha1, len);
380 *id = malloc(sizeof(**id));
381 if (*id == NULL) {
382 err = got_error_from_errno("malloc");
383 goto done;
385 SHA1Final((*id)->sha1, &sha1_ctx);
387 if (fflush(treefile) != 0) {
388 err = got_error_from_errno("fflush");
389 goto done;
391 rewind(treefile);
393 err = create_object_file(*id, treefile, repo);
394 done:
395 free(header);
396 free(sorted_entries);
397 if (treefile && fclose(treefile) == EOF && err == NULL)
398 err = got_error_from_errno("fclose");
399 if (err) {
400 free(*id);
401 *id = NULL;
403 return err;
406 const struct got_error *
407 got_object_commit_create(struct got_object_id **id,
408 struct got_object_id *tree_id, struct got_object_id_queue *parent_ids,
409 int nparents, const char *author, time_t author_time,
410 const char *committer, time_t committer_time,
411 const char *logmsg, struct got_repository *repo)
413 const struct got_error *err = NULL;
414 SHA1_CTX sha1_ctx;
415 char *header = NULL, *tree_str = NULL;
416 char *author_str = NULL, *committer_str = NULL;
417 char *id_str = NULL;
418 size_t headerlen, len = 0, n;
419 FILE *commitfile = NULL;
420 struct got_object_qid *qid;
421 char *msg0, *msg;
423 *id = NULL;
425 SHA1Init(&sha1_ctx);
427 msg0 = strdup(logmsg);
428 if (msg0 == NULL)
429 return got_error_from_errno("strdup");
430 msg = msg0;
432 while (isspace((unsigned char)msg[0]))
433 msg++;
434 len = strlen(msg);
435 while (len > 0 && isspace((unsigned char)msg[len - 1])) {
436 msg[len - 1] = '\0';
437 len--;
440 if (asprintf(&author_str, "%s%s %lld +0000\n",
441 GOT_COMMIT_LABEL_AUTHOR, author, (long long)author_time) == -1)
442 return got_error_from_errno("asprintf");
444 if (asprintf(&committer_str, "%s%s %lld +0000\n",
445 GOT_COMMIT_LABEL_COMMITTER, committer ? committer : author,
446 (long long)(committer ? committer_time : author_time))
447 == -1) {
448 err = got_error_from_errno("asprintf");
449 goto done;
452 len = strlen(GOT_COMMIT_LABEL_TREE) + SHA1_DIGEST_STRING_LENGTH +
453 nparents *
454 (strlen(GOT_COMMIT_LABEL_PARENT) + SHA1_DIGEST_STRING_LENGTH) +
455 + strlen(author_str) + strlen(committer_str) + 2 + strlen(msg);
457 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_COMMIT, len) == -1) {
458 err = got_error_from_errno("asprintf");
459 goto done;
461 headerlen = strlen(header) + 1;
462 SHA1Update(&sha1_ctx, header, headerlen);
464 commitfile = got_opentemp();
465 if (commitfile == NULL) {
466 err = got_error_from_errno("got_opentemp");
467 goto done;
470 n = fwrite(header, 1, headerlen, commitfile);
471 if (n != headerlen) {
472 err = got_ferror(commitfile, GOT_ERR_IO);
473 goto done;
476 err = got_object_id_str(&id_str, tree_id);
477 if (err)
478 goto done;
479 if (asprintf(&tree_str, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str)
480 == -1) {
481 err = got_error_from_errno("asprintf");
482 goto done;
484 len = strlen(tree_str);
485 SHA1Update(&sha1_ctx, tree_str, len);
486 n = fwrite(tree_str, 1, len, commitfile);
487 if (n != len) {
488 err = got_ferror(commitfile, GOT_ERR_IO);
489 goto done;
492 if (parent_ids) {
493 free(id_str);
494 id_str = NULL;
495 STAILQ_FOREACH(qid, parent_ids, entry) {
496 char *parent_str = NULL;
498 err = got_object_id_str(&id_str, qid->id);
499 if (err)
500 goto done;
501 if (asprintf(&parent_str, "%s%s\n",
502 GOT_COMMIT_LABEL_PARENT, id_str) == -1) {
503 err = got_error_from_errno("asprintf");
504 goto done;
506 len = strlen(parent_str);
507 SHA1Update(&sha1_ctx, parent_str, len);
508 n = fwrite(parent_str, 1, len, commitfile);
509 if (n != len) {
510 err = got_ferror(commitfile, GOT_ERR_IO);
511 free(parent_str);
512 goto done;
514 free(parent_str);
515 free(id_str);
516 id_str = NULL;
520 len = strlen(author_str);
521 SHA1Update(&sha1_ctx, author_str, len);
522 n = fwrite(author_str, 1, len, commitfile);
523 if (n != len) {
524 err = got_ferror(commitfile, GOT_ERR_IO);
525 goto done;
528 len = strlen(committer_str);
529 SHA1Update(&sha1_ctx, committer_str, len);
530 n = fwrite(committer_str, 1, len, commitfile);
531 if (n != len) {
532 err = got_ferror(commitfile, GOT_ERR_IO);
533 goto done;
536 SHA1Update(&sha1_ctx, "\n", 1);
537 n = fwrite("\n", 1, 1, commitfile);
538 if (n != 1) {
539 err = got_ferror(commitfile, GOT_ERR_IO);
540 goto done;
543 len = strlen(msg);
544 SHA1Update(&sha1_ctx, msg, len);
545 n = fwrite(msg, 1, len, commitfile);
546 if (n != len) {
547 err = got_ferror(commitfile, GOT_ERR_IO);
548 goto done;
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;
558 *id = malloc(sizeof(**id));
559 if (*id == NULL) {
560 err = got_error_from_errno("malloc");
561 goto done;
563 SHA1Final((*id)->sha1, &sha1_ctx);
565 if (fflush(commitfile) != 0) {
566 err = got_error_from_errno("fflush");
567 goto done;
569 rewind(commitfile);
571 err = create_object_file(*id, commitfile, repo);
572 done:
573 free(id_str);
574 free(msg0);
575 free(header);
576 free(tree_str);
577 free(author_str);
578 free(committer_str);
579 if (commitfile && fclose(commitfile) == EOF && err == NULL)
580 err = got_error_from_errno("fclose");
581 if (err) {
582 free(*id);
583 *id = NULL;
585 return err;
588 const struct got_error *
589 got_object_tag_create(struct got_object_id **id,
590 const char *tag_name, struct got_object_id *object_id, const char *tagger,
591 time_t tagger_time, const char *tagmsg, struct got_repository *repo)
593 const struct got_error *err = NULL;
594 SHA1_CTX sha1_ctx;
595 char *header = NULL;
596 char *tag_str = NULL, *tagger_str = NULL;
597 char *id_str = NULL, *obj_str = NULL, *type_str = NULL;
598 size_t headerlen, len = 0, n;
599 FILE *tagfile = NULL;
600 char *msg0 = NULL, *msg;
601 const char *obj_type_str;
602 int obj_type;
604 *id = NULL;
606 SHA1Init(&sha1_ctx);
608 err = got_object_id_str(&id_str, object_id);
609 if (err)
610 goto done;
611 if (asprintf(&obj_str, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str) == -1) {
612 err = got_error_from_errno("asprintf");
613 goto done;
616 err = got_object_get_type(&obj_type, repo, object_id);
617 if (err)
618 goto done;
620 switch (obj_type) {
621 case GOT_OBJ_TYPE_BLOB:
622 obj_type_str = GOT_OBJ_LABEL_BLOB;
623 break;
624 case GOT_OBJ_TYPE_TREE:
625 obj_type_str = GOT_OBJ_LABEL_TREE;
626 break;
627 case GOT_OBJ_TYPE_COMMIT:
628 obj_type_str = GOT_OBJ_LABEL_COMMIT;
629 break;
630 case GOT_OBJ_TYPE_TAG:
631 obj_type_str = GOT_OBJ_LABEL_TAG;
632 break;
633 default:
634 err = got_error(GOT_ERR_OBJ_TYPE);
635 goto done;
638 if (asprintf(&type_str, "%s%s\n", GOT_TAG_LABEL_TYPE,
639 obj_type_str) == -1) {
640 err = got_error_from_errno("asprintf");
641 goto done;
644 if (asprintf(&tag_str, "%s%s\n", GOT_TAG_LABEL_TAG, tag_name) == -1) {
645 err = got_error_from_errno("asprintf");
646 goto done;
649 if (asprintf(&tagger_str, "%s%s %lld +0000\n",
650 GOT_TAG_LABEL_TAGGER, tagger, (long long)tagger_time) == -1)
651 return got_error_from_errno("asprintf");
653 msg0 = strdup(tagmsg);
654 if (msg0 == NULL) {
655 err = got_error_from_errno("strdup");
656 goto done;
658 msg = msg0;
660 while (isspace((unsigned char)msg[0]))
661 msg++;
663 len = strlen(obj_str) + strlen(type_str) + strlen(tag_str) +
664 strlen(tagger_str) + 1 + strlen(msg) + 1;
666 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TAG, len) == -1) {
667 err = got_error_from_errno("asprintf");
668 goto done;
671 headerlen = strlen(header) + 1;
672 SHA1Update(&sha1_ctx, header, headerlen);
674 tagfile = got_opentemp();
675 if (tagfile == NULL) {
676 err = got_error_from_errno("got_opentemp");
677 goto done;
680 n = fwrite(header, 1, headerlen, tagfile);
681 if (n != headerlen) {
682 err = got_ferror(tagfile, GOT_ERR_IO);
683 goto done;
685 len = strlen(obj_str);
686 SHA1Update(&sha1_ctx, obj_str, len);
687 n = fwrite(obj_str, 1, len, tagfile);
688 if (n != len) {
689 err = got_ferror(tagfile, GOT_ERR_IO);
690 goto done;
692 len = strlen(type_str);
693 SHA1Update(&sha1_ctx, type_str, len);
694 n = fwrite(type_str, 1, len, tagfile);
695 if (n != len) {
696 err = got_ferror(tagfile, GOT_ERR_IO);
697 goto done;
700 len = strlen(tag_str);
701 SHA1Update(&sha1_ctx, tag_str, len);
702 n = fwrite(tag_str, 1, len, tagfile);
703 if (n != len) {
704 err = got_ferror(tagfile, GOT_ERR_IO);
705 goto done;
708 len = strlen(tagger_str);
709 SHA1Update(&sha1_ctx, tagger_str, len);
710 n = fwrite(tagger_str, 1, len, tagfile);
711 if (n != len) {
712 err = got_ferror(tagfile, GOT_ERR_IO);
713 goto done;
716 SHA1Update(&sha1_ctx, "\n", 1);
717 n = fwrite("\n", 1, 1, tagfile);
718 if (n != 1) {
719 err = got_ferror(tagfile, GOT_ERR_IO);
720 goto done;
723 len = strlen(msg);
724 SHA1Update(&sha1_ctx, msg, len);
725 n = fwrite(msg, 1, len, tagfile);
726 if (n != len) {
727 err = got_ferror(tagfile, GOT_ERR_IO);
728 goto done;
731 SHA1Update(&sha1_ctx, "\n", 1);
732 n = fwrite("\n", 1, 1, tagfile);
733 if (n != 1) {
734 err = got_ferror(tagfile, GOT_ERR_IO);
735 goto done;
738 *id = malloc(sizeof(**id));
739 if (*id == NULL) {
740 err = got_error_from_errno("malloc");
741 goto done;
743 SHA1Final((*id)->sha1, &sha1_ctx);
745 if (fflush(tagfile) != 0) {
746 err = got_error_from_errno("fflush");
747 goto done;
749 rewind(tagfile);
751 err = create_object_file(*id, tagfile, repo);
752 done:
753 free(msg0);
754 free(header);
755 free(obj_str);
756 free(tagger_str);
757 if (tagfile && fclose(tagfile) == EOF && err == NULL)
758 err = got_error_from_errno("fclose");
759 if (err) {
760 free(*id);
761 *id = NULL;
763 return err;