Blob


1 /*
2 * Copyright (c) 2018, 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/queue.h>
19 #include <sys/stat.h>
21 #include <errno.h>
22 #include <ctype.h>
23 #include <dirent.h>
24 #include <sha1.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <util.h>
29 #include <zlib.h>
30 #include <time.h>
31 #include <libgen.h>
33 #include "got_error.h"
34 #include "got_object.h"
35 #include "got_repository.h"
36 #include "got_reference.h"
37 #include "got_opentemp.h"
39 #include "got_lib_sha1.h"
40 #include "got_lib_path.h"
41 #include "got_lib_delta.h"
42 #include "got_lib_inflate.h"
43 #include "got_lib_object.h"
44 #include "got_lib_lockfile.h"
46 #ifndef nitems
47 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
48 #endif
50 #define GOT_REF_HEADS "heads"
51 #define GOT_REF_TAGS "tags"
52 #define GOT_REF_REMOTES "remotes"
54 /* A symbolic reference. */
55 struct got_symref {
56 char *name;
57 char *ref;
58 };
60 /* A non-symbolic reference (there is no better designation). */
61 struct got_ref {
62 char *name;
63 u_int8_t sha1[SHA1_DIGEST_LENGTH];
64 };
66 /* A reference which points to an arbitrary object. */
67 struct got_reference {
68 unsigned int flags;
69 #define GOT_REF_IS_SYMBOLIC 0x01
71 union {
72 struct got_ref ref;
73 struct got_symref symref;
74 } ref;
75 };
77 static const struct got_error *
78 parse_symref(struct got_reference **ref, const char *name, const char *line)
79 {
80 struct got_symref *symref;
81 char *symref_name;
82 char *symref_ref;
84 if (line[0] == '\0')
85 return got_error(GOT_ERR_BAD_REF_DATA);
87 symref_name = strdup(name);
88 if (symref_name == NULL)
89 return got_error_from_errno();
90 symref_ref = strdup(line);
91 if (symref_ref == NULL) {
92 const struct got_error *err = got_error_from_errno();
93 free(symref_name);
94 return err;
95 }
97 *ref = calloc(1, sizeof(**ref));
98 if (*ref == NULL)
99 return got_error_from_errno();
100 (*ref)->flags |= GOT_REF_IS_SYMBOLIC;
101 symref = &((*ref)->ref.symref);
102 symref->name = symref_name;
103 symref->ref = symref_ref;
104 return NULL;
107 static const struct got_error *
108 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
110 struct got_object_id id;
112 if (strncmp(line, "ref: ", 5) == 0) {
113 line += 5;
114 return parse_symref(ref, name, line);
117 if (!got_parse_sha1_digest(id.sha1, line))
118 return got_error(GOT_ERR_BAD_REF_DATA);
120 return got_ref_alloc(ref, name, &id);
123 static const struct got_error *
124 parse_ref_file(struct got_reference **ref, const char *name,
125 const char *abspath)
127 const struct got_error *err = NULL;
128 FILE *f = fopen(abspath, "rb");
129 char *line;
130 size_t len;
131 const char delim[3] = {'\0', '\0', '\0'};
133 if (f == NULL)
134 return NULL;
136 line = fparseln(f, &len, NULL, delim, 0);
137 if (line == NULL) {
138 err = got_error(GOT_ERR_BAD_REF_DATA);
139 goto done;
142 err = parse_ref_line(ref, name, line);
143 done:
144 free(line);
145 if (fclose(f) != 0 && err == NULL)
146 err = got_error_from_errno();
147 return err;
150 static int
151 is_well_known_ref(const char *refname)
153 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
154 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
155 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
156 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
159 static char *
160 get_refs_dir_path(struct got_repository *repo, const char *refname)
162 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
163 return strdup(got_repo_get_path_git_dir(repo));
165 return got_repo_get_path_refs(repo);
168 static int
169 is_valid_ref_name(const char *name)
171 const char *s, *slash, *seg;
172 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
173 const char *forbidden_seq[] = { "//", "..", "@{" };
174 const char *lfs = GOT_LOCKFILE_SUFFIX;
175 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
176 int i;
178 if (name[0] == '@' && name[1] == '\0')
179 return 0;
181 slash = strchr(name, '/');
182 if (slash == NULL)
183 return 0;
185 s = name;
186 seg = s;
187 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
188 return 0;
189 while (*s) {
190 for (i = 0; i < nitems(forbidden); i++) {
191 if (*s == forbidden[i])
192 return 0;
194 for (i = 0; i < nitems(forbidden_seq); i++) {
195 if (s[0] == forbidden_seq[i][0] &&
196 s[1] == forbidden_seq[i][1])
197 return 0;
199 if (iscntrl((unsigned char)s[0]))
200 return 0;
201 if (s[0] == '.' && s[1] == '\0')
202 return 0;
203 if (*s == '/') {
204 const char *nextseg = s + 1;
205 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
206 nextseg[0] == '/')
207 return 0;
208 if (seg <= s - lfs_len &&
209 strncmp(s - lfs_len, lfs, lfs_len) == 0)
210 return 0;
211 seg = nextseg;
213 s++;
216 if (seg <= s - lfs_len &&
217 strncmp(s - lfs_len, lfs, lfs_len) == 0)
218 return 0;
220 return 1;
223 const struct got_error *
224 got_ref_alloc(struct got_reference **ref, const char *name,
225 struct got_object_id *id)
227 const struct got_error *err = NULL;
229 if (!is_valid_ref_name(name))
230 return got_error(GOT_ERR_BAD_REF_NAME);
232 *ref = calloc(1, sizeof(**ref));
233 if (*ref == NULL)
234 return got_error_from_errno();
236 memcpy(&(*ref)->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
237 (*ref)->ref.ref.name = strdup(name);
238 if ((*ref)->ref.ref.name == NULL) {
239 err = got_error_from_errno();
240 free(*ref);
241 *ref = NULL;
243 return err;
246 static const struct got_error *
247 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
248 const char *line)
250 struct got_object_id id;
251 const char *name;
253 *ref = NULL;
255 if (line[0] == '#' || line[0] == '^')
256 return NULL;
258 if (!got_parse_sha1_digest(id.sha1, line))
259 return got_error(GOT_ERR_BAD_REF_DATA);
261 if (abs_refname) {
262 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
263 return NULL;
264 name = abs_refname;
265 } else
266 name = line + SHA1_DIGEST_STRING_LENGTH;
268 return got_ref_alloc(ref, name, &id);
271 static const struct got_error *
272 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
273 int nsubdirs, const char *refname)
275 const struct got_error *err = NULL;
276 char *abs_refname;
277 char *line;
278 size_t len;
279 const char delim[3] = {'\0', '\0', '\0'};
280 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
282 *ref = NULL;
284 if (ref_is_absolute)
285 abs_refname = (char *)refname;
286 do {
287 line = fparseln(f, &len, NULL, delim, 0);
288 if (line == NULL)
289 break;
290 for (i = 0; i < nsubdirs; i++) {
291 if (!ref_is_absolute &&
292 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
293 refname) == -1)
294 return got_error_from_errno();
295 err = parse_packed_ref_line(ref, abs_refname, line);
296 if (!ref_is_absolute)
297 free(abs_refname);
298 if (err || *ref != NULL)
299 break;
301 free(line);
302 if (err)
303 break;
304 } while (*ref == NULL);
306 return err;
309 static const struct got_error *
310 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
311 const char *name)
313 const struct got_error *err = NULL;
314 char *path = NULL;
315 char *normpath = NULL;
316 char *absname = NULL;
317 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
318 int ref_is_well_known = is_well_known_ref(name);
320 *ref = NULL;
322 if (ref_is_absolute || ref_is_well_known) {
323 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
324 return got_error_from_errno();
325 absname = (char *)name;
326 } else {
327 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
328 subdir[0] ? "/" : "", name) == -1)
329 return got_error_from_errno();
331 if (asprintf(&absname, "refs/%s%s%s",
332 subdir, subdir[0] ? "/" : "", name) == -1) {
333 err = got_error_from_errno();
334 goto done;
338 normpath = got_path_normalize(path);
339 if (normpath == NULL) {
340 err = got_error_from_errno();
341 goto done;
344 err = parse_ref_file(ref, absname, normpath);
345 done:
346 if (!ref_is_absolute && !ref_is_well_known)
347 free(absname);
348 free(path);
349 free(normpath);
350 return err;
353 const struct got_error *
354 got_ref_open(struct got_reference **ref, struct got_repository *repo,
355 const char *refname)
357 const struct got_error *err = NULL;
358 char *path_refs = NULL;
359 const char *subdirs[] = {
360 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
361 };
362 int i, well_known = is_well_known_ref(refname);
364 *ref = NULL;
366 path_refs = get_refs_dir_path(repo, refname);
367 if (path_refs == NULL) {
368 err = got_error_from_errno();
369 goto done;
372 if (!well_known) {
373 char *packed_refs_path;
374 FILE *f;
376 /* Search on-disk refs before packed refs! */
377 for (i = 0; i < nitems(subdirs); i++) {
378 err = open_ref(ref, path_refs, subdirs[i], refname);
379 if (err || *ref)
380 goto done;
383 packed_refs_path = got_repo_get_path_packed_refs(repo);
384 if (packed_refs_path == NULL) {
385 err = got_error_from_errno();
386 goto done;
389 f = fopen(packed_refs_path, "rb");
390 free(packed_refs_path);
391 if (f != NULL) {
392 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
393 refname);
394 if (fclose(f) != 0 && err == NULL)
395 err = got_error_from_errno();
396 if (err || *ref)
397 goto done;
401 err = open_ref(ref, path_refs, "", refname);
402 if (err)
403 goto done;
404 done:
405 if (*ref == NULL)
406 err = got_error_not_ref(refname);
407 free(path_refs);
408 return err;
411 void
412 got_ref_close(struct got_reference *ref)
414 if (ref->flags & GOT_REF_IS_SYMBOLIC)
415 free(ref->ref.symref.name);
416 else
417 free(ref->ref.ref.name);
418 free(ref);
421 struct got_reference *
422 got_ref_dup(struct got_reference *ref)
424 struct got_reference *ret;
426 ret = calloc(1, sizeof(*ret));
427 if (ret == NULL)
428 return NULL;
430 ret->flags = ref->flags;
431 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
432 ret->ref.symref.name = strdup(ref->ref.symref.name);
433 if (ret->ref.symref.name == NULL) {
434 free(ret);
435 return NULL;
437 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
438 if (ret->ref.symref.ref == NULL) {
439 free(ret->ref.symref.name);
440 free(ret);
441 return NULL;
443 } else {
444 ref->ref.ref.name = strdup(ref->ref.ref.name);
445 if (ref->ref.ref.name == NULL) {
446 free(ret);
447 return NULL;
449 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
450 SHA1_DIGEST_LENGTH);
453 return ret;
456 static const struct got_error *
457 resolve_symbolic_ref(struct got_reference **resolved,
458 struct got_repository *repo, struct got_reference *ref)
460 struct got_reference *nextref;
461 const struct got_error *err;
463 err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
464 if (err)
465 return err;
467 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
468 err = resolve_symbolic_ref(resolved, repo, nextref);
469 else
470 *resolved = got_ref_dup(nextref);
472 got_ref_close(nextref);
473 return err;
476 const struct got_error *
477 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
478 struct got_reference *ref)
480 const struct got_error *err;
482 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
483 struct got_reference *resolved = NULL;
484 err = resolve_symbolic_ref(&resolved, repo, ref);
485 if (err == NULL)
486 err = got_ref_resolve(id, repo, resolved);
487 free(resolved);
488 return err;
491 *id = calloc(1, sizeof(**id));
492 if (*id == NULL)
493 return got_error_from_errno();
494 memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
495 return NULL;
498 char *
499 got_ref_to_str(struct got_reference *ref)
501 char *str;
503 if (ref->flags & GOT_REF_IS_SYMBOLIC)
504 return strdup(ref->ref.symref.ref);
506 str = malloc(SHA1_DIGEST_STRING_LENGTH);
507 if (str == NULL)
508 return NULL;
510 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
511 SHA1_DIGEST_STRING_LENGTH) == NULL) {
512 free(str);
513 return NULL;
516 return str;
519 const char *
520 got_ref_get_name(struct got_reference *ref)
522 if (ref->flags & GOT_REF_IS_SYMBOLIC)
523 return ref->ref.symref.name;
525 return ref->ref.ref.name;
528 static const struct got_error *
529 insert_ref(struct got_reflist_head *refs, struct got_reference *ref,
530 struct got_repository *repo)
532 const struct got_error *err;
533 struct got_object_id *id;
534 struct got_reflist_entry *new, *re, *prev;
535 int cmp;
537 err = got_ref_resolve(&id, repo, ref);
538 if (err)
539 return err;
541 new = malloc(sizeof(*re));
542 if (new == NULL) {
543 free(id);
544 return got_error_from_errno();
546 new->ref = ref;
547 new->id = id;
549 /*
550 * We must de-duplicate entries on insert because packed-refs may
551 * contain redundant entries. On-disk refs take precedence.
552 * This code assumes that on-disk revs are read before packed-refs.
553 * We're iterating the list anyway, so insert elements sorted by name.
554 */
555 re = SIMPLEQ_FIRST(refs);
556 while (re) {
557 cmp = got_path_cmp(got_ref_get_name(re->ref),
558 got_ref_get_name(ref));
559 if (cmp == 0) {
560 free(ref); /* duplicate */
561 return NULL;
562 } else if (cmp > 0) {
563 if (prev)
564 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
565 else
566 SIMPLEQ_INSERT_HEAD(refs, new, entry);
567 return NULL;
568 } else {
569 prev = re;
570 re = SIMPLEQ_NEXT(re, entry);
574 SIMPLEQ_INSERT_TAIL(refs, new, entry);
575 return NULL;
578 static const struct got_error *
579 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
580 const char *subdir, struct got_repository *repo)
582 const struct got_error *err = NULL;
583 DIR *d = NULL;
584 char *path_subdir;
586 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
587 return got_error_from_errno();
589 d = opendir(path_subdir);
590 if (d == NULL)
591 goto done;
593 while (1) {
594 struct dirent *dent;
595 struct got_reference *ref;
596 char *child;
598 dent = readdir(d);
599 if (dent == NULL)
600 break;
602 if (strcmp(dent->d_name, ".") == 0 ||
603 strcmp(dent->d_name, "..") == 0)
604 continue;
606 switch (dent->d_type) {
607 case DT_REG:
608 err = open_ref(&ref, path_refs, subdir, dent->d_name);
609 if (err)
610 goto done;
611 if (ref) {
612 err = insert_ref(refs, ref, repo);
613 if (err)
614 goto done;
616 break;
617 case DT_DIR:
618 if (asprintf(&child, "%s%s%s", subdir,
619 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
620 err = got_error_from_errno();
621 break;
623 err = gather_on_disk_refs(refs, path_refs, child, repo);
624 free(child);
625 break;
626 default:
627 break;
630 done:
631 if (d)
632 closedir(d);
633 free(path_subdir);
634 return err;
637 const struct got_error *
638 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
640 const struct got_error *err;
641 char *packed_refs_path, *path_refs = NULL;
642 FILE *f = NULL;
643 struct got_reference *ref;
645 /* HEAD ref should always exist. */
646 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
647 if (path_refs == NULL) {
648 err = got_error_from_errno();
649 goto done;
651 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD);
652 if (err)
653 goto done;
654 err = insert_ref(refs, ref, repo);
655 if (err)
656 goto done;
658 /* Gather on-disk refs before parsing packed-refs. */
659 free(path_refs);
660 path_refs = get_refs_dir_path(repo, "");
661 if (path_refs == NULL) {
662 err = got_error_from_errno();
663 goto done;
665 err = gather_on_disk_refs(refs, path_refs, "", repo);
666 if (err)
667 goto done;
669 /*
670 * The packed-refs file may contain redundant entries, in which
671 * case on-disk refs take precedence.
672 */
673 packed_refs_path = got_repo_get_path_packed_refs(repo);
674 if (packed_refs_path == NULL) {
675 err = got_error_from_errno();
676 goto done;
679 f = fopen(packed_refs_path, "r");
680 free(packed_refs_path);
681 if (f) {
682 char *line;
683 size_t len;
684 const char delim[3] = {'\0', '\0', '\0'};
685 while (1) {
686 line = fparseln(f, &len, NULL, delim, 0);
687 if (line == NULL)
688 break;
689 err = parse_packed_ref_line(&ref, NULL, line);
690 if (err)
691 goto done;
692 if (ref) {
693 err = insert_ref(refs, ref, repo);
694 if (err)
695 goto done;
699 done:
700 free(path_refs);
701 if (f && fclose(f) != 0 && err == NULL)
702 err = got_error_from_errno();
703 return err;
706 void
707 got_ref_list_free(struct got_reflist_head *refs)
709 struct got_reflist_entry *re;
711 while (!SIMPLEQ_EMPTY(refs)) {
712 re = SIMPLEQ_FIRST(refs);
713 SIMPLEQ_REMOVE_HEAD(refs, entry);
714 got_ref_close(re->ref);
715 free(re->id);
716 free(re);
721 const struct got_error *
722 got_ref_write(struct got_reference *ref, struct got_repository *repo)
724 const struct got_error *err = NULL, *unlock_err = NULL;
725 const char *name = got_ref_get_name(ref);
726 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
727 struct got_lockfile *lf = NULL;
728 FILE *f = NULL;
729 size_t n;
730 struct stat sb;
732 path_refs = get_refs_dir_path(repo, name);
733 if (path_refs == NULL) {
734 err = got_error_from_errno();
735 goto done;
738 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
739 err = got_error_from_errno();
740 goto done;
743 err = got_opentemp_named(&tmppath, &f, path);
744 if (err) {
745 char *parent;
746 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
747 goto done;
748 err = got_path_dirname(&parent, path);
749 if (err)
750 goto done;
751 err = got_path_mkdir(parent);
752 free(parent);
753 if (err)
754 goto done;
755 err = got_opentemp_named(&tmppath, &f, path);
756 if (err)
757 goto done;
760 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
761 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
762 if (n != strlen(ref->ref.symref.ref) + 6) {
763 err = got_ferror(f, GOT_ERR_IO);
764 goto done;
766 } else {
767 char hex[SHA1_DIGEST_STRING_LENGTH];
768 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
769 sizeof(hex)) == NULL) {
770 err = got_error(GOT_ERR_BAD_REF_DATA);
771 goto done;
773 n = fprintf(f, "%s\n", hex);
774 if (n != sizeof(hex)) {
775 err = got_ferror(f, GOT_ERR_IO);
776 goto done;
780 err = got_lockfile_lock(&lf, path);
781 if (err)
782 goto done;
784 /* XXX: check if old content matches our expectations? */
786 if (stat(path, &sb) != 0) {
787 if (errno != ENOENT) {
788 err = got_error_from_errno();
789 goto done;
791 sb.st_mode = GOT_DEFAULT_FILE_MODE;
794 if (rename(tmppath, path) != 0) {
795 err = got_error_from_errno();
796 goto done;
798 free(tmppath);
799 tmppath = NULL;
801 if (chmod(path, sb.st_mode) != 0) {
802 err = got_error_from_errno();
803 goto done;
805 done:
806 if (lf)
807 unlock_err = got_lockfile_unlock(lf);
808 if (f) {
809 if (fclose(f) != 0 && err == NULL)
810 err = got_error_from_errno();
812 free(path_refs);
813 free(path);
814 if (tmppath) {
815 if (unlink(tmppath) != 0 && err == NULL)
816 err = got_error_from_errno();
817 free(tmppath);
819 return err ? err : unlock_err;
822 const struct got_error *
823 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
825 const struct got_error *err = NULL, *unlock_err = NULL;
826 const char *name = got_ref_get_name(ref);
827 char *path_refs = NULL, *path = NULL;
828 struct got_lockfile *lf = NULL;
830 /* TODO: handle packed refs ! */
832 path_refs = get_refs_dir_path(repo, name);
833 if (path_refs == NULL) {
834 err = got_error_from_errno();
835 goto done;
838 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
839 err = got_error_from_errno();
840 goto done;
843 err = got_lockfile_lock(&lf, path);
844 if (err)
845 goto done;
847 /* XXX: check if old content matches our expectations? */
849 if (unlink(path) != 0)
850 err = got_error_from_errno();
851 done:
852 if (lf)
853 unlock_err = got_lockfile_unlock(lf);
855 free(path_refs);
856 free(path);
857 return err ? err : unlock_err;