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 324d37e7 2019-05-11 stsp #include "got_path.h"
39 5261c201 2018-04-01 stsp
40 5261c201 2018-04-01 stsp #include "got_lib_sha1.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 598a8b91 2019-03-15 stsp /*
55 598a8b91 2019-03-15 stsp * We do not resolve tags yet, and don't yet care about sorting refs either,
56 598a8b91 2019-03-15 stsp * so packed-refs files we write contain a minimal header which disables all
57 598a8b91 2019-03-15 stsp * packed-refs "traits" supported by Git.
58 598a8b91 2019-03-15 stsp */
59 598a8b91 2019-03-15 stsp #define GOT_PACKED_REFS_HEADER "# pack-refs with:"
60 598a8b91 2019-03-15 stsp
61 5261c201 2018-04-01 stsp /* A symbolic reference. */
62 5261c201 2018-04-01 stsp struct got_symref {
63 5261c201 2018-04-01 stsp char *name;
64 5261c201 2018-04-01 stsp char *ref;
65 5261c201 2018-04-01 stsp };
66 29e86f7a 2019-08-12 stsp
67 29e86f7a 2019-08-12 stsp #define GOT_REF_RECURSE_MAX 20
68 5261c201 2018-04-01 stsp
69 5261c201 2018-04-01 stsp /* A non-symbolic reference (there is no better designation). */
70 5261c201 2018-04-01 stsp struct got_ref {
71 5261c201 2018-04-01 stsp char *name;
72 5261c201 2018-04-01 stsp u_int8_t sha1[SHA1_DIGEST_LENGTH];
73 5261c201 2018-04-01 stsp };
74 5261c201 2018-04-01 stsp
75 5261c201 2018-04-01 stsp /* A reference which points to an arbitrary object. */
76 5261c201 2018-04-01 stsp struct got_reference {
77 5261c201 2018-04-01 stsp unsigned int flags;
78 5261c201 2018-04-01 stsp #define GOT_REF_IS_SYMBOLIC 0x01
79 f9267c9a 2019-03-15 stsp #define GOT_REF_IS_PACKED 0x02
80 5261c201 2018-04-01 stsp
81 5261c201 2018-04-01 stsp union {
82 5261c201 2018-04-01 stsp struct got_ref ref;
83 5261c201 2018-04-01 stsp struct got_symref symref;
84 5261c201 2018-04-01 stsp } ref;
85 2f17228e 2019-05-12 stsp
86 2f17228e 2019-05-12 stsp struct got_lockfile *lf;
87 5261c201 2018-04-01 stsp };
88 5261c201 2018-04-01 stsp
89 5261c201 2018-04-01 stsp static const struct got_error *
90 f9267c9a 2019-03-15 stsp alloc_ref(struct got_reference **ref, const char *name,
91 f9267c9a 2019-03-15 stsp struct got_object_id *id, int flags)
92 5261c201 2018-04-01 stsp {
93 f9267c9a 2019-03-15 stsp const struct got_error *err = NULL;
94 5261c201 2018-04-01 stsp
95 f9267c9a 2019-03-15 stsp *ref = calloc(1, sizeof(**ref));
96 f9267c9a 2019-03-15 stsp if (*ref == NULL)
97 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
98 f9267c9a 2019-03-15 stsp
99 80d5fc1f 2019-03-15 stsp memcpy((*ref)->ref.ref.sha1, id->sha1, sizeof((*ref)->ref.ref.sha1));
100 c980e470 2019-03-15 stsp (*ref)->flags = flags;
101 f9267c9a 2019-03-15 stsp (*ref)->ref.ref.name = strdup(name);
102 f9267c9a 2019-03-15 stsp if ((*ref)->ref.ref.name == NULL) {
103 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
104 c980e470 2019-03-15 stsp got_ref_close(*ref);
105 f9267c9a 2019-03-15 stsp *ref = NULL;
106 5261c201 2018-04-01 stsp }
107 f9267c9a 2019-03-15 stsp return err;
108 f9267c9a 2019-03-15 stsp }
109 5261c201 2018-04-01 stsp
110 f9267c9a 2019-03-15 stsp static const struct got_error *
111 f9267c9a 2019-03-15 stsp alloc_symref(struct got_reference **ref, const char *name,
112 f9267c9a 2019-03-15 stsp const char *target_ref, int flags)
113 f9267c9a 2019-03-15 stsp {
114 f9267c9a 2019-03-15 stsp const struct got_error *err = NULL;
115 f9267c9a 2019-03-15 stsp
116 5261c201 2018-04-01 stsp *ref = calloc(1, sizeof(**ref));
117 5261c201 2018-04-01 stsp if (*ref == NULL)
118 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
119 f9267c9a 2019-03-15 stsp
120 f9267c9a 2019-03-15 stsp (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
121 f9267c9a 2019-03-15 stsp (*ref)->ref.symref.name = strdup(name);
122 f9267c9a 2019-03-15 stsp if ((*ref)->ref.symref.name == NULL) {
123 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
124 f9267c9a 2019-03-15 stsp got_ref_close(*ref);
125 f9267c9a 2019-03-15 stsp *ref = NULL;
126 cdb8f1fa 2019-08-28 hiltjo return err;
127 f9267c9a 2019-03-15 stsp }
128 f9267c9a 2019-03-15 stsp (*ref)->ref.symref.ref = strdup(target_ref);
129 f9267c9a 2019-03-15 stsp if ((*ref)->ref.symref.ref == NULL) {
130 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
131 f9267c9a 2019-03-15 stsp got_ref_close(*ref);
132 f9267c9a 2019-03-15 stsp *ref = NULL;
133 f9267c9a 2019-03-15 stsp }
134 f9267c9a 2019-03-15 stsp return err;
135 5261c201 2018-04-01 stsp }
136 5261c201 2018-04-01 stsp
137 5261c201 2018-04-01 stsp static const struct got_error *
138 f9267c9a 2019-03-15 stsp parse_symref(struct got_reference **ref, const char *name, const char *line)
139 f9267c9a 2019-03-15 stsp {
140 f9267c9a 2019-03-15 stsp if (line[0] == '\0')
141 f9267c9a 2019-03-15 stsp return got_error(GOT_ERR_BAD_REF_DATA);
142 f9267c9a 2019-03-15 stsp
143 f9267c9a 2019-03-15 stsp return alloc_symref(ref, name, line, 0);
144 f9267c9a 2019-03-15 stsp }
145 f9267c9a 2019-03-15 stsp
146 f9267c9a 2019-03-15 stsp static const struct got_error *
147 5261c201 2018-04-01 stsp parse_ref_line(struct got_reference **ref, const char *name, const char *line)
148 5261c201 2018-04-01 stsp {
149 5892cdd6 2019-03-10 stsp struct got_object_id id;
150 5261c201 2018-04-01 stsp
151 5261c201 2018-04-01 stsp if (strncmp(line, "ref: ", 5) == 0) {
152 5261c201 2018-04-01 stsp line += 5;
153 5261c201 2018-04-01 stsp return parse_symref(ref, name, line);
154 5261c201 2018-04-01 stsp }
155 5261c201 2018-04-01 stsp
156 5892cdd6 2019-03-10 stsp if (!got_parse_sha1_digest(id.sha1, line))
157 30c0868d 2019-02-03 stsp return got_error(GOT_ERR_BAD_REF_DATA);
158 5261c201 2018-04-01 stsp
159 f9267c9a 2019-03-15 stsp return alloc_ref(ref, name, &id, 0);
160 5261c201 2018-04-01 stsp }
161 5261c201 2018-04-01 stsp
162 5261c201 2018-04-01 stsp static const struct got_error *
163 5261c201 2018-04-01 stsp parse_ref_file(struct got_reference **ref, const char *name,
164 2f17228e 2019-05-12 stsp const char *abspath, int lock)
165 5261c201 2018-04-01 stsp {
166 5261c201 2018-04-01 stsp const struct got_error *err = NULL;
167 c0a1c016 2019-03-15 stsp FILE *f;
168 5261c201 2018-04-01 stsp char *line;
169 5261c201 2018-04-01 stsp size_t len;
170 5261c201 2018-04-01 stsp const char delim[3] = {'\0', '\0', '\0'};
171 2f17228e 2019-05-12 stsp struct got_lockfile *lf = NULL;
172 5261c201 2018-04-01 stsp
173 2f17228e 2019-05-12 stsp if (lock) {
174 2f17228e 2019-05-12 stsp err = got_lockfile_lock(&lf, abspath);
175 2f17228e 2019-05-12 stsp if (err)
176 2f17228e 2019-05-12 stsp return (err);
177 2f17228e 2019-05-12 stsp }
178 2f17228e 2019-05-12 stsp
179 c0a1c016 2019-03-15 stsp f = fopen(abspath, "rb");
180 f5c58ad1 2019-05-12 stsp if (f == NULL) {
181 f5c58ad1 2019-05-12 stsp if (lock)
182 f5c58ad1 2019-05-12 stsp got_lockfile_unlock(lf);
183 1e37702e 2019-02-04 stsp return NULL;
184 f5c58ad1 2019-05-12 stsp }
185 5261c201 2018-04-01 stsp
186 5261c201 2018-04-01 stsp line = fparseln(f, &len, NULL, delim, 0);
187 5261c201 2018-04-01 stsp if (line == NULL) {
188 30c0868d 2019-02-03 stsp err = got_error(GOT_ERR_BAD_REF_DATA);
189 f5c58ad1 2019-05-12 stsp if (lock)
190 f5c58ad1 2019-05-12 stsp got_lockfile_unlock(lf);
191 5261c201 2018-04-01 stsp goto done;
192 5261c201 2018-04-01 stsp }
193 5261c201 2018-04-01 stsp
194 5261c201 2018-04-01 stsp err = parse_ref_line(ref, name, line);
195 f5c58ad1 2019-05-12 stsp if (lock) {
196 f5c58ad1 2019-05-12 stsp if (err)
197 f5c58ad1 2019-05-12 stsp got_lockfile_unlock(lf);
198 f5c58ad1 2019-05-12 stsp else {
199 f5c58ad1 2019-05-12 stsp if (*ref)
200 f5c58ad1 2019-05-12 stsp (*ref)->lf = lf;
201 f5c58ad1 2019-05-12 stsp else
202 f5c58ad1 2019-05-12 stsp got_lockfile_unlock(lf);
203 f5c58ad1 2019-05-12 stsp }
204 f5c58ad1 2019-05-12 stsp }
205 5261c201 2018-04-01 stsp done:
206 5261c201 2018-04-01 stsp free(line);
207 2f17228e 2019-05-12 stsp if (fclose(f) != 0 && err == NULL) {
208 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
209 2f17228e 2019-05-12 stsp if (*ref) {
210 f5c58ad1 2019-05-12 stsp if (lock)
211 f5c58ad1 2019-05-12 stsp got_ref_unlock(*ref);
212 2f17228e 2019-05-12 stsp got_ref_close(*ref);
213 2f17228e 2019-05-12 stsp *ref = NULL;
214 2f17228e 2019-05-12 stsp }
215 2f17228e 2019-05-12 stsp }
216 5261c201 2018-04-01 stsp return err;
217 5261c201 2018-04-01 stsp }
218 5261c201 2018-04-01 stsp
219 c5f754cc 2019-02-01 stsp static int
220 c5f754cc 2019-02-01 stsp is_well_known_ref(const char *refname)
221 5261c201 2018-04-01 stsp {
222 c5f754cc 2019-02-01 stsp return (strcmp(refname, GOT_REF_HEAD) == 0 ||
223 5261c201 2018-04-01 stsp strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
224 5261c201 2018-04-01 stsp strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
225 c5f754cc 2019-02-01 stsp strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
226 c5f754cc 2019-02-01 stsp }
227 c5f754cc 2019-02-01 stsp
228 c5f754cc 2019-02-01 stsp static char *
229 c5f754cc 2019-02-01 stsp get_refs_dir_path(struct got_repository *repo, const char *refname)
230 c5f754cc 2019-02-01 stsp {
231 c5f754cc 2019-02-01 stsp if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
232 6e9da951 2019-01-06 stsp return strdup(got_repo_get_path_git_dir(repo));
233 5261c201 2018-04-01 stsp
234 5261c201 2018-04-01 stsp return got_repo_get_path_refs(repo);
235 f77a24b0 2019-03-11 stsp }
236 f77a24b0 2019-03-11 stsp
237 f77a24b0 2019-03-11 stsp static int
238 f77a24b0 2019-03-11 stsp is_valid_ref_name(const char *name)
239 f77a24b0 2019-03-11 stsp {
240 f77a24b0 2019-03-11 stsp const char *s, *slash, *seg;
241 f77a24b0 2019-03-11 stsp const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
242 f77a24b0 2019-03-11 stsp const char *forbidden_seq[] = { "//", "..", "@{" };
243 f77a24b0 2019-03-11 stsp const char *lfs = GOT_LOCKFILE_SUFFIX;
244 f77a24b0 2019-03-11 stsp const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
245 f77a24b0 2019-03-11 stsp int i;
246 f77a24b0 2019-03-11 stsp
247 f77a24b0 2019-03-11 stsp if (name[0] == '@' && name[1] == '\0')
248 f77a24b0 2019-03-11 stsp return 0;
249 f77a24b0 2019-03-11 stsp
250 f77a24b0 2019-03-11 stsp slash = strchr(name, '/');
251 f77a24b0 2019-03-11 stsp if (slash == NULL)
252 f77a24b0 2019-03-11 stsp return 0;
253 f77a24b0 2019-03-11 stsp
254 f77a24b0 2019-03-11 stsp s = name;
255 f77a24b0 2019-03-11 stsp seg = s;
256 f77a24b0 2019-03-11 stsp if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
257 f77a24b0 2019-03-11 stsp return 0;
258 f77a24b0 2019-03-11 stsp while (*s) {
259 f77a24b0 2019-03-11 stsp for (i = 0; i < nitems(forbidden); i++) {
260 f77a24b0 2019-03-11 stsp if (*s == forbidden[i])
261 f77a24b0 2019-03-11 stsp return 0;
262 f77a24b0 2019-03-11 stsp }
263 f77a24b0 2019-03-11 stsp for (i = 0; i < nitems(forbidden_seq); i++) {
264 f77a24b0 2019-03-11 stsp if (s[0] == forbidden_seq[i][0] &&
265 f77a24b0 2019-03-11 stsp s[1] == forbidden_seq[i][1])
266 f77a24b0 2019-03-11 stsp return 0;
267 f77a24b0 2019-03-11 stsp }
268 f77a24b0 2019-03-11 stsp if (iscntrl((unsigned char)s[0]))
269 f77a24b0 2019-03-11 stsp return 0;
270 f77a24b0 2019-03-11 stsp if (s[0] == '.' && s[1] == '\0')
271 f77a24b0 2019-03-11 stsp return 0;
272 f77a24b0 2019-03-11 stsp if (*s == '/') {
273 f77a24b0 2019-03-11 stsp const char *nextseg = s + 1;
274 f77a24b0 2019-03-11 stsp if (nextseg[0] == '\0' || nextseg[0] == '.' ||
275 f77a24b0 2019-03-11 stsp nextseg[0] == '/')
276 f77a24b0 2019-03-11 stsp return 0;
277 f77a24b0 2019-03-11 stsp if (seg <= s - lfs_len &&
278 f77a24b0 2019-03-11 stsp strncmp(s - lfs_len, lfs, lfs_len) == 0)
279 f77a24b0 2019-03-11 stsp return 0;
280 f77a24b0 2019-03-11 stsp seg = nextseg;
281 f77a24b0 2019-03-11 stsp }
282 f77a24b0 2019-03-11 stsp s++;
283 f77a24b0 2019-03-11 stsp }
284 f77a24b0 2019-03-11 stsp
285 f77a24b0 2019-03-11 stsp if (seg <= s - lfs_len &&
286 f77a24b0 2019-03-11 stsp strncmp(s - lfs_len, lfs, lfs_len) == 0)
287 f77a24b0 2019-03-11 stsp return 0;
288 f77a24b0 2019-03-11 stsp
289 f77a24b0 2019-03-11 stsp return 1;
290 5892cdd6 2019-03-10 stsp }
291 5892cdd6 2019-03-10 stsp
292 5892cdd6 2019-03-10 stsp const struct got_error *
293 5892cdd6 2019-03-10 stsp got_ref_alloc(struct got_reference **ref, const char *name,
294 5892cdd6 2019-03-10 stsp struct got_object_id *id)
295 5892cdd6 2019-03-10 stsp {
296 0f148cb7 2019-05-13 stsp if (!is_valid_ref_name(name))
297 0f148cb7 2019-05-13 stsp return got_error(GOT_ERR_BAD_REF_NAME);
298 f77a24b0 2019-03-11 stsp
299 0f148cb7 2019-05-13 stsp return alloc_ref(ref, name, id, 0);
300 aaf88317 2019-07-10 stsp }
301 aaf88317 2019-07-10 stsp
302 aaf88317 2019-07-10 stsp const struct got_error *
303 aaf88317 2019-07-10 stsp got_ref_alloc_symref(struct got_reference **ref, const char *name,
304 aaf88317 2019-07-10 stsp struct got_reference *target_ref)
305 aaf88317 2019-07-10 stsp {
306 aaf88317 2019-07-10 stsp if (!is_valid_ref_name(name))
307 aaf88317 2019-07-10 stsp return got_error(GOT_ERR_BAD_REF_NAME);
308 aaf88317 2019-07-10 stsp
309 aaf88317 2019-07-10 stsp return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
310 5261c201 2018-04-01 stsp }
311 5261c201 2018-04-01 stsp
312 d1f2edc9 2018-06-13 stsp static const struct got_error *
313 fb79db15 2019-02-01 stsp parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
314 fb79db15 2019-02-01 stsp const char *line)
315 fb79db15 2019-02-01 stsp {
316 5892cdd6 2019-03-10 stsp struct got_object_id id;
317 5892cdd6 2019-03-10 stsp const char *name;
318 fb79db15 2019-02-01 stsp
319 fb79db15 2019-02-01 stsp *ref = NULL;
320 fb79db15 2019-02-01 stsp
321 532920c8 2019-02-01 stsp if (line[0] == '#' || line[0] == '^')
322 fb79db15 2019-02-01 stsp return NULL;
323 fb79db15 2019-02-01 stsp
324 5892cdd6 2019-03-10 stsp if (!got_parse_sha1_digest(id.sha1, line))
325 30c0868d 2019-02-03 stsp return got_error(GOT_ERR_BAD_REF_DATA);
326 fb79db15 2019-02-01 stsp
327 199a4027 2019-02-02 stsp if (abs_refname) {
328 199a4027 2019-02-02 stsp if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
329 199a4027 2019-02-02 stsp return NULL;
330 5892cdd6 2019-03-10 stsp name = abs_refname;
331 5892cdd6 2019-03-10 stsp } else
332 5892cdd6 2019-03-10 stsp name = line + SHA1_DIGEST_STRING_LENGTH;
333 fb79db15 2019-02-01 stsp
334 f9267c9a 2019-03-15 stsp return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
335 fb79db15 2019-02-01 stsp }
336 fb79db15 2019-02-01 stsp
337 fb79db15 2019-02-01 stsp static const struct got_error *
338 0dec1cc0 2019-02-01 stsp open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
339 0dec1cc0 2019-02-01 stsp int nsubdirs, const char *refname)
340 fb79db15 2019-02-01 stsp {
341 fb79db15 2019-02-01 stsp const struct got_error *err = NULL;
342 fb79db15 2019-02-01 stsp char *abs_refname;
343 fb79db15 2019-02-01 stsp char *line;
344 fb79db15 2019-02-01 stsp size_t len;
345 fb79db15 2019-02-01 stsp const char delim[3] = {'\0', '\0', '\0'};
346 0dec1cc0 2019-02-01 stsp int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
347 fb79db15 2019-02-01 stsp
348 1e37702e 2019-02-04 stsp *ref = NULL;
349 1e37702e 2019-02-04 stsp
350 0dec1cc0 2019-02-01 stsp if (ref_is_absolute)
351 0dec1cc0 2019-02-01 stsp abs_refname = (char *)refname;
352 fb79db15 2019-02-01 stsp do {
353 fb79db15 2019-02-01 stsp line = fparseln(f, &len, NULL, delim, 0);
354 7ab0422a 2019-03-15 stsp if (line == NULL) {
355 7ab0422a 2019-03-15 stsp if (feof(f))
356 7ab0422a 2019-03-15 stsp break;
357 7ab0422a 2019-03-15 stsp err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
358 fb79db15 2019-02-01 stsp break;
359 7ab0422a 2019-03-15 stsp }
360 0dec1cc0 2019-02-01 stsp for (i = 0; i < nsubdirs; i++) {
361 0dec1cc0 2019-02-01 stsp if (!ref_is_absolute &&
362 0dec1cc0 2019-02-01 stsp asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
363 0dec1cc0 2019-02-01 stsp refname) == -1)
364 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
365 0dec1cc0 2019-02-01 stsp err = parse_packed_ref_line(ref, abs_refname, line);
366 0dec1cc0 2019-02-01 stsp if (!ref_is_absolute)
367 0dec1cc0 2019-02-01 stsp free(abs_refname);
368 532920c8 2019-02-01 stsp if (err || *ref != NULL)
369 0dec1cc0 2019-02-01 stsp break;
370 0dec1cc0 2019-02-01 stsp }
371 fb79db15 2019-02-01 stsp free(line);
372 fb79db15 2019-02-01 stsp if (err)
373 fb79db15 2019-02-01 stsp break;
374 fb79db15 2019-02-01 stsp } while (*ref == NULL);
375 fb79db15 2019-02-01 stsp
376 fb79db15 2019-02-01 stsp return err;
377 fb79db15 2019-02-01 stsp }
378 fb79db15 2019-02-01 stsp
379 fb79db15 2019-02-01 stsp static const struct got_error *
380 d1f2edc9 2018-06-13 stsp open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
381 2f17228e 2019-05-12 stsp const char *name, int lock)
382 d1f2edc9 2018-06-13 stsp {
383 d1f2edc9 2018-06-13 stsp const struct got_error *err = NULL;
384 a04f49d2 2019-02-04 stsp char *path = NULL;
385 a04f49d2 2019-02-04 stsp char *absname = NULL;
386 a04f49d2 2019-02-04 stsp int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
387 a04f49d2 2019-02-04 stsp int ref_is_well_known = is_well_known_ref(name);
388 d1f2edc9 2018-06-13 stsp
389 1e37702e 2019-02-04 stsp *ref = NULL;
390 1e37702e 2019-02-04 stsp
391 a04f49d2 2019-02-04 stsp if (ref_is_absolute || ref_is_well_known) {
392 a04f49d2 2019-02-04 stsp if (asprintf(&path, "%s/%s", path_refs, name) == -1)
393 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
394 a04f49d2 2019-02-04 stsp absname = (char *)name;
395 a04f49d2 2019-02-04 stsp } else {
396 58908ed0 2019-03-11 stsp if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
397 58908ed0 2019-03-11 stsp subdir[0] ? "/" : "", name) == -1)
398 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
399 d1f2edc9 2018-06-13 stsp
400 58908ed0 2019-03-11 stsp if (asprintf(&absname, "refs/%s%s%s",
401 58908ed0 2019-03-11 stsp subdir, subdir[0] ? "/" : "", name) == -1) {
402 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
403 a04f49d2 2019-02-04 stsp goto done;
404 a04f49d2 2019-02-04 stsp }
405 a04f49d2 2019-02-04 stsp }
406 a04f49d2 2019-02-04 stsp
407 6e472252 2019-07-22 stsp err = parse_ref_file(ref, absname, path, lock);
408 d1f2edc9 2018-06-13 stsp done:
409 a04f49d2 2019-02-04 stsp if (!ref_is_absolute && !ref_is_well_known)
410 a04f49d2 2019-02-04 stsp free(absname);
411 a04f49d2 2019-02-04 stsp free(path);
412 d1f2edc9 2018-06-13 stsp return err;
413 d1f2edc9 2018-06-13 stsp }
414 d1f2edc9 2018-06-13 stsp
415 5261c201 2018-04-01 stsp const struct got_error *
416 5261c201 2018-04-01 stsp got_ref_open(struct got_reference **ref, struct got_repository *repo,
417 2f17228e 2019-05-12 stsp const char *refname, int lock)
418 5261c201 2018-04-01 stsp {
419 5261c201 2018-04-01 stsp const struct got_error *err = NULL;
420 c5f754cc 2019-02-01 stsp char *path_refs = NULL;
421 fb79db15 2019-02-01 stsp const char *subdirs[] = {
422 fb79db15 2019-02-01 stsp GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
423 fb79db15 2019-02-01 stsp };
424 c5f754cc 2019-02-01 stsp int i, well_known = is_well_known_ref(refname);
425 a875589a 2019-05-12 stsp struct got_lockfile *lf = NULL;
426 1e37702e 2019-02-04 stsp
427 1e37702e 2019-02-04 stsp *ref = NULL;
428 e135804e 2019-02-10 stsp
429 e135804e 2019-02-10 stsp path_refs = get_refs_dir_path(repo, refname);
430 e135804e 2019-02-10 stsp if (path_refs == NULL) {
431 638f9024 2019-05-13 stsp err = got_error_from_errno2("get_refs_dir_path", refname);
432 e135804e 2019-02-10 stsp goto done;
433 e135804e 2019-02-10 stsp }
434 5261c201 2018-04-01 stsp
435 0885ce8f 2019-05-12 stsp if (well_known) {
436 0885ce8f 2019-05-12 stsp err = open_ref(ref, path_refs, "", refname, lock);
437 0885ce8f 2019-05-12 stsp } else {
438 c5f754cc 2019-02-01 stsp char *packed_refs_path;
439 c5f754cc 2019-02-01 stsp FILE *f;
440 c5f754cc 2019-02-01 stsp
441 e135804e 2019-02-10 stsp /* Search on-disk refs before packed refs! */
442 e135804e 2019-02-10 stsp for (i = 0; i < nitems(subdirs); i++) {
443 2f17228e 2019-05-12 stsp err = open_ref(ref, path_refs, subdirs[i], refname,
444 2f17228e 2019-05-12 stsp lock);
445 e135804e 2019-02-10 stsp if (err || *ref)
446 e135804e 2019-02-10 stsp goto done;
447 e135804e 2019-02-10 stsp }
448 e135804e 2019-02-10 stsp
449 c5f754cc 2019-02-01 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
450 e135804e 2019-02-10 stsp if (packed_refs_path == NULL) {
451 638f9024 2019-05-13 stsp err = got_error_from_errno(
452 230a42bd 2019-05-11 jcs "got_repo_get_path_packed_refs");
453 e135804e 2019-02-10 stsp goto done;
454 e135804e 2019-02-10 stsp }
455 c5f754cc 2019-02-01 stsp
456 2f17228e 2019-05-12 stsp if (lock) {
457 a875589a 2019-05-12 stsp err = got_lockfile_lock(&lf, packed_refs_path);
458 2f17228e 2019-05-12 stsp if (err)
459 2f17228e 2019-05-12 stsp goto done;
460 2f17228e 2019-05-12 stsp }
461 c5f754cc 2019-02-01 stsp f = fopen(packed_refs_path, "rb");
462 c5f754cc 2019-02-01 stsp free(packed_refs_path);
463 c5f754cc 2019-02-01 stsp if (f != NULL) {
464 0dec1cc0 2019-02-01 stsp err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
465 0dec1cc0 2019-02-01 stsp refname);
466 a875589a 2019-05-12 stsp if (!err) {
467 a875589a 2019-05-12 stsp if (fclose(f) != 0) {
468 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
469 a875589a 2019-05-12 stsp got_ref_close(*ref);
470 a875589a 2019-05-12 stsp *ref = NULL;
471 6e472abb 2019-05-13 stsp } else if (*ref)
472 a875589a 2019-05-12 stsp (*ref)->lf = lf;
473 a875589a 2019-05-12 stsp }
474 fb79db15 2019-02-01 stsp }
475 fb79db15 2019-02-01 stsp }
476 e135804e 2019-02-10 stsp done:
477 5b575c25 2019-05-12 stsp if (!err && *ref == NULL)
478 1e37702e 2019-02-04 stsp err = got_error_not_ref(refname);
479 a875589a 2019-05-12 stsp if (err && lf)
480 a875589a 2019-05-12 stsp got_lockfile_unlock(lf);
481 5261c201 2018-04-01 stsp free(path_refs);
482 5261c201 2018-04-01 stsp return err;
483 5261c201 2018-04-01 stsp }
484 5261c201 2018-04-01 stsp
485 5261c201 2018-04-01 stsp void
486 5261c201 2018-04-01 stsp got_ref_close(struct got_reference *ref)
487 5261c201 2018-04-01 stsp {
488 e09d28b1 2019-03-15 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
489 5261c201 2018-04-01 stsp free(ref->ref.symref.name);
490 e09d28b1 2019-03-15 stsp free(ref->ref.symref.ref);
491 e09d28b1 2019-03-15 stsp } else
492 5261c201 2018-04-01 stsp free(ref->ref.ref.name);
493 5261c201 2018-04-01 stsp free(ref);
494 5261c201 2018-04-01 stsp }
495 5261c201 2018-04-01 stsp
496 5261c201 2018-04-01 stsp struct got_reference *
497 5261c201 2018-04-01 stsp got_ref_dup(struct got_reference *ref)
498 5261c201 2018-04-01 stsp {
499 5261c201 2018-04-01 stsp struct got_reference *ret;
500 5261c201 2018-04-01 stsp
501 5261c201 2018-04-01 stsp ret = calloc(1, sizeof(*ret));
502 5261c201 2018-04-01 stsp if (ret == NULL)
503 5261c201 2018-04-01 stsp return NULL;
504 5261c201 2018-04-01 stsp
505 5261c201 2018-04-01 stsp ret->flags = ref->flags;
506 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
507 5261c201 2018-04-01 stsp ret->ref.symref.name = strdup(ref->ref.symref.name);
508 5261c201 2018-04-01 stsp if (ret->ref.symref.name == NULL) {
509 5261c201 2018-04-01 stsp free(ret);
510 5261c201 2018-04-01 stsp return NULL;
511 5261c201 2018-04-01 stsp }
512 5261c201 2018-04-01 stsp ret->ref.symref.ref = strdup(ref->ref.symref.ref);
513 5261c201 2018-04-01 stsp if (ret->ref.symref.ref == NULL) {
514 5261c201 2018-04-01 stsp free(ret->ref.symref.name);
515 5261c201 2018-04-01 stsp free(ret);
516 5261c201 2018-04-01 stsp return NULL;
517 5261c201 2018-04-01 stsp }
518 5261c201 2018-04-01 stsp } else {
519 5261c201 2018-04-01 stsp ref->ref.ref.name = strdup(ref->ref.ref.name);
520 5261c201 2018-04-01 stsp if (ref->ref.ref.name == NULL) {
521 5261c201 2018-04-01 stsp free(ret);
522 5261c201 2018-04-01 stsp return NULL;
523 5261c201 2018-04-01 stsp }
524 5261c201 2018-04-01 stsp memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
525 80d5fc1f 2019-03-15 stsp sizeof(ret->ref.ref.sha1));
526 5261c201 2018-04-01 stsp }
527 5261c201 2018-04-01 stsp
528 5261c201 2018-04-01 stsp return ret;
529 5261c201 2018-04-01 stsp }
530 b8bad2ba 2019-08-23 stsp
531 b8bad2ba 2019-08-23 stsp const struct got_error *
532 b8bad2ba 2019-08-23 stsp got_reflist_entry_dup(struct got_reflist_entry **newp,
533 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re)
534 b8bad2ba 2019-08-23 stsp {
535 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
536 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *new;
537 b8bad2ba 2019-08-23 stsp
538 b8bad2ba 2019-08-23 stsp *newp = NULL;
539 b8bad2ba 2019-08-23 stsp
540 b8bad2ba 2019-08-23 stsp new = malloc(sizeof(*new));
541 b8bad2ba 2019-08-23 stsp if (new == NULL)
542 b8bad2ba 2019-08-23 stsp return got_error_from_errno("malloc");
543 5261c201 2018-04-01 stsp
544 b8bad2ba 2019-08-23 stsp new->ref = got_ref_dup(re->ref);
545 b8bad2ba 2019-08-23 stsp if (new->ref == NULL) {
546 b8bad2ba 2019-08-23 stsp err = got_error_from_errno("got_ref_dup");
547 b8bad2ba 2019-08-23 stsp free(new);
548 b8bad2ba 2019-08-23 stsp return err;
549 b8bad2ba 2019-08-23 stsp }
550 b8bad2ba 2019-08-23 stsp
551 b8bad2ba 2019-08-23 stsp new->id = got_object_id_dup(re->id);
552 b8bad2ba 2019-08-23 stsp if (new->id == NULL) {
553 b8bad2ba 2019-08-23 stsp err = got_error_from_errno("got_ref_dup");
554 b8bad2ba 2019-08-23 stsp free(new->id);
555 b8bad2ba 2019-08-23 stsp free(new);
556 b8bad2ba 2019-08-23 stsp return err;
557 b8bad2ba 2019-08-23 stsp }
558 b8bad2ba 2019-08-23 stsp
559 b8bad2ba 2019-08-23 stsp *newp = new;
560 b8bad2ba 2019-08-23 stsp return NULL;
561 b8bad2ba 2019-08-23 stsp }
562 b8bad2ba 2019-08-23 stsp
563 5261c201 2018-04-01 stsp static const struct got_error *
564 5261c201 2018-04-01 stsp resolve_symbolic_ref(struct got_reference **resolved,
565 5261c201 2018-04-01 stsp struct got_repository *repo, struct got_reference *ref)
566 5261c201 2018-04-01 stsp {
567 5261c201 2018-04-01 stsp struct got_reference *nextref;
568 5261c201 2018-04-01 stsp const struct got_error *err;
569 5261c201 2018-04-01 stsp
570 2f17228e 2019-05-12 stsp err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
571 5261c201 2018-04-01 stsp if (err)
572 5261c201 2018-04-01 stsp return err;
573 5261c201 2018-04-01 stsp
574 5261c201 2018-04-01 stsp if (nextref->flags & GOT_REF_IS_SYMBOLIC)
575 5261c201 2018-04-01 stsp err = resolve_symbolic_ref(resolved, repo, nextref);
576 5261c201 2018-04-01 stsp else
577 5261c201 2018-04-01 stsp *resolved = got_ref_dup(nextref);
578 5261c201 2018-04-01 stsp
579 5261c201 2018-04-01 stsp got_ref_close(nextref);
580 5261c201 2018-04-01 stsp return err;
581 5261c201 2018-04-01 stsp }
582 5261c201 2018-04-01 stsp
583 29e86f7a 2019-08-12 stsp static const struct got_error *
584 29e86f7a 2019-08-12 stsp ref_resolve(struct got_object_id **id, struct got_repository *repo,
585 29e86f7a 2019-08-12 stsp struct got_reference *ref, int recursion)
586 5261c201 2018-04-01 stsp {
587 5261c201 2018-04-01 stsp const struct got_error *err;
588 5261c201 2018-04-01 stsp
589 29e86f7a 2019-08-12 stsp if (recursion <= 0)
590 29e86f7a 2019-08-12 stsp return got_error_msg(GOT_ERR_RECURSION,
591 29e86f7a 2019-08-12 stsp "reference recursion limit reached");
592 29e86f7a 2019-08-12 stsp
593 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
594 5261c201 2018-04-01 stsp struct got_reference *resolved = NULL;
595 5261c201 2018-04-01 stsp err = resolve_symbolic_ref(&resolved, repo, ref);
596 5261c201 2018-04-01 stsp if (err == NULL)
597 29e86f7a 2019-08-12 stsp err = ref_resolve(id, repo, resolved, --recursion);
598 57b6f99a 2019-04-13 stsp if (resolved)
599 57b6f99a 2019-04-13 stsp got_ref_close(resolved);
600 5261c201 2018-04-01 stsp return err;
601 5261c201 2018-04-01 stsp }
602 5261c201 2018-04-01 stsp
603 5261c201 2018-04-01 stsp *id = calloc(1, sizeof(**id));
604 5261c201 2018-04-01 stsp if (*id == NULL)
605 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
606 80d5fc1f 2019-03-15 stsp memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
607 5261c201 2018-04-01 stsp return NULL;
608 29e86f7a 2019-08-12 stsp }
609 29e86f7a 2019-08-12 stsp
610 29e86f7a 2019-08-12 stsp const struct got_error *
611 29e86f7a 2019-08-12 stsp got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
612 29e86f7a 2019-08-12 stsp struct got_reference *ref)
613 29e86f7a 2019-08-12 stsp {
614 29e86f7a 2019-08-12 stsp return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
615 5261c201 2018-04-01 stsp }
616 5261c201 2018-04-01 stsp
617 5261c201 2018-04-01 stsp char *
618 5261c201 2018-04-01 stsp got_ref_to_str(struct got_reference *ref)
619 5261c201 2018-04-01 stsp {
620 5261c201 2018-04-01 stsp char *str;
621 271d2a38 2018-12-25 stsp
622 271d2a38 2018-12-25 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
623 271d2a38 2018-12-25 stsp return strdup(ref->ref.symref.ref);
624 271d2a38 2018-12-25 stsp
625 271d2a38 2018-12-25 stsp str = malloc(SHA1_DIGEST_STRING_LENGTH);
626 271d2a38 2018-12-25 stsp if (str == NULL)
627 271d2a38 2018-12-25 stsp return NULL;
628 271d2a38 2018-12-25 stsp
629 271d2a38 2018-12-25 stsp if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
630 271d2a38 2018-12-25 stsp SHA1_DIGEST_STRING_LENGTH) == NULL) {
631 271d2a38 2018-12-25 stsp free(str);
632 271d2a38 2018-12-25 stsp return NULL;
633 5261c201 2018-04-01 stsp }
634 5261c201 2018-04-01 stsp
635 5261c201 2018-04-01 stsp return str;
636 5261c201 2018-04-01 stsp }
637 0bd18d37 2019-02-01 stsp
638 0bd18d37 2019-02-01 stsp const char *
639 0bd18d37 2019-02-01 stsp got_ref_get_name(struct got_reference *ref)
640 0bd18d37 2019-02-01 stsp {
641 0bd18d37 2019-02-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
642 0bd18d37 2019-02-01 stsp return ref->ref.symref.name;
643 0bd18d37 2019-02-01 stsp
644 0bd18d37 2019-02-01 stsp return ref->ref.ref.name;
645 199a4027 2019-02-02 stsp }
646 199a4027 2019-02-02 stsp
647 aaf88317 2019-07-10 stsp const char *
648 aaf88317 2019-07-10 stsp got_ref_get_symref_target(struct got_reference *ref)
649 aaf88317 2019-07-10 stsp {
650 aaf88317 2019-07-10 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
651 aaf88317 2019-07-10 stsp return ref->ref.symref.ref;
652 b8bad2ba 2019-08-23 stsp
653 b8bad2ba 2019-08-23 stsp return NULL;
654 b8bad2ba 2019-08-23 stsp }
655 b8bad2ba 2019-08-23 stsp
656 b8bad2ba 2019-08-23 stsp const struct got_error *
657 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
658 b8bad2ba 2019-08-23 stsp struct got_reference* re2)
659 b8bad2ba 2019-08-23 stsp {
660 b8bad2ba 2019-08-23 stsp const char *name1 = got_ref_get_name(re1);
661 b8bad2ba 2019-08-23 stsp const char *name2 = got_ref_get_name(re2);
662 aaf88317 2019-07-10 stsp
663 b8bad2ba 2019-08-23 stsp *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
664 aaf88317 2019-07-10 stsp return NULL;
665 aaf88317 2019-07-10 stsp }
666 aaf88317 2019-07-10 stsp
667 199a4027 2019-02-02 stsp static const struct got_error *
668 505287be 2019-03-15 stsp insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
669 b8bad2ba 2019-08-23 stsp struct got_reference *ref, struct got_repository *repo,
670 b8bad2ba 2019-08-23 stsp got_ref_cmp_cb cmp_cb, void *cmp_arg)
671 199a4027 2019-02-02 stsp {
672 199a4027 2019-02-02 stsp const struct got_error *err;
673 199a4027 2019-02-02 stsp struct got_object_id *id;
674 6249107b 2019-03-15 stsp struct got_reflist_entry *new, *re, *prev = NULL;
675 e397b6db 2019-02-04 stsp int cmp;
676 7a3c76f5 2019-02-05 stsp
677 505287be 2019-03-15 stsp *newp = NULL;
678 505287be 2019-03-15 stsp
679 7a3c76f5 2019-02-05 stsp err = got_ref_resolve(&id, repo, ref);
680 7a3c76f5 2019-02-05 stsp if (err)
681 7a3c76f5 2019-02-05 stsp return err;
682 29b5c214 2019-02-04 stsp
683 6fdbf7b0 2019-03-15 stsp new = malloc(sizeof(*new));
684 7a3c76f5 2019-02-05 stsp if (new == NULL) {
685 7a3c76f5 2019-02-05 stsp free(id);
686 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
687 7a3c76f5 2019-02-05 stsp }
688 7a3c76f5 2019-02-05 stsp new->ref = ref;
689 7a3c76f5 2019-02-05 stsp new->id = id;
690 505287be 2019-03-15 stsp *newp = new;
691 7a3c76f5 2019-02-05 stsp
692 29b5c214 2019-02-04 stsp /*
693 29b5c214 2019-02-04 stsp * We must de-duplicate entries on insert because packed-refs may
694 29b5c214 2019-02-04 stsp * contain redundant entries. On-disk refs take precedence.
695 29b5c214 2019-02-04 stsp * This code assumes that on-disk revs are read before packed-refs.
696 e397b6db 2019-02-04 stsp * We're iterating the list anyway, so insert elements sorted by name.
697 29b5c214 2019-02-04 stsp */
698 7a3c76f5 2019-02-05 stsp re = SIMPLEQ_FIRST(refs);
699 7a3c76f5 2019-02-05 stsp while (re) {
700 b8bad2ba 2019-08-23 stsp err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
701 b8bad2ba 2019-08-23 stsp if (err)
702 b8bad2ba 2019-08-23 stsp return err;
703 e397b6db 2019-02-04 stsp if (cmp == 0) {
704 27a1ed03 2019-03-15 stsp /* duplicate */
705 27a1ed03 2019-03-15 stsp free(new->id);
706 27a1ed03 2019-03-15 stsp free(new);
707 505287be 2019-03-15 stsp *newp = NULL;
708 7a3c76f5 2019-02-05 stsp return NULL;
709 7a3c76f5 2019-02-05 stsp } else if (cmp > 0) {
710 7a3c76f5 2019-02-05 stsp if (prev)
711 7a3c76f5 2019-02-05 stsp SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
712 7a3c76f5 2019-02-05 stsp else
713 7a3c76f5 2019-02-05 stsp SIMPLEQ_INSERT_HEAD(refs, new, entry);
714 7a3c76f5 2019-02-05 stsp return NULL;
715 7a3c76f5 2019-02-05 stsp } else {
716 e397b6db 2019-02-04 stsp prev = re;
717 7a3c76f5 2019-02-05 stsp re = SIMPLEQ_NEXT(re, entry);
718 7a3c76f5 2019-02-05 stsp }
719 29b5c214 2019-02-04 stsp }
720 199a4027 2019-02-02 stsp
721 7a3c76f5 2019-02-05 stsp SIMPLEQ_INSERT_TAIL(refs, new, entry);
722 199a4027 2019-02-02 stsp return NULL;
723 0bd18d37 2019-02-01 stsp }
724 199a4027 2019-02-02 stsp
725 a04f49d2 2019-02-04 stsp static const struct got_error *
726 29b5c214 2019-02-04 stsp gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
727 b8bad2ba 2019-08-23 stsp const char *subdir, struct got_repository *repo,
728 b8bad2ba 2019-08-23 stsp got_ref_cmp_cb cmp_cb, void *cmp_arg)
729 a04f49d2 2019-02-04 stsp {
730 a04f49d2 2019-02-04 stsp const struct got_error *err = NULL;
731 a04f49d2 2019-02-04 stsp DIR *d = NULL;
732 a04f49d2 2019-02-04 stsp char *path_subdir;
733 a04f49d2 2019-02-04 stsp
734 a04f49d2 2019-02-04 stsp if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
735 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
736 a04f49d2 2019-02-04 stsp
737 a04f49d2 2019-02-04 stsp d = opendir(path_subdir);
738 a04f49d2 2019-02-04 stsp if (d == NULL)
739 a04f49d2 2019-02-04 stsp goto done;
740 a04f49d2 2019-02-04 stsp
741 656b1f76 2019-05-11 jcs for (;;) {
742 a04f49d2 2019-02-04 stsp struct dirent *dent;
743 a04f49d2 2019-02-04 stsp struct got_reference *ref;
744 a04f49d2 2019-02-04 stsp char *child;
745 a04f49d2 2019-02-04 stsp
746 a04f49d2 2019-02-04 stsp dent = readdir(d);
747 a04f49d2 2019-02-04 stsp if (dent == NULL)
748 a04f49d2 2019-02-04 stsp break;
749 a04f49d2 2019-02-04 stsp
750 a04f49d2 2019-02-04 stsp if (strcmp(dent->d_name, ".") == 0 ||
751 a04f49d2 2019-02-04 stsp strcmp(dent->d_name, "..") == 0)
752 a04f49d2 2019-02-04 stsp continue;
753 a04f49d2 2019-02-04 stsp
754 a04f49d2 2019-02-04 stsp switch (dent->d_type) {
755 a04f49d2 2019-02-04 stsp case DT_REG:
756 2f17228e 2019-05-12 stsp err = open_ref(&ref, path_refs, subdir, dent->d_name,
757 2f17228e 2019-05-12 stsp 0);
758 1e37702e 2019-02-04 stsp if (err)
759 a04f49d2 2019-02-04 stsp goto done;
760 a04f49d2 2019-02-04 stsp if (ref) {
761 505287be 2019-03-15 stsp struct got_reflist_entry *new;
762 b8bad2ba 2019-08-23 stsp err = insert_ref(&new, refs, ref, repo,
763 b8bad2ba 2019-08-23 stsp cmp_cb, cmp_arg);
764 505287be 2019-03-15 stsp if (err || new == NULL /* duplicate */)
765 505287be 2019-03-15 stsp got_ref_close(ref);
766 a04f49d2 2019-02-04 stsp if (err)
767 a04f49d2 2019-02-04 stsp goto done;
768 a04f49d2 2019-02-04 stsp }
769 a04f49d2 2019-02-04 stsp break;
770 a04f49d2 2019-02-04 stsp case DT_DIR:
771 a04f49d2 2019-02-04 stsp if (asprintf(&child, "%s%s%s", subdir,
772 a04f49d2 2019-02-04 stsp subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
773 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
774 a04f49d2 2019-02-04 stsp break;
775 a04f49d2 2019-02-04 stsp }
776 b8bad2ba 2019-08-23 stsp err = gather_on_disk_refs(refs, path_refs, child, repo,
777 b8bad2ba 2019-08-23 stsp cmp_cb, cmp_arg);
778 a04f49d2 2019-02-04 stsp free(child);
779 a04f49d2 2019-02-04 stsp break;
780 a04f49d2 2019-02-04 stsp default:
781 a04f49d2 2019-02-04 stsp break;
782 a04f49d2 2019-02-04 stsp }
783 a04f49d2 2019-02-04 stsp }
784 a04f49d2 2019-02-04 stsp done:
785 a04f49d2 2019-02-04 stsp if (d)
786 a04f49d2 2019-02-04 stsp closedir(d);
787 a04f49d2 2019-02-04 stsp free(path_subdir);
788 a04f49d2 2019-02-04 stsp return err;
789 a04f49d2 2019-02-04 stsp }
790 a04f49d2 2019-02-04 stsp
791 199a4027 2019-02-02 stsp const struct got_error *
792 29606af7 2019-08-23 stsp got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
793 b8bad2ba 2019-08-23 stsp const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
794 199a4027 2019-02-02 stsp {
795 199a4027 2019-02-02 stsp const struct got_error *err;
796 a04f49d2 2019-02-04 stsp char *packed_refs_path, *path_refs = NULL;
797 6aeab596 2019-08-28 stsp const char *ondisk_ref_namespace = NULL;
798 29b5c214 2019-02-04 stsp FILE *f = NULL;
799 199a4027 2019-02-02 stsp struct got_reference *ref;
800 505287be 2019-03-15 stsp struct got_reflist_entry *new;
801 199a4027 2019-02-02 stsp
802 29606af7 2019-08-23 stsp if (ref_namespace == NULL || ref_namespace[0] == '\0') {
803 29606af7 2019-08-23 stsp /* HEAD ref should always exist. */
804 29606af7 2019-08-23 stsp path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
805 29606af7 2019-08-23 stsp if (path_refs == NULL) {
806 29606af7 2019-08-23 stsp err = got_error_from_errno("get_refs_dir_path");
807 29606af7 2019-08-23 stsp goto done;
808 29606af7 2019-08-23 stsp }
809 29606af7 2019-08-23 stsp err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
810 29606af7 2019-08-23 stsp if (err)
811 29606af7 2019-08-23 stsp goto done;
812 b8bad2ba 2019-08-23 stsp err = insert_ref(&new, refs, ref, repo, cmp_cb, cmp_arg);
813 29606af7 2019-08-23 stsp if (err || new == NULL /* duplicate */)
814 29606af7 2019-08-23 stsp got_ref_close(ref);
815 29606af7 2019-08-23 stsp if (err)
816 29606af7 2019-08-23 stsp goto done;
817 29b5c214 2019-02-04 stsp }
818 29b5c214 2019-02-04 stsp
819 6aeab596 2019-08-28 stsp ondisk_ref_namespace = ref_namespace;
820 29606af7 2019-08-23 stsp if (ref_namespace && strncmp(ref_namespace, "refs/", 5) == 0)
821 6aeab596 2019-08-28 stsp ondisk_ref_namespace += 5;
822 29606af7 2019-08-23 stsp
823 29b5c214 2019-02-04 stsp /* Gather on-disk refs before parsing packed-refs. */
824 29b5c214 2019-02-04 stsp free(path_refs);
825 29b5c214 2019-02-04 stsp path_refs = get_refs_dir_path(repo, "");
826 29b5c214 2019-02-04 stsp if (path_refs == NULL) {
827 638f9024 2019-05-13 stsp err = got_error_from_errno("get_refs_dir_path");
828 29b5c214 2019-02-04 stsp goto done;
829 29b5c214 2019-02-04 stsp }
830 29606af7 2019-08-23 stsp err = gather_on_disk_refs(refs, path_refs,
831 6aeab596 2019-08-28 stsp ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
832 6aeab596 2019-08-28 stsp cmp_cb, cmp_arg);
833 29b5c214 2019-02-04 stsp if (err)
834 29b5c214 2019-02-04 stsp goto done;
835 29b5c214 2019-02-04 stsp
836 29b5c214 2019-02-04 stsp /*
837 29b5c214 2019-02-04 stsp * The packed-refs file may contain redundant entries, in which
838 29b5c214 2019-02-04 stsp * case on-disk refs take precedence.
839 29b5c214 2019-02-04 stsp */
840 199a4027 2019-02-02 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
841 29b5c214 2019-02-04 stsp if (packed_refs_path == NULL) {
842 638f9024 2019-05-13 stsp err = got_error_from_errno("got_repo_get_path_packed_refs");
843 29b5c214 2019-02-04 stsp goto done;
844 29b5c214 2019-02-04 stsp }
845 199a4027 2019-02-02 stsp
846 199a4027 2019-02-02 stsp f = fopen(packed_refs_path, "r");
847 199a4027 2019-02-02 stsp free(packed_refs_path);
848 199a4027 2019-02-02 stsp if (f) {
849 199a4027 2019-02-02 stsp char *line;
850 199a4027 2019-02-02 stsp size_t len;
851 199a4027 2019-02-02 stsp const char delim[3] = {'\0', '\0', '\0'};
852 656b1f76 2019-05-11 jcs for (;;) {
853 199a4027 2019-02-02 stsp line = fparseln(f, &len, NULL, delim, 0);
854 0bb4abae 2019-03-15 stsp if (line == NULL) {
855 0bb4abae 2019-03-15 stsp if (feof(f))
856 0bb4abae 2019-03-15 stsp break;
857 0bb4abae 2019-03-15 stsp err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
858 0bb4abae 2019-03-15 stsp goto done;
859 0bb4abae 2019-03-15 stsp }
860 199a4027 2019-02-02 stsp err = parse_packed_ref_line(&ref, NULL, line);
861 0bb4abae 2019-03-15 stsp free(line);
862 199a4027 2019-02-02 stsp if (err)
863 199a4027 2019-02-02 stsp goto done;
864 76b4ead2 2019-02-03 stsp if (ref) {
865 29606af7 2019-08-23 stsp if (ref_namespace) {
866 29606af7 2019-08-23 stsp const char *name;
867 29606af7 2019-08-23 stsp name = got_ref_get_name(ref);
868 29606af7 2019-08-23 stsp if (strncmp(name, ref_namespace,
869 29606af7 2019-08-23 stsp strlen(ref_namespace)) != 0) {
870 29606af7 2019-08-23 stsp got_ref_close(ref);
871 29606af7 2019-08-23 stsp continue;
872 29606af7 2019-08-23 stsp }
873 29606af7 2019-08-23 stsp }
874 b8bad2ba 2019-08-23 stsp err = insert_ref(&new, refs, ref, repo,
875 b8bad2ba 2019-08-23 stsp cmp_cb, cmp_arg);
876 505287be 2019-03-15 stsp if (err || new == NULL /* duplicate */)
877 505287be 2019-03-15 stsp got_ref_close(ref);
878 76b4ead2 2019-02-03 stsp if (err)
879 76b4ead2 2019-02-03 stsp goto done;
880 76b4ead2 2019-02-03 stsp }
881 199a4027 2019-02-02 stsp }
882 199a4027 2019-02-02 stsp }
883 199a4027 2019-02-02 stsp done:
884 a04f49d2 2019-02-04 stsp free(path_refs);
885 fb43ecf1 2019-02-11 stsp if (f && fclose(f) != 0 && err == NULL)
886 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
887 199a4027 2019-02-02 stsp return err;
888 199a4027 2019-02-02 stsp }
889 9e672c74 2019-03-11 stsp
890 e2e879a0 2019-03-11 stsp void
891 e2e879a0 2019-03-11 stsp got_ref_list_free(struct got_reflist_head *refs)
892 e2e879a0 2019-03-11 stsp {
893 e2e879a0 2019-03-11 stsp struct got_reflist_entry *re;
894 e2e879a0 2019-03-11 stsp
895 e2e879a0 2019-03-11 stsp while (!SIMPLEQ_EMPTY(refs)) {
896 e2e879a0 2019-03-11 stsp re = SIMPLEQ_FIRST(refs);
897 e2e879a0 2019-03-11 stsp SIMPLEQ_REMOVE_HEAD(refs, entry);
898 e2e879a0 2019-03-11 stsp got_ref_close(re->ref);
899 e2e879a0 2019-03-11 stsp free(re->id);
900 e2e879a0 2019-03-11 stsp free(re);
901 e2e879a0 2019-03-11 stsp }
902 b249b824 2019-05-09 stsp
903 b249b824 2019-05-09 stsp }
904 b249b824 2019-05-09 stsp
905 b249b824 2019-05-09 stsp int
906 b249b824 2019-05-09 stsp got_ref_is_symbolic(struct got_reference *ref)
907 b249b824 2019-05-09 stsp {
908 b249b824 2019-05-09 stsp return (ref->flags & GOT_REF_IS_SYMBOLIC);
909 b249b824 2019-05-09 stsp }
910 b249b824 2019-05-09 stsp
911 b249b824 2019-05-09 stsp const struct got_error *
912 b249b824 2019-05-09 stsp got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
913 b249b824 2019-05-09 stsp {
914 b249b824 2019-05-09 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
915 b249b824 2019-05-09 stsp return got_error(GOT_ERR_BAD_REF_TYPE);
916 e2e879a0 2019-03-11 stsp
917 b249b824 2019-05-09 stsp memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
918 b249b824 2019-05-09 stsp return NULL;
919 e2e879a0 2019-03-11 stsp }
920 e2e879a0 2019-03-11 stsp
921 9e672c74 2019-03-11 stsp const struct got_error *
922 b249b824 2019-05-09 stsp got_ref_change_symref(struct got_reference *ref, char *refname)
923 b249b824 2019-05-09 stsp {
924 b249b824 2019-05-09 stsp char *new_name;
925 b249b824 2019-05-09 stsp
926 b249b824 2019-05-09 stsp if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
927 b249b824 2019-05-09 stsp return got_error(GOT_ERR_BAD_REF_TYPE);
928 b249b824 2019-05-09 stsp
929 b249b824 2019-05-09 stsp new_name = strdup(refname);
930 b249b824 2019-05-09 stsp if (new_name == NULL)
931 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
932 b249b824 2019-05-09 stsp
933 b249b824 2019-05-09 stsp free(ref->ref.symref.name);
934 b249b824 2019-05-09 stsp ref->ref.symref.name = new_name;
935 b249b824 2019-05-09 stsp return NULL;
936 b249b824 2019-05-09 stsp }
937 b249b824 2019-05-09 stsp
938 b249b824 2019-05-09 stsp const struct got_error *
939 9e672c74 2019-03-11 stsp got_ref_write(struct got_reference *ref, struct got_repository *repo)
940 9e672c74 2019-03-11 stsp {
941 9e672c74 2019-03-11 stsp const struct got_error *err = NULL, *unlock_err = NULL;
942 9e672c74 2019-03-11 stsp const char *name = got_ref_get_name(ref);
943 9e672c74 2019-03-11 stsp char *path_refs = NULL, *path = NULL, *tmppath = NULL;
944 9e672c74 2019-03-11 stsp struct got_lockfile *lf = NULL;
945 9e672c74 2019-03-11 stsp FILE *f = NULL;
946 9e672c74 2019-03-11 stsp size_t n;
947 9e672c74 2019-03-11 stsp struct stat sb;
948 9e672c74 2019-03-11 stsp
949 9e672c74 2019-03-11 stsp path_refs = get_refs_dir_path(repo, name);
950 9e672c74 2019-03-11 stsp if (path_refs == NULL) {
951 638f9024 2019-05-13 stsp err = got_error_from_errno2("get_refs_dir_path", name);
952 9e672c74 2019-03-11 stsp goto done;
953 9e672c74 2019-03-11 stsp }
954 9e672c74 2019-03-11 stsp
955 9e672c74 2019-03-11 stsp if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
956 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
957 9e672c74 2019-03-11 stsp goto done;
958 9e672c74 2019-03-11 stsp }
959 9e672c74 2019-03-11 stsp
960 9e672c74 2019-03-11 stsp err = got_opentemp_named(&tmppath, &f, path);
961 49c7094f 2019-03-11 stsp if (err) {
962 d1667f0d 2019-03-11 stsp char *parent;
963 49c7094f 2019-03-11 stsp if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
964 5e1c9f23 2019-03-11 stsp goto done;
965 d1667f0d 2019-03-11 stsp err = got_path_dirname(&parent, path);
966 d1667f0d 2019-03-11 stsp if (err)
967 0cd1c46a 2019-03-11 stsp goto done;
968 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(parent);
969 5e1c9f23 2019-03-11 stsp free(parent);
970 0cd1c46a 2019-03-11 stsp if (err)
971 0cd1c46a 2019-03-11 stsp goto done;
972 0cd1c46a 2019-03-11 stsp err = got_opentemp_named(&tmppath, &f, path);
973 49c7094f 2019-03-11 stsp if (err)
974 0cd1c46a 2019-03-11 stsp goto done;
975 9e672c74 2019-03-11 stsp }
976 9e672c74 2019-03-11 stsp
977 9e672c74 2019-03-11 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
978 9e672c74 2019-03-11 stsp n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
979 9e672c74 2019-03-11 stsp if (n != strlen(ref->ref.symref.ref) + 6) {
980 9e672c74 2019-03-11 stsp err = got_ferror(f, GOT_ERR_IO);
981 9e672c74 2019-03-11 stsp goto done;
982 9e672c74 2019-03-11 stsp }
983 9e672c74 2019-03-11 stsp } else {
984 9e672c74 2019-03-11 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
985 9e672c74 2019-03-11 stsp if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
986 9e672c74 2019-03-11 stsp sizeof(hex)) == NULL) {
987 9e672c74 2019-03-11 stsp err = got_error(GOT_ERR_BAD_REF_DATA);
988 9e672c74 2019-03-11 stsp goto done;
989 9e672c74 2019-03-11 stsp }
990 9e672c74 2019-03-11 stsp n = fprintf(f, "%s\n", hex);
991 8fa2f096 2019-03-11 stsp if (n != sizeof(hex)) {
992 9e672c74 2019-03-11 stsp err = got_ferror(f, GOT_ERR_IO);
993 9e672c74 2019-03-11 stsp goto done;
994 9e672c74 2019-03-11 stsp }
995 9e672c74 2019-03-11 stsp }
996 9e672c74 2019-03-11 stsp
997 2f17228e 2019-05-12 stsp if (ref->lf == NULL) {
998 2f17228e 2019-05-12 stsp err = got_lockfile_lock(&lf, path);
999 2f17228e 2019-05-12 stsp if (err)
1000 2f17228e 2019-05-12 stsp goto done;
1001 2f17228e 2019-05-12 stsp }
1002 9e672c74 2019-03-11 stsp
1003 9e672c74 2019-03-11 stsp /* XXX: check if old content matches our expectations? */
1004 9e672c74 2019-03-11 stsp
1005 0cd1c46a 2019-03-11 stsp if (stat(path, &sb) != 0) {
1006 0cd1c46a 2019-03-11 stsp if (errno != ENOENT) {
1007 638f9024 2019-05-13 stsp err = got_error_from_errno2("stat", path);
1008 0cd1c46a 2019-03-11 stsp goto done;
1009 0cd1c46a 2019-03-11 stsp }
1010 0cd1c46a 2019-03-11 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1011 9e672c74 2019-03-11 stsp }
1012 9e672c74 2019-03-11 stsp
1013 9e672c74 2019-03-11 stsp if (rename(tmppath, path) != 0) {
1014 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
1015 9e672c74 2019-03-11 stsp goto done;
1016 9e672c74 2019-03-11 stsp }
1017 9e672c74 2019-03-11 stsp free(tmppath);
1018 9e672c74 2019-03-11 stsp tmppath = NULL;
1019 9e672c74 2019-03-11 stsp
1020 9e672c74 2019-03-11 stsp if (chmod(path, sb.st_mode) != 0) {
1021 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", path);
1022 9e672c74 2019-03-11 stsp goto done;
1023 9e672c74 2019-03-11 stsp }
1024 9e672c74 2019-03-11 stsp done:
1025 2f17228e 2019-05-12 stsp if (ref->lf == NULL && lf)
1026 9e672c74 2019-03-11 stsp unlock_err = got_lockfile_unlock(lf);
1027 9e672c74 2019-03-11 stsp if (f) {
1028 9e672c74 2019-03-11 stsp if (fclose(f) != 0 && err == NULL)
1029 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1030 9e672c74 2019-03-11 stsp }
1031 9e672c74 2019-03-11 stsp free(path_refs);
1032 9e672c74 2019-03-11 stsp free(path);
1033 9e672c74 2019-03-11 stsp if (tmppath) {
1034 9e672c74 2019-03-11 stsp if (unlink(tmppath) != 0 && err == NULL)
1035 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", tmppath);
1036 9e672c74 2019-03-11 stsp free(tmppath);
1037 2d2e1378 2019-03-11 stsp }
1038 2d2e1378 2019-03-11 stsp return err ? err : unlock_err;
1039 2d2e1378 2019-03-11 stsp }
1040 598a8b91 2019-03-15 stsp
1041 598a8b91 2019-03-15 stsp static const struct got_error *
1042 598a8b91 2019-03-15 stsp delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1043 598a8b91 2019-03-15 stsp {
1044 598a8b91 2019-03-15 stsp const struct got_error *err = NULL, *unlock_err = NULL;
1045 598a8b91 2019-03-15 stsp struct got_lockfile *lf = NULL;
1046 598a8b91 2019-03-15 stsp FILE *f = NULL, *tmpf = NULL;
1047 598a8b91 2019-03-15 stsp char *packed_refs_path, *tmppath = NULL;
1048 598a8b91 2019-03-15 stsp struct got_reflist_head refs;
1049 598a8b91 2019-03-15 stsp int found_delref = 0;
1050 2d2e1378 2019-03-11 stsp
1051 598a8b91 2019-03-15 stsp /* The packed-refs file does not cotain symbolic references. */
1052 598a8b91 2019-03-15 stsp if (delref->flags & GOT_REF_IS_SYMBOLIC)
1053 598a8b91 2019-03-15 stsp return got_error(GOT_ERR_BAD_REF_DATA);
1054 598a8b91 2019-03-15 stsp
1055 598a8b91 2019-03-15 stsp SIMPLEQ_INIT(&refs);
1056 598a8b91 2019-03-15 stsp
1057 598a8b91 2019-03-15 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
1058 598a8b91 2019-03-15 stsp if (packed_refs_path == NULL)
1059 638f9024 2019-05-13 stsp return got_error_from_errno("got_repo_get_path_packed_refs");
1060 598a8b91 2019-03-15 stsp
1061 598a8b91 2019-03-15 stsp err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1062 598a8b91 2019-03-15 stsp if (err)
1063 598a8b91 2019-03-15 stsp goto done;
1064 598a8b91 2019-03-15 stsp
1065 2f17228e 2019-05-12 stsp if (delref->lf == NULL) {
1066 2f17228e 2019-05-12 stsp err = got_lockfile_lock(&lf, packed_refs_path);
1067 2f17228e 2019-05-12 stsp if (err)
1068 2f17228e 2019-05-12 stsp goto done;
1069 2f17228e 2019-05-12 stsp }
1070 598a8b91 2019-03-15 stsp
1071 598a8b91 2019-03-15 stsp f = fopen(packed_refs_path, "r");
1072 598a8b91 2019-03-15 stsp if (f == NULL) {
1073 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", packed_refs_path);
1074 598a8b91 2019-03-15 stsp goto done;
1075 598a8b91 2019-03-15 stsp }
1076 656b1f76 2019-05-11 jcs for (;;) {
1077 598a8b91 2019-03-15 stsp char *line;
1078 598a8b91 2019-03-15 stsp size_t len;
1079 598a8b91 2019-03-15 stsp const char delim[3] = {'\0', '\0', '\0'};
1080 598a8b91 2019-03-15 stsp struct got_reference *ref;
1081 598a8b91 2019-03-15 stsp struct got_reflist_entry *new;
1082 598a8b91 2019-03-15 stsp
1083 598a8b91 2019-03-15 stsp line = fparseln(f, &len, NULL, delim, 0);
1084 598a8b91 2019-03-15 stsp if (line == NULL) {
1085 598a8b91 2019-03-15 stsp if (feof(f))
1086 598a8b91 2019-03-15 stsp break;
1087 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1088 598a8b91 2019-03-15 stsp goto done;
1089 598a8b91 2019-03-15 stsp }
1090 598a8b91 2019-03-15 stsp err = parse_packed_ref_line(&ref, NULL, line);
1091 598a8b91 2019-03-15 stsp free(line);
1092 598a8b91 2019-03-15 stsp if (err)
1093 598a8b91 2019-03-15 stsp goto done;
1094 598a8b91 2019-03-15 stsp if (ref == NULL)
1095 598a8b91 2019-03-15 stsp continue;
1096 598a8b91 2019-03-15 stsp
1097 598a8b91 2019-03-15 stsp if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1098 598a8b91 2019-03-15 stsp memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1099 598a8b91 2019-03-15 stsp sizeof(delref->ref.ref.sha1)) == 0) {
1100 598a8b91 2019-03-15 stsp found_delref = 1;
1101 598a8b91 2019-03-15 stsp got_ref_close(ref);
1102 598a8b91 2019-03-15 stsp continue;
1103 598a8b91 2019-03-15 stsp }
1104 598a8b91 2019-03-15 stsp
1105 b8bad2ba 2019-08-23 stsp err = insert_ref(&new, &refs, ref, repo,
1106 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
1107 598a8b91 2019-03-15 stsp if (err || new == NULL /* duplicate */)
1108 598a8b91 2019-03-15 stsp got_ref_close(ref);
1109 598a8b91 2019-03-15 stsp if (err)
1110 598a8b91 2019-03-15 stsp goto done;
1111 598a8b91 2019-03-15 stsp }
1112 598a8b91 2019-03-15 stsp
1113 598a8b91 2019-03-15 stsp if (found_delref) {
1114 598a8b91 2019-03-15 stsp struct got_reflist_entry *re;
1115 598a8b91 2019-03-15 stsp size_t n;
1116 598a8b91 2019-03-15 stsp struct stat sb;
1117 598a8b91 2019-03-15 stsp
1118 598a8b91 2019-03-15 stsp n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1119 598a8b91 2019-03-15 stsp if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1120 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_IO);
1121 598a8b91 2019-03-15 stsp goto done;
1122 598a8b91 2019-03-15 stsp }
1123 598a8b91 2019-03-15 stsp
1124 598a8b91 2019-03-15 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
1125 598a8b91 2019-03-15 stsp uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1126 598a8b91 2019-03-15 stsp
1127 598a8b91 2019-03-15 stsp if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1128 598a8b91 2019-03-15 stsp sizeof(hex)) == NULL) {
1129 598a8b91 2019-03-15 stsp err = got_error(GOT_ERR_BAD_REF_DATA);
1130 598a8b91 2019-03-15 stsp goto done;
1131 598a8b91 2019-03-15 stsp }
1132 598a8b91 2019-03-15 stsp n = fprintf(tmpf, "%s ", hex);
1133 598a8b91 2019-03-15 stsp if (n != sizeof(hex)) {
1134 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_IO);
1135 598a8b91 2019-03-15 stsp goto done;
1136 598a8b91 2019-03-15 stsp }
1137 598a8b91 2019-03-15 stsp n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1138 598a8b91 2019-03-15 stsp if (n != strlen(re->ref->ref.ref.name) + 1) {
1139 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_IO);
1140 598a8b91 2019-03-15 stsp goto done;
1141 598a8b91 2019-03-15 stsp }
1142 598a8b91 2019-03-15 stsp }
1143 598a8b91 2019-03-15 stsp
1144 598a8b91 2019-03-15 stsp if (fflush(tmpf) != 0) {
1145 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1146 598a8b91 2019-03-15 stsp goto done;
1147 598a8b91 2019-03-15 stsp }
1148 598a8b91 2019-03-15 stsp
1149 598a8b91 2019-03-15 stsp if (stat(packed_refs_path, &sb) != 0) {
1150 598a8b91 2019-03-15 stsp if (errno != ENOENT) {
1151 638f9024 2019-05-13 stsp err = got_error_from_errno2("stat",
1152 230a42bd 2019-05-11 jcs packed_refs_path);
1153 598a8b91 2019-03-15 stsp goto done;
1154 598a8b91 2019-03-15 stsp }
1155 598a8b91 2019-03-15 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1156 598a8b91 2019-03-15 stsp }
1157 598a8b91 2019-03-15 stsp
1158 598a8b91 2019-03-15 stsp if (rename(tmppath, packed_refs_path) != 0) {
1159 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1160 230a42bd 2019-05-11 jcs packed_refs_path);
1161 598a8b91 2019-03-15 stsp goto done;
1162 598a8b91 2019-03-15 stsp }
1163 598a8b91 2019-03-15 stsp
1164 598a8b91 2019-03-15 stsp if (chmod(packed_refs_path, sb.st_mode) != 0) {
1165 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod",
1166 230a42bd 2019-05-11 jcs packed_refs_path);
1167 598a8b91 2019-03-15 stsp goto done;
1168 598a8b91 2019-03-15 stsp }
1169 598a8b91 2019-03-15 stsp }
1170 598a8b91 2019-03-15 stsp done:
1171 2f17228e 2019-05-12 stsp if (delref->lf == NULL && lf)
1172 598a8b91 2019-03-15 stsp unlock_err = got_lockfile_unlock(lf);
1173 598a8b91 2019-03-15 stsp if (f) {
1174 598a8b91 2019-03-15 stsp if (fclose(f) != 0 && err == NULL)
1175 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1176 598a8b91 2019-03-15 stsp }
1177 598a8b91 2019-03-15 stsp if (tmpf) {
1178 598a8b91 2019-03-15 stsp unlink(tmppath);
1179 598a8b91 2019-03-15 stsp if (fclose(tmpf) != 0 && err == NULL)
1180 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1181 598a8b91 2019-03-15 stsp }
1182 598a8b91 2019-03-15 stsp free(tmppath);
1183 598a8b91 2019-03-15 stsp free(packed_refs_path);
1184 598a8b91 2019-03-15 stsp got_ref_list_free(&refs);
1185 598a8b91 2019-03-15 stsp return err ? err : unlock_err;
1186 598a8b91 2019-03-15 stsp }
1187 598a8b91 2019-03-15 stsp
1188 2d2e1378 2019-03-11 stsp const struct got_error *
1189 2d2e1378 2019-03-11 stsp got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1190 2d2e1378 2019-03-11 stsp {
1191 2d2e1378 2019-03-11 stsp const struct got_error *err = NULL, *unlock_err = NULL;
1192 2d2e1378 2019-03-11 stsp const char *name = got_ref_get_name(ref);
1193 2d2e1378 2019-03-11 stsp char *path_refs = NULL, *path = NULL;
1194 2d2e1378 2019-03-11 stsp struct got_lockfile *lf = NULL;
1195 2d2e1378 2019-03-11 stsp
1196 598a8b91 2019-03-15 stsp if (ref->flags & GOT_REF_IS_PACKED)
1197 598a8b91 2019-03-15 stsp return delete_packed_ref(ref, repo);
1198 2d2e1378 2019-03-11 stsp
1199 2d2e1378 2019-03-11 stsp path_refs = get_refs_dir_path(repo, name);
1200 2d2e1378 2019-03-11 stsp if (path_refs == NULL) {
1201 638f9024 2019-05-13 stsp err = got_error_from_errno2("get_refs_dir_path", name);
1202 2d2e1378 2019-03-11 stsp goto done;
1203 2d2e1378 2019-03-11 stsp }
1204 2d2e1378 2019-03-11 stsp
1205 2d2e1378 2019-03-11 stsp if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1206 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1207 2d2e1378 2019-03-11 stsp goto done;
1208 9e672c74 2019-03-11 stsp }
1209 2d2e1378 2019-03-11 stsp
1210 2f17228e 2019-05-12 stsp if (ref->lf == NULL) {
1211 2f17228e 2019-05-12 stsp err = got_lockfile_lock(&lf, path);
1212 2f17228e 2019-05-12 stsp if (err)
1213 2f17228e 2019-05-12 stsp goto done;
1214 2f17228e 2019-05-12 stsp }
1215 2d2e1378 2019-03-11 stsp
1216 2d2e1378 2019-03-11 stsp /* XXX: check if old content matches our expectations? */
1217 2d2e1378 2019-03-11 stsp
1218 2d2e1378 2019-03-11 stsp if (unlink(path) != 0)
1219 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", path);
1220 2d2e1378 2019-03-11 stsp done:
1221 2f17228e 2019-05-12 stsp if (ref->lf == NULL && lf)
1222 2d2e1378 2019-03-11 stsp unlock_err = got_lockfile_unlock(lf);
1223 2d2e1378 2019-03-11 stsp
1224 2d2e1378 2019-03-11 stsp free(path_refs);
1225 2d2e1378 2019-03-11 stsp free(path);
1226 9e672c74 2019-03-11 stsp return err ? err : unlock_err;
1227 9e672c74 2019-03-11 stsp }
1228 2f17228e 2019-05-12 stsp
1229 2f17228e 2019-05-12 stsp const struct got_error *
1230 2f17228e 2019-05-12 stsp got_ref_unlock(struct got_reference *ref)
1231 2f17228e 2019-05-12 stsp {
1232 2f17228e 2019-05-12 stsp const struct got_error *err;
1233 2f17228e 2019-05-12 stsp err = got_lockfile_unlock(ref->lf);
1234 2f17228e 2019-05-12 stsp ref->lf = NULL;
1235 2f17228e 2019-05-12 stsp return err;
1236 2f17228e 2019-05-12 stsp }