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 /*
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.
58 */
59 #define GOT_PACKED_REFS_HEADER "# pack-refs with:"
61 /* A symbolic reference. */
62 struct got_symref {
63 char *name;
64 char *ref;
65 };
67 /* A non-symbolic reference (there is no better designation). */
68 struct got_ref {
69 char *name;
70 u_int8_t sha1[SHA1_DIGEST_LENGTH];
71 };
73 /* A reference which points to an arbitrary object. */
74 struct got_reference {
75 unsigned int flags;
76 #define GOT_REF_IS_SYMBOLIC 0x01
77 #define GOT_REF_IS_PACKED 0x02
79 union {
80 struct got_ref ref;
81 struct got_symref symref;
82 } ref;
83 };
85 static const struct got_error *
86 alloc_ref(struct got_reference **ref, const char *name,
87 struct got_object_id *id, int flags)
88 {
89 const struct got_error *err = NULL;
91 *ref = calloc(1, sizeof(**ref));
92 if (*ref == NULL)
93 return got_error_from_errno();
95 memcpy((*ref)->ref.ref.sha1, id->sha1, sizeof((*ref)->ref.ref.sha1));
96 (*ref)->flags = flags;
97 (*ref)->ref.ref.name = strdup(name);
98 if ((*ref)->ref.ref.name == NULL) {
99 err = got_error_from_errno();
100 got_ref_close(*ref);
101 *ref = NULL;
103 return err;
106 static const struct got_error *
107 alloc_symref(struct got_reference **ref, const char *name,
108 const char *target_ref, int flags)
110 const struct got_error *err = NULL;
112 *ref = calloc(1, sizeof(**ref));
113 if (*ref == NULL)
114 return got_error_from_errno();
116 (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
117 (*ref)->ref.symref.name = strdup(name);
118 if ((*ref)->ref.symref.name == NULL) {
119 err = got_error_from_errno();
120 got_ref_close(*ref);
121 *ref = NULL;
123 (*ref)->ref.symref.ref = strdup(target_ref);
124 if ((*ref)->ref.symref.ref == NULL) {
125 err = got_error_from_errno();
126 got_ref_close(*ref);
127 *ref = NULL;
129 return err;
132 static const struct got_error *
133 parse_symref(struct got_reference **ref, const char *name, const char *line)
135 if (line[0] == '\0')
136 return got_error(GOT_ERR_BAD_REF_DATA);
138 return alloc_symref(ref, name, line, 0);
141 static const struct got_error *
142 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
144 struct got_object_id id;
146 if (strncmp(line, "ref: ", 5) == 0) {
147 line += 5;
148 return parse_symref(ref, name, line);
151 if (!got_parse_sha1_digest(id.sha1, line))
152 return got_error(GOT_ERR_BAD_REF_DATA);
154 return alloc_ref(ref, name, &id, 0);
157 static const struct got_error *
158 parse_ref_file(struct got_reference **ref, const char *name,
159 const char *abspath)
161 const struct got_error *err = NULL;
162 FILE *f;
163 char *line;
164 size_t len;
165 const char delim[3] = {'\0', '\0', '\0'};
167 f = fopen(abspath, "rb");
168 if (f == NULL)
169 return NULL;
171 line = fparseln(f, &len, NULL, delim, 0);
172 if (line == NULL) {
173 err = got_error(GOT_ERR_BAD_REF_DATA);
174 goto done;
177 err = parse_ref_line(ref, name, line);
178 done:
179 free(line);
180 if (fclose(f) != 0 && err == NULL)
181 err = got_error_from_errno();
182 return err;
185 static int
186 is_well_known_ref(const char *refname)
188 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
189 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
190 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
191 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
194 static char *
195 get_refs_dir_path(struct got_repository *repo, const char *refname)
197 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
198 return strdup(got_repo_get_path_git_dir(repo));
200 return got_repo_get_path_refs(repo);
203 static int
204 is_valid_ref_name(const char *name)
206 const char *s, *slash, *seg;
207 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
208 const char *forbidden_seq[] = { "//", "..", "@{" };
209 const char *lfs = GOT_LOCKFILE_SUFFIX;
210 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
211 int i;
213 if (name[0] == '@' && name[1] == '\0')
214 return 0;
216 slash = strchr(name, '/');
217 if (slash == NULL)
218 return 0;
220 s = name;
221 seg = s;
222 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
223 return 0;
224 while (*s) {
225 for (i = 0; i < nitems(forbidden); i++) {
226 if (*s == forbidden[i])
227 return 0;
229 for (i = 0; i < nitems(forbidden_seq); i++) {
230 if (s[0] == forbidden_seq[i][0] &&
231 s[1] == forbidden_seq[i][1])
232 return 0;
234 if (iscntrl((unsigned char)s[0]))
235 return 0;
236 if (s[0] == '.' && s[1] == '\0')
237 return 0;
238 if (*s == '/') {
239 const char *nextseg = s + 1;
240 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
241 nextseg[0] == '/')
242 return 0;
243 if (seg <= s - lfs_len &&
244 strncmp(s - lfs_len, lfs, lfs_len) == 0)
245 return 0;
246 seg = nextseg;
248 s++;
251 if (seg <= s - lfs_len &&
252 strncmp(s - lfs_len, lfs, lfs_len) == 0)
253 return 0;
255 return 1;
258 const struct got_error *
259 got_ref_alloc(struct got_reference **ref, const char *name,
260 struct got_object_id *id)
262 if (!is_valid_ref_name(name))
263 return got_error(GOT_ERR_BAD_REF_NAME);
265 return alloc_ref(ref, name, id, 0);
268 static const struct got_error *
269 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
270 const char *line)
272 struct got_object_id id;
273 const char *name;
275 *ref = NULL;
277 if (line[0] == '#' || line[0] == '^')
278 return NULL;
280 if (!got_parse_sha1_digest(id.sha1, line))
281 return got_error(GOT_ERR_BAD_REF_DATA);
283 if (abs_refname) {
284 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
285 return NULL;
286 name = abs_refname;
287 } else
288 name = line + SHA1_DIGEST_STRING_LENGTH;
290 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
293 static const struct got_error *
294 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
295 int nsubdirs, const char *refname)
297 const struct got_error *err = NULL;
298 char *abs_refname;
299 char *line;
300 size_t len;
301 const char delim[3] = {'\0', '\0', '\0'};
302 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
304 *ref = NULL;
306 if (ref_is_absolute)
307 abs_refname = (char *)refname;
308 do {
309 line = fparseln(f, &len, NULL, delim, 0);
310 if (line == NULL) {
311 if (feof(f))
312 break;
313 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
314 break;
316 for (i = 0; i < nsubdirs; i++) {
317 if (!ref_is_absolute &&
318 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
319 refname) == -1)
320 return got_error_from_errno();
321 err = parse_packed_ref_line(ref, abs_refname, line);
322 if (!ref_is_absolute)
323 free(abs_refname);
324 if (err || *ref != NULL)
325 break;
327 free(line);
328 if (err)
329 break;
330 } while (*ref == NULL);
332 return err;
335 static const struct got_error *
336 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
337 const char *name)
339 const struct got_error *err = NULL;
340 char *path = NULL;
341 char *normpath = NULL;
342 char *absname = NULL;
343 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
344 int ref_is_well_known = is_well_known_ref(name);
346 *ref = NULL;
348 if (ref_is_absolute || ref_is_well_known) {
349 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
350 return got_error_from_errno();
351 absname = (char *)name;
352 } else {
353 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
354 subdir[0] ? "/" : "", name) == -1)
355 return got_error_from_errno();
357 if (asprintf(&absname, "refs/%s%s%s",
358 subdir, subdir[0] ? "/" : "", name) == -1) {
359 err = got_error_from_errno();
360 goto done;
364 normpath = got_path_normalize(path);
365 if (normpath == NULL) {
366 err = got_error_from_errno();
367 goto done;
370 err = parse_ref_file(ref, absname, normpath);
371 done:
372 if (!ref_is_absolute && !ref_is_well_known)
373 free(absname);
374 free(path);
375 free(normpath);
376 return err;
379 const struct got_error *
380 got_ref_open(struct got_reference **ref, struct got_repository *repo,
381 const char *refname)
383 const struct got_error *err = NULL;
384 char *path_refs = NULL;
385 const char *subdirs[] = {
386 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
387 };
388 int i, well_known = is_well_known_ref(refname);
390 *ref = NULL;
392 path_refs = get_refs_dir_path(repo, refname);
393 if (path_refs == NULL) {
394 err = got_error_from_errno();
395 goto done;
398 if (!well_known) {
399 char *packed_refs_path;
400 FILE *f;
402 /* Search on-disk refs before packed refs! */
403 for (i = 0; i < nitems(subdirs); i++) {
404 err = open_ref(ref, path_refs, subdirs[i], refname);
405 if (err || *ref)
406 goto done;
409 packed_refs_path = got_repo_get_path_packed_refs(repo);
410 if (packed_refs_path == NULL) {
411 err = got_error_from_errno();
412 goto done;
415 f = fopen(packed_refs_path, "rb");
416 free(packed_refs_path);
417 if (f != NULL) {
418 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
419 refname);
420 if (fclose(f) != 0 && err == NULL)
421 err = got_error_from_errno();
422 if (err || *ref)
423 goto done;
427 err = open_ref(ref, path_refs, "", refname);
428 if (err)
429 goto done;
430 done:
431 if (*ref == NULL)
432 err = got_error_not_ref(refname);
433 free(path_refs);
434 return err;
437 void
438 got_ref_close(struct got_reference *ref)
440 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
441 free(ref->ref.symref.name);
442 free(ref->ref.symref.ref);
443 } else
444 free(ref->ref.ref.name);
445 free(ref);
448 struct got_reference *
449 got_ref_dup(struct got_reference *ref)
451 struct got_reference *ret;
453 ret = calloc(1, sizeof(*ret));
454 if (ret == NULL)
455 return NULL;
457 ret->flags = ref->flags;
458 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
459 ret->ref.symref.name = strdup(ref->ref.symref.name);
460 if (ret->ref.symref.name == NULL) {
461 free(ret);
462 return NULL;
464 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
465 if (ret->ref.symref.ref == NULL) {
466 free(ret->ref.symref.name);
467 free(ret);
468 return NULL;
470 } else {
471 ref->ref.ref.name = strdup(ref->ref.ref.name);
472 if (ref->ref.ref.name == NULL) {
473 free(ret);
474 return NULL;
476 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
477 sizeof(ret->ref.ref.sha1));
480 return ret;
483 static const struct got_error *
484 resolve_symbolic_ref(struct got_reference **resolved,
485 struct got_repository *repo, struct got_reference *ref)
487 struct got_reference *nextref;
488 const struct got_error *err;
490 err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
491 if (err)
492 return err;
494 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
495 err = resolve_symbolic_ref(resolved, repo, nextref);
496 else
497 *resolved = got_ref_dup(nextref);
499 got_ref_close(nextref);
500 return err;
503 const struct got_error *
504 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
505 struct got_reference *ref)
507 const struct got_error *err;
509 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
510 struct got_reference *resolved = NULL;
511 err = resolve_symbolic_ref(&resolved, repo, ref);
512 if (err == NULL)
513 err = got_ref_resolve(id, repo, resolved);
514 got_ref_close(resolved);
515 return err;
518 *id = calloc(1, sizeof(**id));
519 if (*id == NULL)
520 return got_error_from_errno();
521 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
522 return NULL;
525 char *
526 got_ref_to_str(struct got_reference *ref)
528 char *str;
530 if (ref->flags & GOT_REF_IS_SYMBOLIC)
531 return strdup(ref->ref.symref.ref);
533 str = malloc(SHA1_DIGEST_STRING_LENGTH);
534 if (str == NULL)
535 return NULL;
537 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
538 SHA1_DIGEST_STRING_LENGTH) == NULL) {
539 free(str);
540 return NULL;
543 return str;
546 const char *
547 got_ref_get_name(struct got_reference *ref)
549 if (ref->flags & GOT_REF_IS_SYMBOLIC)
550 return ref->ref.symref.name;
552 return ref->ref.ref.name;
555 static const struct got_error *
556 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
557 struct got_reference *ref, struct got_repository *repo)
559 const struct got_error *err;
560 struct got_object_id *id;
561 struct got_reflist_entry *new, *re, *prev = NULL;
562 int cmp;
564 *newp = NULL;
566 err = got_ref_resolve(&id, repo, ref);
567 if (err)
568 return err;
570 new = malloc(sizeof(*new));
571 if (new == NULL) {
572 free(id);
573 return got_error_from_errno();
575 new->ref = ref;
576 new->id = id;
577 *newp = new;
579 /*
580 * We must de-duplicate entries on insert because packed-refs may
581 * contain redundant entries. On-disk refs take precedence.
582 * This code assumes that on-disk revs are read before packed-refs.
583 * We're iterating the list anyway, so insert elements sorted by name.
584 */
585 re = SIMPLEQ_FIRST(refs);
586 while (re) {
587 cmp = got_path_cmp(got_ref_get_name(re->ref),
588 got_ref_get_name(new->ref));
589 if (cmp == 0) {
590 /* duplicate */
591 free(new->id);
592 free(new);
593 *newp = NULL;
594 return NULL;
595 } else if (cmp > 0) {
596 if (prev)
597 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
598 else
599 SIMPLEQ_INSERT_HEAD(refs, new, entry);
600 return NULL;
601 } else {
602 prev = re;
603 re = SIMPLEQ_NEXT(re, entry);
607 SIMPLEQ_INSERT_TAIL(refs, new, entry);
608 return NULL;
611 static const struct got_error *
612 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
613 const char *subdir, struct got_repository *repo)
615 const struct got_error *err = NULL;
616 DIR *d = NULL;
617 char *path_subdir;
619 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
620 return got_error_from_errno();
622 d = opendir(path_subdir);
623 if (d == NULL)
624 goto done;
626 while (1) {
627 struct dirent *dent;
628 struct got_reference *ref;
629 char *child;
631 dent = readdir(d);
632 if (dent == NULL)
633 break;
635 if (strcmp(dent->d_name, ".") == 0 ||
636 strcmp(dent->d_name, "..") == 0)
637 continue;
639 switch (dent->d_type) {
640 case DT_REG:
641 err = open_ref(&ref, path_refs, subdir, dent->d_name);
642 if (err)
643 goto done;
644 if (ref) {
645 struct got_reflist_entry *new;
646 err = insert_ref(&new, refs, ref, repo);
647 if (err || new == NULL /* duplicate */)
648 got_ref_close(ref);
649 if (err)
650 goto done;
652 break;
653 case DT_DIR:
654 if (asprintf(&child, "%s%s%s", subdir,
655 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
656 err = got_error_from_errno();
657 break;
659 err = gather_on_disk_refs(refs, path_refs, child, repo);
660 free(child);
661 break;
662 default:
663 break;
666 done:
667 if (d)
668 closedir(d);
669 free(path_subdir);
670 return err;
673 const struct got_error *
674 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
676 const struct got_error *err;
677 char *packed_refs_path, *path_refs = NULL;
678 FILE *f = NULL;
679 struct got_reference *ref;
680 struct got_reflist_entry *new;
682 /* HEAD ref should always exist. */
683 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
684 if (path_refs == NULL) {
685 err = got_error_from_errno();
686 goto done;
688 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD);
689 if (err)
690 goto done;
691 err = insert_ref(&new, refs, ref, repo);
692 if (err || new == NULL /* duplicate */)
693 got_ref_close(ref);
694 if (err)
695 goto done;
697 /* Gather on-disk refs before parsing packed-refs. */
698 free(path_refs);
699 path_refs = get_refs_dir_path(repo, "");
700 if (path_refs == NULL) {
701 err = got_error_from_errno();
702 goto done;
704 err = gather_on_disk_refs(refs, path_refs, "", repo);
705 if (err)
706 goto done;
708 /*
709 * The packed-refs file may contain redundant entries, in which
710 * case on-disk refs take precedence.
711 */
712 packed_refs_path = got_repo_get_path_packed_refs(repo);
713 if (packed_refs_path == NULL) {
714 err = got_error_from_errno();
715 goto done;
718 f = fopen(packed_refs_path, "r");
719 free(packed_refs_path);
720 if (f) {
721 char *line;
722 size_t len;
723 const char delim[3] = {'\0', '\0', '\0'};
724 while (1) {
725 line = fparseln(f, &len, NULL, delim, 0);
726 if (line == NULL) {
727 if (feof(f))
728 break;
729 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
730 goto done;
732 err = parse_packed_ref_line(&ref, NULL, line);
733 free(line);
734 if (err)
735 goto done;
736 if (ref) {
737 err = insert_ref(&new, refs, ref, repo);
738 if (err || new == NULL /* duplicate */)
739 got_ref_close(ref);
740 if (err)
741 goto done;
745 done:
746 free(path_refs);
747 if (f && fclose(f) != 0 && err == NULL)
748 err = got_error_from_errno();
749 return err;
752 void
753 got_ref_list_free(struct got_reflist_head *refs)
755 struct got_reflist_entry *re;
757 while (!SIMPLEQ_EMPTY(refs)) {
758 re = SIMPLEQ_FIRST(refs);
759 SIMPLEQ_REMOVE_HEAD(refs, entry);
760 got_ref_close(re->ref);
761 free(re->id);
762 free(re);
767 const struct got_error *
768 got_ref_write(struct got_reference *ref, struct got_repository *repo)
770 const struct got_error *err = NULL, *unlock_err = NULL;
771 const char *name = got_ref_get_name(ref);
772 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
773 struct got_lockfile *lf = NULL;
774 FILE *f = NULL;
775 size_t n;
776 struct stat sb;
778 path_refs = get_refs_dir_path(repo, name);
779 if (path_refs == NULL) {
780 err = got_error_from_errno();
781 goto done;
784 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
785 err = got_error_from_errno();
786 goto done;
789 err = got_opentemp_named(&tmppath, &f, path);
790 if (err) {
791 char *parent;
792 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
793 goto done;
794 err = got_path_dirname(&parent, path);
795 if (err)
796 goto done;
797 err = got_path_mkdir(parent);
798 free(parent);
799 if (err)
800 goto done;
801 err = got_opentemp_named(&tmppath, &f, path);
802 if (err)
803 goto done;
806 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
807 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
808 if (n != strlen(ref->ref.symref.ref) + 6) {
809 err = got_ferror(f, GOT_ERR_IO);
810 goto done;
812 } else {
813 char hex[SHA1_DIGEST_STRING_LENGTH];
814 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
815 sizeof(hex)) == NULL) {
816 err = got_error(GOT_ERR_BAD_REF_DATA);
817 goto done;
819 n = fprintf(f, "%s\n", hex);
820 if (n != sizeof(hex)) {
821 err = got_ferror(f, GOT_ERR_IO);
822 goto done;
826 err = got_lockfile_lock(&lf, path);
827 if (err)
828 goto done;
830 /* XXX: check if old content matches our expectations? */
832 if (stat(path, &sb) != 0) {
833 if (errno != ENOENT) {
834 err = got_error_from_errno();
835 goto done;
837 sb.st_mode = GOT_DEFAULT_FILE_MODE;
840 if (rename(tmppath, path) != 0) {
841 err = got_error_from_errno();
842 goto done;
844 free(tmppath);
845 tmppath = NULL;
847 if (chmod(path, sb.st_mode) != 0) {
848 err = got_error_from_errno();
849 goto done;
851 done:
852 if (lf)
853 unlock_err = got_lockfile_unlock(lf);
854 if (f) {
855 if (fclose(f) != 0 && err == NULL)
856 err = got_error_from_errno();
858 free(path_refs);
859 free(path);
860 if (tmppath) {
861 if (unlink(tmppath) != 0 && err == NULL)
862 err = got_error_from_errno();
863 free(tmppath);
865 return err ? err : unlock_err;
868 static const struct got_error *
869 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
871 const struct got_error *err = NULL, *unlock_err = NULL;
872 struct got_lockfile *lf = NULL;
873 FILE *f = NULL, *tmpf = NULL;
874 char *packed_refs_path, *tmppath = NULL;
875 struct got_reflist_head refs;
876 int found_delref = 0;
878 /* The packed-refs file does not cotain symbolic references. */
879 if (delref->flags & GOT_REF_IS_SYMBOLIC)
880 return got_error(GOT_ERR_BAD_REF_DATA);
882 SIMPLEQ_INIT(&refs);
884 packed_refs_path = got_repo_get_path_packed_refs(repo);
885 if (packed_refs_path == NULL)
886 return got_error_from_errno();
888 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
889 if (err)
890 goto done;
892 err = got_lockfile_lock(&lf, packed_refs_path);
893 if (err)
894 goto done;
896 f = fopen(packed_refs_path, "r");
897 if (f == NULL) {
898 err = got_error_from_errno();
899 goto done;
901 while (1) {
902 char *line;
903 size_t len;
904 const char delim[3] = {'\0', '\0', '\0'};
905 struct got_reference *ref;
906 struct got_reflist_entry *new;
908 line = fparseln(f, &len, NULL, delim, 0);
909 if (line == NULL) {
910 if (feof(f))
911 break;
912 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
913 goto done;
915 err = parse_packed_ref_line(&ref, NULL, line);
916 free(line);
917 if (err)
918 goto done;
919 if (ref == NULL)
920 continue;
922 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
923 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
924 sizeof(delref->ref.ref.sha1)) == 0) {
925 found_delref = 1;
926 got_ref_close(ref);
927 continue;
930 err = insert_ref(&new, &refs, ref, repo);
931 if (err || new == NULL /* duplicate */)
932 got_ref_close(ref);
933 if (err)
934 goto done;
937 if (found_delref) {
938 struct got_reflist_entry *re;
939 size_t n;
940 struct stat sb;
942 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
943 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
944 err = got_ferror(f, GOT_ERR_IO);
945 goto done;
948 SIMPLEQ_FOREACH(re, &refs, entry) {
949 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
951 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
952 sizeof(hex)) == NULL) {
953 err = got_error(GOT_ERR_BAD_REF_DATA);
954 goto done;
956 n = fprintf(tmpf, "%s ", hex);
957 if (n != sizeof(hex)) {
958 err = got_ferror(f, GOT_ERR_IO);
959 goto done;
961 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
962 if (n != strlen(re->ref->ref.ref.name) + 1) {
963 err = got_ferror(f, GOT_ERR_IO);
964 goto done;
968 if (fflush(tmpf) != 0) {
969 err = got_error_from_errno();
970 goto done;
973 if (stat(packed_refs_path, &sb) != 0) {
974 if (errno != ENOENT) {
975 err = got_error_from_errno();
976 goto done;
978 sb.st_mode = GOT_DEFAULT_FILE_MODE;
981 if (rename(tmppath, packed_refs_path) != 0) {
982 err = got_error_from_errno();
983 goto done;
986 if (chmod(packed_refs_path, sb.st_mode) != 0) {
987 err = got_error_from_errno();
988 goto done;
991 done:
992 if (lf)
993 unlock_err = got_lockfile_unlock(lf);
994 if (f) {
995 if (fclose(f) != 0 && err == NULL)
996 err = got_error_from_errno();
998 if (tmpf) {
999 unlink(tmppath);
1000 if (fclose(tmpf) != 0 && err == NULL)
1001 err = got_error_from_errno();
1003 free(tmppath);
1004 free(packed_refs_path);
1005 got_ref_list_free(&refs);
1006 return err ? err : unlock_err;
1009 const struct got_error *
1010 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1012 const struct got_error *err = NULL, *unlock_err = NULL;
1013 const char *name = got_ref_get_name(ref);
1014 char *path_refs = NULL, *path = NULL;
1015 struct got_lockfile *lf = NULL;
1017 if (ref->flags & GOT_REF_IS_PACKED)
1018 return delete_packed_ref(ref, repo);
1020 path_refs = get_refs_dir_path(repo, name);
1021 if (path_refs == NULL) {
1022 err = got_error_from_errno();
1023 goto done;
1026 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1027 err = got_error_from_errno();
1028 goto done;
1031 err = got_lockfile_lock(&lf, path);
1032 if (err)
1033 goto done;
1035 /* XXX: check if old content matches our expectations? */
1037 if (unlink(path) != 0)
1038 err = got_error_from_errno();
1039 done:
1040 if (lf)
1041 unlock_err = got_lockfile_unlock(lf);
1043 free(path_refs);
1044 free(path);
1045 return err ? err : unlock_err;