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_from_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_from_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_from_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_from_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_from_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 if (lock)
179 got_lockfile_unlock(lf);
180 return NULL;
183 line = fparseln(f, &len, NULL, delim, 0);
184 if (line == NULL) {
185 err = got_error(GOT_ERR_BAD_REF_DATA);
186 if (lock)
187 got_lockfile_unlock(lf);
188 goto done;
191 err = parse_ref_line(ref, name, line);
192 if (lock) {
193 if (err)
194 got_lockfile_unlock(lf);
195 else {
196 if (*ref)
197 (*ref)->lf = lf;
198 else
199 got_lockfile_unlock(lf);
202 done:
203 free(line);
204 if (fclose(f) != 0 && err == NULL) {
205 err = got_error_from_errno("fclose");
206 if (*ref) {
207 if (lock)
208 got_ref_unlock(*ref);
209 got_ref_close(*ref);
210 *ref = NULL;
213 return err;
216 static int
217 is_well_known_ref(const char *refname)
219 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
220 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
221 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
222 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
225 static char *
226 get_refs_dir_path(struct got_repository *repo, const char *refname)
228 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
229 return strdup(got_repo_get_path_git_dir(repo));
231 return got_repo_get_path_refs(repo);
234 static int
235 is_valid_ref_name(const char *name)
237 const char *s, *slash, *seg;
238 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
239 const char *forbidden_seq[] = { "//", "..", "@{" };
240 const char *lfs = GOT_LOCKFILE_SUFFIX;
241 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
242 int i;
244 if (name[0] == '@' && name[1] == '\0')
245 return 0;
247 slash = strchr(name, '/');
248 if (slash == NULL)
249 return 0;
251 s = name;
252 seg = s;
253 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
254 return 0;
255 while (*s) {
256 for (i = 0; i < nitems(forbidden); i++) {
257 if (*s == forbidden[i])
258 return 0;
260 for (i = 0; i < nitems(forbidden_seq); i++) {
261 if (s[0] == forbidden_seq[i][0] &&
262 s[1] == forbidden_seq[i][1])
263 return 0;
265 if (iscntrl((unsigned char)s[0]))
266 return 0;
267 if (s[0] == '.' && s[1] == '\0')
268 return 0;
269 if (*s == '/') {
270 const char *nextseg = s + 1;
271 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
272 nextseg[0] == '/')
273 return 0;
274 if (seg <= s - lfs_len &&
275 strncmp(s - lfs_len, lfs, lfs_len) == 0)
276 return 0;
277 seg = nextseg;
279 s++;
282 if (seg <= s - lfs_len &&
283 strncmp(s - lfs_len, lfs, lfs_len) == 0)
284 return 0;
286 return 1;
289 const struct got_error *
290 got_ref_alloc(struct got_reference **ref, const char *name,
291 struct got_object_id *id)
293 if (!is_valid_ref_name(name))
294 return got_error(GOT_ERR_BAD_REF_NAME);
296 return alloc_ref(ref, name, id, 0);
299 const struct got_error *
300 got_ref_alloc_symref(struct got_reference **ref, const char *name,
301 struct got_reference *target_ref)
303 if (!is_valid_ref_name(name))
304 return got_error(GOT_ERR_BAD_REF_NAME);
306 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
309 static const struct got_error *
310 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
311 const char *line)
313 struct got_object_id id;
314 const char *name;
316 *ref = NULL;
318 if (line[0] == '#' || line[0] == '^')
319 return NULL;
321 if (!got_parse_sha1_digest(id.sha1, line))
322 return got_error(GOT_ERR_BAD_REF_DATA);
324 if (abs_refname) {
325 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
326 return NULL;
327 name = abs_refname;
328 } else
329 name = line + SHA1_DIGEST_STRING_LENGTH;
331 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
334 static const struct got_error *
335 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
336 int nsubdirs, const char *refname)
338 const struct got_error *err = NULL;
339 char *abs_refname;
340 char *line;
341 size_t len;
342 const char delim[3] = {'\0', '\0', '\0'};
343 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
345 *ref = NULL;
347 if (ref_is_absolute)
348 abs_refname = (char *)refname;
349 do {
350 line = fparseln(f, &len, NULL, delim, 0);
351 if (line == NULL) {
352 if (feof(f))
353 break;
354 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
355 break;
357 for (i = 0; i < nsubdirs; i++) {
358 if (!ref_is_absolute &&
359 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
360 refname) == -1)
361 return got_error_from_errno("asprintf");
362 err = parse_packed_ref_line(ref, abs_refname, line);
363 if (!ref_is_absolute)
364 free(abs_refname);
365 if (err || *ref != NULL)
366 break;
368 free(line);
369 if (err)
370 break;
371 } while (*ref == NULL);
373 return err;
376 static const struct got_error *
377 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
378 const char *name, int lock)
380 const struct got_error *err = NULL;
381 char *path = NULL;
382 char *normpath = NULL;
383 char *absname = NULL;
384 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
385 int ref_is_well_known = is_well_known_ref(name);
387 *ref = NULL;
389 if (ref_is_absolute || ref_is_well_known) {
390 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
391 return got_error_from_errno("asprintf");
392 absname = (char *)name;
393 } else {
394 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
395 subdir[0] ? "/" : "", name) == -1)
396 return got_error_from_errno("asprintf");
398 if (asprintf(&absname, "refs/%s%s%s",
399 subdir, subdir[0] ? "/" : "", name) == -1) {
400 err = got_error_from_errno("asprintf");
401 goto done;
405 normpath = got_path_normalize(path);
406 if (normpath == NULL) {
407 if (errno == ENOENT)
408 err = NULL;
409 else
410 err = got_error_from_errno2("got_path_normalize", path);
411 goto done;
414 err = parse_ref_file(ref, absname, normpath, lock);
415 done:
416 if (!ref_is_absolute && !ref_is_well_known)
417 free(absname);
418 free(path);
419 free(normpath);
420 return err;
423 const struct got_error *
424 got_ref_open(struct got_reference **ref, struct got_repository *repo,
425 const char *refname, int lock)
427 const struct got_error *err = NULL;
428 char *path_refs = NULL;
429 const char *subdirs[] = {
430 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
431 };
432 int i, well_known = is_well_known_ref(refname);
433 struct got_lockfile *lf = NULL;
435 *ref = NULL;
437 path_refs = get_refs_dir_path(repo, refname);
438 if (path_refs == NULL) {
439 err = got_error_from_errno2("get_refs_dir_path", refname);
440 goto done;
443 if (well_known) {
444 err = open_ref(ref, path_refs, "", refname, lock);
445 } else {
446 char *packed_refs_path;
447 FILE *f;
449 /* Search on-disk refs before packed refs! */
450 for (i = 0; i < nitems(subdirs); i++) {
451 err = open_ref(ref, path_refs, subdirs[i], refname,
452 lock);
453 if (err || *ref)
454 goto done;
457 packed_refs_path = got_repo_get_path_packed_refs(repo);
458 if (packed_refs_path == NULL) {
459 err = got_error_from_errno(
460 "got_repo_get_path_packed_refs");
461 goto done;
464 if (lock) {
465 err = got_lockfile_lock(&lf, packed_refs_path);
466 if (err)
467 goto done;
469 f = fopen(packed_refs_path, "rb");
470 free(packed_refs_path);
471 if (f != NULL) {
472 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
473 refname);
474 if (!err) {
475 if (fclose(f) != 0) {
476 err = got_error_from_errno("fclose");
477 got_ref_close(*ref);
478 *ref = NULL;
479 } else if (*ref)
480 (*ref)->lf = lf;
484 done:
485 if (!err && *ref == NULL)
486 err = got_error_not_ref(refname);
487 if (err && lf)
488 got_lockfile_unlock(lf);
489 free(path_refs);
490 return err;
493 void
494 got_ref_close(struct got_reference *ref)
496 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
497 free(ref->ref.symref.name);
498 free(ref->ref.symref.ref);
499 } else
500 free(ref->ref.ref.name);
501 free(ref);
504 struct got_reference *
505 got_ref_dup(struct got_reference *ref)
507 struct got_reference *ret;
509 ret = calloc(1, sizeof(*ret));
510 if (ret == NULL)
511 return NULL;
513 ret->flags = ref->flags;
514 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
515 ret->ref.symref.name = strdup(ref->ref.symref.name);
516 if (ret->ref.symref.name == NULL) {
517 free(ret);
518 return NULL;
520 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
521 if (ret->ref.symref.ref == NULL) {
522 free(ret->ref.symref.name);
523 free(ret);
524 return NULL;
526 } else {
527 ref->ref.ref.name = strdup(ref->ref.ref.name);
528 if (ref->ref.ref.name == NULL) {
529 free(ret);
530 return NULL;
532 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
533 sizeof(ret->ref.ref.sha1));
536 return ret;
539 static const struct got_error *
540 resolve_symbolic_ref(struct got_reference **resolved,
541 struct got_repository *repo, struct got_reference *ref)
543 struct got_reference *nextref;
544 const struct got_error *err;
546 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
547 if (err)
548 return err;
550 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
551 err = resolve_symbolic_ref(resolved, repo, nextref);
552 else
553 *resolved = got_ref_dup(nextref);
555 got_ref_close(nextref);
556 return err;
559 const struct got_error *
560 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
561 struct got_reference *ref)
563 const struct got_error *err;
565 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
566 struct got_reference *resolved = NULL;
567 err = resolve_symbolic_ref(&resolved, repo, ref);
568 if (err == NULL)
569 err = got_ref_resolve(id, repo, resolved);
570 if (resolved)
571 got_ref_close(resolved);
572 return err;
575 *id = calloc(1, sizeof(**id));
576 if (*id == NULL)
577 return got_error_from_errno("calloc");
578 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
579 return NULL;
582 char *
583 got_ref_to_str(struct got_reference *ref)
585 char *str;
587 if (ref->flags & GOT_REF_IS_SYMBOLIC)
588 return strdup(ref->ref.symref.ref);
590 str = malloc(SHA1_DIGEST_STRING_LENGTH);
591 if (str == NULL)
592 return NULL;
594 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
595 SHA1_DIGEST_STRING_LENGTH) == NULL) {
596 free(str);
597 return NULL;
600 return str;
603 const char *
604 got_ref_get_name(struct got_reference *ref)
606 if (ref->flags & GOT_REF_IS_SYMBOLIC)
607 return ref->ref.symref.name;
609 return ref->ref.ref.name;
612 const char *
613 got_ref_get_symref_target(struct got_reference *ref)
615 if (ref->flags & GOT_REF_IS_SYMBOLIC)
616 return ref->ref.symref.ref;
618 return NULL;
621 static const struct got_error *
622 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
623 struct got_reference *ref, struct got_repository *repo)
625 const struct got_error *err;
626 struct got_object_id *id;
627 struct got_reflist_entry *new, *re, *prev = NULL;
628 int cmp;
630 *newp = NULL;
632 err = got_ref_resolve(&id, repo, ref);
633 if (err)
634 return err;
636 new = malloc(sizeof(*new));
637 if (new == NULL) {
638 free(id);
639 return got_error_from_errno("malloc");
641 new->ref = ref;
642 new->id = id;
643 *newp = new;
645 /*
646 * We must de-duplicate entries on insert because packed-refs may
647 * contain redundant entries. On-disk refs take precedence.
648 * This code assumes that on-disk revs are read before packed-refs.
649 * We're iterating the list anyway, so insert elements sorted by name.
650 */
651 re = SIMPLEQ_FIRST(refs);
652 while (re) {
653 cmp = got_path_cmp(got_ref_get_name(re->ref),
654 got_ref_get_name(new->ref));
655 if (cmp == 0) {
656 /* duplicate */
657 free(new->id);
658 free(new);
659 *newp = NULL;
660 return NULL;
661 } else if (cmp > 0) {
662 if (prev)
663 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
664 else
665 SIMPLEQ_INSERT_HEAD(refs, new, entry);
666 return NULL;
667 } else {
668 prev = re;
669 re = SIMPLEQ_NEXT(re, entry);
673 SIMPLEQ_INSERT_TAIL(refs, new, entry);
674 return NULL;
677 static const struct got_error *
678 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
679 const char *subdir, struct got_repository *repo)
681 const struct got_error *err = NULL;
682 DIR *d = NULL;
683 char *path_subdir;
685 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
686 return got_error_from_errno("asprintf");
688 d = opendir(path_subdir);
689 if (d == NULL)
690 goto done;
692 for (;;) {
693 struct dirent *dent;
694 struct got_reference *ref;
695 char *child;
697 dent = readdir(d);
698 if (dent == NULL)
699 break;
701 if (strcmp(dent->d_name, ".") == 0 ||
702 strcmp(dent->d_name, "..") == 0)
703 continue;
705 switch (dent->d_type) {
706 case DT_REG:
707 err = open_ref(&ref, path_refs, subdir, dent->d_name,
708 0);
709 if (err)
710 goto done;
711 if (ref) {
712 struct got_reflist_entry *new;
713 err = insert_ref(&new, refs, ref, repo);
714 if (err || new == NULL /* duplicate */)
715 got_ref_close(ref);
716 if (err)
717 goto done;
719 break;
720 case DT_DIR:
721 if (asprintf(&child, "%s%s%s", subdir,
722 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
723 err = got_error_from_errno("asprintf");
724 break;
726 err = gather_on_disk_refs(refs, path_refs, child, repo);
727 free(child);
728 break;
729 default:
730 break;
733 done:
734 if (d)
735 closedir(d);
736 free(path_subdir);
737 return err;
740 const struct got_error *
741 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
743 const struct got_error *err;
744 char *packed_refs_path, *path_refs = NULL;
745 FILE *f = NULL;
746 struct got_reference *ref;
747 struct got_reflist_entry *new;
749 /* HEAD ref should always exist. */
750 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
751 if (path_refs == NULL) {
752 err = got_error_from_errno("get_refs_dir_path");
753 goto done;
755 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
756 if (err)
757 goto done;
758 err = insert_ref(&new, refs, ref, repo);
759 if (err || new == NULL /* duplicate */)
760 got_ref_close(ref);
761 if (err)
762 goto done;
764 /* Gather on-disk refs before parsing packed-refs. */
765 free(path_refs);
766 path_refs = get_refs_dir_path(repo, "");
767 if (path_refs == NULL) {
768 err = got_error_from_errno("get_refs_dir_path");
769 goto done;
771 err = gather_on_disk_refs(refs, path_refs, "", repo);
772 if (err)
773 goto done;
775 /*
776 * The packed-refs file may contain redundant entries, in which
777 * case on-disk refs take precedence.
778 */
779 packed_refs_path = got_repo_get_path_packed_refs(repo);
780 if (packed_refs_path == NULL) {
781 err = got_error_from_errno("got_repo_get_path_packed_refs");
782 goto done;
785 f = fopen(packed_refs_path, "r");
786 free(packed_refs_path);
787 if (f) {
788 char *line;
789 size_t len;
790 const char delim[3] = {'\0', '\0', '\0'};
791 for (;;) {
792 line = fparseln(f, &len, NULL, delim, 0);
793 if (line == NULL) {
794 if (feof(f))
795 break;
796 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
797 goto done;
799 err = parse_packed_ref_line(&ref, NULL, line);
800 free(line);
801 if (err)
802 goto done;
803 if (ref) {
804 err = insert_ref(&new, refs, ref, repo);
805 if (err || new == NULL /* duplicate */)
806 got_ref_close(ref);
807 if (err)
808 goto done;
812 done:
813 free(path_refs);
814 if (f && fclose(f) != 0 && err == NULL)
815 err = got_error_from_errno("fclose");
816 return err;
819 void
820 got_ref_list_free(struct got_reflist_head *refs)
822 struct got_reflist_entry *re;
824 while (!SIMPLEQ_EMPTY(refs)) {
825 re = SIMPLEQ_FIRST(refs);
826 SIMPLEQ_REMOVE_HEAD(refs, entry);
827 got_ref_close(re->ref);
828 free(re->id);
829 free(re);
834 int
835 got_ref_is_symbolic(struct got_reference *ref)
837 return (ref->flags & GOT_REF_IS_SYMBOLIC);
840 const struct got_error *
841 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
843 if (ref->flags & GOT_REF_IS_SYMBOLIC)
844 return got_error(GOT_ERR_BAD_REF_TYPE);
846 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
847 return NULL;
850 const struct got_error *
851 got_ref_change_symref(struct got_reference *ref, char *refname)
853 char *new_name;
855 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
856 return got_error(GOT_ERR_BAD_REF_TYPE);
858 new_name = strdup(refname);
859 if (new_name == NULL)
860 return got_error_from_errno("strdup");
862 free(ref->ref.symref.name);
863 ref->ref.symref.name = new_name;
864 return NULL;
867 const struct got_error *
868 got_ref_write(struct got_reference *ref, struct got_repository *repo)
870 const struct got_error *err = NULL, *unlock_err = NULL;
871 const char *name = got_ref_get_name(ref);
872 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
873 struct got_lockfile *lf = NULL;
874 FILE *f = NULL;
875 size_t n;
876 struct stat sb;
878 path_refs = get_refs_dir_path(repo, name);
879 if (path_refs == NULL) {
880 err = got_error_from_errno2("get_refs_dir_path", name);
881 goto done;
884 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
885 err = got_error_from_errno("asprintf");
886 goto done;
889 err = got_opentemp_named(&tmppath, &f, path);
890 if (err) {
891 char *parent;
892 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
893 goto done;
894 err = got_path_dirname(&parent, path);
895 if (err)
896 goto done;
897 err = got_path_mkdir(parent);
898 free(parent);
899 if (err)
900 goto done;
901 err = got_opentemp_named(&tmppath, &f, path);
902 if (err)
903 goto done;
906 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
907 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
908 if (n != strlen(ref->ref.symref.ref) + 6) {
909 err = got_ferror(f, GOT_ERR_IO);
910 goto done;
912 } else {
913 char hex[SHA1_DIGEST_STRING_LENGTH];
914 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
915 sizeof(hex)) == NULL) {
916 err = got_error(GOT_ERR_BAD_REF_DATA);
917 goto done;
919 n = fprintf(f, "%s\n", hex);
920 if (n != sizeof(hex)) {
921 err = got_ferror(f, GOT_ERR_IO);
922 goto done;
926 if (ref->lf == NULL) {
927 err = got_lockfile_lock(&lf, path);
928 if (err)
929 goto done;
932 /* XXX: check if old content matches our expectations? */
934 if (stat(path, &sb) != 0) {
935 if (errno != ENOENT) {
936 err = got_error_from_errno2("stat", path);
937 goto done;
939 sb.st_mode = GOT_DEFAULT_FILE_MODE;
942 if (rename(tmppath, path) != 0) {
943 err = got_error_from_errno3("rename", tmppath, path);
944 goto done;
946 free(tmppath);
947 tmppath = NULL;
949 if (chmod(path, sb.st_mode) != 0) {
950 err = got_error_from_errno2("chmod", path);
951 goto done;
953 done:
954 if (ref->lf == NULL && lf)
955 unlock_err = got_lockfile_unlock(lf);
956 if (f) {
957 if (fclose(f) != 0 && err == NULL)
958 err = got_error_from_errno("fclose");
960 free(path_refs);
961 free(path);
962 if (tmppath) {
963 if (unlink(tmppath) != 0 && err == NULL)
964 err = got_error_from_errno2("unlink", tmppath);
965 free(tmppath);
967 return err ? err : unlock_err;
970 static const struct got_error *
971 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
973 const struct got_error *err = NULL, *unlock_err = NULL;
974 struct got_lockfile *lf = NULL;
975 FILE *f = NULL, *tmpf = NULL;
976 char *packed_refs_path, *tmppath = NULL;
977 struct got_reflist_head refs;
978 int found_delref = 0;
980 /* The packed-refs file does not cotain symbolic references. */
981 if (delref->flags & GOT_REF_IS_SYMBOLIC)
982 return got_error(GOT_ERR_BAD_REF_DATA);
984 SIMPLEQ_INIT(&refs);
986 packed_refs_path = got_repo_get_path_packed_refs(repo);
987 if (packed_refs_path == NULL)
988 return got_error_from_errno("got_repo_get_path_packed_refs");
990 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
991 if (err)
992 goto done;
994 if (delref->lf == NULL) {
995 err = got_lockfile_lock(&lf, packed_refs_path);
996 if (err)
997 goto done;
1000 f = fopen(packed_refs_path, "r");
1001 if (f == NULL) {
1002 err = got_error_from_errno2("fopen", packed_refs_path);
1003 goto done;
1005 for (;;) {
1006 char *line;
1007 size_t len;
1008 const char delim[3] = {'\0', '\0', '\0'};
1009 struct got_reference *ref;
1010 struct got_reflist_entry *new;
1012 line = fparseln(f, &len, NULL, delim, 0);
1013 if (line == NULL) {
1014 if (feof(f))
1015 break;
1016 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1017 goto done;
1019 err = parse_packed_ref_line(&ref, NULL, line);
1020 free(line);
1021 if (err)
1022 goto done;
1023 if (ref == NULL)
1024 continue;
1026 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1027 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1028 sizeof(delref->ref.ref.sha1)) == 0) {
1029 found_delref = 1;
1030 got_ref_close(ref);
1031 continue;
1034 err = insert_ref(&new, &refs, ref, repo);
1035 if (err || new == NULL /* duplicate */)
1036 got_ref_close(ref);
1037 if (err)
1038 goto done;
1041 if (found_delref) {
1042 struct got_reflist_entry *re;
1043 size_t n;
1044 struct stat sb;
1046 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1047 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1048 err = got_ferror(f, GOT_ERR_IO);
1049 goto done;
1052 SIMPLEQ_FOREACH(re, &refs, entry) {
1053 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1055 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1056 sizeof(hex)) == NULL) {
1057 err = got_error(GOT_ERR_BAD_REF_DATA);
1058 goto done;
1060 n = fprintf(tmpf, "%s ", hex);
1061 if (n != sizeof(hex)) {
1062 err = got_ferror(f, GOT_ERR_IO);
1063 goto done;
1065 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1066 if (n != strlen(re->ref->ref.ref.name) + 1) {
1067 err = got_ferror(f, GOT_ERR_IO);
1068 goto done;
1072 if (fflush(tmpf) != 0) {
1073 err = got_error_from_errno("fflush");
1074 goto done;
1077 if (stat(packed_refs_path, &sb) != 0) {
1078 if (errno != ENOENT) {
1079 err = got_error_from_errno2("stat",
1080 packed_refs_path);
1081 goto done;
1083 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1086 if (rename(tmppath, packed_refs_path) != 0) {
1087 err = got_error_from_errno3("rename", tmppath,
1088 packed_refs_path);
1089 goto done;
1092 if (chmod(packed_refs_path, sb.st_mode) != 0) {
1093 err = got_error_from_errno2("chmod",
1094 packed_refs_path);
1095 goto done;
1098 done:
1099 if (delref->lf == NULL && lf)
1100 unlock_err = got_lockfile_unlock(lf);
1101 if (f) {
1102 if (fclose(f) != 0 && err == NULL)
1103 err = got_error_from_errno("fclose");
1105 if (tmpf) {
1106 unlink(tmppath);
1107 if (fclose(tmpf) != 0 && err == NULL)
1108 err = got_error_from_errno("fclose");
1110 free(tmppath);
1111 free(packed_refs_path);
1112 got_ref_list_free(&refs);
1113 return err ? err : unlock_err;
1116 const struct got_error *
1117 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1119 const struct got_error *err = NULL, *unlock_err = NULL;
1120 const char *name = got_ref_get_name(ref);
1121 char *path_refs = NULL, *path = NULL;
1122 struct got_lockfile *lf = NULL;
1124 if (ref->flags & GOT_REF_IS_PACKED)
1125 return delete_packed_ref(ref, repo);
1127 path_refs = get_refs_dir_path(repo, name);
1128 if (path_refs == NULL) {
1129 err = got_error_from_errno2("get_refs_dir_path", name);
1130 goto done;
1133 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1134 err = got_error_from_errno("asprintf");
1135 goto done;
1138 if (ref->lf == NULL) {
1139 err = got_lockfile_lock(&lf, path);
1140 if (err)
1141 goto done;
1144 /* XXX: check if old content matches our expectations? */
1146 if (unlink(path) != 0)
1147 err = got_error_from_errno2("unlink", path);
1148 done:
1149 if (ref->lf == NULL && lf)
1150 unlock_err = got_lockfile_unlock(lf);
1152 free(path_refs);
1153 free(path);
1154 return err ? err : unlock_err;
1157 const struct got_error *
1158 got_ref_unlock(struct got_reference *ref)
1160 const struct got_error *err;
1161 err = got_lockfile_unlock(ref->lf);
1162 ref->lf = NULL;
1163 return err;