Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/stat.h>
21 #include <errno.h>
22 #include <ctype.h>
23 #include <dirent.h>
24 #include <sha1.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <util.h>
29 #include <zlib.h>
30 #include <time.h>
31 #include <libgen.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"
39 #include "got_lib_sha1.h"
40 #include "got_lib_path.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"
46 #ifndef nitems
47 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
48 #endif
50 #define GOT_REF_HEADS "heads"
51 #define GOT_REF_TAGS "tags"
52 #define GOT_REF_REMOTES "remotes"
54 /* A symbolic reference. */
55 struct got_symref {
56 char *name;
57 char *ref;
58 };
60 /* A non-symbolic reference (there is no better designation). */
61 struct got_ref {
62 char *name;
63 u_int8_t sha1[SHA1_DIGEST_LENGTH];
64 };
66 /* A reference which points to an arbitrary object. */
67 struct got_reference {
68 unsigned int flags;
69 #define GOT_REF_IS_SYMBOLIC 0x01
70 #define GOT_REF_IS_PACKED 0x02
72 union {
73 struct got_ref ref;
74 struct got_symref symref;
75 } ref;
76 };
78 static const struct got_error *
79 alloc_ref(struct got_reference **ref, const char *name,
80 struct got_object_id *id, int flags)
81 {
82 const struct got_error *err = NULL;
84 *ref = calloc(1, sizeof(**ref));
85 if (*ref == NULL)
86 return got_error_from_errno();
88 memcpy(&(*ref)->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
89 (*ref)->flags = flags;
90 (*ref)->ref.ref.name = strdup(name);
91 if ((*ref)->ref.ref.name == NULL) {
92 err = got_error_from_errno();
93 got_ref_close(*ref);
94 *ref = NULL;
95 }
96 return err;
97 }
99 static const struct got_error *
100 alloc_symref(struct got_reference **ref, const char *name,
101 const char *target_ref, int flags)
103 const struct got_error *err = NULL;
105 *ref = calloc(1, sizeof(**ref));
106 if (*ref == NULL)
107 return got_error_from_errno();
109 (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
110 (*ref)->ref.symref.name = strdup(name);
111 if ((*ref)->ref.symref.name == NULL) {
112 err = got_error_from_errno();
113 got_ref_close(*ref);
114 *ref = NULL;
116 (*ref)->ref.symref.ref = strdup(target_ref);
117 if ((*ref)->ref.symref.ref == NULL) {
118 err = got_error_from_errno();
119 got_ref_close(*ref);
120 *ref = NULL;
122 return err;
125 static const struct got_error *
126 parse_symref(struct got_reference **ref, const char *name, const char *line)
128 if (line[0] == '\0')
129 return got_error(GOT_ERR_BAD_REF_DATA);
131 return alloc_symref(ref, name, line, 0);
134 static const struct got_error *
135 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
137 struct got_object_id id;
139 if (strncmp(line, "ref: ", 5) == 0) {
140 line += 5;
141 return parse_symref(ref, name, line);
144 if (!got_parse_sha1_digest(id.sha1, line))
145 return got_error(GOT_ERR_BAD_REF_DATA);
147 return alloc_ref(ref, name, &id, 0);
150 static const struct got_error *
151 parse_ref_file(struct got_reference **ref, const char *name,
152 const char *abspath)
154 const struct got_error *err = NULL;
155 FILE *f = fopen(abspath, "rb");
156 char *line;
157 size_t len;
158 const char delim[3] = {'\0', '\0', '\0'};
160 if (f == NULL)
161 return NULL;
163 line = fparseln(f, &len, NULL, delim, 0);
164 if (line == NULL) {
165 err = got_error(GOT_ERR_BAD_REF_DATA);
166 goto done;
169 err = parse_ref_line(ref, name, line);
170 done:
171 free(line);
172 if (fclose(f) != 0 && err == NULL)
173 err = got_error_from_errno();
174 return err;
177 static int
178 is_well_known_ref(const char *refname)
180 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
181 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
182 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
183 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
186 static char *
187 get_refs_dir_path(struct got_repository *repo, const char *refname)
189 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
190 return strdup(got_repo_get_path_git_dir(repo));
192 return got_repo_get_path_refs(repo);
195 static int
196 is_valid_ref_name(const char *name)
198 const char *s, *slash, *seg;
199 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
200 const char *forbidden_seq[] = { "//", "..", "@{" };
201 const char *lfs = GOT_LOCKFILE_SUFFIX;
202 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
203 int i;
205 if (name[0] == '@' && name[1] == '\0')
206 return 0;
208 slash = strchr(name, '/');
209 if (slash == NULL)
210 return 0;
212 s = name;
213 seg = s;
214 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
215 return 0;
216 while (*s) {
217 for (i = 0; i < nitems(forbidden); i++) {
218 if (*s == forbidden[i])
219 return 0;
221 for (i = 0; i < nitems(forbidden_seq); i++) {
222 if (s[0] == forbidden_seq[i][0] &&
223 s[1] == forbidden_seq[i][1])
224 return 0;
226 if (iscntrl((unsigned char)s[0]))
227 return 0;
228 if (s[0] == '.' && s[1] == '\0')
229 return 0;
230 if (*s == '/') {
231 const char *nextseg = s + 1;
232 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
233 nextseg[0] == '/')
234 return 0;
235 if (seg <= s - lfs_len &&
236 strncmp(s - lfs_len, lfs, lfs_len) == 0)
237 return 0;
238 seg = nextseg;
240 s++;
243 if (seg <= s - lfs_len &&
244 strncmp(s - lfs_len, lfs, lfs_len) == 0)
245 return 0;
247 return 1;
250 const struct got_error *
251 got_ref_alloc(struct got_reference **ref, const char *name,
252 struct got_object_id *id)
254 if (!is_valid_ref_name(name))
255 return got_error(GOT_ERR_BAD_REF_NAME);
257 return alloc_ref(ref, name, id, 0);
260 static const struct got_error *
261 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
262 const char *line)
264 struct got_object_id id;
265 const char *name;
267 *ref = NULL;
269 if (line[0] == '#' || line[0] == '^')
270 return NULL;
272 if (!got_parse_sha1_digest(id.sha1, line))
273 return got_error(GOT_ERR_BAD_REF_DATA);
275 if (abs_refname) {
276 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
277 return NULL;
278 name = abs_refname;
279 } else
280 name = line + SHA1_DIGEST_STRING_LENGTH;
282 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
285 static const struct got_error *
286 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
287 int nsubdirs, const char *refname)
289 const struct got_error *err = NULL;
290 char *abs_refname;
291 char *line;
292 size_t len;
293 const char delim[3] = {'\0', '\0', '\0'};
294 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
296 *ref = NULL;
298 if (ref_is_absolute)
299 abs_refname = (char *)refname;
300 do {
301 line = fparseln(f, &len, NULL, delim, 0);
302 if (line == NULL) {
303 if (feof(f))
304 break;
305 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
306 break;
308 for (i = 0; i < nsubdirs; i++) {
309 if (!ref_is_absolute &&
310 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
311 refname) == -1)
312 return got_error_from_errno();
313 err = parse_packed_ref_line(ref, abs_refname, line);
314 if (!ref_is_absolute)
315 free(abs_refname);
316 if (err || *ref != NULL)
317 break;
319 free(line);
320 if (err)
321 break;
322 } while (*ref == NULL);
324 return err;
327 static const struct got_error *
328 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
329 const char *name)
331 const struct got_error *err = NULL;
332 char *path = NULL;
333 char *normpath = NULL;
334 char *absname = NULL;
335 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
336 int ref_is_well_known = is_well_known_ref(name);
338 *ref = NULL;
340 if (ref_is_absolute || ref_is_well_known) {
341 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
342 return got_error_from_errno();
343 absname = (char *)name;
344 } else {
345 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
346 subdir[0] ? "/" : "", name) == -1)
347 return got_error_from_errno();
349 if (asprintf(&absname, "refs/%s%s%s",
350 subdir, subdir[0] ? "/" : "", name) == -1) {
351 err = got_error_from_errno();
352 goto done;
356 normpath = got_path_normalize(path);
357 if (normpath == NULL) {
358 err = got_error_from_errno();
359 goto done;
362 err = parse_ref_file(ref, absname, normpath);
363 done:
364 if (!ref_is_absolute && !ref_is_well_known)
365 free(absname);
366 free(path);
367 free(normpath);
368 return err;
371 const struct got_error *
372 got_ref_open(struct got_reference **ref, struct got_repository *repo,
373 const char *refname)
375 const struct got_error *err = NULL;
376 char *path_refs = NULL;
377 const char *subdirs[] = {
378 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
379 };
380 int i, well_known = is_well_known_ref(refname);
382 *ref = NULL;
384 path_refs = get_refs_dir_path(repo, refname);
385 if (path_refs == NULL) {
386 err = got_error_from_errno();
387 goto done;
390 if (!well_known) {
391 char *packed_refs_path;
392 FILE *f;
394 /* Search on-disk refs before packed refs! */
395 for (i = 0; i < nitems(subdirs); i++) {
396 err = open_ref(ref, path_refs, subdirs[i], refname);
397 if (err || *ref)
398 goto done;
401 packed_refs_path = got_repo_get_path_packed_refs(repo);
402 if (packed_refs_path == NULL) {
403 err = got_error_from_errno();
404 goto done;
407 f = fopen(packed_refs_path, "rb");
408 free(packed_refs_path);
409 if (f != NULL) {
410 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
411 refname);
412 if (fclose(f) != 0 && err == NULL)
413 err = got_error_from_errno();
414 if (err || *ref)
415 goto done;
419 err = open_ref(ref, path_refs, "", refname);
420 if (err)
421 goto done;
422 done:
423 if (*ref == NULL)
424 err = got_error_not_ref(refname);
425 free(path_refs);
426 return err;
429 void
430 got_ref_close(struct got_reference *ref)
432 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
433 free(ref->ref.symref.name);
434 free(ref->ref.symref.ref);
435 } else
436 free(ref->ref.ref.name);
437 free(ref);
440 struct got_reference *
441 got_ref_dup(struct got_reference *ref)
443 struct got_reference *ret;
445 ret = calloc(1, sizeof(*ret));
446 if (ret == NULL)
447 return NULL;
449 ret->flags = ref->flags;
450 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
451 ret->ref.symref.name = strdup(ref->ref.symref.name);
452 if (ret->ref.symref.name == NULL) {
453 free(ret);
454 return NULL;
456 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
457 if (ret->ref.symref.ref == NULL) {
458 free(ret->ref.symref.name);
459 free(ret);
460 return NULL;
462 } else {
463 ref->ref.ref.name = strdup(ref->ref.ref.name);
464 if (ref->ref.ref.name == NULL) {
465 free(ret);
466 return NULL;
468 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
469 SHA1_DIGEST_LENGTH);
472 return ret;
475 static const struct got_error *
476 resolve_symbolic_ref(struct got_reference **resolved,
477 struct got_repository *repo, struct got_reference *ref)
479 struct got_reference *nextref;
480 const struct got_error *err;
482 err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
483 if (err)
484 return err;
486 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
487 err = resolve_symbolic_ref(resolved, repo, nextref);
488 else
489 *resolved = got_ref_dup(nextref);
491 got_ref_close(nextref);
492 return err;
495 const struct got_error *
496 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
497 struct got_reference *ref)
499 const struct got_error *err;
501 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
502 struct got_reference *resolved = NULL;
503 err = resolve_symbolic_ref(&resolved, repo, ref);
504 if (err == NULL)
505 err = got_ref_resolve(id, repo, resolved);
506 got_ref_close(resolved);
507 return err;
510 *id = calloc(1, sizeof(**id));
511 if (*id == NULL)
512 return got_error_from_errno();
513 memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
514 return NULL;
517 char *
518 got_ref_to_str(struct got_reference *ref)
520 char *str;
522 if (ref->flags & GOT_REF_IS_SYMBOLIC)
523 return strdup(ref->ref.symref.ref);
525 str = malloc(SHA1_DIGEST_STRING_LENGTH);
526 if (str == NULL)
527 return NULL;
529 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
530 SHA1_DIGEST_STRING_LENGTH) == NULL) {
531 free(str);
532 return NULL;
535 return str;
538 const char *
539 got_ref_get_name(struct got_reference *ref)
541 if (ref->flags & GOT_REF_IS_SYMBOLIC)
542 return ref->ref.symref.name;
544 return ref->ref.ref.name;
547 static const struct got_error *
548 insert_ref(struct got_reflist_head *refs, struct got_reference *ref,
549 struct got_repository *repo)
551 const struct got_error *err;
552 struct got_object_id *id;
553 struct got_reflist_entry *new, *re, *prev;
554 int cmp;
556 err = got_ref_resolve(&id, repo, ref);
557 if (err)
558 return err;
560 new = malloc(sizeof(*re));
561 if (new == NULL) {
562 free(id);
563 return got_error_from_errno();
565 new->ref = ref;
566 new->id = id;
568 /*
569 * We must de-duplicate entries on insert because packed-refs may
570 * contain redundant entries. On-disk refs take precedence.
571 * This code assumes that on-disk revs are read before packed-refs.
572 * We're iterating the list anyway, so insert elements sorted by name.
573 */
574 re = SIMPLEQ_FIRST(refs);
575 while (re) {
576 cmp = got_path_cmp(got_ref_get_name(re->ref),
577 got_ref_get_name(ref));
578 if (cmp == 0) {
579 got_ref_close(ref); /* duplicate */
580 return NULL;
581 } else if (cmp > 0) {
582 if (prev)
583 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
584 else
585 SIMPLEQ_INSERT_HEAD(refs, new, entry);
586 return NULL;
587 } else {
588 prev = re;
589 re = SIMPLEQ_NEXT(re, entry);
593 SIMPLEQ_INSERT_TAIL(refs, new, entry);
594 return NULL;
597 static const struct got_error *
598 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
599 const char *subdir, struct got_repository *repo)
601 const struct got_error *err = NULL;
602 DIR *d = NULL;
603 char *path_subdir;
605 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
606 return got_error_from_errno();
608 d = opendir(path_subdir);
609 if (d == NULL)
610 goto done;
612 while (1) {
613 struct dirent *dent;
614 struct got_reference *ref;
615 char *child;
617 dent = readdir(d);
618 if (dent == NULL)
619 break;
621 if (strcmp(dent->d_name, ".") == 0 ||
622 strcmp(dent->d_name, "..") == 0)
623 continue;
625 switch (dent->d_type) {
626 case DT_REG:
627 err = open_ref(&ref, path_refs, subdir, dent->d_name);
628 if (err)
629 goto done;
630 if (ref) {
631 err = insert_ref(refs, ref, repo);
632 if (err)
633 goto done;
635 break;
636 case DT_DIR:
637 if (asprintf(&child, "%s%s%s", subdir,
638 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
639 err = got_error_from_errno();
640 break;
642 err = gather_on_disk_refs(refs, path_refs, child, repo);
643 free(child);
644 break;
645 default:
646 break;
649 done:
650 if (d)
651 closedir(d);
652 free(path_subdir);
653 return err;
656 const struct got_error *
657 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
659 const struct got_error *err;
660 char *packed_refs_path, *path_refs = NULL;
661 FILE *f = NULL;
662 struct got_reference *ref;
664 /* HEAD ref should always exist. */
665 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
666 if (path_refs == NULL) {
667 err = got_error_from_errno();
668 goto done;
670 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD);
671 if (err)
672 goto done;
673 err = insert_ref(refs, ref, repo);
674 if (err)
675 goto done;
677 /* Gather on-disk refs before parsing packed-refs. */
678 free(path_refs);
679 path_refs = get_refs_dir_path(repo, "");
680 if (path_refs == NULL) {
681 err = got_error_from_errno();
682 goto done;
684 err = gather_on_disk_refs(refs, path_refs, "", repo);
685 if (err)
686 goto done;
688 /*
689 * The packed-refs file may contain redundant entries, in which
690 * case on-disk refs take precedence.
691 */
692 packed_refs_path = got_repo_get_path_packed_refs(repo);
693 if (packed_refs_path == NULL) {
694 err = got_error_from_errno();
695 goto done;
698 f = fopen(packed_refs_path, "r");
699 free(packed_refs_path);
700 if (f) {
701 char *line;
702 size_t len;
703 const char delim[3] = {'\0', '\0', '\0'};
704 while (1) {
705 line = fparseln(f, &len, NULL, delim, 0);
706 if (line == NULL)
707 break;
708 err = parse_packed_ref_line(&ref, NULL, line);
709 if (err)
710 goto done;
711 if (ref) {
712 err = insert_ref(refs, ref, repo);
713 if (err)
714 goto done;
718 done:
719 free(path_refs);
720 if (f && fclose(f) != 0 && err == NULL)
721 err = got_error_from_errno();
722 return err;
725 void
726 got_ref_list_free(struct got_reflist_head *refs)
728 struct got_reflist_entry *re;
730 while (!SIMPLEQ_EMPTY(refs)) {
731 re = SIMPLEQ_FIRST(refs);
732 SIMPLEQ_REMOVE_HEAD(refs, entry);
733 got_ref_close(re->ref);
734 free(re->id);
735 free(re);
740 const struct got_error *
741 got_ref_write(struct got_reference *ref, struct got_repository *repo)
743 const struct got_error *err = NULL, *unlock_err = NULL;
744 const char *name = got_ref_get_name(ref);
745 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
746 struct got_lockfile *lf = NULL;
747 FILE *f = NULL;
748 size_t n;
749 struct stat sb;
751 path_refs = get_refs_dir_path(repo, name);
752 if (path_refs == NULL) {
753 err = got_error_from_errno();
754 goto done;
757 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
758 err = got_error_from_errno();
759 goto done;
762 err = got_opentemp_named(&tmppath, &f, path);
763 if (err) {
764 char *parent;
765 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
766 goto done;
767 err = got_path_dirname(&parent, path);
768 if (err)
769 goto done;
770 err = got_path_mkdir(parent);
771 free(parent);
772 if (err)
773 goto done;
774 err = got_opentemp_named(&tmppath, &f, path);
775 if (err)
776 goto done;
779 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
780 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
781 if (n != strlen(ref->ref.symref.ref) + 6) {
782 err = got_ferror(f, GOT_ERR_IO);
783 goto done;
785 } else {
786 char hex[SHA1_DIGEST_STRING_LENGTH];
787 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
788 sizeof(hex)) == NULL) {
789 err = got_error(GOT_ERR_BAD_REF_DATA);
790 goto done;
792 n = fprintf(f, "%s\n", hex);
793 if (n != sizeof(hex)) {
794 err = got_ferror(f, GOT_ERR_IO);
795 goto done;
799 err = got_lockfile_lock(&lf, path);
800 if (err)
801 goto done;
803 /* XXX: check if old content matches our expectations? */
805 if (stat(path, &sb) != 0) {
806 if (errno != ENOENT) {
807 err = got_error_from_errno();
808 goto done;
810 sb.st_mode = GOT_DEFAULT_FILE_MODE;
813 if (rename(tmppath, path) != 0) {
814 err = got_error_from_errno();
815 goto done;
817 free(tmppath);
818 tmppath = NULL;
820 if (chmod(path, sb.st_mode) != 0) {
821 err = got_error_from_errno();
822 goto done;
824 done:
825 if (lf)
826 unlock_err = got_lockfile_unlock(lf);
827 if (f) {
828 if (fclose(f) != 0 && err == NULL)
829 err = got_error_from_errno();
831 free(path_refs);
832 free(path);
833 if (tmppath) {
834 if (unlink(tmppath) != 0 && err == NULL)
835 err = got_error_from_errno();
836 free(tmppath);
838 return err ? err : unlock_err;
841 const struct got_error *
842 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
844 const struct got_error *err = NULL, *unlock_err = NULL;
845 const char *name = got_ref_get_name(ref);
846 char *path_refs = NULL, *path = NULL;
847 struct got_lockfile *lf = NULL;
849 /* TODO: handle packed refs ! */
851 path_refs = get_refs_dir_path(repo, name);
852 if (path_refs == NULL) {
853 err = got_error_from_errno();
854 goto done;
857 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
858 err = got_error_from_errno();
859 goto done;
862 err = got_lockfile_lock(&lf, path);
863 if (err)
864 goto done;
866 /* XXX: check if old content matches our expectations? */
868 if (unlink(path) != 0)
869 err = got_error_from_errno();
870 done:
871 if (lf)
872 unlock_err = got_lockfile_unlock(lf);
874 free(path_refs);
875 free(path);
876 return err ? err : unlock_err;