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