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