Blame


1 7b19e0f1 2017-11-05 stsp /*
2 72bcf0f9 2018-01-12 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 7b19e0f1 2017-11-05 stsp *
4 7b19e0f1 2017-11-05 stsp * Permission to use, copy, modify, and distribute this software for any
5 7b19e0f1 2017-11-05 stsp * purpose with or without fee is hereby granted, provided that the above
6 7b19e0f1 2017-11-05 stsp * copyright notice and this permission notice appear in all copies.
7 7b19e0f1 2017-11-05 stsp *
8 7b19e0f1 2017-11-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 7b19e0f1 2017-11-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 7b19e0f1 2017-11-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 7b19e0f1 2017-11-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 7b19e0f1 2017-11-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 7b19e0f1 2017-11-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 7b19e0f1 2017-11-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 7b19e0f1 2017-11-05 stsp */
16 7b19e0f1 2017-11-05 stsp
17 91a3d81f 2018-11-11 stsp #include <sys/queue.h>
18 91a3d81f 2018-11-11 stsp
19 f334529e 2018-01-12 stsp #include <errno.h>
20 8251fdbc 2018-01-12 stsp #include <stdio.h>
21 f334529e 2018-01-12 stsp #include <stdlib.h>
22 f334529e 2018-01-12 stsp #include <string.h>
23 91a3d81f 2018-11-11 stsp #include <sha1.h>
24 91a3d81f 2018-11-11 stsp #include <zlib.h>
25 09589288 2019-03-10 stsp #include <uuid.h>
26 48b8b0eb 2019-05-11 jcs #include <sys/param.h>
27 f334529e 2018-01-12 stsp
28 4027f31a 2017-11-04 stsp #include "got_error.h"
29 91a3d81f 2018-11-11 stsp #include "got_object.h"
30 4027f31a 2017-11-04 stsp
31 91a3d81f 2018-11-11 stsp #include "got_lib_delta.h"
32 91a3d81f 2018-11-11 stsp #include "got_lib_inflate.h"
33 91a3d81f 2018-11-11 stsp #include "got_lib_object.h"
34 91a3d81f 2018-11-11 stsp #include "got_lib_sha1.h"
35 91a3d81f 2018-11-11 stsp
36 2b4402a2 2017-11-05 stsp #ifndef nitems
37 2b4402a2 2017-11-05 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
38 2b4402a2 2017-11-05 stsp #endif
39 4027f31a 2017-11-04 stsp
40 4027f31a 2017-11-04 stsp const struct got_error *
41 4027f31a 2017-11-04 stsp got_error(int code)
42 4027f31a 2017-11-04 stsp {
43 4027f31a 2017-11-04 stsp int i;
44 4027f31a 2017-11-04 stsp
45 4027f31a 2017-11-04 stsp for (i = 0; i < nitems(got_errors); i++) {
46 4027f31a 2017-11-04 stsp if (code == got_errors[i].code)
47 4027f31a 2017-11-04 stsp return &got_errors[i];
48 4027f31a 2017-11-04 stsp }
49 4027f31a 2017-11-04 stsp
50 f334529e 2018-01-12 stsp abort();
51 4027f31a 2017-11-04 stsp }
52 f334529e 2018-01-12 stsp
53 f334529e 2018-01-12 stsp const struct got_error *
54 91a3d81f 2018-11-11 stsp got_error_msg(int code, const char *msg)
55 91a3d81f 2018-11-11 stsp {
56 91a3d81f 2018-11-11 stsp static struct got_error err;
57 91a3d81f 2018-11-11 stsp int i;
58 91a3d81f 2018-11-11 stsp
59 91a3d81f 2018-11-11 stsp for (i = 0; i < nitems(got_errors); i++) {
60 91a3d81f 2018-11-11 stsp if (code == got_errors[i].code) {
61 91a3d81f 2018-11-11 stsp err.code = code;
62 91a3d81f 2018-11-11 stsp err.msg = msg;
63 8fa9fd14 2018-11-11 stsp return &err;
64 91a3d81f 2018-11-11 stsp }
65 91a3d81f 2018-11-11 stsp }
66 91a3d81f 2018-11-11 stsp
67 91a3d81f 2018-11-11 stsp abort();
68 91a3d81f 2018-11-11 stsp }
69 91a3d81f 2018-11-11 stsp
70 91a3d81f 2018-11-11 stsp const struct got_error *
71 10689f3a 2019-05-11 jcs got_error_from_errno(void)
72 f334529e 2018-01-12 stsp {
73 f334529e 2018-01-12 stsp static struct got_error err;
74 f334529e 2018-01-12 stsp
75 f334529e 2018-01-12 stsp err.code = GOT_ERR_ERRNO;
76 b4691ea5 2018-04-02 stsp err.msg = strerror(errno);
77 f334529e 2018-01-12 stsp return &err;
78 f334529e 2018-01-12 stsp }
79 8251fdbc 2018-01-12 stsp
80 8251fdbc 2018-01-12 stsp const struct got_error *
81 48b8b0eb 2019-05-11 jcs got_error_prefix_errno(const char *prefix)
82 48b8b0eb 2019-05-11 jcs {
83 48b8b0eb 2019-05-11 jcs static struct got_error err;
84 48b8b0eb 2019-05-11 jcs static char err_msg[MAXPATHLEN + 20];
85 48b8b0eb 2019-05-11 jcs
86 48b8b0eb 2019-05-11 jcs snprintf(err_msg, sizeof(err_msg), "%s: %s", prefix,
87 48b8b0eb 2019-05-11 jcs strerror(errno));
88 48b8b0eb 2019-05-11 jcs
89 48b8b0eb 2019-05-11 jcs err.code = GOT_ERR_ERRNO;
90 48b8b0eb 2019-05-11 jcs err.msg = err_msg;
91 48b8b0eb 2019-05-11 jcs return &err;
92 48b8b0eb 2019-05-11 jcs }
93 48b8b0eb 2019-05-11 jcs
94 48b8b0eb 2019-05-11 jcs const struct got_error *
95 1a76625f 2018-10-22 stsp got_error_set_errno(int code)
96 1a76625f 2018-10-22 stsp {
97 1a76625f 2018-10-22 stsp errno = code;
98 1a76625f 2018-10-22 stsp return got_error_from_errno();
99 1a76625f 2018-10-22 stsp }
100 1a76625f 2018-10-22 stsp
101 1a76625f 2018-10-22 stsp const struct got_error *
102 8251fdbc 2018-01-12 stsp got_ferror(FILE *f, int code)
103 8251fdbc 2018-01-12 stsp {
104 8251fdbc 2018-01-12 stsp if (ferror(f))
105 8251fdbc 2018-01-12 stsp return got_error_from_errno();
106 8251fdbc 2018-01-12 stsp return got_error(code);
107 8251fdbc 2018-01-12 stsp }
108 91a3d81f 2018-11-11 stsp
109 91a3d81f 2018-11-11 stsp const struct got_error *
110 91a3d81f 2018-11-11 stsp got_error_no_obj(struct got_object_id *id)
111 91a3d81f 2018-11-11 stsp {
112 91a3d81f 2018-11-11 stsp static char msg[sizeof("object not found") +
113 91a3d81f 2018-11-11 stsp SHA1_DIGEST_STRING_LENGTH];
114 91a3d81f 2018-11-11 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
115 91a3d81f 2018-11-11 stsp int ret;
116 91a3d81f 2018-11-11 stsp
117 91a3d81f 2018-11-11 stsp if (!got_sha1_digest_to_str(id->sha1, id_str, sizeof(id_str)))
118 91a3d81f 2018-11-11 stsp return got_error(GOT_ERR_NO_OBJ);
119 91a3d81f 2018-11-11 stsp
120 91a3d81f 2018-11-11 stsp ret = snprintf(msg, sizeof(msg), "object %s not found", id_str);
121 91a3d81f 2018-11-11 stsp if (ret == -1 || ret >= sizeof(msg))
122 91a3d81f 2018-11-11 stsp return got_error(GOT_ERR_NO_OBJ);
123 91a3d81f 2018-11-11 stsp
124 91a3d81f 2018-11-11 stsp return got_error_msg(GOT_ERR_NO_OBJ, msg);
125 91a3d81f 2018-11-11 stsp }
126 2aa0475c 2019-02-03 stsp
127 2aa0475c 2019-02-03 stsp const struct got_error *
128 2aa0475c 2019-02-03 stsp got_error_not_ref(const char *refname)
129 2aa0475c 2019-02-03 stsp {
130 2aa0475c 2019-02-03 stsp static char msg[sizeof("reference not found") + 1004];
131 2aa0475c 2019-02-03 stsp int ret;
132 2aa0475c 2019-02-03 stsp
133 2aa0475c 2019-02-03 stsp ret = snprintf(msg, sizeof(msg), "reference %s not found", refname);
134 2aa0475c 2019-02-03 stsp if (ret == -1 || ret >= sizeof(msg))
135 2aa0475c 2019-02-03 stsp return got_error(GOT_ERR_NOT_REF);
136 2aa0475c 2019-02-03 stsp
137 2aa0475c 2019-02-03 stsp return got_error_msg(GOT_ERR_NOT_REF, msg);
138 2aa0475c 2019-02-03 stsp }
139 09589288 2019-03-10 stsp
140 09589288 2019-03-10 stsp const struct got_error *
141 09589288 2019-03-10 stsp got_error_uuid(uint32_t uuid_status)
142 09589288 2019-03-10 stsp {
143 09589288 2019-03-10 stsp switch (uuid_status) {
144 09589288 2019-03-10 stsp case uuid_s_ok:
145 09589288 2019-03-10 stsp return NULL;
146 09589288 2019-03-10 stsp case uuid_s_bad_version:
147 09589288 2019-03-10 stsp return got_error(GOT_ERR_UUID_VERSION);
148 09589288 2019-03-10 stsp case uuid_s_invalid_string_uuid:
149 09589288 2019-03-10 stsp return got_error(GOT_ERR_UUID_INVALID);
150 09589288 2019-03-10 stsp case uuid_s_no_memory:
151 09589288 2019-03-10 stsp return got_error_set_errno(ENOMEM);
152 09589288 2019-03-10 stsp default:
153 09589288 2019-03-10 stsp return got_error(GOT_ERR_UUID);
154 09589288 2019-03-10 stsp }
155 09589288 2019-03-10 stsp }