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 *absname = NULL;
383 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
384 int ref_is_well_known = is_well_known_ref(name);
386 *ref = NULL;
388 if (ref_is_absolute || ref_is_well_known) {
389 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
390 return got_error_from_errno("asprintf");
391 absname = (char *)name;
392 } else {
393 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
394 subdir[0] ? "/" : "", name) == -1)
395 return got_error_from_errno("asprintf");
397 if (asprintf(&absname, "refs/%s%s%s",
398 subdir, subdir[0] ? "/" : "", name) == -1) {
399 err = got_error_from_errno("asprintf");
400 goto done;
404 err = parse_ref_file(ref, absname, path, lock);
405 done:
406 if (!ref_is_absolute && !ref_is_well_known)
407 free(absname);
408 free(path);
409 return err;
412 const struct got_error *
413 got_ref_open(struct got_reference **ref, struct got_repository *repo,
414 const char *refname, int lock)
416 const struct got_error *err = NULL;
417 char *path_refs = NULL;
418 const char *subdirs[] = {
419 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
420 };
421 int i, well_known = is_well_known_ref(refname);
422 struct got_lockfile *lf = NULL;
424 *ref = NULL;
426 path_refs = get_refs_dir_path(repo, refname);
427 if (path_refs == NULL) {
428 err = got_error_from_errno2("get_refs_dir_path", refname);
429 goto done;
432 if (well_known) {
433 err = open_ref(ref, path_refs, "", refname, lock);
434 } else {
435 char *packed_refs_path;
436 FILE *f;
438 /* Search on-disk refs before packed refs! */
439 for (i = 0; i < nitems(subdirs); i++) {
440 err = open_ref(ref, path_refs, subdirs[i], refname,
441 lock);
442 if (err || *ref)
443 goto done;
446 packed_refs_path = got_repo_get_path_packed_refs(repo);
447 if (packed_refs_path == NULL) {
448 err = got_error_from_errno(
449 "got_repo_get_path_packed_refs");
450 goto done;
453 if (lock) {
454 err = got_lockfile_lock(&lf, packed_refs_path);
455 if (err)
456 goto done;
458 f = fopen(packed_refs_path, "rb");
459 free(packed_refs_path);
460 if (f != NULL) {
461 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
462 refname);
463 if (!err) {
464 if (fclose(f) != 0) {
465 err = got_error_from_errno("fclose");
466 got_ref_close(*ref);
467 *ref = NULL;
468 } else if (*ref)
469 (*ref)->lf = lf;
473 done:
474 if (!err && *ref == NULL)
475 err = got_error_not_ref(refname);
476 if (err && lf)
477 got_lockfile_unlock(lf);
478 free(path_refs);
479 return err;
482 void
483 got_ref_close(struct got_reference *ref)
485 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
486 free(ref->ref.symref.name);
487 free(ref->ref.symref.ref);
488 } else
489 free(ref->ref.ref.name);
490 free(ref);
493 struct got_reference *
494 got_ref_dup(struct got_reference *ref)
496 struct got_reference *ret;
498 ret = calloc(1, sizeof(*ret));
499 if (ret == NULL)
500 return NULL;
502 ret->flags = ref->flags;
503 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
504 ret->ref.symref.name = strdup(ref->ref.symref.name);
505 if (ret->ref.symref.name == NULL) {
506 free(ret);
507 return NULL;
509 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
510 if (ret->ref.symref.ref == NULL) {
511 free(ret->ref.symref.name);
512 free(ret);
513 return NULL;
515 } else {
516 ref->ref.ref.name = strdup(ref->ref.ref.name);
517 if (ref->ref.ref.name == NULL) {
518 free(ret);
519 return NULL;
521 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
522 sizeof(ret->ref.ref.sha1));
525 return ret;
528 static const struct got_error *
529 resolve_symbolic_ref(struct got_reference **resolved,
530 struct got_repository *repo, struct got_reference *ref)
532 struct got_reference *nextref;
533 const struct got_error *err;
535 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
536 if (err)
537 return err;
539 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
540 err = resolve_symbolic_ref(resolved, repo, nextref);
541 else
542 *resolved = got_ref_dup(nextref);
544 got_ref_close(nextref);
545 return err;
548 const struct got_error *
549 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
550 struct got_reference *ref)
552 const struct got_error *err;
554 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
555 struct got_reference *resolved = NULL;
556 err = resolve_symbolic_ref(&resolved, repo, ref);
557 if (err == NULL)
558 err = got_ref_resolve(id, repo, resolved);
559 if (resolved)
560 got_ref_close(resolved);
561 return err;
564 *id = calloc(1, sizeof(**id));
565 if (*id == NULL)
566 return got_error_from_errno("calloc");
567 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
568 return NULL;
571 char *
572 got_ref_to_str(struct got_reference *ref)
574 char *str;
576 if (ref->flags & GOT_REF_IS_SYMBOLIC)
577 return strdup(ref->ref.symref.ref);
579 str = malloc(SHA1_DIGEST_STRING_LENGTH);
580 if (str == NULL)
581 return NULL;
583 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
584 SHA1_DIGEST_STRING_LENGTH) == NULL) {
585 free(str);
586 return NULL;
589 return str;
592 const char *
593 got_ref_get_name(struct got_reference *ref)
595 if (ref->flags & GOT_REF_IS_SYMBOLIC)
596 return ref->ref.symref.name;
598 return ref->ref.ref.name;
601 const char *
602 got_ref_get_symref_target(struct got_reference *ref)
604 if (ref->flags & GOT_REF_IS_SYMBOLIC)
605 return ref->ref.symref.ref;
607 return NULL;
610 static const struct got_error *
611 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
612 struct got_reference *ref, struct got_repository *repo)
614 const struct got_error *err;
615 struct got_object_id *id;
616 struct got_reflist_entry *new, *re, *prev = NULL;
617 int cmp;
619 *newp = NULL;
621 err = got_ref_resolve(&id, repo, ref);
622 if (err)
623 return err;
625 new = malloc(sizeof(*new));
626 if (new == NULL) {
627 free(id);
628 return got_error_from_errno("malloc");
630 new->ref = ref;
631 new->id = id;
632 *newp = new;
634 /*
635 * We must de-duplicate entries on insert because packed-refs may
636 * contain redundant entries. On-disk refs take precedence.
637 * This code assumes that on-disk revs are read before packed-refs.
638 * We're iterating the list anyway, so insert elements sorted by name.
639 */
640 re = SIMPLEQ_FIRST(refs);
641 while (re) {
642 cmp = got_path_cmp(got_ref_get_name(re->ref),
643 got_ref_get_name(new->ref));
644 if (cmp == 0) {
645 /* duplicate */
646 free(new->id);
647 free(new);
648 *newp = NULL;
649 return NULL;
650 } else if (cmp > 0) {
651 if (prev)
652 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
653 else
654 SIMPLEQ_INSERT_HEAD(refs, new, entry);
655 return NULL;
656 } else {
657 prev = re;
658 re = SIMPLEQ_NEXT(re, entry);
662 SIMPLEQ_INSERT_TAIL(refs, new, entry);
663 return NULL;
666 static const struct got_error *
667 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
668 const char *subdir, struct got_repository *repo)
670 const struct got_error *err = NULL;
671 DIR *d = NULL;
672 char *path_subdir;
674 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
675 return got_error_from_errno("asprintf");
677 d = opendir(path_subdir);
678 if (d == NULL)
679 goto done;
681 for (;;) {
682 struct dirent *dent;
683 struct got_reference *ref;
684 char *child;
686 dent = readdir(d);
687 if (dent == NULL)
688 break;
690 if (strcmp(dent->d_name, ".") == 0 ||
691 strcmp(dent->d_name, "..") == 0)
692 continue;
694 switch (dent->d_type) {
695 case DT_REG:
696 err = open_ref(&ref, path_refs, subdir, dent->d_name,
697 0);
698 if (err)
699 goto done;
700 if (ref) {
701 struct got_reflist_entry *new;
702 err = insert_ref(&new, refs, ref, repo);
703 if (err || new == NULL /* duplicate */)
704 got_ref_close(ref);
705 if (err)
706 goto done;
708 break;
709 case DT_DIR:
710 if (asprintf(&child, "%s%s%s", subdir,
711 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
712 err = got_error_from_errno("asprintf");
713 break;
715 err = gather_on_disk_refs(refs, path_refs, child, repo);
716 free(child);
717 break;
718 default:
719 break;
722 done:
723 if (d)
724 closedir(d);
725 free(path_subdir);
726 return err;
729 const struct got_error *
730 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
732 const struct got_error *err;
733 char *packed_refs_path, *path_refs = NULL;
734 FILE *f = NULL;
735 struct got_reference *ref;
736 struct got_reflist_entry *new;
738 /* HEAD ref should always exist. */
739 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
740 if (path_refs == NULL) {
741 err = got_error_from_errno("get_refs_dir_path");
742 goto done;
744 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
745 if (err)
746 goto done;
747 err = insert_ref(&new, refs, ref, repo);
748 if (err || new == NULL /* duplicate */)
749 got_ref_close(ref);
750 if (err)
751 goto done;
753 /* Gather on-disk refs before parsing packed-refs. */
754 free(path_refs);
755 path_refs = get_refs_dir_path(repo, "");
756 if (path_refs == NULL) {
757 err = got_error_from_errno("get_refs_dir_path");
758 goto done;
760 err = gather_on_disk_refs(refs, path_refs, "", repo);
761 if (err)
762 goto done;
764 /*
765 * The packed-refs file may contain redundant entries, in which
766 * case on-disk refs take precedence.
767 */
768 packed_refs_path = got_repo_get_path_packed_refs(repo);
769 if (packed_refs_path == NULL) {
770 err = got_error_from_errno("got_repo_get_path_packed_refs");
771 goto done;
774 f = fopen(packed_refs_path, "r");
775 free(packed_refs_path);
776 if (f) {
777 char *line;
778 size_t len;
779 const char delim[3] = {'\0', '\0', '\0'};
780 for (;;) {
781 line = fparseln(f, &len, NULL, delim, 0);
782 if (line == NULL) {
783 if (feof(f))
784 break;
785 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
786 goto done;
788 err = parse_packed_ref_line(&ref, NULL, line);
789 free(line);
790 if (err)
791 goto done;
792 if (ref) {
793 err = insert_ref(&new, refs, ref, repo);
794 if (err || new == NULL /* duplicate */)
795 got_ref_close(ref);
796 if (err)
797 goto done;
801 done:
802 free(path_refs);
803 if (f && fclose(f) != 0 && err == NULL)
804 err = got_error_from_errno("fclose");
805 return err;
808 void
809 got_ref_list_free(struct got_reflist_head *refs)
811 struct got_reflist_entry *re;
813 while (!SIMPLEQ_EMPTY(refs)) {
814 re = SIMPLEQ_FIRST(refs);
815 SIMPLEQ_REMOVE_HEAD(refs, entry);
816 got_ref_close(re->ref);
817 free(re->id);
818 free(re);
823 int
824 got_ref_is_symbolic(struct got_reference *ref)
826 return (ref->flags & GOT_REF_IS_SYMBOLIC);
829 const struct got_error *
830 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
832 if (ref->flags & GOT_REF_IS_SYMBOLIC)
833 return got_error(GOT_ERR_BAD_REF_TYPE);
835 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
836 return NULL;
839 const struct got_error *
840 got_ref_change_symref(struct got_reference *ref, char *refname)
842 char *new_name;
844 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
845 return got_error(GOT_ERR_BAD_REF_TYPE);
847 new_name = strdup(refname);
848 if (new_name == NULL)
849 return got_error_from_errno("strdup");
851 free(ref->ref.symref.name);
852 ref->ref.symref.name = new_name;
853 return NULL;
856 const struct got_error *
857 got_ref_write(struct got_reference *ref, struct got_repository *repo)
859 const struct got_error *err = NULL, *unlock_err = NULL;
860 const char *name = got_ref_get_name(ref);
861 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
862 struct got_lockfile *lf = NULL;
863 FILE *f = NULL;
864 size_t n;
865 struct stat sb;
867 path_refs = get_refs_dir_path(repo, name);
868 if (path_refs == NULL) {
869 err = got_error_from_errno2("get_refs_dir_path", name);
870 goto done;
873 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
874 err = got_error_from_errno("asprintf");
875 goto done;
878 err = got_opentemp_named(&tmppath, &f, path);
879 if (err) {
880 char *parent;
881 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
882 goto done;
883 err = got_path_dirname(&parent, path);
884 if (err)
885 goto done;
886 err = got_path_mkdir(parent);
887 free(parent);
888 if (err)
889 goto done;
890 err = got_opentemp_named(&tmppath, &f, path);
891 if (err)
892 goto done;
895 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
896 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
897 if (n != strlen(ref->ref.symref.ref) + 6) {
898 err = got_ferror(f, GOT_ERR_IO);
899 goto done;
901 } else {
902 char hex[SHA1_DIGEST_STRING_LENGTH];
903 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
904 sizeof(hex)) == NULL) {
905 err = got_error(GOT_ERR_BAD_REF_DATA);
906 goto done;
908 n = fprintf(f, "%s\n", hex);
909 if (n != sizeof(hex)) {
910 err = got_ferror(f, GOT_ERR_IO);
911 goto done;
915 if (ref->lf == NULL) {
916 err = got_lockfile_lock(&lf, path);
917 if (err)
918 goto done;
921 /* XXX: check if old content matches our expectations? */
923 if (stat(path, &sb) != 0) {
924 if (errno != ENOENT) {
925 err = got_error_from_errno2("stat", path);
926 goto done;
928 sb.st_mode = GOT_DEFAULT_FILE_MODE;
931 if (rename(tmppath, path) != 0) {
932 err = got_error_from_errno3("rename", tmppath, path);
933 goto done;
935 free(tmppath);
936 tmppath = NULL;
938 if (chmod(path, sb.st_mode) != 0) {
939 err = got_error_from_errno2("chmod", path);
940 goto done;
942 done:
943 if (ref->lf == NULL && lf)
944 unlock_err = got_lockfile_unlock(lf);
945 if (f) {
946 if (fclose(f) != 0 && err == NULL)
947 err = got_error_from_errno("fclose");
949 free(path_refs);
950 free(path);
951 if (tmppath) {
952 if (unlink(tmppath) != 0 && err == NULL)
953 err = got_error_from_errno2("unlink", tmppath);
954 free(tmppath);
956 return err ? err : unlock_err;
959 static const struct got_error *
960 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
962 const struct got_error *err = NULL, *unlock_err = NULL;
963 struct got_lockfile *lf = NULL;
964 FILE *f = NULL, *tmpf = NULL;
965 char *packed_refs_path, *tmppath = NULL;
966 struct got_reflist_head refs;
967 int found_delref = 0;
969 /* The packed-refs file does not cotain symbolic references. */
970 if (delref->flags & GOT_REF_IS_SYMBOLIC)
971 return got_error(GOT_ERR_BAD_REF_DATA);
973 SIMPLEQ_INIT(&refs);
975 packed_refs_path = got_repo_get_path_packed_refs(repo);
976 if (packed_refs_path == NULL)
977 return got_error_from_errno("got_repo_get_path_packed_refs");
979 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
980 if (err)
981 goto done;
983 if (delref->lf == NULL) {
984 err = got_lockfile_lock(&lf, packed_refs_path);
985 if (err)
986 goto done;
989 f = fopen(packed_refs_path, "r");
990 if (f == NULL) {
991 err = got_error_from_errno2("fopen", packed_refs_path);
992 goto done;
994 for (;;) {
995 char *line;
996 size_t len;
997 const char delim[3] = {'\0', '\0', '\0'};
998 struct got_reference *ref;
999 struct got_reflist_entry *new;
1001 line = fparseln(f, &len, NULL, delim, 0);
1002 if (line == NULL) {
1003 if (feof(f))
1004 break;
1005 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1006 goto done;
1008 err = parse_packed_ref_line(&ref, NULL, line);
1009 free(line);
1010 if (err)
1011 goto done;
1012 if (ref == NULL)
1013 continue;
1015 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1016 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1017 sizeof(delref->ref.ref.sha1)) == 0) {
1018 found_delref = 1;
1019 got_ref_close(ref);
1020 continue;
1023 err = insert_ref(&new, &refs, ref, repo);
1024 if (err || new == NULL /* duplicate */)
1025 got_ref_close(ref);
1026 if (err)
1027 goto done;
1030 if (found_delref) {
1031 struct got_reflist_entry *re;
1032 size_t n;
1033 struct stat sb;
1035 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1036 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1037 err = got_ferror(f, GOT_ERR_IO);
1038 goto done;
1041 SIMPLEQ_FOREACH(re, &refs, entry) {
1042 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1044 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1045 sizeof(hex)) == NULL) {
1046 err = got_error(GOT_ERR_BAD_REF_DATA);
1047 goto done;
1049 n = fprintf(tmpf, "%s ", hex);
1050 if (n != sizeof(hex)) {
1051 err = got_ferror(f, GOT_ERR_IO);
1052 goto done;
1054 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1055 if (n != strlen(re->ref->ref.ref.name) + 1) {
1056 err = got_ferror(f, GOT_ERR_IO);
1057 goto done;
1061 if (fflush(tmpf) != 0) {
1062 err = got_error_from_errno("fflush");
1063 goto done;
1066 if (stat(packed_refs_path, &sb) != 0) {
1067 if (errno != ENOENT) {
1068 err = got_error_from_errno2("stat",
1069 packed_refs_path);
1070 goto done;
1072 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1075 if (rename(tmppath, packed_refs_path) != 0) {
1076 err = got_error_from_errno3("rename", tmppath,
1077 packed_refs_path);
1078 goto done;
1081 if (chmod(packed_refs_path, sb.st_mode) != 0) {
1082 err = got_error_from_errno2("chmod",
1083 packed_refs_path);
1084 goto done;
1087 done:
1088 if (delref->lf == NULL && lf)
1089 unlock_err = got_lockfile_unlock(lf);
1090 if (f) {
1091 if (fclose(f) != 0 && err == NULL)
1092 err = got_error_from_errno("fclose");
1094 if (tmpf) {
1095 unlink(tmppath);
1096 if (fclose(tmpf) != 0 && err == NULL)
1097 err = got_error_from_errno("fclose");
1099 free(tmppath);
1100 free(packed_refs_path);
1101 got_ref_list_free(&refs);
1102 return err ? err : unlock_err;
1105 const struct got_error *
1106 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1108 const struct got_error *err = NULL, *unlock_err = NULL;
1109 const char *name = got_ref_get_name(ref);
1110 char *path_refs = NULL, *path = NULL;
1111 struct got_lockfile *lf = NULL;
1113 if (ref->flags & GOT_REF_IS_PACKED)
1114 return delete_packed_ref(ref, repo);
1116 path_refs = get_refs_dir_path(repo, name);
1117 if (path_refs == NULL) {
1118 err = got_error_from_errno2("get_refs_dir_path", name);
1119 goto done;
1122 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1123 err = got_error_from_errno("asprintf");
1124 goto done;
1127 if (ref->lf == NULL) {
1128 err = got_lockfile_lock(&lf, path);
1129 if (err)
1130 goto done;
1133 /* XXX: check if old content matches our expectations? */
1135 if (unlink(path) != 0)
1136 err = got_error_from_errno2("unlink", path);
1137 done:
1138 if (ref->lf == NULL && lf)
1139 unlock_err = got_lockfile_unlock(lf);
1141 free(path_refs);
1142 free(path);
1143 return err ? err : unlock_err;
1146 const struct got_error *
1147 got_ref_unlock(struct got_reference *ref)
1149 const struct got_error *err;
1150 err = got_lockfile_unlock(ref->lf);
1151 ref->lf = NULL;
1152 return err;