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 const struct got_error *err;
294 char *absname = NULL;
296 if (!is_valid_ref_name(name)) {
297 if (strchr(name, '/') != NULL)
298 return got_error(GOT_ERR_BAD_REF_NAME);
299 if (asprintf(&absname, "refs/heads/%s", name) == -1)
300 return got_error_from_errno("asprintf");
303 err = alloc_ref(ref, absname ? absname : name, id, 0);
308 static const struct got_error *
309 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
312 struct got_object_id id;
317 if (line[0] == '#' || line[0] == '^')
320 if (!got_parse_sha1_digest(id.sha1, line))
321 return got_error(GOT_ERR_BAD_REF_DATA);
324 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
328 name = line + SHA1_DIGEST_STRING_LENGTH;
330 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
333 static const struct got_error *
334 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
335 int nsubdirs, const char *refname)
337 const struct got_error *err = NULL;
341 const char delim[3] = {'\0', '\0', '\0'};
342 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
347 abs_refname = (char *)refname;
349 line = fparseln(f, &len, NULL, delim, 0);
353 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
356 for (i = 0; i < nsubdirs; i++) {
357 if (!ref_is_absolute &&
358 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
360 return got_error_from_errno("asprintf");
361 err = parse_packed_ref_line(ref, abs_refname, line);
362 if (!ref_is_absolute)
364 if (err || *ref != NULL)
370 } while (*ref == NULL);
375 static const struct got_error *
376 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
377 const char *name, int lock)
379 const struct got_error *err = NULL;
381 char *normpath = NULL;
382 char *absname = NULL;
383 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
384 int ref_is_well_known = is_well_known_ref(name);
388 if (ref_is_absolute || ref_is_well_known) {
389 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
390 return got_error_from_errno("asprintf");
391 absname = (char *)name;
393 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
394 subdir[0] ? "/" : "", name) == -1)
395 return got_error_from_errno("asprintf");
397 if (asprintf(&absname, "refs/%s%s%s",
398 subdir, subdir[0] ? "/" : "", name) == -1) {
399 err = got_error_from_errno("asprintf");
404 normpath = got_path_normalize(path);
405 if (normpath == NULL) {
409 err = got_error_from_errno2("got_path_normalize", path);
413 err = parse_ref_file(ref, absname, normpath, lock);
415 if (!ref_is_absolute && !ref_is_well_known)
422 const struct got_error *
423 got_ref_open(struct got_reference **ref, struct got_repository *repo,
424 const char *refname, int lock)
426 const struct got_error *err = NULL;
427 char *path_refs = NULL;
428 const char *subdirs[] = {
429 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
431 int i, well_known = is_well_known_ref(refname);
432 struct got_lockfile *lf = NULL;
436 path_refs = get_refs_dir_path(repo, refname);
437 if (path_refs == NULL) {
438 err = got_error_from_errno2("get_refs_dir_path", refname);
443 err = open_ref(ref, path_refs, "", refname, lock);
445 char *packed_refs_path;
448 /* Search on-disk refs before packed refs! */
449 for (i = 0; i < nitems(subdirs); i++) {
450 err = open_ref(ref, path_refs, subdirs[i], refname,
456 packed_refs_path = got_repo_get_path_packed_refs(repo);
457 if (packed_refs_path == NULL) {
458 err = got_error_from_errno(
459 "got_repo_get_path_packed_refs");
464 err = got_lockfile_lock(&lf, packed_refs_path);
468 f = fopen(packed_refs_path, "rb");
469 free(packed_refs_path);
471 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
474 if (fclose(f) != 0) {
475 err = got_error_from_errno("fclose");
484 if (!err && *ref == NULL)
485 err = got_error_not_ref(refname);
487 got_lockfile_unlock(lf);
493 got_ref_close(struct got_reference *ref)
495 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
496 free(ref->ref.symref.name);
497 free(ref->ref.symref.ref);
499 free(ref->ref.ref.name);
503 struct got_reference *
504 got_ref_dup(struct got_reference *ref)
506 struct got_reference *ret;
508 ret = calloc(1, sizeof(*ret));
512 ret->flags = ref->flags;
513 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
514 ret->ref.symref.name = strdup(ref->ref.symref.name);
515 if (ret->ref.symref.name == NULL) {
519 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
520 if (ret->ref.symref.ref == NULL) {
521 free(ret->ref.symref.name);
526 ref->ref.ref.name = strdup(ref->ref.ref.name);
527 if (ref->ref.ref.name == NULL) {
531 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
532 sizeof(ret->ref.ref.sha1));
538 static const struct got_error *
539 resolve_symbolic_ref(struct got_reference **resolved,
540 struct got_repository *repo, struct got_reference *ref)
542 struct got_reference *nextref;
543 const struct got_error *err;
545 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
549 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
550 err = resolve_symbolic_ref(resolved, repo, nextref);
552 *resolved = got_ref_dup(nextref);
554 got_ref_close(nextref);
558 const struct got_error *
559 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
560 struct got_reference *ref)
562 const struct got_error *err;
564 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
565 struct got_reference *resolved = NULL;
566 err = resolve_symbolic_ref(&resolved, repo, ref);
568 err = got_ref_resolve(id, repo, resolved);
570 got_ref_close(resolved);
574 *id = calloc(1, sizeof(**id));
576 return got_error_from_errno("calloc");
577 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
582 got_ref_to_str(struct got_reference *ref)
586 if (ref->flags & GOT_REF_IS_SYMBOLIC)
587 return strdup(ref->ref.symref.ref);
589 str = malloc(SHA1_DIGEST_STRING_LENGTH);
593 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
594 SHA1_DIGEST_STRING_LENGTH) == NULL) {
603 got_ref_get_name(struct got_reference *ref)
605 if (ref->flags & GOT_REF_IS_SYMBOLIC)
606 return ref->ref.symref.name;
608 return ref->ref.ref.name;
611 static const struct got_error *
612 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
613 struct got_reference *ref, struct got_repository *repo)
615 const struct got_error *err;
616 struct got_object_id *id;
617 struct got_reflist_entry *new, *re, *prev = NULL;
622 err = got_ref_resolve(&id, repo, ref);
626 new = malloc(sizeof(*new));
629 return got_error_from_errno("malloc");
636 * We must de-duplicate entries on insert because packed-refs may
637 * contain redundant entries. On-disk refs take precedence.
638 * This code assumes that on-disk revs are read before packed-refs.
639 * We're iterating the list anyway, so insert elements sorted by name.
641 re = SIMPLEQ_FIRST(refs);
643 cmp = got_path_cmp(got_ref_get_name(re->ref),
644 got_ref_get_name(new->ref));
651 } else if (cmp > 0) {
653 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
655 SIMPLEQ_INSERT_HEAD(refs, new, entry);
659 re = SIMPLEQ_NEXT(re, entry);
663 SIMPLEQ_INSERT_TAIL(refs, new, entry);
667 static const struct got_error *
668 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
669 const char *subdir, struct got_repository *repo)
671 const struct got_error *err = NULL;
675 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
676 return got_error_from_errno("asprintf");
678 d = opendir(path_subdir);
684 struct got_reference *ref;
691 if (strcmp(dent->d_name, ".") == 0 ||
692 strcmp(dent->d_name, "..") == 0)
695 switch (dent->d_type) {
697 err = open_ref(&ref, path_refs, subdir, dent->d_name,
702 struct got_reflist_entry *new;
703 err = insert_ref(&new, refs, ref, repo);
704 if (err || new == NULL /* duplicate */)
711 if (asprintf(&child, "%s%s%s", subdir,
712 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
713 err = got_error_from_errno("asprintf");
716 err = gather_on_disk_refs(refs, path_refs, child, repo);
730 const struct got_error *
731 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
733 const struct got_error *err;
734 char *packed_refs_path, *path_refs = NULL;
736 struct got_reference *ref;
737 struct got_reflist_entry *new;
739 /* HEAD ref should always exist. */
740 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
741 if (path_refs == NULL) {
742 err = got_error_from_errno("get_refs_dir_path");
745 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
748 err = insert_ref(&new, refs, ref, repo);
749 if (err || new == NULL /* duplicate */)
754 /* Gather on-disk refs before parsing packed-refs. */
756 path_refs = get_refs_dir_path(repo, "");
757 if (path_refs == NULL) {
758 err = got_error_from_errno("get_refs_dir_path");
761 err = gather_on_disk_refs(refs, path_refs, "", repo);
766 * The packed-refs file may contain redundant entries, in which
767 * case on-disk refs take precedence.
769 packed_refs_path = got_repo_get_path_packed_refs(repo);
770 if (packed_refs_path == NULL) {
771 err = got_error_from_errno("got_repo_get_path_packed_refs");
775 f = fopen(packed_refs_path, "r");
776 free(packed_refs_path);
780 const char delim[3] = {'\0', '\0', '\0'};
782 line = fparseln(f, &len, NULL, delim, 0);
786 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
789 err = parse_packed_ref_line(&ref, NULL, line);
794 err = insert_ref(&new, refs, ref, repo);
795 if (err || new == NULL /* duplicate */)
804 if (f && fclose(f) != 0 && err == NULL)
805 err = got_error_from_errno("fclose");
810 got_ref_list_free(struct got_reflist_head *refs)
812 struct got_reflist_entry *re;
814 while (!SIMPLEQ_EMPTY(refs)) {
815 re = SIMPLEQ_FIRST(refs);
816 SIMPLEQ_REMOVE_HEAD(refs, entry);
817 got_ref_close(re->ref);
825 got_ref_is_symbolic(struct got_reference *ref)
827 return (ref->flags & GOT_REF_IS_SYMBOLIC);
830 const struct got_error *
831 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
833 if (ref->flags & GOT_REF_IS_SYMBOLIC)
834 return got_error(GOT_ERR_BAD_REF_TYPE);
836 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
840 const struct got_error *
841 got_ref_change_symref(struct got_reference *ref, char *refname)
845 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
846 return got_error(GOT_ERR_BAD_REF_TYPE);
848 new_name = strdup(refname);
849 if (new_name == NULL)
850 return got_error_from_errno("strdup");
852 free(ref->ref.symref.name);
853 ref->ref.symref.name = new_name;
857 const struct got_error *
858 got_ref_write(struct got_reference *ref, struct got_repository *repo)
860 const struct got_error *err = NULL, *unlock_err = NULL;
861 const char *name = got_ref_get_name(ref);
862 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
863 struct got_lockfile *lf = NULL;
868 path_refs = get_refs_dir_path(repo, name);
869 if (path_refs == NULL) {
870 err = got_error_from_errno2("get_refs_dir_path", name);
874 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
875 err = got_error_from_errno("asprintf");
879 err = got_opentemp_named(&tmppath, &f, path);
882 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
884 err = got_path_dirname(&parent, path);
887 err = got_path_mkdir(parent);
891 err = got_opentemp_named(&tmppath, &f, path);
896 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
897 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
898 if (n != strlen(ref->ref.symref.ref) + 6) {
899 err = got_ferror(f, GOT_ERR_IO);
903 char hex[SHA1_DIGEST_STRING_LENGTH];
904 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
905 sizeof(hex)) == NULL) {
906 err = got_error(GOT_ERR_BAD_REF_DATA);
909 n = fprintf(f, "%s\n", hex);
910 if (n != sizeof(hex)) {
911 err = got_ferror(f, GOT_ERR_IO);
916 if (ref->lf == NULL) {
917 err = got_lockfile_lock(&lf, path);
922 /* XXX: check if old content matches our expectations? */
924 if (stat(path, &sb) != 0) {
925 if (errno != ENOENT) {
926 err = got_error_from_errno2("stat", path);
929 sb.st_mode = GOT_DEFAULT_FILE_MODE;
932 if (rename(tmppath, path) != 0) {
933 err = got_error_from_errno3("rename", tmppath, path);
939 if (chmod(path, sb.st_mode) != 0) {
940 err = got_error_from_errno2("chmod", path);
944 if (ref->lf == NULL && lf)
945 unlock_err = got_lockfile_unlock(lf);
947 if (fclose(f) != 0 && err == NULL)
948 err = got_error_from_errno("fclose");
953 if (unlink(tmppath) != 0 && err == NULL)
954 err = got_error_from_errno2("unlink", tmppath);
957 return err ? err : unlock_err;
960 static const struct got_error *
961 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
963 const struct got_error *err = NULL, *unlock_err = NULL;
964 struct got_lockfile *lf = NULL;
965 FILE *f = NULL, *tmpf = NULL;
966 char *packed_refs_path, *tmppath = NULL;
967 struct got_reflist_head refs;
968 int found_delref = 0;
970 /* The packed-refs file does not cotain symbolic references. */
971 if (delref->flags & GOT_REF_IS_SYMBOLIC)
972 return got_error(GOT_ERR_BAD_REF_DATA);
976 packed_refs_path = got_repo_get_path_packed_refs(repo);
977 if (packed_refs_path == NULL)
978 return got_error_from_errno("got_repo_get_path_packed_refs");
980 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
984 if (delref->lf == NULL) {
985 err = got_lockfile_lock(&lf, packed_refs_path);
990 f = fopen(packed_refs_path, "r");
992 err = got_error_from_errno2("fopen", packed_refs_path);
998 const char delim[3] = {'\0', '\0', '\0'};
999 struct got_reference *ref;
1000 struct got_reflist_entry *new;
1002 line = fparseln(f, &len, NULL, delim, 0);
1006 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1009 err = parse_packed_ref_line(&ref, NULL, line);
1016 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1017 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1018 sizeof(delref->ref.ref.sha1)) == 0) {
1024 err = insert_ref(&new, &refs, ref, repo);
1025 if (err || new == NULL /* duplicate */)
1032 struct got_reflist_entry *re;
1036 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1037 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1038 err = got_ferror(f, GOT_ERR_IO);
1042 SIMPLEQ_FOREACH(re, &refs, entry) {
1043 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1045 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1046 sizeof(hex)) == NULL) {
1047 err = got_error(GOT_ERR_BAD_REF_DATA);
1050 n = fprintf(tmpf, "%s ", hex);
1051 if (n != sizeof(hex)) {
1052 err = got_ferror(f, GOT_ERR_IO);
1055 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1056 if (n != strlen(re->ref->ref.ref.name) + 1) {
1057 err = got_ferror(f, GOT_ERR_IO);
1062 if (fflush(tmpf) != 0) {
1063 err = got_error_from_errno("fflush");
1067 if (stat(packed_refs_path, &sb) != 0) {
1068 if (errno != ENOENT) {
1069 err = got_error_from_errno2("stat",
1073 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1076 if (rename(tmppath, packed_refs_path) != 0) {
1077 err = got_error_from_errno3("rename", tmppath,
1082 if (chmod(packed_refs_path, sb.st_mode) != 0) {
1083 err = got_error_from_errno2("chmod",
1089 if (delref->lf == NULL && lf)
1090 unlock_err = got_lockfile_unlock(lf);
1092 if (fclose(f) != 0 && err == NULL)
1093 err = got_error_from_errno("fclose");
1097 if (fclose(tmpf) != 0 && err == NULL)
1098 err = got_error_from_errno("fclose");
1101 free(packed_refs_path);
1102 got_ref_list_free(&refs);
1103 return err ? err : unlock_err;
1106 const struct got_error *
1107 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1109 const struct got_error *err = NULL, *unlock_err = NULL;
1110 const char *name = got_ref_get_name(ref);
1111 char *path_refs = NULL, *path = NULL;
1112 struct got_lockfile *lf = NULL;
1114 if (ref->flags & GOT_REF_IS_PACKED)
1115 return delete_packed_ref(ref, repo);
1117 path_refs = get_refs_dir_path(repo, name);
1118 if (path_refs == NULL) {
1119 err = got_error_from_errno2("get_refs_dir_path", name);
1123 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1124 err = got_error_from_errno("asprintf");
1128 if (ref->lf == NULL) {
1129 err = got_lockfile_lock(&lf, path);
1134 /* XXX: check if old content matches our expectations? */
1136 if (unlink(path) != 0)
1137 err = got_error_from_errno2("unlink", path);
1139 if (ref->lf == NULL && lf)
1140 unlock_err = got_lockfile_unlock(lf);
1144 return err ? err : unlock_err;
1147 const struct got_error *
1148 got_ref_unlock(struct got_reference *ref)
1150 const struct got_error *err;
1151 err = got_lockfile_unlock(ref->lf);