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 break;
304 for (i = 0; i < nsubdirs; i++) {
305 if (!ref_is_absolute &&
306 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
307 refname) == -1)
308 return got_error_from_errno();
309 err = parse_packed_ref_line(ref, abs_refname, line);
310 if (!ref_is_absolute)
311 free(abs_refname);
312 if (err || *ref != NULL)
313 break;
315 free(line);
316 if (err)
317 break;
318 } while (*ref == NULL);
320 return err;
323 static const struct got_error *
324 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
325 const char *name)
327 const struct got_error *err = NULL;
328 char *path = NULL;
329 char *normpath = NULL;
330 char *absname = NULL;
331 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
332 int ref_is_well_known = is_well_known_ref(name);
334 *ref = NULL;
336 if (ref_is_absolute || ref_is_well_known) {
337 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
338 return got_error_from_errno();
339 absname = (char *)name;
340 } else {
341 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
342 subdir[0] ? "/" : "", name) == -1)
343 return got_error_from_errno();
345 if (asprintf(&absname, "refs/%s%s%s",
346 subdir, subdir[0] ? "/" : "", name) == -1) {
347 err = got_error_from_errno();
348 goto done;
352 normpath = got_path_normalize(path);
353 if (normpath == NULL) {
354 err = got_error_from_errno();
355 goto done;
358 err = parse_ref_file(ref, absname, normpath);
359 done:
360 if (!ref_is_absolute && !ref_is_well_known)
361 free(absname);
362 free(path);
363 free(normpath);
364 return err;
367 const struct got_error *
368 got_ref_open(struct got_reference **ref, struct got_repository *repo,
369 const char *refname)
371 const struct got_error *err = NULL;
372 char *path_refs = NULL;
373 const char *subdirs[] = {
374 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
375 };
376 int i, well_known = is_well_known_ref(refname);
378 *ref = NULL;
380 path_refs = get_refs_dir_path(repo, refname);
381 if (path_refs == NULL) {
382 err = got_error_from_errno();
383 goto done;
386 if (!well_known) {
387 char *packed_refs_path;
388 FILE *f;
390 /* Search on-disk refs before packed refs! */
391 for (i = 0; i < nitems(subdirs); i++) {
392 err = open_ref(ref, path_refs, subdirs[i], refname);
393 if (err || *ref)
394 goto done;
397 packed_refs_path = got_repo_get_path_packed_refs(repo);
398 if (packed_refs_path == NULL) {
399 err = got_error_from_errno();
400 goto done;
403 f = fopen(packed_refs_path, "rb");
404 free(packed_refs_path);
405 if (f != NULL) {
406 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
407 refname);
408 if (fclose(f) != 0 && err == NULL)
409 err = got_error_from_errno();
410 if (err || *ref)
411 goto done;
415 err = open_ref(ref, path_refs, "", refname);
416 if (err)
417 goto done;
418 done:
419 if (*ref == NULL)
420 err = got_error_not_ref(refname);
421 free(path_refs);
422 return err;
425 void
426 got_ref_close(struct got_reference *ref)
428 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
429 free(ref->ref.symref.name);
430 free(ref->ref.symref.ref);
431 } else
432 free(ref->ref.ref.name);
433 free(ref);
436 struct got_reference *
437 got_ref_dup(struct got_reference *ref)
439 struct got_reference *ret;
441 ret = calloc(1, sizeof(*ret));
442 if (ret == NULL)
443 return NULL;
445 ret->flags = ref->flags;
446 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
447 ret->ref.symref.name = strdup(ref->ref.symref.name);
448 if (ret->ref.symref.name == NULL) {
449 free(ret);
450 return NULL;
452 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
453 if (ret->ref.symref.ref == NULL) {
454 free(ret->ref.symref.name);
455 free(ret);
456 return NULL;
458 } else {
459 ref->ref.ref.name = strdup(ref->ref.ref.name);
460 if (ref->ref.ref.name == NULL) {
461 free(ret);
462 return NULL;
464 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
465 SHA1_DIGEST_LENGTH);
468 return ret;
471 static const struct got_error *
472 resolve_symbolic_ref(struct got_reference **resolved,
473 struct got_repository *repo, struct got_reference *ref)
475 struct got_reference *nextref;
476 const struct got_error *err;
478 err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
479 if (err)
480 return err;
482 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
483 err = resolve_symbolic_ref(resolved, repo, nextref);
484 else
485 *resolved = got_ref_dup(nextref);
487 got_ref_close(nextref);
488 return err;
491 const struct got_error *
492 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
493 struct got_reference *ref)
495 const struct got_error *err;
497 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
498 struct got_reference *resolved = NULL;
499 err = resolve_symbolic_ref(&resolved, repo, ref);
500 if (err == NULL)
501 err = got_ref_resolve(id, repo, resolved);
502 got_ref_close(resolved);
503 return err;
506 *id = calloc(1, sizeof(**id));
507 if (*id == NULL)
508 return got_error_from_errno();
509 memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
510 return NULL;
513 char *
514 got_ref_to_str(struct got_reference *ref)
516 char *str;
518 if (ref->flags & GOT_REF_IS_SYMBOLIC)
519 return strdup(ref->ref.symref.ref);
521 str = malloc(SHA1_DIGEST_STRING_LENGTH);
522 if (str == NULL)
523 return NULL;
525 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
526 SHA1_DIGEST_STRING_LENGTH) == NULL) {
527 free(str);
528 return NULL;
531 return str;
534 const char *
535 got_ref_get_name(struct got_reference *ref)
537 if (ref->flags & GOT_REF_IS_SYMBOLIC)
538 return ref->ref.symref.name;
540 return ref->ref.ref.name;
543 static const struct got_error *
544 insert_ref(struct got_reflist_head *refs, struct got_reference *ref,
545 struct got_repository *repo)
547 const struct got_error *err;
548 struct got_object_id *id;
549 struct got_reflist_entry *new, *re, *prev;
550 int cmp;
552 err = got_ref_resolve(&id, repo, ref);
553 if (err)
554 return err;
556 new = malloc(sizeof(*re));
557 if (new == NULL) {
558 free(id);
559 return got_error_from_errno();
561 new->ref = ref;
562 new->id = id;
564 /*
565 * We must de-duplicate entries on insert because packed-refs may
566 * contain redundant entries. On-disk refs take precedence.
567 * This code assumes that on-disk revs are read before packed-refs.
568 * We're iterating the list anyway, so insert elements sorted by name.
569 */
570 re = SIMPLEQ_FIRST(refs);
571 while (re) {
572 cmp = got_path_cmp(got_ref_get_name(re->ref),
573 got_ref_get_name(ref));
574 if (cmp == 0) {
575 got_ref_close(ref); /* duplicate */
576 return NULL;
577 } else if (cmp > 0) {
578 if (prev)
579 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
580 else
581 SIMPLEQ_INSERT_HEAD(refs, new, entry);
582 return NULL;
583 } else {
584 prev = re;
585 re = SIMPLEQ_NEXT(re, entry);
589 SIMPLEQ_INSERT_TAIL(refs, new, entry);
590 return NULL;
593 static const struct got_error *
594 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
595 const char *subdir, struct got_repository *repo)
597 const struct got_error *err = NULL;
598 DIR *d = NULL;
599 char *path_subdir;
601 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
602 return got_error_from_errno();
604 d = opendir(path_subdir);
605 if (d == NULL)
606 goto done;
608 while (1) {
609 struct dirent *dent;
610 struct got_reference *ref;
611 char *child;
613 dent = readdir(d);
614 if (dent == NULL)
615 break;
617 if (strcmp(dent->d_name, ".") == 0 ||
618 strcmp(dent->d_name, "..") == 0)
619 continue;
621 switch (dent->d_type) {
622 case DT_REG:
623 err = open_ref(&ref, path_refs, subdir, dent->d_name);
624 if (err)
625 goto done;
626 if (ref) {
627 err = insert_ref(refs, ref, repo);
628 if (err)
629 goto done;
631 break;
632 case DT_DIR:
633 if (asprintf(&child, "%s%s%s", subdir,
634 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
635 err = got_error_from_errno();
636 break;
638 err = gather_on_disk_refs(refs, path_refs, child, repo);
639 free(child);
640 break;
641 default:
642 break;
645 done:
646 if (d)
647 closedir(d);
648 free(path_subdir);
649 return err;
652 const struct got_error *
653 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
655 const struct got_error *err;
656 char *packed_refs_path, *path_refs = NULL;
657 FILE *f = NULL;
658 struct got_reference *ref;
660 /* HEAD ref should always exist. */
661 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
662 if (path_refs == NULL) {
663 err = got_error_from_errno();
664 goto done;
666 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD);
667 if (err)
668 goto done;
669 err = insert_ref(refs, ref, repo);
670 if (err)
671 goto done;
673 /* Gather on-disk refs before parsing packed-refs. */
674 free(path_refs);
675 path_refs = get_refs_dir_path(repo, "");
676 if (path_refs == NULL) {
677 err = got_error_from_errno();
678 goto done;
680 err = gather_on_disk_refs(refs, path_refs, "", repo);
681 if (err)
682 goto done;
684 /*
685 * The packed-refs file may contain redundant entries, in which
686 * case on-disk refs take precedence.
687 */
688 packed_refs_path = got_repo_get_path_packed_refs(repo);
689 if (packed_refs_path == NULL) {
690 err = got_error_from_errno();
691 goto done;
694 f = fopen(packed_refs_path, "r");
695 free(packed_refs_path);
696 if (f) {
697 char *line;
698 size_t len;
699 const char delim[3] = {'\0', '\0', '\0'};
700 while (1) {
701 line = fparseln(f, &len, NULL, delim, 0);
702 if (line == NULL)
703 break;
704 err = parse_packed_ref_line(&ref, NULL, line);
705 if (err)
706 goto done;
707 if (ref) {
708 err = insert_ref(refs, ref, repo);
709 if (err)
710 goto done;
714 done:
715 free(path_refs);
716 if (f && fclose(f) != 0 && err == NULL)
717 err = got_error_from_errno();
718 return err;
721 void
722 got_ref_list_free(struct got_reflist_head *refs)
724 struct got_reflist_entry *re;
726 while (!SIMPLEQ_EMPTY(refs)) {
727 re = SIMPLEQ_FIRST(refs);
728 SIMPLEQ_REMOVE_HEAD(refs, entry);
729 got_ref_close(re->ref);
730 free(re->id);
731 free(re);
736 const struct got_error *
737 got_ref_write(struct got_reference *ref, struct got_repository *repo)
739 const struct got_error *err = NULL, *unlock_err = NULL;
740 const char *name = got_ref_get_name(ref);
741 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
742 struct got_lockfile *lf = NULL;
743 FILE *f = NULL;
744 size_t n;
745 struct stat sb;
747 path_refs = get_refs_dir_path(repo, name);
748 if (path_refs == NULL) {
749 err = got_error_from_errno();
750 goto done;
753 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
754 err = got_error_from_errno();
755 goto done;
758 err = got_opentemp_named(&tmppath, &f, path);
759 if (err) {
760 char *parent;
761 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
762 goto done;
763 err = got_path_dirname(&parent, path);
764 if (err)
765 goto done;
766 err = got_path_mkdir(parent);
767 free(parent);
768 if (err)
769 goto done;
770 err = got_opentemp_named(&tmppath, &f, path);
771 if (err)
772 goto done;
775 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
776 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
777 if (n != strlen(ref->ref.symref.ref) + 6) {
778 err = got_ferror(f, GOT_ERR_IO);
779 goto done;
781 } else {
782 char hex[SHA1_DIGEST_STRING_LENGTH];
783 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
784 sizeof(hex)) == NULL) {
785 err = got_error(GOT_ERR_BAD_REF_DATA);
786 goto done;
788 n = fprintf(f, "%s\n", hex);
789 if (n != sizeof(hex)) {
790 err = got_ferror(f, GOT_ERR_IO);
791 goto done;
795 err = got_lockfile_lock(&lf, path);
796 if (err)
797 goto done;
799 /* XXX: check if old content matches our expectations? */
801 if (stat(path, &sb) != 0) {
802 if (errno != ENOENT) {
803 err = got_error_from_errno();
804 goto done;
806 sb.st_mode = GOT_DEFAULT_FILE_MODE;
809 if (rename(tmppath, path) != 0) {
810 err = got_error_from_errno();
811 goto done;
813 free(tmppath);
814 tmppath = NULL;
816 if (chmod(path, sb.st_mode) != 0) {
817 err = got_error_from_errno();
818 goto done;
820 done:
821 if (lf)
822 unlock_err = got_lockfile_unlock(lf);
823 if (f) {
824 if (fclose(f) != 0 && err == NULL)
825 err = got_error_from_errno();
827 free(path_refs);
828 free(path);
829 if (tmppath) {
830 if (unlink(tmppath) != 0 && err == NULL)
831 err = got_error_from_errno();
832 free(tmppath);
834 return err ? err : unlock_err;
837 const struct got_error *
838 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
840 const struct got_error *err = NULL, *unlock_err = NULL;
841 const char *name = got_ref_get_name(ref);
842 char *path_refs = NULL, *path = NULL;
843 struct got_lockfile *lf = NULL;
845 /* TODO: handle packed refs ! */
847 path_refs = get_refs_dir_path(repo, name);
848 if (path_refs == NULL) {
849 err = got_error_from_errno();
850 goto done;
853 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
854 err = got_error_from_errno();
855 goto done;
858 err = got_lockfile_lock(&lf, path);
859 if (err)
860 goto done;
862 /* XXX: check if old content matches our expectations? */
864 if (unlink(path) != 0)
865 err = got_error_from_errno();
866 done:
867 if (lf)
868 unlock_err = got_lockfile_unlock(lf);
870 free(path_refs);
871 free(path);
872 return err ? err : unlock_err;