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_refs.h"
26 4027f31a 2017-11-04 stsp
27 c3f94f68 2017-11-05 stsp #include "path.h"
28 c3f94f68 2017-11-05 stsp
29 4027f31a 2017-11-04 stsp static const struct got_error *
30 4027f31a 2017-11-04 stsp parse_symref(struct got_reference **ref, const char *name, const char *line)
31 4027f31a 2017-11-04 stsp {
32 4027f31a 2017-11-04 stsp struct got_symref *symref;
33 4027f31a 2017-11-04 stsp char *symref_name;
34 4027f31a 2017-11-04 stsp char *symref_ref;
35 4027f31a 2017-11-04 stsp
36 4027f31a 2017-11-04 stsp if (line[0] == '\0')
37 4027f31a 2017-11-04 stsp return got_error(GOT_ERR_NOT_REF);
38 4027f31a 2017-11-04 stsp
39 4027f31a 2017-11-04 stsp symref_name = strdup(name);
40 4027f31a 2017-11-04 stsp if (symref_name == NULL)
41 4027f31a 2017-11-04 stsp return got_error(GOT_ERR_NO_MEM);
42 4027f31a 2017-11-04 stsp symref_ref = strdup(line);
43 4027f31a 2017-11-04 stsp if (symref_ref == NULL) {
44 4027f31a 2017-11-04 stsp free(symref_name);
45 4027f31a 2017-11-04 stsp return got_error(GOT_ERR_NO_MEM);
46 4027f31a 2017-11-04 stsp }
47 4027f31a 2017-11-04 stsp
48 4027f31a 2017-11-04 stsp *ref = calloc(1, sizeof(**ref));
49 4027f31a 2017-11-04 stsp (*ref)->flags |= GOT_REF_IS_SYMBOLIC;
50 4027f31a 2017-11-04 stsp symref = &((*ref)->ref.symref);
51 4027f31a 2017-11-04 stsp symref->name = symref_name;
52 4027f31a 2017-11-04 stsp symref->ref = symref_ref;
53 4027f31a 2017-11-04 stsp return NULL;
54 4027f31a 2017-11-04 stsp }
55 4027f31a 2017-11-04 stsp
56 4027f31a 2017-11-04 stsp static int
57 4027f31a 2017-11-04 stsp parse_sha1_digest(uint8_t *digest, const char *line)
58 4027f31a 2017-11-04 stsp {
59 4027f31a 2017-11-04 stsp uint8_t b;
60 4027f31a 2017-11-04 stsp int i, n;
61 4027f31a 2017-11-04 stsp
62 bbfe163a 2017-11-05 stsp memset(digest, 0, SHA1_DIGEST_LENGTH);
63 4027f31a 2017-11-04 stsp for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
64 4027f31a 2017-11-04 stsp n = sscanf(line, "%hhx", &b);
65 4027f31a 2017-11-04 stsp if (n == 1)
66 4027f31a 2017-11-04 stsp digest[i] = b;
67 4027f31a 2017-11-04 stsp else
68 4027f31a 2017-11-04 stsp return 0;
69 4027f31a 2017-11-04 stsp }
70 4027f31a 2017-11-04 stsp
71 4027f31a 2017-11-04 stsp return 1;
72 4027f31a 2017-11-04 stsp }
73 4027f31a 2017-11-04 stsp
74 4027f31a 2017-11-04 stsp static const struct got_error *
75 4027f31a 2017-11-04 stsp parse_ref_line(struct got_reference **ref, const char *name, const char *line)
76 4027f31a 2017-11-04 stsp {
77 4027f31a 2017-11-04 stsp uint8_t digest[SHA1_DIGEST_LENGTH];
78 4027f31a 2017-11-04 stsp char *ref_name;
79 4027f31a 2017-11-04 stsp
80 4027f31a 2017-11-04 stsp if (strncmp(line, "ref: ", 5) == 0) {
81 4027f31a 2017-11-04 stsp line += 5;
82 4027f31a 2017-11-04 stsp return parse_symref(ref, name, line);
83 4027f31a 2017-11-04 stsp }
84 4027f31a 2017-11-04 stsp
85 4027f31a 2017-11-04 stsp ref_name = strdup(name);
86 4027f31a 2017-11-04 stsp if (ref_name == NULL)
87 4027f31a 2017-11-04 stsp return got_error(GOT_ERR_NO_MEM);
88 4027f31a 2017-11-04 stsp
89 4027f31a 2017-11-04 stsp if (!parse_sha1_digest(digest, line))
90 4027f31a 2017-11-04 stsp return got_error(GOT_ERR_NOT_REF);
91 4027f31a 2017-11-04 stsp
92 4027f31a 2017-11-04 stsp *ref = calloc(1, sizeof(**ref));
93 4027f31a 2017-11-04 stsp (*ref)->ref.ref.name = ref_name;
94 4027f31a 2017-11-04 stsp memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
95 4027f31a 2017-11-04 stsp return NULL;
96 4027f31a 2017-11-04 stsp }
97 4027f31a 2017-11-04 stsp
98 4027f31a 2017-11-04 stsp static const struct got_error *
99 4027f31a 2017-11-04 stsp parse_ref_file(struct got_reference **ref, const char *name,
100 4027f31a 2017-11-04 stsp const char *abspath)
101 4027f31a 2017-11-04 stsp {
102 4027f31a 2017-11-04 stsp const struct got_error *err = NULL;
103 4027f31a 2017-11-04 stsp FILE *f = fopen(abspath, "rb");
104 4027f31a 2017-11-04 stsp char *line;
105 4027f31a 2017-11-04 stsp size_t len;
106 4027f31a 2017-11-04 stsp const char delim[3] = {'\0', '\0', '\0'};
107 4027f31a 2017-11-04 stsp
108 4027f31a 2017-11-04 stsp if (f == NULL)
109 4027f31a 2017-11-04 stsp return got_error(GOT_ERR_NOT_REF);
110 4027f31a 2017-11-04 stsp
111 4027f31a 2017-11-04 stsp line = fparseln(f, &len, NULL, delim, 0);
112 4027f31a 2017-11-04 stsp if (line == NULL) {
113 4027f31a 2017-11-04 stsp err = got_error(GOT_ERR_NOT_REF);
114 4027f31a 2017-11-04 stsp goto done;
115 4027f31a 2017-11-04 stsp }
116 4027f31a 2017-11-04 stsp
117 4027f31a 2017-11-04 stsp err = parse_ref_line(ref, name, line);
118 4027f31a 2017-11-04 stsp done:
119 4027f31a 2017-11-04 stsp free(line);
120 4027f31a 2017-11-04 stsp fclose(f);
121 4027f31a 2017-11-04 stsp return err;
122 4027f31a 2017-11-04 stsp }
123 4027f31a 2017-11-04 stsp
124 4027f31a 2017-11-04 stsp const struct got_error *
125 4027f31a 2017-11-04 stsp got_ref_open(struct got_reference **ref, const char *path_refs,
126 4027f31a 2017-11-04 stsp const char *refname)
127 4027f31a 2017-11-04 stsp {
128 4027f31a 2017-11-04 stsp const struct got_error *err = NULL;
129 4027f31a 2017-11-04 stsp char *path_ref = NULL;
130 4027f31a 2017-11-04 stsp char *normpath = NULL;
131 4027f31a 2017-11-04 stsp const char *parent_dir;
132 4027f31a 2017-11-04 stsp
133 4027f31a 2017-11-04 stsp /* XXX For now, this assumes that refs exist in the filesystem. */
134 4027f31a 2017-11-04 stsp
135 4027f31a 2017-11-04 stsp if (asprintf(&path_ref, "%s/%s", path_refs, refname) == -1) {
136 4027f31a 2017-11-04 stsp err = got_error(GOT_ERR_NO_MEM);
137 4027f31a 2017-11-04 stsp goto done;
138 4027f31a 2017-11-04 stsp }
139 4027f31a 2017-11-04 stsp
140 4027f31a 2017-11-04 stsp normpath = got_path_normalize(path_ref);
141 4027f31a 2017-11-04 stsp if (normpath == NULL) {
142 4027f31a 2017-11-04 stsp err = got_error(GOT_ERR_NOT_REF);
143 4027f31a 2017-11-04 stsp goto done;
144 4027f31a 2017-11-04 stsp }
145 4027f31a 2017-11-04 stsp
146 4027f31a 2017-11-04 stsp err = parse_ref_file(ref, refname, normpath ? normpath : path_refs);
147 4027f31a 2017-11-04 stsp done:
148 4027f31a 2017-11-04 stsp free(normpath);
149 4027f31a 2017-11-04 stsp free(path_ref);
150 4027f31a 2017-11-04 stsp return err;
151 4027f31a 2017-11-04 stsp }
152 4027f31a 2017-11-04 stsp
153 4027f31a 2017-11-04 stsp void
154 4027f31a 2017-11-04 stsp got_ref_close(struct got_reference *ref)
155 4027f31a 2017-11-04 stsp {
156 4027f31a 2017-11-04 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
157 4027f31a 2017-11-04 stsp free(ref->ref.symref.name);
158 4027f31a 2017-11-04 stsp else
159 4027f31a 2017-11-04 stsp free(ref->ref.ref.name);
160 4027f31a 2017-11-04 stsp free(ref);
161 4027f31a 2017-11-04 stsp }