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>
26 #include "got_error.h"
27 #include "got_object.h"
29 #include "got_lib_delta.h"
30 #include "got_lib_inflate.h"
31 #include "got_lib_object.h"
32 #include "got_lib_sha1.h"
34 #ifndef nitems
35 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
36 #endif
38 const struct got_error *
39 got_error(int code)
40 {
41 int i;
43 for (i = 0; i < nitems(got_errors); i++) {
44 if (code == got_errors[i].code)
45 return &got_errors[i];
46 }
48 abort();
49 }
51 const struct got_error *
52 got_error_msg(int code, const char *msg)
53 {
54 static struct got_error err;
55 int i;
57 for (i = 0; i < nitems(got_errors); i++) {
58 if (code == got_errors[i].code) {
59 err.code = code;
60 err.msg = msg;
61 return &err;
62 }
63 }
65 abort();
66 }
68 const struct got_error *
69 got_error_from_errno()
70 {
71 static struct got_error err;
73 err.code = GOT_ERR_ERRNO;
74 err.msg = strerror(errno);
75 return &err;
76 }
78 const struct got_error *
79 got_error_set_errno(int code)
80 {
81 errno = code;
82 return got_error_from_errno();
83 }
85 const struct got_error *
86 got_ferror(FILE *f, int code)
87 {
88 if (ferror(f))
89 return got_error_from_errno();
90 return got_error(code);
91 }
93 const struct got_error *
94 got_error_no_obj(struct got_object_id *id)
95 {
96 static char msg[sizeof("object not found") +
97 SHA1_DIGEST_STRING_LENGTH];
98 char id_str[SHA1_DIGEST_STRING_LENGTH];
99 int ret;
101 if (!got_sha1_digest_to_str(id->sha1, id_str, sizeof(id_str)))
102 return got_error(GOT_ERR_NO_OBJ);
104 ret = snprintf(msg, sizeof(msg), "object %s not found", id_str);
105 if (ret == -1 || ret >= sizeof(msg))
106 return got_error(GOT_ERR_NO_OBJ);
108 return got_error_msg(GOT_ERR_NO_OBJ, msg);