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, sizeof((*ref)->ref.ref.sha1));
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;
156 char *line;
157 size_t len;
158 const char delim[3] = {'\0', '\0', '\0'};
160 f = fopen(abspath, "rb");
161 if (f == NULL)
162 return NULL;
164 line = fparseln(f, &len, NULL, delim, 0);
165 if (line == NULL) {
166 err = got_error(GOT_ERR_BAD_REF_DATA);
167 goto done;
170 err = parse_ref_line(ref, name, line);
171 done:
172 free(line);
173 if (fclose(f) != 0 && err == NULL)
174 err = got_error_from_errno();
175 return err;
178 static int
179 is_well_known_ref(const char *refname)
181 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
182 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
183 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
184 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
187 static char *
188 get_refs_dir_path(struct got_repository *repo, const char *refname)
190 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
191 return strdup(got_repo_get_path_git_dir(repo));
193 return got_repo_get_path_refs(repo);
196 static int
197 is_valid_ref_name(const char *name)
199 const char *s, *slash, *seg;
200 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
201 const char *forbidden_seq[] = { "//", "..", "@{" };
202 const char *lfs = GOT_LOCKFILE_SUFFIX;
203 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
204 int i;
206 if (name[0] == '@' && name[1] == '\0')
207 return 0;
209 slash = strchr(name, '/');
210 if (slash == NULL)
211 return 0;
213 s = name;
214 seg = s;
215 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
216 return 0;
217 while (*s) {
218 for (i = 0; i < nitems(forbidden); i++) {
219 if (*s == forbidden[i])
220 return 0;
222 for (i = 0; i < nitems(forbidden_seq); i++) {
223 if (s[0] == forbidden_seq[i][0] &&
224 s[1] == forbidden_seq[i][1])
225 return 0;
227 if (iscntrl((unsigned char)s[0]))
228 return 0;
229 if (s[0] == '.' && s[1] == '\0')
230 return 0;
231 if (*s == '/') {
232 const char *nextseg = s + 1;
233 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
234 nextseg[0] == '/')
235 return 0;
236 if (seg <= s - lfs_len &&
237 strncmp(s - lfs_len, lfs, lfs_len) == 0)
238 return 0;
239 seg = nextseg;
241 s++;
244 if (seg <= s - lfs_len &&
245 strncmp(s - lfs_len, lfs, lfs_len) == 0)
246 return 0;
248 return 1;
251 const struct got_error *
252 got_ref_alloc(struct got_reference **ref, const char *name,
253 struct got_object_id *id)
255 if (!is_valid_ref_name(name))
256 return got_error(GOT_ERR_BAD_REF_NAME);
258 return alloc_ref(ref, name, id, 0);
261 static const struct got_error *
262 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
263 const char *line)
265 struct got_object_id id;
266 const char *name;
268 *ref = NULL;
270 if (line[0] == '#' || line[0] == '^')
271 return NULL;
273 if (!got_parse_sha1_digest(id.sha1, line))
274 return got_error(GOT_ERR_BAD_REF_DATA);
276 if (abs_refname) {
277 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
278 return NULL;
279 name = abs_refname;
280 } else
281 name = line + SHA1_DIGEST_STRING_LENGTH;
283 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
286 static const struct got_error *
287 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
288 int nsubdirs, const char *refname)
290 const struct got_error *err = NULL;
291 char *abs_refname;
292 char *line;
293 size_t len;
294 const char delim[3] = {'\0', '\0', '\0'};
295 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
297 *ref = NULL;
299 if (ref_is_absolute)
300 abs_refname = (char *)refname;
301 do {
302 line = fparseln(f, &len, NULL, delim, 0);
303 if (line == NULL) {
304 if (feof(f))
305 break;
306 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
307 break;
309 for (i = 0; i < nsubdirs; i++) {
310 if (!ref_is_absolute &&
311 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
312 refname) == -1)
313 return got_error_from_errno();
314 err = parse_packed_ref_line(ref, abs_refname, line);
315 if (!ref_is_absolute)
316 free(abs_refname);
317 if (err || *ref != NULL)
318 break;
320 free(line);
321 if (err)
322 break;
323 } while (*ref == NULL);
325 return err;
328 static const struct got_error *
329 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
330 const char *name)
332 const struct got_error *err = NULL;
333 char *path = NULL;
334 char *normpath = NULL;
335 char *absname = NULL;
336 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
337 int ref_is_well_known = is_well_known_ref(name);
339 *ref = NULL;
341 if (ref_is_absolute || ref_is_well_known) {
342 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
343 return got_error_from_errno();
344 absname = (char *)name;
345 } else {
346 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
347 subdir[0] ? "/" : "", name) == -1)
348 return got_error_from_errno();
350 if (asprintf(&absname, "refs/%s%s%s",
351 subdir, subdir[0] ? "/" : "", name) == -1) {
352 err = got_error_from_errno();
353 goto done;
357 normpath = got_path_normalize(path);
358 if (normpath == NULL) {
359 err = got_error_from_errno();
360 goto done;
363 err = parse_ref_file(ref, absname, normpath);
364 done:
365 if (!ref_is_absolute && !ref_is_well_known)
366 free(absname);
367 free(path);
368 free(normpath);
369 return err;
372 const struct got_error *
373 got_ref_open(struct got_reference **ref, struct got_repository *repo,
374 const char *refname)
376 const struct got_error *err = NULL;
377 char *path_refs = NULL;
378 const char *subdirs[] = {
379 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
380 };
381 int i, well_known = is_well_known_ref(refname);
383 *ref = NULL;
385 path_refs = get_refs_dir_path(repo, refname);
386 if (path_refs == NULL) {
387 err = got_error_from_errno();
388 goto done;
391 if (!well_known) {
392 char *packed_refs_path;
393 FILE *f;
395 /* Search on-disk refs before packed refs! */
396 for (i = 0; i < nitems(subdirs); i++) {
397 err = open_ref(ref, path_refs, subdirs[i], refname);
398 if (err || *ref)
399 goto done;
402 packed_refs_path = got_repo_get_path_packed_refs(repo);
403 if (packed_refs_path == NULL) {
404 err = got_error_from_errno();
405 goto done;
408 f = fopen(packed_refs_path, "rb");
409 free(packed_refs_path);
410 if (f != NULL) {
411 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
412 refname);
413 if (fclose(f) != 0 && err == NULL)
414 err = got_error_from_errno();
415 if (err || *ref)
416 goto done;
420 err = open_ref(ref, path_refs, "", refname);
421 if (err)
422 goto done;
423 done:
424 if (*ref == NULL)
425 err = got_error_not_ref(refname);
426 free(path_refs);
427 return err;
430 void
431 got_ref_close(struct got_reference *ref)
433 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
434 free(ref->ref.symref.name);
435 free(ref->ref.symref.ref);
436 } else
437 free(ref->ref.ref.name);
438 free(ref);
441 struct got_reference *
442 got_ref_dup(struct got_reference *ref)
444 struct got_reference *ret;
446 ret = calloc(1, sizeof(*ret));
447 if (ret == NULL)
448 return NULL;
450 ret->flags = ref->flags;
451 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
452 ret->ref.symref.name = strdup(ref->ref.symref.name);
453 if (ret->ref.symref.name == NULL) {
454 free(ret);
455 return NULL;
457 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
458 if (ret->ref.symref.ref == NULL) {
459 free(ret->ref.symref.name);
460 free(ret);
461 return NULL;
463 } else {
464 ref->ref.ref.name = strdup(ref->ref.ref.name);
465 if (ref->ref.ref.name == NULL) {
466 free(ret);
467 return NULL;
469 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
470 sizeof(ret->ref.ref.sha1));
473 return ret;
476 static const struct got_error *
477 resolve_symbolic_ref(struct got_reference **resolved,
478 struct got_repository *repo, struct got_reference *ref)
480 struct got_reference *nextref;
481 const struct got_error *err;
483 err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
484 if (err)
485 return err;
487 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
488 err = resolve_symbolic_ref(resolved, repo, nextref);
489 else
490 *resolved = got_ref_dup(nextref);
492 got_ref_close(nextref);
493 return err;
496 const struct got_error *
497 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
498 struct got_reference *ref)
500 const struct got_error *err;
502 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
503 struct got_reference *resolved = NULL;
504 err = resolve_symbolic_ref(&resolved, repo, ref);
505 if (err == NULL)
506 err = got_ref_resolve(id, repo, resolved);
507 got_ref_close(resolved);
508 return err;
511 *id = calloc(1, sizeof(**id));
512 if (*id == NULL)
513 return got_error_from_errno();
514 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
515 return NULL;
518 char *
519 got_ref_to_str(struct got_reference *ref)
521 char *str;
523 if (ref->flags & GOT_REF_IS_SYMBOLIC)
524 return strdup(ref->ref.symref.ref);
526 str = malloc(SHA1_DIGEST_STRING_LENGTH);
527 if (str == NULL)
528 return NULL;
530 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
531 SHA1_DIGEST_STRING_LENGTH) == NULL) {
532 free(str);
533 return NULL;
536 return str;
539 const char *
540 got_ref_get_name(struct got_reference *ref)
542 if (ref->flags & GOT_REF_IS_SYMBOLIC)
543 return ref->ref.symref.name;
545 return ref->ref.ref.name;
548 static const struct got_error *
549 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
550 struct got_reference *ref, struct got_repository *repo)
552 const struct got_error *err;
553 struct got_object_id *id;
554 struct got_reflist_entry *new, *re, *prev;
555 int cmp;
557 *newp = NULL;
559 err = got_ref_resolve(&id, repo, ref);
560 if (err)
561 return err;
563 new = malloc(sizeof(*new));
564 if (new == NULL) {
565 free(id);
566 return got_error_from_errno();
568 new->ref = ref;
569 new->id = id;
570 *newp = new;
572 /*
573 * We must de-duplicate entries on insert because packed-refs may
574 * contain redundant entries. On-disk refs take precedence.
575 * This code assumes that on-disk revs are read before packed-refs.
576 * We're iterating the list anyway, so insert elements sorted by name.
577 */
578 re = SIMPLEQ_FIRST(refs);
579 while (re) {
580 cmp = got_path_cmp(got_ref_get_name(re->ref),
581 got_ref_get_name(new->ref));
582 if (cmp == 0) {
583 /* duplicate */
584 free(new->id);
585 free(new);
586 *newp = NULL;
587 return NULL;
588 } else if (cmp > 0) {
589 if (prev)
590 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
591 else
592 SIMPLEQ_INSERT_HEAD(refs, new, entry);
593 return NULL;
594 } else {
595 prev = re;
596 re = SIMPLEQ_NEXT(re, entry);
600 SIMPLEQ_INSERT_TAIL(refs, new, entry);
601 return NULL;
604 static const struct got_error *
605 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
606 const char *subdir, struct got_repository *repo)
608 const struct got_error *err = NULL;
609 DIR *d = NULL;
610 char *path_subdir;
612 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
613 return got_error_from_errno();
615 d = opendir(path_subdir);
616 if (d == NULL)
617 goto done;
619 while (1) {
620 struct dirent *dent;
621 struct got_reference *ref;
622 char *child;
624 dent = readdir(d);
625 if (dent == NULL)
626 break;
628 if (strcmp(dent->d_name, ".") == 0 ||
629 strcmp(dent->d_name, "..") == 0)
630 continue;
632 switch (dent->d_type) {
633 case DT_REG:
634 err = open_ref(&ref, path_refs, subdir, dent->d_name);
635 if (err)
636 goto done;
637 if (ref) {
638 struct got_reflist_entry *new;
639 err = insert_ref(&new, refs, ref, repo);
640 if (err || new == NULL /* duplicate */)
641 got_ref_close(ref);
642 if (err)
643 goto done;
645 break;
646 case DT_DIR:
647 if (asprintf(&child, "%s%s%s", subdir,
648 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
649 err = got_error_from_errno();
650 break;
652 err = gather_on_disk_refs(refs, path_refs, child, repo);
653 free(child);
654 break;
655 default:
656 break;
659 done:
660 if (d)
661 closedir(d);
662 free(path_subdir);
663 return err;
666 const struct got_error *
667 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
669 const struct got_error *err;
670 char *packed_refs_path, *path_refs = NULL;
671 FILE *f = NULL;
672 struct got_reference *ref;
673 struct got_reflist_entry *new;
675 /* HEAD ref should always exist. */
676 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
677 if (path_refs == NULL) {
678 err = got_error_from_errno();
679 goto done;
681 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD);
682 if (err)
683 goto done;
684 err = insert_ref(&new, refs, ref, repo);
685 if (err || new == NULL /* duplicate */)
686 got_ref_close(ref);
687 if (err)
688 goto done;
690 /* Gather on-disk refs before parsing packed-refs. */
691 free(path_refs);
692 path_refs = get_refs_dir_path(repo, "");
693 if (path_refs == NULL) {
694 err = got_error_from_errno();
695 goto done;
697 err = gather_on_disk_refs(refs, path_refs, "", repo);
698 if (err)
699 goto done;
701 /*
702 * The packed-refs file may contain redundant entries, in which
703 * case on-disk refs take precedence.
704 */
705 packed_refs_path = got_repo_get_path_packed_refs(repo);
706 if (packed_refs_path == NULL) {
707 err = got_error_from_errno();
708 goto done;
711 f = fopen(packed_refs_path, "r");
712 free(packed_refs_path);
713 if (f) {
714 char *line;
715 size_t len;
716 const char delim[3] = {'\0', '\0', '\0'};
717 while (1) {
718 line = fparseln(f, &len, NULL, delim, 0);
719 if (line == NULL) {
720 if (feof(f))
721 break;
722 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
723 goto done;
725 err = parse_packed_ref_line(&ref, NULL, line);
726 free(line);
727 if (err)
728 goto done;
729 if (ref) {
730 err = insert_ref(&new, refs, ref, repo);
731 if (err || new == NULL /* duplicate */)
732 got_ref_close(ref);
733 if (err)
734 goto done;
738 done:
739 free(path_refs);
740 if (f && fclose(f) != 0 && err == NULL)
741 err = got_error_from_errno();
742 return err;
745 void
746 got_ref_list_free(struct got_reflist_head *refs)
748 struct got_reflist_entry *re;
750 while (!SIMPLEQ_EMPTY(refs)) {
751 re = SIMPLEQ_FIRST(refs);
752 SIMPLEQ_REMOVE_HEAD(refs, entry);
753 got_ref_close(re->ref);
754 free(re->id);
755 free(re);
760 const struct got_error *
761 got_ref_write(struct got_reference *ref, struct got_repository *repo)
763 const struct got_error *err = NULL, *unlock_err = NULL;
764 const char *name = got_ref_get_name(ref);
765 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
766 struct got_lockfile *lf = NULL;
767 FILE *f = NULL;
768 size_t n;
769 struct stat sb;
771 path_refs = get_refs_dir_path(repo, name);
772 if (path_refs == NULL) {
773 err = got_error_from_errno();
774 goto done;
777 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
778 err = got_error_from_errno();
779 goto done;
782 err = got_opentemp_named(&tmppath, &f, path);
783 if (err) {
784 char *parent;
785 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
786 goto done;
787 err = got_path_dirname(&parent, path);
788 if (err)
789 goto done;
790 err = got_path_mkdir(parent);
791 free(parent);
792 if (err)
793 goto done;
794 err = got_opentemp_named(&tmppath, &f, path);
795 if (err)
796 goto done;
799 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
800 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
801 if (n != strlen(ref->ref.symref.ref) + 6) {
802 err = got_ferror(f, GOT_ERR_IO);
803 goto done;
805 } else {
806 char hex[SHA1_DIGEST_STRING_LENGTH];
807 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
808 sizeof(hex)) == NULL) {
809 err = got_error(GOT_ERR_BAD_REF_DATA);
810 goto done;
812 n = fprintf(f, "%s\n", hex);
813 if (n != sizeof(hex)) {
814 err = got_ferror(f, GOT_ERR_IO);
815 goto done;
819 err = got_lockfile_lock(&lf, path);
820 if (err)
821 goto done;
823 /* XXX: check if old content matches our expectations? */
825 if (stat(path, &sb) != 0) {
826 if (errno != ENOENT) {
827 err = got_error_from_errno();
828 goto done;
830 sb.st_mode = GOT_DEFAULT_FILE_MODE;
833 if (rename(tmppath, path) != 0) {
834 err = got_error_from_errno();
835 goto done;
837 free(tmppath);
838 tmppath = NULL;
840 if (chmod(path, sb.st_mode) != 0) {
841 err = got_error_from_errno();
842 goto done;
844 done:
845 if (lf)
846 unlock_err = got_lockfile_unlock(lf);
847 if (f) {
848 if (fclose(f) != 0 && err == NULL)
849 err = got_error_from_errno();
851 free(path_refs);
852 free(path);
853 if (tmppath) {
854 if (unlink(tmppath) != 0 && err == NULL)
855 err = got_error_from_errno();
856 free(tmppath);
858 return err ? err : unlock_err;
861 const struct got_error *
862 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
864 const struct got_error *err = NULL, *unlock_err = NULL;
865 const char *name = got_ref_get_name(ref);
866 char *path_refs = NULL, *path = NULL;
867 struct got_lockfile *lf = NULL;
869 /* TODO: handle packed refs ! */
871 path_refs = get_refs_dir_path(repo, name);
872 if (path_refs == NULL) {
873 err = got_error_from_errno();
874 goto done;
877 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
878 err = got_error_from_errno();
879 goto done;
882 err = got_lockfile_lock(&lf, path);
883 if (err)
884 goto done;
886 /* XXX: check if old content matches our expectations? */
888 if (unlink(path) != 0)
889 err = got_error_from_errno();
890 done:
891 if (lf)
892 unlock_err = got_lockfile_unlock(lf);
894 free(path_refs);
895 free(path);
896 return err ? err : unlock_err;