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 const struct got_error *
300 got_ref_alloc_symref(struct got_reference **ref, const char *name,
301 struct got_reference *target_ref)
303 if (!is_valid_ref_name(name))
304 return got_error(GOT_ERR_BAD_REF_NAME);
306 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
309 static const struct got_error *
310 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
313 struct got_object_id id;
318 if (line[0] == '#' || line[0] == '^')
321 if (!got_parse_sha1_digest(id.sha1, line))
322 return got_error(GOT_ERR_BAD_REF_DATA);
325 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
329 name = line + SHA1_DIGEST_STRING_LENGTH;
331 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
334 static const struct got_error *
335 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
336 int nsubdirs, const char *refname)
338 const struct got_error *err = NULL;
342 const char delim[3] = {'\0', '\0', '\0'};
343 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
348 abs_refname = (char *)refname;
350 line = fparseln(f, &len, NULL, delim, 0);
354 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
357 for (i = 0; i < nsubdirs; i++) {
358 if (!ref_is_absolute &&
359 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
361 return got_error_from_errno("asprintf");
362 err = parse_packed_ref_line(ref, abs_refname, line);
363 if (!ref_is_absolute)
365 if (err || *ref != NULL)
371 } while (*ref == NULL);
376 static const struct got_error *
377 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
378 const char *name, int lock)
380 const struct got_error *err = 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 err = parse_ref_file(ref, absname, path, lock);
406 if (!ref_is_absolute && !ref_is_well_known)
412 const struct got_error *
413 got_ref_open(struct got_reference **ref, struct got_repository *repo,
414 const char *refname, int lock)
416 const struct got_error *err = NULL;
417 char *path_refs = NULL;
418 const char *subdirs[] = {
419 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
421 int i, well_known = is_well_known_ref(refname);
422 struct got_lockfile *lf = NULL;
426 path_refs = get_refs_dir_path(repo, refname);
427 if (path_refs == NULL) {
428 err = got_error_from_errno2("get_refs_dir_path", refname);
433 err = open_ref(ref, path_refs, "", refname, lock);
435 char *packed_refs_path;
438 /* Search on-disk refs before packed refs! */
439 for (i = 0; i < nitems(subdirs); i++) {
440 err = open_ref(ref, path_refs, subdirs[i], refname,
446 packed_refs_path = got_repo_get_path_packed_refs(repo);
447 if (packed_refs_path == NULL) {
448 err = got_error_from_errno(
449 "got_repo_get_path_packed_refs");
454 err = got_lockfile_lock(&lf, packed_refs_path);
458 f = fopen(packed_refs_path, "rb");
459 free(packed_refs_path);
461 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
464 if (fclose(f) != 0) {
465 err = got_error_from_errno("fclose");
474 if (!err && *ref == NULL)
475 err = got_error_not_ref(refname);
477 got_lockfile_unlock(lf);
483 got_ref_close(struct got_reference *ref)
485 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
486 free(ref->ref.symref.name);
487 free(ref->ref.symref.ref);
489 free(ref->ref.ref.name);
493 struct got_reference *
494 got_ref_dup(struct got_reference *ref)
496 struct got_reference *ret;
498 ret = calloc(1, sizeof(*ret));
502 ret->flags = ref->flags;
503 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
504 ret->ref.symref.name = strdup(ref->ref.symref.name);
505 if (ret->ref.symref.name == NULL) {
509 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
510 if (ret->ref.symref.ref == NULL) {
511 free(ret->ref.symref.name);
516 ref->ref.ref.name = strdup(ref->ref.ref.name);
517 if (ref->ref.ref.name == NULL) {
521 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
522 sizeof(ret->ref.ref.sha1));
528 static const struct got_error *
529 resolve_symbolic_ref(struct got_reference **resolved,
530 struct got_repository *repo, struct got_reference *ref)
532 struct got_reference *nextref;
533 const struct got_error *err;
535 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
539 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
540 err = resolve_symbolic_ref(resolved, repo, nextref);
542 *resolved = got_ref_dup(nextref);
544 got_ref_close(nextref);
548 const struct got_error *
549 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
550 struct got_reference *ref)
552 const struct got_error *err;
554 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
555 struct got_reference *resolved = NULL;
556 err = resolve_symbolic_ref(&resolved, repo, ref);
558 err = got_ref_resolve(id, repo, resolved);
560 got_ref_close(resolved);
564 *id = calloc(1, sizeof(**id));
566 return got_error_from_errno("calloc");
567 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
572 got_ref_to_str(struct got_reference *ref)
576 if (ref->flags & GOT_REF_IS_SYMBOLIC)
577 return strdup(ref->ref.symref.ref);
579 str = malloc(SHA1_DIGEST_STRING_LENGTH);
583 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
584 SHA1_DIGEST_STRING_LENGTH) == NULL) {
593 got_ref_get_name(struct got_reference *ref)
595 if (ref->flags & GOT_REF_IS_SYMBOLIC)
596 return ref->ref.symref.name;
598 return ref->ref.ref.name;
602 got_ref_get_symref_target(struct got_reference *ref)
604 if (ref->flags & GOT_REF_IS_SYMBOLIC)
605 return ref->ref.symref.ref;
610 static const struct got_error *
611 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
612 struct got_reference *ref, struct got_repository *repo)
614 const struct got_error *err;
615 struct got_object_id *id;
616 struct got_reflist_entry *new, *re, *prev = NULL;
621 err = got_ref_resolve(&id, repo, ref);
625 new = malloc(sizeof(*new));
628 return got_error_from_errno("malloc");
635 * We must de-duplicate entries on insert because packed-refs may
636 * contain redundant entries. On-disk refs take precedence.
637 * This code assumes that on-disk revs are read before packed-refs.
638 * We're iterating the list anyway, so insert elements sorted by name.
640 re = SIMPLEQ_FIRST(refs);
642 const char *name = got_ref_get_name(re->ref);
643 const char *new_name = got_ref_get_name(new->ref);
644 cmp = got_path_cmp(name, new_name, strlen(name),
652 } else if (cmp > 0) {
654 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
656 SIMPLEQ_INSERT_HEAD(refs, new, entry);
660 re = SIMPLEQ_NEXT(re, entry);
664 SIMPLEQ_INSERT_TAIL(refs, new, entry);
668 static const struct got_error *
669 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
670 const char *subdir, struct got_repository *repo)
672 const struct got_error *err = NULL;
676 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
677 return got_error_from_errno("asprintf");
679 d = opendir(path_subdir);
685 struct got_reference *ref;
692 if (strcmp(dent->d_name, ".") == 0 ||
693 strcmp(dent->d_name, "..") == 0)
696 switch (dent->d_type) {
698 err = open_ref(&ref, path_refs, subdir, dent->d_name,
703 struct got_reflist_entry *new;
704 err = insert_ref(&new, refs, ref, repo);
705 if (err || new == NULL /* duplicate */)
712 if (asprintf(&child, "%s%s%s", subdir,
713 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
714 err = got_error_from_errno("asprintf");
717 err = gather_on_disk_refs(refs, path_refs, child, repo);
731 const struct got_error *
732 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
734 const struct got_error *err;
735 char *packed_refs_path, *path_refs = NULL;
737 struct got_reference *ref;
738 struct got_reflist_entry *new;
740 /* HEAD ref should always exist. */
741 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
742 if (path_refs == NULL) {
743 err = got_error_from_errno("get_refs_dir_path");
746 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
749 err = insert_ref(&new, refs, ref, repo);
750 if (err || new == NULL /* duplicate */)
755 /* Gather on-disk refs before parsing packed-refs. */
757 path_refs = get_refs_dir_path(repo, "");
758 if (path_refs == NULL) {
759 err = got_error_from_errno("get_refs_dir_path");
762 err = gather_on_disk_refs(refs, path_refs, "", repo);
767 * The packed-refs file may contain redundant entries, in which
768 * case on-disk refs take precedence.
770 packed_refs_path = got_repo_get_path_packed_refs(repo);
771 if (packed_refs_path == NULL) {
772 err = got_error_from_errno("got_repo_get_path_packed_refs");
776 f = fopen(packed_refs_path, "r");
777 free(packed_refs_path);
781 const char delim[3] = {'\0', '\0', '\0'};
783 line = fparseln(f, &len, NULL, delim, 0);
787 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
790 err = parse_packed_ref_line(&ref, NULL, line);
795 err = insert_ref(&new, refs, ref, repo);
796 if (err || new == NULL /* duplicate */)
805 if (f && fclose(f) != 0 && err == NULL)
806 err = got_error_from_errno("fclose");
811 got_ref_list_free(struct got_reflist_head *refs)
813 struct got_reflist_entry *re;
815 while (!SIMPLEQ_EMPTY(refs)) {
816 re = SIMPLEQ_FIRST(refs);
817 SIMPLEQ_REMOVE_HEAD(refs, entry);
818 got_ref_close(re->ref);
826 got_ref_is_symbolic(struct got_reference *ref)
828 return (ref->flags & GOT_REF_IS_SYMBOLIC);
831 const struct got_error *
832 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
834 if (ref->flags & GOT_REF_IS_SYMBOLIC)
835 return got_error(GOT_ERR_BAD_REF_TYPE);
837 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
841 const struct got_error *
842 got_ref_change_symref(struct got_reference *ref, char *refname)
846 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
847 return got_error(GOT_ERR_BAD_REF_TYPE);
849 new_name = strdup(refname);
850 if (new_name == NULL)
851 return got_error_from_errno("strdup");
853 free(ref->ref.symref.name);
854 ref->ref.symref.name = new_name;
858 const struct got_error *
859 got_ref_write(struct got_reference *ref, struct got_repository *repo)
861 const struct got_error *err = NULL, *unlock_err = NULL;
862 const char *name = got_ref_get_name(ref);
863 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
864 struct got_lockfile *lf = NULL;
869 path_refs = get_refs_dir_path(repo, name);
870 if (path_refs == NULL) {
871 err = got_error_from_errno2("get_refs_dir_path", name);
875 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
876 err = got_error_from_errno("asprintf");
880 err = got_opentemp_named(&tmppath, &f, path);
883 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
885 err = got_path_dirname(&parent, path);
888 err = got_path_mkdir(parent);
892 err = got_opentemp_named(&tmppath, &f, path);
897 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
898 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
899 if (n != strlen(ref->ref.symref.ref) + 6) {
900 err = got_ferror(f, GOT_ERR_IO);
904 char hex[SHA1_DIGEST_STRING_LENGTH];
905 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
906 sizeof(hex)) == NULL) {
907 err = got_error(GOT_ERR_BAD_REF_DATA);
910 n = fprintf(f, "%s\n", hex);
911 if (n != sizeof(hex)) {
912 err = got_ferror(f, GOT_ERR_IO);
917 if (ref->lf == NULL) {
918 err = got_lockfile_lock(&lf, path);
923 /* XXX: check if old content matches our expectations? */
925 if (stat(path, &sb) != 0) {
926 if (errno != ENOENT) {
927 err = got_error_from_errno2("stat", path);
930 sb.st_mode = GOT_DEFAULT_FILE_MODE;
933 if (rename(tmppath, path) != 0) {
934 err = got_error_from_errno3("rename", tmppath, path);
940 if (chmod(path, sb.st_mode) != 0) {
941 err = got_error_from_errno2("chmod", path);
945 if (ref->lf == NULL && lf)
946 unlock_err = got_lockfile_unlock(lf);
948 if (fclose(f) != 0 && err == NULL)
949 err = got_error_from_errno("fclose");
954 if (unlink(tmppath) != 0 && err == NULL)
955 err = got_error_from_errno2("unlink", tmppath);
958 return err ? err : unlock_err;
961 static const struct got_error *
962 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
964 const struct got_error *err = NULL, *unlock_err = NULL;
965 struct got_lockfile *lf = NULL;
966 FILE *f = NULL, *tmpf = NULL;
967 char *packed_refs_path, *tmppath = NULL;
968 struct got_reflist_head refs;
969 int found_delref = 0;
971 /* The packed-refs file does not cotain symbolic references. */
972 if (delref->flags & GOT_REF_IS_SYMBOLIC)
973 return got_error(GOT_ERR_BAD_REF_DATA);
977 packed_refs_path = got_repo_get_path_packed_refs(repo);
978 if (packed_refs_path == NULL)
979 return got_error_from_errno("got_repo_get_path_packed_refs");
981 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
985 if (delref->lf == NULL) {
986 err = got_lockfile_lock(&lf, packed_refs_path);
991 f = fopen(packed_refs_path, "r");
993 err = got_error_from_errno2("fopen", packed_refs_path);
999 const char delim[3] = {'\0', '\0', '\0'};
1000 struct got_reference *ref;
1001 struct got_reflist_entry *new;
1003 line = fparseln(f, &len, NULL, delim, 0);
1007 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1010 err = parse_packed_ref_line(&ref, NULL, line);
1017 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1018 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1019 sizeof(delref->ref.ref.sha1)) == 0) {
1025 err = insert_ref(&new, &refs, ref, repo);
1026 if (err || new == NULL /* duplicate */)
1033 struct got_reflist_entry *re;
1037 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1038 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1039 err = got_ferror(f, GOT_ERR_IO);
1043 SIMPLEQ_FOREACH(re, &refs, entry) {
1044 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1046 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1047 sizeof(hex)) == NULL) {
1048 err = got_error(GOT_ERR_BAD_REF_DATA);
1051 n = fprintf(tmpf, "%s ", hex);
1052 if (n != sizeof(hex)) {
1053 err = got_ferror(f, GOT_ERR_IO);
1056 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1057 if (n != strlen(re->ref->ref.ref.name) + 1) {
1058 err = got_ferror(f, GOT_ERR_IO);
1063 if (fflush(tmpf) != 0) {
1064 err = got_error_from_errno("fflush");
1068 if (stat(packed_refs_path, &sb) != 0) {
1069 if (errno != ENOENT) {
1070 err = got_error_from_errno2("stat",
1074 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1077 if (rename(tmppath, packed_refs_path) != 0) {
1078 err = got_error_from_errno3("rename", tmppath,
1083 if (chmod(packed_refs_path, sb.st_mode) != 0) {
1084 err = got_error_from_errno2("chmod",
1090 if (delref->lf == NULL && lf)
1091 unlock_err = got_lockfile_unlock(lf);
1093 if (fclose(f) != 0 && err == NULL)
1094 err = got_error_from_errno("fclose");
1098 if (fclose(tmpf) != 0 && err == NULL)
1099 err = got_error_from_errno("fclose");
1102 free(packed_refs_path);
1103 got_ref_list_free(&refs);
1104 return err ? err : unlock_err;
1107 const struct got_error *
1108 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1110 const struct got_error *err = NULL, *unlock_err = NULL;
1111 const char *name = got_ref_get_name(ref);
1112 char *path_refs = NULL, *path = NULL;
1113 struct got_lockfile *lf = NULL;
1115 if (ref->flags & GOT_REF_IS_PACKED)
1116 return delete_packed_ref(ref, repo);
1118 path_refs = get_refs_dir_path(repo, name);
1119 if (path_refs == NULL) {
1120 err = got_error_from_errno2("get_refs_dir_path", name);
1124 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1125 err = got_error_from_errno("asprintf");
1129 if (ref->lf == NULL) {
1130 err = got_lockfile_lock(&lf, path);
1135 /* XXX: check if old content matches our expectations? */
1137 if (unlink(path) != 0)
1138 err = got_error_from_errno2("unlink", path);
1140 if (ref->lf == NULL && lf)
1141 unlock_err = got_lockfile_unlock(lf);
1145 return err ? err : unlock_err;
1148 const struct got_error *
1149 got_ref_unlock(struct got_reference *ref)
1151 const struct got_error *err;
1152 err = got_lockfile_unlock(ref->lf);