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"
39 #include "got_lib_sha1.h"
40 #include "got_lib_path.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;
83 };
85 static const struct got_error *
86 alloc_ref(struct got_reference **ref, const char *name,
87 struct got_object_id *id, int flags)
88 {
89 const struct got_error *err = NULL;
91 *ref = calloc(1, sizeof(**ref));
92 if (*ref == NULL)
93 return got_error_from_errno();
95 memcpy((*ref)->ref.ref.sha1, id->sha1, sizeof((*ref)->ref.ref.sha1));
96 (*ref)->flags = flags;
97 (*ref)->ref.ref.name = strdup(name);
98 if ((*ref)->ref.ref.name == NULL) {
99 err = got_error_from_errno();
100 got_ref_close(*ref);
101 *ref = NULL;
103 return err;
106 static const struct got_error *
107 alloc_symref(struct got_reference **ref, const char *name,
108 const char *target_ref, int flags)
110 const struct got_error *err = NULL;
112 *ref = calloc(1, sizeof(**ref));
113 if (*ref == NULL)
114 return got_error_from_errno();
116 (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
117 (*ref)->ref.symref.name = strdup(name);
118 if ((*ref)->ref.symref.name == NULL) {
119 err = got_error_from_errno();
120 got_ref_close(*ref);
121 *ref = NULL;
123 (*ref)->ref.symref.ref = strdup(target_ref);
124 if ((*ref)->ref.symref.ref == NULL) {
125 err = got_error_from_errno();
126 got_ref_close(*ref);
127 *ref = NULL;
129 return err;
132 static const struct got_error *
133 parse_symref(struct got_reference **ref, const char *name, const char *line)
135 if (line[0] == '\0')
136 return got_error(GOT_ERR_BAD_REF_DATA);
138 return alloc_symref(ref, name, line, 0);
141 static const struct got_error *
142 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
144 struct got_object_id id;
146 if (strncmp(line, "ref: ", 5) == 0) {
147 line += 5;
148 return parse_symref(ref, name, line);
151 if (!got_parse_sha1_digest(id.sha1, line))
152 return got_error(GOT_ERR_BAD_REF_DATA);
154 return alloc_ref(ref, name, &id, 0);
157 static const struct got_error *
158 parse_ref_file(struct got_reference **ref, const char *name,
159 const char *abspath)
161 const struct got_error *err = NULL;
162 FILE *f;
163 char *line;
164 size_t len;
165 const char delim[3] = {'\0', '\0', '\0'};
167 f = fopen(abspath, "rb");
168 if (f == NULL)
169 return NULL;
171 line = fparseln(f, &len, NULL, delim, 0);
172 if (line == NULL) {
173 err = got_error(GOT_ERR_BAD_REF_DATA);
174 goto done;
177 err = parse_ref_line(ref, name, line);
178 done:
179 free(line);
180 if (fclose(f) != 0 && err == NULL)
181 err = got_error_from_errno();
182 return err;
185 static int
186 is_well_known_ref(const char *refname)
188 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
189 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
190 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
191 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
194 static char *
195 get_refs_dir_path(struct got_repository *repo, const char *refname)
197 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
198 return strdup(got_repo_get_path_git_dir(repo));
200 return got_repo_get_path_refs(repo);
203 static int
204 is_valid_ref_name(const char *name)
206 const char *s, *slash, *seg;
207 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
208 const char *forbidden_seq[] = { "//", "..", "@{" };
209 const char *lfs = GOT_LOCKFILE_SUFFIX;
210 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
211 int i;
213 if (name[0] == '@' && name[1] == '\0')
214 return 0;
216 slash = strchr(name, '/');
217 if (slash == NULL)
218 return 0;
220 s = name;
221 seg = s;
222 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
223 return 0;
224 while (*s) {
225 for (i = 0; i < nitems(forbidden); i++) {
226 if (*s == forbidden[i])
227 return 0;
229 for (i = 0; i < nitems(forbidden_seq); i++) {
230 if (s[0] == forbidden_seq[i][0] &&
231 s[1] == forbidden_seq[i][1])
232 return 0;
234 if (iscntrl((unsigned char)s[0]))
235 return 0;
236 if (s[0] == '.' && s[1] == '\0')
237 return 0;
238 if (*s == '/') {
239 const char *nextseg = s + 1;
240 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
241 nextseg[0] == '/')
242 return 0;
243 if (seg <= s - lfs_len &&
244 strncmp(s - lfs_len, lfs, lfs_len) == 0)
245 return 0;
246 seg = nextseg;
248 s++;
251 if (seg <= s - lfs_len &&
252 strncmp(s - lfs_len, lfs, lfs_len) == 0)
253 return 0;
255 return 1;
258 const struct got_error *
259 got_ref_alloc(struct got_reference **ref, const char *name,
260 struct got_object_id *id)
262 if (!is_valid_ref_name(name))
263 return got_error(GOT_ERR_BAD_REF_NAME);
265 return alloc_ref(ref, name, id, 0);
268 static const struct got_error *
269 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
270 const char *line)
272 struct got_object_id id;
273 const char *name;
275 *ref = NULL;
277 if (line[0] == '#' || line[0] == '^')
278 return NULL;
280 if (!got_parse_sha1_digest(id.sha1, line))
281 return got_error(GOT_ERR_BAD_REF_DATA);
283 if (abs_refname) {
284 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
285 return NULL;
286 name = abs_refname;
287 } else
288 name = line + SHA1_DIGEST_STRING_LENGTH;
290 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
293 static const struct got_error *
294 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
295 int nsubdirs, const char *refname)
297 const struct got_error *err = NULL;
298 char *abs_refname;
299 char *line;
300 size_t len;
301 const char delim[3] = {'\0', '\0', '\0'};
302 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
304 *ref = NULL;
306 if (ref_is_absolute)
307 abs_refname = (char *)refname;
308 do {
309 line = fparseln(f, &len, NULL, delim, 0);
310 if (line == NULL) {
311 if (feof(f))
312 break;
313 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
314 break;
316 for (i = 0; i < nsubdirs; i++) {
317 if (!ref_is_absolute &&
318 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
319 refname) == -1)
320 return got_error_from_errno();
321 err = parse_packed_ref_line(ref, abs_refname, line);
322 if (!ref_is_absolute)
323 free(abs_refname);
324 if (err || *ref != NULL)
325 break;
327 free(line);
328 if (err)
329 break;
330 } while (*ref == NULL);
332 return err;
335 static const struct got_error *
336 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
337 const char *name)
339 const struct got_error *err = NULL;
340 char *path = NULL;
341 char *normpath = NULL;
342 char *absname = NULL;
343 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
344 int ref_is_well_known = is_well_known_ref(name);
346 *ref = NULL;
348 if (ref_is_absolute || ref_is_well_known) {
349 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
350 return got_error_from_errno();
351 absname = (char *)name;
352 } else {
353 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
354 subdir[0] ? "/" : "", name) == -1)
355 return got_error_from_errno();
357 if (asprintf(&absname, "refs/%s%s%s",
358 subdir, subdir[0] ? "/" : "", name) == -1) {
359 err = got_error_from_errno();
360 goto done;
364 normpath = got_path_normalize(path);
365 if (normpath == NULL) {
366 err = got_error_from_errno();
367 goto done;
370 err = parse_ref_file(ref, absname, normpath);
371 done:
372 if (!ref_is_absolute && !ref_is_well_known)
373 free(absname);
374 free(path);
375 free(normpath);
376 return err;
379 const struct got_error *
380 got_ref_open(struct got_reference **ref, struct got_repository *repo,
381 const char *refname)
383 const struct got_error *err = NULL;
384 char *path_refs = NULL;
385 const char *subdirs[] = {
386 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
387 };
388 int i, well_known = is_well_known_ref(refname);
390 *ref = NULL;
392 path_refs = get_refs_dir_path(repo, refname);
393 if (path_refs == NULL) {
394 err = got_error_from_errno();
395 goto done;
398 if (!well_known) {
399 char *packed_refs_path;
400 FILE *f;
402 /* Search on-disk refs before packed refs! */
403 for (i = 0; i < nitems(subdirs); i++) {
404 err = open_ref(ref, path_refs, subdirs[i], refname);
405 if (err || *ref)
406 goto done;
409 packed_refs_path = got_repo_get_path_packed_refs(repo);
410 if (packed_refs_path == NULL) {
411 err = got_error_from_errno();
412 goto done;
415 f = fopen(packed_refs_path, "rb");
416 free(packed_refs_path);
417 if (f != NULL) {
418 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
419 refname);
420 if (fclose(f) != 0 && err == NULL)
421 err = got_error_from_errno();
422 if (err || *ref)
423 goto done;
427 err = open_ref(ref, path_refs, "", refname);
428 if (err)
429 goto done;
430 done:
431 if (*ref == NULL)
432 err = got_error_not_ref(refname);
433 free(path_refs);
434 return err;
437 void
438 got_ref_close(struct got_reference *ref)
440 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
441 free(ref->ref.symref.name);
442 free(ref->ref.symref.ref);
443 } else
444 free(ref->ref.ref.name);
445 free(ref);
448 struct got_reference *
449 got_ref_dup(struct got_reference *ref)
451 struct got_reference *ret;
453 ret = calloc(1, sizeof(*ret));
454 if (ret == NULL)
455 return NULL;
457 ret->flags = ref->flags;
458 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
459 ret->ref.symref.name = strdup(ref->ref.symref.name);
460 if (ret->ref.symref.name == NULL) {
461 free(ret);
462 return NULL;
464 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
465 if (ret->ref.symref.ref == NULL) {
466 free(ret->ref.symref.name);
467 free(ret);
468 return NULL;
470 } else {
471 ref->ref.ref.name = strdup(ref->ref.ref.name);
472 if (ref->ref.ref.name == NULL) {
473 free(ret);
474 return NULL;
476 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
477 sizeof(ret->ref.ref.sha1));
480 return ret;
483 static const struct got_error *
484 resolve_symbolic_ref(struct got_reference **resolved,
485 struct got_repository *repo, struct got_reference *ref)
487 struct got_reference *nextref;
488 const struct got_error *err;
490 err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
491 if (err)
492 return err;
494 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
495 err = resolve_symbolic_ref(resolved, repo, nextref);
496 else
497 *resolved = got_ref_dup(nextref);
499 got_ref_close(nextref);
500 return err;
503 const struct got_error *
504 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
505 struct got_reference *ref)
507 const struct got_error *err;
509 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
510 struct got_reference *resolved = NULL;
511 err = resolve_symbolic_ref(&resolved, repo, ref);
512 if (err == NULL)
513 err = got_ref_resolve(id, repo, resolved);
514 if (resolved)
515 got_ref_close(resolved);
516 return err;
519 *id = calloc(1, sizeof(**id));
520 if (*id == NULL)
521 return got_error_from_errno();
522 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
523 return NULL;
526 char *
527 got_ref_to_str(struct got_reference *ref)
529 char *str;
531 if (ref->flags & GOT_REF_IS_SYMBOLIC)
532 return strdup(ref->ref.symref.ref);
534 str = malloc(SHA1_DIGEST_STRING_LENGTH);
535 if (str == NULL)
536 return NULL;
538 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
539 SHA1_DIGEST_STRING_LENGTH) == NULL) {
540 free(str);
541 return NULL;
544 return str;
547 const char *
548 got_ref_get_name(struct got_reference *ref)
550 if (ref->flags & GOT_REF_IS_SYMBOLIC)
551 return ref->ref.symref.name;
553 return ref->ref.ref.name;
556 static const struct got_error *
557 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
558 struct got_reference *ref, struct got_repository *repo)
560 const struct got_error *err;
561 struct got_object_id *id;
562 struct got_reflist_entry *new, *re, *prev = NULL;
563 int cmp;
565 *newp = NULL;
567 err = got_ref_resolve(&id, repo, ref);
568 if (err)
569 return err;
571 new = malloc(sizeof(*new));
572 if (new == NULL) {
573 free(id);
574 return got_error_from_errno();
576 new->ref = ref;
577 new->id = id;
578 *newp = new;
580 /*
581 * We must de-duplicate entries on insert because packed-refs may
582 * contain redundant entries. On-disk refs take precedence.
583 * This code assumes that on-disk revs are read before packed-refs.
584 * We're iterating the list anyway, so insert elements sorted by name.
585 */
586 re = SIMPLEQ_FIRST(refs);
587 while (re) {
588 cmp = got_path_cmp(got_ref_get_name(re->ref),
589 got_ref_get_name(new->ref));
590 if (cmp == 0) {
591 /* duplicate */
592 free(new->id);
593 free(new);
594 *newp = NULL;
595 return NULL;
596 } else if (cmp > 0) {
597 if (prev)
598 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
599 else
600 SIMPLEQ_INSERT_HEAD(refs, new, entry);
601 return NULL;
602 } else {
603 prev = re;
604 re = SIMPLEQ_NEXT(re, entry);
608 SIMPLEQ_INSERT_TAIL(refs, new, entry);
609 return NULL;
612 static const struct got_error *
613 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
614 const char *subdir, struct got_repository *repo)
616 const struct got_error *err = NULL;
617 DIR *d = NULL;
618 char *path_subdir;
620 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
621 return got_error_from_errno();
623 d = opendir(path_subdir);
624 if (d == NULL)
625 goto done;
627 while (1) {
628 struct dirent *dent;
629 struct got_reference *ref;
630 char *child;
632 dent = readdir(d);
633 if (dent == NULL)
634 break;
636 if (strcmp(dent->d_name, ".") == 0 ||
637 strcmp(dent->d_name, "..") == 0)
638 continue;
640 switch (dent->d_type) {
641 case DT_REG:
642 err = open_ref(&ref, path_refs, subdir, dent->d_name);
643 if (err)
644 goto done;
645 if (ref) {
646 struct got_reflist_entry *new;
647 err = insert_ref(&new, refs, ref, repo);
648 if (err || new == NULL /* duplicate */)
649 got_ref_close(ref);
650 if (err)
651 goto done;
653 break;
654 case DT_DIR:
655 if (asprintf(&child, "%s%s%s", subdir,
656 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
657 err = got_error_from_errno();
658 break;
660 err = gather_on_disk_refs(refs, path_refs, child, repo);
661 free(child);
662 break;
663 default:
664 break;
667 done:
668 if (d)
669 closedir(d);
670 free(path_subdir);
671 return err;
674 const struct got_error *
675 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
677 const struct got_error *err;
678 char *packed_refs_path, *path_refs = NULL;
679 FILE *f = NULL;
680 struct got_reference *ref;
681 struct got_reflist_entry *new;
683 /* HEAD ref should always exist. */
684 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
685 if (path_refs == NULL) {
686 err = got_error_from_errno();
687 goto done;
689 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD);
690 if (err)
691 goto done;
692 err = insert_ref(&new, refs, ref, repo);
693 if (err || new == NULL /* duplicate */)
694 got_ref_close(ref);
695 if (err)
696 goto done;
698 /* Gather on-disk refs before parsing packed-refs. */
699 free(path_refs);
700 path_refs = get_refs_dir_path(repo, "");
701 if (path_refs == NULL) {
702 err = got_error_from_errno();
703 goto done;
705 err = gather_on_disk_refs(refs, path_refs, "", repo);
706 if (err)
707 goto done;
709 /*
710 * The packed-refs file may contain redundant entries, in which
711 * case on-disk refs take precedence.
712 */
713 packed_refs_path = got_repo_get_path_packed_refs(repo);
714 if (packed_refs_path == NULL) {
715 err = got_error_from_errno();
716 goto done;
719 f = fopen(packed_refs_path, "r");
720 free(packed_refs_path);
721 if (f) {
722 char *line;
723 size_t len;
724 const char delim[3] = {'\0', '\0', '\0'};
725 while (1) {
726 line = fparseln(f, &len, NULL, delim, 0);
727 if (line == NULL) {
728 if (feof(f))
729 break;
730 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
731 goto done;
733 err = parse_packed_ref_line(&ref, NULL, line);
734 free(line);
735 if (err)
736 goto done;
737 if (ref) {
738 err = insert_ref(&new, refs, ref, repo);
739 if (err || new == NULL /* duplicate */)
740 got_ref_close(ref);
741 if (err)
742 goto done;
746 done:
747 free(path_refs);
748 if (f && fclose(f) != 0 && err == NULL)
749 err = got_error_from_errno();
750 return err;
753 void
754 got_ref_list_free(struct got_reflist_head *refs)
756 struct got_reflist_entry *re;
758 while (!SIMPLEQ_EMPTY(refs)) {
759 re = SIMPLEQ_FIRST(refs);
760 SIMPLEQ_REMOVE_HEAD(refs, entry);
761 got_ref_close(re->ref);
762 free(re->id);
763 free(re);
768 const struct got_error *
769 got_ref_write(struct got_reference *ref, struct got_repository *repo)
771 const struct got_error *err = NULL, *unlock_err = NULL;
772 const char *name = got_ref_get_name(ref);
773 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
774 struct got_lockfile *lf = NULL;
775 FILE *f = NULL;
776 size_t n;
777 struct stat sb;
779 path_refs = get_refs_dir_path(repo, name);
780 if (path_refs == NULL) {
781 err = got_error_from_errno();
782 goto done;
785 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
786 err = got_error_from_errno();
787 goto done;
790 err = got_opentemp_named(&tmppath, &f, path);
791 if (err) {
792 char *parent;
793 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
794 goto done;
795 err = got_path_dirname(&parent, path);
796 if (err)
797 goto done;
798 err = got_path_mkdir(parent);
799 free(parent);
800 if (err)
801 goto done;
802 err = got_opentemp_named(&tmppath, &f, path);
803 if (err)
804 goto done;
807 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
808 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
809 if (n != strlen(ref->ref.symref.ref) + 6) {
810 err = got_ferror(f, GOT_ERR_IO);
811 goto done;
813 } else {
814 char hex[SHA1_DIGEST_STRING_LENGTH];
815 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
816 sizeof(hex)) == NULL) {
817 err = got_error(GOT_ERR_BAD_REF_DATA);
818 goto done;
820 n = fprintf(f, "%s\n", hex);
821 if (n != sizeof(hex)) {
822 err = got_ferror(f, GOT_ERR_IO);
823 goto done;
827 err = got_lockfile_lock(&lf, path);
828 if (err)
829 goto done;
831 /* XXX: check if old content matches our expectations? */
833 if (stat(path, &sb) != 0) {
834 if (errno != ENOENT) {
835 err = got_error_from_errno();
836 goto done;
838 sb.st_mode = GOT_DEFAULT_FILE_MODE;
841 if (rename(tmppath, path) != 0) {
842 err = got_error_from_errno();
843 goto done;
845 free(tmppath);
846 tmppath = NULL;
848 if (chmod(path, sb.st_mode) != 0) {
849 err = got_error_from_errno();
850 goto done;
852 done:
853 if (lf)
854 unlock_err = got_lockfile_unlock(lf);
855 if (f) {
856 if (fclose(f) != 0 && err == NULL)
857 err = got_error_from_errno();
859 free(path_refs);
860 free(path);
861 if (tmppath) {
862 if (unlink(tmppath) != 0 && err == NULL)
863 err = got_error_from_errno();
864 free(tmppath);
866 return err ? err : unlock_err;
869 static const struct got_error *
870 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
872 const struct got_error *err = NULL, *unlock_err = NULL;
873 struct got_lockfile *lf = NULL;
874 FILE *f = NULL, *tmpf = NULL;
875 char *packed_refs_path, *tmppath = NULL;
876 struct got_reflist_head refs;
877 int found_delref = 0;
879 /* The packed-refs file does not cotain symbolic references. */
880 if (delref->flags & GOT_REF_IS_SYMBOLIC)
881 return got_error(GOT_ERR_BAD_REF_DATA);
883 SIMPLEQ_INIT(&refs);
885 packed_refs_path = got_repo_get_path_packed_refs(repo);
886 if (packed_refs_path == NULL)
887 return got_error_from_errno();
889 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
890 if (err)
891 goto done;
893 err = got_lockfile_lock(&lf, packed_refs_path);
894 if (err)
895 goto done;
897 f = fopen(packed_refs_path, "r");
898 if (f == NULL) {
899 err = got_error_from_errno();
900 goto done;
902 while (1) {
903 char *line;
904 size_t len;
905 const char delim[3] = {'\0', '\0', '\0'};
906 struct got_reference *ref;
907 struct got_reflist_entry *new;
909 line = fparseln(f, &len, NULL, delim, 0);
910 if (line == NULL) {
911 if (feof(f))
912 break;
913 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
914 goto done;
916 err = parse_packed_ref_line(&ref, NULL, line);
917 free(line);
918 if (err)
919 goto done;
920 if (ref == NULL)
921 continue;
923 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
924 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
925 sizeof(delref->ref.ref.sha1)) == 0) {
926 found_delref = 1;
927 got_ref_close(ref);
928 continue;
931 err = insert_ref(&new, &refs, ref, repo);
932 if (err || new == NULL /* duplicate */)
933 got_ref_close(ref);
934 if (err)
935 goto done;
938 if (found_delref) {
939 struct got_reflist_entry *re;
940 size_t n;
941 struct stat sb;
943 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
944 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
945 err = got_ferror(f, GOT_ERR_IO);
946 goto done;
949 SIMPLEQ_FOREACH(re, &refs, entry) {
950 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
952 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
953 sizeof(hex)) == NULL) {
954 err = got_error(GOT_ERR_BAD_REF_DATA);
955 goto done;
957 n = fprintf(tmpf, "%s ", hex);
958 if (n != sizeof(hex)) {
959 err = got_ferror(f, GOT_ERR_IO);
960 goto done;
962 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
963 if (n != strlen(re->ref->ref.ref.name) + 1) {
964 err = got_ferror(f, GOT_ERR_IO);
965 goto done;
969 if (fflush(tmpf) != 0) {
970 err = got_error_from_errno();
971 goto done;
974 if (stat(packed_refs_path, &sb) != 0) {
975 if (errno != ENOENT) {
976 err = got_error_from_errno();
977 goto done;
979 sb.st_mode = GOT_DEFAULT_FILE_MODE;
982 if (rename(tmppath, packed_refs_path) != 0) {
983 err = got_error_from_errno();
984 goto done;
987 if (chmod(packed_refs_path, sb.st_mode) != 0) {
988 err = got_error_from_errno();
989 goto done;
992 done:
993 if (lf)
994 unlock_err = got_lockfile_unlock(lf);
995 if (f) {
996 if (fclose(f) != 0 && err == NULL)
997 err = got_error_from_errno();
999 if (tmpf) {
1000 unlink(tmppath);
1001 if (fclose(tmpf) != 0 && err == NULL)
1002 err = got_error_from_errno();
1004 free(tmppath);
1005 free(packed_refs_path);
1006 got_ref_list_free(&refs);
1007 return err ? err : unlock_err;
1010 const struct got_error *
1011 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1013 const struct got_error *err = NULL, *unlock_err = NULL;
1014 const char *name = got_ref_get_name(ref);
1015 char *path_refs = NULL, *path = NULL;
1016 struct got_lockfile *lf = NULL;
1018 if (ref->flags & GOT_REF_IS_PACKED)
1019 return delete_packed_ref(ref, repo);
1021 path_refs = get_refs_dir_path(repo, name);
1022 if (path_refs == NULL) {
1023 err = got_error_from_errno();
1024 goto done;
1027 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1028 err = got_error_from_errno();
1029 goto done;
1032 err = got_lockfile_lock(&lf, path);
1033 if (err)
1034 goto done;
1036 /* XXX: check if old content matches our expectations? */
1038 if (unlink(path) != 0)
1039 err = got_error_from_errno();
1040 done:
1041 if (lf)
1042 unlock_err = got_lockfile_unlock(lf);
1044 free(path_refs);
1045 free(path);
1046 return err ? err : unlock_err;