Blame


1 7b19e0f1 2017-11-05 stsp /*
2 7b19e0f1 2017-11-05 stsp * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
3 7b19e0f1 2017-11-05 stsp *
4 7b19e0f1 2017-11-05 stsp * Permission to use, copy, modify, and distribute this software for any
5 7b19e0f1 2017-11-05 stsp * purpose with or without fee is hereby granted, provided that the above
6 7b19e0f1 2017-11-05 stsp * copyright notice and this permission notice appear in all copies.
7 7b19e0f1 2017-11-05 stsp *
8 7b19e0f1 2017-11-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 7b19e0f1 2017-11-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 7b19e0f1 2017-11-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 7b19e0f1 2017-11-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 7b19e0f1 2017-11-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 7b19e0f1 2017-11-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 7b19e0f1 2017-11-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 7b19e0f1 2017-11-05 stsp */
16 7b19e0f1 2017-11-05 stsp
17 4027f31a 2017-11-04 stsp #include <sys/types.h>
18 4027f31a 2017-11-04 stsp #include <sha1.h>
19 4027f31a 2017-11-04 stsp #include <stdio.h>
20 4027f31a 2017-11-04 stsp #include <stdlib.h>
21 4027f31a 2017-11-04 stsp #include <string.h>
22 4027f31a 2017-11-04 stsp #include <util.h>
23 4027f31a 2017-11-04 stsp
24 4027f31a 2017-11-04 stsp #include "got_error.h"
25 4027f31a 2017-11-04 stsp #include "got_path.h"
26 4027f31a 2017-11-04 stsp #include "got_refs.h"
27 4027f31a 2017-11-04 stsp
28 4027f31a 2017-11-04 stsp static const struct got_error *
29 4027f31a 2017-11-04 stsp parse_symref(struct got_reference **ref, const char *name, const char *line)
30 4027f31a 2017-11-04 stsp {
31 4027f31a 2017-11-04 stsp struct got_symref *symref;
32 4027f31a 2017-11-04 stsp char *symref_name;
33 4027f31a 2017-11-04 stsp char *symref_ref;
34 4027f31a 2017-11-04 stsp
35 4027f31a 2017-11-04 stsp if (line[0] == '\0')
36 4027f31a 2017-11-04 stsp return got_error(GOT_ERR_NOT_REF);
37 4027f31a 2017-11-04 stsp
38 4027f31a 2017-11-04 stsp symref_name = strdup(name);
39 4027f31a 2017-11-04 stsp if (symref_name == NULL)
40 4027f31a 2017-11-04 stsp return got_error(GOT_ERR_NO_MEM);
41 4027f31a 2017-11-04 stsp symref_ref = strdup(line);
42 4027f31a 2017-11-04 stsp if (symref_ref == NULL) {
43 4027f31a 2017-11-04 stsp free(symref_name);
44 4027f31a 2017-11-04 stsp return got_error(GOT_ERR_NO_MEM);
45 4027f31a 2017-11-04 stsp }
46 4027f31a 2017-11-04 stsp
47 4027f31a 2017-11-04 stsp *ref = calloc(1, sizeof(**ref));
48 4027f31a 2017-11-04 stsp (*ref)->flags |= GOT_REF_IS_SYMBOLIC;
49 4027f31a 2017-11-04 stsp symref = &((*ref)->ref.symref);
50 4027f31a 2017-11-04 stsp symref->name = symref_name;
51 4027f31a 2017-11-04 stsp symref->ref = symref_ref;
52 4027f31a 2017-11-04 stsp return NULL;
53 4027f31a 2017-11-04 stsp }
54 4027f31a 2017-11-04 stsp
55 4027f31a 2017-11-04 stsp static int
56 4027f31a 2017-11-04 stsp parse_sha1_digest(uint8_t *digest, const char *line)
57 4027f31a 2017-11-04 stsp {
58 4027f31a 2017-11-04 stsp uint8_t b;
59 4027f31a 2017-11-04 stsp int i, n;
60 4027f31a 2017-11-04 stsp
61 4027f31a 2017-11-04 stsp for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
62 4027f31a 2017-11-04 stsp n = sscanf(line, "%hhx", &b);
63 4027f31a 2017-11-04 stsp if (n == 1)
64 4027f31a 2017-11-04 stsp digest[i] = b;
65 4027f31a 2017-11-04 stsp else
66 4027f31a 2017-11-04 stsp return 0;
67 4027f31a 2017-11-04 stsp }
68 4027f31a 2017-11-04 stsp
69 4027f31a 2017-11-04 stsp return 1;
70 4027f31a 2017-11-04 stsp }
71 4027f31a 2017-11-04 stsp
72 4027f31a 2017-11-04 stsp static const struct got_error *
73 4027f31a 2017-11-04 stsp parse_ref_line(struct got_reference **ref, const char *name, const char *line)
74 4027f31a 2017-11-04 stsp {
75 4027f31a 2017-11-04 stsp uint8_t digest[SHA1_DIGEST_LENGTH];
76 4027f31a 2017-11-04 stsp char *ref_name;
77 4027f31a 2017-11-04 stsp
78 4027f31a 2017-11-04 stsp if (strncmp(line, "ref: ", 5) == 0) {
79 4027f31a 2017-11-04 stsp line += 5;
80 4027f31a 2017-11-04 stsp return parse_symref(ref, name, line);
81 4027f31a 2017-11-04 stsp }
82 4027f31a 2017-11-04 stsp
83 4027f31a 2017-11-04 stsp ref_name = strdup(name);
84 4027f31a 2017-11-04 stsp if (ref_name == NULL)
85 4027f31a 2017-11-04 stsp return got_error(GOT_ERR_NO_MEM);
86 4027f31a 2017-11-04 stsp
87 4027f31a 2017-11-04 stsp if (!parse_sha1_digest(digest, line))
88 4027f31a 2017-11-04 stsp return got_error(GOT_ERR_NOT_REF);
89 4027f31a 2017-11-04 stsp
90 4027f31a 2017-11-04 stsp *ref = calloc(1, sizeof(**ref));
91 4027f31a 2017-11-04 stsp (*ref)->ref.ref.name = ref_name;
92 4027f31a 2017-11-04 stsp memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
93 4027f31a 2017-11-04 stsp return NULL;
94 4027f31a 2017-11-04 stsp }
95 4027f31a 2017-11-04 stsp
96 4027f31a 2017-11-04 stsp static const struct got_error *
97 4027f31a 2017-11-04 stsp parse_ref_file(struct got_reference **ref, const char *name,
98 4027f31a 2017-11-04 stsp const char *abspath)
99 4027f31a 2017-11-04 stsp {
100 4027f31a 2017-11-04 stsp const struct got_error *err = NULL;
101 4027f31a 2017-11-04 stsp FILE *f = fopen(abspath, "rb");
102 4027f31a 2017-11-04 stsp char *line;
103 4027f31a 2017-11-04 stsp size_t len;
104 4027f31a 2017-11-04 stsp const char delim[3] = {'\0', '\0', '\0'};
105 4027f31a 2017-11-04 stsp
106 4027f31a 2017-11-04 stsp if (f == NULL)
107 4027f31a 2017-11-04 stsp return got_error(GOT_ERR_NOT_REF);
108 4027f31a 2017-11-04 stsp
109 4027f31a 2017-11-04 stsp line = fparseln(f, &len, NULL, delim, 0);
110 4027f31a 2017-11-04 stsp if (line == NULL) {
111 4027f31a 2017-11-04 stsp err = got_error(GOT_ERR_NOT_REF);
112 4027f31a 2017-11-04 stsp goto done;
113 4027f31a 2017-11-04 stsp }
114 4027f31a 2017-11-04 stsp
115 4027f31a 2017-11-04 stsp err = parse_ref_line(ref, name, line);
116 4027f31a 2017-11-04 stsp done:
117 4027f31a 2017-11-04 stsp free(line);
118 4027f31a 2017-11-04 stsp fclose(f);
119 4027f31a 2017-11-04 stsp return err;
120 4027f31a 2017-11-04 stsp }
121 4027f31a 2017-11-04 stsp
122 4027f31a 2017-11-04 stsp const struct got_error *
123 4027f31a 2017-11-04 stsp got_ref_open(struct got_reference **ref, const char *path_refs,
124 4027f31a 2017-11-04 stsp const char *refname)
125 4027f31a 2017-11-04 stsp {
126 4027f31a 2017-11-04 stsp const struct got_error *err = NULL;
127 4027f31a 2017-11-04 stsp char *path_ref = NULL;
128 4027f31a 2017-11-04 stsp char *normpath = NULL;
129 4027f31a 2017-11-04 stsp const char *parent_dir;
130 4027f31a 2017-11-04 stsp
131 4027f31a 2017-11-04 stsp /* XXX For now, this assumes that refs exist in the filesystem. */
132 4027f31a 2017-11-04 stsp
133 4027f31a 2017-11-04 stsp if (asprintf(&path_ref, "%s/%s", path_refs, refname) == -1) {
134 4027f31a 2017-11-04 stsp err = got_error(GOT_ERR_NO_MEM);
135 4027f31a 2017-11-04 stsp goto done;
136 4027f31a 2017-11-04 stsp }
137 4027f31a 2017-11-04 stsp
138 4027f31a 2017-11-04 stsp normpath = got_path_normalize(path_ref);
139 4027f31a 2017-11-04 stsp if (normpath == NULL) {
140 4027f31a 2017-11-04 stsp err = got_error(GOT_ERR_NOT_REF);
141 4027f31a 2017-11-04 stsp goto done;
142 4027f31a 2017-11-04 stsp }
143 4027f31a 2017-11-04 stsp
144 4027f31a 2017-11-04 stsp err = parse_ref_file(ref, refname, normpath ? normpath : path_refs);
145 4027f31a 2017-11-04 stsp done:
146 4027f31a 2017-11-04 stsp free(normpath);
147 4027f31a 2017-11-04 stsp free(path_ref);
148 4027f31a 2017-11-04 stsp return err;
149 4027f31a 2017-11-04 stsp }
150 4027f31a 2017-11-04 stsp
151 4027f31a 2017-11-04 stsp void
152 4027f31a 2017-11-04 stsp got_ref_close(struct got_reference *ref)
153 4027f31a 2017-11-04 stsp {
154 4027f31a 2017-11-04 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
155 4027f31a 2017-11-04 stsp free(ref->ref.symref.name);
156 4027f31a 2017-11-04 stsp else
157 4027f31a 2017-11-04 stsp free(ref->ref.ref.name);
158 4027f31a 2017-11-04 stsp free(ref);
159 4027f31a 2017-11-04 stsp }