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;
84 struct got_lockfile *lf;
85 };
87 static const struct got_error *
88 alloc_ref(struct got_reference **ref, const char *name,
89 struct got_object_id *id, int flags)
90 {
91 const struct got_error *err = NULL;
93 *ref = calloc(1, sizeof(**ref));
94 if (*ref == NULL)
95 return got_error_from_errno("calloc");
97 memcpy((*ref)->ref.ref.sha1, id->sha1, sizeof((*ref)->ref.ref.sha1));
98 (*ref)->flags = flags;
99 (*ref)->ref.ref.name = strdup(name);
100 if ((*ref)->ref.ref.name == NULL) {
101 err = got_error_from_errno("strdup");
102 got_ref_close(*ref);
103 *ref = NULL;
105 return err;
108 static const struct got_error *
109 alloc_symref(struct got_reference **ref, const char *name,
110 const char *target_ref, int flags)
112 const struct got_error *err = NULL;
114 *ref = calloc(1, sizeof(**ref));
115 if (*ref == NULL)
116 return got_error_from_errno("calloc");
118 (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
119 (*ref)->ref.symref.name = strdup(name);
120 if ((*ref)->ref.symref.name == NULL) {
121 err = got_error_from_errno("strdup");
122 got_ref_close(*ref);
123 *ref = NULL;
125 (*ref)->ref.symref.ref = strdup(target_ref);
126 if ((*ref)->ref.symref.ref == NULL) {
127 err = got_error_from_errno("strdup");
128 got_ref_close(*ref);
129 *ref = NULL;
131 return err;
134 static const struct got_error *
135 parse_symref(struct got_reference **ref, const char *name, const char *line)
137 if (line[0] == '\0')
138 return got_error(GOT_ERR_BAD_REF_DATA);
140 return alloc_symref(ref, name, line, 0);
143 static const struct got_error *
144 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
146 struct got_object_id id;
148 if (strncmp(line, "ref: ", 5) == 0) {
149 line += 5;
150 return parse_symref(ref, name, line);
153 if (!got_parse_sha1_digest(id.sha1, line))
154 return got_error(GOT_ERR_BAD_REF_DATA);
156 return alloc_ref(ref, name, &id, 0);
159 static const struct got_error *
160 parse_ref_file(struct got_reference **ref, const char *name,
161 const char *abspath, int lock)
163 const struct got_error *err = NULL;
164 FILE *f;
165 char *line;
166 size_t len;
167 const char delim[3] = {'\0', '\0', '\0'};
168 struct got_lockfile *lf = NULL;
170 if (lock) {
171 err = got_lockfile_lock(&lf, abspath);
172 if (err)
173 return (err);
176 f = fopen(abspath, "rb");
177 if (f == NULL) {
178 if (lock)
179 got_lockfile_unlock(lf);
180 return NULL;
183 line = fparseln(f, &len, NULL, delim, 0);
184 if (line == NULL) {
185 err = got_error(GOT_ERR_BAD_REF_DATA);
186 if (lock)
187 got_lockfile_unlock(lf);
188 goto done;
191 err = parse_ref_line(ref, name, line);
192 if (lock) {
193 if (err)
194 got_lockfile_unlock(lf);
195 else {
196 if (*ref)
197 (*ref)->lf = lf;
198 else
199 got_lockfile_unlock(lf);
202 done:
203 free(line);
204 if (fclose(f) != 0 && err == NULL) {
205 err = got_error_from_errno("fclose");
206 if (*ref) {
207 if (lock)
208 got_ref_unlock(*ref);
209 got_ref_close(*ref);
210 *ref = NULL;
213 return err;
216 static int
217 is_well_known_ref(const char *refname)
219 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
220 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
221 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
222 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
225 static char *
226 get_refs_dir_path(struct got_repository *repo, const char *refname)
228 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
229 return strdup(got_repo_get_path_git_dir(repo));
231 return got_repo_get_path_refs(repo);
234 static int
235 is_valid_ref_name(const char *name)
237 const char *s, *slash, *seg;
238 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
239 const char *forbidden_seq[] = { "//", "..", "@{" };
240 const char *lfs = GOT_LOCKFILE_SUFFIX;
241 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
242 int i;
244 if (name[0] == '@' && name[1] == '\0')
245 return 0;
247 slash = strchr(name, '/');
248 if (slash == NULL)
249 return 0;
251 s = name;
252 seg = s;
253 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
254 return 0;
255 while (*s) {
256 for (i = 0; i < nitems(forbidden); i++) {
257 if (*s == forbidden[i])
258 return 0;
260 for (i = 0; i < nitems(forbidden_seq); i++) {
261 if (s[0] == forbidden_seq[i][0] &&
262 s[1] == forbidden_seq[i][1])
263 return 0;
265 if (iscntrl((unsigned char)s[0]))
266 return 0;
267 if (s[0] == '.' && s[1] == '\0')
268 return 0;
269 if (*s == '/') {
270 const char *nextseg = s + 1;
271 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
272 nextseg[0] == '/')
273 return 0;
274 if (seg <= s - lfs_len &&
275 strncmp(s - lfs_len, lfs, lfs_len) == 0)
276 return 0;
277 seg = nextseg;
279 s++;
282 if (seg <= s - lfs_len &&
283 strncmp(s - lfs_len, lfs, lfs_len) == 0)
284 return 0;
286 return 1;
289 const struct got_error *
290 got_ref_alloc(struct got_reference **ref, const char *name,
291 struct got_object_id *id)
293 if (!is_valid_ref_name(name))
294 return got_error(GOT_ERR_BAD_REF_NAME);
296 return alloc_ref(ref, name, id, 0);
299 const struct got_error *
300 got_ref_alloc_symref(struct got_reference **ref, const char *name,
301 struct got_reference *target_ref)
303 if (!is_valid_ref_name(name))
304 return got_error(GOT_ERR_BAD_REF_NAME);
306 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
309 static const struct got_error *
310 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
311 const char *line)
313 struct got_object_id id;
314 const char *name;
316 *ref = NULL;
318 if (line[0] == '#' || line[0] == '^')
319 return NULL;
321 if (!got_parse_sha1_digest(id.sha1, line))
322 return got_error(GOT_ERR_BAD_REF_DATA);
324 if (abs_refname) {
325 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
326 return NULL;
327 name = abs_refname;
328 } else
329 name = line + SHA1_DIGEST_STRING_LENGTH;
331 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
334 static const struct got_error *
335 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
336 int nsubdirs, const char *refname)
338 const struct got_error *err = NULL;
339 char *abs_refname;
340 char *line;
341 size_t len;
342 const char delim[3] = {'\0', '\0', '\0'};
343 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
345 *ref = NULL;
347 if (ref_is_absolute)
348 abs_refname = (char *)refname;
349 do {
350 line = fparseln(f, &len, NULL, delim, 0);
351 if (line == NULL) {
352 if (feof(f))
353 break;
354 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
355 break;
357 for (i = 0; i < nsubdirs; i++) {
358 if (!ref_is_absolute &&
359 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
360 refname) == -1)
361 return got_error_from_errno("asprintf");
362 err = parse_packed_ref_line(ref, abs_refname, line);
363 if (!ref_is_absolute)
364 free(abs_refname);
365 if (err || *ref != NULL)
366 break;
368 free(line);
369 if (err)
370 break;
371 } while (*ref == NULL);
373 return err;
376 static const struct got_error *
377 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
378 const char *name, int lock)
380 const struct got_error *err = NULL;
381 char *path = NULL;
382 char *absname = NULL;
383 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
384 int ref_is_well_known = is_well_known_ref(name);
386 *ref = NULL;
388 if (ref_is_absolute || ref_is_well_known) {
389 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
390 return got_error_from_errno("asprintf");
391 absname = (char *)name;
392 } else {
393 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
394 subdir[0] ? "/" : "", name) == -1)
395 return got_error_from_errno("asprintf");
397 if (asprintf(&absname, "refs/%s%s%s",
398 subdir, subdir[0] ? "/" : "", name) == -1) {
399 err = got_error_from_errno("asprintf");
400 goto done;
404 err = parse_ref_file(ref, absname, path, lock);
405 done:
406 if (!ref_is_absolute && !ref_is_well_known)
407 free(absname);
408 free(path);
409 return err;
412 const struct got_error *
413 got_ref_open(struct got_reference **ref, struct got_repository *repo,
414 const char *refname, int lock)
416 const struct got_error *err = NULL;
417 char *path_refs = NULL;
418 const char *subdirs[] = {
419 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
420 };
421 int i, well_known = is_well_known_ref(refname);
422 struct got_lockfile *lf = NULL;
424 *ref = NULL;
426 path_refs = get_refs_dir_path(repo, refname);
427 if (path_refs == NULL) {
428 err = got_error_from_errno2("get_refs_dir_path", refname);
429 goto done;
432 if (well_known) {
433 err = open_ref(ref, path_refs, "", refname, lock);
434 } else {
435 char *packed_refs_path;
436 FILE *f;
438 /* Search on-disk refs before packed refs! */
439 for (i = 0; i < nitems(subdirs); i++) {
440 err = open_ref(ref, path_refs, subdirs[i], refname,
441 lock);
442 if (err || *ref)
443 goto done;
446 packed_refs_path = got_repo_get_path_packed_refs(repo);
447 if (packed_refs_path == NULL) {
448 err = got_error_from_errno(
449 "got_repo_get_path_packed_refs");
450 goto done;
453 if (lock) {
454 err = got_lockfile_lock(&lf, packed_refs_path);
455 if (err)
456 goto done;
458 f = fopen(packed_refs_path, "rb");
459 free(packed_refs_path);
460 if (f != NULL) {
461 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
462 refname);
463 if (!err) {
464 if (fclose(f) != 0) {
465 err = got_error_from_errno("fclose");
466 got_ref_close(*ref);
467 *ref = NULL;
468 } else if (*ref)
469 (*ref)->lf = lf;
473 done:
474 if (!err && *ref == NULL)
475 err = got_error_not_ref(refname);
476 if (err && lf)
477 got_lockfile_unlock(lf);
478 free(path_refs);
479 return err;
482 void
483 got_ref_close(struct got_reference *ref)
485 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
486 free(ref->ref.symref.name);
487 free(ref->ref.symref.ref);
488 } else
489 free(ref->ref.ref.name);
490 free(ref);
493 struct got_reference *
494 got_ref_dup(struct got_reference *ref)
496 struct got_reference *ret;
498 ret = calloc(1, sizeof(*ret));
499 if (ret == NULL)
500 return NULL;
502 ret->flags = ref->flags;
503 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
504 ret->ref.symref.name = strdup(ref->ref.symref.name);
505 if (ret->ref.symref.name == NULL) {
506 free(ret);
507 return NULL;
509 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
510 if (ret->ref.symref.ref == NULL) {
511 free(ret->ref.symref.name);
512 free(ret);
513 return NULL;
515 } else {
516 ref->ref.ref.name = strdup(ref->ref.ref.name);
517 if (ref->ref.ref.name == NULL) {
518 free(ret);
519 return NULL;
521 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
522 sizeof(ret->ref.ref.sha1));
525 return ret;
528 static const struct got_error *
529 resolve_symbolic_ref(struct got_reference **resolved,
530 struct got_repository *repo, struct got_reference *ref)
532 struct got_reference *nextref;
533 const struct got_error *err;
535 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
536 if (err)
537 return err;
539 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
540 err = resolve_symbolic_ref(resolved, repo, nextref);
541 else
542 *resolved = got_ref_dup(nextref);
544 got_ref_close(nextref);
545 return err;
548 const struct got_error *
549 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
550 struct got_reference *ref)
552 const struct got_error *err;
554 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
555 struct got_reference *resolved = NULL;
556 err = resolve_symbolic_ref(&resolved, repo, ref);
557 if (err == NULL)
558 err = got_ref_resolve(id, repo, resolved);
559 if (resolved)
560 got_ref_close(resolved);
561 return err;
564 *id = calloc(1, sizeof(**id));
565 if (*id == NULL)
566 return got_error_from_errno("calloc");
567 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
568 return NULL;
571 char *
572 got_ref_to_str(struct got_reference *ref)
574 char *str;
576 if (ref->flags & GOT_REF_IS_SYMBOLIC)
577 return strdup(ref->ref.symref.ref);
579 str = malloc(SHA1_DIGEST_STRING_LENGTH);
580 if (str == NULL)
581 return NULL;
583 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
584 SHA1_DIGEST_STRING_LENGTH) == NULL) {
585 free(str);
586 return NULL;
589 return str;
592 const char *
593 got_ref_get_name(struct got_reference *ref)
595 if (ref->flags & GOT_REF_IS_SYMBOLIC)
596 return ref->ref.symref.name;
598 return ref->ref.ref.name;
601 const char *
602 got_ref_get_symref_target(struct got_reference *ref)
604 if (ref->flags & GOT_REF_IS_SYMBOLIC)
605 return ref->ref.symref.ref;
607 return NULL;
610 static const struct got_error *
611 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
612 struct got_reference *ref, struct got_repository *repo)
614 const struct got_error *err;
615 struct got_object_id *id;
616 struct got_reflist_entry *new, *re, *prev = NULL;
617 int cmp;
619 *newp = NULL;
621 err = got_ref_resolve(&id, repo, ref);
622 if (err)
623 return err;
625 new = malloc(sizeof(*new));
626 if (new == NULL) {
627 free(id);
628 return got_error_from_errno("malloc");
630 new->ref = ref;
631 new->id = id;
632 *newp = new;
634 /*
635 * We must de-duplicate entries on insert because packed-refs may
636 * contain redundant entries. On-disk refs take precedence.
637 * This code assumes that on-disk revs are read before packed-refs.
638 * We're iterating the list anyway, so insert elements sorted by name.
639 */
640 re = SIMPLEQ_FIRST(refs);
641 while (re) {
642 const char *name = got_ref_get_name(re->ref);
643 const char *new_name = got_ref_get_name(new->ref);
644 cmp = got_path_cmp(name, new_name, strlen(name),
645 strlen(new_name));
646 if (cmp == 0) {
647 /* duplicate */
648 free(new->id);
649 free(new);
650 *newp = NULL;
651 return NULL;
652 } else if (cmp > 0) {
653 if (prev)
654 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
655 else
656 SIMPLEQ_INSERT_HEAD(refs, new, entry);
657 return NULL;
658 } else {
659 prev = re;
660 re = SIMPLEQ_NEXT(re, entry);
664 SIMPLEQ_INSERT_TAIL(refs, new, entry);
665 return NULL;
668 static const struct got_error *
669 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
670 const char *subdir, struct got_repository *repo)
672 const struct got_error *err = NULL;
673 DIR *d = NULL;
674 char *path_subdir;
676 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
677 return got_error_from_errno("asprintf");
679 d = opendir(path_subdir);
680 if (d == NULL)
681 goto done;
683 for (;;) {
684 struct dirent *dent;
685 struct got_reference *ref;
686 char *child;
688 dent = readdir(d);
689 if (dent == NULL)
690 break;
692 if (strcmp(dent->d_name, ".") == 0 ||
693 strcmp(dent->d_name, "..") == 0)
694 continue;
696 switch (dent->d_type) {
697 case DT_REG:
698 err = open_ref(&ref, path_refs, subdir, dent->d_name,
699 0);
700 if (err)
701 goto done;
702 if (ref) {
703 struct got_reflist_entry *new;
704 err = insert_ref(&new, refs, ref, repo);
705 if (err || new == NULL /* duplicate */)
706 got_ref_close(ref);
707 if (err)
708 goto done;
710 break;
711 case DT_DIR:
712 if (asprintf(&child, "%s%s%s", subdir,
713 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
714 err = got_error_from_errno("asprintf");
715 break;
717 err = gather_on_disk_refs(refs, path_refs, child, repo);
718 free(child);
719 break;
720 default:
721 break;
724 done:
725 if (d)
726 closedir(d);
727 free(path_subdir);
728 return err;
731 const struct got_error *
732 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
734 const struct got_error *err;
735 char *packed_refs_path, *path_refs = NULL;
736 FILE *f = NULL;
737 struct got_reference *ref;
738 struct got_reflist_entry *new;
740 /* HEAD ref should always exist. */
741 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
742 if (path_refs == NULL) {
743 err = got_error_from_errno("get_refs_dir_path");
744 goto done;
746 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
747 if (err)
748 goto done;
749 err = insert_ref(&new, refs, ref, repo);
750 if (err || new == NULL /* duplicate */)
751 got_ref_close(ref);
752 if (err)
753 goto done;
755 /* Gather on-disk refs before parsing packed-refs. */
756 free(path_refs);
757 path_refs = get_refs_dir_path(repo, "");
758 if (path_refs == NULL) {
759 err = got_error_from_errno("get_refs_dir_path");
760 goto done;
762 err = gather_on_disk_refs(refs, path_refs, "", repo);
763 if (err)
764 goto done;
766 /*
767 * The packed-refs file may contain redundant entries, in which
768 * case on-disk refs take precedence.
769 */
770 packed_refs_path = got_repo_get_path_packed_refs(repo);
771 if (packed_refs_path == NULL) {
772 err = got_error_from_errno("got_repo_get_path_packed_refs");
773 goto done;
776 f = fopen(packed_refs_path, "r");
777 free(packed_refs_path);
778 if (f) {
779 char *line;
780 size_t len;
781 const char delim[3] = {'\0', '\0', '\0'};
782 for (;;) {
783 line = fparseln(f, &len, NULL, delim, 0);
784 if (line == NULL) {
785 if (feof(f))
786 break;
787 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
788 goto done;
790 err = parse_packed_ref_line(&ref, NULL, line);
791 free(line);
792 if (err)
793 goto done;
794 if (ref) {
795 err = insert_ref(&new, refs, ref, repo);
796 if (err || new == NULL /* duplicate */)
797 got_ref_close(ref);
798 if (err)
799 goto done;
803 done:
804 free(path_refs);
805 if (f && fclose(f) != 0 && err == NULL)
806 err = got_error_from_errno("fclose");
807 return err;
810 void
811 got_ref_list_free(struct got_reflist_head *refs)
813 struct got_reflist_entry *re;
815 while (!SIMPLEQ_EMPTY(refs)) {
816 re = SIMPLEQ_FIRST(refs);
817 SIMPLEQ_REMOVE_HEAD(refs, entry);
818 got_ref_close(re->ref);
819 free(re->id);
820 free(re);
825 int
826 got_ref_is_symbolic(struct got_reference *ref)
828 return (ref->flags & GOT_REF_IS_SYMBOLIC);
831 const struct got_error *
832 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
834 if (ref->flags & GOT_REF_IS_SYMBOLIC)
835 return got_error(GOT_ERR_BAD_REF_TYPE);
837 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
838 return NULL;
841 const struct got_error *
842 got_ref_change_symref(struct got_reference *ref, char *refname)
844 char *new_name;
846 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
847 return got_error(GOT_ERR_BAD_REF_TYPE);
849 new_name = strdup(refname);
850 if (new_name == NULL)
851 return got_error_from_errno("strdup");
853 free(ref->ref.symref.name);
854 ref->ref.symref.name = new_name;
855 return NULL;
858 const struct got_error *
859 got_ref_write(struct got_reference *ref, struct got_repository *repo)
861 const struct got_error *err = NULL, *unlock_err = NULL;
862 const char *name = got_ref_get_name(ref);
863 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
864 struct got_lockfile *lf = NULL;
865 FILE *f = NULL;
866 size_t n;
867 struct stat sb;
869 path_refs = get_refs_dir_path(repo, name);
870 if (path_refs == NULL) {
871 err = got_error_from_errno2("get_refs_dir_path", name);
872 goto done;
875 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
876 err = got_error_from_errno("asprintf");
877 goto done;
880 err = got_opentemp_named(&tmppath, &f, path);
881 if (err) {
882 char *parent;
883 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
884 goto done;
885 err = got_path_dirname(&parent, path);
886 if (err)
887 goto done;
888 err = got_path_mkdir(parent);
889 free(parent);
890 if (err)
891 goto done;
892 err = got_opentemp_named(&tmppath, &f, path);
893 if (err)
894 goto done;
897 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
898 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
899 if (n != strlen(ref->ref.symref.ref) + 6) {
900 err = got_ferror(f, GOT_ERR_IO);
901 goto done;
903 } else {
904 char hex[SHA1_DIGEST_STRING_LENGTH];
905 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
906 sizeof(hex)) == NULL) {
907 err = got_error(GOT_ERR_BAD_REF_DATA);
908 goto done;
910 n = fprintf(f, "%s\n", hex);
911 if (n != sizeof(hex)) {
912 err = got_ferror(f, GOT_ERR_IO);
913 goto done;
917 if (ref->lf == NULL) {
918 err = got_lockfile_lock(&lf, path);
919 if (err)
920 goto done;
923 /* XXX: check if old content matches our expectations? */
925 if (stat(path, &sb) != 0) {
926 if (errno != ENOENT) {
927 err = got_error_from_errno2("stat", path);
928 goto done;
930 sb.st_mode = GOT_DEFAULT_FILE_MODE;
933 if (rename(tmppath, path) != 0) {
934 err = got_error_from_errno3("rename", tmppath, path);
935 goto done;
937 free(tmppath);
938 tmppath = NULL;
940 if (chmod(path, sb.st_mode) != 0) {
941 err = got_error_from_errno2("chmod", path);
942 goto done;
944 done:
945 if (ref->lf == NULL && lf)
946 unlock_err = got_lockfile_unlock(lf);
947 if (f) {
948 if (fclose(f) != 0 && err == NULL)
949 err = got_error_from_errno("fclose");
951 free(path_refs);
952 free(path);
953 if (tmppath) {
954 if (unlink(tmppath) != 0 && err == NULL)
955 err = got_error_from_errno2("unlink", tmppath);
956 free(tmppath);
958 return err ? err : unlock_err;
961 static const struct got_error *
962 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
964 const struct got_error *err = NULL, *unlock_err = NULL;
965 struct got_lockfile *lf = NULL;
966 FILE *f = NULL, *tmpf = NULL;
967 char *packed_refs_path, *tmppath = NULL;
968 struct got_reflist_head refs;
969 int found_delref = 0;
971 /* The packed-refs file does not cotain symbolic references. */
972 if (delref->flags & GOT_REF_IS_SYMBOLIC)
973 return got_error(GOT_ERR_BAD_REF_DATA);
975 SIMPLEQ_INIT(&refs);
977 packed_refs_path = got_repo_get_path_packed_refs(repo);
978 if (packed_refs_path == NULL)
979 return got_error_from_errno("got_repo_get_path_packed_refs");
981 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
982 if (err)
983 goto done;
985 if (delref->lf == NULL) {
986 err = got_lockfile_lock(&lf, packed_refs_path);
987 if (err)
988 goto done;
991 f = fopen(packed_refs_path, "r");
992 if (f == NULL) {
993 err = got_error_from_errno2("fopen", packed_refs_path);
994 goto done;
996 for (;;) {
997 char *line;
998 size_t len;
999 const char delim[3] = {'\0', '\0', '\0'};
1000 struct got_reference *ref;
1001 struct got_reflist_entry *new;
1003 line = fparseln(f, &len, NULL, delim, 0);
1004 if (line == NULL) {
1005 if (feof(f))
1006 break;
1007 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1008 goto done;
1010 err = parse_packed_ref_line(&ref, NULL, line);
1011 free(line);
1012 if (err)
1013 goto done;
1014 if (ref == NULL)
1015 continue;
1017 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1018 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1019 sizeof(delref->ref.ref.sha1)) == 0) {
1020 found_delref = 1;
1021 got_ref_close(ref);
1022 continue;
1025 err = insert_ref(&new, &refs, ref, repo);
1026 if (err || new == NULL /* duplicate */)
1027 got_ref_close(ref);
1028 if (err)
1029 goto done;
1032 if (found_delref) {
1033 struct got_reflist_entry *re;
1034 size_t n;
1035 struct stat sb;
1037 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1038 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1039 err = got_ferror(f, GOT_ERR_IO);
1040 goto done;
1043 SIMPLEQ_FOREACH(re, &refs, entry) {
1044 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1046 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1047 sizeof(hex)) == NULL) {
1048 err = got_error(GOT_ERR_BAD_REF_DATA);
1049 goto done;
1051 n = fprintf(tmpf, "%s ", hex);
1052 if (n != sizeof(hex)) {
1053 err = got_ferror(f, GOT_ERR_IO);
1054 goto done;
1056 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1057 if (n != strlen(re->ref->ref.ref.name) + 1) {
1058 err = got_ferror(f, GOT_ERR_IO);
1059 goto done;
1063 if (fflush(tmpf) != 0) {
1064 err = got_error_from_errno("fflush");
1065 goto done;
1068 if (stat(packed_refs_path, &sb) != 0) {
1069 if (errno != ENOENT) {
1070 err = got_error_from_errno2("stat",
1071 packed_refs_path);
1072 goto done;
1074 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1077 if (rename(tmppath, packed_refs_path) != 0) {
1078 err = got_error_from_errno3("rename", tmppath,
1079 packed_refs_path);
1080 goto done;
1083 if (chmod(packed_refs_path, sb.st_mode) != 0) {
1084 err = got_error_from_errno2("chmod",
1085 packed_refs_path);
1086 goto done;
1089 done:
1090 if (delref->lf == NULL && lf)
1091 unlock_err = got_lockfile_unlock(lf);
1092 if (f) {
1093 if (fclose(f) != 0 && err == NULL)
1094 err = got_error_from_errno("fclose");
1096 if (tmpf) {
1097 unlink(tmppath);
1098 if (fclose(tmpf) != 0 && err == NULL)
1099 err = got_error_from_errno("fclose");
1101 free(tmppath);
1102 free(packed_refs_path);
1103 got_ref_list_free(&refs);
1104 return err ? err : unlock_err;
1107 const struct got_error *
1108 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1110 const struct got_error *err = NULL, *unlock_err = NULL;
1111 const char *name = got_ref_get_name(ref);
1112 char *path_refs = NULL, *path = NULL;
1113 struct got_lockfile *lf = NULL;
1115 if (ref->flags & GOT_REF_IS_PACKED)
1116 return delete_packed_ref(ref, repo);
1118 path_refs = get_refs_dir_path(repo, name);
1119 if (path_refs == NULL) {
1120 err = got_error_from_errno2("get_refs_dir_path", name);
1121 goto done;
1124 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1125 err = got_error_from_errno("asprintf");
1126 goto done;
1129 if (ref->lf == NULL) {
1130 err = got_lockfile_lock(&lf, path);
1131 if (err)
1132 goto done;
1135 /* XXX: check if old content matches our expectations? */
1137 if (unlink(path) != 0)
1138 err = got_error_from_errno2("unlink", path);
1139 done:
1140 if (ref->lf == NULL && lf)
1141 unlock_err = got_lockfile_unlock(lf);
1143 free(path_refs);
1144 free(path);
1145 return err ? err : unlock_err;
1148 const struct got_error *
1149 got_ref_unlock(struct got_reference *ref)
1151 const struct got_error *err;
1152 err = got_lockfile_unlock(ref->lf);
1153 ref->lf = NULL;
1154 return err;