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