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 <ctype.h>
22 #include <dirent.h>
23 #include <sha1.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <util.h>
28 #include <zlib.h>
29 #include <time.h>
31 #include "got_error.h"
32 #include "got_object.h"
33 #include "got_repository.h"
34 #include "got_reference.h"
35 #include "got_opentemp.h"
37 #include "got_lib_sha1.h"
38 #include "got_lib_path.h"
39 #include "got_lib_delta.h"
40 #include "got_lib_inflate.h"
41 #include "got_lib_object.h"
42 #include "got_lib_lockfile.h"
44 #ifndef nitems
45 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
46 #endif
48 #define GOT_REF_HEADS "heads"
49 #define GOT_REF_TAGS "tags"
50 #define GOT_REF_REMOTES "remotes"
52 /* A symbolic reference. */
53 struct got_symref {
54 char *name;
55 char *ref;
56 };
58 /* A non-symbolic reference (there is no better designation). */
59 struct got_ref {
60 char *name;
61 u_int8_t sha1[SHA1_DIGEST_LENGTH];
62 };
64 /* A reference which points to an arbitrary object. */
65 struct got_reference {
66 unsigned int flags;
67 #define GOT_REF_IS_SYMBOLIC 0x01
69 union {
70 struct got_ref ref;
71 struct got_symref symref;
72 } ref;
73 };
75 static const struct got_error *
76 parse_symref(struct got_reference **ref, const char *name, const char *line)
77 {
78 struct got_symref *symref;
79 char *symref_name;
80 char *symref_ref;
82 if (line[0] == '\0')
83 return got_error(GOT_ERR_BAD_REF_DATA);
85 symref_name = strdup(name);
86 if (symref_name == NULL)
87 return got_error_from_errno();
88 symref_ref = strdup(line);
89 if (symref_ref == NULL) {
90 const struct got_error *err = got_error_from_errno();
91 free(symref_name);
92 return err;
93 }
95 *ref = calloc(1, sizeof(**ref));
96 if (*ref == NULL)
97 return got_error_from_errno();
98 (*ref)->flags |= GOT_REF_IS_SYMBOLIC;
99 symref = &((*ref)->ref.symref);
100 symref->name = symref_name;
101 symref->ref = symref_ref;
102 return NULL;
105 static const struct got_error *
106 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
108 struct got_object_id id;
110 if (strncmp(line, "ref: ", 5) == 0) {
111 line += 5;
112 return parse_symref(ref, name, line);
115 if (!got_parse_sha1_digest(id.sha1, line))
116 return got_error(GOT_ERR_BAD_REF_DATA);
118 return got_ref_alloc(ref, name, &id);
121 static const struct got_error *
122 parse_ref_file(struct got_reference **ref, const char *name,
123 const char *abspath)
125 const struct got_error *err = NULL;
126 FILE *f = fopen(abspath, "rb");
127 char *line;
128 size_t len;
129 const char delim[3] = {'\0', '\0', '\0'};
131 if (f == NULL)
132 return NULL;
134 line = fparseln(f, &len, NULL, delim, 0);
135 if (line == NULL) {
136 err = got_error(GOT_ERR_BAD_REF_DATA);
137 goto done;
140 err = parse_ref_line(ref, name, line);
141 done:
142 free(line);
143 if (fclose(f) != 0 && err == NULL)
144 err = got_error_from_errno();
145 return err;
148 static int
149 is_well_known_ref(const char *refname)
151 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
152 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
153 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
154 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
157 static char *
158 get_refs_dir_path(struct got_repository *repo, const char *refname)
160 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
161 return strdup(got_repo_get_path_git_dir(repo));
163 return got_repo_get_path_refs(repo);
166 static int
167 is_valid_ref_name(const char *name)
169 const char *s, *slash, *seg;
170 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
171 const char *forbidden_seq[] = { "//", "..", "@{" };
172 const char *lfs = GOT_LOCKFILE_SUFFIX;
173 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
174 int i;
176 if (name[0] == '@' && name[1] == '\0')
177 return 0;
179 slash = strchr(name, '/');
180 if (slash == NULL)
181 return 0;
183 s = name;
184 seg = s;
185 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
186 return 0;
187 while (*s) {
188 for (i = 0; i < nitems(forbidden); i++) {
189 if (*s == forbidden[i])
190 return 0;
192 for (i = 0; i < nitems(forbidden_seq); i++) {
193 if (s[0] == forbidden_seq[i][0] &&
194 s[1] == forbidden_seq[i][1])
195 return 0;
197 if (iscntrl((unsigned char)s[0]))
198 return 0;
199 if (s[0] == '.' && s[1] == '\0')
200 return 0;
201 if (*s == '/') {
202 const char *nextseg = s + 1;
203 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
204 nextseg[0] == '/')
205 return 0;
206 if (seg <= s - lfs_len &&
207 strncmp(s - lfs_len, lfs, lfs_len) == 0)
208 return 0;
209 seg = nextseg;
211 s++;
214 if (seg <= s - lfs_len &&
215 strncmp(s - lfs_len, lfs, lfs_len) == 0)
216 return 0;
218 return 1;
221 const struct got_error *
222 got_ref_alloc(struct got_reference **ref, const char *name,
223 struct got_object_id *id)
225 const struct got_error *err = NULL;
227 if (!is_valid_ref_name(name))
228 return got_error(GOT_ERR_BAD_REF_NAME);
230 *ref = calloc(1, sizeof(**ref));
231 if (*ref == NULL)
232 return got_error_from_errno();
234 memcpy(&(*ref)->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
235 (*ref)->ref.ref.name = strdup(name);
236 if ((*ref)->ref.ref.name == NULL) {
237 err = got_error_from_errno();
238 free(*ref);
239 *ref = NULL;
241 return err;
244 static const struct got_error *
245 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
246 const char *line)
248 struct got_object_id id;
249 const char *name;
251 *ref = NULL;
253 if (line[0] == '#' || line[0] == '^')
254 return NULL;
256 if (!got_parse_sha1_digest(id.sha1, line))
257 return got_error(GOT_ERR_BAD_REF_DATA);
259 if (abs_refname) {
260 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
261 return NULL;
262 name = abs_refname;
263 } else
264 name = line + SHA1_DIGEST_STRING_LENGTH;
266 return got_ref_alloc(ref, name, &id);
269 static const struct got_error *
270 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
271 int nsubdirs, const char *refname)
273 const struct got_error *err = NULL;
274 char *abs_refname;
275 char *line;
276 size_t len;
277 const char delim[3] = {'\0', '\0', '\0'};
278 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
280 *ref = NULL;
282 if (ref_is_absolute)
283 abs_refname = (char *)refname;
284 do {
285 line = fparseln(f, &len, NULL, delim, 0);
286 if (line == NULL)
287 break;
288 for (i = 0; i < nsubdirs; i++) {
289 if (!ref_is_absolute &&
290 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
291 refname) == -1)
292 return got_error_from_errno();
293 err = parse_packed_ref_line(ref, abs_refname, line);
294 if (!ref_is_absolute)
295 free(abs_refname);
296 if (err || *ref != NULL)
297 break;
299 free(line);
300 if (err)
301 break;
302 } while (*ref == NULL);
304 return err;
307 static const struct got_error *
308 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
309 const char *name)
311 const struct got_error *err = NULL;
312 char *path = NULL;
313 char *normpath = NULL;
314 char *absname = NULL;
315 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
316 int ref_is_well_known = is_well_known_ref(name);
318 *ref = NULL;
320 if (ref_is_absolute || ref_is_well_known) {
321 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
322 return got_error_from_errno();
323 absname = (char *)name;
324 } else {
325 if (asprintf(&path, "%s/%s/%s", path_refs, subdir, name) == -1)
326 return got_error_from_errno();
328 if (asprintf(&absname, "refs/%s/%s", subdir, name) == -1) {
329 err = got_error_from_errno();
330 goto done;
334 normpath = got_path_normalize(path);
335 if (normpath == NULL) {
336 err = got_error_from_errno();
337 goto done;
340 err = parse_ref_file(ref, absname, normpath);
341 done:
342 if (!ref_is_absolute && !ref_is_well_known)
343 free(absname);
344 free(path);
345 free(normpath);
346 return err;
349 const struct got_error *
350 got_ref_open(struct got_reference **ref, struct got_repository *repo,
351 const char *refname)
353 const struct got_error *err = NULL;
354 char *path_refs = NULL;
355 const char *subdirs[] = {
356 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
357 };
358 int i, well_known = is_well_known_ref(refname);
360 *ref = NULL;
362 path_refs = get_refs_dir_path(repo, refname);
363 if (path_refs == NULL) {
364 err = got_error_from_errno();
365 goto done;
368 if (!well_known) {
369 char *packed_refs_path;
370 FILE *f;
372 /* Search on-disk refs before packed refs! */
373 for (i = 0; i < nitems(subdirs); i++) {
374 err = open_ref(ref, path_refs, subdirs[i], refname);
375 if (err || *ref)
376 goto done;
379 packed_refs_path = got_repo_get_path_packed_refs(repo);
380 if (packed_refs_path == NULL) {
381 err = got_error_from_errno();
382 goto done;
385 f = fopen(packed_refs_path, "rb");
386 free(packed_refs_path);
387 if (f != NULL) {
388 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
389 refname);
390 if (fclose(f) != 0 && err == NULL)
391 err = got_error_from_errno();
392 if (err || *ref)
393 goto done;
397 err = open_ref(ref, path_refs, "", refname);
398 if (err)
399 goto done;
400 done:
401 if (*ref == NULL)
402 err = got_error_not_ref(refname);
403 free(path_refs);
404 return err;
407 void
408 got_ref_close(struct got_reference *ref)
410 if (ref->flags & GOT_REF_IS_SYMBOLIC)
411 free(ref->ref.symref.name);
412 else
413 free(ref->ref.ref.name);
414 free(ref);
417 struct got_reference *
418 got_ref_dup(struct got_reference *ref)
420 struct got_reference *ret;
422 ret = calloc(1, sizeof(*ret));
423 if (ret == NULL)
424 return NULL;
426 ret->flags = ref->flags;
427 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
428 ret->ref.symref.name = strdup(ref->ref.symref.name);
429 if (ret->ref.symref.name == NULL) {
430 free(ret);
431 return NULL;
433 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
434 if (ret->ref.symref.ref == NULL) {
435 free(ret->ref.symref.name);
436 free(ret);
437 return NULL;
439 } else {
440 ref->ref.ref.name = strdup(ref->ref.ref.name);
441 if (ref->ref.ref.name == NULL) {
442 free(ret);
443 return NULL;
445 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
446 SHA1_DIGEST_LENGTH);
449 return ret;
452 static const struct got_error *
453 resolve_symbolic_ref(struct got_reference **resolved,
454 struct got_repository *repo, struct got_reference *ref)
456 struct got_reference *nextref;
457 const struct got_error *err;
459 err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
460 if (err)
461 return err;
463 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
464 err = resolve_symbolic_ref(resolved, repo, nextref);
465 else
466 *resolved = got_ref_dup(nextref);
468 got_ref_close(nextref);
469 return err;
472 const struct got_error *
473 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
474 struct got_reference *ref)
476 const struct got_error *err;
478 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
479 struct got_reference *resolved = NULL;
480 err = resolve_symbolic_ref(&resolved, repo, ref);
481 if (err == NULL)
482 err = got_ref_resolve(id, repo, resolved);
483 free(resolved);
484 return err;
487 *id = calloc(1, sizeof(**id));
488 if (*id == NULL)
489 return got_error_from_errno();
490 memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
491 return NULL;
494 char *
495 got_ref_to_str(struct got_reference *ref)
497 char *str;
499 if (ref->flags & GOT_REF_IS_SYMBOLIC)
500 return strdup(ref->ref.symref.ref);
502 str = malloc(SHA1_DIGEST_STRING_LENGTH);
503 if (str == NULL)
504 return NULL;
506 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
507 SHA1_DIGEST_STRING_LENGTH) == NULL) {
508 free(str);
509 return NULL;
512 return str;
515 const char *
516 got_ref_get_name(struct got_reference *ref)
518 if (ref->flags & GOT_REF_IS_SYMBOLIC)
519 return ref->ref.symref.name;
521 return ref->ref.ref.name;
524 static const struct got_error *
525 insert_ref(struct got_reflist_head *refs, struct got_reference *ref,
526 struct got_repository *repo)
528 const struct got_error *err;
529 struct got_object_id *id;
530 struct got_reflist_entry *new, *re, *prev;
531 int cmp;
533 err = got_ref_resolve(&id, repo, ref);
534 if (err)
535 return err;
537 new = malloc(sizeof(*re));
538 if (new == NULL) {
539 free(id);
540 return got_error_from_errno();
542 new->ref = ref;
543 new->id = id;
545 /*
546 * We must de-duplicate entries on insert because packed-refs may
547 * contain redundant entries. On-disk refs take precedence.
548 * This code assumes that on-disk revs are read before packed-refs.
549 * We're iterating the list anyway, so insert elements sorted by name.
550 */
551 re = SIMPLEQ_FIRST(refs);
552 while (re) {
553 cmp = got_path_cmp(got_ref_get_name(re->ref),
554 got_ref_get_name(ref));
555 if (cmp == 0) {
556 free(ref); /* duplicate */
557 return NULL;
558 } else if (cmp > 0) {
559 if (prev)
560 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
561 else
562 SIMPLEQ_INSERT_HEAD(refs, new, entry);
563 return NULL;
564 } else {
565 prev = re;
566 re = SIMPLEQ_NEXT(re, entry);
570 SIMPLEQ_INSERT_TAIL(refs, new, entry);
571 return NULL;
574 static const struct got_error *
575 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
576 const char *subdir, struct got_repository *repo)
578 const struct got_error *err = NULL;
579 DIR *d = NULL;
580 char *path_subdir;
582 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
583 return got_error_from_errno();
585 d = opendir(path_subdir);
586 if (d == NULL)
587 goto done;
589 while (1) {
590 struct dirent *dent;
591 struct got_reference *ref;
592 char *child;
594 dent = readdir(d);
595 if (dent == NULL)
596 break;
598 if (strcmp(dent->d_name, ".") == 0 ||
599 strcmp(dent->d_name, "..") == 0)
600 continue;
602 switch (dent->d_type) {
603 case DT_REG:
604 err = open_ref(&ref, path_refs, subdir, dent->d_name);
605 if (err)
606 goto done;
607 if (ref) {
608 err = insert_ref(refs, ref, repo);
609 if (err)
610 goto done;
612 break;
613 case DT_DIR:
614 if (asprintf(&child, "%s%s%s", subdir,
615 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
616 err = got_error_from_errno();
617 break;
619 err = gather_on_disk_refs(refs, path_refs, child, repo);
620 free(child);
621 break;
622 default:
623 break;
626 done:
627 if (d)
628 closedir(d);
629 free(path_subdir);
630 return err;
633 const struct got_error *
634 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
636 const struct got_error *err;
637 char *packed_refs_path, *path_refs = NULL;
638 FILE *f = NULL;
639 struct got_reference *ref;
641 /* HEAD ref should always exist. */
642 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
643 if (path_refs == NULL) {
644 err = got_error_from_errno();
645 goto done;
647 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD);
648 if (err)
649 goto done;
650 err = insert_ref(refs, ref, repo);
651 if (err)
652 goto done;
654 /* Gather on-disk refs before parsing packed-refs. */
655 free(path_refs);
656 path_refs = get_refs_dir_path(repo, "");
657 if (path_refs == NULL) {
658 err = got_error_from_errno();
659 goto done;
661 err = gather_on_disk_refs(refs, path_refs, "", repo);
662 if (err)
663 goto done;
665 /*
666 * The packed-refs file may contain redundant entries, in which
667 * case on-disk refs take precedence.
668 */
669 packed_refs_path = got_repo_get_path_packed_refs(repo);
670 if (packed_refs_path == NULL) {
671 err = got_error_from_errno();
672 goto done;
675 f = fopen(packed_refs_path, "r");
676 free(packed_refs_path);
677 if (f) {
678 char *line;
679 size_t len;
680 const char delim[3] = {'\0', '\0', '\0'};
681 while (1) {
682 line = fparseln(f, &len, NULL, delim, 0);
683 if (line == NULL)
684 break;
685 err = parse_packed_ref_line(&ref, NULL, line);
686 if (err)
687 goto done;
688 if (ref) {
689 err = insert_ref(refs, ref, repo);
690 if (err)
691 goto done;
695 done:
696 free(path_refs);
697 if (f && fclose(f) != 0 && err == NULL)
698 err = got_error_from_errno();
699 return err;
702 const struct got_error *
703 got_ref_write(struct got_reference *ref, struct got_repository *repo)
705 const struct got_error *err = NULL, *unlock_err = NULL;
706 const char *name = got_ref_get_name(ref);
707 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
708 struct got_lockfile *lf = NULL;
709 FILE *f = NULL;
710 size_t n;
711 struct stat sb;
713 path_refs = get_refs_dir_path(repo, name);
714 if (path_refs == NULL) {
715 err = got_error_from_errno();
716 goto done;
719 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
720 err = got_error_from_errno();
721 goto done;
724 err = got_opentemp_named(&tmppath, &f, path);
725 if (f == NULL) {
726 err = got_error_from_errno();
727 goto done;
730 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
731 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
732 if (n != strlen(ref->ref.symref.ref) + 6) {
733 err = got_ferror(f, GOT_ERR_IO);
734 goto done;
736 } else {
737 char hex[SHA1_DIGEST_STRING_LENGTH];
738 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
739 sizeof(hex)) == NULL) {
740 err = got_error(GOT_ERR_BAD_REF_DATA);
741 goto done;
743 n = fprintf(f, "%s\n", hex);
744 if (n != sizeof(hex) + 1) {
745 err = got_ferror(f, GOT_ERR_IO);
746 goto done;
750 err = got_lockfile_lock(&lf, path);
751 if (err)
752 goto done;
754 /* XXX: check if old content matches our expectations? */
756 if (stat(path, &sb) != 0) {
757 err = got_error_from_errno();
758 goto done;
761 if (rename(tmppath, path) != 0) {
762 err = got_error_from_errno();
763 goto done;
765 free(tmppath);
766 tmppath = NULL;
768 if (chmod(path, sb.st_mode) != 0) {
769 err = got_error_from_errno();
770 goto done;
772 done:
773 if (lf)
774 unlock_err = got_lockfile_unlock(lf);
775 if (f) {
776 if (fclose(f) != 0 && err == NULL)
777 err = got_error_from_errno();
779 free(path_refs);
780 free(path);
781 if (tmppath) {
782 if (unlink(tmppath) != 0 && err == NULL)
783 err = got_error_from_errno();
784 free(tmppath);
786 return err ? err : unlock_err;