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 <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sha1.h>
24 #include <zlib.h>
25 #include <uuid.h>
26 #include <sys/param.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(void)
72 {
73 static struct got_error err;
75 err.code = GOT_ERR_ERRNO;
76 err.msg = strerror(errno);
77 return &err;
78 }
80 const struct got_error *
81 got_error_prefix_errno(const char *prefix)
82 {
83 static struct got_error err;
84 static char err_msg[MAXPATHLEN + 20];
86 snprintf(err_msg, sizeof(err_msg), "%s: %s", prefix,
87 strerror(errno));
89 err.code = GOT_ERR_ERRNO;
90 err.msg = err_msg;
91 return &err;
92 }
94 const struct got_error *
95 got_error_set_errno(int code)
96 {
97 errno = code;
98 return got_error_from_errno();
99 }
101 const struct got_error *
102 got_ferror(FILE *f, int code)
104 if (ferror(f))
105 return got_error_from_errno();
106 return got_error(code);
109 const struct got_error *
110 got_error_no_obj(struct got_object_id *id)
112 static char msg[sizeof("object not found") +
113 SHA1_DIGEST_STRING_LENGTH];
114 char id_str[SHA1_DIGEST_STRING_LENGTH];
115 int ret;
117 if (!got_sha1_digest_to_str(id->sha1, id_str, sizeof(id_str)))
118 return got_error(GOT_ERR_NO_OBJ);
120 ret = snprintf(msg, sizeof(msg), "object %s not found", id_str);
121 if (ret == -1 || ret >= sizeof(msg))
122 return got_error(GOT_ERR_NO_OBJ);
124 return got_error_msg(GOT_ERR_NO_OBJ, msg);
127 const struct got_error *
128 got_error_not_ref(const char *refname)
130 static char msg[sizeof("reference not found") + 1004];
131 int ret;
133 ret = snprintf(msg, sizeof(msg), "reference %s not found", refname);
134 if (ret == -1 || ret >= sizeof(msg))
135 return got_error(GOT_ERR_NOT_REF);
137 return got_error_msg(GOT_ERR_NOT_REF, msg);
140 const struct got_error *
141 got_error_uuid(uint32_t uuid_status)
143 switch (uuid_status) {
144 case uuid_s_ok:
145 return NULL;
146 case uuid_s_bad_version:
147 return got_error(GOT_ERR_UUID_VERSION);
148 case uuid_s_invalid_string_uuid:
149 return got_error(GOT_ERR_UUID_INVALID);
150 case uuid_s_no_memory:
151 return got_error_set_errno(ENOMEM);
152 default:
153 return got_error(GOT_ERR_UUID);