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 <sha1.h>
29 #include <unistd.h>
30 #include <zlib.h>
32 #include "got_compat.h"
34 #include "got_error.h"
35 #include "got_object.h"
36 #include "got_repository.h"
37 #include "got_opentemp.h"
38 #include "got_path.h"
40 #include "got_lib_sha1.h"
41 #include "got_lib_deflate.h"
42 #include "got_lib_delta.h"
43 #include "got_lib_object.h"
44 #include "got_lib_object_parse.h"
45 #include "got_lib_lockfile.h"
47 #ifndef nitems
48 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
49 #endif
51 static const struct got_error *
52 create_object_file(struct got_object_id *id, FILE *content,
53 struct got_repository *repo)
54 {
55 const struct got_error *err = NULL, *unlock_err = NULL;
56 char *objpath = NULL, *tmppath = NULL;
57 FILE *tmpfile = NULL;
58 struct got_lockfile *lf = NULL;
59 size_t tmplen = 0;
61 err = got_object_get_path(&objpath, id, repo);
62 if (err)
63 return err;
65 err = got_opentemp_named(&tmppath, &tmpfile, objpath);
66 if (err) {
67 char *parent_path;
68 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
69 goto done;
70 err = got_path_dirname(&parent_path, objpath);
71 if (err)
72 goto done;
73 err = got_path_mkdir(parent_path);
74 free(parent_path);
75 if (err)
76 goto done;
77 err = got_opentemp_named(&tmppath, &tmpfile, objpath);
78 if (err)
79 goto done;
80 }
82 if (fchmod(fileno(tmpfile), GOT_DEFAULT_FILE_MODE) != 0) {
83 err = got_error_from_errno2("fchmod", tmppath);
84 goto done;
85 }
87 err = got_deflate_to_file(&tmplen, content, tmpfile, NULL);
88 if (err)
89 goto done;
91 err = got_lockfile_lock(&lf, objpath, -1);
92 if (err)
93 goto done;
95 if (rename(tmppath, objpath) != 0) {
96 err = got_error_from_errno3("rename", tmppath, objpath);
97 goto done;
98 }
99 free(tmppath);
100 tmppath = NULL;
101 done:
102 free(objpath);
103 if (tmppath) {
104 if (unlink(tmppath) != 0 && err == NULL)
105 err = got_error_from_errno2("unlink", tmppath);
106 free(tmppath);
108 if (tmpfile && fclose(tmpfile) == EOF && err == NULL)
109 err = got_error_from_errno("fclose");
110 if (lf)
111 unlock_err = got_lockfile_unlock(lf, -1);
112 return err ? err : unlock_err;
115 const struct got_error *
116 got_object_blob_file_create(struct got_object_id **id, FILE **blobfile,
117 const char *ondisk_path)
119 const struct got_error *err = NULL;
120 char *header = NULL;
121 int fd = -1;
122 struct stat sb;
123 SHA1_CTX sha1_ctx;
124 size_t headerlen = 0, n;
126 *id = NULL;
127 *blobfile = NULL;
129 SHA1Init(&sha1_ctx);
131 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
132 if (fd == -1) {
133 if (errno != ELOOP)
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 for (;;) {
165 char buf[PATH_MAX * 8];
166 ssize_t inlen;
168 if (S_ISLNK(sb.st_mode)) {
169 inlen = readlink(ondisk_path, buf, sizeof(buf));
170 if (inlen == -1) {
171 err = got_error_from_errno("readlink");
172 goto done;
174 } else {
175 inlen = read(fd, buf, sizeof(buf));
176 if (inlen == -1) {
177 err = got_error_from_errno("read");
178 goto done;
181 if (inlen == 0)
182 break; /* EOF */
183 SHA1Update(&sha1_ctx, buf, inlen);
184 n = fwrite(buf, 1, inlen, *blobfile);
185 if (n != inlen) {
186 err = got_ferror(*blobfile, GOT_ERR_IO);
187 goto done;
189 if (S_ISLNK(sb.st_mode))
190 break;
193 *id = malloc(sizeof(**id));
194 if (*id == NULL) {
195 err = got_error_from_errno("malloc");
196 goto done;
198 SHA1Final((*id)->sha1, &sha1_ctx);
200 if (fflush(*blobfile) != 0) {
201 err = got_error_from_errno("fflush");
202 goto done;
204 rewind(*blobfile);
205 done:
206 free(header);
207 if (fd != -1 && close(fd) == -1 && err == NULL)
208 err = got_error_from_errno("close");
209 if (err) {
210 free(*id);
211 *id = NULL;
212 if (*blobfile) {
213 fclose(*blobfile);
214 *blobfile = NULL;
217 return err;
220 const struct got_error *
221 got_object_blob_create(struct got_object_id **id, const char *ondisk_path,
222 struct got_repository *repo)
224 const struct got_error *err = NULL;
225 FILE *blobfile = NULL;
227 err = got_object_blob_file_create(id, &blobfile, ondisk_path);
228 if (err)
229 return err;
231 err = create_object_file(*id, blobfile, repo);
232 if (fclose(blobfile) == EOF && err == NULL)
233 err = got_error_from_errno("fclose");
234 if (err) {
235 free(*id);
236 *id = NULL;
238 return err;
241 static const struct got_error *
242 te_mode2str(char *buf, size_t len, struct got_tree_entry *te)
244 int ret;
245 mode_t mode;
247 /*
248 * Some Git implementations are picky about modes seen in tree entries.
249 * For best compatibility we normalize the file/directory mode here.
250 * Note that we do not support committing symlinks.
251 */
252 if (S_ISREG(te->mode)) {
253 mode = GOT_DEFAULT_FILE_MODE;
254 if (te->mode & (S_IXUSR | S_IXGRP | S_IXOTH))
255 mode |= S_IXUSR | S_IXGRP | S_IXOTH;
256 } else if (got_object_tree_entry_is_submodule(te))
257 mode = S_IFDIR | S_IFLNK;
258 else if (S_ISLNK(te->mode))
259 mode = S_IFLNK; /* Git leaves all the other bits unset. */
260 else if (S_ISDIR(te->mode))
261 mode = S_IFDIR; /* Git leaves all the other bits unset. */
262 else
263 return got_error(GOT_ERR_BAD_FILETYPE);
265 ret = snprintf(buf, len, "%o ", mode);
266 if (ret == -1 || ret >= len)
267 return got_error(GOT_ERR_NO_SPACE);
268 return NULL;
271 /*
272 * Git expects directory tree entries to be sorted with an imaginary slash
273 * appended to their name, and will break otherwise. Let's be nice.
274 * This function is intended to be used with mergesort(3) to sort an
275 * array of pointers to struct got_tree_entry objects.
276 */
277 static int
278 sort_tree_entries_the_way_git_likes_it(const void *arg1, const void *arg2)
280 struct got_tree_entry * const *te1 = arg1;
281 struct got_tree_entry * const *te2 = arg2;
282 char name1[NAME_MAX + 2];
283 char name2[NAME_MAX + 2];
285 strlcpy(name1, (*te1)->name, sizeof(name1));
286 strlcpy(name2, (*te2)->name, sizeof(name2));
287 if (S_ISDIR((*te1)->mode))
288 strlcat(name1, "/", sizeof(name1));
289 if (S_ISDIR((*te2)->mode))
290 strlcat(name2, "/", sizeof(name2));
291 return strcmp(name1, name2);
294 const struct got_error *
295 got_object_tree_create(struct got_object_id **id,
296 struct got_pathlist_head *paths, int nentries, struct got_repository *repo)
298 const struct got_error *err = NULL;
299 char modebuf[sizeof("100644 ")];
300 SHA1_CTX sha1_ctx;
301 char *header = NULL;
302 size_t headerlen, len = 0, n;
303 FILE *treefile = NULL;
304 struct got_pathlist_entry *pe;
305 struct got_tree_entry **sorted_entries;
306 struct got_tree_entry *te;
307 int i;
309 *id = NULL;
311 SHA1Init(&sha1_ctx);
313 sorted_entries = calloc(nentries, sizeof(struct got_tree_entry *));
314 if (sorted_entries == NULL)
315 return got_error_from_errno("calloc");
317 i = 0;
318 TAILQ_FOREACH(pe, paths, entry)
319 sorted_entries[i++] = pe->data;
320 mergesort(sorted_entries, nentries, sizeof(struct got_tree_entry *),
321 sort_tree_entries_the_way_git_likes_it);
323 for (i = 0; i < nentries; i++) {
324 te = sorted_entries[i];
325 err = te_mode2str(modebuf, sizeof(modebuf), te);
326 if (err)
327 goto done;
328 len += strlen(modebuf) + strlen(te->name) + 1 +
329 SHA1_DIGEST_LENGTH;
332 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TREE, len) == -1) {
333 err = got_error_from_errno("asprintf");
334 goto done;
336 headerlen = strlen(header) + 1;
337 SHA1Update(&sha1_ctx, header, headerlen);
339 treefile = got_opentemp();
340 if (treefile == NULL) {
341 err = got_error_from_errno("got_opentemp");
342 goto done;
345 n = fwrite(header, 1, headerlen, treefile);
346 if (n != headerlen) {
347 err = got_ferror(treefile, GOT_ERR_IO);
348 goto done;
351 for (i = 0; i < nentries; i++) {
352 te = sorted_entries[i];
353 err = te_mode2str(modebuf, sizeof(modebuf), te);
354 if (err)
355 goto done;
356 len = strlen(modebuf);
357 n = fwrite(modebuf, 1, len, treefile);
358 if (n != len) {
359 err = got_ferror(treefile, GOT_ERR_IO);
360 goto done;
362 SHA1Update(&sha1_ctx, modebuf, len);
364 len = strlen(te->name) + 1; /* must include NUL */
365 n = fwrite(te->name, 1, len, treefile);
366 if (n != len) {
367 err = got_ferror(treefile, GOT_ERR_IO);
368 goto done;
370 SHA1Update(&sha1_ctx, te->name, len);
372 len = SHA1_DIGEST_LENGTH;
373 n = fwrite(te->id.sha1, 1, len, treefile);
374 if (n != len) {
375 err = got_ferror(treefile, GOT_ERR_IO);
376 goto done;
378 SHA1Update(&sha1_ctx, te->id.sha1, len);
381 *id = malloc(sizeof(**id));
382 if (*id == NULL) {
383 err = got_error_from_errno("malloc");
384 goto done;
386 SHA1Final((*id)->sha1, &sha1_ctx);
388 if (fflush(treefile) != 0) {
389 err = got_error_from_errno("fflush");
390 goto done;
392 rewind(treefile);
394 err = create_object_file(*id, treefile, repo);
395 done:
396 free(header);
397 free(sorted_entries);
398 if (treefile && fclose(treefile) == EOF && err == NULL)
399 err = got_error_from_errno("fclose");
400 if (err) {
401 free(*id);
402 *id = NULL;
404 return err;
407 const struct got_error *
408 got_object_commit_create(struct got_object_id **id,
409 struct got_object_id *tree_id, struct got_object_id_queue *parent_ids,
410 int nparents, const char *author, time_t author_time,
411 const char *committer, time_t committer_time,
412 const char *logmsg, struct got_repository *repo)
414 const struct got_error *err = NULL;
415 SHA1_CTX sha1_ctx;
416 char *header = NULL, *tree_str = NULL;
417 char *author_str = NULL, *committer_str = NULL;
418 char *id_str = NULL;
419 size_t headerlen, len = 0, n;
420 FILE *commitfile = NULL;
421 struct got_object_qid *qid;
422 char *msg0, *msg;
424 *id = NULL;
426 SHA1Init(&sha1_ctx);
428 msg0 = strdup(logmsg);
429 if (msg0 == NULL)
430 return got_error_from_errno("strdup");
431 msg = msg0;
433 while (isspace((unsigned char)msg[0]))
434 msg++;
435 len = strlen(msg);
436 while (len > 0 && isspace((unsigned char)msg[len - 1])) {
437 msg[len - 1] = '\0';
438 len--;
441 if (asprintf(&author_str, "%s%s %lld +0000\n",
442 GOT_COMMIT_LABEL_AUTHOR, author, (long long)author_time) == -1)
443 return got_error_from_errno("asprintf");
445 if (asprintf(&committer_str, "%s%s %lld +0000\n",
446 GOT_COMMIT_LABEL_COMMITTER, committer ? committer : author,
447 (long long)(committer ? committer_time : author_time))
448 == -1) {
449 err = got_error_from_errno("asprintf");
450 goto done;
453 len = strlen(GOT_COMMIT_LABEL_TREE) + SHA1_DIGEST_STRING_LENGTH +
454 nparents *
455 (strlen(GOT_COMMIT_LABEL_PARENT) + SHA1_DIGEST_STRING_LENGTH) +
456 + strlen(author_str) + strlen(committer_str) + 2 + strlen(msg);
458 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_COMMIT, len) == -1) {
459 err = got_error_from_errno("asprintf");
460 goto done;
462 headerlen = strlen(header) + 1;
463 SHA1Update(&sha1_ctx, header, headerlen);
465 commitfile = got_opentemp();
466 if (commitfile == NULL) {
467 err = got_error_from_errno("got_opentemp");
468 goto done;
471 n = fwrite(header, 1, headerlen, commitfile);
472 if (n != headerlen) {
473 err = got_ferror(commitfile, GOT_ERR_IO);
474 goto done;
477 err = got_object_id_str(&id_str, tree_id);
478 if (err)
479 goto done;
480 if (asprintf(&tree_str, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str)
481 == -1) {
482 err = got_error_from_errno("asprintf");
483 goto done;
485 len = strlen(tree_str);
486 SHA1Update(&sha1_ctx, tree_str, len);
487 n = fwrite(tree_str, 1, len, commitfile);
488 if (n != len) {
489 err = got_ferror(commitfile, GOT_ERR_IO);
490 goto done;
493 if (parent_ids) {
494 STAILQ_FOREACH(qid, parent_ids, entry) {
495 char *parent_str = NULL;
497 free(id_str);
499 err = got_object_id_str(&id_str, qid->id);
500 if (err)
501 goto done;
502 if (asprintf(&parent_str, "%s%s\n",
503 GOT_COMMIT_LABEL_PARENT, id_str) == -1) {
504 err = got_error_from_errno("asprintf");
505 goto done;
507 len = strlen(parent_str);
508 SHA1Update(&sha1_ctx, parent_str, len);
509 n = fwrite(parent_str, 1, len, commitfile);
510 if (n != len) {
511 err = got_ferror(commitfile, GOT_ERR_IO);
512 free(parent_str);
513 goto done;
515 free(parent_str);
519 len = strlen(author_str);
520 SHA1Update(&sha1_ctx, author_str, len);
521 n = fwrite(author_str, 1, len, commitfile);
522 if (n != len) {
523 err = got_ferror(commitfile, GOT_ERR_IO);
524 goto done;
527 len = strlen(committer_str);
528 SHA1Update(&sha1_ctx, committer_str, len);
529 n = fwrite(committer_str, 1, len, commitfile);
530 if (n != len) {
531 err = got_ferror(commitfile, GOT_ERR_IO);
532 goto done;
535 SHA1Update(&sha1_ctx, "\n", 1);
536 n = fwrite("\n", 1, 1, commitfile);
537 if (n != 1) {
538 err = got_ferror(commitfile, GOT_ERR_IO);
539 goto done;
542 len = strlen(msg);
543 SHA1Update(&sha1_ctx, msg, len);
544 n = fwrite(msg, 1, len, commitfile);
545 if (n != len) {
546 err = got_ferror(commitfile, GOT_ERR_IO);
547 goto done;
550 SHA1Update(&sha1_ctx, "\n", 1);
551 n = fwrite("\n", 1, 1, commitfile);
552 if (n != 1) {
553 err = got_ferror(commitfile, GOT_ERR_IO);
554 goto done;
557 *id = malloc(sizeof(**id));
558 if (*id == NULL) {
559 err = got_error_from_errno("malloc");
560 goto done;
562 SHA1Final((*id)->sha1, &sha1_ctx);
564 if (fflush(commitfile) != 0) {
565 err = got_error_from_errno("fflush");
566 goto done;
568 rewind(commitfile);
570 err = create_object_file(*id, commitfile, repo);
571 done:
572 free(msg0);
573 free(header);
574 free(tree_str);
575 free(author_str);
576 free(committer_str);
577 if (commitfile && fclose(commitfile) == EOF && err == NULL)
578 err = got_error_from_errno("fclose");
579 if (err) {
580 free(*id);
581 *id = NULL;
583 return err;
586 const struct got_error *
587 got_object_tag_create(struct got_object_id **id,
588 const char *tag_name, struct got_object_id *object_id, const char *tagger,
589 time_t tagger_time, const char *tagmsg, struct got_repository *repo)
591 const struct got_error *err = NULL;
592 SHA1_CTX sha1_ctx;
593 char *header = NULL;
594 char *tag_str = NULL, *tagger_str = NULL;
595 char *id_str = NULL, *obj_str = NULL, *type_str = NULL;
596 size_t headerlen, len = 0, n;
597 FILE *tagfile = NULL;
598 char *msg0 = NULL, *msg;
599 const char *obj_type_str;
600 int obj_type;
602 *id = NULL;
604 SHA1Init(&sha1_ctx);
606 err = got_object_id_str(&id_str, object_id);
607 if (err)
608 goto done;
609 if (asprintf(&obj_str, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str) == -1) {
610 err = got_error_from_errno("asprintf");
611 goto done;
614 err = got_object_get_type(&obj_type, repo, object_id);
615 if (err)
616 goto done;
618 switch (obj_type) {
619 case GOT_OBJ_TYPE_BLOB:
620 obj_type_str = GOT_OBJ_LABEL_BLOB;
621 break;
622 case GOT_OBJ_TYPE_TREE:
623 obj_type_str = GOT_OBJ_LABEL_TREE;
624 break;
625 case GOT_OBJ_TYPE_COMMIT:
626 obj_type_str = GOT_OBJ_LABEL_COMMIT;
627 break;
628 case GOT_OBJ_TYPE_TAG:
629 obj_type_str = GOT_OBJ_LABEL_TAG;
630 break;
631 default:
632 err = got_error(GOT_ERR_OBJ_TYPE);
633 goto done;
636 if (asprintf(&type_str, "%s%s\n", GOT_TAG_LABEL_TYPE,
637 obj_type_str) == -1) {
638 err = got_error_from_errno("asprintf");
639 goto done;
642 if (asprintf(&tag_str, "%s%s\n", GOT_TAG_LABEL_TAG, tag_name) == -1) {
643 err = got_error_from_errno("asprintf");
644 goto done;
647 if (asprintf(&tagger_str, "%s%s %lld +0000\n",
648 GOT_TAG_LABEL_TAGGER, tagger, (long long)tagger_time) == -1)
649 return got_error_from_errno("asprintf");
651 msg0 = strdup(tagmsg);
652 if (msg0 == NULL) {
653 err = got_error_from_errno("strdup");
654 goto done;
656 msg = msg0;
658 while (isspace((unsigned char)msg[0]))
659 msg++;
661 len = strlen(obj_str) + strlen(type_str) + strlen(tag_str) +
662 strlen(tagger_str) + 1 + strlen(msg) + 1;
664 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TAG, len) == -1) {
665 err = got_error_from_errno("asprintf");
666 goto done;
669 headerlen = strlen(header) + 1;
670 SHA1Update(&sha1_ctx, header, headerlen);
672 tagfile = got_opentemp();
673 if (tagfile == NULL) {
674 err = got_error_from_errno("got_opentemp");
675 goto done;
678 n = fwrite(header, 1, headerlen, tagfile);
679 if (n != headerlen) {
680 err = got_ferror(tagfile, GOT_ERR_IO);
681 goto done;
683 len = strlen(obj_str);
684 SHA1Update(&sha1_ctx, obj_str, len);
685 n = fwrite(obj_str, 1, len, tagfile);
686 if (n != len) {
687 err = got_ferror(tagfile, GOT_ERR_IO);
688 goto done;
690 len = strlen(type_str);
691 SHA1Update(&sha1_ctx, type_str, len);
692 n = fwrite(type_str, 1, len, tagfile);
693 if (n != len) {
694 err = got_ferror(tagfile, GOT_ERR_IO);
695 goto done;
698 len = strlen(tag_str);
699 SHA1Update(&sha1_ctx, tag_str, len);
700 n = fwrite(tag_str, 1, len, tagfile);
701 if (n != len) {
702 err = got_ferror(tagfile, GOT_ERR_IO);
703 goto done;
706 len = strlen(tagger_str);
707 SHA1Update(&sha1_ctx, tagger_str, len);
708 n = fwrite(tagger_str, 1, len, tagfile);
709 if (n != len) {
710 err = got_ferror(tagfile, GOT_ERR_IO);
711 goto done;
714 SHA1Update(&sha1_ctx, "\n", 1);
715 n = fwrite("\n", 1, 1, tagfile);
716 if (n != 1) {
717 err = got_ferror(tagfile, GOT_ERR_IO);
718 goto done;
721 len = strlen(msg);
722 SHA1Update(&sha1_ctx, msg, len);
723 n = fwrite(msg, 1, len, tagfile);
724 if (n != len) {
725 err = got_ferror(tagfile, GOT_ERR_IO);
726 goto done;
729 SHA1Update(&sha1_ctx, "\n", 1);
730 n = fwrite("\n", 1, 1, tagfile);
731 if (n != 1) {
732 err = got_ferror(tagfile, GOT_ERR_IO);
733 goto done;
736 *id = malloc(sizeof(**id));
737 if (*id == NULL) {
738 err = got_error_from_errno("malloc");
739 goto done;
741 SHA1Final((*id)->sha1, &sha1_ctx);
743 if (fflush(tagfile) != 0) {
744 err = got_error_from_errno("fflush");
745 goto done;
747 rewind(tagfile);
749 err = create_object_file(*id, tagfile, repo);
750 done:
751 free(msg0);
752 free(header);
753 free(obj_str);
754 free(tagger_str);
755 if (tagfile && fclose(tagfile) == EOF && err == NULL)
756 err = got_error_from_errno("fclose");
757 if (err) {
758 free(*id);
759 *id = NULL;
761 return err;