2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/types.h>
18 #include <sys/queue.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"
40 #include "got_lib_sha1.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"
47 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
50 #define GOT_REF_HEADS "heads"
51 #define GOT_REF_TAGS "tags"
52 #define GOT_REF_REMOTES "remotes"
55 * We do not resolve tags yet, and don't yet care about sorting refs either,
56 * so packed-refs files we write contain a minimal header which disables all
57 * packed-refs "traits" supported by Git.
59 #define GOT_PACKED_REFS_HEADER "# pack-refs with:"
61 /* A symbolic reference. */
67 /* A non-symbolic reference (there is no better designation). */
70 u_int8_t sha1[SHA1_DIGEST_LENGTH];
73 /* A reference which points to an arbitrary object. */
74 struct got_reference {
76 #define GOT_REF_IS_SYMBOLIC 0x01
77 #define GOT_REF_IS_PACKED 0x02
81 struct got_symref symref;
85 static const struct got_error *
86 alloc_ref(struct got_reference **ref, const char *name,
87 struct got_object_id *id, int flags)
89 const struct got_error *err = NULL;
91 *ref = calloc(1, sizeof(**ref));
93 return got_error_prefix_errno("calloc");
95 memcpy((*ref)->ref.ref.sha1, id->sha1, sizeof((*ref)->ref.ref.sha1));
96 (*ref)->flags = flags;
97 (*ref)->ref.ref.name = strdup(name);
98 if ((*ref)->ref.ref.name == NULL) {
99 err = got_error_prefix_errno("strdup");
106 static const struct got_error *
107 alloc_symref(struct got_reference **ref, const char *name,
108 const char *target_ref, int flags)
110 const struct got_error *err = NULL;
112 *ref = calloc(1, sizeof(**ref));
114 return got_error_prefix_errno("calloc");
116 (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
117 (*ref)->ref.symref.name = strdup(name);
118 if ((*ref)->ref.symref.name == NULL) {
119 err = got_error_prefix_errno("strdup");
123 (*ref)->ref.symref.ref = strdup(target_ref);
124 if ((*ref)->ref.symref.ref == NULL) {
125 err = got_error_prefix_errno("strdup");
132 static const struct got_error *
133 parse_symref(struct got_reference **ref, const char *name, const char *line)
136 return got_error(GOT_ERR_BAD_REF_DATA);
138 return alloc_symref(ref, name, line, 0);
141 static const struct got_error *
142 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
144 struct got_object_id id;
146 if (strncmp(line, "ref: ", 5) == 0) {
148 return parse_symref(ref, name, line);
151 if (!got_parse_sha1_digest(id.sha1, line))
152 return got_error(GOT_ERR_BAD_REF_DATA);
154 return alloc_ref(ref, name, &id, 0);
157 static const struct got_error *
158 parse_ref_file(struct got_reference **ref, const char *name,
161 const struct got_error *err = NULL;
165 const char delim[3] = {'\0', '\0', '\0'};
167 f = fopen(abspath, "rb");
171 line = fparseln(f, &len, NULL, delim, 0);
173 err = got_error(GOT_ERR_BAD_REF_DATA);
177 err = parse_ref_line(ref, name, line);
180 if (fclose(f) != 0 && err == NULL)
181 err = got_error_prefix_errno("fclose");
186 is_well_known_ref(const char *refname)
188 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
189 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
190 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
191 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
195 get_refs_dir_path(struct got_repository *repo, const char *refname)
197 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
198 return strdup(got_repo_get_path_git_dir(repo));
200 return got_repo_get_path_refs(repo);
204 is_valid_ref_name(const char *name)
206 const char *s, *slash, *seg;
207 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
208 const char *forbidden_seq[] = { "//", "..", "@{" };
209 const char *lfs = GOT_LOCKFILE_SUFFIX;
210 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
213 if (name[0] == '@' && name[1] == '\0')
216 slash = strchr(name, '/');
222 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
225 for (i = 0; i < nitems(forbidden); i++) {
226 if (*s == forbidden[i])
229 for (i = 0; i < nitems(forbidden_seq); i++) {
230 if (s[0] == forbidden_seq[i][0] &&
231 s[1] == forbidden_seq[i][1])
234 if (iscntrl((unsigned char)s[0]))
236 if (s[0] == '.' && s[1] == '\0')
239 const char *nextseg = s + 1;
240 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
243 if (seg <= s - lfs_len &&
244 strncmp(s - lfs_len, lfs, lfs_len) == 0)
251 if (seg <= s - lfs_len &&
252 strncmp(s - lfs_len, lfs, lfs_len) == 0)
258 const struct got_error *
259 got_ref_alloc(struct got_reference **ref, const char *name,
260 struct got_object_id *id)
262 if (!is_valid_ref_name(name))
263 return got_error(GOT_ERR_BAD_REF_NAME);
265 return alloc_ref(ref, name, id, 0);
268 static const struct got_error *
269 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
272 struct got_object_id id;
277 if (line[0] == '#' || line[0] == '^')
280 if (!got_parse_sha1_digest(id.sha1, line))
281 return got_error(GOT_ERR_BAD_REF_DATA);
284 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
288 name = line + SHA1_DIGEST_STRING_LENGTH;
290 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
293 static const struct got_error *
294 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
295 int nsubdirs, const char *refname)
297 const struct got_error *err = NULL;
301 const char delim[3] = {'\0', '\0', '\0'};
302 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
307 abs_refname = (char *)refname;
309 line = fparseln(f, &len, NULL, delim, 0);
313 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
316 for (i = 0; i < nsubdirs; i++) {
317 if (!ref_is_absolute &&
318 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
320 return got_error_prefix_errno("asprintf");
321 err = parse_packed_ref_line(ref, abs_refname, line);
322 if (!ref_is_absolute)
324 if (err || *ref != NULL)
330 } while (*ref == NULL);
335 static const struct got_error *
336 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
339 const struct got_error *err = NULL;
341 char *normpath = NULL;
342 char *absname = NULL;
343 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
344 int ref_is_well_known = is_well_known_ref(name);
348 if (ref_is_absolute || ref_is_well_known) {
349 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
350 return got_error_prefix_errno("asprintf");
351 absname = (char *)name;
353 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
354 subdir[0] ? "/" : "", name) == -1)
355 return got_error_prefix_errno("asprintf");
357 if (asprintf(&absname, "refs/%s%s%s",
358 subdir, subdir[0] ? "/" : "", name) == -1) {
359 err = got_error_prefix_errno("asprintf");
364 normpath = got_path_normalize(path);
365 if (normpath == NULL) {
366 err = got_error_prefix_errno2("got_path_normalize", path);
370 err = parse_ref_file(ref, absname, normpath);
372 if (!ref_is_absolute && !ref_is_well_known)
379 const struct got_error *
380 got_ref_open(struct got_reference **ref, struct got_repository *repo,
383 const struct got_error *err = NULL;
384 char *path_refs = NULL;
385 const char *subdirs[] = {
386 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
388 int i, well_known = is_well_known_ref(refname);
392 path_refs = get_refs_dir_path(repo, refname);
393 if (path_refs == NULL) {
394 err = got_error_prefix_errno2("get_refs_dir_path", refname);
399 char *packed_refs_path;
402 /* Search on-disk refs before packed refs! */
403 for (i = 0; i < nitems(subdirs); i++) {
404 err = open_ref(ref, path_refs, subdirs[i], refname);
409 packed_refs_path = got_repo_get_path_packed_refs(repo);
410 if (packed_refs_path == NULL) {
411 err = got_error_prefix_errno(
412 "got_repo_get_path_packed_refs");
416 f = fopen(packed_refs_path, "rb");
417 free(packed_refs_path);
419 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
421 if (fclose(f) != 0 && err == NULL)
422 err = got_error_prefix_errno("fclose");
428 err = open_ref(ref, path_refs, "", refname);
433 err = got_error_not_ref(refname);
439 got_ref_close(struct got_reference *ref)
441 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
442 free(ref->ref.symref.name);
443 free(ref->ref.symref.ref);
445 free(ref->ref.ref.name);
449 struct got_reference *
450 got_ref_dup(struct got_reference *ref)
452 struct got_reference *ret;
454 ret = calloc(1, sizeof(*ret));
458 ret->flags = ref->flags;
459 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
460 ret->ref.symref.name = strdup(ref->ref.symref.name);
461 if (ret->ref.symref.name == NULL) {
465 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
466 if (ret->ref.symref.ref == NULL) {
467 free(ret->ref.symref.name);
472 ref->ref.ref.name = strdup(ref->ref.ref.name);
473 if (ref->ref.ref.name == NULL) {
477 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
478 sizeof(ret->ref.ref.sha1));
484 static const struct got_error *
485 resolve_symbolic_ref(struct got_reference **resolved,
486 struct got_repository *repo, struct got_reference *ref)
488 struct got_reference *nextref;
489 const struct got_error *err;
491 err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
495 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
496 err = resolve_symbolic_ref(resolved, repo, nextref);
498 *resolved = got_ref_dup(nextref);
500 got_ref_close(nextref);
504 const struct got_error *
505 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
506 struct got_reference *ref)
508 const struct got_error *err;
510 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
511 struct got_reference *resolved = NULL;
512 err = resolve_symbolic_ref(&resolved, repo, ref);
514 err = got_ref_resolve(id, repo, resolved);
516 got_ref_close(resolved);
520 *id = calloc(1, sizeof(**id));
522 return got_error_prefix_errno("calloc");
523 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
528 got_ref_to_str(struct got_reference *ref)
532 if (ref->flags & GOT_REF_IS_SYMBOLIC)
533 return strdup(ref->ref.symref.ref);
535 str = malloc(SHA1_DIGEST_STRING_LENGTH);
539 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
540 SHA1_DIGEST_STRING_LENGTH) == NULL) {
549 got_ref_get_name(struct got_reference *ref)
551 if (ref->flags & GOT_REF_IS_SYMBOLIC)
552 return ref->ref.symref.name;
554 return ref->ref.ref.name;
557 static const struct got_error *
558 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
559 struct got_reference *ref, struct got_repository *repo)
561 const struct got_error *err;
562 struct got_object_id *id;
563 struct got_reflist_entry *new, *re, *prev = NULL;
568 err = got_ref_resolve(&id, repo, ref);
572 new = malloc(sizeof(*new));
575 return got_error_prefix_errno("malloc");
582 * We must de-duplicate entries on insert because packed-refs may
583 * contain redundant entries. On-disk refs take precedence.
584 * This code assumes that on-disk revs are read before packed-refs.
585 * We're iterating the list anyway, so insert elements sorted by name.
587 re = SIMPLEQ_FIRST(refs);
589 cmp = got_path_cmp(got_ref_get_name(re->ref),
590 got_ref_get_name(new->ref));
597 } else if (cmp > 0) {
599 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
601 SIMPLEQ_INSERT_HEAD(refs, new, entry);
605 re = SIMPLEQ_NEXT(re, entry);
609 SIMPLEQ_INSERT_TAIL(refs, new, entry);
613 static const struct got_error *
614 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
615 const char *subdir, struct got_repository *repo)
617 const struct got_error *err = NULL;
621 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
622 return got_error_prefix_errno("asprintf");
624 d = opendir(path_subdir);
630 struct got_reference *ref;
637 if (strcmp(dent->d_name, ".") == 0 ||
638 strcmp(dent->d_name, "..") == 0)
641 switch (dent->d_type) {
643 err = open_ref(&ref, path_refs, subdir, dent->d_name);
647 struct got_reflist_entry *new;
648 err = insert_ref(&new, refs, ref, repo);
649 if (err || new == NULL /* duplicate */)
656 if (asprintf(&child, "%s%s%s", subdir,
657 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
658 err = got_error_prefix_errno("asprintf");
661 err = gather_on_disk_refs(refs, path_refs, child, repo);
675 const struct got_error *
676 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
678 const struct got_error *err;
679 char *packed_refs_path, *path_refs = NULL;
681 struct got_reference *ref;
682 struct got_reflist_entry *new;
684 /* HEAD ref should always exist. */
685 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
686 if (path_refs == NULL) {
687 err = got_error_prefix_errno("get_refs_dir_path");
690 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD);
693 err = insert_ref(&new, refs, ref, repo);
694 if (err || new == NULL /* duplicate */)
699 /* Gather on-disk refs before parsing packed-refs. */
701 path_refs = get_refs_dir_path(repo, "");
702 if (path_refs == NULL) {
703 err = got_error_prefix_errno("get_refs_dir_path");
706 err = gather_on_disk_refs(refs, path_refs, "", repo);
711 * The packed-refs file may contain redundant entries, in which
712 * case on-disk refs take precedence.
714 packed_refs_path = got_repo_get_path_packed_refs(repo);
715 if (packed_refs_path == NULL) {
716 err = got_error_prefix_errno("got_repo_get_path_packed_refs");
720 f = fopen(packed_refs_path, "r");
721 free(packed_refs_path);
725 const char delim[3] = {'\0', '\0', '\0'};
727 line = fparseln(f, &len, NULL, delim, 0);
731 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
734 err = parse_packed_ref_line(&ref, NULL, line);
739 err = insert_ref(&new, refs, ref, repo);
740 if (err || new == NULL /* duplicate */)
749 if (f && fclose(f) != 0 && err == NULL)
750 err = got_error_prefix_errno("fclose");
755 got_ref_list_free(struct got_reflist_head *refs)
757 struct got_reflist_entry *re;
759 while (!SIMPLEQ_EMPTY(refs)) {
760 re = SIMPLEQ_FIRST(refs);
761 SIMPLEQ_REMOVE_HEAD(refs, entry);
762 got_ref_close(re->ref);
770 got_ref_is_symbolic(struct got_reference *ref)
772 return (ref->flags & GOT_REF_IS_SYMBOLIC);
775 const struct got_error *
776 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
778 if (ref->flags & GOT_REF_IS_SYMBOLIC)
779 return got_error(GOT_ERR_BAD_REF_TYPE);
781 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
785 const struct got_error *
786 got_ref_change_symref(struct got_reference *ref, char *refname)
790 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
791 return got_error(GOT_ERR_BAD_REF_TYPE);
793 new_name = strdup(refname);
794 if (new_name == NULL)
795 return got_error_prefix_errno("strdup");
797 free(ref->ref.symref.name);
798 ref->ref.symref.name = new_name;
802 const struct got_error *
803 got_ref_write(struct got_reference *ref, struct got_repository *repo)
805 const struct got_error *err = NULL, *unlock_err = NULL;
806 const char *name = got_ref_get_name(ref);
807 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
808 struct got_lockfile *lf = NULL;
813 path_refs = get_refs_dir_path(repo, name);
814 if (path_refs == NULL) {
815 err = got_error_prefix_errno2("get_refs_dir_path", name);
819 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
820 err = got_error_prefix_errno("asprintf");
824 err = got_opentemp_named(&tmppath, &f, path);
827 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
829 err = got_path_dirname(&parent, path);
832 err = got_path_mkdir(parent);
836 err = got_opentemp_named(&tmppath, &f, path);
841 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
842 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
843 if (n != strlen(ref->ref.symref.ref) + 6) {
844 err = got_ferror(f, GOT_ERR_IO);
848 char hex[SHA1_DIGEST_STRING_LENGTH];
849 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
850 sizeof(hex)) == NULL) {
851 err = got_error(GOT_ERR_BAD_REF_DATA);
854 n = fprintf(f, "%s\n", hex);
855 if (n != sizeof(hex)) {
856 err = got_ferror(f, GOT_ERR_IO);
861 err = got_lockfile_lock(&lf, path);
865 /* XXX: check if old content matches our expectations? */
867 if (stat(path, &sb) != 0) {
868 if (errno != ENOENT) {
869 err = got_error_prefix_errno2("stat", path);
872 sb.st_mode = GOT_DEFAULT_FILE_MODE;
875 if (rename(tmppath, path) != 0) {
876 err = got_error_prefix_errno3("rename", tmppath, path);
882 if (chmod(path, sb.st_mode) != 0) {
883 err = got_error_prefix_errno2("chmod", path);
888 unlock_err = got_lockfile_unlock(lf);
890 if (fclose(f) != 0 && err == NULL)
891 err = got_error_prefix_errno("fclose");
896 if (unlink(tmppath) != 0 && err == NULL)
897 err = got_error_prefix_errno2("unlink", tmppath);
900 return err ? err : unlock_err;
903 static const struct got_error *
904 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
906 const struct got_error *err = NULL, *unlock_err = NULL;
907 struct got_lockfile *lf = NULL;
908 FILE *f = NULL, *tmpf = NULL;
909 char *packed_refs_path, *tmppath = NULL;
910 struct got_reflist_head refs;
911 int found_delref = 0;
913 /* The packed-refs file does not cotain symbolic references. */
914 if (delref->flags & GOT_REF_IS_SYMBOLIC)
915 return got_error(GOT_ERR_BAD_REF_DATA);
919 packed_refs_path = got_repo_get_path_packed_refs(repo);
920 if (packed_refs_path == NULL)
921 return got_error_prefix_errno("got_repo_get_path_packed_refs");
923 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
927 err = got_lockfile_lock(&lf, packed_refs_path);
931 f = fopen(packed_refs_path, "r");
933 err = got_error_prefix_errno2("fopen", packed_refs_path);
939 const char delim[3] = {'\0', '\0', '\0'};
940 struct got_reference *ref;
941 struct got_reflist_entry *new;
943 line = fparseln(f, &len, NULL, delim, 0);
947 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
950 err = parse_packed_ref_line(&ref, NULL, line);
957 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
958 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
959 sizeof(delref->ref.ref.sha1)) == 0) {
965 err = insert_ref(&new, &refs, ref, repo);
966 if (err || new == NULL /* duplicate */)
973 struct got_reflist_entry *re;
977 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
978 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
979 err = got_ferror(f, GOT_ERR_IO);
983 SIMPLEQ_FOREACH(re, &refs, entry) {
984 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
986 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
987 sizeof(hex)) == NULL) {
988 err = got_error(GOT_ERR_BAD_REF_DATA);
991 n = fprintf(tmpf, "%s ", hex);
992 if (n != sizeof(hex)) {
993 err = got_ferror(f, GOT_ERR_IO);
996 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
997 if (n != strlen(re->ref->ref.ref.name) + 1) {
998 err = got_ferror(f, GOT_ERR_IO);
1003 if (fflush(tmpf) != 0) {
1004 err = got_error_prefix_errno("fflush");
1008 if (stat(packed_refs_path, &sb) != 0) {
1009 if (errno != ENOENT) {
1010 err = got_error_prefix_errno2("stat",
1014 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1017 if (rename(tmppath, packed_refs_path) != 0) {
1018 err = got_error_prefix_errno3("rename", tmppath,
1023 if (chmod(packed_refs_path, sb.st_mode) != 0) {
1024 err = got_error_prefix_errno2("chmod",
1031 unlock_err = got_lockfile_unlock(lf);
1033 if (fclose(f) != 0 && err == NULL)
1034 err = got_error_prefix_errno("fclose");
1038 if (fclose(tmpf) != 0 && err == NULL)
1039 err = got_error_prefix_errno("fclose");
1042 free(packed_refs_path);
1043 got_ref_list_free(&refs);
1044 return err ? err : unlock_err;
1047 const struct got_error *
1048 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1050 const struct got_error *err = NULL, *unlock_err = NULL;
1051 const char *name = got_ref_get_name(ref);
1052 char *path_refs = NULL, *path = NULL;
1053 struct got_lockfile *lf = NULL;
1055 if (ref->flags & GOT_REF_IS_PACKED)
1056 return delete_packed_ref(ref, repo);
1058 path_refs = get_refs_dir_path(repo, name);
1059 if (path_refs == NULL) {
1060 err = got_error_prefix_errno2("get_refs_dir_path", name);
1064 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1065 err = got_error_prefix_errno("asprintf");
1069 err = got_lockfile_lock(&lf, path);
1073 /* XXX: check if old content matches our expectations? */
1075 if (unlink(path) != 0)
1076 err = got_error_prefix_errno2("unlink", path);
1079 unlock_err = got_lockfile_unlock(lf);
1083 return err ? err : unlock_err;