2 * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/types.h>
24 #include "got_error.h"
28 static const struct got_error *
29 parse_symref(struct got_reference **ref, const char *name, const char *line)
31 struct got_symref *symref;
36 return got_error(GOT_ERR_NOT_REF);
38 symref_name = strdup(name);
39 if (symref_name == NULL)
40 return got_error(GOT_ERR_NO_MEM);
41 symref_ref = strdup(line);
42 if (symref_ref == NULL) {
44 return got_error(GOT_ERR_NO_MEM);
47 *ref = calloc(1, sizeof(**ref));
48 (*ref)->flags |= GOT_REF_IS_SYMBOLIC;
49 symref = &((*ref)->ref.symref);
50 symref->name = symref_name;
51 symref->ref = symref_ref;
56 parse_sha1_digest(uint8_t *digest, const char *line)
61 for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
62 n = sscanf(line, "%hhx", &b);
72 static const struct got_error *
73 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
75 uint8_t digest[SHA1_DIGEST_LENGTH];
78 if (strncmp(line, "ref: ", 5) == 0) {
80 return parse_symref(ref, name, line);
83 ref_name = strdup(name);
85 return got_error(GOT_ERR_NO_MEM);
87 if (!parse_sha1_digest(digest, line))
88 return got_error(GOT_ERR_NOT_REF);
90 *ref = calloc(1, sizeof(**ref));
91 (*ref)->ref.ref.name = ref_name;
92 memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
96 static const struct got_error *
97 parse_ref_file(struct got_reference **ref, const char *name,
100 const struct got_error *err = NULL;
101 FILE *f = fopen(abspath, "rb");
104 const char delim[3] = {'\0', '\0', '\0'};
107 return got_error(GOT_ERR_NOT_REF);
109 line = fparseln(f, &len, NULL, delim, 0);
111 err = got_error(GOT_ERR_NOT_REF);
115 err = parse_ref_line(ref, name, line);
122 const struct got_error *
123 got_ref_open(struct got_reference **ref, const char *path_refs,
126 const struct got_error *err = NULL;
127 char *path_ref = NULL;
128 char *normpath = NULL;
129 const char *parent_dir;
131 /* XXX For now, this assumes that refs exist in the filesystem. */
133 if (asprintf(&path_ref, "%s/%s", path_refs, refname) == -1) {
134 err = got_error(GOT_ERR_NO_MEM);
138 normpath = got_path_normalize(path_ref);
139 if (normpath == NULL) {
140 err = got_error(GOT_ERR_NOT_REF);
144 err = parse_ref_file(ref, refname, normpath ? normpath : path_refs);
152 got_ref_close(struct got_reference *ref)
154 if (ref->flags & GOT_REF_IS_SYMBOLIC)
155 free(ref->ref.symref.name);
157 free(ref->ref.ref.name);