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