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 static const struct got_error *
300 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
301 const char *line)
303 struct got_object_id id;
304 const char *name;
306 *ref = NULL;
308 if (line[0] == '#' || line[0] == '^')
309 return NULL;
311 if (!got_parse_sha1_digest(id.sha1, line))
312 return got_error(GOT_ERR_BAD_REF_DATA);
314 if (abs_refname) {
315 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
316 return NULL;
317 name = abs_refname;
318 } else
319 name = line + SHA1_DIGEST_STRING_LENGTH;
321 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
324 static const struct got_error *
325 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
326 int nsubdirs, const char *refname)
328 const struct got_error *err = NULL;
329 char *abs_refname;
330 char *line;
331 size_t len;
332 const char delim[3] = {'\0', '\0', '\0'};
333 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
335 *ref = NULL;
337 if (ref_is_absolute)
338 abs_refname = (char *)refname;
339 do {
340 line = fparseln(f, &len, NULL, delim, 0);
341 if (line == NULL) {
342 if (feof(f))
343 break;
344 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
345 break;
347 for (i = 0; i < nsubdirs; i++) {
348 if (!ref_is_absolute &&
349 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
350 refname) == -1)
351 return got_error_from_errno("asprintf");
352 err = parse_packed_ref_line(ref, abs_refname, line);
353 if (!ref_is_absolute)
354 free(abs_refname);
355 if (err || *ref != NULL)
356 break;
358 free(line);
359 if (err)
360 break;
361 } while (*ref == NULL);
363 return err;
366 static const struct got_error *
367 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
368 const char *name, int lock)
370 const struct got_error *err = NULL;
371 char *path = NULL;
372 char *normpath = NULL;
373 char *absname = NULL;
374 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
375 int ref_is_well_known = is_well_known_ref(name);
377 *ref = NULL;
379 if (ref_is_absolute || ref_is_well_known) {
380 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
381 return got_error_from_errno("asprintf");
382 absname = (char *)name;
383 } else {
384 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
385 subdir[0] ? "/" : "", name) == -1)
386 return got_error_from_errno("asprintf");
388 if (asprintf(&absname, "refs/%s%s%s",
389 subdir, subdir[0] ? "/" : "", name) == -1) {
390 err = got_error_from_errno("asprintf");
391 goto done;
395 normpath = got_path_normalize(path);
396 if (normpath == NULL) {
397 if (errno == ENOENT)
398 err = NULL;
399 else
400 err = got_error_from_errno2("got_path_normalize", path);
401 goto done;
404 err = parse_ref_file(ref, absname, normpath, lock);
405 done:
406 if (!ref_is_absolute && !ref_is_well_known)
407 free(absname);
408 free(path);
409 free(normpath);
410 return err;
413 const struct got_error *
414 got_ref_open(struct got_reference **ref, struct got_repository *repo,
415 const char *refname, int lock)
417 const struct got_error *err = NULL;
418 char *path_refs = NULL;
419 const char *subdirs[] = {
420 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
421 };
422 int i, well_known = is_well_known_ref(refname);
423 struct got_lockfile *lf = NULL;
425 *ref = NULL;
427 path_refs = get_refs_dir_path(repo, refname);
428 if (path_refs == NULL) {
429 err = got_error_from_errno2("get_refs_dir_path", refname);
430 goto done;
433 if (well_known) {
434 err = open_ref(ref, path_refs, "", refname, lock);
435 } else {
436 char *packed_refs_path;
437 FILE *f;
439 /* Search on-disk refs before packed refs! */
440 for (i = 0; i < nitems(subdirs); i++) {
441 err = open_ref(ref, path_refs, subdirs[i], refname,
442 lock);
443 if (err || *ref)
444 goto done;
447 packed_refs_path = got_repo_get_path_packed_refs(repo);
448 if (packed_refs_path == NULL) {
449 err = got_error_from_errno(
450 "got_repo_get_path_packed_refs");
451 goto done;
454 if (lock) {
455 err = got_lockfile_lock(&lf, packed_refs_path);
456 if (err)
457 goto done;
459 f = fopen(packed_refs_path, "rb");
460 free(packed_refs_path);
461 if (f != NULL) {
462 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
463 refname);
464 if (!err) {
465 if (fclose(f) != 0) {
466 err = got_error_from_errno("fclose");
467 got_ref_close(*ref);
468 *ref = NULL;
469 } else if (*ref)
470 (*ref)->lf = lf;
474 done:
475 if (!err && *ref == NULL)
476 err = got_error_not_ref(refname);
477 if (err && lf)
478 got_lockfile_unlock(lf);
479 free(path_refs);
480 return err;
483 void
484 got_ref_close(struct got_reference *ref)
486 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
487 free(ref->ref.symref.name);
488 free(ref->ref.symref.ref);
489 } else
490 free(ref->ref.ref.name);
491 free(ref);
494 struct got_reference *
495 got_ref_dup(struct got_reference *ref)
497 struct got_reference *ret;
499 ret = calloc(1, sizeof(*ret));
500 if (ret == NULL)
501 return NULL;
503 ret->flags = ref->flags;
504 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
505 ret->ref.symref.name = strdup(ref->ref.symref.name);
506 if (ret->ref.symref.name == NULL) {
507 free(ret);
508 return NULL;
510 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
511 if (ret->ref.symref.ref == NULL) {
512 free(ret->ref.symref.name);
513 free(ret);
514 return NULL;
516 } else {
517 ref->ref.ref.name = strdup(ref->ref.ref.name);
518 if (ref->ref.ref.name == NULL) {
519 free(ret);
520 return NULL;
522 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
523 sizeof(ret->ref.ref.sha1));
526 return ret;
529 static const struct got_error *
530 resolve_symbolic_ref(struct got_reference **resolved,
531 struct got_repository *repo, struct got_reference *ref)
533 struct got_reference *nextref;
534 const struct got_error *err;
536 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
537 if (err)
538 return err;
540 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
541 err = resolve_symbolic_ref(resolved, repo, nextref);
542 else
543 *resolved = got_ref_dup(nextref);
545 got_ref_close(nextref);
546 return err;
549 const struct got_error *
550 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
551 struct got_reference *ref)
553 const struct got_error *err;
555 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
556 struct got_reference *resolved = NULL;
557 err = resolve_symbolic_ref(&resolved, repo, ref);
558 if (err == NULL)
559 err = got_ref_resolve(id, repo, resolved);
560 if (resolved)
561 got_ref_close(resolved);
562 return err;
565 *id = calloc(1, sizeof(**id));
566 if (*id == NULL)
567 return got_error_from_errno("calloc");
568 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
569 return NULL;
572 char *
573 got_ref_to_str(struct got_reference *ref)
575 char *str;
577 if (ref->flags & GOT_REF_IS_SYMBOLIC)
578 return strdup(ref->ref.symref.ref);
580 str = malloc(SHA1_DIGEST_STRING_LENGTH);
581 if (str == NULL)
582 return NULL;
584 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
585 SHA1_DIGEST_STRING_LENGTH) == NULL) {
586 free(str);
587 return NULL;
590 return str;
593 const char *
594 got_ref_get_name(struct got_reference *ref)
596 if (ref->flags & GOT_REF_IS_SYMBOLIC)
597 return ref->ref.symref.name;
599 return ref->ref.ref.name;
602 static const struct got_error *
603 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
604 struct got_reference *ref, struct got_repository *repo)
606 const struct got_error *err;
607 struct got_object_id *id;
608 struct got_reflist_entry *new, *re, *prev = NULL;
609 int cmp;
611 *newp = NULL;
613 err = got_ref_resolve(&id, repo, ref);
614 if (err)
615 return err;
617 new = malloc(sizeof(*new));
618 if (new == NULL) {
619 free(id);
620 return got_error_from_errno("malloc");
622 new->ref = ref;
623 new->id = id;
624 *newp = new;
626 /*
627 * We must de-duplicate entries on insert because packed-refs may
628 * contain redundant entries. On-disk refs take precedence.
629 * This code assumes that on-disk revs are read before packed-refs.
630 * We're iterating the list anyway, so insert elements sorted by name.
631 */
632 re = SIMPLEQ_FIRST(refs);
633 while (re) {
634 cmp = got_path_cmp(got_ref_get_name(re->ref),
635 got_ref_get_name(new->ref));
636 if (cmp == 0) {
637 /* duplicate */
638 free(new->id);
639 free(new);
640 *newp = NULL;
641 return NULL;
642 } else if (cmp > 0) {
643 if (prev)
644 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
645 else
646 SIMPLEQ_INSERT_HEAD(refs, new, entry);
647 return NULL;
648 } else {
649 prev = re;
650 re = SIMPLEQ_NEXT(re, entry);
654 SIMPLEQ_INSERT_TAIL(refs, new, entry);
655 return NULL;
658 static const struct got_error *
659 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
660 const char *subdir, struct got_repository *repo)
662 const struct got_error *err = NULL;
663 DIR *d = NULL;
664 char *path_subdir;
666 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
667 return got_error_from_errno("asprintf");
669 d = opendir(path_subdir);
670 if (d == NULL)
671 goto done;
673 for (;;) {
674 struct dirent *dent;
675 struct got_reference *ref;
676 char *child;
678 dent = readdir(d);
679 if (dent == NULL)
680 break;
682 if (strcmp(dent->d_name, ".") == 0 ||
683 strcmp(dent->d_name, "..") == 0)
684 continue;
686 switch (dent->d_type) {
687 case DT_REG:
688 err = open_ref(&ref, path_refs, subdir, dent->d_name,
689 0);
690 if (err)
691 goto done;
692 if (ref) {
693 struct got_reflist_entry *new;
694 err = insert_ref(&new, refs, ref, repo);
695 if (err || new == NULL /* duplicate */)
696 got_ref_close(ref);
697 if (err)
698 goto done;
700 break;
701 case DT_DIR:
702 if (asprintf(&child, "%s%s%s", subdir,
703 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
704 err = got_error_from_errno("asprintf");
705 break;
707 err = gather_on_disk_refs(refs, path_refs, child, repo);
708 free(child);
709 break;
710 default:
711 break;
714 done:
715 if (d)
716 closedir(d);
717 free(path_subdir);
718 return err;
721 const struct got_error *
722 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
724 const struct got_error *err;
725 char *packed_refs_path, *path_refs = NULL;
726 FILE *f = NULL;
727 struct got_reference *ref;
728 struct got_reflist_entry *new;
730 /* HEAD ref should always exist. */
731 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
732 if (path_refs == NULL) {
733 err = got_error_from_errno("get_refs_dir_path");
734 goto done;
736 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
737 if (err)
738 goto done;
739 err = insert_ref(&new, refs, ref, repo);
740 if (err || new == NULL /* duplicate */)
741 got_ref_close(ref);
742 if (err)
743 goto done;
745 /* Gather on-disk refs before parsing packed-refs. */
746 free(path_refs);
747 path_refs = get_refs_dir_path(repo, "");
748 if (path_refs == NULL) {
749 err = got_error_from_errno("get_refs_dir_path");
750 goto done;
752 err = gather_on_disk_refs(refs, path_refs, "", repo);
753 if (err)
754 goto done;
756 /*
757 * The packed-refs file may contain redundant entries, in which
758 * case on-disk refs take precedence.
759 */
760 packed_refs_path = got_repo_get_path_packed_refs(repo);
761 if (packed_refs_path == NULL) {
762 err = got_error_from_errno("got_repo_get_path_packed_refs");
763 goto done;
766 f = fopen(packed_refs_path, "r");
767 free(packed_refs_path);
768 if (f) {
769 char *line;
770 size_t len;
771 const char delim[3] = {'\0', '\0', '\0'};
772 for (;;) {
773 line = fparseln(f, &len, NULL, delim, 0);
774 if (line == NULL) {
775 if (feof(f))
776 break;
777 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
778 goto done;
780 err = parse_packed_ref_line(&ref, NULL, line);
781 free(line);
782 if (err)
783 goto done;
784 if (ref) {
785 err = insert_ref(&new, refs, ref, repo);
786 if (err || new == NULL /* duplicate */)
787 got_ref_close(ref);
788 if (err)
789 goto done;
793 done:
794 free(path_refs);
795 if (f && fclose(f) != 0 && err == NULL)
796 err = got_error_from_errno("fclose");
797 return err;
800 void
801 got_ref_list_free(struct got_reflist_head *refs)
803 struct got_reflist_entry *re;
805 while (!SIMPLEQ_EMPTY(refs)) {
806 re = SIMPLEQ_FIRST(refs);
807 SIMPLEQ_REMOVE_HEAD(refs, entry);
808 got_ref_close(re->ref);
809 free(re->id);
810 free(re);
815 int
816 got_ref_is_symbolic(struct got_reference *ref)
818 return (ref->flags & GOT_REF_IS_SYMBOLIC);
821 const struct got_error *
822 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
824 if (ref->flags & GOT_REF_IS_SYMBOLIC)
825 return got_error(GOT_ERR_BAD_REF_TYPE);
827 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
828 return NULL;
831 const struct got_error *
832 got_ref_change_symref(struct got_reference *ref, char *refname)
834 char *new_name;
836 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
837 return got_error(GOT_ERR_BAD_REF_TYPE);
839 new_name = strdup(refname);
840 if (new_name == NULL)
841 return got_error_from_errno("strdup");
843 free(ref->ref.symref.name);
844 ref->ref.symref.name = new_name;
845 return NULL;
848 const struct got_error *
849 got_ref_write(struct got_reference *ref, struct got_repository *repo)
851 const struct got_error *err = NULL, *unlock_err = NULL;
852 const char *name = got_ref_get_name(ref);
853 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
854 struct got_lockfile *lf = NULL;
855 FILE *f = NULL;
856 size_t n;
857 struct stat sb;
859 path_refs = get_refs_dir_path(repo, name);
860 if (path_refs == NULL) {
861 err = got_error_from_errno2("get_refs_dir_path", name);
862 goto done;
865 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
866 err = got_error_from_errno("asprintf");
867 goto done;
870 err = got_opentemp_named(&tmppath, &f, path);
871 if (err) {
872 char *parent;
873 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
874 goto done;
875 err = got_path_dirname(&parent, path);
876 if (err)
877 goto done;
878 err = got_path_mkdir(parent);
879 free(parent);
880 if (err)
881 goto done;
882 err = got_opentemp_named(&tmppath, &f, path);
883 if (err)
884 goto done;
887 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
888 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
889 if (n != strlen(ref->ref.symref.ref) + 6) {
890 err = got_ferror(f, GOT_ERR_IO);
891 goto done;
893 } else {
894 char hex[SHA1_DIGEST_STRING_LENGTH];
895 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
896 sizeof(hex)) == NULL) {
897 err = got_error(GOT_ERR_BAD_REF_DATA);
898 goto done;
900 n = fprintf(f, "%s\n", hex);
901 if (n != sizeof(hex)) {
902 err = got_ferror(f, GOT_ERR_IO);
903 goto done;
907 if (ref->lf == NULL) {
908 err = got_lockfile_lock(&lf, path);
909 if (err)
910 goto done;
913 /* XXX: check if old content matches our expectations? */
915 if (stat(path, &sb) != 0) {
916 if (errno != ENOENT) {
917 err = got_error_from_errno2("stat", path);
918 goto done;
920 sb.st_mode = GOT_DEFAULT_FILE_MODE;
923 if (rename(tmppath, path) != 0) {
924 err = got_error_from_errno3("rename", tmppath, path);
925 goto done;
927 free(tmppath);
928 tmppath = NULL;
930 if (chmod(path, sb.st_mode) != 0) {
931 err = got_error_from_errno2("chmod", path);
932 goto done;
934 done:
935 if (ref->lf == NULL && lf)
936 unlock_err = got_lockfile_unlock(lf);
937 if (f) {
938 if (fclose(f) != 0 && err == NULL)
939 err = got_error_from_errno("fclose");
941 free(path_refs);
942 free(path);
943 if (tmppath) {
944 if (unlink(tmppath) != 0 && err == NULL)
945 err = got_error_from_errno2("unlink", tmppath);
946 free(tmppath);
948 return err ? err : unlock_err;
951 static const struct got_error *
952 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
954 const struct got_error *err = NULL, *unlock_err = NULL;
955 struct got_lockfile *lf = NULL;
956 FILE *f = NULL, *tmpf = NULL;
957 char *packed_refs_path, *tmppath = NULL;
958 struct got_reflist_head refs;
959 int found_delref = 0;
961 /* The packed-refs file does not cotain symbolic references. */
962 if (delref->flags & GOT_REF_IS_SYMBOLIC)
963 return got_error(GOT_ERR_BAD_REF_DATA);
965 SIMPLEQ_INIT(&refs);
967 packed_refs_path = got_repo_get_path_packed_refs(repo);
968 if (packed_refs_path == NULL)
969 return got_error_from_errno("got_repo_get_path_packed_refs");
971 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
972 if (err)
973 goto done;
975 if (delref->lf == NULL) {
976 err = got_lockfile_lock(&lf, packed_refs_path);
977 if (err)
978 goto done;
981 f = fopen(packed_refs_path, "r");
982 if (f == NULL) {
983 err = got_error_from_errno2("fopen", packed_refs_path);
984 goto done;
986 for (;;) {
987 char *line;
988 size_t len;
989 const char delim[3] = {'\0', '\0', '\0'};
990 struct got_reference *ref;
991 struct got_reflist_entry *new;
993 line = fparseln(f, &len, NULL, delim, 0);
994 if (line == NULL) {
995 if (feof(f))
996 break;
997 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
998 goto done;
1000 err = parse_packed_ref_line(&ref, NULL, line);
1001 free(line);
1002 if (err)
1003 goto done;
1004 if (ref == NULL)
1005 continue;
1007 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1008 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1009 sizeof(delref->ref.ref.sha1)) == 0) {
1010 found_delref = 1;
1011 got_ref_close(ref);
1012 continue;
1015 err = insert_ref(&new, &refs, ref, repo);
1016 if (err || new == NULL /* duplicate */)
1017 got_ref_close(ref);
1018 if (err)
1019 goto done;
1022 if (found_delref) {
1023 struct got_reflist_entry *re;
1024 size_t n;
1025 struct stat sb;
1027 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1028 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1029 err = got_ferror(f, GOT_ERR_IO);
1030 goto done;
1033 SIMPLEQ_FOREACH(re, &refs, entry) {
1034 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1036 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1037 sizeof(hex)) == NULL) {
1038 err = got_error(GOT_ERR_BAD_REF_DATA);
1039 goto done;
1041 n = fprintf(tmpf, "%s ", hex);
1042 if (n != sizeof(hex)) {
1043 err = got_ferror(f, GOT_ERR_IO);
1044 goto done;
1046 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1047 if (n != strlen(re->ref->ref.ref.name) + 1) {
1048 err = got_ferror(f, GOT_ERR_IO);
1049 goto done;
1053 if (fflush(tmpf) != 0) {
1054 err = got_error_from_errno("fflush");
1055 goto done;
1058 if (stat(packed_refs_path, &sb) != 0) {
1059 if (errno != ENOENT) {
1060 err = got_error_from_errno2("stat",
1061 packed_refs_path);
1062 goto done;
1064 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1067 if (rename(tmppath, packed_refs_path) != 0) {
1068 err = got_error_from_errno3("rename", tmppath,
1069 packed_refs_path);
1070 goto done;
1073 if (chmod(packed_refs_path, sb.st_mode) != 0) {
1074 err = got_error_from_errno2("chmod",
1075 packed_refs_path);
1076 goto done;
1079 done:
1080 if (delref->lf == NULL && lf)
1081 unlock_err = got_lockfile_unlock(lf);
1082 if (f) {
1083 if (fclose(f) != 0 && err == NULL)
1084 err = got_error_from_errno("fclose");
1086 if (tmpf) {
1087 unlink(tmppath);
1088 if (fclose(tmpf) != 0 && err == NULL)
1089 err = got_error_from_errno("fclose");
1091 free(tmppath);
1092 free(packed_refs_path);
1093 got_ref_list_free(&refs);
1094 return err ? err : unlock_err;
1097 const struct got_error *
1098 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1100 const struct got_error *err = NULL, *unlock_err = NULL;
1101 const char *name = got_ref_get_name(ref);
1102 char *path_refs = NULL, *path = NULL;
1103 struct got_lockfile *lf = NULL;
1105 if (ref->flags & GOT_REF_IS_PACKED)
1106 return delete_packed_ref(ref, repo);
1108 path_refs = get_refs_dir_path(repo, name);
1109 if (path_refs == NULL) {
1110 err = got_error_from_errno2("get_refs_dir_path", name);
1111 goto done;
1114 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1115 err = got_error_from_errno("asprintf");
1116 goto done;
1119 if (ref->lf == NULL) {
1120 err = got_lockfile_lock(&lf, path);
1121 if (err)
1122 goto done;
1125 /* XXX: check if old content matches our expectations? */
1127 if (unlink(path) != 0)
1128 err = got_error_from_errno2("unlink", path);
1129 done:
1130 if (ref->lf == NULL && lf)
1131 unlock_err = got_lockfile_unlock(lf);
1133 free(path_refs);
1134 free(path);
1135 return err ? err : unlock_err;
1138 const struct got_error *
1139 got_ref_unlock(struct got_reference *ref)
1141 const struct got_error *err;
1142 err = got_lockfile_unlock(ref->lf);
1143 ref->lf = NULL;
1144 return err;