Blob


1 /*
2 * Copyright (c) 2017 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 <sha1.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <util.h>
24 #include "got_error.h"
25 #include "got_path.h"
26 #include "got_refs.h"
28 static const struct got_error *
29 parse_symref(struct got_reference **ref, const char *name, const char *line)
30 {
31 struct got_symref *symref;
32 char *symref_name;
33 char *symref_ref;
35 if (line[0] == '\0')
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) {
43 free(symref_name);
44 return got_error(GOT_ERR_NO_MEM);
45 }
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;
52 return NULL;
53 }
55 static int
56 parse_sha1_digest(uint8_t *digest, const char *line)
57 {
58 uint8_t b;
59 int i, n;
61 for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
62 n = sscanf(line, "%hhx", &b);
63 if (n == 1)
64 digest[i] = b;
65 else
66 return 0;
67 }
69 return 1;
70 }
72 static const struct got_error *
73 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
74 {
75 uint8_t digest[SHA1_DIGEST_LENGTH];
76 char *ref_name;
78 if (strncmp(line, "ref: ", 5) == 0) {
79 line += 5;
80 return parse_symref(ref, name, line);
81 }
83 ref_name = strdup(name);
84 if (ref_name == NULL)
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);
93 return NULL;
94 }
96 static const struct got_error *
97 parse_ref_file(struct got_reference **ref, const char *name,
98 const char *abspath)
99 {
100 const struct got_error *err = NULL;
101 FILE *f = fopen(abspath, "rb");
102 char *line;
103 size_t len;
104 const char delim[3] = {'\0', '\0', '\0'};
106 if (f == NULL)
107 return got_error(GOT_ERR_NOT_REF);
109 line = fparseln(f, &len, NULL, delim, 0);
110 if (line == NULL) {
111 err = got_error(GOT_ERR_NOT_REF);
112 goto done;
115 err = parse_ref_line(ref, name, line);
116 done:
117 free(line);
118 fclose(f);
119 return err;
122 const struct got_error *
123 got_ref_open(struct got_reference **ref, const char *path_refs,
124 const char *refname)
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);
135 goto done;
138 normpath = got_path_normalize(path_ref);
139 if (normpath == NULL) {
140 err = got_error(GOT_ERR_NOT_REF);
141 goto done;
144 err = parse_ref_file(ref, refname, normpath ? normpath : path_refs);
145 done:
146 free(normpath);
147 free(path_ref);
148 return err;
151 void
152 got_ref_close(struct got_reference *ref)
154 if (ref->flags & GOT_REF_IS_SYMBOLIC)
155 free(ref->ref.symref.name);
156 else
157 free(ref->ref.ref.name);
158 free(ref);