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 f77a24b0 2019-03-11 stsp #include <ctype.h>
22 a04f49d2 2019-02-04 stsp #include <dirent.h>
23 5261c201 2018-04-01 stsp #include <sha1.h>
24 5261c201 2018-04-01 stsp #include <stdio.h>
25 5261c201 2018-04-01 stsp #include <stdlib.h>
26 5261c201 2018-04-01 stsp #include <string.h>
27 5261c201 2018-04-01 stsp #include <util.h>
28 5261c201 2018-04-01 stsp #include <zlib.h>
29 788c352e 2018-06-16 stsp #include <time.h>
30 5261c201 2018-04-01 stsp
31 5261c201 2018-04-01 stsp #include "got_error.h"
32 5261c201 2018-04-01 stsp #include "got_object.h"
33 5261c201 2018-04-01 stsp #include "got_repository.h"
34 5261c201 2018-04-01 stsp #include "got_reference.h"
35 9e672c74 2019-03-11 stsp #include "got_opentemp.h"
36 5261c201 2018-04-01 stsp
37 5261c201 2018-04-01 stsp #include "got_lib_sha1.h"
38 5261c201 2018-04-01 stsp #include "got_lib_path.h"
39 5261c201 2018-04-01 stsp #include "got_lib_delta.h"
40 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
41 5261c201 2018-04-01 stsp #include "got_lib_object.h"
42 f77a24b0 2019-03-11 stsp #include "got_lib_lockfile.h"
43 5261c201 2018-04-01 stsp
44 fb79db15 2019-02-01 stsp #ifndef nitems
45 fb79db15 2019-02-01 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
46 fb79db15 2019-02-01 stsp #endif
47 fb79db15 2019-02-01 stsp
48 d1f2edc9 2018-06-13 stsp #define GOT_REF_HEADS "heads"
49 d1f2edc9 2018-06-13 stsp #define GOT_REF_TAGS "tags"
50 d1f2edc9 2018-06-13 stsp #define GOT_REF_REMOTES "remotes"
51 d1f2edc9 2018-06-13 stsp
52 5261c201 2018-04-01 stsp /* A symbolic reference. */
53 5261c201 2018-04-01 stsp struct got_symref {
54 5261c201 2018-04-01 stsp char *name;
55 5261c201 2018-04-01 stsp char *ref;
56 5261c201 2018-04-01 stsp };
57 5261c201 2018-04-01 stsp
58 5261c201 2018-04-01 stsp /* A non-symbolic reference (there is no better designation). */
59 5261c201 2018-04-01 stsp struct got_ref {
60 5261c201 2018-04-01 stsp char *name;
61 5261c201 2018-04-01 stsp u_int8_t sha1[SHA1_DIGEST_LENGTH];
62 5261c201 2018-04-01 stsp };
63 5261c201 2018-04-01 stsp
64 5261c201 2018-04-01 stsp /* A reference which points to an arbitrary object. */
65 5261c201 2018-04-01 stsp struct got_reference {
66 5261c201 2018-04-01 stsp unsigned int flags;
67 5261c201 2018-04-01 stsp #define GOT_REF_IS_SYMBOLIC 0x01
68 5261c201 2018-04-01 stsp
69 5261c201 2018-04-01 stsp union {
70 5261c201 2018-04-01 stsp struct got_ref ref;
71 5261c201 2018-04-01 stsp struct got_symref symref;
72 5261c201 2018-04-01 stsp } ref;
73 5261c201 2018-04-01 stsp };
74 5261c201 2018-04-01 stsp
75 5261c201 2018-04-01 stsp static const struct got_error *
76 5261c201 2018-04-01 stsp parse_symref(struct got_reference **ref, const char *name, const char *line)
77 5261c201 2018-04-01 stsp {
78 5261c201 2018-04-01 stsp struct got_symref *symref;
79 5261c201 2018-04-01 stsp char *symref_name;
80 5261c201 2018-04-01 stsp char *symref_ref;
81 5261c201 2018-04-01 stsp
82 5261c201 2018-04-01 stsp if (line[0] == '\0')
83 30c0868d 2019-02-03 stsp return got_error(GOT_ERR_BAD_REF_DATA);
84 5261c201 2018-04-01 stsp
85 5261c201 2018-04-01 stsp symref_name = strdup(name);
86 5261c201 2018-04-01 stsp if (symref_name == NULL)
87 5261c201 2018-04-01 stsp return got_error_from_errno();
88 5261c201 2018-04-01 stsp symref_ref = strdup(line);
89 5261c201 2018-04-01 stsp if (symref_ref == NULL) {
90 5261c201 2018-04-01 stsp const struct got_error *err = got_error_from_errno();
91 5261c201 2018-04-01 stsp free(symref_name);
92 5261c201 2018-04-01 stsp return err;
93 5261c201 2018-04-01 stsp }
94 5261c201 2018-04-01 stsp
95 5261c201 2018-04-01 stsp *ref = calloc(1, sizeof(**ref));
96 5261c201 2018-04-01 stsp if (*ref == NULL)
97 5261c201 2018-04-01 stsp return got_error_from_errno();
98 5261c201 2018-04-01 stsp (*ref)->flags |= GOT_REF_IS_SYMBOLIC;
99 5261c201 2018-04-01 stsp symref = &((*ref)->ref.symref);
100 5261c201 2018-04-01 stsp symref->name = symref_name;
101 5261c201 2018-04-01 stsp symref->ref = symref_ref;
102 5261c201 2018-04-01 stsp return NULL;
103 5261c201 2018-04-01 stsp }
104 5261c201 2018-04-01 stsp
105 5261c201 2018-04-01 stsp static const struct got_error *
106 5261c201 2018-04-01 stsp parse_ref_line(struct got_reference **ref, const char *name, const char *line)
107 5261c201 2018-04-01 stsp {
108 5892cdd6 2019-03-10 stsp struct got_object_id id;
109 5261c201 2018-04-01 stsp
110 5261c201 2018-04-01 stsp if (strncmp(line, "ref: ", 5) == 0) {
111 5261c201 2018-04-01 stsp line += 5;
112 5261c201 2018-04-01 stsp return parse_symref(ref, name, line);
113 5261c201 2018-04-01 stsp }
114 5261c201 2018-04-01 stsp
115 5892cdd6 2019-03-10 stsp if (!got_parse_sha1_digest(id.sha1, line))
116 30c0868d 2019-02-03 stsp return got_error(GOT_ERR_BAD_REF_DATA);
117 5261c201 2018-04-01 stsp
118 5892cdd6 2019-03-10 stsp return got_ref_alloc(ref, name, &id);
119 5261c201 2018-04-01 stsp }
120 5261c201 2018-04-01 stsp
121 5261c201 2018-04-01 stsp static const struct got_error *
122 5261c201 2018-04-01 stsp parse_ref_file(struct got_reference **ref, const char *name,
123 5261c201 2018-04-01 stsp const char *abspath)
124 5261c201 2018-04-01 stsp {
125 5261c201 2018-04-01 stsp const struct got_error *err = NULL;
126 5261c201 2018-04-01 stsp FILE *f = fopen(abspath, "rb");
127 5261c201 2018-04-01 stsp char *line;
128 5261c201 2018-04-01 stsp size_t len;
129 5261c201 2018-04-01 stsp const char delim[3] = {'\0', '\0', '\0'};
130 5261c201 2018-04-01 stsp
131 5261c201 2018-04-01 stsp if (f == NULL)
132 1e37702e 2019-02-04 stsp return NULL;
133 5261c201 2018-04-01 stsp
134 5261c201 2018-04-01 stsp line = fparseln(f, &len, NULL, delim, 0);
135 5261c201 2018-04-01 stsp if (line == NULL) {
136 30c0868d 2019-02-03 stsp err = got_error(GOT_ERR_BAD_REF_DATA);
137 5261c201 2018-04-01 stsp goto done;
138 5261c201 2018-04-01 stsp }
139 5261c201 2018-04-01 stsp
140 5261c201 2018-04-01 stsp err = parse_ref_line(ref, name, line);
141 5261c201 2018-04-01 stsp done:
142 5261c201 2018-04-01 stsp free(line);
143 fb43ecf1 2019-02-11 stsp if (fclose(f) != 0 && err == NULL)
144 fb43ecf1 2019-02-11 stsp err = got_error_from_errno();
145 5261c201 2018-04-01 stsp return err;
146 5261c201 2018-04-01 stsp }
147 5261c201 2018-04-01 stsp
148 c5f754cc 2019-02-01 stsp static int
149 c5f754cc 2019-02-01 stsp is_well_known_ref(const char *refname)
150 5261c201 2018-04-01 stsp {
151 c5f754cc 2019-02-01 stsp return (strcmp(refname, GOT_REF_HEAD) == 0 ||
152 5261c201 2018-04-01 stsp strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
153 5261c201 2018-04-01 stsp strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
154 c5f754cc 2019-02-01 stsp strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
155 c5f754cc 2019-02-01 stsp }
156 c5f754cc 2019-02-01 stsp
157 c5f754cc 2019-02-01 stsp static char *
158 c5f754cc 2019-02-01 stsp get_refs_dir_path(struct got_repository *repo, const char *refname)
159 c5f754cc 2019-02-01 stsp {
160 c5f754cc 2019-02-01 stsp if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
161 6e9da951 2019-01-06 stsp return strdup(got_repo_get_path_git_dir(repo));
162 5261c201 2018-04-01 stsp
163 5261c201 2018-04-01 stsp return got_repo_get_path_refs(repo);
164 f77a24b0 2019-03-11 stsp }
165 f77a24b0 2019-03-11 stsp
166 f77a24b0 2019-03-11 stsp static int
167 f77a24b0 2019-03-11 stsp is_valid_ref_name(const char *name)
168 f77a24b0 2019-03-11 stsp {
169 f77a24b0 2019-03-11 stsp const char *s, *slash, *seg;
170 f77a24b0 2019-03-11 stsp const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
171 f77a24b0 2019-03-11 stsp const char *forbidden_seq[] = { "//", "..", "@{" };
172 f77a24b0 2019-03-11 stsp const char *lfs = GOT_LOCKFILE_SUFFIX;
173 f77a24b0 2019-03-11 stsp const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
174 f77a24b0 2019-03-11 stsp int i;
175 f77a24b0 2019-03-11 stsp
176 f77a24b0 2019-03-11 stsp if (name[0] == '@' && name[1] == '\0')
177 f77a24b0 2019-03-11 stsp return 0;
178 f77a24b0 2019-03-11 stsp
179 f77a24b0 2019-03-11 stsp slash = strchr(name, '/');
180 f77a24b0 2019-03-11 stsp if (slash == NULL)
181 f77a24b0 2019-03-11 stsp return 0;
182 f77a24b0 2019-03-11 stsp
183 f77a24b0 2019-03-11 stsp s = name;
184 f77a24b0 2019-03-11 stsp seg = s;
185 f77a24b0 2019-03-11 stsp if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
186 f77a24b0 2019-03-11 stsp return 0;
187 f77a24b0 2019-03-11 stsp while (*s) {
188 f77a24b0 2019-03-11 stsp for (i = 0; i < nitems(forbidden); i++) {
189 f77a24b0 2019-03-11 stsp if (*s == forbidden[i])
190 f77a24b0 2019-03-11 stsp return 0;
191 f77a24b0 2019-03-11 stsp }
192 f77a24b0 2019-03-11 stsp for (i = 0; i < nitems(forbidden_seq); i++) {
193 f77a24b0 2019-03-11 stsp if (s[0] == forbidden_seq[i][0] &&
194 f77a24b0 2019-03-11 stsp s[1] == forbidden_seq[i][1])
195 f77a24b0 2019-03-11 stsp return 0;
196 f77a24b0 2019-03-11 stsp }
197 f77a24b0 2019-03-11 stsp if (iscntrl((unsigned char)s[0]))
198 f77a24b0 2019-03-11 stsp return 0;
199 f77a24b0 2019-03-11 stsp if (s[0] == '.' && s[1] == '\0')
200 f77a24b0 2019-03-11 stsp return 0;
201 f77a24b0 2019-03-11 stsp if (*s == '/') {
202 f77a24b0 2019-03-11 stsp const char *nextseg = s + 1;
203 f77a24b0 2019-03-11 stsp if (nextseg[0] == '\0' || nextseg[0] == '.' ||
204 f77a24b0 2019-03-11 stsp nextseg[0] == '/')
205 f77a24b0 2019-03-11 stsp return 0;
206 f77a24b0 2019-03-11 stsp if (seg <= s - lfs_len &&
207 f77a24b0 2019-03-11 stsp strncmp(s - lfs_len, lfs, lfs_len) == 0)
208 f77a24b0 2019-03-11 stsp return 0;
209 f77a24b0 2019-03-11 stsp seg = nextseg;
210 f77a24b0 2019-03-11 stsp }
211 f77a24b0 2019-03-11 stsp s++;
212 f77a24b0 2019-03-11 stsp }
213 f77a24b0 2019-03-11 stsp
214 f77a24b0 2019-03-11 stsp if (seg <= s - lfs_len &&
215 f77a24b0 2019-03-11 stsp strncmp(s - lfs_len, lfs, lfs_len) == 0)
216 f77a24b0 2019-03-11 stsp return 0;
217 f77a24b0 2019-03-11 stsp
218 f77a24b0 2019-03-11 stsp return 1;
219 5892cdd6 2019-03-10 stsp }
220 5892cdd6 2019-03-10 stsp
221 5892cdd6 2019-03-10 stsp const struct got_error *
222 5892cdd6 2019-03-10 stsp got_ref_alloc(struct got_reference **ref, const char *name,
223 5892cdd6 2019-03-10 stsp struct got_object_id *id)
224 5892cdd6 2019-03-10 stsp {
225 5892cdd6 2019-03-10 stsp const struct got_error *err = NULL;
226 5892cdd6 2019-03-10 stsp
227 f77a24b0 2019-03-11 stsp if (!is_valid_ref_name(name))
228 f77a24b0 2019-03-11 stsp return got_error(GOT_ERR_BAD_REF_NAME);
229 f77a24b0 2019-03-11 stsp
230 5892cdd6 2019-03-10 stsp *ref = calloc(1, sizeof(**ref));
231 5892cdd6 2019-03-10 stsp if (*ref == NULL)
232 5892cdd6 2019-03-10 stsp return got_error_from_errno();
233 5892cdd6 2019-03-10 stsp
234 5892cdd6 2019-03-10 stsp memcpy(&(*ref)->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
235 5892cdd6 2019-03-10 stsp (*ref)->ref.ref.name = strdup(name);
236 5892cdd6 2019-03-10 stsp if ((*ref)->ref.ref.name == NULL) {
237 5892cdd6 2019-03-10 stsp err = got_error_from_errno();
238 5892cdd6 2019-03-10 stsp free(*ref);
239 5892cdd6 2019-03-10 stsp *ref = NULL;
240 5892cdd6 2019-03-10 stsp }
241 5892cdd6 2019-03-10 stsp return err;
242 5261c201 2018-04-01 stsp }
243 5261c201 2018-04-01 stsp
244 d1f2edc9 2018-06-13 stsp static const struct got_error *
245 fb79db15 2019-02-01 stsp parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
246 fb79db15 2019-02-01 stsp const char *line)
247 fb79db15 2019-02-01 stsp {
248 5892cdd6 2019-03-10 stsp struct got_object_id id;
249 5892cdd6 2019-03-10 stsp const char *name;
250 fb79db15 2019-02-01 stsp
251 fb79db15 2019-02-01 stsp *ref = NULL;
252 fb79db15 2019-02-01 stsp
253 532920c8 2019-02-01 stsp if (line[0] == '#' || line[0] == '^')
254 fb79db15 2019-02-01 stsp return NULL;
255 fb79db15 2019-02-01 stsp
256 5892cdd6 2019-03-10 stsp if (!got_parse_sha1_digest(id.sha1, line))
257 30c0868d 2019-02-03 stsp return got_error(GOT_ERR_BAD_REF_DATA);
258 fb79db15 2019-02-01 stsp
259 199a4027 2019-02-02 stsp if (abs_refname) {
260 199a4027 2019-02-02 stsp if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
261 199a4027 2019-02-02 stsp return NULL;
262 5892cdd6 2019-03-10 stsp name = abs_refname;
263 5892cdd6 2019-03-10 stsp } else
264 5892cdd6 2019-03-10 stsp name = line + SHA1_DIGEST_STRING_LENGTH;
265 fb79db15 2019-02-01 stsp
266 5892cdd6 2019-03-10 stsp return got_ref_alloc(ref, name, &id);
267 fb79db15 2019-02-01 stsp }
268 fb79db15 2019-02-01 stsp
269 fb79db15 2019-02-01 stsp static const struct got_error *
270 0dec1cc0 2019-02-01 stsp open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
271 0dec1cc0 2019-02-01 stsp int nsubdirs, const char *refname)
272 fb79db15 2019-02-01 stsp {
273 fb79db15 2019-02-01 stsp const struct got_error *err = NULL;
274 fb79db15 2019-02-01 stsp char *abs_refname;
275 fb79db15 2019-02-01 stsp char *line;
276 fb79db15 2019-02-01 stsp size_t len;
277 fb79db15 2019-02-01 stsp const char delim[3] = {'\0', '\0', '\0'};
278 0dec1cc0 2019-02-01 stsp int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
279 fb79db15 2019-02-01 stsp
280 1e37702e 2019-02-04 stsp *ref = NULL;
281 1e37702e 2019-02-04 stsp
282 0dec1cc0 2019-02-01 stsp if (ref_is_absolute)
283 0dec1cc0 2019-02-01 stsp abs_refname = (char *)refname;
284 fb79db15 2019-02-01 stsp do {
285 fb79db15 2019-02-01 stsp line = fparseln(f, &len, NULL, delim, 0);
286 1e37702e 2019-02-04 stsp if (line == NULL)
287 fb79db15 2019-02-01 stsp break;
288 0dec1cc0 2019-02-01 stsp for (i = 0; i < nsubdirs; i++) {
289 0dec1cc0 2019-02-01 stsp if (!ref_is_absolute &&
290 0dec1cc0 2019-02-01 stsp asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
291 0dec1cc0 2019-02-01 stsp refname) == -1)
292 0dec1cc0 2019-02-01 stsp return got_error_from_errno();
293 0dec1cc0 2019-02-01 stsp err = parse_packed_ref_line(ref, abs_refname, line);
294 0dec1cc0 2019-02-01 stsp if (!ref_is_absolute)
295 0dec1cc0 2019-02-01 stsp free(abs_refname);
296 532920c8 2019-02-01 stsp if (err || *ref != NULL)
297 0dec1cc0 2019-02-01 stsp break;
298 0dec1cc0 2019-02-01 stsp }
299 fb79db15 2019-02-01 stsp free(line);
300 fb79db15 2019-02-01 stsp if (err)
301 fb79db15 2019-02-01 stsp break;
302 fb79db15 2019-02-01 stsp } while (*ref == NULL);
303 fb79db15 2019-02-01 stsp
304 fb79db15 2019-02-01 stsp return err;
305 fb79db15 2019-02-01 stsp }
306 fb79db15 2019-02-01 stsp
307 fb79db15 2019-02-01 stsp static const struct got_error *
308 d1f2edc9 2018-06-13 stsp open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
309 a04f49d2 2019-02-04 stsp const char *name)
310 d1f2edc9 2018-06-13 stsp {
311 d1f2edc9 2018-06-13 stsp const struct got_error *err = NULL;
312 a04f49d2 2019-02-04 stsp char *path = NULL;
313 a04f49d2 2019-02-04 stsp char *normpath = NULL;
314 a04f49d2 2019-02-04 stsp char *absname = NULL;
315 a04f49d2 2019-02-04 stsp int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
316 a04f49d2 2019-02-04 stsp int ref_is_well_known = is_well_known_ref(name);
317 d1f2edc9 2018-06-13 stsp
318 1e37702e 2019-02-04 stsp *ref = NULL;
319 1e37702e 2019-02-04 stsp
320 a04f49d2 2019-02-04 stsp if (ref_is_absolute || ref_is_well_known) {
321 a04f49d2 2019-02-04 stsp if (asprintf(&path, "%s/%s", path_refs, name) == -1)
322 a04f49d2 2019-02-04 stsp return got_error_from_errno();
323 a04f49d2 2019-02-04 stsp absname = (char *)name;
324 a04f49d2 2019-02-04 stsp } else {
325 a04f49d2 2019-02-04 stsp if (asprintf(&path, "%s/%s/%s", path_refs, subdir, name) == -1)
326 a04f49d2 2019-02-04 stsp return got_error_from_errno();
327 d1f2edc9 2018-06-13 stsp
328 a04f49d2 2019-02-04 stsp if (asprintf(&absname, "refs/%s/%s", subdir, name) == -1) {
329 a04f49d2 2019-02-04 stsp err = got_error_from_errno();
330 a04f49d2 2019-02-04 stsp goto done;
331 a04f49d2 2019-02-04 stsp }
332 a04f49d2 2019-02-04 stsp }
333 a04f49d2 2019-02-04 stsp
334 a04f49d2 2019-02-04 stsp normpath = got_path_normalize(path);
335 d1f2edc9 2018-06-13 stsp if (normpath == NULL) {
336 30c0868d 2019-02-03 stsp err = got_error_from_errno();
337 d1f2edc9 2018-06-13 stsp goto done;
338 d1f2edc9 2018-06-13 stsp }
339 d1f2edc9 2018-06-13 stsp
340 a04f49d2 2019-02-04 stsp err = parse_ref_file(ref, absname, normpath);
341 d1f2edc9 2018-06-13 stsp done:
342 a04f49d2 2019-02-04 stsp if (!ref_is_absolute && !ref_is_well_known)
343 a04f49d2 2019-02-04 stsp free(absname);
344 a04f49d2 2019-02-04 stsp free(path);
345 d1f2edc9 2018-06-13 stsp free(normpath);
346 d1f2edc9 2018-06-13 stsp return err;
347 d1f2edc9 2018-06-13 stsp }
348 d1f2edc9 2018-06-13 stsp
349 5261c201 2018-04-01 stsp const struct got_error *
350 5261c201 2018-04-01 stsp got_ref_open(struct got_reference **ref, struct got_repository *repo,
351 5261c201 2018-04-01 stsp const char *refname)
352 5261c201 2018-04-01 stsp {
353 5261c201 2018-04-01 stsp const struct got_error *err = NULL;
354 c5f754cc 2019-02-01 stsp char *path_refs = NULL;
355 fb79db15 2019-02-01 stsp const char *subdirs[] = {
356 fb79db15 2019-02-01 stsp GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
357 fb79db15 2019-02-01 stsp };
358 c5f754cc 2019-02-01 stsp int i, well_known = is_well_known_ref(refname);
359 1e37702e 2019-02-04 stsp
360 1e37702e 2019-02-04 stsp *ref = NULL;
361 e135804e 2019-02-10 stsp
362 e135804e 2019-02-10 stsp path_refs = get_refs_dir_path(repo, refname);
363 e135804e 2019-02-10 stsp if (path_refs == NULL) {
364 e135804e 2019-02-10 stsp err = got_error_from_errno();
365 e135804e 2019-02-10 stsp goto done;
366 e135804e 2019-02-10 stsp }
367 5261c201 2018-04-01 stsp
368 c5f754cc 2019-02-01 stsp if (!well_known) {
369 c5f754cc 2019-02-01 stsp char *packed_refs_path;
370 c5f754cc 2019-02-01 stsp FILE *f;
371 c5f754cc 2019-02-01 stsp
372 e135804e 2019-02-10 stsp /* Search on-disk refs before packed refs! */
373 e135804e 2019-02-10 stsp for (i = 0; i < nitems(subdirs); i++) {
374 e135804e 2019-02-10 stsp err = open_ref(ref, path_refs, subdirs[i], refname);
375 e135804e 2019-02-10 stsp if (err || *ref)
376 e135804e 2019-02-10 stsp goto done;
377 e135804e 2019-02-10 stsp }
378 e135804e 2019-02-10 stsp
379 c5f754cc 2019-02-01 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
380 e135804e 2019-02-10 stsp if (packed_refs_path == NULL) {
381 e135804e 2019-02-10 stsp err = got_error_from_errno();
382 e135804e 2019-02-10 stsp goto done;
383 e135804e 2019-02-10 stsp }
384 c5f754cc 2019-02-01 stsp
385 c5f754cc 2019-02-01 stsp f = fopen(packed_refs_path, "rb");
386 c5f754cc 2019-02-01 stsp free(packed_refs_path);
387 c5f754cc 2019-02-01 stsp if (f != NULL) {
388 0dec1cc0 2019-02-01 stsp err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
389 0dec1cc0 2019-02-01 stsp refname);
390 fb43ecf1 2019-02-11 stsp if (fclose(f) != 0 && err == NULL)
391 fb43ecf1 2019-02-11 stsp err = got_error_from_errno();
392 1e37702e 2019-02-04 stsp if (err || *ref)
393 0dec1cc0 2019-02-01 stsp goto done;
394 fb79db15 2019-02-01 stsp }
395 fb79db15 2019-02-01 stsp }
396 fb79db15 2019-02-01 stsp
397 fb79db15 2019-02-01 stsp err = open_ref(ref, path_refs, "", refname);
398 1e37702e 2019-02-04 stsp if (err)
399 1e37702e 2019-02-04 stsp goto done;
400 e135804e 2019-02-10 stsp done:
401 1e37702e 2019-02-04 stsp if (*ref == NULL)
402 1e37702e 2019-02-04 stsp err = got_error_not_ref(refname);
403 5261c201 2018-04-01 stsp free(path_refs);
404 5261c201 2018-04-01 stsp return err;
405 5261c201 2018-04-01 stsp }
406 5261c201 2018-04-01 stsp
407 5261c201 2018-04-01 stsp void
408 5261c201 2018-04-01 stsp got_ref_close(struct got_reference *ref)
409 5261c201 2018-04-01 stsp {
410 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
411 5261c201 2018-04-01 stsp free(ref->ref.symref.name);
412 5261c201 2018-04-01 stsp else
413 5261c201 2018-04-01 stsp free(ref->ref.ref.name);
414 5261c201 2018-04-01 stsp free(ref);
415 5261c201 2018-04-01 stsp }
416 5261c201 2018-04-01 stsp
417 5261c201 2018-04-01 stsp struct got_reference *
418 5261c201 2018-04-01 stsp got_ref_dup(struct got_reference *ref)
419 5261c201 2018-04-01 stsp {
420 5261c201 2018-04-01 stsp struct got_reference *ret;
421 5261c201 2018-04-01 stsp
422 5261c201 2018-04-01 stsp ret = calloc(1, sizeof(*ret));
423 5261c201 2018-04-01 stsp if (ret == NULL)
424 5261c201 2018-04-01 stsp return NULL;
425 5261c201 2018-04-01 stsp
426 5261c201 2018-04-01 stsp ret->flags = ref->flags;
427 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
428 5261c201 2018-04-01 stsp ret->ref.symref.name = strdup(ref->ref.symref.name);
429 5261c201 2018-04-01 stsp if (ret->ref.symref.name == NULL) {
430 5261c201 2018-04-01 stsp free(ret);
431 5261c201 2018-04-01 stsp return NULL;
432 5261c201 2018-04-01 stsp }
433 5261c201 2018-04-01 stsp ret->ref.symref.ref = strdup(ref->ref.symref.ref);
434 5261c201 2018-04-01 stsp if (ret->ref.symref.ref == NULL) {
435 5261c201 2018-04-01 stsp free(ret->ref.symref.name);
436 5261c201 2018-04-01 stsp free(ret);
437 5261c201 2018-04-01 stsp return NULL;
438 5261c201 2018-04-01 stsp }
439 5261c201 2018-04-01 stsp } else {
440 5261c201 2018-04-01 stsp ref->ref.ref.name = strdup(ref->ref.ref.name);
441 5261c201 2018-04-01 stsp if (ref->ref.ref.name == NULL) {
442 5261c201 2018-04-01 stsp free(ret);
443 5261c201 2018-04-01 stsp return NULL;
444 5261c201 2018-04-01 stsp }
445 5261c201 2018-04-01 stsp memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
446 5261c201 2018-04-01 stsp SHA1_DIGEST_LENGTH);
447 5261c201 2018-04-01 stsp }
448 5261c201 2018-04-01 stsp
449 5261c201 2018-04-01 stsp return ret;
450 5261c201 2018-04-01 stsp }
451 5261c201 2018-04-01 stsp
452 5261c201 2018-04-01 stsp static const struct got_error *
453 5261c201 2018-04-01 stsp resolve_symbolic_ref(struct got_reference **resolved,
454 5261c201 2018-04-01 stsp struct got_repository *repo, struct got_reference *ref)
455 5261c201 2018-04-01 stsp {
456 5261c201 2018-04-01 stsp struct got_reference *nextref;
457 5261c201 2018-04-01 stsp const struct got_error *err;
458 5261c201 2018-04-01 stsp
459 5261c201 2018-04-01 stsp err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
460 5261c201 2018-04-01 stsp if (err)
461 5261c201 2018-04-01 stsp return err;
462 5261c201 2018-04-01 stsp
463 5261c201 2018-04-01 stsp if (nextref->flags & GOT_REF_IS_SYMBOLIC)
464 5261c201 2018-04-01 stsp err = resolve_symbolic_ref(resolved, repo, nextref);
465 5261c201 2018-04-01 stsp else
466 5261c201 2018-04-01 stsp *resolved = got_ref_dup(nextref);
467 5261c201 2018-04-01 stsp
468 5261c201 2018-04-01 stsp got_ref_close(nextref);
469 5261c201 2018-04-01 stsp return err;
470 5261c201 2018-04-01 stsp }
471 5261c201 2018-04-01 stsp
472 5261c201 2018-04-01 stsp const struct got_error *
473 5261c201 2018-04-01 stsp got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
474 5261c201 2018-04-01 stsp struct got_reference *ref)
475 5261c201 2018-04-01 stsp {
476 5261c201 2018-04-01 stsp const struct got_error *err;
477 5261c201 2018-04-01 stsp
478 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
479 5261c201 2018-04-01 stsp struct got_reference *resolved = NULL;
480 5261c201 2018-04-01 stsp err = resolve_symbolic_ref(&resolved, repo, ref);
481 5261c201 2018-04-01 stsp if (err == NULL)
482 5261c201 2018-04-01 stsp err = got_ref_resolve(id, repo, resolved);
483 5261c201 2018-04-01 stsp free(resolved);
484 5261c201 2018-04-01 stsp return err;
485 5261c201 2018-04-01 stsp }
486 5261c201 2018-04-01 stsp
487 5261c201 2018-04-01 stsp *id = calloc(1, sizeof(**id));
488 5261c201 2018-04-01 stsp if (*id == NULL)
489 5261c201 2018-04-01 stsp return got_error_from_errno();
490 5261c201 2018-04-01 stsp memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
491 5261c201 2018-04-01 stsp return NULL;
492 5261c201 2018-04-01 stsp }
493 5261c201 2018-04-01 stsp
494 5261c201 2018-04-01 stsp char *
495 5261c201 2018-04-01 stsp got_ref_to_str(struct got_reference *ref)
496 5261c201 2018-04-01 stsp {
497 5261c201 2018-04-01 stsp char *str;
498 271d2a38 2018-12-25 stsp
499 271d2a38 2018-12-25 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
500 271d2a38 2018-12-25 stsp return strdup(ref->ref.symref.ref);
501 271d2a38 2018-12-25 stsp
502 271d2a38 2018-12-25 stsp str = malloc(SHA1_DIGEST_STRING_LENGTH);
503 271d2a38 2018-12-25 stsp if (str == NULL)
504 271d2a38 2018-12-25 stsp return NULL;
505 271d2a38 2018-12-25 stsp
506 271d2a38 2018-12-25 stsp if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
507 271d2a38 2018-12-25 stsp SHA1_DIGEST_STRING_LENGTH) == NULL) {
508 271d2a38 2018-12-25 stsp free(str);
509 271d2a38 2018-12-25 stsp return NULL;
510 5261c201 2018-04-01 stsp }
511 5261c201 2018-04-01 stsp
512 5261c201 2018-04-01 stsp return str;
513 5261c201 2018-04-01 stsp }
514 0bd18d37 2019-02-01 stsp
515 0bd18d37 2019-02-01 stsp const char *
516 0bd18d37 2019-02-01 stsp got_ref_get_name(struct got_reference *ref)
517 0bd18d37 2019-02-01 stsp {
518 0bd18d37 2019-02-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
519 0bd18d37 2019-02-01 stsp return ref->ref.symref.name;
520 0bd18d37 2019-02-01 stsp
521 0bd18d37 2019-02-01 stsp return ref->ref.ref.name;
522 199a4027 2019-02-02 stsp }
523 199a4027 2019-02-02 stsp
524 199a4027 2019-02-02 stsp static const struct got_error *
525 29b5c214 2019-02-04 stsp insert_ref(struct got_reflist_head *refs, struct got_reference *ref,
526 199a4027 2019-02-02 stsp struct got_repository *repo)
527 199a4027 2019-02-02 stsp {
528 199a4027 2019-02-02 stsp const struct got_error *err;
529 199a4027 2019-02-02 stsp struct got_object_id *id;
530 7a3c76f5 2019-02-05 stsp struct got_reflist_entry *new, *re, *prev;
531 e397b6db 2019-02-04 stsp int cmp;
532 7a3c76f5 2019-02-05 stsp
533 7a3c76f5 2019-02-05 stsp err = got_ref_resolve(&id, repo, ref);
534 7a3c76f5 2019-02-05 stsp if (err)
535 7a3c76f5 2019-02-05 stsp return err;
536 29b5c214 2019-02-04 stsp
537 7a3c76f5 2019-02-05 stsp new = malloc(sizeof(*re));
538 7a3c76f5 2019-02-05 stsp if (new == NULL) {
539 7a3c76f5 2019-02-05 stsp free(id);
540 7a3c76f5 2019-02-05 stsp return got_error_from_errno();
541 7a3c76f5 2019-02-05 stsp }
542 7a3c76f5 2019-02-05 stsp new->ref = ref;
543 7a3c76f5 2019-02-05 stsp new->id = id;
544 7a3c76f5 2019-02-05 stsp
545 29b5c214 2019-02-04 stsp /*
546 29b5c214 2019-02-04 stsp * We must de-duplicate entries on insert because packed-refs may
547 29b5c214 2019-02-04 stsp * contain redundant entries. On-disk refs take precedence.
548 29b5c214 2019-02-04 stsp * This code assumes that on-disk revs are read before packed-refs.
549 e397b6db 2019-02-04 stsp * We're iterating the list anyway, so insert elements sorted by name.
550 29b5c214 2019-02-04 stsp */
551 7a3c76f5 2019-02-05 stsp re = SIMPLEQ_FIRST(refs);
552 7a3c76f5 2019-02-05 stsp while (re) {
553 7a3c76f5 2019-02-05 stsp cmp = got_path_cmp(got_ref_get_name(re->ref),
554 7a3c76f5 2019-02-05 stsp got_ref_get_name(ref));
555 e397b6db 2019-02-04 stsp if (cmp == 0) {
556 7a3c76f5 2019-02-05 stsp free(ref); /* duplicate */
557 7a3c76f5 2019-02-05 stsp return NULL;
558 7a3c76f5 2019-02-05 stsp } else if (cmp > 0) {
559 7a3c76f5 2019-02-05 stsp if (prev)
560 7a3c76f5 2019-02-05 stsp SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
561 7a3c76f5 2019-02-05 stsp else
562 7a3c76f5 2019-02-05 stsp SIMPLEQ_INSERT_HEAD(refs, new, entry);
563 7a3c76f5 2019-02-05 stsp return NULL;
564 7a3c76f5 2019-02-05 stsp } else {
565 e397b6db 2019-02-04 stsp prev = re;
566 7a3c76f5 2019-02-05 stsp re = SIMPLEQ_NEXT(re, entry);
567 7a3c76f5 2019-02-05 stsp }
568 29b5c214 2019-02-04 stsp }
569 199a4027 2019-02-02 stsp
570 7a3c76f5 2019-02-05 stsp SIMPLEQ_INSERT_TAIL(refs, new, entry);
571 199a4027 2019-02-02 stsp return NULL;
572 0bd18d37 2019-02-01 stsp }
573 199a4027 2019-02-02 stsp
574 a04f49d2 2019-02-04 stsp static const struct got_error *
575 29b5c214 2019-02-04 stsp gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
576 a04f49d2 2019-02-04 stsp const char *subdir, struct got_repository *repo)
577 a04f49d2 2019-02-04 stsp {
578 a04f49d2 2019-02-04 stsp const struct got_error *err = NULL;
579 a04f49d2 2019-02-04 stsp DIR *d = NULL;
580 a04f49d2 2019-02-04 stsp char *path_subdir;
581 a04f49d2 2019-02-04 stsp
582 a04f49d2 2019-02-04 stsp if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
583 a04f49d2 2019-02-04 stsp return got_error_from_errno();
584 a04f49d2 2019-02-04 stsp
585 a04f49d2 2019-02-04 stsp d = opendir(path_subdir);
586 a04f49d2 2019-02-04 stsp if (d == NULL)
587 a04f49d2 2019-02-04 stsp goto done;
588 a04f49d2 2019-02-04 stsp
589 a04f49d2 2019-02-04 stsp while (1) {
590 a04f49d2 2019-02-04 stsp struct dirent *dent;
591 a04f49d2 2019-02-04 stsp struct got_reference *ref;
592 a04f49d2 2019-02-04 stsp char *child;
593 a04f49d2 2019-02-04 stsp
594 a04f49d2 2019-02-04 stsp dent = readdir(d);
595 a04f49d2 2019-02-04 stsp if (dent == NULL)
596 a04f49d2 2019-02-04 stsp break;
597 a04f49d2 2019-02-04 stsp
598 a04f49d2 2019-02-04 stsp if (strcmp(dent->d_name, ".") == 0 ||
599 a04f49d2 2019-02-04 stsp strcmp(dent->d_name, "..") == 0)
600 a04f49d2 2019-02-04 stsp continue;
601 a04f49d2 2019-02-04 stsp
602 a04f49d2 2019-02-04 stsp switch (dent->d_type) {
603 a04f49d2 2019-02-04 stsp case DT_REG:
604 a04f49d2 2019-02-04 stsp err = open_ref(&ref, path_refs, subdir, dent->d_name);
605 1e37702e 2019-02-04 stsp if (err)
606 a04f49d2 2019-02-04 stsp goto done;
607 a04f49d2 2019-02-04 stsp if (ref) {
608 29b5c214 2019-02-04 stsp err = insert_ref(refs, ref, repo);
609 a04f49d2 2019-02-04 stsp if (err)
610 a04f49d2 2019-02-04 stsp goto done;
611 a04f49d2 2019-02-04 stsp }
612 a04f49d2 2019-02-04 stsp break;
613 a04f49d2 2019-02-04 stsp case DT_DIR:
614 a04f49d2 2019-02-04 stsp if (asprintf(&child, "%s%s%s", subdir,
615 a04f49d2 2019-02-04 stsp subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
616 a04f49d2 2019-02-04 stsp err = got_error_from_errno();
617 a04f49d2 2019-02-04 stsp break;
618 a04f49d2 2019-02-04 stsp }
619 29b5c214 2019-02-04 stsp err = gather_on_disk_refs(refs, path_refs, child, repo);
620 a04f49d2 2019-02-04 stsp free(child);
621 a04f49d2 2019-02-04 stsp break;
622 a04f49d2 2019-02-04 stsp default:
623 a04f49d2 2019-02-04 stsp break;
624 a04f49d2 2019-02-04 stsp }
625 a04f49d2 2019-02-04 stsp }
626 a04f49d2 2019-02-04 stsp done:
627 a04f49d2 2019-02-04 stsp if (d)
628 a04f49d2 2019-02-04 stsp closedir(d);
629 a04f49d2 2019-02-04 stsp free(path_subdir);
630 a04f49d2 2019-02-04 stsp return err;
631 a04f49d2 2019-02-04 stsp }
632 a04f49d2 2019-02-04 stsp
633 199a4027 2019-02-02 stsp const struct got_error *
634 199a4027 2019-02-02 stsp got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
635 199a4027 2019-02-02 stsp {
636 199a4027 2019-02-02 stsp const struct got_error *err;
637 a04f49d2 2019-02-04 stsp char *packed_refs_path, *path_refs = NULL;
638 29b5c214 2019-02-04 stsp FILE *f = NULL;
639 199a4027 2019-02-02 stsp struct got_reference *ref;
640 199a4027 2019-02-02 stsp
641 29b5c214 2019-02-04 stsp /* HEAD ref should always exist. */
642 29b5c214 2019-02-04 stsp path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
643 29b5c214 2019-02-04 stsp if (path_refs == NULL) {
644 29b5c214 2019-02-04 stsp err = got_error_from_errno();
645 29b5c214 2019-02-04 stsp goto done;
646 29b5c214 2019-02-04 stsp }
647 29b5c214 2019-02-04 stsp err = open_ref(&ref, path_refs, "", GOT_REF_HEAD);
648 29b5c214 2019-02-04 stsp if (err)
649 29b5c214 2019-02-04 stsp goto done;
650 29b5c214 2019-02-04 stsp err = insert_ref(refs, ref, repo);
651 29b5c214 2019-02-04 stsp if (err)
652 29b5c214 2019-02-04 stsp goto done;
653 29b5c214 2019-02-04 stsp
654 29b5c214 2019-02-04 stsp /* Gather on-disk refs before parsing packed-refs. */
655 29b5c214 2019-02-04 stsp free(path_refs);
656 29b5c214 2019-02-04 stsp path_refs = get_refs_dir_path(repo, "");
657 29b5c214 2019-02-04 stsp if (path_refs == NULL) {
658 29b5c214 2019-02-04 stsp err = got_error_from_errno();
659 29b5c214 2019-02-04 stsp goto done;
660 29b5c214 2019-02-04 stsp }
661 29b5c214 2019-02-04 stsp err = gather_on_disk_refs(refs, path_refs, "", repo);
662 29b5c214 2019-02-04 stsp if (err)
663 29b5c214 2019-02-04 stsp goto done;
664 29b5c214 2019-02-04 stsp
665 29b5c214 2019-02-04 stsp /*
666 29b5c214 2019-02-04 stsp * The packed-refs file may contain redundant entries, in which
667 29b5c214 2019-02-04 stsp * case on-disk refs take precedence.
668 29b5c214 2019-02-04 stsp */
669 199a4027 2019-02-02 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
670 29b5c214 2019-02-04 stsp if (packed_refs_path == NULL) {
671 29b5c214 2019-02-04 stsp err = got_error_from_errno();
672 29b5c214 2019-02-04 stsp goto done;
673 29b5c214 2019-02-04 stsp }
674 199a4027 2019-02-02 stsp
675 199a4027 2019-02-02 stsp f = fopen(packed_refs_path, "r");
676 199a4027 2019-02-02 stsp free(packed_refs_path);
677 199a4027 2019-02-02 stsp if (f) {
678 199a4027 2019-02-02 stsp char *line;
679 199a4027 2019-02-02 stsp size_t len;
680 199a4027 2019-02-02 stsp const char delim[3] = {'\0', '\0', '\0'};
681 199a4027 2019-02-02 stsp while (1) {
682 199a4027 2019-02-02 stsp line = fparseln(f, &len, NULL, delim, 0);
683 199a4027 2019-02-02 stsp if (line == NULL)
684 199a4027 2019-02-02 stsp break;
685 199a4027 2019-02-02 stsp err = parse_packed_ref_line(&ref, NULL, line);
686 199a4027 2019-02-02 stsp if (err)
687 199a4027 2019-02-02 stsp goto done;
688 76b4ead2 2019-02-03 stsp if (ref) {
689 29b5c214 2019-02-04 stsp err = insert_ref(refs, ref, repo);
690 76b4ead2 2019-02-03 stsp if (err)
691 76b4ead2 2019-02-03 stsp goto done;
692 76b4ead2 2019-02-03 stsp }
693 199a4027 2019-02-02 stsp }
694 199a4027 2019-02-02 stsp }
695 199a4027 2019-02-02 stsp done:
696 a04f49d2 2019-02-04 stsp free(path_refs);
697 fb43ecf1 2019-02-11 stsp if (f && fclose(f) != 0 && err == NULL)
698 fb43ecf1 2019-02-11 stsp err = got_error_from_errno();
699 199a4027 2019-02-02 stsp return err;
700 199a4027 2019-02-02 stsp }
701 9e672c74 2019-03-11 stsp
702 9e672c74 2019-03-11 stsp const struct got_error *
703 9e672c74 2019-03-11 stsp got_ref_write(struct got_reference *ref, struct got_repository *repo)
704 9e672c74 2019-03-11 stsp {
705 9e672c74 2019-03-11 stsp const struct got_error *err = NULL, *unlock_err = NULL;
706 9e672c74 2019-03-11 stsp const char *name = got_ref_get_name(ref);
707 9e672c74 2019-03-11 stsp char *path_refs = NULL, *path = NULL, *tmppath = NULL;
708 9e672c74 2019-03-11 stsp struct got_lockfile *lf = NULL;
709 9e672c74 2019-03-11 stsp FILE *f = NULL;
710 9e672c74 2019-03-11 stsp size_t n;
711 9e672c74 2019-03-11 stsp struct stat sb;
712 9e672c74 2019-03-11 stsp
713 9e672c74 2019-03-11 stsp path_refs = get_refs_dir_path(repo, name);
714 9e672c74 2019-03-11 stsp if (path_refs == NULL) {
715 9e672c74 2019-03-11 stsp err = got_error_from_errno();
716 9e672c74 2019-03-11 stsp goto done;
717 9e672c74 2019-03-11 stsp }
718 9e672c74 2019-03-11 stsp
719 9e672c74 2019-03-11 stsp if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
720 9e672c74 2019-03-11 stsp err = got_error_from_errno();
721 9e672c74 2019-03-11 stsp goto done;
722 9e672c74 2019-03-11 stsp }
723 9e672c74 2019-03-11 stsp
724 9e672c74 2019-03-11 stsp err = got_opentemp_named(&tmppath, &f, path);
725 9e672c74 2019-03-11 stsp if (f == NULL) {
726 9e672c74 2019-03-11 stsp err = got_error_from_errno();
727 9e672c74 2019-03-11 stsp goto done;
728 9e672c74 2019-03-11 stsp }
729 9e672c74 2019-03-11 stsp
730 9e672c74 2019-03-11 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
731 9e672c74 2019-03-11 stsp n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
732 9e672c74 2019-03-11 stsp if (n != strlen(ref->ref.symref.ref) + 6) {
733 9e672c74 2019-03-11 stsp err = got_ferror(f, GOT_ERR_IO);
734 9e672c74 2019-03-11 stsp goto done;
735 9e672c74 2019-03-11 stsp }
736 9e672c74 2019-03-11 stsp } else {
737 9e672c74 2019-03-11 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
738 9e672c74 2019-03-11 stsp if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
739 9e672c74 2019-03-11 stsp sizeof(hex)) == NULL) {
740 9e672c74 2019-03-11 stsp err = got_error(GOT_ERR_BAD_REF_DATA);
741 9e672c74 2019-03-11 stsp goto done;
742 9e672c74 2019-03-11 stsp }
743 9e672c74 2019-03-11 stsp n = fprintf(f, "%s\n", hex);
744 9e672c74 2019-03-11 stsp if (n != sizeof(hex) + 1) {
745 9e672c74 2019-03-11 stsp err = got_ferror(f, GOT_ERR_IO);
746 9e672c74 2019-03-11 stsp goto done;
747 9e672c74 2019-03-11 stsp }
748 9e672c74 2019-03-11 stsp }
749 9e672c74 2019-03-11 stsp
750 9e672c74 2019-03-11 stsp err = got_lockfile_lock(&lf, path);
751 9e672c74 2019-03-11 stsp if (err)
752 9e672c74 2019-03-11 stsp goto done;
753 9e672c74 2019-03-11 stsp
754 9e672c74 2019-03-11 stsp /* XXX: check if old content matches our expectations? */
755 9e672c74 2019-03-11 stsp
756 9e672c74 2019-03-11 stsp if (stat(path, &sb) != 0) {
757 9e672c74 2019-03-11 stsp err = got_error_from_errno();
758 9e672c74 2019-03-11 stsp goto done;
759 9e672c74 2019-03-11 stsp }
760 9e672c74 2019-03-11 stsp
761 9e672c74 2019-03-11 stsp if (rename(tmppath, path) != 0) {
762 9e672c74 2019-03-11 stsp err = got_error_from_errno();
763 9e672c74 2019-03-11 stsp goto done;
764 9e672c74 2019-03-11 stsp }
765 9e672c74 2019-03-11 stsp free(tmppath);
766 9e672c74 2019-03-11 stsp tmppath = NULL;
767 9e672c74 2019-03-11 stsp
768 9e672c74 2019-03-11 stsp if (chmod(path, sb.st_mode) != 0) {
769 9e672c74 2019-03-11 stsp err = got_error_from_errno();
770 9e672c74 2019-03-11 stsp goto done;
771 9e672c74 2019-03-11 stsp }
772 9e672c74 2019-03-11 stsp done:
773 9e672c74 2019-03-11 stsp if (lf)
774 9e672c74 2019-03-11 stsp unlock_err = got_lockfile_unlock(lf);
775 9e672c74 2019-03-11 stsp if (f) {
776 9e672c74 2019-03-11 stsp if (fclose(f) != 0 && err == NULL)
777 9e672c74 2019-03-11 stsp err = got_error_from_errno();
778 9e672c74 2019-03-11 stsp }
779 9e672c74 2019-03-11 stsp free(path_refs);
780 9e672c74 2019-03-11 stsp free(path);
781 9e672c74 2019-03-11 stsp if (tmppath) {
782 9e672c74 2019-03-11 stsp if (unlink(tmppath) != 0 && err == NULL)
783 9e672c74 2019-03-11 stsp err = got_error_from_errno();
784 9e672c74 2019-03-11 stsp free(tmppath);
785 9e672c74 2019-03-11 stsp }
786 9e672c74 2019-03-11 stsp return err ? err : unlock_err;
787 9e672c74 2019-03-11 stsp }