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.
27 #include "got_compat.h"
29 #include "got_error.h"
30 #include "got_object.h"
32 #include "got_lib_delta.h"
33 #include "got_lib_inflate.h"
34 #include "got_lib_object.h"
35 #include "got_lib_sha1.h"
38 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
41 #if defined(__GLIBC__)
43 * The autoconf test for strerror_r is broken in current versions
44 * of autoconf: https://savannah.gnu.org/support/?110367
46 #define strerror_r __xpg_strerror_r
49 static struct got_custom_error
{
54 static struct got_custom_error
*
57 static unsigned int idx
;
58 return &custom_errors
[(idx
++) % nitems(custom_errors
)];
61 const struct got_error
*
66 for (i
= 0; i
< nitems(got_errors
); i
++) {
67 if (code
== got_errors
[i
].code
)
68 return &got_errors
[i
];
74 const struct got_error
*
75 got_error_msg(int code
, const char *msg
)
77 struct got_custom_error
*cerr
= get_custom_err();
78 struct got_error
*err
= &cerr
->err
;
81 for (i
= 0; i
< nitems(got_errors
); i
++) {
82 if (code
== got_errors
[i
].code
) {
84 strlcpy(cerr
->msg
, msg
, sizeof(cerr
->msg
));
93 const struct got_error
*
94 got_error_from_errno(const char *prefix
)
96 struct got_custom_error
*cerr
= get_custom_err();
97 struct got_error
*err
= &cerr
->err
;
100 strerror_r(errno
, strerr
, sizeof(strerr
));
101 snprintf(cerr
->msg
, sizeof(cerr
->msg
), "%s: %s", prefix
, strerr
);
103 err
->code
= GOT_ERR_ERRNO
;
104 err
->msg
= cerr
->msg
;
108 const struct got_error
*
109 got_error_from_errno2(const char *prefix
, const char *prefix2
)
111 struct got_custom_error
*cerr
= get_custom_err();
112 struct got_error
*err
= &cerr
->err
;
115 strerror_r(errno
, strerr
, sizeof(strerr
));
116 snprintf(cerr
->msg
, sizeof(cerr
->msg
), "%s: %s: %s", prefix
, prefix2
,
119 err
->code
= GOT_ERR_ERRNO
;
120 err
->msg
= cerr
->msg
;
124 const struct got_error
*
125 got_error_from_errno3(const char *prefix
, const char *prefix2
,
128 struct got_custom_error
*cerr
= get_custom_err();
129 struct got_error
*err
= &cerr
->err
;
132 strerror_r(errno
, strerr
, sizeof(strerr
));
133 snprintf(cerr
->msg
, sizeof(cerr
->msg
), "%s: %s: %s: %s", prefix
,
134 prefix2
, prefix3
, strerr
);
136 err
->code
= GOT_ERR_ERRNO
;
137 err
->msg
= cerr
->msg
;
141 const struct got_error
*
142 got_error_from_errno_fmt(const char *fmt
, ...)
144 struct got_custom_error
*cerr
= get_custom_err();
145 struct got_error
*err
= &cerr
->err
;
146 char buf
[PATH_MAX
* 4];
151 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
156 * The autoconf test for strerror_r is broken in current versions
157 * of autoconf: https://savannah.gnu.org/support/?110367
159 __xpg_strerror_r(errno
, strerr
, sizeof(strerr
));
161 strerror_r(errno
, strerr
, sizeof(strerr
));
163 snprintf(cerr
->msg
, sizeof(cerr
->msg
), "%s: %s", buf
, strerr
);
165 err
->code
= GOT_ERR_ERRNO
;
166 err
->msg
= cerr
->msg
;
170 const struct got_error
*
171 got_error_set_errno(int code
, const char *prefix
)
174 return got_error_from_errno(prefix
);
177 const struct got_error
*
178 got_ferror(FILE *f
, int code
)
181 return got_error_from_errno("");
182 return got_error(code
);
185 const struct got_error
*
186 got_error_no_obj(struct got_object_id
*id
)
188 char msg
[sizeof("object not found") + SHA1_DIGEST_STRING_LENGTH
];
189 char id_str
[SHA1_DIGEST_STRING_LENGTH
];
192 if (!got_sha1_digest_to_str(id
->sha1
, id_str
, sizeof(id_str
)))
193 return got_error(GOT_ERR_NO_OBJ
);
195 ret
= snprintf(msg
, sizeof(msg
), "object %s not found", id_str
);
196 if (ret
== -1 || ret
>= sizeof(msg
))
197 return got_error(GOT_ERR_NO_OBJ
);
199 return got_error_msg(GOT_ERR_NO_OBJ
, msg
);
202 const struct got_error
*
203 got_error_not_ref(const char *refname
)
205 char msg
[sizeof("reference not found") + 1004];
208 ret
= snprintf(msg
, sizeof(msg
), "reference %s not found", refname
);
209 if (ret
== -1 || ret
>= sizeof(msg
))
210 return got_error(GOT_ERR_NOT_REF
);
212 return got_error_msg(GOT_ERR_NOT_REF
, msg
);
215 const struct got_error
*
216 got_error_uuid(uint32_t uuid_status
, const char *prefix
)
218 switch (uuid_status
) {
221 case uuid_s_bad_version
:
222 return got_error(GOT_ERR_UUID_VERSION
);
223 case uuid_s_invalid_string_uuid
:
224 return got_error(GOT_ERR_UUID_INVALID
);
225 case uuid_s_no_memory
:
226 return got_error_set_errno(ENOMEM
, prefix
);
228 return got_error(GOT_ERR_UUID
);
232 const struct got_error
*
233 got_error_path(const char *path
, int code
)
235 struct got_custom_error
*cerr
= get_custom_err();
236 struct got_error
*err
= &cerr
->err
;
239 for (i
= 0; i
< nitems(got_errors
); i
++) {
240 if (code
== got_errors
[i
].code
) {
242 snprintf(cerr
->msg
, sizeof(cerr
->msg
), "%s: %s", path
,
244 err
->msg
= cerr
->msg
;
252 const struct got_error
*
253 got_error_fmt(int code
, const char *fmt
, ...)
255 struct got_custom_error
*cerr
= get_custom_err();
256 struct got_error
*err
= &cerr
->err
;
257 char buf
[PATH_MAX
* 4];
262 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
265 for (i
= 0; i
< nitems(got_errors
); i
++) {
266 if (code
== got_errors
[i
].code
) {
268 snprintf(cerr
->msg
, sizeof(cerr
->msg
), "%s: %s", buf
,
270 err
->msg
= cerr
->msg
;
279 got_err_open_nofollow_on_symlink(void)
282 * Check whether open(2) with O_NOFOLLOW failed on a symlink.
283 * Posix mandates ELOOP and OpenBSD follows it. Others return
284 * different error codes. We carry this workaround to help the
285 * portable version a little.
287 return (errno
== ELOOP