Blob


1 /*
2 * Copyright (c) 2018 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/queue.h>
19 #include <errno.h>
20 #include <limits.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sha1.h>
25 #include <zlib.h>
26 #include <uuid.h>
28 #include "got_error.h"
29 #include "got_object.h"
31 #include "got_lib_delta.h"
32 #include "got_lib_inflate.h"
33 #include "got_lib_object.h"
34 #include "got_lib_sha1.h"
36 #ifndef nitems
37 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
38 #endif
40 const struct got_error *
41 got_error(int code)
42 {
43 int i;
45 for (i = 0; i < nitems(got_errors); i++) {
46 if (code == got_errors[i].code)
47 return &got_errors[i];
48 }
50 abort();
51 }
53 const struct got_error *
54 got_error_msg(int code, const char *msg)
55 {
56 static struct got_error err;
57 int i;
59 for (i = 0; i < nitems(got_errors); i++) {
60 if (code == got_errors[i].code) {
61 err.code = code;
62 err.msg = msg;
63 return &err;
64 }
65 }
67 abort();
68 }
70 const struct got_error *
71 got_error_from_errno(const char *prefix)
72 {
73 static struct got_error err;
74 static char err_msg[PATH_MAX + 20];
76 snprintf(err_msg, sizeof(err_msg), "%s: %s", prefix,
77 strerror(errno));
79 err.code = GOT_ERR_ERRNO;
80 err.msg = err_msg;
81 return &err;
82 }
84 const struct got_error *
85 got_error_from_errno2(const char *prefix, const char *prefix2)
86 {
87 static struct got_error err;
88 static char err_msg[(PATH_MAX * 2) + 20];
90 snprintf(err_msg, sizeof(err_msg), "%s: %s: %s", prefix, prefix2,
91 strerror(errno));
93 err.code = GOT_ERR_ERRNO;
94 err.msg = err_msg;
95 return &err;
96 }
98 const struct got_error *
99 got_error_from_errno3(const char *prefix, const char *prefix2,
100 const char *prefix3)
102 static struct got_error err;
103 static char err_msg[(PATH_MAX * 3) + 20];
105 snprintf(err_msg, sizeof(err_msg), "%s: %s: %s: %s", prefix, prefix2,
106 prefix3, strerror(errno));
108 err.code = GOT_ERR_ERRNO;
109 err.msg = err_msg;
110 return &err;
113 const struct got_error *
114 got_error_set_errno(int code, const char *prefix)
116 errno = code;
117 return got_error_from_errno(prefix);
120 const struct got_error *
121 got_ferror(FILE *f, int code)
123 if (ferror(f))
124 return got_error_from_errno("");
125 return got_error(code);
128 const struct got_error *
129 got_error_no_obj(struct got_object_id *id)
131 static char msg[sizeof("object not found") +
132 SHA1_DIGEST_STRING_LENGTH];
133 char id_str[SHA1_DIGEST_STRING_LENGTH];
134 int ret;
136 if (!got_sha1_digest_to_str(id->sha1, id_str, sizeof(id_str)))
137 return got_error(GOT_ERR_NO_OBJ);
139 ret = snprintf(msg, sizeof(msg), "object %s not found", id_str);
140 if (ret == -1 || ret >= sizeof(msg))
141 return got_error(GOT_ERR_NO_OBJ);
143 return got_error_msg(GOT_ERR_NO_OBJ, msg);
146 const struct got_error *
147 got_error_not_ref(const char *refname)
149 static char msg[sizeof("reference not found") + 1004];
150 int ret;
152 ret = snprintf(msg, sizeof(msg), "reference %s not found", refname);
153 if (ret == -1 || ret >= sizeof(msg))
154 return got_error(GOT_ERR_NOT_REF);
156 return got_error_msg(GOT_ERR_NOT_REF, msg);
159 const struct got_error *
160 got_error_uuid(uint32_t uuid_status, const char *prefix)
162 switch (uuid_status) {
163 case uuid_s_ok:
164 return NULL;
165 case uuid_s_bad_version:
166 return got_error(GOT_ERR_UUID_VERSION);
167 case uuid_s_invalid_string_uuid:
168 return got_error(GOT_ERR_UUID_INVALID);
169 case uuid_s_no_memory:
170 return got_error_set_errno(ENOMEM, prefix);
171 default:
172 return got_error(GOT_ERR_UUID);
176 const struct got_error *
177 got_error_path(const char *path, int code)
179 static struct got_error err;
180 static char msg[PATH_MAX + 128];
181 int i;
183 for (i = 0; i < nitems(got_errors); i++) {
184 if (code == got_errors[i].code) {
185 err.code = code;
186 snprintf(msg, sizeof(msg), "%s: %s", path,
187 got_errors[i].msg);
188 err.msg = msg;
189 return &err;
193 abort();