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) {
400 err = got_error_from_errno2("got_path_normalize", path);
404 err = parse_ref_file(ref, absname, normpath, lock);
406 if (!ref_is_absolute && !ref_is_well_known)
413 const struct got_error *
414 got_ref_open(struct got_reference **ref, struct got_repository *repo,
415 const char *refname, int lock)
417 const struct got_error *err = NULL;
418 char *path_refs = NULL;
419 const char *subdirs[] = {
420 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
422 int i, well_known = is_well_known_ref(refname);
423 struct got_lockfile *lf = NULL;
427 path_refs = get_refs_dir_path(repo, refname);
428 if (path_refs == NULL) {
429 err = got_error_from_errno2("get_refs_dir_path", refname);
434 err = open_ref(ref, path_refs, "", refname, lock);
436 char *packed_refs_path;
439 /* Search on-disk refs before packed refs! */
440 for (i = 0; i < nitems(subdirs); i++) {
441 err = open_ref(ref, path_refs, subdirs[i], refname,
447 packed_refs_path = got_repo_get_path_packed_refs(repo);
448 if (packed_refs_path == NULL) {
449 err = got_error_from_errno(
450 "got_repo_get_path_packed_refs");
455 err = got_lockfile_lock(&lf, packed_refs_path);
459 f = fopen(packed_refs_path, "rb");
460 free(packed_refs_path);
462 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
465 if (fclose(f) != 0) {
466 err = got_error_from_errno("fclose");
475 if (!err && *ref == NULL)
476 err = got_error_not_ref(refname);
478 got_lockfile_unlock(lf);
484 got_ref_close(struct got_reference *ref)
486 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
487 free(ref->ref.symref.name);
488 free(ref->ref.symref.ref);
490 free(ref->ref.ref.name);
494 struct got_reference *
495 got_ref_dup(struct got_reference *ref)
497 struct got_reference *ret;
499 ret = calloc(1, sizeof(*ret));
503 ret->flags = ref->flags;
504 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
505 ret->ref.symref.name = strdup(ref->ref.symref.name);
506 if (ret->ref.symref.name == NULL) {
510 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
511 if (ret->ref.symref.ref == NULL) {
512 free(ret->ref.symref.name);
517 ref->ref.ref.name = strdup(ref->ref.ref.name);
518 if (ref->ref.ref.name == NULL) {
522 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
523 sizeof(ret->ref.ref.sha1));
529 static const struct got_error *
530 resolve_symbolic_ref(struct got_reference **resolved,
531 struct got_repository *repo, struct got_reference *ref)
533 struct got_reference *nextref;
534 const struct got_error *err;
536 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
540 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
541 err = resolve_symbolic_ref(resolved, repo, nextref);
543 *resolved = got_ref_dup(nextref);
545 got_ref_close(nextref);
549 const struct got_error *
550 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
551 struct got_reference *ref)
553 const struct got_error *err;
555 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
556 struct got_reference *resolved = NULL;
557 err = resolve_symbolic_ref(&resolved, repo, ref);
559 err = got_ref_resolve(id, repo, resolved);
561 got_ref_close(resolved);
565 *id = calloc(1, sizeof(**id));
567 return got_error_from_errno("calloc");
568 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
573 got_ref_to_str(struct got_reference *ref)
577 if (ref->flags & GOT_REF_IS_SYMBOLIC)
578 return strdup(ref->ref.symref.ref);
580 str = malloc(SHA1_DIGEST_STRING_LENGTH);
584 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
585 SHA1_DIGEST_STRING_LENGTH) == NULL) {
594 got_ref_get_name(struct got_reference *ref)
596 if (ref->flags & GOT_REF_IS_SYMBOLIC)
597 return ref->ref.symref.name;
599 return ref->ref.ref.name;
602 static const struct got_error *
603 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
604 struct got_reference *ref, struct got_repository *repo)
606 const struct got_error *err;
607 struct got_object_id *id;
608 struct got_reflist_entry *new, *re, *prev = NULL;
613 err = got_ref_resolve(&id, repo, ref);
617 new = malloc(sizeof(*new));
620 return got_error_from_errno("malloc");
627 * We must de-duplicate entries on insert because packed-refs may
628 * contain redundant entries. On-disk refs take precedence.
629 * This code assumes that on-disk revs are read before packed-refs.
630 * We're iterating the list anyway, so insert elements sorted by name.
632 re = SIMPLEQ_FIRST(refs);
634 cmp = got_path_cmp(got_ref_get_name(re->ref),
635 got_ref_get_name(new->ref));
642 } else if (cmp > 0) {
644 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
646 SIMPLEQ_INSERT_HEAD(refs, new, entry);
650 re = SIMPLEQ_NEXT(re, entry);
654 SIMPLEQ_INSERT_TAIL(refs, new, entry);
658 static const struct got_error *
659 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
660 const char *subdir, struct got_repository *repo)
662 const struct got_error *err = NULL;
666 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
667 return got_error_from_errno("asprintf");
669 d = opendir(path_subdir);
675 struct got_reference *ref;
682 if (strcmp(dent->d_name, ".") == 0 ||
683 strcmp(dent->d_name, "..") == 0)
686 switch (dent->d_type) {
688 err = open_ref(&ref, path_refs, subdir, dent->d_name,
693 struct got_reflist_entry *new;
694 err = insert_ref(&new, refs, ref, repo);
695 if (err || new == NULL /* duplicate */)
702 if (asprintf(&child, "%s%s%s", subdir,
703 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
704 err = got_error_from_errno("asprintf");
707 err = gather_on_disk_refs(refs, path_refs, child, repo);
721 const struct got_error *
722 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
724 const struct got_error *err;
725 char *packed_refs_path, *path_refs = NULL;
727 struct got_reference *ref;
728 struct got_reflist_entry *new;
730 /* HEAD ref should always exist. */
731 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
732 if (path_refs == NULL) {
733 err = got_error_from_errno("get_refs_dir_path");
736 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
739 err = insert_ref(&new, refs, ref, repo);
740 if (err || new == NULL /* duplicate */)
745 /* Gather on-disk refs before parsing packed-refs. */
747 path_refs = get_refs_dir_path(repo, "");
748 if (path_refs == NULL) {
749 err = got_error_from_errno("get_refs_dir_path");
752 err = gather_on_disk_refs(refs, path_refs, "", repo);
757 * The packed-refs file may contain redundant entries, in which
758 * case on-disk refs take precedence.
760 packed_refs_path = got_repo_get_path_packed_refs(repo);
761 if (packed_refs_path == NULL) {
762 err = got_error_from_errno("got_repo_get_path_packed_refs");
766 f = fopen(packed_refs_path, "r");
767 free(packed_refs_path);
771 const char delim[3] = {'\0', '\0', '\0'};
773 line = fparseln(f, &len, NULL, delim, 0);
777 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
780 err = parse_packed_ref_line(&ref, NULL, line);
785 err = insert_ref(&new, refs, ref, repo);
786 if (err || new == NULL /* duplicate */)
795 if (f && fclose(f) != 0 && err == NULL)
796 err = got_error_from_errno("fclose");
801 got_ref_list_free(struct got_reflist_head *refs)
803 struct got_reflist_entry *re;
805 while (!SIMPLEQ_EMPTY(refs)) {
806 re = SIMPLEQ_FIRST(refs);
807 SIMPLEQ_REMOVE_HEAD(refs, entry);
808 got_ref_close(re->ref);
816 got_ref_is_symbolic(struct got_reference *ref)
818 return (ref->flags & GOT_REF_IS_SYMBOLIC);
821 const struct got_error *
822 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
824 if (ref->flags & GOT_REF_IS_SYMBOLIC)
825 return got_error(GOT_ERR_BAD_REF_TYPE);
827 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
831 const struct got_error *
832 got_ref_change_symref(struct got_reference *ref, char *refname)
836 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
837 return got_error(GOT_ERR_BAD_REF_TYPE);
839 new_name = strdup(refname);
840 if (new_name == NULL)
841 return got_error_from_errno("strdup");
843 free(ref->ref.symref.name);
844 ref->ref.symref.name = new_name;
848 const struct got_error *
849 got_ref_write(struct got_reference *ref, struct got_repository *repo)
851 const struct got_error *err = NULL, *unlock_err = NULL;
852 const char *name = got_ref_get_name(ref);
853 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
854 struct got_lockfile *lf = NULL;
859 path_refs = get_refs_dir_path(repo, name);
860 if (path_refs == NULL) {
861 err = got_error_from_errno2("get_refs_dir_path", name);
865 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
866 err = got_error_from_errno("asprintf");
870 err = got_opentemp_named(&tmppath, &f, path);
873 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
875 err = got_path_dirname(&parent, path);
878 err = got_path_mkdir(parent);
882 err = got_opentemp_named(&tmppath, &f, path);
887 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
888 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
889 if (n != strlen(ref->ref.symref.ref) + 6) {
890 err = got_ferror(f, GOT_ERR_IO);
894 char hex[SHA1_DIGEST_STRING_LENGTH];
895 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
896 sizeof(hex)) == NULL) {
897 err = got_error(GOT_ERR_BAD_REF_DATA);
900 n = fprintf(f, "%s\n", hex);
901 if (n != sizeof(hex)) {
902 err = got_ferror(f, GOT_ERR_IO);
907 if (ref->lf == NULL) {
908 err = got_lockfile_lock(&lf, path);
913 /* XXX: check if old content matches our expectations? */
915 if (stat(path, &sb) != 0) {
916 if (errno != ENOENT) {
917 err = got_error_from_errno2("stat", path);
920 sb.st_mode = GOT_DEFAULT_FILE_MODE;
923 if (rename(tmppath, path) != 0) {
924 err = got_error_from_errno3("rename", tmppath, path);
930 if (chmod(path, sb.st_mode) != 0) {
931 err = got_error_from_errno2("chmod", path);
935 if (ref->lf == NULL && lf)
936 unlock_err = got_lockfile_unlock(lf);
938 if (fclose(f) != 0 && err == NULL)
939 err = got_error_from_errno("fclose");
944 if (unlink(tmppath) != 0 && err == NULL)
945 err = got_error_from_errno2("unlink", tmppath);
948 return err ? err : unlock_err;
951 static const struct got_error *
952 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
954 const struct got_error *err = NULL, *unlock_err = NULL;
955 struct got_lockfile *lf = NULL;
956 FILE *f = NULL, *tmpf = NULL;
957 char *packed_refs_path, *tmppath = NULL;
958 struct got_reflist_head refs;
959 int found_delref = 0;
961 /* The packed-refs file does not cotain symbolic references. */
962 if (delref->flags & GOT_REF_IS_SYMBOLIC)
963 return got_error(GOT_ERR_BAD_REF_DATA);
967 packed_refs_path = got_repo_get_path_packed_refs(repo);
968 if (packed_refs_path == NULL)
969 return got_error_from_errno("got_repo_get_path_packed_refs");
971 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
975 if (delref->lf == NULL) {
976 err = got_lockfile_lock(&lf, packed_refs_path);
981 f = fopen(packed_refs_path, "r");
983 err = got_error_from_errno2("fopen", packed_refs_path);
989 const char delim[3] = {'\0', '\0', '\0'};
990 struct got_reference *ref;
991 struct got_reflist_entry *new;
993 line = fparseln(f, &len, NULL, delim, 0);
997 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1000 err = parse_packed_ref_line(&ref, NULL, line);
1007 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1008 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1009 sizeof(delref->ref.ref.sha1)) == 0) {
1015 err = insert_ref(&new, &refs, ref, repo);
1016 if (err || new == NULL /* duplicate */)
1023 struct got_reflist_entry *re;
1027 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1028 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1029 err = got_ferror(f, GOT_ERR_IO);
1033 SIMPLEQ_FOREACH(re, &refs, entry) {
1034 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1036 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1037 sizeof(hex)) == NULL) {
1038 err = got_error(GOT_ERR_BAD_REF_DATA);
1041 n = fprintf(tmpf, "%s ", hex);
1042 if (n != sizeof(hex)) {
1043 err = got_ferror(f, GOT_ERR_IO);
1046 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1047 if (n != strlen(re->ref->ref.ref.name) + 1) {
1048 err = got_ferror(f, GOT_ERR_IO);
1053 if (fflush(tmpf) != 0) {
1054 err = got_error_from_errno("fflush");
1058 if (stat(packed_refs_path, &sb) != 0) {
1059 if (errno != ENOENT) {
1060 err = got_error_from_errno2("stat",
1064 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1067 if (rename(tmppath, packed_refs_path) != 0) {
1068 err = got_error_from_errno3("rename", tmppath,
1073 if (chmod(packed_refs_path, sb.st_mode) != 0) {
1074 err = got_error_from_errno2("chmod",
1080 if (delref->lf == NULL && lf)
1081 unlock_err = got_lockfile_unlock(lf);
1083 if (fclose(f) != 0 && err == NULL)
1084 err = got_error_from_errno("fclose");
1088 if (fclose(tmpf) != 0 && err == NULL)
1089 err = got_error_from_errno("fclose");
1092 free(packed_refs_path);
1093 got_ref_list_free(&refs);
1094 return err ? err : unlock_err;
1097 const struct got_error *
1098 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1100 const struct got_error *err = NULL, *unlock_err = NULL;
1101 const char *name = got_ref_get_name(ref);
1102 char *path_refs = NULL, *path = NULL;
1103 struct got_lockfile *lf = NULL;
1105 if (ref->flags & GOT_REF_IS_PACKED)
1106 return delete_packed_ref(ref, repo);
1108 path_refs = get_refs_dir_path(repo, name);
1109 if (path_refs == NULL) {
1110 err = got_error_from_errno2("get_refs_dir_path", name);
1114 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1115 err = got_error_from_errno("asprintf");
1119 if (ref->lf == NULL) {
1120 err = got_lockfile_lock(&lf, path);
1125 /* XXX: check if old content matches our expectations? */
1127 if (unlink(path) != 0)
1128 err = got_error_from_errno2("unlink", path);
1130 if (ref->lf == NULL && lf)
1131 unlock_err = got_lockfile_unlock(lf);
1135 return err ? err : unlock_err;
1138 const struct got_error *
1139 got_ref_unlock(struct got_reference *ref)
1141 const struct got_error *err;
1142 err = got_lockfile_unlock(ref->lf);