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>
27 #include "got_error.h"
28 #include "got_object.h"
30 #include "got_lib_delta.h"
31 #include "got_lib_inflate.h"
32 #include "got_lib_object.h"
33 #include "got_lib_sha1.h"
36 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
39 const struct got_error *
44 for (i = 0; i < nitems(got_errors); i++) {
45 if (code == got_errors[i].code)
46 return &got_errors[i];
52 const struct got_error *
53 got_error_msg(int code, const char *msg)
55 static struct got_error err;
58 for (i = 0; i < nitems(got_errors); i++) {
59 if (code == got_errors[i].code) {
69 const struct got_error *
70 got_error_from_errno()
72 static struct got_error err;
74 err.code = GOT_ERR_ERRNO;
75 err.msg = strerror(errno);
79 const struct got_error *
80 got_error_set_errno(int code)
83 return got_error_from_errno();
86 const struct got_error *
87 got_ferror(FILE *f, int code)
90 return got_error_from_errno();
91 return got_error(code);
94 const struct got_error *
95 got_error_no_obj(struct got_object_id *id)
97 static char msg[sizeof("object not found") +
98 SHA1_DIGEST_STRING_LENGTH];
99 char id_str[SHA1_DIGEST_STRING_LENGTH];
102 if (!got_sha1_digest_to_str(id->sha1, id_str, sizeof(id_str)))
103 return got_error(GOT_ERR_NO_OBJ);
105 ret = snprintf(msg, sizeof(msg), "object %s not found", id_str);
106 if (ret == -1 || ret >= sizeof(msg))
107 return got_error(GOT_ERR_NO_OBJ);
109 return got_error_msg(GOT_ERR_NO_OBJ, msg);
112 const struct got_error *
113 got_error_not_ref(const char *refname)
115 static char msg[sizeof("reference not found") + 1004];
118 ret = snprintf(msg, sizeof(msg), "reference %s not found", refname);
119 if (ret == -1 || ret >= sizeof(msg))
120 return got_error(GOT_ERR_NOT_REF);
122 return got_error_msg(GOT_ERR_NOT_REF, msg);
125 const struct got_error *
126 got_error_uuid(uint32_t uuid_status)
128 switch (uuid_status) {
131 case uuid_s_bad_version:
132 return got_error(GOT_ERR_UUID_VERSION);
133 case uuid_s_invalid_string_uuid:
134 return got_error(GOT_ERR_UUID_INVALID);
135 case uuid_s_no_memory:
136 return got_error_set_errno(ENOMEM);
138 return got_error(GOT_ERR_UUID);