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>
32 #include "got_error.h"
33 #include "got_object.h"
34 #include "got_repository.h"
35 #include "got_reference.h"
36 #include "got_opentemp.h"
38 #include "got_lib_sha1.h"
39 #include "got_lib_path.h"
40 #include "got_lib_delta.h"
41 #include "got_lib_inflate.h"
42 #include "got_lib_object.h"
43 #include "got_lib_lockfile.h"
45 #ifndef nitems
46 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
47 #endif
49 #define GOT_REF_HEADS "heads"
50 #define GOT_REF_TAGS "tags"
51 #define GOT_REF_REMOTES "remotes"
53 /* A symbolic reference. */
54 struct got_symref {
55 char *name;
56 char *ref;
57 };
59 /* A non-symbolic reference (there is no better designation). */
60 struct got_ref {
61 char *name;
62 u_int8_t sha1[SHA1_DIGEST_LENGTH];
63 };
65 /* A reference which points to an arbitrary object. */
66 struct got_reference {
67 unsigned int flags;
68 #define GOT_REF_IS_SYMBOLIC 0x01
70 union {
71 struct got_ref ref;
72 struct got_symref symref;
73 } ref;
74 };
76 static const struct got_error *
77 parse_symref(struct got_reference **ref, const char *name, const char *line)
78 {
79 struct got_symref *symref;
80 char *symref_name;
81 char *symref_ref;
83 if (line[0] == '\0')
84 return got_error(GOT_ERR_BAD_REF_DATA);
86 symref_name = strdup(name);
87 if (symref_name == NULL)
88 return got_error_from_errno();
89 symref_ref = strdup(line);
90 if (symref_ref == NULL) {
91 const struct got_error *err = got_error_from_errno();
92 free(symref_name);
93 return err;
94 }
96 *ref = calloc(1, sizeof(**ref));
97 if (*ref == NULL)
98 return got_error_from_errno();
99 (*ref)->flags |= GOT_REF_IS_SYMBOLIC;
100 symref = &((*ref)->ref.symref);
101 symref->name = symref_name;
102 symref->ref = symref_ref;
103 return NULL;
106 static const struct got_error *
107 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
109 struct got_object_id id;
111 if (strncmp(line, "ref: ", 5) == 0) {
112 line += 5;
113 return parse_symref(ref, name, line);
116 if (!got_parse_sha1_digest(id.sha1, line))
117 return got_error(GOT_ERR_BAD_REF_DATA);
119 return got_ref_alloc(ref, name, &id);
122 static const struct got_error *
123 parse_ref_file(struct got_reference **ref, const char *name,
124 const char *abspath)
126 const struct got_error *err = NULL;
127 FILE *f = fopen(abspath, "rb");
128 char *line;
129 size_t len;
130 const char delim[3] = {'\0', '\0', '\0'};
132 if (f == NULL)
133 return NULL;
135 line = fparseln(f, &len, NULL, delim, 0);
136 if (line == NULL) {
137 err = got_error(GOT_ERR_BAD_REF_DATA);
138 goto done;
141 err = parse_ref_line(ref, name, line);
142 done:
143 free(line);
144 if (fclose(f) != 0 && err == NULL)
145 err = got_error_from_errno();
146 return err;
149 static int
150 is_well_known_ref(const char *refname)
152 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
153 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
154 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
155 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
158 static char *
159 get_refs_dir_path(struct got_repository *repo, const char *refname)
161 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
162 return strdup(got_repo_get_path_git_dir(repo));
164 return got_repo_get_path_refs(repo);
167 static int
168 is_valid_ref_name(const char *name)
170 const char *s, *slash, *seg;
171 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
172 const char *forbidden_seq[] = { "//", "..", "@{" };
173 const char *lfs = GOT_LOCKFILE_SUFFIX;
174 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
175 int i;
177 if (name[0] == '@' && name[1] == '\0')
178 return 0;
180 slash = strchr(name, '/');
181 if (slash == NULL)
182 return 0;
184 s = name;
185 seg = s;
186 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
187 return 0;
188 while (*s) {
189 for (i = 0; i < nitems(forbidden); i++) {
190 if (*s == forbidden[i])
191 return 0;
193 for (i = 0; i < nitems(forbidden_seq); i++) {
194 if (s[0] == forbidden_seq[i][0] &&
195 s[1] == forbidden_seq[i][1])
196 return 0;
198 if (iscntrl((unsigned char)s[0]))
199 return 0;
200 if (s[0] == '.' && s[1] == '\0')
201 return 0;
202 if (*s == '/') {
203 const char *nextseg = s + 1;
204 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
205 nextseg[0] == '/')
206 return 0;
207 if (seg <= s - lfs_len &&
208 strncmp(s - lfs_len, lfs, lfs_len) == 0)
209 return 0;
210 seg = nextseg;
212 s++;
215 if (seg <= s - lfs_len &&
216 strncmp(s - lfs_len, lfs, lfs_len) == 0)
217 return 0;
219 return 1;
222 const struct got_error *
223 got_ref_alloc(struct got_reference **ref, const char *name,
224 struct got_object_id *id)
226 const struct got_error *err = NULL;
228 if (!is_valid_ref_name(name))
229 return got_error(GOT_ERR_BAD_REF_NAME);
231 *ref = calloc(1, sizeof(**ref));
232 if (*ref == NULL)
233 return got_error_from_errno();
235 memcpy(&(*ref)->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
236 (*ref)->ref.ref.name = strdup(name);
237 if ((*ref)->ref.ref.name == NULL) {
238 err = got_error_from_errno();
239 free(*ref);
240 *ref = NULL;
242 return err;
245 static const struct got_error *
246 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
247 const char *line)
249 struct got_object_id id;
250 const char *name;
252 *ref = NULL;
254 if (line[0] == '#' || line[0] == '^')
255 return NULL;
257 if (!got_parse_sha1_digest(id.sha1, line))
258 return got_error(GOT_ERR_BAD_REF_DATA);
260 if (abs_refname) {
261 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
262 return NULL;
263 name = abs_refname;
264 } else
265 name = line + SHA1_DIGEST_STRING_LENGTH;
267 return got_ref_alloc(ref, name, &id);
270 static const struct got_error *
271 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
272 int nsubdirs, const char *refname)
274 const struct got_error *err = NULL;
275 char *abs_refname;
276 char *line;
277 size_t len;
278 const char delim[3] = {'\0', '\0', '\0'};
279 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
281 *ref = NULL;
283 if (ref_is_absolute)
284 abs_refname = (char *)refname;
285 do {
286 line = fparseln(f, &len, NULL, delim, 0);
287 if (line == NULL)
288 break;
289 for (i = 0; i < nsubdirs; i++) {
290 if (!ref_is_absolute &&
291 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
292 refname) == -1)
293 return got_error_from_errno();
294 err = parse_packed_ref_line(ref, abs_refname, line);
295 if (!ref_is_absolute)
296 free(abs_refname);
297 if (err || *ref != NULL)
298 break;
300 free(line);
301 if (err)
302 break;
303 } while (*ref == NULL);
305 return err;
308 static const struct got_error *
309 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
310 const char *name)
312 const struct got_error *err = NULL;
313 char *path = NULL;
314 char *normpath = NULL;
315 char *absname = NULL;
316 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
317 int ref_is_well_known = is_well_known_ref(name);
319 *ref = NULL;
321 if (ref_is_absolute || ref_is_well_known) {
322 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
323 return got_error_from_errno();
324 absname = (char *)name;
325 } else {
326 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
327 subdir[0] ? "/" : "", name) == -1)
328 return got_error_from_errno();
330 if (asprintf(&absname, "refs/%s%s%s",
331 subdir, subdir[0] ? "/" : "", name) == -1) {
332 err = got_error_from_errno();
333 goto done;
337 normpath = got_path_normalize(path);
338 if (normpath == NULL) {
339 err = got_error_from_errno();
340 goto done;
343 err = parse_ref_file(ref, absname, normpath);
344 done:
345 if (!ref_is_absolute && !ref_is_well_known)
346 free(absname);
347 free(path);
348 free(normpath);
349 return err;
352 const struct got_error *
353 got_ref_open(struct got_reference **ref, struct got_repository *repo,
354 const char *refname)
356 const struct got_error *err = NULL;
357 char *path_refs = NULL;
358 const char *subdirs[] = {
359 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
360 };
361 int i, well_known = is_well_known_ref(refname);
363 *ref = NULL;
365 path_refs = get_refs_dir_path(repo, refname);
366 if (path_refs == NULL) {
367 err = got_error_from_errno();
368 goto done;
371 if (!well_known) {
372 char *packed_refs_path;
373 FILE *f;
375 /* Search on-disk refs before packed refs! */
376 for (i = 0; i < nitems(subdirs); i++) {
377 err = open_ref(ref, path_refs, subdirs[i], refname);
378 if (err || *ref)
379 goto done;
382 packed_refs_path = got_repo_get_path_packed_refs(repo);
383 if (packed_refs_path == NULL) {
384 err = got_error_from_errno();
385 goto done;
388 f = fopen(packed_refs_path, "rb");
389 free(packed_refs_path);
390 if (f != NULL) {
391 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
392 refname);
393 if (fclose(f) != 0 && err == NULL)
394 err = got_error_from_errno();
395 if (err || *ref)
396 goto done;
400 err = open_ref(ref, path_refs, "", refname);
401 if (err)
402 goto done;
403 done:
404 if (*ref == NULL)
405 err = got_error_not_ref(refname);
406 free(path_refs);
407 return err;
410 void
411 got_ref_close(struct got_reference *ref)
413 if (ref->flags & GOT_REF_IS_SYMBOLIC)
414 free(ref->ref.symref.name);
415 else
416 free(ref->ref.ref.name);
417 free(ref);
420 struct got_reference *
421 got_ref_dup(struct got_reference *ref)
423 struct got_reference *ret;
425 ret = calloc(1, sizeof(*ret));
426 if (ret == NULL)
427 return NULL;
429 ret->flags = ref->flags;
430 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
431 ret->ref.symref.name = strdup(ref->ref.symref.name);
432 if (ret->ref.symref.name == NULL) {
433 free(ret);
434 return NULL;
436 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
437 if (ret->ref.symref.ref == NULL) {
438 free(ret->ref.symref.name);
439 free(ret);
440 return NULL;
442 } else {
443 ref->ref.ref.name = strdup(ref->ref.ref.name);
444 if (ref->ref.ref.name == NULL) {
445 free(ret);
446 return NULL;
448 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
449 SHA1_DIGEST_LENGTH);
452 return ret;
455 static const struct got_error *
456 resolve_symbolic_ref(struct got_reference **resolved,
457 struct got_repository *repo, struct got_reference *ref)
459 struct got_reference *nextref;
460 const struct got_error *err;
462 err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
463 if (err)
464 return err;
466 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
467 err = resolve_symbolic_ref(resolved, repo, nextref);
468 else
469 *resolved = got_ref_dup(nextref);
471 got_ref_close(nextref);
472 return err;
475 const struct got_error *
476 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
477 struct got_reference *ref)
479 const struct got_error *err;
481 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
482 struct got_reference *resolved = NULL;
483 err = resolve_symbolic_ref(&resolved, repo, ref);
484 if (err == NULL)
485 err = got_ref_resolve(id, repo, resolved);
486 free(resolved);
487 return err;
490 *id = calloc(1, sizeof(**id));
491 if (*id == NULL)
492 return got_error_from_errno();
493 memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
494 return NULL;
497 char *
498 got_ref_to_str(struct got_reference *ref)
500 char *str;
502 if (ref->flags & GOT_REF_IS_SYMBOLIC)
503 return strdup(ref->ref.symref.ref);
505 str = malloc(SHA1_DIGEST_STRING_LENGTH);
506 if (str == NULL)
507 return NULL;
509 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
510 SHA1_DIGEST_STRING_LENGTH) == NULL) {
511 free(str);
512 return NULL;
515 return str;
518 const char *
519 got_ref_get_name(struct got_reference *ref)
521 if (ref->flags & GOT_REF_IS_SYMBOLIC)
522 return ref->ref.symref.name;
524 return ref->ref.ref.name;
527 static const struct got_error *
528 insert_ref(struct got_reflist_head *refs, struct got_reference *ref,
529 struct got_repository *repo)
531 const struct got_error *err;
532 struct got_object_id *id;
533 struct got_reflist_entry *new, *re, *prev;
534 int cmp;
536 err = got_ref_resolve(&id, repo, ref);
537 if (err)
538 return err;
540 new = malloc(sizeof(*re));
541 if (new == NULL) {
542 free(id);
543 return got_error_from_errno();
545 new->ref = ref;
546 new->id = id;
548 /*
549 * We must de-duplicate entries on insert because packed-refs may
550 * contain redundant entries. On-disk refs take precedence.
551 * This code assumes that on-disk revs are read before packed-refs.
552 * We're iterating the list anyway, so insert elements sorted by name.
553 */
554 re = SIMPLEQ_FIRST(refs);
555 while (re) {
556 cmp = got_path_cmp(got_ref_get_name(re->ref),
557 got_ref_get_name(ref));
558 if (cmp == 0) {
559 free(ref); /* duplicate */
560 return NULL;
561 } else if (cmp > 0) {
562 if (prev)
563 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
564 else
565 SIMPLEQ_INSERT_HEAD(refs, new, entry);
566 return NULL;
567 } else {
568 prev = re;
569 re = SIMPLEQ_NEXT(re, entry);
573 SIMPLEQ_INSERT_TAIL(refs, new, entry);
574 return NULL;
577 static const struct got_error *
578 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
579 const char *subdir, struct got_repository *repo)
581 const struct got_error *err = NULL;
582 DIR *d = NULL;
583 char *path_subdir;
585 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
586 return got_error_from_errno();
588 d = opendir(path_subdir);
589 if (d == NULL)
590 goto done;
592 while (1) {
593 struct dirent *dent;
594 struct got_reference *ref;
595 char *child;
597 dent = readdir(d);
598 if (dent == NULL)
599 break;
601 if (strcmp(dent->d_name, ".") == 0 ||
602 strcmp(dent->d_name, "..") == 0)
603 continue;
605 switch (dent->d_type) {
606 case DT_REG:
607 err = open_ref(&ref, path_refs, subdir, dent->d_name);
608 if (err)
609 goto done;
610 if (ref) {
611 err = insert_ref(refs, ref, repo);
612 if (err)
613 goto done;
615 break;
616 case DT_DIR:
617 if (asprintf(&child, "%s%s%s", subdir,
618 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
619 err = got_error_from_errno();
620 break;
622 err = gather_on_disk_refs(refs, path_refs, child, repo);
623 free(child);
624 break;
625 default:
626 break;
629 done:
630 if (d)
631 closedir(d);
632 free(path_subdir);
633 return err;
636 const struct got_error *
637 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
639 const struct got_error *err;
640 char *packed_refs_path, *path_refs = NULL;
641 FILE *f = NULL;
642 struct got_reference *ref;
644 /* HEAD ref should always exist. */
645 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
646 if (path_refs == NULL) {
647 err = got_error_from_errno();
648 goto done;
650 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD);
651 if (err)
652 goto done;
653 err = insert_ref(refs, ref, repo);
654 if (err)
655 goto done;
657 /* Gather on-disk refs before parsing packed-refs. */
658 free(path_refs);
659 path_refs = get_refs_dir_path(repo, "");
660 if (path_refs == NULL) {
661 err = got_error_from_errno();
662 goto done;
664 err = gather_on_disk_refs(refs, path_refs, "", repo);
665 if (err)
666 goto done;
668 /*
669 * The packed-refs file may contain redundant entries, in which
670 * case on-disk refs take precedence.
671 */
672 packed_refs_path = got_repo_get_path_packed_refs(repo);
673 if (packed_refs_path == NULL) {
674 err = got_error_from_errno();
675 goto done;
678 f = fopen(packed_refs_path, "r");
679 free(packed_refs_path);
680 if (f) {
681 char *line;
682 size_t len;
683 const char delim[3] = {'\0', '\0', '\0'};
684 while (1) {
685 line = fparseln(f, &len, NULL, delim, 0);
686 if (line == NULL)
687 break;
688 err = parse_packed_ref_line(&ref, NULL, line);
689 if (err)
690 goto done;
691 if (ref) {
692 err = insert_ref(refs, ref, repo);
693 if (err)
694 goto done;
698 done:
699 free(path_refs);
700 if (f && fclose(f) != 0 && err == NULL)
701 err = got_error_from_errno();
702 return err;
705 const struct got_error *
706 got_ref_write(struct got_reference *ref, struct got_repository *repo)
708 const struct got_error *err = NULL, *unlock_err = NULL;
709 const char *name = got_ref_get_name(ref);
710 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
711 struct got_lockfile *lf = NULL;
712 FILE *f = NULL;
713 size_t n;
714 struct stat sb;
716 path_refs = get_refs_dir_path(repo, name);
717 if (path_refs == NULL) {
718 err = got_error_from_errno();
719 goto done;
722 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
723 err = got_error_from_errno();
724 goto done;
727 err = got_opentemp_named(&tmppath, &f, path);
728 if (f == NULL) {
729 err = got_error_from_errno();
730 goto done;
733 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
734 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
735 if (n != strlen(ref->ref.symref.ref) + 6) {
736 err = got_ferror(f, GOT_ERR_IO);
737 goto done;
739 } else {
740 char hex[SHA1_DIGEST_STRING_LENGTH];
741 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
742 sizeof(hex)) == NULL) {
743 err = got_error(GOT_ERR_BAD_REF_DATA);
744 goto done;
746 n = fprintf(f, "%s\n", hex);
747 if (n != sizeof(hex)) {
748 err = got_ferror(f, GOT_ERR_IO);
749 goto done;
753 err = got_lockfile_lock(&lf, path);
754 if (err)
755 goto done;
757 /* XXX: check if old content matches our expectations? */
759 if (stat(path, &sb) != 0 && errno != ENOENT) {
760 err = got_error_from_errno();
761 goto done;
764 if (rename(tmppath, path) != 0) {
765 err = got_error_from_errno();
766 goto done;
768 free(tmppath);
769 tmppath = NULL;
771 if (chmod(path, sb.st_mode) != 0) {
772 err = got_error_from_errno();
773 goto done;
775 done:
776 if (lf)
777 unlock_err = got_lockfile_unlock(lf);
778 if (f) {
779 if (fclose(f) != 0 && err == NULL)
780 err = got_error_from_errno();
782 free(path_refs);
783 free(path);
784 if (tmppath) {
785 if (unlink(tmppath) != 0 && err == NULL)
786 err = got_error_from_errno();
787 free(tmppath);
789 return err ? err : unlock_err;
792 const struct got_error *
793 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
795 const struct got_error *err = NULL, *unlock_err = NULL;
796 const char *name = got_ref_get_name(ref);
797 char *path_refs = NULL, *path = NULL;
798 struct got_lockfile *lf = NULL;
800 /* TODO: handle packed refs ! */
802 path_refs = get_refs_dir_path(repo, name);
803 if (path_refs == NULL) {
804 err = got_error_from_errno();
805 goto done;
808 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
809 err = got_error_from_errno();
810 goto done;
813 err = got_lockfile_lock(&lf, path);
814 if (err)
815 goto done;
817 /* XXX: check if old content matches our expectations? */
819 if (unlink(path) != 0)
820 err = got_error_from_errno();
821 done:
822 if (lf)
823 unlock_err = got_lockfile_unlock(lf);
825 free(path_refs);
826 free(path);
827 return err ? err : unlock_err;