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