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"
38 #include "got_path.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"
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_prefix_errno("calloc");
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_prefix_errno("strdup");
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_prefix_errno("calloc");
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_prefix_errno("strdup");
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_prefix_errno("strdup");
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_prefix_errno("fclose");
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_prefix_errno("asprintf");
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_prefix_errno("asprintf");
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_prefix_errno("asprintf");
357 if (asprintf(&absname, "refs/%s%s%s",
358 subdir, subdir[0] ? "/" : "", name) == -1) {
359 err = got_error_prefix_errno("asprintf");
360 goto done;
364 normpath = got_path_normalize(path);
365 if (normpath == NULL) {
366 err = got_error_prefix_errno2("got_path_normalize", path);
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_prefix_errno2("get_refs_dir_path", refname);
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_prefix_errno(
412 "got_repo_get_path_packed_refs");
413 goto done;
416 f = fopen(packed_refs_path, "rb");
417 free(packed_refs_path);
418 if (f != NULL) {
419 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
420 refname);
421 if (fclose(f) != 0 && err == NULL)
422 err = got_error_prefix_errno("fclose");
423 if (err || *ref)
424 goto done;
428 err = open_ref(ref, path_refs, "", refname);
429 if (err)
430 goto done;
431 done:
432 if (*ref == NULL)
433 err = got_error_not_ref(refname);
434 free(path_refs);
435 return err;
438 void
439 got_ref_close(struct got_reference *ref)
441 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
442 free(ref->ref.symref.name);
443 free(ref->ref.symref.ref);
444 } else
445 free(ref->ref.ref.name);
446 free(ref);
449 struct got_reference *
450 got_ref_dup(struct got_reference *ref)
452 struct got_reference *ret;
454 ret = calloc(1, sizeof(*ret));
455 if (ret == NULL)
456 return NULL;
458 ret->flags = ref->flags;
459 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
460 ret->ref.symref.name = strdup(ref->ref.symref.name);
461 if (ret->ref.symref.name == NULL) {
462 free(ret);
463 return NULL;
465 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
466 if (ret->ref.symref.ref == NULL) {
467 free(ret->ref.symref.name);
468 free(ret);
469 return NULL;
471 } else {
472 ref->ref.ref.name = strdup(ref->ref.ref.name);
473 if (ref->ref.ref.name == NULL) {
474 free(ret);
475 return NULL;
477 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
478 sizeof(ret->ref.ref.sha1));
481 return ret;
484 static const struct got_error *
485 resolve_symbolic_ref(struct got_reference **resolved,
486 struct got_repository *repo, struct got_reference *ref)
488 struct got_reference *nextref;
489 const struct got_error *err;
491 err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
492 if (err)
493 return err;
495 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
496 err = resolve_symbolic_ref(resolved, repo, nextref);
497 else
498 *resolved = got_ref_dup(nextref);
500 got_ref_close(nextref);
501 return err;
504 const struct got_error *
505 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
506 struct got_reference *ref)
508 const struct got_error *err;
510 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
511 struct got_reference *resolved = NULL;
512 err = resolve_symbolic_ref(&resolved, repo, ref);
513 if (err == NULL)
514 err = got_ref_resolve(id, repo, resolved);
515 if (resolved)
516 got_ref_close(resolved);
517 return err;
520 *id = calloc(1, sizeof(**id));
521 if (*id == NULL)
522 return got_error_prefix_errno("calloc");
523 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
524 return NULL;
527 char *
528 got_ref_to_str(struct got_reference *ref)
530 char *str;
532 if (ref->flags & GOT_REF_IS_SYMBOLIC)
533 return strdup(ref->ref.symref.ref);
535 str = malloc(SHA1_DIGEST_STRING_LENGTH);
536 if (str == NULL)
537 return NULL;
539 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
540 SHA1_DIGEST_STRING_LENGTH) == NULL) {
541 free(str);
542 return NULL;
545 return str;
548 const char *
549 got_ref_get_name(struct got_reference *ref)
551 if (ref->flags & GOT_REF_IS_SYMBOLIC)
552 return ref->ref.symref.name;
554 return ref->ref.ref.name;
557 static const struct got_error *
558 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
559 struct got_reference *ref, struct got_repository *repo)
561 const struct got_error *err;
562 struct got_object_id *id;
563 struct got_reflist_entry *new, *re, *prev = NULL;
564 int cmp;
566 *newp = NULL;
568 err = got_ref_resolve(&id, repo, ref);
569 if (err)
570 return err;
572 new = malloc(sizeof(*new));
573 if (new == NULL) {
574 free(id);
575 return got_error_prefix_errno("malloc");
577 new->ref = ref;
578 new->id = id;
579 *newp = new;
581 /*
582 * We must de-duplicate entries on insert because packed-refs may
583 * contain redundant entries. On-disk refs take precedence.
584 * This code assumes that on-disk revs are read before packed-refs.
585 * We're iterating the list anyway, so insert elements sorted by name.
586 */
587 re = SIMPLEQ_FIRST(refs);
588 while (re) {
589 cmp = got_path_cmp(got_ref_get_name(re->ref),
590 got_ref_get_name(new->ref));
591 if (cmp == 0) {
592 /* duplicate */
593 free(new->id);
594 free(new);
595 *newp = NULL;
596 return NULL;
597 } else if (cmp > 0) {
598 if (prev)
599 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
600 else
601 SIMPLEQ_INSERT_HEAD(refs, new, entry);
602 return NULL;
603 } else {
604 prev = re;
605 re = SIMPLEQ_NEXT(re, entry);
609 SIMPLEQ_INSERT_TAIL(refs, new, entry);
610 return NULL;
613 static const struct got_error *
614 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
615 const char *subdir, struct got_repository *repo)
617 const struct got_error *err = NULL;
618 DIR *d = NULL;
619 char *path_subdir;
621 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
622 return got_error_prefix_errno("asprintf");
624 d = opendir(path_subdir);
625 if (d == NULL)
626 goto done;
628 while (1) {
629 struct dirent *dent;
630 struct got_reference *ref;
631 char *child;
633 dent = readdir(d);
634 if (dent == NULL)
635 break;
637 if (strcmp(dent->d_name, ".") == 0 ||
638 strcmp(dent->d_name, "..") == 0)
639 continue;
641 switch (dent->d_type) {
642 case DT_REG:
643 err = open_ref(&ref, path_refs, subdir, dent->d_name);
644 if (err)
645 goto done;
646 if (ref) {
647 struct got_reflist_entry *new;
648 err = insert_ref(&new, refs, ref, repo);
649 if (err || new == NULL /* duplicate */)
650 got_ref_close(ref);
651 if (err)
652 goto done;
654 break;
655 case DT_DIR:
656 if (asprintf(&child, "%s%s%s", subdir,
657 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
658 err = got_error_prefix_errno("asprintf");
659 break;
661 err = gather_on_disk_refs(refs, path_refs, child, repo);
662 free(child);
663 break;
664 default:
665 break;
668 done:
669 if (d)
670 closedir(d);
671 free(path_subdir);
672 return err;
675 const struct got_error *
676 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
678 const struct got_error *err;
679 char *packed_refs_path, *path_refs = NULL;
680 FILE *f = NULL;
681 struct got_reference *ref;
682 struct got_reflist_entry *new;
684 /* HEAD ref should always exist. */
685 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
686 if (path_refs == NULL) {
687 err = got_error_prefix_errno("get_refs_dir_path");
688 goto done;
690 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD);
691 if (err)
692 goto done;
693 err = insert_ref(&new, refs, ref, repo);
694 if (err || new == NULL /* duplicate */)
695 got_ref_close(ref);
696 if (err)
697 goto done;
699 /* Gather on-disk refs before parsing packed-refs. */
700 free(path_refs);
701 path_refs = get_refs_dir_path(repo, "");
702 if (path_refs == NULL) {
703 err = got_error_prefix_errno("get_refs_dir_path");
704 goto done;
706 err = gather_on_disk_refs(refs, path_refs, "", repo);
707 if (err)
708 goto done;
710 /*
711 * The packed-refs file may contain redundant entries, in which
712 * case on-disk refs take precedence.
713 */
714 packed_refs_path = got_repo_get_path_packed_refs(repo);
715 if (packed_refs_path == NULL) {
716 err = got_error_prefix_errno("got_repo_get_path_packed_refs");
717 goto done;
720 f = fopen(packed_refs_path, "r");
721 free(packed_refs_path);
722 if (f) {
723 char *line;
724 size_t len;
725 const char delim[3] = {'\0', '\0', '\0'};
726 while (1) {
727 line = fparseln(f, &len, NULL, delim, 0);
728 if (line == NULL) {
729 if (feof(f))
730 break;
731 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
732 goto done;
734 err = parse_packed_ref_line(&ref, NULL, line);
735 free(line);
736 if (err)
737 goto done;
738 if (ref) {
739 err = insert_ref(&new, refs, ref, repo);
740 if (err || new == NULL /* duplicate */)
741 got_ref_close(ref);
742 if (err)
743 goto done;
747 done:
748 free(path_refs);
749 if (f && fclose(f) != 0 && err == NULL)
750 err = got_error_prefix_errno("fclose");
751 return err;
754 void
755 got_ref_list_free(struct got_reflist_head *refs)
757 struct got_reflist_entry *re;
759 while (!SIMPLEQ_EMPTY(refs)) {
760 re = SIMPLEQ_FIRST(refs);
761 SIMPLEQ_REMOVE_HEAD(refs, entry);
762 got_ref_close(re->ref);
763 free(re->id);
764 free(re);
769 int
770 got_ref_is_symbolic(struct got_reference *ref)
772 return (ref->flags & GOT_REF_IS_SYMBOLIC);
775 const struct got_error *
776 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
778 if (ref->flags & GOT_REF_IS_SYMBOLIC)
779 return got_error(GOT_ERR_BAD_REF_TYPE);
781 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
782 return NULL;
785 const struct got_error *
786 got_ref_change_symref(struct got_reference *ref, char *refname)
788 char *new_name;
790 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
791 return got_error(GOT_ERR_BAD_REF_TYPE);
793 new_name = strdup(refname);
794 if (new_name == NULL)
795 return got_error_prefix_errno("strdup");
797 free(ref->ref.symref.name);
798 ref->ref.symref.name = new_name;
799 return NULL;
802 const struct got_error *
803 got_ref_write(struct got_reference *ref, struct got_repository *repo)
805 const struct got_error *err = NULL, *unlock_err = NULL;
806 const char *name = got_ref_get_name(ref);
807 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
808 struct got_lockfile *lf = NULL;
809 FILE *f = NULL;
810 size_t n;
811 struct stat sb;
813 path_refs = get_refs_dir_path(repo, name);
814 if (path_refs == NULL) {
815 err = got_error_prefix_errno2("get_refs_dir_path", name);
816 goto done;
819 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
820 err = got_error_prefix_errno("asprintf");
821 goto done;
824 err = got_opentemp_named(&tmppath, &f, path);
825 if (err) {
826 char *parent;
827 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
828 goto done;
829 err = got_path_dirname(&parent, path);
830 if (err)
831 goto done;
832 err = got_path_mkdir(parent);
833 free(parent);
834 if (err)
835 goto done;
836 err = got_opentemp_named(&tmppath, &f, path);
837 if (err)
838 goto done;
841 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
842 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
843 if (n != strlen(ref->ref.symref.ref) + 6) {
844 err = got_ferror(f, GOT_ERR_IO);
845 goto done;
847 } else {
848 char hex[SHA1_DIGEST_STRING_LENGTH];
849 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
850 sizeof(hex)) == NULL) {
851 err = got_error(GOT_ERR_BAD_REF_DATA);
852 goto done;
854 n = fprintf(f, "%s\n", hex);
855 if (n != sizeof(hex)) {
856 err = got_ferror(f, GOT_ERR_IO);
857 goto done;
861 err = got_lockfile_lock(&lf, path);
862 if (err)
863 goto done;
865 /* XXX: check if old content matches our expectations? */
867 if (stat(path, &sb) != 0) {
868 if (errno != ENOENT) {
869 err = got_error_prefix_errno2("stat", path);
870 goto done;
872 sb.st_mode = GOT_DEFAULT_FILE_MODE;
875 if (rename(tmppath, path) != 0) {
876 err = got_error_prefix_errno3("rename", tmppath, path);
877 goto done;
879 free(tmppath);
880 tmppath = NULL;
882 if (chmod(path, sb.st_mode) != 0) {
883 err = got_error_prefix_errno2("chmod", path);
884 goto done;
886 done:
887 if (lf)
888 unlock_err = got_lockfile_unlock(lf);
889 if (f) {
890 if (fclose(f) != 0 && err == NULL)
891 err = got_error_prefix_errno("fclose");
893 free(path_refs);
894 free(path);
895 if (tmppath) {
896 if (unlink(tmppath) != 0 && err == NULL)
897 err = got_error_prefix_errno2("unlink", tmppath);
898 free(tmppath);
900 return err ? err : unlock_err;
903 static const struct got_error *
904 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
906 const struct got_error *err = NULL, *unlock_err = NULL;
907 struct got_lockfile *lf = NULL;
908 FILE *f = NULL, *tmpf = NULL;
909 char *packed_refs_path, *tmppath = NULL;
910 struct got_reflist_head refs;
911 int found_delref = 0;
913 /* The packed-refs file does not cotain symbolic references. */
914 if (delref->flags & GOT_REF_IS_SYMBOLIC)
915 return got_error(GOT_ERR_BAD_REF_DATA);
917 SIMPLEQ_INIT(&refs);
919 packed_refs_path = got_repo_get_path_packed_refs(repo);
920 if (packed_refs_path == NULL)
921 return got_error_prefix_errno("got_repo_get_path_packed_refs");
923 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
924 if (err)
925 goto done;
927 err = got_lockfile_lock(&lf, packed_refs_path);
928 if (err)
929 goto done;
931 f = fopen(packed_refs_path, "r");
932 if (f == NULL) {
933 err = got_error_prefix_errno2("fopen", packed_refs_path);
934 goto done;
936 while (1) {
937 char *line;
938 size_t len;
939 const char delim[3] = {'\0', '\0', '\0'};
940 struct got_reference *ref;
941 struct got_reflist_entry *new;
943 line = fparseln(f, &len, NULL, delim, 0);
944 if (line == NULL) {
945 if (feof(f))
946 break;
947 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
948 goto done;
950 err = parse_packed_ref_line(&ref, NULL, line);
951 free(line);
952 if (err)
953 goto done;
954 if (ref == NULL)
955 continue;
957 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
958 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
959 sizeof(delref->ref.ref.sha1)) == 0) {
960 found_delref = 1;
961 got_ref_close(ref);
962 continue;
965 err = insert_ref(&new, &refs, ref, repo);
966 if (err || new == NULL /* duplicate */)
967 got_ref_close(ref);
968 if (err)
969 goto done;
972 if (found_delref) {
973 struct got_reflist_entry *re;
974 size_t n;
975 struct stat sb;
977 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
978 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
979 err = got_ferror(f, GOT_ERR_IO);
980 goto done;
983 SIMPLEQ_FOREACH(re, &refs, entry) {
984 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
986 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
987 sizeof(hex)) == NULL) {
988 err = got_error(GOT_ERR_BAD_REF_DATA);
989 goto done;
991 n = fprintf(tmpf, "%s ", hex);
992 if (n != sizeof(hex)) {
993 err = got_ferror(f, GOT_ERR_IO);
994 goto done;
996 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
997 if (n != strlen(re->ref->ref.ref.name) + 1) {
998 err = got_ferror(f, GOT_ERR_IO);
999 goto done;
1003 if (fflush(tmpf) != 0) {
1004 err = got_error_prefix_errno("fflush");
1005 goto done;
1008 if (stat(packed_refs_path, &sb) != 0) {
1009 if (errno != ENOENT) {
1010 err = got_error_prefix_errno2("stat",
1011 packed_refs_path);
1012 goto done;
1014 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1017 if (rename(tmppath, packed_refs_path) != 0) {
1018 err = got_error_prefix_errno3("rename", tmppath,
1019 packed_refs_path);
1020 goto done;
1023 if (chmod(packed_refs_path, sb.st_mode) != 0) {
1024 err = got_error_prefix_errno2("chmod",
1025 packed_refs_path);
1026 goto done;
1029 done:
1030 if (lf)
1031 unlock_err = got_lockfile_unlock(lf);
1032 if (f) {
1033 if (fclose(f) != 0 && err == NULL)
1034 err = got_error_prefix_errno("fclose");
1036 if (tmpf) {
1037 unlink(tmppath);
1038 if (fclose(tmpf) != 0 && err == NULL)
1039 err = got_error_prefix_errno("fclose");
1041 free(tmppath);
1042 free(packed_refs_path);
1043 got_ref_list_free(&refs);
1044 return err ? err : unlock_err;
1047 const struct got_error *
1048 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1050 const struct got_error *err = NULL, *unlock_err = NULL;
1051 const char *name = got_ref_get_name(ref);
1052 char *path_refs = NULL, *path = NULL;
1053 struct got_lockfile *lf = NULL;
1055 if (ref->flags & GOT_REF_IS_PACKED)
1056 return delete_packed_ref(ref, repo);
1058 path_refs = get_refs_dir_path(repo, name);
1059 if (path_refs == NULL) {
1060 err = got_error_prefix_errno2("get_refs_dir_path", name);
1061 goto done;
1064 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1065 err = got_error_prefix_errno("asprintf");
1066 goto done;
1069 err = got_lockfile_lock(&lf, path);
1070 if (err)
1071 goto done;
1073 /* XXX: check if old content matches our expectations? */
1075 if (unlink(path) != 0)
1076 err = got_error_prefix_errno2("unlink", path);
1077 done:
1078 if (lf)
1079 unlock_err = got_lockfile_unlock(lf);
1081 free(path_refs);
1082 free(path);
1083 return err ? err : unlock_err;