Blame


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