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;
84 struct got_lockfile *lf;
87 static const struct got_error *
88 alloc_ref(struct got_reference **ref, const char *name,
89 struct got_object_id *id, int flags)
91 const struct got_error *err = NULL;
93 *ref = calloc(1, sizeof(**ref));
95 return got_error_prefix_errno("calloc");
97 memcpy((*ref)->ref.ref.sha1, id->sha1, sizeof((*ref)->ref.ref.sha1));
98 (*ref)->flags = flags;
99 (*ref)->ref.ref.name = strdup(name);
100 if ((*ref)->ref.ref.name == NULL) {
101 err = got_error_prefix_errno("strdup");
108 static const struct got_error *
109 alloc_symref(struct got_reference **ref, const char *name,
110 const char *target_ref, int flags)
112 const struct got_error *err = NULL;
114 *ref = calloc(1, sizeof(**ref));
116 return got_error_prefix_errno("calloc");
118 (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
119 (*ref)->ref.symref.name = strdup(name);
120 if ((*ref)->ref.symref.name == NULL) {
121 err = got_error_prefix_errno("strdup");
125 (*ref)->ref.symref.ref = strdup(target_ref);
126 if ((*ref)->ref.symref.ref == NULL) {
127 err = got_error_prefix_errno("strdup");
134 static const struct got_error *
135 parse_symref(struct got_reference **ref, const char *name, const char *line)
138 return got_error(GOT_ERR_BAD_REF_DATA);
140 return alloc_symref(ref, name, line, 0);
143 static const struct got_error *
144 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
146 struct got_object_id id;
148 if (strncmp(line, "ref: ", 5) == 0) {
150 return parse_symref(ref, name, line);
153 if (!got_parse_sha1_digest(id.sha1, line))
154 return got_error(GOT_ERR_BAD_REF_DATA);
156 return alloc_ref(ref, name, &id, 0);
159 static const struct got_error *
160 parse_ref_file(struct got_reference **ref, const char *name,
161 const char *abspath, int lock)
163 const struct got_error *err = NULL;
167 const char delim[3] = {'\0', '\0', '\0'};
168 struct got_lockfile *lf = NULL;
171 err = got_lockfile_lock(&lf, abspath);
176 f = fopen(abspath, "rb");
180 line = fparseln(f, &len, NULL, delim, 0);
182 err = got_error(GOT_ERR_BAD_REF_DATA);
186 err = parse_ref_line(ref, name, line);
187 if (err == NULL && lf)
191 if (fclose(f) != 0 && err == NULL) {
192 err = got_error_prefix_errno("fclose");
194 got_lockfile_unlock(lf);
204 is_well_known_ref(const char *refname)
206 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
207 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
208 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
209 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
213 get_refs_dir_path(struct got_repository *repo, const char *refname)
215 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
216 return strdup(got_repo_get_path_git_dir(repo));
218 return got_repo_get_path_refs(repo);
222 is_valid_ref_name(const char *name)
224 const char *s, *slash, *seg;
225 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
226 const char *forbidden_seq[] = { "//", "..", "@{" };
227 const char *lfs = GOT_LOCKFILE_SUFFIX;
228 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
231 if (name[0] == '@' && name[1] == '\0')
234 slash = strchr(name, '/');
240 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
243 for (i = 0; i < nitems(forbidden); i++) {
244 if (*s == forbidden[i])
247 for (i = 0; i < nitems(forbidden_seq); i++) {
248 if (s[0] == forbidden_seq[i][0] &&
249 s[1] == forbidden_seq[i][1])
252 if (iscntrl((unsigned char)s[0]))
254 if (s[0] == '.' && s[1] == '\0')
257 const char *nextseg = s + 1;
258 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
261 if (seg <= s - lfs_len &&
262 strncmp(s - lfs_len, lfs, lfs_len) == 0)
269 if (seg <= s - lfs_len &&
270 strncmp(s - lfs_len, lfs, lfs_len) == 0)
276 const struct got_error *
277 got_ref_alloc(struct got_reference **ref, const char *name,
278 struct got_object_id *id)
280 if (!is_valid_ref_name(name))
281 return got_error(GOT_ERR_BAD_REF_NAME);
283 return alloc_ref(ref, name, id, 0);
286 static const struct got_error *
287 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
290 struct got_object_id id;
295 if (line[0] == '#' || line[0] == '^')
298 if (!got_parse_sha1_digest(id.sha1, line))
299 return got_error(GOT_ERR_BAD_REF_DATA);
302 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
306 name = line + SHA1_DIGEST_STRING_LENGTH;
308 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
311 static const struct got_error *
312 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
313 int nsubdirs, const char *refname)
315 const struct got_error *err = NULL;
319 const char delim[3] = {'\0', '\0', '\0'};
320 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
325 abs_refname = (char *)refname;
327 line = fparseln(f, &len, NULL, delim, 0);
331 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
334 for (i = 0; i < nsubdirs; i++) {
335 if (!ref_is_absolute &&
336 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
338 return got_error_prefix_errno("asprintf");
339 err = parse_packed_ref_line(ref, abs_refname, line);
340 if (!ref_is_absolute)
342 if (err || *ref != NULL)
348 } while (*ref == NULL);
353 static const struct got_error *
354 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
355 const char *name, int lock)
357 const struct got_error *err = NULL;
359 char *normpath = NULL;
360 char *absname = NULL;
361 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
362 int ref_is_well_known = is_well_known_ref(name);
366 if (ref_is_absolute || ref_is_well_known) {
367 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
368 return got_error_prefix_errno("asprintf");
369 absname = (char *)name;
371 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
372 subdir[0] ? "/" : "", name) == -1)
373 return got_error_prefix_errno("asprintf");
375 if (asprintf(&absname, "refs/%s%s%s",
376 subdir, subdir[0] ? "/" : "", name) == -1) {
377 err = got_error_prefix_errno("asprintf");
382 normpath = got_path_normalize(path);
383 if (normpath == NULL) {
384 err = got_error_prefix_errno2("got_path_normalize", path);
388 err = parse_ref_file(ref, absname, normpath, lock);
390 if (!ref_is_absolute && !ref_is_well_known)
397 const struct got_error *
398 got_ref_open(struct got_reference **ref, struct got_repository *repo,
399 const char *refname, int lock)
401 const struct got_error *err = NULL;
402 char *path_refs = NULL;
403 const char *subdirs[] = {
404 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
406 int i, well_known = is_well_known_ref(refname);
410 path_refs = get_refs_dir_path(repo, refname);
411 if (path_refs == NULL) {
412 err = got_error_prefix_errno2("get_refs_dir_path", refname);
417 char *packed_refs_path;
420 /* Search on-disk refs before packed refs! */
421 for (i = 0; i < nitems(subdirs); i++) {
422 err = open_ref(ref, path_refs, subdirs[i], refname,
428 packed_refs_path = got_repo_get_path_packed_refs(repo);
429 if (packed_refs_path == NULL) {
430 err = got_error_prefix_errno(
431 "got_repo_get_path_packed_refs");
436 err = got_lockfile_lock(&(*ref)->lf, packed_refs_path);
440 f = fopen(packed_refs_path, "rb");
441 free(packed_refs_path);
443 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
445 if (fclose(f) != 0 && err == NULL)
446 err = got_error_prefix_errno("fclose");
452 err = open_ref(ref, path_refs, "", refname, lock);
457 err = got_error_not_ref(refname);
463 got_ref_close(struct got_reference *ref)
465 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
466 free(ref->ref.symref.name);
467 free(ref->ref.symref.ref);
469 free(ref->ref.ref.name);
473 struct got_reference *
474 got_ref_dup(struct got_reference *ref)
476 struct got_reference *ret;
478 ret = calloc(1, sizeof(*ret));
482 ret->flags = ref->flags;
483 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
484 ret->ref.symref.name = strdup(ref->ref.symref.name);
485 if (ret->ref.symref.name == NULL) {
489 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
490 if (ret->ref.symref.ref == NULL) {
491 free(ret->ref.symref.name);
496 ref->ref.ref.name = strdup(ref->ref.ref.name);
497 if (ref->ref.ref.name == NULL) {
501 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
502 sizeof(ret->ref.ref.sha1));
508 static const struct got_error *
509 resolve_symbolic_ref(struct got_reference **resolved,
510 struct got_repository *repo, struct got_reference *ref)
512 struct got_reference *nextref;
513 const struct got_error *err;
515 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
519 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
520 err = resolve_symbolic_ref(resolved, repo, nextref);
522 *resolved = got_ref_dup(nextref);
524 got_ref_close(nextref);
528 const struct got_error *
529 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
530 struct got_reference *ref)
532 const struct got_error *err;
534 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
535 struct got_reference *resolved = NULL;
536 err = resolve_symbolic_ref(&resolved, repo, ref);
538 err = got_ref_resolve(id, repo, resolved);
540 got_ref_close(resolved);
544 *id = calloc(1, sizeof(**id));
546 return got_error_prefix_errno("calloc");
547 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
552 got_ref_to_str(struct got_reference *ref)
556 if (ref->flags & GOT_REF_IS_SYMBOLIC)
557 return strdup(ref->ref.symref.ref);
559 str = malloc(SHA1_DIGEST_STRING_LENGTH);
563 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
564 SHA1_DIGEST_STRING_LENGTH) == NULL) {
573 got_ref_get_name(struct got_reference *ref)
575 if (ref->flags & GOT_REF_IS_SYMBOLIC)
576 return ref->ref.symref.name;
578 return ref->ref.ref.name;
581 static const struct got_error *
582 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
583 struct got_reference *ref, struct got_repository *repo)
585 const struct got_error *err;
586 struct got_object_id *id;
587 struct got_reflist_entry *new, *re, *prev = NULL;
592 err = got_ref_resolve(&id, repo, ref);
596 new = malloc(sizeof(*new));
599 return got_error_prefix_errno("malloc");
606 * We must de-duplicate entries on insert because packed-refs may
607 * contain redundant entries. On-disk refs take precedence.
608 * This code assumes that on-disk revs are read before packed-refs.
609 * We're iterating the list anyway, so insert elements sorted by name.
611 re = SIMPLEQ_FIRST(refs);
613 cmp = got_path_cmp(got_ref_get_name(re->ref),
614 got_ref_get_name(new->ref));
621 } else if (cmp > 0) {
623 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
625 SIMPLEQ_INSERT_HEAD(refs, new, entry);
629 re = SIMPLEQ_NEXT(re, entry);
633 SIMPLEQ_INSERT_TAIL(refs, new, entry);
637 static const struct got_error *
638 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
639 const char *subdir, struct got_repository *repo)
641 const struct got_error *err = NULL;
645 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
646 return got_error_prefix_errno("asprintf");
648 d = opendir(path_subdir);
654 struct got_reference *ref;
661 if (strcmp(dent->d_name, ".") == 0 ||
662 strcmp(dent->d_name, "..") == 0)
665 switch (dent->d_type) {
667 err = open_ref(&ref, path_refs, subdir, dent->d_name,
672 struct got_reflist_entry *new;
673 err = insert_ref(&new, refs, ref, repo);
674 if (err || new == NULL /* duplicate */)
681 if (asprintf(&child, "%s%s%s", subdir,
682 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
683 err = got_error_prefix_errno("asprintf");
686 err = gather_on_disk_refs(refs, path_refs, child, repo);
700 const struct got_error *
701 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
703 const struct got_error *err;
704 char *packed_refs_path, *path_refs = NULL;
706 struct got_reference *ref;
707 struct got_reflist_entry *new;
709 /* HEAD ref should always exist. */
710 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
711 if (path_refs == NULL) {
712 err = got_error_prefix_errno("get_refs_dir_path");
715 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
718 err = insert_ref(&new, refs, ref, repo);
719 if (err || new == NULL /* duplicate */)
724 /* Gather on-disk refs before parsing packed-refs. */
726 path_refs = get_refs_dir_path(repo, "");
727 if (path_refs == NULL) {
728 err = got_error_prefix_errno("get_refs_dir_path");
731 err = gather_on_disk_refs(refs, path_refs, "", repo);
736 * The packed-refs file may contain redundant entries, in which
737 * case on-disk refs take precedence.
739 packed_refs_path = got_repo_get_path_packed_refs(repo);
740 if (packed_refs_path == NULL) {
741 err = got_error_prefix_errno("got_repo_get_path_packed_refs");
745 f = fopen(packed_refs_path, "r");
746 free(packed_refs_path);
750 const char delim[3] = {'\0', '\0', '\0'};
752 line = fparseln(f, &len, NULL, delim, 0);
756 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
759 err = parse_packed_ref_line(&ref, NULL, line);
764 err = insert_ref(&new, refs, ref, repo);
765 if (err || new == NULL /* duplicate */)
774 if (f && fclose(f) != 0 && err == NULL)
775 err = got_error_prefix_errno("fclose");
780 got_ref_list_free(struct got_reflist_head *refs)
782 struct got_reflist_entry *re;
784 while (!SIMPLEQ_EMPTY(refs)) {
785 re = SIMPLEQ_FIRST(refs);
786 SIMPLEQ_REMOVE_HEAD(refs, entry);
787 got_ref_close(re->ref);
795 got_ref_is_symbolic(struct got_reference *ref)
797 return (ref->flags & GOT_REF_IS_SYMBOLIC);
800 const struct got_error *
801 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
803 if (ref->flags & GOT_REF_IS_SYMBOLIC)
804 return got_error(GOT_ERR_BAD_REF_TYPE);
806 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
810 const struct got_error *
811 got_ref_change_symref(struct got_reference *ref, char *refname)
815 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
816 return got_error(GOT_ERR_BAD_REF_TYPE);
818 new_name = strdup(refname);
819 if (new_name == NULL)
820 return got_error_prefix_errno("strdup");
822 free(ref->ref.symref.name);
823 ref->ref.symref.name = new_name;
827 const struct got_error *
828 got_ref_write(struct got_reference *ref, struct got_repository *repo)
830 const struct got_error *err = NULL, *unlock_err = NULL;
831 const char *name = got_ref_get_name(ref);
832 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
833 struct got_lockfile *lf = NULL;
838 path_refs = get_refs_dir_path(repo, name);
839 if (path_refs == NULL) {
840 err = got_error_prefix_errno2("get_refs_dir_path", name);
844 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
845 err = got_error_prefix_errno("asprintf");
849 err = got_opentemp_named(&tmppath, &f, path);
852 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
854 err = got_path_dirname(&parent, path);
857 err = got_path_mkdir(parent);
861 err = got_opentemp_named(&tmppath, &f, path);
866 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
867 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
868 if (n != strlen(ref->ref.symref.ref) + 6) {
869 err = got_ferror(f, GOT_ERR_IO);
873 char hex[SHA1_DIGEST_STRING_LENGTH];
874 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
875 sizeof(hex)) == NULL) {
876 err = got_error(GOT_ERR_BAD_REF_DATA);
879 n = fprintf(f, "%s\n", hex);
880 if (n != sizeof(hex)) {
881 err = got_ferror(f, GOT_ERR_IO);
886 if (ref->lf == NULL) {
887 err = got_lockfile_lock(&lf, path);
892 /* XXX: check if old content matches our expectations? */
894 if (stat(path, &sb) != 0) {
895 if (errno != ENOENT) {
896 err = got_error_prefix_errno2("stat", path);
899 sb.st_mode = GOT_DEFAULT_FILE_MODE;
902 if (rename(tmppath, path) != 0) {
903 err = got_error_prefix_errno3("rename", tmppath, path);
909 if (chmod(path, sb.st_mode) != 0) {
910 err = got_error_prefix_errno2("chmod", path);
914 if (ref->lf == NULL && lf)
915 unlock_err = got_lockfile_unlock(lf);
917 if (fclose(f) != 0 && err == NULL)
918 err = got_error_prefix_errno("fclose");
923 if (unlink(tmppath) != 0 && err == NULL)
924 err = got_error_prefix_errno2("unlink", tmppath);
927 return err ? err : unlock_err;
930 static const struct got_error *
931 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
933 const struct got_error *err = NULL, *unlock_err = NULL;
934 struct got_lockfile *lf = NULL;
935 FILE *f = NULL, *tmpf = NULL;
936 char *packed_refs_path, *tmppath = NULL;
937 struct got_reflist_head refs;
938 int found_delref = 0;
940 /* The packed-refs file does not cotain symbolic references. */
941 if (delref->flags & GOT_REF_IS_SYMBOLIC)
942 return got_error(GOT_ERR_BAD_REF_DATA);
946 packed_refs_path = got_repo_get_path_packed_refs(repo);
947 if (packed_refs_path == NULL)
948 return got_error_prefix_errno("got_repo_get_path_packed_refs");
950 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
954 if (delref->lf == NULL) {
955 err = got_lockfile_lock(&lf, packed_refs_path);
960 f = fopen(packed_refs_path, "r");
962 err = got_error_prefix_errno2("fopen", packed_refs_path);
968 const char delim[3] = {'\0', '\0', '\0'};
969 struct got_reference *ref;
970 struct got_reflist_entry *new;
972 line = fparseln(f, &len, NULL, delim, 0);
976 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
979 err = parse_packed_ref_line(&ref, NULL, line);
986 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
987 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
988 sizeof(delref->ref.ref.sha1)) == 0) {
994 err = insert_ref(&new, &refs, ref, repo);
995 if (err || new == NULL /* duplicate */)
1002 struct got_reflist_entry *re;
1006 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1007 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1008 err = got_ferror(f, GOT_ERR_IO);
1012 SIMPLEQ_FOREACH(re, &refs, entry) {
1013 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1015 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1016 sizeof(hex)) == NULL) {
1017 err = got_error(GOT_ERR_BAD_REF_DATA);
1020 n = fprintf(tmpf, "%s ", hex);
1021 if (n != sizeof(hex)) {
1022 err = got_ferror(f, GOT_ERR_IO);
1025 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1026 if (n != strlen(re->ref->ref.ref.name) + 1) {
1027 err = got_ferror(f, GOT_ERR_IO);
1032 if (fflush(tmpf) != 0) {
1033 err = got_error_prefix_errno("fflush");
1037 if (stat(packed_refs_path, &sb) != 0) {
1038 if (errno != ENOENT) {
1039 err = got_error_prefix_errno2("stat",
1043 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1046 if (rename(tmppath, packed_refs_path) != 0) {
1047 err = got_error_prefix_errno3("rename", tmppath,
1052 if (chmod(packed_refs_path, sb.st_mode) != 0) {
1053 err = got_error_prefix_errno2("chmod",
1059 if (delref->lf == NULL && lf)
1060 unlock_err = got_lockfile_unlock(lf);
1062 if (fclose(f) != 0 && err == NULL)
1063 err = got_error_prefix_errno("fclose");
1067 if (fclose(tmpf) != 0 && err == NULL)
1068 err = got_error_prefix_errno("fclose");
1071 free(packed_refs_path);
1072 got_ref_list_free(&refs);
1073 return err ? err : unlock_err;
1076 const struct got_error *
1077 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1079 const struct got_error *err = NULL, *unlock_err = NULL;
1080 const char *name = got_ref_get_name(ref);
1081 char *path_refs = NULL, *path = NULL;
1082 struct got_lockfile *lf = NULL;
1084 if (ref->flags & GOT_REF_IS_PACKED)
1085 return delete_packed_ref(ref, repo);
1087 path_refs = get_refs_dir_path(repo, name);
1088 if (path_refs == NULL) {
1089 err = got_error_prefix_errno2("get_refs_dir_path", name);
1093 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1094 err = got_error_prefix_errno("asprintf");
1098 if (ref->lf == NULL) {
1099 err = got_lockfile_lock(&lf, path);
1104 /* XXX: check if old content matches our expectations? */
1106 if (unlink(path) != 0)
1107 err = got_error_prefix_errno2("unlink", path);
1109 if (ref->lf == NULL && lf)
1110 unlock_err = got_lockfile_unlock(lf);
1114 return err ? err : unlock_err;
1117 const struct got_error *
1118 got_ref_unlock(struct got_reference *ref)
1120 const struct got_error *err;
1121 err = got_lockfile_unlock(ref->lf);