2 * Copyright (c) 2018 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/queue.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"
37 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
40 const struct got_error *
45 for (i = 0; i < nitems(got_errors); i++) {
46 if (code == got_errors[i].code)
47 return &got_errors[i];
53 const struct got_error *
54 got_error_msg(int code, const char *msg)
56 static struct got_error err;
59 for (i = 0; i < nitems(got_errors); i++) {
60 if (code == got_errors[i].code) {
70 const struct got_error *
71 got_error_from_errno(const char *prefix)
73 static struct got_error err;
74 static char err_msg[PATH_MAX + 20];
76 snprintf(err_msg, sizeof(err_msg), "%s: %s", prefix,
79 err.code = GOT_ERR_ERRNO;
84 const struct got_error *
85 got_error_from_errno2(const char *prefix, const char *prefix2)
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,
93 err.code = GOT_ERR_ERRNO;
98 const struct got_error *
99 got_error_from_errno3(const char *prefix, const char *prefix2,
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;
113 const struct got_error *
114 got_error_set_errno(int code, const char *prefix)
117 return got_error_from_errno(prefix);
120 const struct got_error *
121 got_ferror(FILE *f, int code)
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];
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];
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) {
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);
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];
183 for (i = 0; i < nitems(got_errors); i++) {
184 if (code == got_errors[i].code) {
186 snprintf(msg, sizeof(msg), "%s: %s", path,