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 const struct got_error *err;
294 char *absname = NULL;
296 if (!is_valid_ref_name(name)) {
297 if (strchr(name, '/') != NULL)
298 return got_error(GOT_ERR_BAD_REF_NAME);
299 if (asprintf(&absname, "refs/heads/%s", name) == -1)
300 return got_error_from_errno("asprintf");
303 err = alloc_ref(ref, absname ? absname : name, id, 0);
304 free(absname);
305 return err;
308 static const struct got_error *
309 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
310 const char *line)
312 struct got_object_id id;
313 const char *name;
315 *ref = NULL;
317 if (line[0] == '#' || line[0] == '^')
318 return NULL;
320 if (!got_parse_sha1_digest(id.sha1, line))
321 return got_error(GOT_ERR_BAD_REF_DATA);
323 if (abs_refname) {
324 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
325 return NULL;
326 name = abs_refname;
327 } else
328 name = line + SHA1_DIGEST_STRING_LENGTH;
330 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
333 static const struct got_error *
334 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
335 int nsubdirs, const char *refname)
337 const struct got_error *err = NULL;
338 char *abs_refname;
339 char *line;
340 size_t len;
341 const char delim[3] = {'\0', '\0', '\0'};
342 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
344 *ref = NULL;
346 if (ref_is_absolute)
347 abs_refname = (char *)refname;
348 do {
349 line = fparseln(f, &len, NULL, delim, 0);
350 if (line == NULL) {
351 if (feof(f))
352 break;
353 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
354 break;
356 for (i = 0; i < nsubdirs; i++) {
357 if (!ref_is_absolute &&
358 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
359 refname) == -1)
360 return got_error_from_errno("asprintf");
361 err = parse_packed_ref_line(ref, abs_refname, line);
362 if (!ref_is_absolute)
363 free(abs_refname);
364 if (err || *ref != NULL)
365 break;
367 free(line);
368 if (err)
369 break;
370 } while (*ref == NULL);
372 return err;
375 static const struct got_error *
376 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
377 const char *name, int lock)
379 const struct got_error *err = NULL;
380 char *path = NULL;
381 char *normpath = 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 normpath = got_path_normalize(path);
405 if (normpath == NULL) {
406 if (errno == ENOENT)
407 err = NULL;
408 else
409 err = got_error_from_errno2("got_path_normalize", path);
410 goto done;
413 err = parse_ref_file(ref, absname, normpath, lock);
414 done:
415 if (!ref_is_absolute && !ref_is_well_known)
416 free(absname);
417 free(path);
418 free(normpath);
419 return err;
422 const struct got_error *
423 got_ref_open(struct got_reference **ref, struct got_repository *repo,
424 const char *refname, int lock)
426 const struct got_error *err = NULL;
427 char *path_refs = NULL;
428 const char *subdirs[] = {
429 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
430 };
431 int i, well_known = is_well_known_ref(refname);
432 struct got_lockfile *lf = NULL;
434 *ref = NULL;
436 path_refs = get_refs_dir_path(repo, refname);
437 if (path_refs == NULL) {
438 err = got_error_from_errno2("get_refs_dir_path", refname);
439 goto done;
442 if (well_known) {
443 err = open_ref(ref, path_refs, "", refname, lock);
444 } else {
445 char *packed_refs_path;
446 FILE *f;
448 /* Search on-disk refs before packed refs! */
449 for (i = 0; i < nitems(subdirs); i++) {
450 err = open_ref(ref, path_refs, subdirs[i], refname,
451 lock);
452 if (err || *ref)
453 goto done;
456 packed_refs_path = got_repo_get_path_packed_refs(repo);
457 if (packed_refs_path == NULL) {
458 err = got_error_from_errno(
459 "got_repo_get_path_packed_refs");
460 goto done;
463 if (lock) {
464 err = got_lockfile_lock(&lf, packed_refs_path);
465 if (err)
466 goto done;
468 f = fopen(packed_refs_path, "rb");
469 free(packed_refs_path);
470 if (f != NULL) {
471 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
472 refname);
473 if (!err) {
474 if (fclose(f) != 0) {
475 err = got_error_from_errno("fclose");
476 got_ref_close(*ref);
477 *ref = NULL;
478 } else
479 (*ref)->lf = lf;
483 done:
484 if (!err && *ref == NULL)
485 err = got_error_not_ref(refname);
486 if (err && lf)
487 got_lockfile_unlock(lf);
488 free(path_refs);
489 return err;
492 void
493 got_ref_close(struct got_reference *ref)
495 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
496 free(ref->ref.symref.name);
497 free(ref->ref.symref.ref);
498 } else
499 free(ref->ref.ref.name);
500 free(ref);
503 struct got_reference *
504 got_ref_dup(struct got_reference *ref)
506 struct got_reference *ret;
508 ret = calloc(1, sizeof(*ret));
509 if (ret == NULL)
510 return NULL;
512 ret->flags = ref->flags;
513 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
514 ret->ref.symref.name = strdup(ref->ref.symref.name);
515 if (ret->ref.symref.name == NULL) {
516 free(ret);
517 return NULL;
519 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
520 if (ret->ref.symref.ref == NULL) {
521 free(ret->ref.symref.name);
522 free(ret);
523 return NULL;
525 } else {
526 ref->ref.ref.name = strdup(ref->ref.ref.name);
527 if (ref->ref.ref.name == NULL) {
528 free(ret);
529 return NULL;
531 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
532 sizeof(ret->ref.ref.sha1));
535 return ret;
538 static const struct got_error *
539 resolve_symbolic_ref(struct got_reference **resolved,
540 struct got_repository *repo, struct got_reference *ref)
542 struct got_reference *nextref;
543 const struct got_error *err;
545 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
546 if (err)
547 return err;
549 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
550 err = resolve_symbolic_ref(resolved, repo, nextref);
551 else
552 *resolved = got_ref_dup(nextref);
554 got_ref_close(nextref);
555 return err;
558 const struct got_error *
559 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
560 struct got_reference *ref)
562 const struct got_error *err;
564 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
565 struct got_reference *resolved = NULL;
566 err = resolve_symbolic_ref(&resolved, repo, ref);
567 if (err == NULL)
568 err = got_ref_resolve(id, repo, resolved);
569 if (resolved)
570 got_ref_close(resolved);
571 return err;
574 *id = calloc(1, sizeof(**id));
575 if (*id == NULL)
576 return got_error_from_errno("calloc");
577 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
578 return NULL;
581 char *
582 got_ref_to_str(struct got_reference *ref)
584 char *str;
586 if (ref->flags & GOT_REF_IS_SYMBOLIC)
587 return strdup(ref->ref.symref.ref);
589 str = malloc(SHA1_DIGEST_STRING_LENGTH);
590 if (str == NULL)
591 return NULL;
593 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
594 SHA1_DIGEST_STRING_LENGTH) == NULL) {
595 free(str);
596 return NULL;
599 return str;
602 const char *
603 got_ref_get_name(struct got_reference *ref)
605 if (ref->flags & GOT_REF_IS_SYMBOLIC)
606 return ref->ref.symref.name;
608 return ref->ref.ref.name;
611 static const struct got_error *
612 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
613 struct got_reference *ref, struct got_repository *repo)
615 const struct got_error *err;
616 struct got_object_id *id;
617 struct got_reflist_entry *new, *re, *prev = NULL;
618 int cmp;
620 *newp = NULL;
622 err = got_ref_resolve(&id, repo, ref);
623 if (err)
624 return err;
626 new = malloc(sizeof(*new));
627 if (new == NULL) {
628 free(id);
629 return got_error_from_errno("malloc");
631 new->ref = ref;
632 new->id = id;
633 *newp = new;
635 /*
636 * We must de-duplicate entries on insert because packed-refs may
637 * contain redundant entries. On-disk refs take precedence.
638 * This code assumes that on-disk revs are read before packed-refs.
639 * We're iterating the list anyway, so insert elements sorted by name.
640 */
641 re = SIMPLEQ_FIRST(refs);
642 while (re) {
643 cmp = got_path_cmp(got_ref_get_name(re->ref),
644 got_ref_get_name(new->ref));
645 if (cmp == 0) {
646 /* duplicate */
647 free(new->id);
648 free(new);
649 *newp = NULL;
650 return NULL;
651 } else if (cmp > 0) {
652 if (prev)
653 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
654 else
655 SIMPLEQ_INSERT_HEAD(refs, new, entry);
656 return NULL;
657 } else {
658 prev = re;
659 re = SIMPLEQ_NEXT(re, entry);
663 SIMPLEQ_INSERT_TAIL(refs, new, entry);
664 return NULL;
667 static const struct got_error *
668 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
669 const char *subdir, struct got_repository *repo)
671 const struct got_error *err = NULL;
672 DIR *d = NULL;
673 char *path_subdir;
675 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
676 return got_error_from_errno("asprintf");
678 d = opendir(path_subdir);
679 if (d == NULL)
680 goto done;
682 for (;;) {
683 struct dirent *dent;
684 struct got_reference *ref;
685 char *child;
687 dent = readdir(d);
688 if (dent == NULL)
689 break;
691 if (strcmp(dent->d_name, ".") == 0 ||
692 strcmp(dent->d_name, "..") == 0)
693 continue;
695 switch (dent->d_type) {
696 case DT_REG:
697 err = open_ref(&ref, path_refs, subdir, dent->d_name,
698 0);
699 if (err)
700 goto done;
701 if (ref) {
702 struct got_reflist_entry *new;
703 err = insert_ref(&new, refs, ref, repo);
704 if (err || new == NULL /* duplicate */)
705 got_ref_close(ref);
706 if (err)
707 goto done;
709 break;
710 case DT_DIR:
711 if (asprintf(&child, "%s%s%s", subdir,
712 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
713 err = got_error_from_errno("asprintf");
714 break;
716 err = gather_on_disk_refs(refs, path_refs, child, repo);
717 free(child);
718 break;
719 default:
720 break;
723 done:
724 if (d)
725 closedir(d);
726 free(path_subdir);
727 return err;
730 const struct got_error *
731 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
733 const struct got_error *err;
734 char *packed_refs_path, *path_refs = NULL;
735 FILE *f = NULL;
736 struct got_reference *ref;
737 struct got_reflist_entry *new;
739 /* HEAD ref should always exist. */
740 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
741 if (path_refs == NULL) {
742 err = got_error_from_errno("get_refs_dir_path");
743 goto done;
745 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
746 if (err)
747 goto done;
748 err = insert_ref(&new, refs, ref, repo);
749 if (err || new == NULL /* duplicate */)
750 got_ref_close(ref);
751 if (err)
752 goto done;
754 /* Gather on-disk refs before parsing packed-refs. */
755 free(path_refs);
756 path_refs = get_refs_dir_path(repo, "");
757 if (path_refs == NULL) {
758 err = got_error_from_errno("get_refs_dir_path");
759 goto done;
761 err = gather_on_disk_refs(refs, path_refs, "", repo);
762 if (err)
763 goto done;
765 /*
766 * The packed-refs file may contain redundant entries, in which
767 * case on-disk refs take precedence.
768 */
769 packed_refs_path = got_repo_get_path_packed_refs(repo);
770 if (packed_refs_path == NULL) {
771 err = got_error_from_errno("got_repo_get_path_packed_refs");
772 goto done;
775 f = fopen(packed_refs_path, "r");
776 free(packed_refs_path);
777 if (f) {
778 char *line;
779 size_t len;
780 const char delim[3] = {'\0', '\0', '\0'};
781 for (;;) {
782 line = fparseln(f, &len, NULL, delim, 0);
783 if (line == NULL) {
784 if (feof(f))
785 break;
786 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
787 goto done;
789 err = parse_packed_ref_line(&ref, NULL, line);
790 free(line);
791 if (err)
792 goto done;
793 if (ref) {
794 err = insert_ref(&new, refs, ref, repo);
795 if (err || new == NULL /* duplicate */)
796 got_ref_close(ref);
797 if (err)
798 goto done;
802 done:
803 free(path_refs);
804 if (f && fclose(f) != 0 && err == NULL)
805 err = got_error_from_errno("fclose");
806 return err;
809 void
810 got_ref_list_free(struct got_reflist_head *refs)
812 struct got_reflist_entry *re;
814 while (!SIMPLEQ_EMPTY(refs)) {
815 re = SIMPLEQ_FIRST(refs);
816 SIMPLEQ_REMOVE_HEAD(refs, entry);
817 got_ref_close(re->ref);
818 free(re->id);
819 free(re);
824 int
825 got_ref_is_symbolic(struct got_reference *ref)
827 return (ref->flags & GOT_REF_IS_SYMBOLIC);
830 const struct got_error *
831 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
833 if (ref->flags & GOT_REF_IS_SYMBOLIC)
834 return got_error(GOT_ERR_BAD_REF_TYPE);
836 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
837 return NULL;
840 const struct got_error *
841 got_ref_change_symref(struct got_reference *ref, char *refname)
843 char *new_name;
845 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
846 return got_error(GOT_ERR_BAD_REF_TYPE);
848 new_name = strdup(refname);
849 if (new_name == NULL)
850 return got_error_from_errno("strdup");
852 free(ref->ref.symref.name);
853 ref->ref.symref.name = new_name;
854 return NULL;
857 const struct got_error *
858 got_ref_write(struct got_reference *ref, struct got_repository *repo)
860 const struct got_error *err = NULL, *unlock_err = NULL;
861 const char *name = got_ref_get_name(ref);
862 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
863 struct got_lockfile *lf = NULL;
864 FILE *f = NULL;
865 size_t n;
866 struct stat sb;
868 path_refs = get_refs_dir_path(repo, name);
869 if (path_refs == NULL) {
870 err = got_error_from_errno2("get_refs_dir_path", name);
871 goto done;
874 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
875 err = got_error_from_errno("asprintf");
876 goto done;
879 err = got_opentemp_named(&tmppath, &f, path);
880 if (err) {
881 char *parent;
882 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
883 goto done;
884 err = got_path_dirname(&parent, path);
885 if (err)
886 goto done;
887 err = got_path_mkdir(parent);
888 free(parent);
889 if (err)
890 goto done;
891 err = got_opentemp_named(&tmppath, &f, path);
892 if (err)
893 goto done;
896 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
897 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
898 if (n != strlen(ref->ref.symref.ref) + 6) {
899 err = got_ferror(f, GOT_ERR_IO);
900 goto done;
902 } else {
903 char hex[SHA1_DIGEST_STRING_LENGTH];
904 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
905 sizeof(hex)) == NULL) {
906 err = got_error(GOT_ERR_BAD_REF_DATA);
907 goto done;
909 n = fprintf(f, "%s\n", hex);
910 if (n != sizeof(hex)) {
911 err = got_ferror(f, GOT_ERR_IO);
912 goto done;
916 if (ref->lf == NULL) {
917 err = got_lockfile_lock(&lf, path);
918 if (err)
919 goto done;
922 /* XXX: check if old content matches our expectations? */
924 if (stat(path, &sb) != 0) {
925 if (errno != ENOENT) {
926 err = got_error_from_errno2("stat", path);
927 goto done;
929 sb.st_mode = GOT_DEFAULT_FILE_MODE;
932 if (rename(tmppath, path) != 0) {
933 err = got_error_from_errno3("rename", tmppath, path);
934 goto done;
936 free(tmppath);
937 tmppath = NULL;
939 if (chmod(path, sb.st_mode) != 0) {
940 err = got_error_from_errno2("chmod", path);
941 goto done;
943 done:
944 if (ref->lf == NULL && lf)
945 unlock_err = got_lockfile_unlock(lf);
946 if (f) {
947 if (fclose(f) != 0 && err == NULL)
948 err = got_error_from_errno("fclose");
950 free(path_refs);
951 free(path);
952 if (tmppath) {
953 if (unlink(tmppath) != 0 && err == NULL)
954 err = got_error_from_errno2("unlink", tmppath);
955 free(tmppath);
957 return err ? err : unlock_err;
960 static const struct got_error *
961 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
963 const struct got_error *err = NULL, *unlock_err = NULL;
964 struct got_lockfile *lf = NULL;
965 FILE *f = NULL, *tmpf = NULL;
966 char *packed_refs_path, *tmppath = NULL;
967 struct got_reflist_head refs;
968 int found_delref = 0;
970 /* The packed-refs file does not cotain symbolic references. */
971 if (delref->flags & GOT_REF_IS_SYMBOLIC)
972 return got_error(GOT_ERR_BAD_REF_DATA);
974 SIMPLEQ_INIT(&refs);
976 packed_refs_path = got_repo_get_path_packed_refs(repo);
977 if (packed_refs_path == NULL)
978 return got_error_from_errno("got_repo_get_path_packed_refs");
980 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
981 if (err)
982 goto done;
984 if (delref->lf == NULL) {
985 err = got_lockfile_lock(&lf, packed_refs_path);
986 if (err)
987 goto done;
990 f = fopen(packed_refs_path, "r");
991 if (f == NULL) {
992 err = got_error_from_errno2("fopen", packed_refs_path);
993 goto done;
995 for (;;) {
996 char *line;
997 size_t len;
998 const char delim[3] = {'\0', '\0', '\0'};
999 struct got_reference *ref;
1000 struct got_reflist_entry *new;
1002 line = fparseln(f, &len, NULL, delim, 0);
1003 if (line == NULL) {
1004 if (feof(f))
1005 break;
1006 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1007 goto done;
1009 err = parse_packed_ref_line(&ref, NULL, line);
1010 free(line);
1011 if (err)
1012 goto done;
1013 if (ref == NULL)
1014 continue;
1016 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1017 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1018 sizeof(delref->ref.ref.sha1)) == 0) {
1019 found_delref = 1;
1020 got_ref_close(ref);
1021 continue;
1024 err = insert_ref(&new, &refs, ref, repo);
1025 if (err || new == NULL /* duplicate */)
1026 got_ref_close(ref);
1027 if (err)
1028 goto done;
1031 if (found_delref) {
1032 struct got_reflist_entry *re;
1033 size_t n;
1034 struct stat sb;
1036 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1037 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1038 err = got_ferror(f, GOT_ERR_IO);
1039 goto done;
1042 SIMPLEQ_FOREACH(re, &refs, entry) {
1043 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1045 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1046 sizeof(hex)) == NULL) {
1047 err = got_error(GOT_ERR_BAD_REF_DATA);
1048 goto done;
1050 n = fprintf(tmpf, "%s ", hex);
1051 if (n != sizeof(hex)) {
1052 err = got_ferror(f, GOT_ERR_IO);
1053 goto done;
1055 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1056 if (n != strlen(re->ref->ref.ref.name) + 1) {
1057 err = got_ferror(f, GOT_ERR_IO);
1058 goto done;
1062 if (fflush(tmpf) != 0) {
1063 err = got_error_from_errno("fflush");
1064 goto done;
1067 if (stat(packed_refs_path, &sb) != 0) {
1068 if (errno != ENOENT) {
1069 err = got_error_from_errno2("stat",
1070 packed_refs_path);
1071 goto done;
1073 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1076 if (rename(tmppath, packed_refs_path) != 0) {
1077 err = got_error_from_errno3("rename", tmppath,
1078 packed_refs_path);
1079 goto done;
1082 if (chmod(packed_refs_path, sb.st_mode) != 0) {
1083 err = got_error_from_errno2("chmod",
1084 packed_refs_path);
1085 goto done;
1088 done:
1089 if (delref->lf == NULL && lf)
1090 unlock_err = got_lockfile_unlock(lf);
1091 if (f) {
1092 if (fclose(f) != 0 && err == NULL)
1093 err = got_error_from_errno("fclose");
1095 if (tmpf) {
1096 unlink(tmppath);
1097 if (fclose(tmpf) != 0 && err == NULL)
1098 err = got_error_from_errno("fclose");
1100 free(tmppath);
1101 free(packed_refs_path);
1102 got_ref_list_free(&refs);
1103 return err ? err : unlock_err;
1106 const struct got_error *
1107 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1109 const struct got_error *err = NULL, *unlock_err = NULL;
1110 const char *name = got_ref_get_name(ref);
1111 char *path_refs = NULL, *path = NULL;
1112 struct got_lockfile *lf = NULL;
1114 if (ref->flags & GOT_REF_IS_PACKED)
1115 return delete_packed_ref(ref, repo);
1117 path_refs = get_refs_dir_path(repo, name);
1118 if (path_refs == NULL) {
1119 err = got_error_from_errno2("get_refs_dir_path", name);
1120 goto done;
1123 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1124 err = got_error_from_errno("asprintf");
1125 goto done;
1128 if (ref->lf == NULL) {
1129 err = got_lockfile_lock(&lf, path);
1130 if (err)
1131 goto done;
1134 /* XXX: check if old content matches our expectations? */
1136 if (unlink(path) != 0)
1137 err = got_error_from_errno2("unlink", path);
1138 done:
1139 if (ref->lf == NULL && lf)
1140 unlock_err = got_lockfile_unlock(lf);
1142 free(path_refs);
1143 free(path);
1144 return err ? err : unlock_err;
1147 const struct got_error *
1148 got_ref_unlock(struct got_reference *ref)
1150 const struct got_error *err;
1151 err = got_lockfile_unlock(ref->lf);
1152 ref->lf = NULL;
1153 return err;