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