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>
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"
35 #ifndef nitems
36 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
37 #endif
39 const struct got_error *
40 got_error(int code)
41 {
42 int i;
44 for (i = 0; i < nitems(got_errors); i++) {
45 if (code == got_errors[i].code)
46 return &got_errors[i];
47 }
49 abort();
50 }
52 const struct got_error *
53 got_error_msg(int code, const char *msg)
54 {
55 static struct got_error err;
56 int i;
58 for (i = 0; i < nitems(got_errors); i++) {
59 if (code == got_errors[i].code) {
60 err.code = code;
61 err.msg = msg;
62 return &err;
63 }
64 }
66 abort();
67 }
69 const struct got_error *
70 got_error_from_errno()
71 {
72 static struct got_error err;
74 err.code = GOT_ERR_ERRNO;
75 err.msg = strerror(errno);
76 return &err;
77 }
79 const struct got_error *
80 got_error_set_errno(int code)
81 {
82 errno = code;
83 return got_error_from_errno();
84 }
86 const struct got_error *
87 got_ferror(FILE *f, int code)
88 {
89 if (ferror(f))
90 return got_error_from_errno();
91 return got_error(code);
92 }
94 const struct got_error *
95 got_error_no_obj(struct got_object_id *id)
96 {
97 static char msg[sizeof("object not found") +
98 SHA1_DIGEST_STRING_LENGTH];
99 char id_str[SHA1_DIGEST_STRING_LENGTH];
100 int ret;
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];
116 int ret;
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) {
129 case uuid_s_ok:
130 return NULL;
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);
137 default:
138 return got_error(GOT_ERR_UUID);