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 f334529e 2018-01-12 stsp
26 4027f31a 2017-11-04 stsp #include "got_error.h"
27 91a3d81f 2018-11-11 stsp #include "got_object.h"
28 4027f31a 2017-11-04 stsp
29 91a3d81f 2018-11-11 stsp #include "got_lib_delta.h"
30 91a3d81f 2018-11-11 stsp #include "got_lib_inflate.h"
31 91a3d81f 2018-11-11 stsp #include "got_lib_object.h"
32 91a3d81f 2018-11-11 stsp #include "got_lib_sha1.h"
33 91a3d81f 2018-11-11 stsp
34 2b4402a2 2017-11-05 stsp #ifndef nitems
35 2b4402a2 2017-11-05 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
36 2b4402a2 2017-11-05 stsp #endif
37 4027f31a 2017-11-04 stsp
38 4027f31a 2017-11-04 stsp const struct got_error *
39 4027f31a 2017-11-04 stsp got_error(int code)
40 4027f31a 2017-11-04 stsp {
41 4027f31a 2017-11-04 stsp int i;
42 4027f31a 2017-11-04 stsp
43 4027f31a 2017-11-04 stsp for (i = 0; i < nitems(got_errors); i++) {
44 4027f31a 2017-11-04 stsp if (code == got_errors[i].code)
45 4027f31a 2017-11-04 stsp return &got_errors[i];
46 4027f31a 2017-11-04 stsp }
47 4027f31a 2017-11-04 stsp
48 f334529e 2018-01-12 stsp abort();
49 4027f31a 2017-11-04 stsp }
50 f334529e 2018-01-12 stsp
51 f334529e 2018-01-12 stsp const struct got_error *
52 91a3d81f 2018-11-11 stsp got_error_msg(int code, const char *msg)
53 91a3d81f 2018-11-11 stsp {
54 91a3d81f 2018-11-11 stsp static struct got_error err;
55 91a3d81f 2018-11-11 stsp int i;
56 91a3d81f 2018-11-11 stsp
57 91a3d81f 2018-11-11 stsp for (i = 0; i < nitems(got_errors); i++) {
58 91a3d81f 2018-11-11 stsp if (code == got_errors[i].code) {
59 91a3d81f 2018-11-11 stsp err.code = code;
60 91a3d81f 2018-11-11 stsp err.msg = msg;
61 8fa9fd14 2018-11-11 stsp return &err;
62 91a3d81f 2018-11-11 stsp }
63 91a3d81f 2018-11-11 stsp }
64 91a3d81f 2018-11-11 stsp
65 91a3d81f 2018-11-11 stsp abort();
66 91a3d81f 2018-11-11 stsp }
67 91a3d81f 2018-11-11 stsp
68 91a3d81f 2018-11-11 stsp const struct got_error *
69 f334529e 2018-01-12 stsp got_error_from_errno()
70 f334529e 2018-01-12 stsp {
71 f334529e 2018-01-12 stsp static struct got_error err;
72 f334529e 2018-01-12 stsp
73 f334529e 2018-01-12 stsp err.code = GOT_ERR_ERRNO;
74 b4691ea5 2018-04-02 stsp err.msg = strerror(errno);
75 f334529e 2018-01-12 stsp return &err;
76 f334529e 2018-01-12 stsp }
77 8251fdbc 2018-01-12 stsp
78 8251fdbc 2018-01-12 stsp const struct got_error *
79 1a76625f 2018-10-22 stsp got_error_set_errno(int code)
80 1a76625f 2018-10-22 stsp {
81 1a76625f 2018-10-22 stsp errno = code;
82 1a76625f 2018-10-22 stsp return got_error_from_errno();
83 1a76625f 2018-10-22 stsp }
84 1a76625f 2018-10-22 stsp
85 1a76625f 2018-10-22 stsp const struct got_error *
86 8251fdbc 2018-01-12 stsp got_ferror(FILE *f, int code)
87 8251fdbc 2018-01-12 stsp {
88 8251fdbc 2018-01-12 stsp if (ferror(f))
89 8251fdbc 2018-01-12 stsp return got_error_from_errno();
90 8251fdbc 2018-01-12 stsp return got_error(code);
91 8251fdbc 2018-01-12 stsp }
92 91a3d81f 2018-11-11 stsp
93 91a3d81f 2018-11-11 stsp const struct got_error *
94 91a3d81f 2018-11-11 stsp got_error_no_obj(struct got_object_id *id)
95 91a3d81f 2018-11-11 stsp {
96 91a3d81f 2018-11-11 stsp static char msg[sizeof("object not found") +
97 91a3d81f 2018-11-11 stsp SHA1_DIGEST_STRING_LENGTH];
98 91a3d81f 2018-11-11 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
99 91a3d81f 2018-11-11 stsp int ret;
100 91a3d81f 2018-11-11 stsp
101 91a3d81f 2018-11-11 stsp if (!got_sha1_digest_to_str(id->sha1, id_str, sizeof(id_str)))
102 91a3d81f 2018-11-11 stsp return got_error(GOT_ERR_NO_OBJ);
103 91a3d81f 2018-11-11 stsp
104 91a3d81f 2018-11-11 stsp ret = snprintf(msg, sizeof(msg), "object %s not found", id_str);
105 91a3d81f 2018-11-11 stsp if (ret == -1 || ret >= sizeof(msg))
106 91a3d81f 2018-11-11 stsp return got_error(GOT_ERR_NO_OBJ);
107 91a3d81f 2018-11-11 stsp
108 91a3d81f 2018-11-11 stsp return got_error_msg(GOT_ERR_NO_OBJ, msg);
109 91a3d81f 2018-11-11 stsp }