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 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_prefix_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_prefix_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_prefix_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_prefix_errno("asprintf");
388 if (asprintf(&absname, "refs/%s%s%s",
389 subdir, subdir[0] ? "/" : "", name) == -1) {
390 err = got_error_prefix_errno("asprintf");
391 goto done;
395 normpath = got_path_normalize(path);
396 if (normpath == NULL) {
397 err = got_error_prefix_errno2("got_path_normalize", path);
398 goto done;
401 err = parse_ref_file(ref, absname, normpath, lock);
402 done:
403 if (!ref_is_absolute && !ref_is_well_known)
404 free(absname);
405 free(path);
406 free(normpath);
407 return err;
410 const struct got_error *
411 got_ref_open(struct got_reference **ref, struct got_repository *repo,
412 const char *refname, int lock)
414 const struct got_error *err = NULL;
415 char *path_refs = NULL;
416 const char *subdirs[] = {
417 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
418 };
419 int i, well_known = is_well_known_ref(refname);
420 struct got_lockfile *lf = NULL;
422 *ref = NULL;
424 path_refs = get_refs_dir_path(repo, refname);
425 if (path_refs == NULL) {
426 err = got_error_prefix_errno2("get_refs_dir_path", refname);
427 goto done;
430 if (well_known) {
431 err = open_ref(ref, path_refs, "", refname, lock);
432 } else {
433 char *packed_refs_path;
434 FILE *f;
436 /* Search on-disk refs before packed refs! */
437 for (i = 0; i < nitems(subdirs); i++) {
438 err = open_ref(ref, path_refs, subdirs[i], refname,
439 lock);
440 if (err || *ref)
441 goto done;
444 packed_refs_path = got_repo_get_path_packed_refs(repo);
445 if (packed_refs_path == NULL) {
446 err = got_error_prefix_errno(
447 "got_repo_get_path_packed_refs");
448 goto done;
451 if (lock) {
452 err = got_lockfile_lock(&lf, packed_refs_path);
453 if (err)
454 goto done;
456 f = fopen(packed_refs_path, "rb");
457 free(packed_refs_path);
458 if (f != NULL) {
459 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
460 refname);
461 if (!err) {
462 if (fclose(f) != 0) {
463 err = got_error_prefix_errno("fclose");
464 got_ref_close(*ref);
465 *ref = NULL;
466 } else
467 (*ref)->lf = lf;
471 done:
472 if (!err && *ref == NULL)
473 err = got_error_not_ref(refname);
474 if (err && lf)
475 got_lockfile_unlock(lf);
476 free(path_refs);
477 return err;
480 void
481 got_ref_close(struct got_reference *ref)
483 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
484 free(ref->ref.symref.name);
485 free(ref->ref.symref.ref);
486 } else
487 free(ref->ref.ref.name);
488 free(ref);
491 struct got_reference *
492 got_ref_dup(struct got_reference *ref)
494 struct got_reference *ret;
496 ret = calloc(1, sizeof(*ret));
497 if (ret == NULL)
498 return NULL;
500 ret->flags = ref->flags;
501 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
502 ret->ref.symref.name = strdup(ref->ref.symref.name);
503 if (ret->ref.symref.name == NULL) {
504 free(ret);
505 return NULL;
507 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
508 if (ret->ref.symref.ref == NULL) {
509 free(ret->ref.symref.name);
510 free(ret);
511 return NULL;
513 } else {
514 ref->ref.ref.name = strdup(ref->ref.ref.name);
515 if (ref->ref.ref.name == NULL) {
516 free(ret);
517 return NULL;
519 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
520 sizeof(ret->ref.ref.sha1));
523 return ret;
526 static const struct got_error *
527 resolve_symbolic_ref(struct got_reference **resolved,
528 struct got_repository *repo, struct got_reference *ref)
530 struct got_reference *nextref;
531 const struct got_error *err;
533 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
534 if (err)
535 return err;
537 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
538 err = resolve_symbolic_ref(resolved, repo, nextref);
539 else
540 *resolved = got_ref_dup(nextref);
542 got_ref_close(nextref);
543 return err;
546 const struct got_error *
547 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
548 struct got_reference *ref)
550 const struct got_error *err;
552 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
553 struct got_reference *resolved = NULL;
554 err = resolve_symbolic_ref(&resolved, repo, ref);
555 if (err == NULL)
556 err = got_ref_resolve(id, repo, resolved);
557 if (resolved)
558 got_ref_close(resolved);
559 return err;
562 *id = calloc(1, sizeof(**id));
563 if (*id == NULL)
564 return got_error_prefix_errno("calloc");
565 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
566 return NULL;
569 char *
570 got_ref_to_str(struct got_reference *ref)
572 char *str;
574 if (ref->flags & GOT_REF_IS_SYMBOLIC)
575 return strdup(ref->ref.symref.ref);
577 str = malloc(SHA1_DIGEST_STRING_LENGTH);
578 if (str == NULL)
579 return NULL;
581 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
582 SHA1_DIGEST_STRING_LENGTH) == NULL) {
583 free(str);
584 return NULL;
587 return str;
590 const char *
591 got_ref_get_name(struct got_reference *ref)
593 if (ref->flags & GOT_REF_IS_SYMBOLIC)
594 return ref->ref.symref.name;
596 return ref->ref.ref.name;
599 static const struct got_error *
600 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
601 struct got_reference *ref, struct got_repository *repo)
603 const struct got_error *err;
604 struct got_object_id *id;
605 struct got_reflist_entry *new, *re, *prev = NULL;
606 int cmp;
608 *newp = NULL;
610 err = got_ref_resolve(&id, repo, ref);
611 if (err)
612 return err;
614 new = malloc(sizeof(*new));
615 if (new == NULL) {
616 free(id);
617 return got_error_prefix_errno("malloc");
619 new->ref = ref;
620 new->id = id;
621 *newp = new;
623 /*
624 * We must de-duplicate entries on insert because packed-refs may
625 * contain redundant entries. On-disk refs take precedence.
626 * This code assumes that on-disk revs are read before packed-refs.
627 * We're iterating the list anyway, so insert elements sorted by name.
628 */
629 re = SIMPLEQ_FIRST(refs);
630 while (re) {
631 cmp = got_path_cmp(got_ref_get_name(re->ref),
632 got_ref_get_name(new->ref));
633 if (cmp == 0) {
634 /* duplicate */
635 free(new->id);
636 free(new);
637 *newp = NULL;
638 return NULL;
639 } else if (cmp > 0) {
640 if (prev)
641 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
642 else
643 SIMPLEQ_INSERT_HEAD(refs, new, entry);
644 return NULL;
645 } else {
646 prev = re;
647 re = SIMPLEQ_NEXT(re, entry);
651 SIMPLEQ_INSERT_TAIL(refs, new, entry);
652 return NULL;
655 static const struct got_error *
656 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
657 const char *subdir, struct got_repository *repo)
659 const struct got_error *err = NULL;
660 DIR *d = NULL;
661 char *path_subdir;
663 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
664 return got_error_prefix_errno("asprintf");
666 d = opendir(path_subdir);
667 if (d == NULL)
668 goto done;
670 for (;;) {
671 struct dirent *dent;
672 struct got_reference *ref;
673 char *child;
675 dent = readdir(d);
676 if (dent == NULL)
677 break;
679 if (strcmp(dent->d_name, ".") == 0 ||
680 strcmp(dent->d_name, "..") == 0)
681 continue;
683 switch (dent->d_type) {
684 case DT_REG:
685 err = open_ref(&ref, path_refs, subdir, dent->d_name,
686 0);
687 if (err)
688 goto done;
689 if (ref) {
690 struct got_reflist_entry *new;
691 err = insert_ref(&new, refs, ref, repo);
692 if (err || new == NULL /* duplicate */)
693 got_ref_close(ref);
694 if (err)
695 goto done;
697 break;
698 case DT_DIR:
699 if (asprintf(&child, "%s%s%s", subdir,
700 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
701 err = got_error_prefix_errno("asprintf");
702 break;
704 err = gather_on_disk_refs(refs, path_refs, child, repo);
705 free(child);
706 break;
707 default:
708 break;
711 done:
712 if (d)
713 closedir(d);
714 free(path_subdir);
715 return err;
718 const struct got_error *
719 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
721 const struct got_error *err;
722 char *packed_refs_path, *path_refs = NULL;
723 FILE *f = NULL;
724 struct got_reference *ref;
725 struct got_reflist_entry *new;
727 /* HEAD ref should always exist. */
728 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
729 if (path_refs == NULL) {
730 err = got_error_prefix_errno("get_refs_dir_path");
731 goto done;
733 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
734 if (err)
735 goto done;
736 err = insert_ref(&new, refs, ref, repo);
737 if (err || new == NULL /* duplicate */)
738 got_ref_close(ref);
739 if (err)
740 goto done;
742 /* Gather on-disk refs before parsing packed-refs. */
743 free(path_refs);
744 path_refs = get_refs_dir_path(repo, "");
745 if (path_refs == NULL) {
746 err = got_error_prefix_errno("get_refs_dir_path");
747 goto done;
749 err = gather_on_disk_refs(refs, path_refs, "", repo);
750 if (err)
751 goto done;
753 /*
754 * The packed-refs file may contain redundant entries, in which
755 * case on-disk refs take precedence.
756 */
757 packed_refs_path = got_repo_get_path_packed_refs(repo);
758 if (packed_refs_path == NULL) {
759 err = got_error_prefix_errno("got_repo_get_path_packed_refs");
760 goto done;
763 f = fopen(packed_refs_path, "r");
764 free(packed_refs_path);
765 if (f) {
766 char *line;
767 size_t len;
768 const char delim[3] = {'\0', '\0', '\0'};
769 for (;;) {
770 line = fparseln(f, &len, NULL, delim, 0);
771 if (line == NULL) {
772 if (feof(f))
773 break;
774 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
775 goto done;
777 err = parse_packed_ref_line(&ref, NULL, line);
778 free(line);
779 if (err)
780 goto done;
781 if (ref) {
782 err = insert_ref(&new, refs, ref, repo);
783 if (err || new == NULL /* duplicate */)
784 got_ref_close(ref);
785 if (err)
786 goto done;
790 done:
791 free(path_refs);
792 if (f && fclose(f) != 0 && err == NULL)
793 err = got_error_prefix_errno("fclose");
794 return err;
797 void
798 got_ref_list_free(struct got_reflist_head *refs)
800 struct got_reflist_entry *re;
802 while (!SIMPLEQ_EMPTY(refs)) {
803 re = SIMPLEQ_FIRST(refs);
804 SIMPLEQ_REMOVE_HEAD(refs, entry);
805 got_ref_close(re->ref);
806 free(re->id);
807 free(re);
812 int
813 got_ref_is_symbolic(struct got_reference *ref)
815 return (ref->flags & GOT_REF_IS_SYMBOLIC);
818 const struct got_error *
819 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
821 if (ref->flags & GOT_REF_IS_SYMBOLIC)
822 return got_error(GOT_ERR_BAD_REF_TYPE);
824 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
825 return NULL;
828 const struct got_error *
829 got_ref_change_symref(struct got_reference *ref, char *refname)
831 char *new_name;
833 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
834 return got_error(GOT_ERR_BAD_REF_TYPE);
836 new_name = strdup(refname);
837 if (new_name == NULL)
838 return got_error_prefix_errno("strdup");
840 free(ref->ref.symref.name);
841 ref->ref.symref.name = new_name;
842 return NULL;
845 const struct got_error *
846 got_ref_write(struct got_reference *ref, struct got_repository *repo)
848 const struct got_error *err = NULL, *unlock_err = NULL;
849 const char *name = got_ref_get_name(ref);
850 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
851 struct got_lockfile *lf = NULL;
852 FILE *f = NULL;
853 size_t n;
854 struct stat sb;
856 path_refs = get_refs_dir_path(repo, name);
857 if (path_refs == NULL) {
858 err = got_error_prefix_errno2("get_refs_dir_path", name);
859 goto done;
862 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
863 err = got_error_prefix_errno("asprintf");
864 goto done;
867 err = got_opentemp_named(&tmppath, &f, path);
868 if (err) {
869 char *parent;
870 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
871 goto done;
872 err = got_path_dirname(&parent, path);
873 if (err)
874 goto done;
875 err = got_path_mkdir(parent);
876 free(parent);
877 if (err)
878 goto done;
879 err = got_opentemp_named(&tmppath, &f, path);
880 if (err)
881 goto done;
884 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
885 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
886 if (n != strlen(ref->ref.symref.ref) + 6) {
887 err = got_ferror(f, GOT_ERR_IO);
888 goto done;
890 } else {
891 char hex[SHA1_DIGEST_STRING_LENGTH];
892 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
893 sizeof(hex)) == NULL) {
894 err = got_error(GOT_ERR_BAD_REF_DATA);
895 goto done;
897 n = fprintf(f, "%s\n", hex);
898 if (n != sizeof(hex)) {
899 err = got_ferror(f, GOT_ERR_IO);
900 goto done;
904 if (ref->lf == NULL) {
905 err = got_lockfile_lock(&lf, path);
906 if (err)
907 goto done;
910 /* XXX: check if old content matches our expectations? */
912 if (stat(path, &sb) != 0) {
913 if (errno != ENOENT) {
914 err = got_error_prefix_errno2("stat", path);
915 goto done;
917 sb.st_mode = GOT_DEFAULT_FILE_MODE;
920 if (rename(tmppath, path) != 0) {
921 err = got_error_prefix_errno3("rename", tmppath, path);
922 goto done;
924 free(tmppath);
925 tmppath = NULL;
927 if (chmod(path, sb.st_mode) != 0) {
928 err = got_error_prefix_errno2("chmod", path);
929 goto done;
931 done:
932 if (ref->lf == NULL && lf)
933 unlock_err = got_lockfile_unlock(lf);
934 if (f) {
935 if (fclose(f) != 0 && err == NULL)
936 err = got_error_prefix_errno("fclose");
938 free(path_refs);
939 free(path);
940 if (tmppath) {
941 if (unlink(tmppath) != 0 && err == NULL)
942 err = got_error_prefix_errno2("unlink", tmppath);
943 free(tmppath);
945 return err ? err : unlock_err;
948 static const struct got_error *
949 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
951 const struct got_error *err = NULL, *unlock_err = NULL;
952 struct got_lockfile *lf = NULL;
953 FILE *f = NULL, *tmpf = NULL;
954 char *packed_refs_path, *tmppath = NULL;
955 struct got_reflist_head refs;
956 int found_delref = 0;
958 /* The packed-refs file does not cotain symbolic references. */
959 if (delref->flags & GOT_REF_IS_SYMBOLIC)
960 return got_error(GOT_ERR_BAD_REF_DATA);
962 SIMPLEQ_INIT(&refs);
964 packed_refs_path = got_repo_get_path_packed_refs(repo);
965 if (packed_refs_path == NULL)
966 return got_error_prefix_errno("got_repo_get_path_packed_refs");
968 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
969 if (err)
970 goto done;
972 if (delref->lf == NULL) {
973 err = got_lockfile_lock(&lf, packed_refs_path);
974 if (err)
975 goto done;
978 f = fopen(packed_refs_path, "r");
979 if (f == NULL) {
980 err = got_error_prefix_errno2("fopen", packed_refs_path);
981 goto done;
983 for (;;) {
984 char *line;
985 size_t len;
986 const char delim[3] = {'\0', '\0', '\0'};
987 struct got_reference *ref;
988 struct got_reflist_entry *new;
990 line = fparseln(f, &len, NULL, delim, 0);
991 if (line == NULL) {
992 if (feof(f))
993 break;
994 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
995 goto done;
997 err = parse_packed_ref_line(&ref, NULL, line);
998 free(line);
999 if (err)
1000 goto done;
1001 if (ref == NULL)
1002 continue;
1004 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1005 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1006 sizeof(delref->ref.ref.sha1)) == 0) {
1007 found_delref = 1;
1008 got_ref_close(ref);
1009 continue;
1012 err = insert_ref(&new, &refs, ref, repo);
1013 if (err || new == NULL /* duplicate */)
1014 got_ref_close(ref);
1015 if (err)
1016 goto done;
1019 if (found_delref) {
1020 struct got_reflist_entry *re;
1021 size_t n;
1022 struct stat sb;
1024 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1025 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1026 err = got_ferror(f, GOT_ERR_IO);
1027 goto done;
1030 SIMPLEQ_FOREACH(re, &refs, entry) {
1031 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1033 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1034 sizeof(hex)) == NULL) {
1035 err = got_error(GOT_ERR_BAD_REF_DATA);
1036 goto done;
1038 n = fprintf(tmpf, "%s ", hex);
1039 if (n != sizeof(hex)) {
1040 err = got_ferror(f, GOT_ERR_IO);
1041 goto done;
1043 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1044 if (n != strlen(re->ref->ref.ref.name) + 1) {
1045 err = got_ferror(f, GOT_ERR_IO);
1046 goto done;
1050 if (fflush(tmpf) != 0) {
1051 err = got_error_prefix_errno("fflush");
1052 goto done;
1055 if (stat(packed_refs_path, &sb) != 0) {
1056 if (errno != ENOENT) {
1057 err = got_error_prefix_errno2("stat",
1058 packed_refs_path);
1059 goto done;
1061 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1064 if (rename(tmppath, packed_refs_path) != 0) {
1065 err = got_error_prefix_errno3("rename", tmppath,
1066 packed_refs_path);
1067 goto done;
1070 if (chmod(packed_refs_path, sb.st_mode) != 0) {
1071 err = got_error_prefix_errno2("chmod",
1072 packed_refs_path);
1073 goto done;
1076 done:
1077 if (delref->lf == NULL && lf)
1078 unlock_err = got_lockfile_unlock(lf);
1079 if (f) {
1080 if (fclose(f) != 0 && err == NULL)
1081 err = got_error_prefix_errno("fclose");
1083 if (tmpf) {
1084 unlink(tmppath);
1085 if (fclose(tmpf) != 0 && err == NULL)
1086 err = got_error_prefix_errno("fclose");
1088 free(tmppath);
1089 free(packed_refs_path);
1090 got_ref_list_free(&refs);
1091 return err ? err : unlock_err;
1094 const struct got_error *
1095 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1097 const struct got_error *err = NULL, *unlock_err = NULL;
1098 const char *name = got_ref_get_name(ref);
1099 char *path_refs = NULL, *path = NULL;
1100 struct got_lockfile *lf = NULL;
1102 if (ref->flags & GOT_REF_IS_PACKED)
1103 return delete_packed_ref(ref, repo);
1105 path_refs = get_refs_dir_path(repo, name);
1106 if (path_refs == NULL) {
1107 err = got_error_prefix_errno2("get_refs_dir_path", name);
1108 goto done;
1111 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1112 err = got_error_prefix_errno("asprintf");
1113 goto done;
1116 if (ref->lf == NULL) {
1117 err = got_lockfile_lock(&lf, path);
1118 if (err)
1119 goto done;
1122 /* XXX: check if old content matches our expectations? */
1124 if (unlink(path) != 0)
1125 err = got_error_prefix_errno2("unlink", path);
1126 done:
1127 if (ref->lf == NULL && lf)
1128 unlock_err = got_lockfile_unlock(lf);
1130 free(path_refs);
1131 free(path);
1132 return err ? err : unlock_err;
1135 const struct got_error *
1136 got_ref_unlock(struct got_reference *ref)
1138 const struct got_error *err;
1139 err = got_lockfile_unlock(ref->lf);
1140 ref->lf = NULL;
1141 return err;