Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/stat.h>
21 #include <errno.h>
22 #include <ctype.h>
23 #include <dirent.h>
24 #include <sha1.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <util.h>
29 #include <zlib.h>
30 #include <time.h>
31 #include <libgen.h>
33 #include "got_error.h"
34 #include "got_object.h"
35 #include "got_repository.h"
36 #include "got_reference.h"
37 #include "got_opentemp.h"
38 #include "got_path.h"
40 #include "got_lib_sha1.h"
41 #include "got_lib_delta.h"
42 #include "got_lib_inflate.h"
43 #include "got_lib_object.h"
44 #include "got_lib_lockfile.h"
46 #ifndef nitems
47 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
48 #endif
50 #define GOT_REF_HEADS "heads"
51 #define GOT_REF_TAGS "tags"
52 #define GOT_REF_REMOTES "remotes"
54 /*
55 * We do not resolve tags yet, and don't yet care about sorting refs either,
56 * so packed-refs files we write contain a minimal header which disables all
57 * packed-refs "traits" supported by Git.
58 */
59 #define GOT_PACKED_REFS_HEADER "# pack-refs with:"
61 /* A symbolic reference. */
62 struct got_symref {
63 char *name;
64 char *ref;
65 };
67 /* A non-symbolic reference (there is no better designation). */
68 struct got_ref {
69 char *name;
70 u_int8_t sha1[SHA1_DIGEST_LENGTH];
71 };
73 /* A reference which points to an arbitrary object. */
74 struct got_reference {
75 unsigned int flags;
76 #define GOT_REF_IS_SYMBOLIC 0x01
77 #define GOT_REF_IS_PACKED 0x02
79 union {
80 struct got_ref ref;
81 struct got_symref symref;
82 } ref;
84 struct got_lockfile *lf;
85 };
87 static const struct got_error *
88 alloc_ref(struct got_reference **ref, const char *name,
89 struct got_object_id *id, int flags)
90 {
91 const struct got_error *err = NULL;
93 *ref = calloc(1, sizeof(**ref));
94 if (*ref == NULL)
95 return got_error_prefix_errno("calloc");
97 memcpy((*ref)->ref.ref.sha1, id->sha1, sizeof((*ref)->ref.ref.sha1));
98 (*ref)->flags = flags;
99 (*ref)->ref.ref.name = strdup(name);
100 if ((*ref)->ref.ref.name == NULL) {
101 err = got_error_prefix_errno("strdup");
102 got_ref_close(*ref);
103 *ref = NULL;
105 return err;
108 static const struct got_error *
109 alloc_symref(struct got_reference **ref, const char *name,
110 const char *target_ref, int flags)
112 const struct got_error *err = NULL;
114 *ref = calloc(1, sizeof(**ref));
115 if (*ref == NULL)
116 return got_error_prefix_errno("calloc");
118 (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
119 (*ref)->ref.symref.name = strdup(name);
120 if ((*ref)->ref.symref.name == NULL) {
121 err = got_error_prefix_errno("strdup");
122 got_ref_close(*ref);
123 *ref = NULL;
125 (*ref)->ref.symref.ref = strdup(target_ref);
126 if ((*ref)->ref.symref.ref == NULL) {
127 err = got_error_prefix_errno("strdup");
128 got_ref_close(*ref);
129 *ref = NULL;
131 return err;
134 static const struct got_error *
135 parse_symref(struct got_reference **ref, const char *name, const char *line)
137 if (line[0] == '\0')
138 return got_error(GOT_ERR_BAD_REF_DATA);
140 return alloc_symref(ref, name, line, 0);
143 static const struct got_error *
144 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
146 struct got_object_id id;
148 if (strncmp(line, "ref: ", 5) == 0) {
149 line += 5;
150 return parse_symref(ref, name, line);
153 if (!got_parse_sha1_digest(id.sha1, line))
154 return got_error(GOT_ERR_BAD_REF_DATA);
156 return alloc_ref(ref, name, &id, 0);
159 static const struct got_error *
160 parse_ref_file(struct got_reference **ref, const char *name,
161 const char *abspath, int lock)
163 const struct got_error *err = NULL;
164 FILE *f;
165 char *line;
166 size_t len;
167 const char delim[3] = {'\0', '\0', '\0'};
168 struct got_lockfile *lf = NULL;
170 if (lock) {
171 err = got_lockfile_lock(&lf, abspath);
172 if (err)
173 return (err);
176 f = fopen(abspath, "rb");
177 if (f == NULL)
178 return NULL;
180 line = fparseln(f, &len, NULL, delim, 0);
181 if (line == NULL) {
182 err = got_error(GOT_ERR_BAD_REF_DATA);
183 goto done;
186 err = parse_ref_line(ref, name, line);
187 if (err == NULL && lf)
188 (*ref)->lf = lf;
189 done:
190 free(line);
191 if (fclose(f) != 0 && err == NULL) {
192 err = got_error_prefix_errno("fclose");
193 if (lf)
194 got_lockfile_unlock(lf);
195 if (*ref) {
196 got_ref_close(*ref);
197 *ref = NULL;
200 return err;
203 static int
204 is_well_known_ref(const char *refname)
206 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
207 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
208 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
209 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
212 static char *
213 get_refs_dir_path(struct got_repository *repo, const char *refname)
215 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
216 return strdup(got_repo_get_path_git_dir(repo));
218 return got_repo_get_path_refs(repo);
221 static int
222 is_valid_ref_name(const char *name)
224 const char *s, *slash, *seg;
225 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
226 const char *forbidden_seq[] = { "//", "..", "@{" };
227 const char *lfs = GOT_LOCKFILE_SUFFIX;
228 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
229 int i;
231 if (name[0] == '@' && name[1] == '\0')
232 return 0;
234 slash = strchr(name, '/');
235 if (slash == NULL)
236 return 0;
238 s = name;
239 seg = s;
240 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
241 return 0;
242 while (*s) {
243 for (i = 0; i < nitems(forbidden); i++) {
244 if (*s == forbidden[i])
245 return 0;
247 for (i = 0; i < nitems(forbidden_seq); i++) {
248 if (s[0] == forbidden_seq[i][0] &&
249 s[1] == forbidden_seq[i][1])
250 return 0;
252 if (iscntrl((unsigned char)s[0]))
253 return 0;
254 if (s[0] == '.' && s[1] == '\0')
255 return 0;
256 if (*s == '/') {
257 const char *nextseg = s + 1;
258 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
259 nextseg[0] == '/')
260 return 0;
261 if (seg <= s - lfs_len &&
262 strncmp(s - lfs_len, lfs, lfs_len) == 0)
263 return 0;
264 seg = nextseg;
266 s++;
269 if (seg <= s - lfs_len &&
270 strncmp(s - lfs_len, lfs, lfs_len) == 0)
271 return 0;
273 return 1;
276 const struct got_error *
277 got_ref_alloc(struct got_reference **ref, const char *name,
278 struct got_object_id *id)
280 if (!is_valid_ref_name(name))
281 return got_error(GOT_ERR_BAD_REF_NAME);
283 return alloc_ref(ref, name, id, 0);
286 static const struct got_error *
287 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
288 const char *line)
290 struct got_object_id id;
291 const char *name;
293 *ref = NULL;
295 if (line[0] == '#' || line[0] == '^')
296 return NULL;
298 if (!got_parse_sha1_digest(id.sha1, line))
299 return got_error(GOT_ERR_BAD_REF_DATA);
301 if (abs_refname) {
302 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
303 return NULL;
304 name = abs_refname;
305 } else
306 name = line + SHA1_DIGEST_STRING_LENGTH;
308 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
311 static const struct got_error *
312 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
313 int nsubdirs, const char *refname)
315 const struct got_error *err = NULL;
316 char *abs_refname;
317 char *line;
318 size_t len;
319 const char delim[3] = {'\0', '\0', '\0'};
320 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
322 *ref = NULL;
324 if (ref_is_absolute)
325 abs_refname = (char *)refname;
326 do {
327 line = fparseln(f, &len, NULL, delim, 0);
328 if (line == NULL) {
329 if (feof(f))
330 break;
331 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
332 break;
334 for (i = 0; i < nsubdirs; i++) {
335 if (!ref_is_absolute &&
336 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
337 refname) == -1)
338 return got_error_prefix_errno("asprintf");
339 err = parse_packed_ref_line(ref, abs_refname, line);
340 if (!ref_is_absolute)
341 free(abs_refname);
342 if (err || *ref != NULL)
343 break;
345 free(line);
346 if (err)
347 break;
348 } while (*ref == NULL);
350 return err;
353 static const struct got_error *
354 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
355 const char *name, int lock)
357 const struct got_error *err = NULL;
358 char *path = NULL;
359 char *normpath = NULL;
360 char *absname = NULL;
361 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
362 int ref_is_well_known = is_well_known_ref(name);
364 *ref = NULL;
366 if (ref_is_absolute || ref_is_well_known) {
367 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
368 return got_error_prefix_errno("asprintf");
369 absname = (char *)name;
370 } else {
371 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
372 subdir[0] ? "/" : "", name) == -1)
373 return got_error_prefix_errno("asprintf");
375 if (asprintf(&absname, "refs/%s%s%s",
376 subdir, subdir[0] ? "/" : "", name) == -1) {
377 err = got_error_prefix_errno("asprintf");
378 goto done;
382 normpath = got_path_normalize(path);
383 if (normpath == NULL) {
384 err = got_error_prefix_errno2("got_path_normalize", path);
385 goto done;
388 err = parse_ref_file(ref, absname, normpath, lock);
389 done:
390 if (!ref_is_absolute && !ref_is_well_known)
391 free(absname);
392 free(path);
393 free(normpath);
394 return err;
397 const struct got_error *
398 got_ref_open(struct got_reference **ref, struct got_repository *repo,
399 const char *refname, int lock)
401 const struct got_error *err = NULL;
402 char *path_refs = NULL;
403 const char *subdirs[] = {
404 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
405 };
406 int i, well_known = is_well_known_ref(refname);
408 *ref = NULL;
410 path_refs = get_refs_dir_path(repo, refname);
411 if (path_refs == NULL) {
412 err = got_error_prefix_errno2("get_refs_dir_path", refname);
413 goto done;
416 if (!well_known) {
417 char *packed_refs_path;
418 FILE *f;
420 /* Search on-disk refs before packed refs! */
421 for (i = 0; i < nitems(subdirs); i++) {
422 err = open_ref(ref, path_refs, subdirs[i], refname,
423 lock);
424 if (err || *ref)
425 goto done;
428 packed_refs_path = got_repo_get_path_packed_refs(repo);
429 if (packed_refs_path == NULL) {
430 err = got_error_prefix_errno(
431 "got_repo_get_path_packed_refs");
432 goto done;
435 if (lock) {
436 err = got_lockfile_lock(&(*ref)->lf, packed_refs_path);
437 if (err)
438 goto done;
440 f = fopen(packed_refs_path, "rb");
441 free(packed_refs_path);
442 if (f != NULL) {
443 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
444 refname);
445 if (fclose(f) != 0 && err == NULL)
446 err = got_error_prefix_errno("fclose");
447 if (err || *ref)
448 goto done;
452 err = open_ref(ref, path_refs, "", refname, lock);
453 if (err)
454 goto done;
455 done:
456 if (*ref == NULL)
457 err = got_error_not_ref(refname);
458 free(path_refs);
459 return err;
462 void
463 got_ref_close(struct got_reference *ref)
465 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
466 free(ref->ref.symref.name);
467 free(ref->ref.symref.ref);
468 } else
469 free(ref->ref.ref.name);
470 free(ref);
473 struct got_reference *
474 got_ref_dup(struct got_reference *ref)
476 struct got_reference *ret;
478 ret = calloc(1, sizeof(*ret));
479 if (ret == NULL)
480 return NULL;
482 ret->flags = ref->flags;
483 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
484 ret->ref.symref.name = strdup(ref->ref.symref.name);
485 if (ret->ref.symref.name == NULL) {
486 free(ret);
487 return NULL;
489 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
490 if (ret->ref.symref.ref == NULL) {
491 free(ret->ref.symref.name);
492 free(ret);
493 return NULL;
495 } else {
496 ref->ref.ref.name = strdup(ref->ref.ref.name);
497 if (ref->ref.ref.name == NULL) {
498 free(ret);
499 return NULL;
501 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
502 sizeof(ret->ref.ref.sha1));
505 return ret;
508 static const struct got_error *
509 resolve_symbolic_ref(struct got_reference **resolved,
510 struct got_repository *repo, struct got_reference *ref)
512 struct got_reference *nextref;
513 const struct got_error *err;
515 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
516 if (err)
517 return err;
519 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
520 err = resolve_symbolic_ref(resolved, repo, nextref);
521 else
522 *resolved = got_ref_dup(nextref);
524 got_ref_close(nextref);
525 return err;
528 const struct got_error *
529 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
530 struct got_reference *ref)
532 const struct got_error *err;
534 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
535 struct got_reference *resolved = NULL;
536 err = resolve_symbolic_ref(&resolved, repo, ref);
537 if (err == NULL)
538 err = got_ref_resolve(id, repo, resolved);
539 if (resolved)
540 got_ref_close(resolved);
541 return err;
544 *id = calloc(1, sizeof(**id));
545 if (*id == NULL)
546 return got_error_prefix_errno("calloc");
547 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
548 return NULL;
551 char *
552 got_ref_to_str(struct got_reference *ref)
554 char *str;
556 if (ref->flags & GOT_REF_IS_SYMBOLIC)
557 return strdup(ref->ref.symref.ref);
559 str = malloc(SHA1_DIGEST_STRING_LENGTH);
560 if (str == NULL)
561 return NULL;
563 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
564 SHA1_DIGEST_STRING_LENGTH) == NULL) {
565 free(str);
566 return NULL;
569 return str;
572 const char *
573 got_ref_get_name(struct got_reference *ref)
575 if (ref->flags & GOT_REF_IS_SYMBOLIC)
576 return ref->ref.symref.name;
578 return ref->ref.ref.name;
581 static const struct got_error *
582 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
583 struct got_reference *ref, struct got_repository *repo)
585 const struct got_error *err;
586 struct got_object_id *id;
587 struct got_reflist_entry *new, *re, *prev = NULL;
588 int cmp;
590 *newp = NULL;
592 err = got_ref_resolve(&id, repo, ref);
593 if (err)
594 return err;
596 new = malloc(sizeof(*new));
597 if (new == NULL) {
598 free(id);
599 return got_error_prefix_errno("malloc");
601 new->ref = ref;
602 new->id = id;
603 *newp = new;
605 /*
606 * We must de-duplicate entries on insert because packed-refs may
607 * contain redundant entries. On-disk refs take precedence.
608 * This code assumes that on-disk revs are read before packed-refs.
609 * We're iterating the list anyway, so insert elements sorted by name.
610 */
611 re = SIMPLEQ_FIRST(refs);
612 while (re) {
613 cmp = got_path_cmp(got_ref_get_name(re->ref),
614 got_ref_get_name(new->ref));
615 if (cmp == 0) {
616 /* duplicate */
617 free(new->id);
618 free(new);
619 *newp = NULL;
620 return NULL;
621 } else if (cmp > 0) {
622 if (prev)
623 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
624 else
625 SIMPLEQ_INSERT_HEAD(refs, new, entry);
626 return NULL;
627 } else {
628 prev = re;
629 re = SIMPLEQ_NEXT(re, entry);
633 SIMPLEQ_INSERT_TAIL(refs, new, entry);
634 return NULL;
637 static const struct got_error *
638 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
639 const char *subdir, struct got_repository *repo)
641 const struct got_error *err = NULL;
642 DIR *d = NULL;
643 char *path_subdir;
645 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
646 return got_error_prefix_errno("asprintf");
648 d = opendir(path_subdir);
649 if (d == NULL)
650 goto done;
652 for (;;) {
653 struct dirent *dent;
654 struct got_reference *ref;
655 char *child;
657 dent = readdir(d);
658 if (dent == NULL)
659 break;
661 if (strcmp(dent->d_name, ".") == 0 ||
662 strcmp(dent->d_name, "..") == 0)
663 continue;
665 switch (dent->d_type) {
666 case DT_REG:
667 err = open_ref(&ref, path_refs, subdir, dent->d_name,
668 0);
669 if (err)
670 goto done;
671 if (ref) {
672 struct got_reflist_entry *new;
673 err = insert_ref(&new, refs, ref, repo);
674 if (err || new == NULL /* duplicate */)
675 got_ref_close(ref);
676 if (err)
677 goto done;
679 break;
680 case DT_DIR:
681 if (asprintf(&child, "%s%s%s", subdir,
682 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
683 err = got_error_prefix_errno("asprintf");
684 break;
686 err = gather_on_disk_refs(refs, path_refs, child, repo);
687 free(child);
688 break;
689 default:
690 break;
693 done:
694 if (d)
695 closedir(d);
696 free(path_subdir);
697 return err;
700 const struct got_error *
701 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
703 const struct got_error *err;
704 char *packed_refs_path, *path_refs = NULL;
705 FILE *f = NULL;
706 struct got_reference *ref;
707 struct got_reflist_entry *new;
709 /* HEAD ref should always exist. */
710 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
711 if (path_refs == NULL) {
712 err = got_error_prefix_errno("get_refs_dir_path");
713 goto done;
715 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
716 if (err)
717 goto done;
718 err = insert_ref(&new, refs, ref, repo);
719 if (err || new == NULL /* duplicate */)
720 got_ref_close(ref);
721 if (err)
722 goto done;
724 /* Gather on-disk refs before parsing packed-refs. */
725 free(path_refs);
726 path_refs = get_refs_dir_path(repo, "");
727 if (path_refs == NULL) {
728 err = got_error_prefix_errno("get_refs_dir_path");
729 goto done;
731 err = gather_on_disk_refs(refs, path_refs, "", repo);
732 if (err)
733 goto done;
735 /*
736 * The packed-refs file may contain redundant entries, in which
737 * case on-disk refs take precedence.
738 */
739 packed_refs_path = got_repo_get_path_packed_refs(repo);
740 if (packed_refs_path == NULL) {
741 err = got_error_prefix_errno("got_repo_get_path_packed_refs");
742 goto done;
745 f = fopen(packed_refs_path, "r");
746 free(packed_refs_path);
747 if (f) {
748 char *line;
749 size_t len;
750 const char delim[3] = {'\0', '\0', '\0'};
751 for (;;) {
752 line = fparseln(f, &len, NULL, delim, 0);
753 if (line == NULL) {
754 if (feof(f))
755 break;
756 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
757 goto done;
759 err = parse_packed_ref_line(&ref, NULL, line);
760 free(line);
761 if (err)
762 goto done;
763 if (ref) {
764 err = insert_ref(&new, refs, ref, repo);
765 if (err || new == NULL /* duplicate */)
766 got_ref_close(ref);
767 if (err)
768 goto done;
772 done:
773 free(path_refs);
774 if (f && fclose(f) != 0 && err == NULL)
775 err = got_error_prefix_errno("fclose");
776 return err;
779 void
780 got_ref_list_free(struct got_reflist_head *refs)
782 struct got_reflist_entry *re;
784 while (!SIMPLEQ_EMPTY(refs)) {
785 re = SIMPLEQ_FIRST(refs);
786 SIMPLEQ_REMOVE_HEAD(refs, entry);
787 got_ref_close(re->ref);
788 free(re->id);
789 free(re);
794 int
795 got_ref_is_symbolic(struct got_reference *ref)
797 return (ref->flags & GOT_REF_IS_SYMBOLIC);
800 const struct got_error *
801 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
803 if (ref->flags & GOT_REF_IS_SYMBOLIC)
804 return got_error(GOT_ERR_BAD_REF_TYPE);
806 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
807 return NULL;
810 const struct got_error *
811 got_ref_change_symref(struct got_reference *ref, char *refname)
813 char *new_name;
815 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
816 return got_error(GOT_ERR_BAD_REF_TYPE);
818 new_name = strdup(refname);
819 if (new_name == NULL)
820 return got_error_prefix_errno("strdup");
822 free(ref->ref.symref.name);
823 ref->ref.symref.name = new_name;
824 return NULL;
827 const struct got_error *
828 got_ref_write(struct got_reference *ref, struct got_repository *repo)
830 const struct got_error *err = NULL, *unlock_err = NULL;
831 const char *name = got_ref_get_name(ref);
832 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
833 struct got_lockfile *lf = NULL;
834 FILE *f = NULL;
835 size_t n;
836 struct stat sb;
838 path_refs = get_refs_dir_path(repo, name);
839 if (path_refs == NULL) {
840 err = got_error_prefix_errno2("get_refs_dir_path", name);
841 goto done;
844 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
845 err = got_error_prefix_errno("asprintf");
846 goto done;
849 err = got_opentemp_named(&tmppath, &f, path);
850 if (err) {
851 char *parent;
852 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
853 goto done;
854 err = got_path_dirname(&parent, path);
855 if (err)
856 goto done;
857 err = got_path_mkdir(parent);
858 free(parent);
859 if (err)
860 goto done;
861 err = got_opentemp_named(&tmppath, &f, path);
862 if (err)
863 goto done;
866 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
867 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
868 if (n != strlen(ref->ref.symref.ref) + 6) {
869 err = got_ferror(f, GOT_ERR_IO);
870 goto done;
872 } else {
873 char hex[SHA1_DIGEST_STRING_LENGTH];
874 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
875 sizeof(hex)) == NULL) {
876 err = got_error(GOT_ERR_BAD_REF_DATA);
877 goto done;
879 n = fprintf(f, "%s\n", hex);
880 if (n != sizeof(hex)) {
881 err = got_ferror(f, GOT_ERR_IO);
882 goto done;
886 if (ref->lf == NULL) {
887 err = got_lockfile_lock(&lf, path);
888 if (err)
889 goto done;
892 /* XXX: check if old content matches our expectations? */
894 if (stat(path, &sb) != 0) {
895 if (errno != ENOENT) {
896 err = got_error_prefix_errno2("stat", path);
897 goto done;
899 sb.st_mode = GOT_DEFAULT_FILE_MODE;
902 if (rename(tmppath, path) != 0) {
903 err = got_error_prefix_errno3("rename", tmppath, path);
904 goto done;
906 free(tmppath);
907 tmppath = NULL;
909 if (chmod(path, sb.st_mode) != 0) {
910 err = got_error_prefix_errno2("chmod", path);
911 goto done;
913 done:
914 if (ref->lf == NULL && lf)
915 unlock_err = got_lockfile_unlock(lf);
916 if (f) {
917 if (fclose(f) != 0 && err == NULL)
918 err = got_error_prefix_errno("fclose");
920 free(path_refs);
921 free(path);
922 if (tmppath) {
923 if (unlink(tmppath) != 0 && err == NULL)
924 err = got_error_prefix_errno2("unlink", tmppath);
925 free(tmppath);
927 return err ? err : unlock_err;
930 static const struct got_error *
931 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
933 const struct got_error *err = NULL, *unlock_err = NULL;
934 struct got_lockfile *lf = NULL;
935 FILE *f = NULL, *tmpf = NULL;
936 char *packed_refs_path, *tmppath = NULL;
937 struct got_reflist_head refs;
938 int found_delref = 0;
940 /* The packed-refs file does not cotain symbolic references. */
941 if (delref->flags & GOT_REF_IS_SYMBOLIC)
942 return got_error(GOT_ERR_BAD_REF_DATA);
944 SIMPLEQ_INIT(&refs);
946 packed_refs_path = got_repo_get_path_packed_refs(repo);
947 if (packed_refs_path == NULL)
948 return got_error_prefix_errno("got_repo_get_path_packed_refs");
950 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
951 if (err)
952 goto done;
954 if (delref->lf == NULL) {
955 err = got_lockfile_lock(&lf, packed_refs_path);
956 if (err)
957 goto done;
960 f = fopen(packed_refs_path, "r");
961 if (f == NULL) {
962 err = got_error_prefix_errno2("fopen", packed_refs_path);
963 goto done;
965 for (;;) {
966 char *line;
967 size_t len;
968 const char delim[3] = {'\0', '\0', '\0'};
969 struct got_reference *ref;
970 struct got_reflist_entry *new;
972 line = fparseln(f, &len, NULL, delim, 0);
973 if (line == NULL) {
974 if (feof(f))
975 break;
976 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
977 goto done;
979 err = parse_packed_ref_line(&ref, NULL, line);
980 free(line);
981 if (err)
982 goto done;
983 if (ref == NULL)
984 continue;
986 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
987 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
988 sizeof(delref->ref.ref.sha1)) == 0) {
989 found_delref = 1;
990 got_ref_close(ref);
991 continue;
994 err = insert_ref(&new, &refs, ref, repo);
995 if (err || new == NULL /* duplicate */)
996 got_ref_close(ref);
997 if (err)
998 goto done;
1001 if (found_delref) {
1002 struct got_reflist_entry *re;
1003 size_t n;
1004 struct stat sb;
1006 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1007 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1008 err = got_ferror(f, GOT_ERR_IO);
1009 goto done;
1012 SIMPLEQ_FOREACH(re, &refs, entry) {
1013 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1015 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1016 sizeof(hex)) == NULL) {
1017 err = got_error(GOT_ERR_BAD_REF_DATA);
1018 goto done;
1020 n = fprintf(tmpf, "%s ", hex);
1021 if (n != sizeof(hex)) {
1022 err = got_ferror(f, GOT_ERR_IO);
1023 goto done;
1025 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1026 if (n != strlen(re->ref->ref.ref.name) + 1) {
1027 err = got_ferror(f, GOT_ERR_IO);
1028 goto done;
1032 if (fflush(tmpf) != 0) {
1033 err = got_error_prefix_errno("fflush");
1034 goto done;
1037 if (stat(packed_refs_path, &sb) != 0) {
1038 if (errno != ENOENT) {
1039 err = got_error_prefix_errno2("stat",
1040 packed_refs_path);
1041 goto done;
1043 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1046 if (rename(tmppath, packed_refs_path) != 0) {
1047 err = got_error_prefix_errno3("rename", tmppath,
1048 packed_refs_path);
1049 goto done;
1052 if (chmod(packed_refs_path, sb.st_mode) != 0) {
1053 err = got_error_prefix_errno2("chmod",
1054 packed_refs_path);
1055 goto done;
1058 done:
1059 if (delref->lf == NULL && lf)
1060 unlock_err = got_lockfile_unlock(lf);
1061 if (f) {
1062 if (fclose(f) != 0 && err == NULL)
1063 err = got_error_prefix_errno("fclose");
1065 if (tmpf) {
1066 unlink(tmppath);
1067 if (fclose(tmpf) != 0 && err == NULL)
1068 err = got_error_prefix_errno("fclose");
1070 free(tmppath);
1071 free(packed_refs_path);
1072 got_ref_list_free(&refs);
1073 return err ? err : unlock_err;
1076 const struct got_error *
1077 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1079 const struct got_error *err = NULL, *unlock_err = NULL;
1080 const char *name = got_ref_get_name(ref);
1081 char *path_refs = NULL, *path = NULL;
1082 struct got_lockfile *lf = NULL;
1084 if (ref->flags & GOT_REF_IS_PACKED)
1085 return delete_packed_ref(ref, repo);
1087 path_refs = get_refs_dir_path(repo, name);
1088 if (path_refs == NULL) {
1089 err = got_error_prefix_errno2("get_refs_dir_path", name);
1090 goto done;
1093 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1094 err = got_error_prefix_errno("asprintf");
1095 goto done;
1098 if (ref->lf == NULL) {
1099 err = got_lockfile_lock(&lf, path);
1100 if (err)
1101 goto done;
1104 /* XXX: check if old content matches our expectations? */
1106 if (unlink(path) != 0)
1107 err = got_error_prefix_errno2("unlink", path);
1108 done:
1109 if (ref->lf == NULL && lf)
1110 unlock_err = got_lockfile_unlock(lf);
1112 free(path_refs);
1113 free(path);
1114 return err ? err : unlock_err;
1117 const struct got_error *
1118 got_ref_unlock(struct got_reference *ref)
1120 const struct got_error *err;
1121 err = got_lockfile_unlock(ref->lf);
1122 ref->lf = NULL;
1123 return err;