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