2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
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.
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.
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>
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>
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"
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"
46 fb79db15 2019-02-01 stsp #ifndef nitems
47 fb79db15 2019-02-01 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
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"
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.
59 598a8b91 2019-03-15 stsp #define GOT_PACKED_REFS_HEADER "# pack-refs with:"
61 5261c201 2018-04-01 stsp /* A symbolic reference. */
62 5261c201 2018-04-01 stsp struct got_symref {
67 5261c201 2018-04-01 stsp /* A non-symbolic reference (there is no better designation). */
68 5261c201 2018-04-01 stsp struct got_ref {
70 5261c201 2018-04-01 stsp u_int8_t sha1[SHA1_DIGEST_LENGTH];
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
80 5261c201 2018-04-01 stsp struct got_ref ref;
81 5261c201 2018-04-01 stsp struct got_symref symref;
84 2f17228e 2019-05-12 stsp struct got_lockfile *lf;
87 5261c201 2018-04-01 stsp static const struct got_error *
88 f9267c9a 2019-03-15 stsp alloc_ref(struct got_reference **ref, const char *name,
89 f9267c9a 2019-03-15 stsp struct got_object_id *id, int flags)
91 f9267c9a 2019-03-15 stsp const struct got_error *err = NULL;
93 f9267c9a 2019-03-15 stsp *ref = calloc(1, sizeof(**ref));
94 f9267c9a 2019-03-15 stsp if (*ref == NULL)
95 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
97 80d5fc1f 2019-03-15 stsp memcpy((*ref)->ref.ref.sha1, id->sha1, sizeof((*ref)->ref.ref.sha1));
98 c980e470 2019-03-15 stsp (*ref)->flags = flags;
99 f9267c9a 2019-03-15 stsp (*ref)->ref.ref.name = strdup(name);
100 f9267c9a 2019-03-15 stsp if ((*ref)->ref.ref.name == NULL) {
101 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
102 c980e470 2019-03-15 stsp got_ref_close(*ref);
103 f9267c9a 2019-03-15 stsp *ref = NULL;
105 f9267c9a 2019-03-15 stsp return err;
108 f9267c9a 2019-03-15 stsp static const struct got_error *
109 f9267c9a 2019-03-15 stsp alloc_symref(struct got_reference **ref, const char *name,
110 f9267c9a 2019-03-15 stsp const char *target_ref, int flags)
112 f9267c9a 2019-03-15 stsp const struct got_error *err = NULL;
114 5261c201 2018-04-01 stsp *ref = calloc(1, sizeof(**ref));
115 5261c201 2018-04-01 stsp if (*ref == NULL)
116 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
118 f9267c9a 2019-03-15 stsp (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
119 f9267c9a 2019-03-15 stsp (*ref)->ref.symref.name = strdup(name);
120 f9267c9a 2019-03-15 stsp if ((*ref)->ref.symref.name == NULL) {
121 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
122 f9267c9a 2019-03-15 stsp got_ref_close(*ref);
123 f9267c9a 2019-03-15 stsp *ref = NULL;
125 f9267c9a 2019-03-15 stsp (*ref)->ref.symref.ref = strdup(target_ref);
126 f9267c9a 2019-03-15 stsp if ((*ref)->ref.symref.ref == NULL) {
127 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
128 f9267c9a 2019-03-15 stsp got_ref_close(*ref);
129 f9267c9a 2019-03-15 stsp *ref = NULL;
131 f9267c9a 2019-03-15 stsp return err;
134 5261c201 2018-04-01 stsp static const struct got_error *
135 f9267c9a 2019-03-15 stsp parse_symref(struct got_reference **ref, const char *name, const char *line)
137 f9267c9a 2019-03-15 stsp if (line[0] == '\0')
138 f9267c9a 2019-03-15 stsp return got_error(GOT_ERR_BAD_REF_DATA);
140 f9267c9a 2019-03-15 stsp return alloc_symref(ref, name, line, 0);
143 f9267c9a 2019-03-15 stsp static const struct got_error *
144 5261c201 2018-04-01 stsp parse_ref_line(struct got_reference **ref, const char *name, const char *line)
146 5892cdd6 2019-03-10 stsp struct got_object_id id;
148 5261c201 2018-04-01 stsp if (strncmp(line, "ref: ", 5) == 0) {
150 5261c201 2018-04-01 stsp return parse_symref(ref, name, line);
153 5892cdd6 2019-03-10 stsp if (!got_parse_sha1_digest(id.sha1, line))
154 30c0868d 2019-02-03 stsp return got_error(GOT_ERR_BAD_REF_DATA);
156 f9267c9a 2019-03-15 stsp return alloc_ref(ref, name, &id, 0);
159 5261c201 2018-04-01 stsp static const struct got_error *
160 5261c201 2018-04-01 stsp parse_ref_file(struct got_reference **ref, const char *name,
161 2f17228e 2019-05-12 stsp const char *abspath, int lock)
163 5261c201 2018-04-01 stsp const struct got_error *err = NULL;
165 5261c201 2018-04-01 stsp char *line;
166 5261c201 2018-04-01 stsp size_t len;
167 5261c201 2018-04-01 stsp const char delim[3] = {'\0', '\0', '\0'};
168 2f17228e 2019-05-12 stsp struct got_lockfile *lf = NULL;
170 2f17228e 2019-05-12 stsp if (lock) {
171 2f17228e 2019-05-12 stsp err = got_lockfile_lock(&lf, abspath);
173 2f17228e 2019-05-12 stsp return (err);
176 c0a1c016 2019-03-15 stsp f = fopen(abspath, "rb");
177 f5c58ad1 2019-05-12 stsp if (f == NULL) {
179 f5c58ad1 2019-05-12 stsp got_lockfile_unlock(lf);
180 1e37702e 2019-02-04 stsp return NULL;
183 5261c201 2018-04-01 stsp line = fparseln(f, &len, NULL, delim, 0);
184 5261c201 2018-04-01 stsp if (line == NULL) {
185 30c0868d 2019-02-03 stsp err = got_error(GOT_ERR_BAD_REF_DATA);
187 f5c58ad1 2019-05-12 stsp got_lockfile_unlock(lf);
191 5261c201 2018-04-01 stsp err = parse_ref_line(ref, name, line);
192 f5c58ad1 2019-05-12 stsp if (lock) {
194 f5c58ad1 2019-05-12 stsp got_lockfile_unlock(lf);
197 f5c58ad1 2019-05-12 stsp (*ref)->lf = lf;
199 f5c58ad1 2019-05-12 stsp got_lockfile_unlock(lf);
203 5261c201 2018-04-01 stsp free(line);
204 2f17228e 2019-05-12 stsp if (fclose(f) != 0 && err == NULL) {
205 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
206 2f17228e 2019-05-12 stsp if (*ref) {
208 f5c58ad1 2019-05-12 stsp got_ref_unlock(*ref);
209 2f17228e 2019-05-12 stsp got_ref_close(*ref);
210 2f17228e 2019-05-12 stsp *ref = NULL;
213 5261c201 2018-04-01 stsp return err;
217 c5f754cc 2019-02-01 stsp is_well_known_ref(const char *refname)
219 c5f754cc 2019-02-01 stsp return (strcmp(refname, GOT_REF_HEAD) == 0 ||
220 5261c201 2018-04-01 stsp strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
221 5261c201 2018-04-01 stsp strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
222 c5f754cc 2019-02-01 stsp strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
225 c5f754cc 2019-02-01 stsp static char *
226 c5f754cc 2019-02-01 stsp get_refs_dir_path(struct got_repository *repo, const char *refname)
228 c5f754cc 2019-02-01 stsp if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
229 6e9da951 2019-01-06 stsp return strdup(got_repo_get_path_git_dir(repo));
231 5261c201 2018-04-01 stsp return got_repo_get_path_refs(repo);
235 f77a24b0 2019-03-11 stsp is_valid_ref_name(const char *name)
237 f77a24b0 2019-03-11 stsp const char *s, *slash, *seg;
238 f77a24b0 2019-03-11 stsp const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
239 f77a24b0 2019-03-11 stsp const char *forbidden_seq[] = { "//", "..", "@{" };
240 f77a24b0 2019-03-11 stsp const char *lfs = GOT_LOCKFILE_SUFFIX;
241 f77a24b0 2019-03-11 stsp const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
244 f77a24b0 2019-03-11 stsp if (name[0] == '@' && name[1] == '\0')
247 f77a24b0 2019-03-11 stsp slash = strchr(name, '/');
248 f77a24b0 2019-03-11 stsp if (slash == NULL)
253 f77a24b0 2019-03-11 stsp if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
255 f77a24b0 2019-03-11 stsp while (*s) {
256 f77a24b0 2019-03-11 stsp for (i = 0; i < nitems(forbidden); i++) {
257 f77a24b0 2019-03-11 stsp if (*s == forbidden[i])
260 f77a24b0 2019-03-11 stsp for (i = 0; i < nitems(forbidden_seq); i++) {
261 f77a24b0 2019-03-11 stsp if (s[0] == forbidden_seq[i][0] &&
262 f77a24b0 2019-03-11 stsp s[1] == forbidden_seq[i][1])
265 f77a24b0 2019-03-11 stsp if (iscntrl((unsigned char)s[0]))
267 f77a24b0 2019-03-11 stsp if (s[0] == '.' && s[1] == '\0')
269 f77a24b0 2019-03-11 stsp if (*s == '/') {
270 f77a24b0 2019-03-11 stsp const char *nextseg = s + 1;
271 f77a24b0 2019-03-11 stsp if (nextseg[0] == '\0' || nextseg[0] == '.' ||
272 f77a24b0 2019-03-11 stsp nextseg[0] == '/')
274 f77a24b0 2019-03-11 stsp if (seg <= s - lfs_len &&
275 f77a24b0 2019-03-11 stsp strncmp(s - lfs_len, lfs, lfs_len) == 0)
277 f77a24b0 2019-03-11 stsp seg = nextseg;
282 f77a24b0 2019-03-11 stsp if (seg <= s - lfs_len &&
283 f77a24b0 2019-03-11 stsp strncmp(s - lfs_len, lfs, lfs_len) == 0)
289 5892cdd6 2019-03-10 stsp const struct got_error *
290 5892cdd6 2019-03-10 stsp got_ref_alloc(struct got_reference **ref, const char *name,
291 5892cdd6 2019-03-10 stsp struct got_object_id *id)
293 0f148cb7 2019-05-13 stsp if (!is_valid_ref_name(name))
294 0f148cb7 2019-05-13 stsp return got_error(GOT_ERR_BAD_REF_NAME);
296 0f148cb7 2019-05-13 stsp return alloc_ref(ref, name, id, 0);
299 aaf88317 2019-07-10 stsp const struct got_error *
300 aaf88317 2019-07-10 stsp got_ref_alloc_symref(struct got_reference **ref, const char *name,
301 aaf88317 2019-07-10 stsp struct got_reference *target_ref)
303 aaf88317 2019-07-10 stsp if (!is_valid_ref_name(name))
304 aaf88317 2019-07-10 stsp return got_error(GOT_ERR_BAD_REF_NAME);
306 aaf88317 2019-07-10 stsp return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
309 d1f2edc9 2018-06-13 stsp static const struct got_error *
310 fb79db15 2019-02-01 stsp parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
311 fb79db15 2019-02-01 stsp const char *line)
313 5892cdd6 2019-03-10 stsp struct got_object_id id;
314 5892cdd6 2019-03-10 stsp const char *name;
316 fb79db15 2019-02-01 stsp *ref = NULL;
318 532920c8 2019-02-01 stsp if (line[0] == '#' || line[0] == '^')
319 fb79db15 2019-02-01 stsp return NULL;
321 5892cdd6 2019-03-10 stsp if (!got_parse_sha1_digest(id.sha1, line))
322 30c0868d 2019-02-03 stsp return got_error(GOT_ERR_BAD_REF_DATA);
324 199a4027 2019-02-02 stsp if (abs_refname) {
325 199a4027 2019-02-02 stsp if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
326 199a4027 2019-02-02 stsp return NULL;
327 5892cdd6 2019-03-10 stsp name = abs_refname;
329 5892cdd6 2019-03-10 stsp name = line + SHA1_DIGEST_STRING_LENGTH;
331 f9267c9a 2019-03-15 stsp return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
334 fb79db15 2019-02-01 stsp static const struct got_error *
335 0dec1cc0 2019-02-01 stsp open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
336 0dec1cc0 2019-02-01 stsp int nsubdirs, const char *refname)
338 fb79db15 2019-02-01 stsp const struct got_error *err = NULL;
339 fb79db15 2019-02-01 stsp char *abs_refname;
340 fb79db15 2019-02-01 stsp char *line;
341 fb79db15 2019-02-01 stsp size_t len;
342 fb79db15 2019-02-01 stsp const char delim[3] = {'\0', '\0', '\0'};
343 0dec1cc0 2019-02-01 stsp int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
345 1e37702e 2019-02-04 stsp *ref = NULL;
347 0dec1cc0 2019-02-01 stsp if (ref_is_absolute)
348 0dec1cc0 2019-02-01 stsp abs_refname = (char *)refname;
350 fb79db15 2019-02-01 stsp line = fparseln(f, &len, NULL, delim, 0);
351 7ab0422a 2019-03-15 stsp if (line == NULL) {
352 7ab0422a 2019-03-15 stsp if (feof(f))
354 7ab0422a 2019-03-15 stsp err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
357 0dec1cc0 2019-02-01 stsp for (i = 0; i < nsubdirs; i++) {
358 0dec1cc0 2019-02-01 stsp if (!ref_is_absolute &&
359 0dec1cc0 2019-02-01 stsp asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
360 0dec1cc0 2019-02-01 stsp refname) == -1)
361 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
362 0dec1cc0 2019-02-01 stsp err = parse_packed_ref_line(ref, abs_refname, line);
363 0dec1cc0 2019-02-01 stsp if (!ref_is_absolute)
364 0dec1cc0 2019-02-01 stsp free(abs_refname);
365 532920c8 2019-02-01 stsp if (err || *ref != NULL)
368 fb79db15 2019-02-01 stsp free(line);
371 fb79db15 2019-02-01 stsp } while (*ref == NULL);
373 fb79db15 2019-02-01 stsp return err;
376 fb79db15 2019-02-01 stsp static const struct got_error *
377 d1f2edc9 2018-06-13 stsp open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
378 2f17228e 2019-05-12 stsp const char *name, int lock)
380 d1f2edc9 2018-06-13 stsp const struct got_error *err = NULL;
381 a04f49d2 2019-02-04 stsp char *path = NULL;
382 a04f49d2 2019-02-04 stsp char *absname = NULL;
383 a04f49d2 2019-02-04 stsp int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
384 a04f49d2 2019-02-04 stsp int ref_is_well_known = is_well_known_ref(name);
386 1e37702e 2019-02-04 stsp *ref = NULL;
388 a04f49d2 2019-02-04 stsp if (ref_is_absolute || ref_is_well_known) {
389 a04f49d2 2019-02-04 stsp if (asprintf(&path, "%s/%s", path_refs, name) == -1)
390 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
391 a04f49d2 2019-02-04 stsp absname = (char *)name;
393 58908ed0 2019-03-11 stsp if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
394 58908ed0 2019-03-11 stsp subdir[0] ? "/" : "", name) == -1)
395 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
397 58908ed0 2019-03-11 stsp if (asprintf(&absname, "refs/%s%s%s",
398 58908ed0 2019-03-11 stsp subdir, subdir[0] ? "/" : "", name) == -1) {
399 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
404 6e472252 2019-07-22 stsp err = parse_ref_file(ref, absname, path, lock);
406 a04f49d2 2019-02-04 stsp if (!ref_is_absolute && !ref_is_well_known)
407 a04f49d2 2019-02-04 stsp free(absname);
408 a04f49d2 2019-02-04 stsp free(path);
409 d1f2edc9 2018-06-13 stsp return err;
412 5261c201 2018-04-01 stsp const struct got_error *
413 5261c201 2018-04-01 stsp got_ref_open(struct got_reference **ref, struct got_repository *repo,
414 2f17228e 2019-05-12 stsp const char *refname, int lock)
416 5261c201 2018-04-01 stsp const struct got_error *err = NULL;
417 c5f754cc 2019-02-01 stsp char *path_refs = NULL;
418 fb79db15 2019-02-01 stsp const char *subdirs[] = {
419 fb79db15 2019-02-01 stsp GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
421 c5f754cc 2019-02-01 stsp int i, well_known = is_well_known_ref(refname);
422 a875589a 2019-05-12 stsp struct got_lockfile *lf = NULL;
424 1e37702e 2019-02-04 stsp *ref = NULL;
426 e135804e 2019-02-10 stsp path_refs = get_refs_dir_path(repo, refname);
427 e135804e 2019-02-10 stsp if (path_refs == NULL) {
428 638f9024 2019-05-13 stsp err = got_error_from_errno2("get_refs_dir_path", refname);
432 0885ce8f 2019-05-12 stsp if (well_known) {
433 0885ce8f 2019-05-12 stsp err = open_ref(ref, path_refs, "", refname, lock);
435 c5f754cc 2019-02-01 stsp char *packed_refs_path;
438 e135804e 2019-02-10 stsp /* Search on-disk refs before packed refs! */
439 e135804e 2019-02-10 stsp for (i = 0; i < nitems(subdirs); i++) {
440 2f17228e 2019-05-12 stsp err = open_ref(ref, path_refs, subdirs[i], refname,
442 e135804e 2019-02-10 stsp if (err || *ref)
446 c5f754cc 2019-02-01 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
447 e135804e 2019-02-10 stsp if (packed_refs_path == NULL) {
448 638f9024 2019-05-13 stsp err = got_error_from_errno(
449 230a42bd 2019-05-11 jcs "got_repo_get_path_packed_refs");
453 2f17228e 2019-05-12 stsp if (lock) {
454 a875589a 2019-05-12 stsp err = got_lockfile_lock(&lf, packed_refs_path);
458 c5f754cc 2019-02-01 stsp f = fopen(packed_refs_path, "rb");
459 c5f754cc 2019-02-01 stsp free(packed_refs_path);
460 c5f754cc 2019-02-01 stsp if (f != NULL) {
461 0dec1cc0 2019-02-01 stsp err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
463 a875589a 2019-05-12 stsp if (!err) {
464 a875589a 2019-05-12 stsp if (fclose(f) != 0) {
465 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
466 a875589a 2019-05-12 stsp got_ref_close(*ref);
467 a875589a 2019-05-12 stsp *ref = NULL;
468 6e472abb 2019-05-13 stsp } else if (*ref)
469 a875589a 2019-05-12 stsp (*ref)->lf = lf;
474 5b575c25 2019-05-12 stsp if (!err && *ref == NULL)
475 1e37702e 2019-02-04 stsp err = got_error_not_ref(refname);
476 a875589a 2019-05-12 stsp if (err && lf)
477 a875589a 2019-05-12 stsp got_lockfile_unlock(lf);
478 5261c201 2018-04-01 stsp free(path_refs);
479 5261c201 2018-04-01 stsp return err;
483 5261c201 2018-04-01 stsp got_ref_close(struct got_reference *ref)
485 e09d28b1 2019-03-15 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
486 5261c201 2018-04-01 stsp free(ref->ref.symref.name);
487 e09d28b1 2019-03-15 stsp free(ref->ref.symref.ref);
489 5261c201 2018-04-01 stsp free(ref->ref.ref.name);
493 5261c201 2018-04-01 stsp struct got_reference *
494 5261c201 2018-04-01 stsp got_ref_dup(struct got_reference *ref)
496 5261c201 2018-04-01 stsp struct got_reference *ret;
498 5261c201 2018-04-01 stsp ret = calloc(1, sizeof(*ret));
499 5261c201 2018-04-01 stsp if (ret == NULL)
500 5261c201 2018-04-01 stsp return NULL;
502 5261c201 2018-04-01 stsp ret->flags = ref->flags;
503 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
504 5261c201 2018-04-01 stsp ret->ref.symref.name = strdup(ref->ref.symref.name);
505 5261c201 2018-04-01 stsp if (ret->ref.symref.name == NULL) {
507 5261c201 2018-04-01 stsp return NULL;
509 5261c201 2018-04-01 stsp ret->ref.symref.ref = strdup(ref->ref.symref.ref);
510 5261c201 2018-04-01 stsp if (ret->ref.symref.ref == NULL) {
511 5261c201 2018-04-01 stsp free(ret->ref.symref.name);
513 5261c201 2018-04-01 stsp return NULL;
516 5261c201 2018-04-01 stsp ref->ref.ref.name = strdup(ref->ref.ref.name);
517 5261c201 2018-04-01 stsp if (ref->ref.ref.name == NULL) {
519 5261c201 2018-04-01 stsp return NULL;
521 5261c201 2018-04-01 stsp memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
522 80d5fc1f 2019-03-15 stsp sizeof(ret->ref.ref.sha1));
525 5261c201 2018-04-01 stsp return ret;
528 5261c201 2018-04-01 stsp static const struct got_error *
529 5261c201 2018-04-01 stsp resolve_symbolic_ref(struct got_reference **resolved,
530 5261c201 2018-04-01 stsp struct got_repository *repo, struct got_reference *ref)
532 5261c201 2018-04-01 stsp struct got_reference *nextref;
533 5261c201 2018-04-01 stsp const struct got_error *err;
535 2f17228e 2019-05-12 stsp err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
537 5261c201 2018-04-01 stsp return err;
539 5261c201 2018-04-01 stsp if (nextref->flags & GOT_REF_IS_SYMBOLIC)
540 5261c201 2018-04-01 stsp err = resolve_symbolic_ref(resolved, repo, nextref);
542 5261c201 2018-04-01 stsp *resolved = got_ref_dup(nextref);
544 5261c201 2018-04-01 stsp got_ref_close(nextref);
545 5261c201 2018-04-01 stsp return err;
548 5261c201 2018-04-01 stsp const struct got_error *
549 5261c201 2018-04-01 stsp got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
550 5261c201 2018-04-01 stsp struct got_reference *ref)
552 5261c201 2018-04-01 stsp const struct got_error *err;
554 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
555 5261c201 2018-04-01 stsp struct got_reference *resolved = NULL;
556 5261c201 2018-04-01 stsp err = resolve_symbolic_ref(&resolved, repo, ref);
557 5261c201 2018-04-01 stsp if (err == NULL)
558 5261c201 2018-04-01 stsp err = got_ref_resolve(id, repo, resolved);
559 57b6f99a 2019-04-13 stsp if (resolved)
560 57b6f99a 2019-04-13 stsp got_ref_close(resolved);
561 5261c201 2018-04-01 stsp return err;
564 5261c201 2018-04-01 stsp *id = calloc(1, sizeof(**id));
565 5261c201 2018-04-01 stsp if (*id == NULL)
566 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
567 80d5fc1f 2019-03-15 stsp memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
568 5261c201 2018-04-01 stsp return NULL;
572 5261c201 2018-04-01 stsp got_ref_to_str(struct got_reference *ref)
576 271d2a38 2018-12-25 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
577 271d2a38 2018-12-25 stsp return strdup(ref->ref.symref.ref);
579 271d2a38 2018-12-25 stsp str = malloc(SHA1_DIGEST_STRING_LENGTH);
580 271d2a38 2018-12-25 stsp if (str == NULL)
581 271d2a38 2018-12-25 stsp return NULL;
583 271d2a38 2018-12-25 stsp if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
584 271d2a38 2018-12-25 stsp SHA1_DIGEST_STRING_LENGTH) == NULL) {
586 271d2a38 2018-12-25 stsp return NULL;
589 5261c201 2018-04-01 stsp return str;
592 0bd18d37 2019-02-01 stsp const char *
593 0bd18d37 2019-02-01 stsp got_ref_get_name(struct got_reference *ref)
595 0bd18d37 2019-02-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
596 0bd18d37 2019-02-01 stsp return ref->ref.symref.name;
598 0bd18d37 2019-02-01 stsp return ref->ref.ref.name;
601 aaf88317 2019-07-10 stsp const char *
602 aaf88317 2019-07-10 stsp got_ref_get_symref_target(struct got_reference *ref)
604 aaf88317 2019-07-10 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
605 aaf88317 2019-07-10 stsp return ref->ref.symref.ref;
607 aaf88317 2019-07-10 stsp return NULL;
610 199a4027 2019-02-02 stsp static const struct got_error *
611 505287be 2019-03-15 stsp insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
612 505287be 2019-03-15 stsp struct got_reference *ref, struct got_repository *repo)
614 199a4027 2019-02-02 stsp const struct got_error *err;
615 199a4027 2019-02-02 stsp struct got_object_id *id;
616 6249107b 2019-03-15 stsp struct got_reflist_entry *new, *re, *prev = NULL;
619 505287be 2019-03-15 stsp *newp = NULL;
621 7a3c76f5 2019-02-05 stsp err = got_ref_resolve(&id, repo, ref);
623 7a3c76f5 2019-02-05 stsp return err;
625 6fdbf7b0 2019-03-15 stsp new = malloc(sizeof(*new));
626 7a3c76f5 2019-02-05 stsp if (new == NULL) {
628 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
630 7a3c76f5 2019-02-05 stsp new->ref = ref;
631 7a3c76f5 2019-02-05 stsp new->id = id;
632 505287be 2019-03-15 stsp *newp = new;
635 29b5c214 2019-02-04 stsp * We must de-duplicate entries on insert because packed-refs may
636 29b5c214 2019-02-04 stsp * contain redundant entries. On-disk refs take precedence.
637 29b5c214 2019-02-04 stsp * This code assumes that on-disk revs are read before packed-refs.
638 e397b6db 2019-02-04 stsp * We're iterating the list anyway, so insert elements sorted by name.
640 7a3c76f5 2019-02-05 stsp re = SIMPLEQ_FIRST(refs);
641 7a3c76f5 2019-02-05 stsp while (re) {
642 d572f586 2019-08-02 stsp const char *name = got_ref_get_name(re->ref);
643 d572f586 2019-08-02 stsp const char *new_name = got_ref_get_name(new->ref);
644 d572f586 2019-08-02 stsp cmp = got_path_cmp(name, new_name, strlen(name),
645 d572f586 2019-08-02 stsp strlen(new_name));
646 e397b6db 2019-02-04 stsp if (cmp == 0) {
647 27a1ed03 2019-03-15 stsp /* duplicate */
648 27a1ed03 2019-03-15 stsp free(new->id);
650 505287be 2019-03-15 stsp *newp = NULL;
651 7a3c76f5 2019-02-05 stsp return NULL;
652 7a3c76f5 2019-02-05 stsp } else if (cmp > 0) {
654 7a3c76f5 2019-02-05 stsp SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
656 7a3c76f5 2019-02-05 stsp SIMPLEQ_INSERT_HEAD(refs, new, entry);
657 7a3c76f5 2019-02-05 stsp return NULL;
660 7a3c76f5 2019-02-05 stsp re = SIMPLEQ_NEXT(re, entry);
664 7a3c76f5 2019-02-05 stsp SIMPLEQ_INSERT_TAIL(refs, new, entry);
665 199a4027 2019-02-02 stsp return NULL;
668 a04f49d2 2019-02-04 stsp static const struct got_error *
669 29b5c214 2019-02-04 stsp gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
670 a04f49d2 2019-02-04 stsp const char *subdir, struct got_repository *repo)
672 a04f49d2 2019-02-04 stsp const struct got_error *err = NULL;
673 a04f49d2 2019-02-04 stsp DIR *d = NULL;
674 a04f49d2 2019-02-04 stsp char *path_subdir;
676 a04f49d2 2019-02-04 stsp if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
677 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
679 a04f49d2 2019-02-04 stsp d = opendir(path_subdir);
680 a04f49d2 2019-02-04 stsp if (d == NULL)
684 a04f49d2 2019-02-04 stsp struct dirent *dent;
685 a04f49d2 2019-02-04 stsp struct got_reference *ref;
686 a04f49d2 2019-02-04 stsp char *child;
688 a04f49d2 2019-02-04 stsp dent = readdir(d);
689 a04f49d2 2019-02-04 stsp if (dent == NULL)
692 a04f49d2 2019-02-04 stsp if (strcmp(dent->d_name, ".") == 0 ||
693 a04f49d2 2019-02-04 stsp strcmp(dent->d_name, "..") == 0)
696 a04f49d2 2019-02-04 stsp switch (dent->d_type) {
697 a04f49d2 2019-02-04 stsp case DT_REG:
698 2f17228e 2019-05-12 stsp err = open_ref(&ref, path_refs, subdir, dent->d_name,
703 505287be 2019-03-15 stsp struct got_reflist_entry *new;
704 505287be 2019-03-15 stsp err = insert_ref(&new, refs, ref, repo);
705 505287be 2019-03-15 stsp if (err || new == NULL /* duplicate */)
706 505287be 2019-03-15 stsp got_ref_close(ref);
711 a04f49d2 2019-02-04 stsp case DT_DIR:
712 a04f49d2 2019-02-04 stsp if (asprintf(&child, "%s%s%s", subdir,
713 a04f49d2 2019-02-04 stsp subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
714 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
717 29b5c214 2019-02-04 stsp err = gather_on_disk_refs(refs, path_refs, child, repo);
718 a04f49d2 2019-02-04 stsp free(child);
726 a04f49d2 2019-02-04 stsp closedir(d);
727 a04f49d2 2019-02-04 stsp free(path_subdir);
728 a04f49d2 2019-02-04 stsp return err;
731 199a4027 2019-02-02 stsp const struct got_error *
732 199a4027 2019-02-02 stsp got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
734 199a4027 2019-02-02 stsp const struct got_error *err;
735 a04f49d2 2019-02-04 stsp char *packed_refs_path, *path_refs = NULL;
736 29b5c214 2019-02-04 stsp FILE *f = NULL;
737 199a4027 2019-02-02 stsp struct got_reference *ref;
738 505287be 2019-03-15 stsp struct got_reflist_entry *new;
740 29b5c214 2019-02-04 stsp /* HEAD ref should always exist. */
741 29b5c214 2019-02-04 stsp path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
742 29b5c214 2019-02-04 stsp if (path_refs == NULL) {
743 638f9024 2019-05-13 stsp err = got_error_from_errno("get_refs_dir_path");
746 2f17228e 2019-05-12 stsp err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
749 505287be 2019-03-15 stsp err = insert_ref(&new, refs, ref, repo);
750 505287be 2019-03-15 stsp if (err || new == NULL /* duplicate */)
751 505287be 2019-03-15 stsp got_ref_close(ref);
755 29b5c214 2019-02-04 stsp /* Gather on-disk refs before parsing packed-refs. */
756 29b5c214 2019-02-04 stsp free(path_refs);
757 29b5c214 2019-02-04 stsp path_refs = get_refs_dir_path(repo, "");
758 29b5c214 2019-02-04 stsp if (path_refs == NULL) {
759 638f9024 2019-05-13 stsp err = got_error_from_errno("get_refs_dir_path");
762 29b5c214 2019-02-04 stsp err = gather_on_disk_refs(refs, path_refs, "", repo);
767 29b5c214 2019-02-04 stsp * The packed-refs file may contain redundant entries, in which
768 29b5c214 2019-02-04 stsp * case on-disk refs take precedence.
770 199a4027 2019-02-02 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
771 29b5c214 2019-02-04 stsp if (packed_refs_path == NULL) {
772 638f9024 2019-05-13 stsp err = got_error_from_errno("got_repo_get_path_packed_refs");
776 199a4027 2019-02-02 stsp f = fopen(packed_refs_path, "r");
777 199a4027 2019-02-02 stsp free(packed_refs_path);
779 199a4027 2019-02-02 stsp char *line;
780 199a4027 2019-02-02 stsp size_t len;
781 199a4027 2019-02-02 stsp const char delim[3] = {'\0', '\0', '\0'};
783 199a4027 2019-02-02 stsp line = fparseln(f, &len, NULL, delim, 0);
784 0bb4abae 2019-03-15 stsp if (line == NULL) {
785 0bb4abae 2019-03-15 stsp if (feof(f))
787 0bb4abae 2019-03-15 stsp err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
790 199a4027 2019-02-02 stsp err = parse_packed_ref_line(&ref, NULL, line);
791 0bb4abae 2019-03-15 stsp free(line);
795 505287be 2019-03-15 stsp err = insert_ref(&new, refs, ref, repo);
796 505287be 2019-03-15 stsp if (err || new == NULL /* duplicate */)
797 505287be 2019-03-15 stsp got_ref_close(ref);
804 a04f49d2 2019-02-04 stsp free(path_refs);
805 fb43ecf1 2019-02-11 stsp if (f && fclose(f) != 0 && err == NULL)
806 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
807 199a4027 2019-02-02 stsp return err;
811 e2e879a0 2019-03-11 stsp got_ref_list_free(struct got_reflist_head *refs)
813 e2e879a0 2019-03-11 stsp struct got_reflist_entry *re;
815 e2e879a0 2019-03-11 stsp while (!SIMPLEQ_EMPTY(refs)) {
816 e2e879a0 2019-03-11 stsp re = SIMPLEQ_FIRST(refs);
817 e2e879a0 2019-03-11 stsp SIMPLEQ_REMOVE_HEAD(refs, entry);
818 e2e879a0 2019-03-11 stsp got_ref_close(re->ref);
819 e2e879a0 2019-03-11 stsp free(re->id);
826 b249b824 2019-05-09 stsp got_ref_is_symbolic(struct got_reference *ref)
828 b249b824 2019-05-09 stsp return (ref->flags & GOT_REF_IS_SYMBOLIC);
831 b249b824 2019-05-09 stsp const struct got_error *
832 b249b824 2019-05-09 stsp got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
834 b249b824 2019-05-09 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
835 b249b824 2019-05-09 stsp return got_error(GOT_ERR_BAD_REF_TYPE);
837 b249b824 2019-05-09 stsp memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
838 b249b824 2019-05-09 stsp return NULL;
841 9e672c74 2019-03-11 stsp const struct got_error *
842 b249b824 2019-05-09 stsp got_ref_change_symref(struct got_reference *ref, char *refname)
844 b249b824 2019-05-09 stsp char *new_name;
846 b249b824 2019-05-09 stsp if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
847 b249b824 2019-05-09 stsp return got_error(GOT_ERR_BAD_REF_TYPE);
849 b249b824 2019-05-09 stsp new_name = strdup(refname);
850 b249b824 2019-05-09 stsp if (new_name == NULL)
851 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
853 b249b824 2019-05-09 stsp free(ref->ref.symref.name);
854 b249b824 2019-05-09 stsp ref->ref.symref.name = new_name;
855 b249b824 2019-05-09 stsp return NULL;
858 b249b824 2019-05-09 stsp const struct got_error *
859 9e672c74 2019-03-11 stsp got_ref_write(struct got_reference *ref, struct got_repository *repo)
861 9e672c74 2019-03-11 stsp const struct got_error *err = NULL, *unlock_err = NULL;
862 9e672c74 2019-03-11 stsp const char *name = got_ref_get_name(ref);
863 9e672c74 2019-03-11 stsp char *path_refs = NULL, *path = NULL, *tmppath = NULL;
864 9e672c74 2019-03-11 stsp struct got_lockfile *lf = NULL;
865 9e672c74 2019-03-11 stsp FILE *f = NULL;
867 9e672c74 2019-03-11 stsp struct stat sb;
869 9e672c74 2019-03-11 stsp path_refs = get_refs_dir_path(repo, name);
870 9e672c74 2019-03-11 stsp if (path_refs == NULL) {
871 638f9024 2019-05-13 stsp err = got_error_from_errno2("get_refs_dir_path", name);
875 9e672c74 2019-03-11 stsp if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
876 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
880 9e672c74 2019-03-11 stsp err = got_opentemp_named(&tmppath, &f, path);
882 d1667f0d 2019-03-11 stsp char *parent;
883 49c7094f 2019-03-11 stsp if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
885 d1667f0d 2019-03-11 stsp err = got_path_dirname(&parent, path);
888 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(parent);
889 5e1c9f23 2019-03-11 stsp free(parent);
892 0cd1c46a 2019-03-11 stsp err = got_opentemp_named(&tmppath, &f, path);
897 9e672c74 2019-03-11 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
898 9e672c74 2019-03-11 stsp n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
899 9e672c74 2019-03-11 stsp if (n != strlen(ref->ref.symref.ref) + 6) {
900 9e672c74 2019-03-11 stsp err = got_ferror(f, GOT_ERR_IO);
904 9e672c74 2019-03-11 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
905 9e672c74 2019-03-11 stsp if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
906 9e672c74 2019-03-11 stsp sizeof(hex)) == NULL) {
907 9e672c74 2019-03-11 stsp err = got_error(GOT_ERR_BAD_REF_DATA);
910 9e672c74 2019-03-11 stsp n = fprintf(f, "%s\n", hex);
911 8fa2f096 2019-03-11 stsp if (n != sizeof(hex)) {
912 9e672c74 2019-03-11 stsp err = got_ferror(f, GOT_ERR_IO);
917 2f17228e 2019-05-12 stsp if (ref->lf == NULL) {
918 2f17228e 2019-05-12 stsp err = got_lockfile_lock(&lf, path);
923 9e672c74 2019-03-11 stsp /* XXX: check if old content matches our expectations? */
925 0cd1c46a 2019-03-11 stsp if (stat(path, &sb) != 0) {
926 0cd1c46a 2019-03-11 stsp if (errno != ENOENT) {
927 638f9024 2019-05-13 stsp err = got_error_from_errno2("stat", path);
930 0cd1c46a 2019-03-11 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
933 9e672c74 2019-03-11 stsp if (rename(tmppath, path) != 0) {
934 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
937 9e672c74 2019-03-11 stsp free(tmppath);
938 9e672c74 2019-03-11 stsp tmppath = NULL;
940 9e672c74 2019-03-11 stsp if (chmod(path, sb.st_mode) != 0) {
941 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", path);
945 2f17228e 2019-05-12 stsp if (ref->lf == NULL && lf)
946 9e672c74 2019-03-11 stsp unlock_err = got_lockfile_unlock(lf);
948 9e672c74 2019-03-11 stsp if (fclose(f) != 0 && err == NULL)
949 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
951 9e672c74 2019-03-11 stsp free(path_refs);
952 9e672c74 2019-03-11 stsp free(path);
953 9e672c74 2019-03-11 stsp if (tmppath) {
954 9e672c74 2019-03-11 stsp if (unlink(tmppath) != 0 && err == NULL)
955 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", tmppath);
956 9e672c74 2019-03-11 stsp free(tmppath);
958 2d2e1378 2019-03-11 stsp return err ? err : unlock_err;
961 598a8b91 2019-03-15 stsp static const struct got_error *
962 598a8b91 2019-03-15 stsp delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
964 598a8b91 2019-03-15 stsp const struct got_error *err = NULL, *unlock_err = NULL;
965 598a8b91 2019-03-15 stsp struct got_lockfile *lf = NULL;
966 598a8b91 2019-03-15 stsp FILE *f = NULL, *tmpf = NULL;
967 598a8b91 2019-03-15 stsp char *packed_refs_path, *tmppath = NULL;
968 598a8b91 2019-03-15 stsp struct got_reflist_head refs;
969 598a8b91 2019-03-15 stsp int found_delref = 0;
971 598a8b91 2019-03-15 stsp /* The packed-refs file does not cotain symbolic references. */
972 598a8b91 2019-03-15 stsp if (delref->flags & GOT_REF_IS_SYMBOLIC)
973 598a8b91 2019-03-15 stsp return got_error(GOT_ERR_BAD_REF_DATA);
975 598a8b91 2019-03-15 stsp SIMPLEQ_INIT(&refs);
977 598a8b91 2019-03-15 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
978 598a8b91 2019-03-15 stsp if (packed_refs_path == NULL)
979 638f9024 2019-05-13 stsp return got_error_from_errno("got_repo_get_path_packed_refs");
981 598a8b91 2019-03-15 stsp err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
985 2f17228e 2019-05-12 stsp if (delref->lf == NULL) {
986 2f17228e 2019-05-12 stsp err = got_lockfile_lock(&lf, packed_refs_path);
991 598a8b91 2019-03-15 stsp f = fopen(packed_refs_path, "r");
992 598a8b91 2019-03-15 stsp if (f == NULL) {
993 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", packed_refs_path);
997 598a8b91 2019-03-15 stsp char *line;
998 598a8b91 2019-03-15 stsp size_t len;
999 598a8b91 2019-03-15 stsp const char delim[3] = {'\0', '\0', '\0'};
1000 598a8b91 2019-03-15 stsp struct got_reference *ref;
1001 598a8b91 2019-03-15 stsp struct got_reflist_entry *new;
1003 598a8b91 2019-03-15 stsp line = fparseln(f, &len, NULL, delim, 0);
1004 598a8b91 2019-03-15 stsp if (line == NULL) {
1005 598a8b91 2019-03-15 stsp if (feof(f))
1007 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1008 598a8b91 2019-03-15 stsp goto done;
1010 598a8b91 2019-03-15 stsp err = parse_packed_ref_line(&ref, NULL, line);
1011 598a8b91 2019-03-15 stsp free(line);
1013 598a8b91 2019-03-15 stsp goto done;
1014 598a8b91 2019-03-15 stsp if (ref == NULL)
1017 598a8b91 2019-03-15 stsp if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1018 598a8b91 2019-03-15 stsp memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1019 598a8b91 2019-03-15 stsp sizeof(delref->ref.ref.sha1)) == 0) {
1020 598a8b91 2019-03-15 stsp found_delref = 1;
1021 598a8b91 2019-03-15 stsp got_ref_close(ref);
1025 598a8b91 2019-03-15 stsp err = insert_ref(&new, &refs, ref, repo);
1026 598a8b91 2019-03-15 stsp if (err || new == NULL /* duplicate */)
1027 598a8b91 2019-03-15 stsp got_ref_close(ref);
1029 598a8b91 2019-03-15 stsp goto done;
1032 598a8b91 2019-03-15 stsp if (found_delref) {
1033 598a8b91 2019-03-15 stsp struct got_reflist_entry *re;
1035 598a8b91 2019-03-15 stsp struct stat sb;
1037 598a8b91 2019-03-15 stsp n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1038 598a8b91 2019-03-15 stsp if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1039 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_IO);
1040 598a8b91 2019-03-15 stsp goto done;
1043 598a8b91 2019-03-15 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
1044 598a8b91 2019-03-15 stsp uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1046 598a8b91 2019-03-15 stsp if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1047 598a8b91 2019-03-15 stsp sizeof(hex)) == NULL) {
1048 598a8b91 2019-03-15 stsp err = got_error(GOT_ERR_BAD_REF_DATA);
1049 598a8b91 2019-03-15 stsp goto done;
1051 598a8b91 2019-03-15 stsp n = fprintf(tmpf, "%s ", hex);
1052 598a8b91 2019-03-15 stsp if (n != sizeof(hex)) {
1053 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_IO);
1054 598a8b91 2019-03-15 stsp goto done;
1056 598a8b91 2019-03-15 stsp n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1057 598a8b91 2019-03-15 stsp if (n != strlen(re->ref->ref.ref.name) + 1) {
1058 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_IO);
1059 598a8b91 2019-03-15 stsp goto done;
1063 598a8b91 2019-03-15 stsp if (fflush(tmpf) != 0) {
1064 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1065 598a8b91 2019-03-15 stsp goto done;
1068 598a8b91 2019-03-15 stsp if (stat(packed_refs_path, &sb) != 0) {
1069 598a8b91 2019-03-15 stsp if (errno != ENOENT) {
1070 638f9024 2019-05-13 stsp err = got_error_from_errno2("stat",
1071 230a42bd 2019-05-11 jcs packed_refs_path);
1072 598a8b91 2019-03-15 stsp goto done;
1074 598a8b91 2019-03-15 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1077 598a8b91 2019-03-15 stsp if (rename(tmppath, packed_refs_path) != 0) {
1078 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1079 230a42bd 2019-05-11 jcs packed_refs_path);
1080 598a8b91 2019-03-15 stsp goto done;
1083 598a8b91 2019-03-15 stsp if (chmod(packed_refs_path, sb.st_mode) != 0) {
1084 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod",
1085 230a42bd 2019-05-11 jcs packed_refs_path);
1086 598a8b91 2019-03-15 stsp goto done;
1090 2f17228e 2019-05-12 stsp if (delref->lf == NULL && lf)
1091 598a8b91 2019-03-15 stsp unlock_err = got_lockfile_unlock(lf);
1093 598a8b91 2019-03-15 stsp if (fclose(f) != 0 && err == NULL)
1094 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1096 598a8b91 2019-03-15 stsp if (tmpf) {
1097 598a8b91 2019-03-15 stsp unlink(tmppath);
1098 598a8b91 2019-03-15 stsp if (fclose(tmpf) != 0 && err == NULL)
1099 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1101 598a8b91 2019-03-15 stsp free(tmppath);
1102 598a8b91 2019-03-15 stsp free(packed_refs_path);
1103 598a8b91 2019-03-15 stsp got_ref_list_free(&refs);
1104 598a8b91 2019-03-15 stsp return err ? err : unlock_err;
1107 2d2e1378 2019-03-11 stsp const struct got_error *
1108 2d2e1378 2019-03-11 stsp got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1110 2d2e1378 2019-03-11 stsp const struct got_error *err = NULL, *unlock_err = NULL;
1111 2d2e1378 2019-03-11 stsp const char *name = got_ref_get_name(ref);
1112 2d2e1378 2019-03-11 stsp char *path_refs = NULL, *path = NULL;
1113 2d2e1378 2019-03-11 stsp struct got_lockfile *lf = NULL;
1115 598a8b91 2019-03-15 stsp if (ref->flags & GOT_REF_IS_PACKED)
1116 598a8b91 2019-03-15 stsp return delete_packed_ref(ref, repo);
1118 2d2e1378 2019-03-11 stsp path_refs = get_refs_dir_path(repo, name);
1119 2d2e1378 2019-03-11 stsp if (path_refs == NULL) {
1120 638f9024 2019-05-13 stsp err = got_error_from_errno2("get_refs_dir_path", name);
1121 2d2e1378 2019-03-11 stsp goto done;
1124 2d2e1378 2019-03-11 stsp if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1125 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1126 2d2e1378 2019-03-11 stsp goto done;
1129 2f17228e 2019-05-12 stsp if (ref->lf == NULL) {
1130 2f17228e 2019-05-12 stsp err = got_lockfile_lock(&lf, path);
1132 2f17228e 2019-05-12 stsp goto done;
1135 2d2e1378 2019-03-11 stsp /* XXX: check if old content matches our expectations? */
1137 2d2e1378 2019-03-11 stsp if (unlink(path) != 0)
1138 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", path);
1140 2f17228e 2019-05-12 stsp if (ref->lf == NULL && lf)
1141 2d2e1378 2019-03-11 stsp unlock_err = got_lockfile_unlock(lf);
1143 2d2e1378 2019-03-11 stsp free(path_refs);
1144 2d2e1378 2019-03-11 stsp free(path);
1145 9e672c74 2019-03-11 stsp return err ? err : unlock_err;
1148 2f17228e 2019-05-12 stsp const struct got_error *
1149 2f17228e 2019-05-12 stsp got_ref_unlock(struct got_reference *ref)
1151 2f17228e 2019-05-12 stsp const struct got_error *err;
1152 2f17228e 2019-05-12 stsp err = got_lockfile_unlock(ref->lf);
1153 2f17228e 2019-05-12 stsp ref->lf = NULL;
1154 2f17228e 2019-05-12 stsp return err;