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_from_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_from_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_from_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_from_errno("strdup");
125 (*ref)->ref.symref.ref = strdup(target_ref);
126 if ((*ref)->ref.symref.ref == NULL) {
127 err = got_error_from_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");
179 got_lockfile_unlock(lf);
183 line = fparseln(f, &len, NULL, delim, 0);
185 err = got_error(GOT_ERR_BAD_REF_DATA);
187 got_lockfile_unlock(lf);
191 err = parse_ref_line(ref, name, line);
194 got_lockfile_unlock(lf);
199 got_lockfile_unlock(lf);
204 if (fclose(f) != 0 && err == NULL) {
205 err = got_error_from_errno("fclose");
208 got_ref_unlock(*ref);
217 is_well_known_ref(const char *refname)
219 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
220 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
221 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
222 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
226 get_refs_dir_path(struct got_repository *repo, const char *refname)
228 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
229 return strdup(got_repo_get_path_git_dir(repo));
231 return got_repo_get_path_refs(repo);
235 is_valid_ref_name(const char *name)
237 const char *s, *slash, *seg;
238 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
239 const char *forbidden_seq[] = { "//", "..", "@{" };
240 const char *lfs = GOT_LOCKFILE_SUFFIX;
241 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
244 if (name[0] == '@' && name[1] == '\0')
247 slash = strchr(name, '/');
253 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
256 for (i = 0; i < nitems(forbidden); i++) {
257 if (*s == forbidden[i])
260 for (i = 0; i < nitems(forbidden_seq); i++) {
261 if (s[0] == forbidden_seq[i][0] &&
262 s[1] == forbidden_seq[i][1])
265 if (iscntrl((unsigned char)s[0]))
267 if (s[0] == '.' && s[1] == '\0')
270 const char *nextseg = s + 1;
271 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
274 if (seg <= s - lfs_len &&
275 strncmp(s - lfs_len, lfs, lfs_len) == 0)
282 if (seg <= s - lfs_len &&
283 strncmp(s - lfs_len, lfs, lfs_len) == 0)
289 const struct got_error *
290 got_ref_alloc(struct got_reference **ref, const char *name,
291 struct got_object_id *id)
293 if (!is_valid_ref_name(name))
294 return got_error(GOT_ERR_BAD_REF_NAME);
296 return alloc_ref(ref, name, id, 0);
299 static const struct got_error *
300 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
303 struct got_object_id id;
308 if (line[0] == '#' || line[0] == '^')
311 if (!got_parse_sha1_digest(id.sha1, line))
312 return got_error(GOT_ERR_BAD_REF_DATA);
315 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
319 name = line + SHA1_DIGEST_STRING_LENGTH;
321 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
324 static const struct got_error *
325 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
326 int nsubdirs, const char *refname)
328 const struct got_error *err = NULL;
332 const char delim[3] = {'\0', '\0', '\0'};
333 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
338 abs_refname = (char *)refname;
340 line = fparseln(f, &len, NULL, delim, 0);
344 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
347 for (i = 0; i < nsubdirs; i++) {
348 if (!ref_is_absolute &&
349 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
351 return got_error_from_errno("asprintf");
352 err = parse_packed_ref_line(ref, abs_refname, line);
353 if (!ref_is_absolute)
355 if (err || *ref != NULL)
361 } while (*ref == NULL);
366 static const struct got_error *
367 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
368 const char *name, int lock)
370 const struct got_error *err = NULL;
372 char *normpath = NULL;
373 char *absname = NULL;
374 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
375 int ref_is_well_known = is_well_known_ref(name);
379 if (ref_is_absolute || ref_is_well_known) {
380 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
381 return got_error_from_errno("asprintf");
382 absname = (char *)name;
384 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
385 subdir[0] ? "/" : "", name) == -1)
386 return got_error_from_errno("asprintf");
388 if (asprintf(&absname, "refs/%s%s%s",
389 subdir, subdir[0] ? "/" : "", name) == -1) {
390 err = got_error_from_errno("asprintf");
395 normpath = got_path_normalize(path);
396 if (normpath == NULL) {
397 err = got_error_from_errno2("got_path_normalize", path);
401 err = parse_ref_file(ref, absname, normpath, lock);
403 if (!ref_is_absolute && !ref_is_well_known)
410 const struct got_error *
411 got_ref_open(struct got_reference **ref, struct got_repository *repo,
412 const char *refname, int lock)
414 const struct got_error *err = NULL;
415 char *path_refs = NULL;
416 const char *subdirs[] = {
417 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
419 int i, well_known = is_well_known_ref(refname);
420 struct got_lockfile *lf = NULL;
424 path_refs = get_refs_dir_path(repo, refname);
425 if (path_refs == NULL) {
426 err = got_error_from_errno2("get_refs_dir_path", refname);
431 err = open_ref(ref, path_refs, "", refname, lock);
433 char *packed_refs_path;
436 /* Search on-disk refs before packed refs! */
437 for (i = 0; i < nitems(subdirs); i++) {
438 err = open_ref(ref, path_refs, subdirs[i], refname,
444 packed_refs_path = got_repo_get_path_packed_refs(repo);
445 if (packed_refs_path == NULL) {
446 err = got_error_from_errno(
447 "got_repo_get_path_packed_refs");
452 err = got_lockfile_lock(&lf, packed_refs_path);
456 f = fopen(packed_refs_path, "rb");
457 free(packed_refs_path);
459 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
462 if (fclose(f) != 0) {
463 err = got_error_from_errno("fclose");
472 if (!err && *ref == NULL)
473 err = got_error_not_ref(refname);
475 got_lockfile_unlock(lf);
481 got_ref_close(struct got_reference *ref)
483 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
484 free(ref->ref.symref.name);
485 free(ref->ref.symref.ref);
487 free(ref->ref.ref.name);
491 struct got_reference *
492 got_ref_dup(struct got_reference *ref)
494 struct got_reference *ret;
496 ret = calloc(1, sizeof(*ret));
500 ret->flags = ref->flags;
501 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
502 ret->ref.symref.name = strdup(ref->ref.symref.name);
503 if (ret->ref.symref.name == NULL) {
507 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
508 if (ret->ref.symref.ref == NULL) {
509 free(ret->ref.symref.name);
514 ref->ref.ref.name = strdup(ref->ref.ref.name);
515 if (ref->ref.ref.name == NULL) {
519 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
520 sizeof(ret->ref.ref.sha1));
526 static const struct got_error *
527 resolve_symbolic_ref(struct got_reference **resolved,
528 struct got_repository *repo, struct got_reference *ref)
530 struct got_reference *nextref;
531 const struct got_error *err;
533 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
537 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
538 err = resolve_symbolic_ref(resolved, repo, nextref);
540 *resolved = got_ref_dup(nextref);
542 got_ref_close(nextref);
546 const struct got_error *
547 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
548 struct got_reference *ref)
550 const struct got_error *err;
552 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
553 struct got_reference *resolved = NULL;
554 err = resolve_symbolic_ref(&resolved, repo, ref);
556 err = got_ref_resolve(id, repo, resolved);
558 got_ref_close(resolved);
562 *id = calloc(1, sizeof(**id));
564 return got_error_from_errno("calloc");
565 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
570 got_ref_to_str(struct got_reference *ref)
574 if (ref->flags & GOT_REF_IS_SYMBOLIC)
575 return strdup(ref->ref.symref.ref);
577 str = malloc(SHA1_DIGEST_STRING_LENGTH);
581 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
582 SHA1_DIGEST_STRING_LENGTH) == NULL) {
591 got_ref_get_name(struct got_reference *ref)
593 if (ref->flags & GOT_REF_IS_SYMBOLIC)
594 return ref->ref.symref.name;
596 return ref->ref.ref.name;
599 static const struct got_error *
600 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
601 struct got_reference *ref, struct got_repository *repo)
603 const struct got_error *err;
604 struct got_object_id *id;
605 struct got_reflist_entry *new, *re, *prev = NULL;
610 err = got_ref_resolve(&id, repo, ref);
614 new = malloc(sizeof(*new));
617 return got_error_from_errno("malloc");
624 * We must de-duplicate entries on insert because packed-refs may
625 * contain redundant entries. On-disk refs take precedence.
626 * This code assumes that on-disk revs are read before packed-refs.
627 * We're iterating the list anyway, so insert elements sorted by name.
629 re = SIMPLEQ_FIRST(refs);
631 cmp = got_path_cmp(got_ref_get_name(re->ref),
632 got_ref_get_name(new->ref));
639 } else if (cmp > 0) {
641 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
643 SIMPLEQ_INSERT_HEAD(refs, new, entry);
647 re = SIMPLEQ_NEXT(re, entry);
651 SIMPLEQ_INSERT_TAIL(refs, new, entry);
655 static const struct got_error *
656 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
657 const char *subdir, struct got_repository *repo)
659 const struct got_error *err = NULL;
663 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
664 return got_error_from_errno("asprintf");
666 d = opendir(path_subdir);
672 struct got_reference *ref;
679 if (strcmp(dent->d_name, ".") == 0 ||
680 strcmp(dent->d_name, "..") == 0)
683 switch (dent->d_type) {
685 err = open_ref(&ref, path_refs, subdir, dent->d_name,
690 struct got_reflist_entry *new;
691 err = insert_ref(&new, refs, ref, repo);
692 if (err || new == NULL /* duplicate */)
699 if (asprintf(&child, "%s%s%s", subdir,
700 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
701 err = got_error_from_errno("asprintf");
704 err = gather_on_disk_refs(refs, path_refs, child, repo);
718 const struct got_error *
719 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
721 const struct got_error *err;
722 char *packed_refs_path, *path_refs = NULL;
724 struct got_reference *ref;
725 struct got_reflist_entry *new;
727 /* HEAD ref should always exist. */
728 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
729 if (path_refs == NULL) {
730 err = got_error_from_errno("get_refs_dir_path");
733 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
736 err = insert_ref(&new, refs, ref, repo);
737 if (err || new == NULL /* duplicate */)
742 /* Gather on-disk refs before parsing packed-refs. */
744 path_refs = get_refs_dir_path(repo, "");
745 if (path_refs == NULL) {
746 err = got_error_from_errno("get_refs_dir_path");
749 err = gather_on_disk_refs(refs, path_refs, "", repo);
754 * The packed-refs file may contain redundant entries, in which
755 * case on-disk refs take precedence.
757 packed_refs_path = got_repo_get_path_packed_refs(repo);
758 if (packed_refs_path == NULL) {
759 err = got_error_from_errno("got_repo_get_path_packed_refs");
763 f = fopen(packed_refs_path, "r");
764 free(packed_refs_path);
768 const char delim[3] = {'\0', '\0', '\0'};
770 line = fparseln(f, &len, NULL, delim, 0);
774 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
777 err = parse_packed_ref_line(&ref, NULL, line);
782 err = insert_ref(&new, refs, ref, repo);
783 if (err || new == NULL /* duplicate */)
792 if (f && fclose(f) != 0 && err == NULL)
793 err = got_error_from_errno("fclose");
798 got_ref_list_free(struct got_reflist_head *refs)
800 struct got_reflist_entry *re;
802 while (!SIMPLEQ_EMPTY(refs)) {
803 re = SIMPLEQ_FIRST(refs);
804 SIMPLEQ_REMOVE_HEAD(refs, entry);
805 got_ref_close(re->ref);
813 got_ref_is_symbolic(struct got_reference *ref)
815 return (ref->flags & GOT_REF_IS_SYMBOLIC);
818 const struct got_error *
819 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
821 if (ref->flags & GOT_REF_IS_SYMBOLIC)
822 return got_error(GOT_ERR_BAD_REF_TYPE);
824 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
828 const struct got_error *
829 got_ref_change_symref(struct got_reference *ref, char *refname)
833 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
834 return got_error(GOT_ERR_BAD_REF_TYPE);
836 new_name = strdup(refname);
837 if (new_name == NULL)
838 return got_error_from_errno("strdup");
840 free(ref->ref.symref.name);
841 ref->ref.symref.name = new_name;
845 const struct got_error *
846 got_ref_write(struct got_reference *ref, struct got_repository *repo)
848 const struct got_error *err = NULL, *unlock_err = NULL;
849 const char *name = got_ref_get_name(ref);
850 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
851 struct got_lockfile *lf = NULL;
856 path_refs = get_refs_dir_path(repo, name);
857 if (path_refs == NULL) {
858 err = got_error_from_errno2("get_refs_dir_path", name);
862 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
863 err = got_error_from_errno("asprintf");
867 err = got_opentemp_named(&tmppath, &f, path);
870 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
872 err = got_path_dirname(&parent, path);
875 err = got_path_mkdir(parent);
879 err = got_opentemp_named(&tmppath, &f, path);
884 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
885 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
886 if (n != strlen(ref->ref.symref.ref) + 6) {
887 err = got_ferror(f, GOT_ERR_IO);
891 char hex[SHA1_DIGEST_STRING_LENGTH];
892 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
893 sizeof(hex)) == NULL) {
894 err = got_error(GOT_ERR_BAD_REF_DATA);
897 n = fprintf(f, "%s\n", hex);
898 if (n != sizeof(hex)) {
899 err = got_ferror(f, GOT_ERR_IO);
904 if (ref->lf == NULL) {
905 err = got_lockfile_lock(&lf, path);
910 /* XXX: check if old content matches our expectations? */
912 if (stat(path, &sb) != 0) {
913 if (errno != ENOENT) {
914 err = got_error_from_errno2("stat", path);
917 sb.st_mode = GOT_DEFAULT_FILE_MODE;
920 if (rename(tmppath, path) != 0) {
921 err = got_error_from_errno3("rename", tmppath, path);
927 if (chmod(path, sb.st_mode) != 0) {
928 err = got_error_from_errno2("chmod", path);
932 if (ref->lf == NULL && lf)
933 unlock_err = got_lockfile_unlock(lf);
935 if (fclose(f) != 0 && err == NULL)
936 err = got_error_from_errno("fclose");
941 if (unlink(tmppath) != 0 && err == NULL)
942 err = got_error_from_errno2("unlink", tmppath);
945 return err ? err : unlock_err;
948 static const struct got_error *
949 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
951 const struct got_error *err = NULL, *unlock_err = NULL;
952 struct got_lockfile *lf = NULL;
953 FILE *f = NULL, *tmpf = NULL;
954 char *packed_refs_path, *tmppath = NULL;
955 struct got_reflist_head refs;
956 int found_delref = 0;
958 /* The packed-refs file does not cotain symbolic references. */
959 if (delref->flags & GOT_REF_IS_SYMBOLIC)
960 return got_error(GOT_ERR_BAD_REF_DATA);
964 packed_refs_path = got_repo_get_path_packed_refs(repo);
965 if (packed_refs_path == NULL)
966 return got_error_from_errno("got_repo_get_path_packed_refs");
968 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
972 if (delref->lf == NULL) {
973 err = got_lockfile_lock(&lf, packed_refs_path);
978 f = fopen(packed_refs_path, "r");
980 err = got_error_from_errno2("fopen", packed_refs_path);
986 const char delim[3] = {'\0', '\0', '\0'};
987 struct got_reference *ref;
988 struct got_reflist_entry *new;
990 line = fparseln(f, &len, NULL, delim, 0);
994 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
997 err = parse_packed_ref_line(&ref, NULL, line);
1004 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1005 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1006 sizeof(delref->ref.ref.sha1)) == 0) {
1012 err = insert_ref(&new, &refs, ref, repo);
1013 if (err || new == NULL /* duplicate */)
1020 struct got_reflist_entry *re;
1024 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1025 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1026 err = got_ferror(f, GOT_ERR_IO);
1030 SIMPLEQ_FOREACH(re, &refs, entry) {
1031 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1033 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1034 sizeof(hex)) == NULL) {
1035 err = got_error(GOT_ERR_BAD_REF_DATA);
1038 n = fprintf(tmpf, "%s ", hex);
1039 if (n != sizeof(hex)) {
1040 err = got_ferror(f, GOT_ERR_IO);
1043 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1044 if (n != strlen(re->ref->ref.ref.name) + 1) {
1045 err = got_ferror(f, GOT_ERR_IO);
1050 if (fflush(tmpf) != 0) {
1051 err = got_error_from_errno("fflush");
1055 if (stat(packed_refs_path, &sb) != 0) {
1056 if (errno != ENOENT) {
1057 err = got_error_from_errno2("stat",
1061 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1064 if (rename(tmppath, packed_refs_path) != 0) {
1065 err = got_error_from_errno3("rename", tmppath,
1070 if (chmod(packed_refs_path, sb.st_mode) != 0) {
1071 err = got_error_from_errno2("chmod",
1077 if (delref->lf == NULL && lf)
1078 unlock_err = got_lockfile_unlock(lf);
1080 if (fclose(f) != 0 && err == NULL)
1081 err = got_error_from_errno("fclose");
1085 if (fclose(tmpf) != 0 && err == NULL)
1086 err = got_error_from_errno("fclose");
1089 free(packed_refs_path);
1090 got_ref_list_free(&refs);
1091 return err ? err : unlock_err;
1094 const struct got_error *
1095 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1097 const struct got_error *err = NULL, *unlock_err = NULL;
1098 const char *name = got_ref_get_name(ref);
1099 char *path_refs = NULL, *path = NULL;
1100 struct got_lockfile *lf = NULL;
1102 if (ref->flags & GOT_REF_IS_PACKED)
1103 return delete_packed_ref(ref, repo);
1105 path_refs = get_refs_dir_path(repo, name);
1106 if (path_refs == NULL) {
1107 err = got_error_from_errno2("get_refs_dir_path", name);
1111 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1112 err = got_error_from_errno("asprintf");
1116 if (ref->lf == NULL) {
1117 err = got_lockfile_lock(&lf, path);
1122 /* XXX: check if old content matches our expectations? */
1124 if (unlink(path) != 0)
1125 err = got_error_from_errno2("unlink", path);
1127 if (ref->lf == NULL && lf)
1128 unlock_err = got_lockfile_unlock(lf);
1132 return err ? err : unlock_err;
1135 const struct got_error *
1136 got_ref_unlock(struct got_reference *ref)
1138 const struct got_error *err;
1139 err = got_lockfile_unlock(ref->lf);