Blame


1 5261c201 2018-04-01 stsp /*
2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 5261c201 2018-04-01 stsp *
4 5261c201 2018-04-01 stsp * Permission to use, copy, modify, and distribute this software for any
5 5261c201 2018-04-01 stsp * purpose with or without fee is hereby granted, provided that the above
6 5261c201 2018-04-01 stsp * copyright notice and this permission notice appear in all copies.
7 5261c201 2018-04-01 stsp *
8 5261c201 2018-04-01 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 5261c201 2018-04-01 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 5261c201 2018-04-01 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 5261c201 2018-04-01 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 5261c201 2018-04-01 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 5261c201 2018-04-01 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 5261c201 2018-04-01 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 5261c201 2018-04-01 stsp */
16 5261c201 2018-04-01 stsp
17 5261c201 2018-04-01 stsp #include <sys/types.h>
18 5261c201 2018-04-01 stsp #include <sys/queue.h>
19 9e672c74 2019-03-11 stsp #include <sys/stat.h>
20 5261c201 2018-04-01 stsp
21 0fd469ce 2019-03-11 stsp #include <errno.h>
22 f77a24b0 2019-03-11 stsp #include <ctype.h>
23 a04f49d2 2019-02-04 stsp #include <dirent.h>
24 5261c201 2018-04-01 stsp #include <sha1.h>
25 5261c201 2018-04-01 stsp #include <stdio.h>
26 5261c201 2018-04-01 stsp #include <stdlib.h>
27 5261c201 2018-04-01 stsp #include <string.h>
28 5261c201 2018-04-01 stsp #include <util.h>
29 5261c201 2018-04-01 stsp #include <zlib.h>
30 788c352e 2018-06-16 stsp #include <time.h>
31 0cd1c46a 2019-03-11 stsp #include <libgen.h>
32 5261c201 2018-04-01 stsp
33 5261c201 2018-04-01 stsp #include "got_error.h"
34 5261c201 2018-04-01 stsp #include "got_object.h"
35 5261c201 2018-04-01 stsp #include "got_repository.h"
36 5261c201 2018-04-01 stsp #include "got_reference.h"
37 9e672c74 2019-03-11 stsp #include "got_opentemp.h"
38 5261c201 2018-04-01 stsp
39 5261c201 2018-04-01 stsp #include "got_lib_sha1.h"
40 5261c201 2018-04-01 stsp #include "got_lib_path.h"
41 5261c201 2018-04-01 stsp #include "got_lib_delta.h"
42 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
43 5261c201 2018-04-01 stsp #include "got_lib_object.h"
44 f77a24b0 2019-03-11 stsp #include "got_lib_lockfile.h"
45 5261c201 2018-04-01 stsp
46 fb79db15 2019-02-01 stsp #ifndef nitems
47 fb79db15 2019-02-01 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
48 fb79db15 2019-02-01 stsp #endif
49 fb79db15 2019-02-01 stsp
50 d1f2edc9 2018-06-13 stsp #define GOT_REF_HEADS "heads"
51 d1f2edc9 2018-06-13 stsp #define GOT_REF_TAGS "tags"
52 d1f2edc9 2018-06-13 stsp #define GOT_REF_REMOTES "remotes"
53 d1f2edc9 2018-06-13 stsp
54 5261c201 2018-04-01 stsp /* A symbolic reference. */
55 5261c201 2018-04-01 stsp struct got_symref {
56 5261c201 2018-04-01 stsp char *name;
57 5261c201 2018-04-01 stsp char *ref;
58 5261c201 2018-04-01 stsp };
59 5261c201 2018-04-01 stsp
60 5261c201 2018-04-01 stsp /* A non-symbolic reference (there is no better designation). */
61 5261c201 2018-04-01 stsp struct got_ref {
62 5261c201 2018-04-01 stsp char *name;
63 5261c201 2018-04-01 stsp u_int8_t sha1[SHA1_DIGEST_LENGTH];
64 5261c201 2018-04-01 stsp };
65 5261c201 2018-04-01 stsp
66 5261c201 2018-04-01 stsp /* A reference which points to an arbitrary object. */
67 5261c201 2018-04-01 stsp struct got_reference {
68 5261c201 2018-04-01 stsp unsigned int flags;
69 5261c201 2018-04-01 stsp #define GOT_REF_IS_SYMBOLIC 0x01
70 5261c201 2018-04-01 stsp
71 5261c201 2018-04-01 stsp union {
72 5261c201 2018-04-01 stsp struct got_ref ref;
73 5261c201 2018-04-01 stsp struct got_symref symref;
74 5261c201 2018-04-01 stsp } ref;
75 5261c201 2018-04-01 stsp };
76 5261c201 2018-04-01 stsp
77 5261c201 2018-04-01 stsp static const struct got_error *
78 5261c201 2018-04-01 stsp parse_symref(struct got_reference **ref, const char *name, const char *line)
79 5261c201 2018-04-01 stsp {
80 5261c201 2018-04-01 stsp struct got_symref *symref;
81 5261c201 2018-04-01 stsp char *symref_name;
82 5261c201 2018-04-01 stsp char *symref_ref;
83 5261c201 2018-04-01 stsp
84 5261c201 2018-04-01 stsp if (line[0] == '\0')
85 30c0868d 2019-02-03 stsp return got_error(GOT_ERR_BAD_REF_DATA);
86 5261c201 2018-04-01 stsp
87 5261c201 2018-04-01 stsp symref_name = strdup(name);
88 5261c201 2018-04-01 stsp if (symref_name == NULL)
89 5261c201 2018-04-01 stsp return got_error_from_errno();
90 5261c201 2018-04-01 stsp symref_ref = strdup(line);
91 5261c201 2018-04-01 stsp if (symref_ref == NULL) {
92 5261c201 2018-04-01 stsp const struct got_error *err = got_error_from_errno();
93 5261c201 2018-04-01 stsp free(symref_name);
94 5261c201 2018-04-01 stsp return err;
95 5261c201 2018-04-01 stsp }
96 5261c201 2018-04-01 stsp
97 5261c201 2018-04-01 stsp *ref = calloc(1, sizeof(**ref));
98 5261c201 2018-04-01 stsp if (*ref == NULL)
99 5261c201 2018-04-01 stsp return got_error_from_errno();
100 5261c201 2018-04-01 stsp (*ref)->flags |= GOT_REF_IS_SYMBOLIC;
101 5261c201 2018-04-01 stsp symref = &((*ref)->ref.symref);
102 5261c201 2018-04-01 stsp symref->name = symref_name;
103 5261c201 2018-04-01 stsp symref->ref = symref_ref;
104 5261c201 2018-04-01 stsp return NULL;
105 5261c201 2018-04-01 stsp }
106 5261c201 2018-04-01 stsp
107 5261c201 2018-04-01 stsp static const struct got_error *
108 5261c201 2018-04-01 stsp parse_ref_line(struct got_reference **ref, const char *name, const char *line)
109 5261c201 2018-04-01 stsp {
110 5892cdd6 2019-03-10 stsp struct got_object_id id;
111 5261c201 2018-04-01 stsp
112 5261c201 2018-04-01 stsp if (strncmp(line, "ref: ", 5) == 0) {
113 5261c201 2018-04-01 stsp line += 5;
114 5261c201 2018-04-01 stsp return parse_symref(ref, name, line);
115 5261c201 2018-04-01 stsp }
116 5261c201 2018-04-01 stsp
117 5892cdd6 2019-03-10 stsp if (!got_parse_sha1_digest(id.sha1, line))
118 30c0868d 2019-02-03 stsp return got_error(GOT_ERR_BAD_REF_DATA);
119 5261c201 2018-04-01 stsp
120 5892cdd6 2019-03-10 stsp return got_ref_alloc(ref, name, &id);
121 5261c201 2018-04-01 stsp }
122 5261c201 2018-04-01 stsp
123 5261c201 2018-04-01 stsp static const struct got_error *
124 5261c201 2018-04-01 stsp parse_ref_file(struct got_reference **ref, const char *name,
125 5261c201 2018-04-01 stsp const char *abspath)
126 5261c201 2018-04-01 stsp {
127 5261c201 2018-04-01 stsp const struct got_error *err = NULL;
128 5261c201 2018-04-01 stsp FILE *f = fopen(abspath, "rb");
129 5261c201 2018-04-01 stsp char *line;
130 5261c201 2018-04-01 stsp size_t len;
131 5261c201 2018-04-01 stsp const char delim[3] = {'\0', '\0', '\0'};
132 5261c201 2018-04-01 stsp
133 5261c201 2018-04-01 stsp if (f == NULL)
134 1e37702e 2019-02-04 stsp return NULL;
135 5261c201 2018-04-01 stsp
136 5261c201 2018-04-01 stsp line = fparseln(f, &len, NULL, delim, 0);
137 5261c201 2018-04-01 stsp if (line == NULL) {
138 30c0868d 2019-02-03 stsp err = got_error(GOT_ERR_BAD_REF_DATA);
139 5261c201 2018-04-01 stsp goto done;
140 5261c201 2018-04-01 stsp }
141 5261c201 2018-04-01 stsp
142 5261c201 2018-04-01 stsp err = parse_ref_line(ref, name, line);
143 5261c201 2018-04-01 stsp done:
144 5261c201 2018-04-01 stsp free(line);
145 fb43ecf1 2019-02-11 stsp if (fclose(f) != 0 && err == NULL)
146 fb43ecf1 2019-02-11 stsp err = got_error_from_errno();
147 5261c201 2018-04-01 stsp return err;
148 5261c201 2018-04-01 stsp }
149 5261c201 2018-04-01 stsp
150 c5f754cc 2019-02-01 stsp static int
151 c5f754cc 2019-02-01 stsp is_well_known_ref(const char *refname)
152 5261c201 2018-04-01 stsp {
153 c5f754cc 2019-02-01 stsp return (strcmp(refname, GOT_REF_HEAD) == 0 ||
154 5261c201 2018-04-01 stsp strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
155 5261c201 2018-04-01 stsp strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
156 c5f754cc 2019-02-01 stsp strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
157 c5f754cc 2019-02-01 stsp }
158 c5f754cc 2019-02-01 stsp
159 c5f754cc 2019-02-01 stsp static char *
160 c5f754cc 2019-02-01 stsp get_refs_dir_path(struct got_repository *repo, const char *refname)
161 c5f754cc 2019-02-01 stsp {
162 c5f754cc 2019-02-01 stsp if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
163 6e9da951 2019-01-06 stsp return strdup(got_repo_get_path_git_dir(repo));
164 5261c201 2018-04-01 stsp
165 5261c201 2018-04-01 stsp return got_repo_get_path_refs(repo);
166 f77a24b0 2019-03-11 stsp }
167 f77a24b0 2019-03-11 stsp
168 f77a24b0 2019-03-11 stsp static int
169 f77a24b0 2019-03-11 stsp is_valid_ref_name(const char *name)
170 f77a24b0 2019-03-11 stsp {
171 f77a24b0 2019-03-11 stsp const char *s, *slash, *seg;
172 f77a24b0 2019-03-11 stsp const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
173 f77a24b0 2019-03-11 stsp const char *forbidden_seq[] = { "//", "..", "@{" };
174 f77a24b0 2019-03-11 stsp const char *lfs = GOT_LOCKFILE_SUFFIX;
175 f77a24b0 2019-03-11 stsp const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
176 f77a24b0 2019-03-11 stsp int i;
177 f77a24b0 2019-03-11 stsp
178 f77a24b0 2019-03-11 stsp if (name[0] == '@' && name[1] == '\0')
179 f77a24b0 2019-03-11 stsp return 0;
180 f77a24b0 2019-03-11 stsp
181 f77a24b0 2019-03-11 stsp slash = strchr(name, '/');
182 f77a24b0 2019-03-11 stsp if (slash == NULL)
183 f77a24b0 2019-03-11 stsp return 0;
184 f77a24b0 2019-03-11 stsp
185 f77a24b0 2019-03-11 stsp s = name;
186 f77a24b0 2019-03-11 stsp seg = s;
187 f77a24b0 2019-03-11 stsp if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
188 f77a24b0 2019-03-11 stsp return 0;
189 f77a24b0 2019-03-11 stsp while (*s) {
190 f77a24b0 2019-03-11 stsp for (i = 0; i < nitems(forbidden); i++) {
191 f77a24b0 2019-03-11 stsp if (*s == forbidden[i])
192 f77a24b0 2019-03-11 stsp return 0;
193 f77a24b0 2019-03-11 stsp }
194 f77a24b0 2019-03-11 stsp for (i = 0; i < nitems(forbidden_seq); i++) {
195 f77a24b0 2019-03-11 stsp if (s[0] == forbidden_seq[i][0] &&
196 f77a24b0 2019-03-11 stsp s[1] == forbidden_seq[i][1])
197 f77a24b0 2019-03-11 stsp return 0;
198 f77a24b0 2019-03-11 stsp }
199 f77a24b0 2019-03-11 stsp if (iscntrl((unsigned char)s[0]))
200 f77a24b0 2019-03-11 stsp return 0;
201 f77a24b0 2019-03-11 stsp if (s[0] == '.' && s[1] == '\0')
202 f77a24b0 2019-03-11 stsp return 0;
203 f77a24b0 2019-03-11 stsp if (*s == '/') {
204 f77a24b0 2019-03-11 stsp const char *nextseg = s + 1;
205 f77a24b0 2019-03-11 stsp if (nextseg[0] == '\0' || nextseg[0] == '.' ||
206 f77a24b0 2019-03-11 stsp nextseg[0] == '/')
207 f77a24b0 2019-03-11 stsp return 0;
208 f77a24b0 2019-03-11 stsp if (seg <= s - lfs_len &&
209 f77a24b0 2019-03-11 stsp strncmp(s - lfs_len, lfs, lfs_len) == 0)
210 f77a24b0 2019-03-11 stsp return 0;
211 f77a24b0 2019-03-11 stsp seg = nextseg;
212 f77a24b0 2019-03-11 stsp }
213 f77a24b0 2019-03-11 stsp s++;
214 f77a24b0 2019-03-11 stsp }
215 f77a24b0 2019-03-11 stsp
216 f77a24b0 2019-03-11 stsp if (seg <= s - lfs_len &&
217 f77a24b0 2019-03-11 stsp strncmp(s - lfs_len, lfs, lfs_len) == 0)
218 f77a24b0 2019-03-11 stsp return 0;
219 f77a24b0 2019-03-11 stsp
220 f77a24b0 2019-03-11 stsp return 1;
221 5892cdd6 2019-03-10 stsp }
222 5892cdd6 2019-03-10 stsp
223 5892cdd6 2019-03-10 stsp const struct got_error *
224 5892cdd6 2019-03-10 stsp got_ref_alloc(struct got_reference **ref, const char *name,
225 5892cdd6 2019-03-10 stsp struct got_object_id *id)
226 5892cdd6 2019-03-10 stsp {
227 5892cdd6 2019-03-10 stsp const struct got_error *err = NULL;
228 5892cdd6 2019-03-10 stsp
229 f77a24b0 2019-03-11 stsp if (!is_valid_ref_name(name))
230 f77a24b0 2019-03-11 stsp return got_error(GOT_ERR_BAD_REF_NAME);
231 f77a24b0 2019-03-11 stsp
232 5892cdd6 2019-03-10 stsp *ref = calloc(1, sizeof(**ref));
233 5892cdd6 2019-03-10 stsp if (*ref == NULL)
234 5892cdd6 2019-03-10 stsp return got_error_from_errno();
235 5892cdd6 2019-03-10 stsp
236 5892cdd6 2019-03-10 stsp memcpy(&(*ref)->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
237 5892cdd6 2019-03-10 stsp (*ref)->ref.ref.name = strdup(name);
238 5892cdd6 2019-03-10 stsp if ((*ref)->ref.ref.name == NULL) {
239 5892cdd6 2019-03-10 stsp err = got_error_from_errno();
240 5892cdd6 2019-03-10 stsp free(*ref);
241 5892cdd6 2019-03-10 stsp *ref = NULL;
242 5892cdd6 2019-03-10 stsp }
243 5892cdd6 2019-03-10 stsp return err;
244 5261c201 2018-04-01 stsp }
245 5261c201 2018-04-01 stsp
246 d1f2edc9 2018-06-13 stsp static const struct got_error *
247 fb79db15 2019-02-01 stsp parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
248 fb79db15 2019-02-01 stsp const char *line)
249 fb79db15 2019-02-01 stsp {
250 5892cdd6 2019-03-10 stsp struct got_object_id id;
251 5892cdd6 2019-03-10 stsp const char *name;
252 fb79db15 2019-02-01 stsp
253 fb79db15 2019-02-01 stsp *ref = NULL;
254 fb79db15 2019-02-01 stsp
255 532920c8 2019-02-01 stsp if (line[0] == '#' || line[0] == '^')
256 fb79db15 2019-02-01 stsp return NULL;
257 fb79db15 2019-02-01 stsp
258 5892cdd6 2019-03-10 stsp if (!got_parse_sha1_digest(id.sha1, line))
259 30c0868d 2019-02-03 stsp return got_error(GOT_ERR_BAD_REF_DATA);
260 fb79db15 2019-02-01 stsp
261 199a4027 2019-02-02 stsp if (abs_refname) {
262 199a4027 2019-02-02 stsp if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
263 199a4027 2019-02-02 stsp return NULL;
264 5892cdd6 2019-03-10 stsp name = abs_refname;
265 5892cdd6 2019-03-10 stsp } else
266 5892cdd6 2019-03-10 stsp name = line + SHA1_DIGEST_STRING_LENGTH;
267 fb79db15 2019-02-01 stsp
268 5892cdd6 2019-03-10 stsp return got_ref_alloc(ref, name, &id);
269 fb79db15 2019-02-01 stsp }
270 fb79db15 2019-02-01 stsp
271 fb79db15 2019-02-01 stsp static const struct got_error *
272 0dec1cc0 2019-02-01 stsp open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
273 0dec1cc0 2019-02-01 stsp int nsubdirs, const char *refname)
274 fb79db15 2019-02-01 stsp {
275 fb79db15 2019-02-01 stsp const struct got_error *err = NULL;
276 fb79db15 2019-02-01 stsp char *abs_refname;
277 fb79db15 2019-02-01 stsp char *line;
278 fb79db15 2019-02-01 stsp size_t len;
279 fb79db15 2019-02-01 stsp const char delim[3] = {'\0', '\0', '\0'};
280 0dec1cc0 2019-02-01 stsp int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
281 fb79db15 2019-02-01 stsp
282 1e37702e 2019-02-04 stsp *ref = NULL;
283 1e37702e 2019-02-04 stsp
284 0dec1cc0 2019-02-01 stsp if (ref_is_absolute)
285 0dec1cc0 2019-02-01 stsp abs_refname = (char *)refname;
286 fb79db15 2019-02-01 stsp do {
287 fb79db15 2019-02-01 stsp line = fparseln(f, &len, NULL, delim, 0);
288 1e37702e 2019-02-04 stsp if (line == NULL)
289 fb79db15 2019-02-01 stsp break;
290 0dec1cc0 2019-02-01 stsp for (i = 0; i < nsubdirs; i++) {
291 0dec1cc0 2019-02-01 stsp if (!ref_is_absolute &&
292 0dec1cc0 2019-02-01 stsp asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
293 0dec1cc0 2019-02-01 stsp refname) == -1)
294 0dec1cc0 2019-02-01 stsp return got_error_from_errno();
295 0dec1cc0 2019-02-01 stsp err = parse_packed_ref_line(ref, abs_refname, line);
296 0dec1cc0 2019-02-01 stsp if (!ref_is_absolute)
297 0dec1cc0 2019-02-01 stsp free(abs_refname);
298 532920c8 2019-02-01 stsp if (err || *ref != NULL)
299 0dec1cc0 2019-02-01 stsp break;
300 0dec1cc0 2019-02-01 stsp }
301 fb79db15 2019-02-01 stsp free(line);
302 fb79db15 2019-02-01 stsp if (err)
303 fb79db15 2019-02-01 stsp break;
304 fb79db15 2019-02-01 stsp } while (*ref == NULL);
305 fb79db15 2019-02-01 stsp
306 fb79db15 2019-02-01 stsp return err;
307 fb79db15 2019-02-01 stsp }
308 fb79db15 2019-02-01 stsp
309 fb79db15 2019-02-01 stsp static const struct got_error *
310 d1f2edc9 2018-06-13 stsp open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
311 a04f49d2 2019-02-04 stsp const char *name)
312 d1f2edc9 2018-06-13 stsp {
313 d1f2edc9 2018-06-13 stsp const struct got_error *err = NULL;
314 a04f49d2 2019-02-04 stsp char *path = NULL;
315 a04f49d2 2019-02-04 stsp char *normpath = NULL;
316 a04f49d2 2019-02-04 stsp char *absname = NULL;
317 a04f49d2 2019-02-04 stsp int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
318 a04f49d2 2019-02-04 stsp int ref_is_well_known = is_well_known_ref(name);
319 d1f2edc9 2018-06-13 stsp
320 1e37702e 2019-02-04 stsp *ref = NULL;
321 1e37702e 2019-02-04 stsp
322 a04f49d2 2019-02-04 stsp if (ref_is_absolute || ref_is_well_known) {
323 a04f49d2 2019-02-04 stsp if (asprintf(&path, "%s/%s", path_refs, name) == -1)
324 a04f49d2 2019-02-04 stsp return got_error_from_errno();
325 a04f49d2 2019-02-04 stsp absname = (char *)name;
326 a04f49d2 2019-02-04 stsp } else {
327 58908ed0 2019-03-11 stsp if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
328 58908ed0 2019-03-11 stsp subdir[0] ? "/" : "", name) == -1)
329 a04f49d2 2019-02-04 stsp return got_error_from_errno();
330 d1f2edc9 2018-06-13 stsp
331 58908ed0 2019-03-11 stsp if (asprintf(&absname, "refs/%s%s%s",
332 58908ed0 2019-03-11 stsp subdir, subdir[0] ? "/" : "", name) == -1) {
333 a04f49d2 2019-02-04 stsp err = got_error_from_errno();
334 a04f49d2 2019-02-04 stsp goto done;
335 a04f49d2 2019-02-04 stsp }
336 a04f49d2 2019-02-04 stsp }
337 a04f49d2 2019-02-04 stsp
338 a04f49d2 2019-02-04 stsp normpath = got_path_normalize(path);
339 d1f2edc9 2018-06-13 stsp if (normpath == NULL) {
340 30c0868d 2019-02-03 stsp err = got_error_from_errno();
341 d1f2edc9 2018-06-13 stsp goto done;
342 d1f2edc9 2018-06-13 stsp }
343 d1f2edc9 2018-06-13 stsp
344 a04f49d2 2019-02-04 stsp err = parse_ref_file(ref, absname, normpath);
345 d1f2edc9 2018-06-13 stsp done:
346 a04f49d2 2019-02-04 stsp if (!ref_is_absolute && !ref_is_well_known)
347 a04f49d2 2019-02-04 stsp free(absname);
348 a04f49d2 2019-02-04 stsp free(path);
349 d1f2edc9 2018-06-13 stsp free(normpath);
350 d1f2edc9 2018-06-13 stsp return err;
351 d1f2edc9 2018-06-13 stsp }
352 d1f2edc9 2018-06-13 stsp
353 5261c201 2018-04-01 stsp const struct got_error *
354 5261c201 2018-04-01 stsp got_ref_open(struct got_reference **ref, struct got_repository *repo,
355 5261c201 2018-04-01 stsp const char *refname)
356 5261c201 2018-04-01 stsp {
357 5261c201 2018-04-01 stsp const struct got_error *err = NULL;
358 c5f754cc 2019-02-01 stsp char *path_refs = NULL;
359 fb79db15 2019-02-01 stsp const char *subdirs[] = {
360 fb79db15 2019-02-01 stsp GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
361 fb79db15 2019-02-01 stsp };
362 c5f754cc 2019-02-01 stsp int i, well_known = is_well_known_ref(refname);
363 1e37702e 2019-02-04 stsp
364 1e37702e 2019-02-04 stsp *ref = NULL;
365 e135804e 2019-02-10 stsp
366 e135804e 2019-02-10 stsp path_refs = get_refs_dir_path(repo, refname);
367 e135804e 2019-02-10 stsp if (path_refs == NULL) {
368 e135804e 2019-02-10 stsp err = got_error_from_errno();
369 e135804e 2019-02-10 stsp goto done;
370 e135804e 2019-02-10 stsp }
371 5261c201 2018-04-01 stsp
372 c5f754cc 2019-02-01 stsp if (!well_known) {
373 c5f754cc 2019-02-01 stsp char *packed_refs_path;
374 c5f754cc 2019-02-01 stsp FILE *f;
375 c5f754cc 2019-02-01 stsp
376 e135804e 2019-02-10 stsp /* Search on-disk refs before packed refs! */
377 e135804e 2019-02-10 stsp for (i = 0; i < nitems(subdirs); i++) {
378 e135804e 2019-02-10 stsp err = open_ref(ref, path_refs, subdirs[i], refname);
379 e135804e 2019-02-10 stsp if (err || *ref)
380 e135804e 2019-02-10 stsp goto done;
381 e135804e 2019-02-10 stsp }
382 e135804e 2019-02-10 stsp
383 c5f754cc 2019-02-01 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
384 e135804e 2019-02-10 stsp if (packed_refs_path == NULL) {
385 e135804e 2019-02-10 stsp err = got_error_from_errno();
386 e135804e 2019-02-10 stsp goto done;
387 e135804e 2019-02-10 stsp }
388 c5f754cc 2019-02-01 stsp
389 c5f754cc 2019-02-01 stsp f = fopen(packed_refs_path, "rb");
390 c5f754cc 2019-02-01 stsp free(packed_refs_path);
391 c5f754cc 2019-02-01 stsp if (f != NULL) {
392 0dec1cc0 2019-02-01 stsp err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
393 0dec1cc0 2019-02-01 stsp refname);
394 fb43ecf1 2019-02-11 stsp if (fclose(f) != 0 && err == NULL)
395 fb43ecf1 2019-02-11 stsp err = got_error_from_errno();
396 1e37702e 2019-02-04 stsp if (err || *ref)
397 0dec1cc0 2019-02-01 stsp goto done;
398 fb79db15 2019-02-01 stsp }
399 fb79db15 2019-02-01 stsp }
400 fb79db15 2019-02-01 stsp
401 fb79db15 2019-02-01 stsp err = open_ref(ref, path_refs, "", refname);
402 1e37702e 2019-02-04 stsp if (err)
403 1e37702e 2019-02-04 stsp goto done;
404 e135804e 2019-02-10 stsp done:
405 1e37702e 2019-02-04 stsp if (*ref == NULL)
406 1e37702e 2019-02-04 stsp err = got_error_not_ref(refname);
407 5261c201 2018-04-01 stsp free(path_refs);
408 5261c201 2018-04-01 stsp return err;
409 5261c201 2018-04-01 stsp }
410 5261c201 2018-04-01 stsp
411 5261c201 2018-04-01 stsp void
412 5261c201 2018-04-01 stsp got_ref_close(struct got_reference *ref)
413 5261c201 2018-04-01 stsp {
414 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
415 5261c201 2018-04-01 stsp free(ref->ref.symref.name);
416 5261c201 2018-04-01 stsp else
417 5261c201 2018-04-01 stsp free(ref->ref.ref.name);
418 5261c201 2018-04-01 stsp free(ref);
419 5261c201 2018-04-01 stsp }
420 5261c201 2018-04-01 stsp
421 5261c201 2018-04-01 stsp struct got_reference *
422 5261c201 2018-04-01 stsp got_ref_dup(struct got_reference *ref)
423 5261c201 2018-04-01 stsp {
424 5261c201 2018-04-01 stsp struct got_reference *ret;
425 5261c201 2018-04-01 stsp
426 5261c201 2018-04-01 stsp ret = calloc(1, sizeof(*ret));
427 5261c201 2018-04-01 stsp if (ret == NULL)
428 5261c201 2018-04-01 stsp return NULL;
429 5261c201 2018-04-01 stsp
430 5261c201 2018-04-01 stsp ret->flags = ref->flags;
431 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
432 5261c201 2018-04-01 stsp ret->ref.symref.name = strdup(ref->ref.symref.name);
433 5261c201 2018-04-01 stsp if (ret->ref.symref.name == NULL) {
434 5261c201 2018-04-01 stsp free(ret);
435 5261c201 2018-04-01 stsp return NULL;
436 5261c201 2018-04-01 stsp }
437 5261c201 2018-04-01 stsp ret->ref.symref.ref = strdup(ref->ref.symref.ref);
438 5261c201 2018-04-01 stsp if (ret->ref.symref.ref == NULL) {
439 5261c201 2018-04-01 stsp free(ret->ref.symref.name);
440 5261c201 2018-04-01 stsp free(ret);
441 5261c201 2018-04-01 stsp return NULL;
442 5261c201 2018-04-01 stsp }
443 5261c201 2018-04-01 stsp } else {
444 5261c201 2018-04-01 stsp ref->ref.ref.name = strdup(ref->ref.ref.name);
445 5261c201 2018-04-01 stsp if (ref->ref.ref.name == NULL) {
446 5261c201 2018-04-01 stsp free(ret);
447 5261c201 2018-04-01 stsp return NULL;
448 5261c201 2018-04-01 stsp }
449 5261c201 2018-04-01 stsp memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
450 5261c201 2018-04-01 stsp SHA1_DIGEST_LENGTH);
451 5261c201 2018-04-01 stsp }
452 5261c201 2018-04-01 stsp
453 5261c201 2018-04-01 stsp return ret;
454 5261c201 2018-04-01 stsp }
455 5261c201 2018-04-01 stsp
456 5261c201 2018-04-01 stsp static const struct got_error *
457 5261c201 2018-04-01 stsp resolve_symbolic_ref(struct got_reference **resolved,
458 5261c201 2018-04-01 stsp struct got_repository *repo, struct got_reference *ref)
459 5261c201 2018-04-01 stsp {
460 5261c201 2018-04-01 stsp struct got_reference *nextref;
461 5261c201 2018-04-01 stsp const struct got_error *err;
462 5261c201 2018-04-01 stsp
463 5261c201 2018-04-01 stsp err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
464 5261c201 2018-04-01 stsp if (err)
465 5261c201 2018-04-01 stsp return err;
466 5261c201 2018-04-01 stsp
467 5261c201 2018-04-01 stsp if (nextref->flags & GOT_REF_IS_SYMBOLIC)
468 5261c201 2018-04-01 stsp err = resolve_symbolic_ref(resolved, repo, nextref);
469 5261c201 2018-04-01 stsp else
470 5261c201 2018-04-01 stsp *resolved = got_ref_dup(nextref);
471 5261c201 2018-04-01 stsp
472 5261c201 2018-04-01 stsp got_ref_close(nextref);
473 5261c201 2018-04-01 stsp return err;
474 5261c201 2018-04-01 stsp }
475 5261c201 2018-04-01 stsp
476 5261c201 2018-04-01 stsp const struct got_error *
477 5261c201 2018-04-01 stsp got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
478 5261c201 2018-04-01 stsp struct got_reference *ref)
479 5261c201 2018-04-01 stsp {
480 5261c201 2018-04-01 stsp const struct got_error *err;
481 5261c201 2018-04-01 stsp
482 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
483 5261c201 2018-04-01 stsp struct got_reference *resolved = NULL;
484 5261c201 2018-04-01 stsp err = resolve_symbolic_ref(&resolved, repo, ref);
485 5261c201 2018-04-01 stsp if (err == NULL)
486 5261c201 2018-04-01 stsp err = got_ref_resolve(id, repo, resolved);
487 5261c201 2018-04-01 stsp free(resolved);
488 5261c201 2018-04-01 stsp return err;
489 5261c201 2018-04-01 stsp }
490 5261c201 2018-04-01 stsp
491 5261c201 2018-04-01 stsp *id = calloc(1, sizeof(**id));
492 5261c201 2018-04-01 stsp if (*id == NULL)
493 5261c201 2018-04-01 stsp return got_error_from_errno();
494 5261c201 2018-04-01 stsp memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
495 5261c201 2018-04-01 stsp return NULL;
496 5261c201 2018-04-01 stsp }
497 5261c201 2018-04-01 stsp
498 5261c201 2018-04-01 stsp char *
499 5261c201 2018-04-01 stsp got_ref_to_str(struct got_reference *ref)
500 5261c201 2018-04-01 stsp {
501 5261c201 2018-04-01 stsp char *str;
502 271d2a38 2018-12-25 stsp
503 271d2a38 2018-12-25 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
504 271d2a38 2018-12-25 stsp return strdup(ref->ref.symref.ref);
505 271d2a38 2018-12-25 stsp
506 271d2a38 2018-12-25 stsp str = malloc(SHA1_DIGEST_STRING_LENGTH);
507 271d2a38 2018-12-25 stsp if (str == NULL)
508 271d2a38 2018-12-25 stsp return NULL;
509 271d2a38 2018-12-25 stsp
510 271d2a38 2018-12-25 stsp if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
511 271d2a38 2018-12-25 stsp SHA1_DIGEST_STRING_LENGTH) == NULL) {
512 271d2a38 2018-12-25 stsp free(str);
513 271d2a38 2018-12-25 stsp return NULL;
514 5261c201 2018-04-01 stsp }
515 5261c201 2018-04-01 stsp
516 5261c201 2018-04-01 stsp return str;
517 5261c201 2018-04-01 stsp }
518 0bd18d37 2019-02-01 stsp
519 0bd18d37 2019-02-01 stsp const char *
520 0bd18d37 2019-02-01 stsp got_ref_get_name(struct got_reference *ref)
521 0bd18d37 2019-02-01 stsp {
522 0bd18d37 2019-02-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
523 0bd18d37 2019-02-01 stsp return ref->ref.symref.name;
524 0bd18d37 2019-02-01 stsp
525 0bd18d37 2019-02-01 stsp return ref->ref.ref.name;
526 199a4027 2019-02-02 stsp }
527 199a4027 2019-02-02 stsp
528 199a4027 2019-02-02 stsp static const struct got_error *
529 29b5c214 2019-02-04 stsp insert_ref(struct got_reflist_head *refs, struct got_reference *ref,
530 199a4027 2019-02-02 stsp struct got_repository *repo)
531 199a4027 2019-02-02 stsp {
532 199a4027 2019-02-02 stsp const struct got_error *err;
533 199a4027 2019-02-02 stsp struct got_object_id *id;
534 7a3c76f5 2019-02-05 stsp struct got_reflist_entry *new, *re, *prev;
535 e397b6db 2019-02-04 stsp int cmp;
536 7a3c76f5 2019-02-05 stsp
537 7a3c76f5 2019-02-05 stsp err = got_ref_resolve(&id, repo, ref);
538 7a3c76f5 2019-02-05 stsp if (err)
539 7a3c76f5 2019-02-05 stsp return err;
540 29b5c214 2019-02-04 stsp
541 7a3c76f5 2019-02-05 stsp new = malloc(sizeof(*re));
542 7a3c76f5 2019-02-05 stsp if (new == NULL) {
543 7a3c76f5 2019-02-05 stsp free(id);
544 7a3c76f5 2019-02-05 stsp return got_error_from_errno();
545 7a3c76f5 2019-02-05 stsp }
546 7a3c76f5 2019-02-05 stsp new->ref = ref;
547 7a3c76f5 2019-02-05 stsp new->id = id;
548 7a3c76f5 2019-02-05 stsp
549 29b5c214 2019-02-04 stsp /*
550 29b5c214 2019-02-04 stsp * We must de-duplicate entries on insert because packed-refs may
551 29b5c214 2019-02-04 stsp * contain redundant entries. On-disk refs take precedence.
552 29b5c214 2019-02-04 stsp * This code assumes that on-disk revs are read before packed-refs.
553 e397b6db 2019-02-04 stsp * We're iterating the list anyway, so insert elements sorted by name.
554 29b5c214 2019-02-04 stsp */
555 7a3c76f5 2019-02-05 stsp re = SIMPLEQ_FIRST(refs);
556 7a3c76f5 2019-02-05 stsp while (re) {
557 7a3c76f5 2019-02-05 stsp cmp = got_path_cmp(got_ref_get_name(re->ref),
558 7a3c76f5 2019-02-05 stsp got_ref_get_name(ref));
559 e397b6db 2019-02-04 stsp if (cmp == 0) {
560 7a3c76f5 2019-02-05 stsp free(ref); /* duplicate */
561 7a3c76f5 2019-02-05 stsp return NULL;
562 7a3c76f5 2019-02-05 stsp } else if (cmp > 0) {
563 7a3c76f5 2019-02-05 stsp if (prev)
564 7a3c76f5 2019-02-05 stsp SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
565 7a3c76f5 2019-02-05 stsp else
566 7a3c76f5 2019-02-05 stsp SIMPLEQ_INSERT_HEAD(refs, new, entry);
567 7a3c76f5 2019-02-05 stsp return NULL;
568 7a3c76f5 2019-02-05 stsp } else {
569 e397b6db 2019-02-04 stsp prev = re;
570 7a3c76f5 2019-02-05 stsp re = SIMPLEQ_NEXT(re, entry);
571 7a3c76f5 2019-02-05 stsp }
572 29b5c214 2019-02-04 stsp }
573 199a4027 2019-02-02 stsp
574 7a3c76f5 2019-02-05 stsp SIMPLEQ_INSERT_TAIL(refs, new, entry);
575 199a4027 2019-02-02 stsp return NULL;
576 0bd18d37 2019-02-01 stsp }
577 199a4027 2019-02-02 stsp
578 a04f49d2 2019-02-04 stsp static const struct got_error *
579 29b5c214 2019-02-04 stsp gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
580 a04f49d2 2019-02-04 stsp const char *subdir, struct got_repository *repo)
581 a04f49d2 2019-02-04 stsp {
582 a04f49d2 2019-02-04 stsp const struct got_error *err = NULL;
583 a04f49d2 2019-02-04 stsp DIR *d = NULL;
584 a04f49d2 2019-02-04 stsp char *path_subdir;
585 a04f49d2 2019-02-04 stsp
586 a04f49d2 2019-02-04 stsp if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
587 a04f49d2 2019-02-04 stsp return got_error_from_errno();
588 a04f49d2 2019-02-04 stsp
589 a04f49d2 2019-02-04 stsp d = opendir(path_subdir);
590 a04f49d2 2019-02-04 stsp if (d == NULL)
591 a04f49d2 2019-02-04 stsp goto done;
592 a04f49d2 2019-02-04 stsp
593 a04f49d2 2019-02-04 stsp while (1) {
594 a04f49d2 2019-02-04 stsp struct dirent *dent;
595 a04f49d2 2019-02-04 stsp struct got_reference *ref;
596 a04f49d2 2019-02-04 stsp char *child;
597 a04f49d2 2019-02-04 stsp
598 a04f49d2 2019-02-04 stsp dent = readdir(d);
599 a04f49d2 2019-02-04 stsp if (dent == NULL)
600 a04f49d2 2019-02-04 stsp break;
601 a04f49d2 2019-02-04 stsp
602 a04f49d2 2019-02-04 stsp if (strcmp(dent->d_name, ".") == 0 ||
603 a04f49d2 2019-02-04 stsp strcmp(dent->d_name, "..") == 0)
604 a04f49d2 2019-02-04 stsp continue;
605 a04f49d2 2019-02-04 stsp
606 a04f49d2 2019-02-04 stsp switch (dent->d_type) {
607 a04f49d2 2019-02-04 stsp case DT_REG:
608 a04f49d2 2019-02-04 stsp err = open_ref(&ref, path_refs, subdir, dent->d_name);
609 1e37702e 2019-02-04 stsp if (err)
610 a04f49d2 2019-02-04 stsp goto done;
611 a04f49d2 2019-02-04 stsp if (ref) {
612 29b5c214 2019-02-04 stsp err = insert_ref(refs, ref, repo);
613 a04f49d2 2019-02-04 stsp if (err)
614 a04f49d2 2019-02-04 stsp goto done;
615 a04f49d2 2019-02-04 stsp }
616 a04f49d2 2019-02-04 stsp break;
617 a04f49d2 2019-02-04 stsp case DT_DIR:
618 a04f49d2 2019-02-04 stsp if (asprintf(&child, "%s%s%s", subdir,
619 a04f49d2 2019-02-04 stsp subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
620 a04f49d2 2019-02-04 stsp err = got_error_from_errno();
621 a04f49d2 2019-02-04 stsp break;
622 a04f49d2 2019-02-04 stsp }
623 29b5c214 2019-02-04 stsp err = gather_on_disk_refs(refs, path_refs, child, repo);
624 a04f49d2 2019-02-04 stsp free(child);
625 a04f49d2 2019-02-04 stsp break;
626 a04f49d2 2019-02-04 stsp default:
627 a04f49d2 2019-02-04 stsp break;
628 a04f49d2 2019-02-04 stsp }
629 a04f49d2 2019-02-04 stsp }
630 a04f49d2 2019-02-04 stsp done:
631 a04f49d2 2019-02-04 stsp if (d)
632 a04f49d2 2019-02-04 stsp closedir(d);
633 a04f49d2 2019-02-04 stsp free(path_subdir);
634 a04f49d2 2019-02-04 stsp return err;
635 a04f49d2 2019-02-04 stsp }
636 a04f49d2 2019-02-04 stsp
637 199a4027 2019-02-02 stsp const struct got_error *
638 199a4027 2019-02-02 stsp got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
639 199a4027 2019-02-02 stsp {
640 199a4027 2019-02-02 stsp const struct got_error *err;
641 a04f49d2 2019-02-04 stsp char *packed_refs_path, *path_refs = NULL;
642 29b5c214 2019-02-04 stsp FILE *f = NULL;
643 199a4027 2019-02-02 stsp struct got_reference *ref;
644 199a4027 2019-02-02 stsp
645 29b5c214 2019-02-04 stsp /* HEAD ref should always exist. */
646 29b5c214 2019-02-04 stsp path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
647 29b5c214 2019-02-04 stsp if (path_refs == NULL) {
648 29b5c214 2019-02-04 stsp err = got_error_from_errno();
649 29b5c214 2019-02-04 stsp goto done;
650 29b5c214 2019-02-04 stsp }
651 29b5c214 2019-02-04 stsp err = open_ref(&ref, path_refs, "", GOT_REF_HEAD);
652 29b5c214 2019-02-04 stsp if (err)
653 29b5c214 2019-02-04 stsp goto done;
654 29b5c214 2019-02-04 stsp err = insert_ref(refs, ref, repo);
655 29b5c214 2019-02-04 stsp if (err)
656 29b5c214 2019-02-04 stsp goto done;
657 29b5c214 2019-02-04 stsp
658 29b5c214 2019-02-04 stsp /* Gather on-disk refs before parsing packed-refs. */
659 29b5c214 2019-02-04 stsp free(path_refs);
660 29b5c214 2019-02-04 stsp path_refs = get_refs_dir_path(repo, "");
661 29b5c214 2019-02-04 stsp if (path_refs == NULL) {
662 29b5c214 2019-02-04 stsp err = got_error_from_errno();
663 29b5c214 2019-02-04 stsp goto done;
664 29b5c214 2019-02-04 stsp }
665 29b5c214 2019-02-04 stsp err = gather_on_disk_refs(refs, path_refs, "", repo);
666 29b5c214 2019-02-04 stsp if (err)
667 29b5c214 2019-02-04 stsp goto done;
668 29b5c214 2019-02-04 stsp
669 29b5c214 2019-02-04 stsp /*
670 29b5c214 2019-02-04 stsp * The packed-refs file may contain redundant entries, in which
671 29b5c214 2019-02-04 stsp * case on-disk refs take precedence.
672 29b5c214 2019-02-04 stsp */
673 199a4027 2019-02-02 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
674 29b5c214 2019-02-04 stsp if (packed_refs_path == NULL) {
675 29b5c214 2019-02-04 stsp err = got_error_from_errno();
676 29b5c214 2019-02-04 stsp goto done;
677 29b5c214 2019-02-04 stsp }
678 199a4027 2019-02-02 stsp
679 199a4027 2019-02-02 stsp f = fopen(packed_refs_path, "r");
680 199a4027 2019-02-02 stsp free(packed_refs_path);
681 199a4027 2019-02-02 stsp if (f) {
682 199a4027 2019-02-02 stsp char *line;
683 199a4027 2019-02-02 stsp size_t len;
684 199a4027 2019-02-02 stsp const char delim[3] = {'\0', '\0', '\0'};
685 199a4027 2019-02-02 stsp while (1) {
686 199a4027 2019-02-02 stsp line = fparseln(f, &len, NULL, delim, 0);
687 199a4027 2019-02-02 stsp if (line == NULL)
688 199a4027 2019-02-02 stsp break;
689 199a4027 2019-02-02 stsp err = parse_packed_ref_line(&ref, NULL, line);
690 199a4027 2019-02-02 stsp if (err)
691 199a4027 2019-02-02 stsp goto done;
692 76b4ead2 2019-02-03 stsp if (ref) {
693 29b5c214 2019-02-04 stsp err = insert_ref(refs, ref, repo);
694 76b4ead2 2019-02-03 stsp if (err)
695 76b4ead2 2019-02-03 stsp goto done;
696 76b4ead2 2019-02-03 stsp }
697 199a4027 2019-02-02 stsp }
698 199a4027 2019-02-02 stsp }
699 199a4027 2019-02-02 stsp done:
700 a04f49d2 2019-02-04 stsp free(path_refs);
701 fb43ecf1 2019-02-11 stsp if (f && fclose(f) != 0 && err == NULL)
702 fb43ecf1 2019-02-11 stsp err = got_error_from_errno();
703 199a4027 2019-02-02 stsp return err;
704 199a4027 2019-02-02 stsp }
705 9e672c74 2019-03-11 stsp
706 e2e879a0 2019-03-11 stsp void
707 e2e879a0 2019-03-11 stsp got_ref_list_free(struct got_reflist_head *refs)
708 e2e879a0 2019-03-11 stsp {
709 e2e879a0 2019-03-11 stsp struct got_reflist_entry *re;
710 e2e879a0 2019-03-11 stsp
711 e2e879a0 2019-03-11 stsp while (!SIMPLEQ_EMPTY(refs)) {
712 e2e879a0 2019-03-11 stsp re = SIMPLEQ_FIRST(refs);
713 e2e879a0 2019-03-11 stsp SIMPLEQ_REMOVE_HEAD(refs, entry);
714 e2e879a0 2019-03-11 stsp got_ref_close(re->ref);
715 e2e879a0 2019-03-11 stsp free(re->id);
716 e2e879a0 2019-03-11 stsp free(re);
717 e2e879a0 2019-03-11 stsp }
718 e2e879a0 2019-03-11 stsp
719 e2e879a0 2019-03-11 stsp }
720 e2e879a0 2019-03-11 stsp
721 9e672c74 2019-03-11 stsp const struct got_error *
722 9e672c74 2019-03-11 stsp got_ref_write(struct got_reference *ref, struct got_repository *repo)
723 9e672c74 2019-03-11 stsp {
724 9e672c74 2019-03-11 stsp const struct got_error *err = NULL, *unlock_err = NULL;
725 9e672c74 2019-03-11 stsp const char *name = got_ref_get_name(ref);
726 9e672c74 2019-03-11 stsp char *path_refs = NULL, *path = NULL, *tmppath = NULL;
727 9e672c74 2019-03-11 stsp struct got_lockfile *lf = NULL;
728 9e672c74 2019-03-11 stsp FILE *f = NULL;
729 9e672c74 2019-03-11 stsp size_t n;
730 9e672c74 2019-03-11 stsp struct stat sb;
731 9e672c74 2019-03-11 stsp
732 9e672c74 2019-03-11 stsp path_refs = get_refs_dir_path(repo, name);
733 9e672c74 2019-03-11 stsp if (path_refs == NULL) {
734 9e672c74 2019-03-11 stsp err = got_error_from_errno();
735 9e672c74 2019-03-11 stsp goto done;
736 9e672c74 2019-03-11 stsp }
737 9e672c74 2019-03-11 stsp
738 9e672c74 2019-03-11 stsp if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
739 9e672c74 2019-03-11 stsp err = got_error_from_errno();
740 9e672c74 2019-03-11 stsp goto done;
741 9e672c74 2019-03-11 stsp }
742 9e672c74 2019-03-11 stsp
743 9e672c74 2019-03-11 stsp err = got_opentemp_named(&tmppath, &f, path);
744 9e672c74 2019-03-11 stsp if (f == NULL) {
745 0cd1c46a 2019-03-11 stsp char *parent;
746 0cd1c46a 2019-03-11 stsp if (errno != ENOENT) {
747 0cd1c46a 2019-03-11 stsp err = got_error_from_errno();
748 0cd1c46a 2019-03-11 stsp goto done;
749 0cd1c46a 2019-03-11 stsp }
750 0cd1c46a 2019-03-11 stsp parent = dirname(path);
751 0cd1c46a 2019-03-11 stsp if (parent == NULL) {
752 0cd1c46a 2019-03-11 stsp err = got_error_from_errno();
753 0cd1c46a 2019-03-11 stsp goto done;
754 0cd1c46a 2019-03-11 stsp }
755 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(parent);
756 0cd1c46a 2019-03-11 stsp if (err)
757 0cd1c46a 2019-03-11 stsp goto done;
758 0cd1c46a 2019-03-11 stsp err = got_opentemp_named(&tmppath, &f, path);
759 0cd1c46a 2019-03-11 stsp if (f == NULL) {
760 0cd1c46a 2019-03-11 stsp err = got_error_from_errno();
761 0cd1c46a 2019-03-11 stsp goto done;
762 0cd1c46a 2019-03-11 stsp }
763 9e672c74 2019-03-11 stsp }
764 9e672c74 2019-03-11 stsp
765 9e672c74 2019-03-11 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
766 9e672c74 2019-03-11 stsp n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
767 9e672c74 2019-03-11 stsp if (n != strlen(ref->ref.symref.ref) + 6) {
768 9e672c74 2019-03-11 stsp err = got_ferror(f, GOT_ERR_IO);
769 9e672c74 2019-03-11 stsp goto done;
770 9e672c74 2019-03-11 stsp }
771 9e672c74 2019-03-11 stsp } else {
772 9e672c74 2019-03-11 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
773 9e672c74 2019-03-11 stsp if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
774 9e672c74 2019-03-11 stsp sizeof(hex)) == NULL) {
775 9e672c74 2019-03-11 stsp err = got_error(GOT_ERR_BAD_REF_DATA);
776 9e672c74 2019-03-11 stsp goto done;
777 9e672c74 2019-03-11 stsp }
778 9e672c74 2019-03-11 stsp n = fprintf(f, "%s\n", hex);
779 8fa2f096 2019-03-11 stsp if (n != sizeof(hex)) {
780 9e672c74 2019-03-11 stsp err = got_ferror(f, GOT_ERR_IO);
781 9e672c74 2019-03-11 stsp goto done;
782 9e672c74 2019-03-11 stsp }
783 9e672c74 2019-03-11 stsp }
784 9e672c74 2019-03-11 stsp
785 9e672c74 2019-03-11 stsp err = got_lockfile_lock(&lf, path);
786 9e672c74 2019-03-11 stsp if (err)
787 9e672c74 2019-03-11 stsp goto done;
788 9e672c74 2019-03-11 stsp
789 9e672c74 2019-03-11 stsp /* XXX: check if old content matches our expectations? */
790 9e672c74 2019-03-11 stsp
791 0cd1c46a 2019-03-11 stsp if (stat(path, &sb) != 0) {
792 0cd1c46a 2019-03-11 stsp if (errno != ENOENT) {
793 0cd1c46a 2019-03-11 stsp err = got_error_from_errno();
794 0cd1c46a 2019-03-11 stsp goto done;
795 0cd1c46a 2019-03-11 stsp }
796 0cd1c46a 2019-03-11 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
797 9e672c74 2019-03-11 stsp }
798 9e672c74 2019-03-11 stsp
799 9e672c74 2019-03-11 stsp if (rename(tmppath, path) != 0) {
800 9e672c74 2019-03-11 stsp err = got_error_from_errno();
801 9e672c74 2019-03-11 stsp goto done;
802 9e672c74 2019-03-11 stsp }
803 9e672c74 2019-03-11 stsp free(tmppath);
804 9e672c74 2019-03-11 stsp tmppath = NULL;
805 9e672c74 2019-03-11 stsp
806 9e672c74 2019-03-11 stsp if (chmod(path, sb.st_mode) != 0) {
807 9e672c74 2019-03-11 stsp err = got_error_from_errno();
808 9e672c74 2019-03-11 stsp goto done;
809 9e672c74 2019-03-11 stsp }
810 9e672c74 2019-03-11 stsp done:
811 9e672c74 2019-03-11 stsp if (lf)
812 9e672c74 2019-03-11 stsp unlock_err = got_lockfile_unlock(lf);
813 9e672c74 2019-03-11 stsp if (f) {
814 9e672c74 2019-03-11 stsp if (fclose(f) != 0 && err == NULL)
815 9e672c74 2019-03-11 stsp err = got_error_from_errno();
816 9e672c74 2019-03-11 stsp }
817 9e672c74 2019-03-11 stsp free(path_refs);
818 9e672c74 2019-03-11 stsp free(path);
819 9e672c74 2019-03-11 stsp if (tmppath) {
820 9e672c74 2019-03-11 stsp if (unlink(tmppath) != 0 && err == NULL)
821 9e672c74 2019-03-11 stsp err = got_error_from_errno();
822 9e672c74 2019-03-11 stsp free(tmppath);
823 2d2e1378 2019-03-11 stsp }
824 2d2e1378 2019-03-11 stsp return err ? err : unlock_err;
825 2d2e1378 2019-03-11 stsp }
826 2d2e1378 2019-03-11 stsp
827 2d2e1378 2019-03-11 stsp const struct got_error *
828 2d2e1378 2019-03-11 stsp got_ref_delete(struct got_reference *ref, struct got_repository *repo)
829 2d2e1378 2019-03-11 stsp {
830 2d2e1378 2019-03-11 stsp const struct got_error *err = NULL, *unlock_err = NULL;
831 2d2e1378 2019-03-11 stsp const char *name = got_ref_get_name(ref);
832 2d2e1378 2019-03-11 stsp char *path_refs = NULL, *path = NULL;
833 2d2e1378 2019-03-11 stsp struct got_lockfile *lf = NULL;
834 2d2e1378 2019-03-11 stsp
835 2d2e1378 2019-03-11 stsp /* TODO: handle packed refs ! */
836 2d2e1378 2019-03-11 stsp
837 2d2e1378 2019-03-11 stsp path_refs = get_refs_dir_path(repo, name);
838 2d2e1378 2019-03-11 stsp if (path_refs == NULL) {
839 2d2e1378 2019-03-11 stsp err = got_error_from_errno();
840 2d2e1378 2019-03-11 stsp goto done;
841 2d2e1378 2019-03-11 stsp }
842 2d2e1378 2019-03-11 stsp
843 2d2e1378 2019-03-11 stsp if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
844 2d2e1378 2019-03-11 stsp err = got_error_from_errno();
845 2d2e1378 2019-03-11 stsp goto done;
846 9e672c74 2019-03-11 stsp }
847 2d2e1378 2019-03-11 stsp
848 2d2e1378 2019-03-11 stsp err = got_lockfile_lock(&lf, path);
849 2d2e1378 2019-03-11 stsp if (err)
850 2d2e1378 2019-03-11 stsp goto done;
851 2d2e1378 2019-03-11 stsp
852 2d2e1378 2019-03-11 stsp /* XXX: check if old content matches our expectations? */
853 2d2e1378 2019-03-11 stsp
854 2d2e1378 2019-03-11 stsp if (unlink(path) != 0)
855 2d2e1378 2019-03-11 stsp err = got_error_from_errno();
856 2d2e1378 2019-03-11 stsp done:
857 2d2e1378 2019-03-11 stsp if (lf)
858 2d2e1378 2019-03-11 stsp unlock_err = got_lockfile_unlock(lf);
859 2d2e1378 2019-03-11 stsp
860 2d2e1378 2019-03-11 stsp free(path_refs);
861 2d2e1378 2019-03-11 stsp free(path);
862 9e672c74 2019-03-11 stsp return err ? err : unlock_err;
863 9e672c74 2019-03-11 stsp }