Branch data Line data Source code
1 : : /*
2 : : * libdpkg - Debian packaging suite library routines
3 : : * error.c - error message reporting
4 : : *
5 : : * Copyright © 2011-2015 Guillem Jover <guillem@debian.org>
6 : : *
7 : : * This is free software; you can redistribute it and/or modify
8 : : * it under the terms of the GNU General Public License as published by
9 : : * the Free Software Foundation; either version 2 of the License, or
10 : : * (at your option) any later version.
11 : : *
12 : : * This is distributed in the hope that it will be useful,
13 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : : * GNU General Public License for more details.
16 : : *
17 : : * You should have received a copy of the GNU General Public License
18 : : * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 : : */
20 : :
21 : : #include <config.h>
22 : : #include <compat.h>
23 : :
24 : : #include <errno.h>
25 : : #include <string.h>
26 : : #include <stdlib.h>
27 : :
28 : : #include <dpkg/dpkg.h>
29 : : #include <dpkg/varbuf.h>
30 : : #include <dpkg/error.h>
31 : :
32 : : static void DPKG_ATTR_VPRINTF(4)
33 : 76 : dpkg_error_set(struct dpkg_error *err, enum dpkg_msg_type type, int syserrno,
34 : : const char *fmt, va_list args)
35 : : {
36 : 76 : struct varbuf str = VARBUF_INIT;
37 : :
38 [ + + ]: 76 : if (err == NULL)
39 : 3 : return;
40 : :
41 : 73 : err->type = type;
42 : 73 : err->syserrno = syserrno;
43 : :
44 : 73 : varbuf_vprintf(&str, fmt, args);
45 [ + + ]: 73 : if (syserrno)
46 : 3 : varbuf_printf(&str, " (%s)", strerror(syserrno));
47 : :
48 : 73 : err->str = str.buf;
49 : : }
50 : :
51 : : bool
52 : 26 : dpkg_has_error(struct dpkg_error *err)
53 : : {
54 [ + - - + ]: 26 : return err != NULL && err->type != DPKG_MSG_NONE;
55 : : }
56 : :
57 : : int
58 : 53 : dpkg_put_warn(struct dpkg_error *err, const char *fmt, ...)
59 : : {
60 : : va_list args;
61 : :
62 : 53 : va_start(args, fmt);
63 : 53 : dpkg_error_set(err, DPKG_MSG_WARN, 0, fmt, args);
64 : 53 : va_end(args);
65 : :
66 : 53 : return -1;
67 : : }
68 : :
69 : : int
70 : 19 : dpkg_put_error(struct dpkg_error *err, const char *fmt, ...)
71 : : {
72 : : va_list args;
73 : :
74 : 19 : va_start(args, fmt);
75 : 19 : dpkg_error_set(err, DPKG_MSG_ERROR, 0, fmt, args);
76 : 19 : va_end(args);
77 : :
78 : 19 : return -1;
79 : : }
80 : :
81 : : int
82 : 4 : dpkg_put_errno(struct dpkg_error *err, const char *fmt, ...)
83 : : {
84 : : va_list args;
85 : :
86 : 4 : va_start(args, fmt);
87 : 4 : dpkg_error_set(err, DPKG_MSG_ERROR, errno, fmt, args);
88 : 4 : va_end(args);
89 : :
90 : 4 : return -1;
91 : : }
92 : :
93 : : void
94 : 0 : dpkg_error_print(struct dpkg_error *err, const char *fmt, ...)
95 : : {
96 : : va_list args;
97 : : char *str;
98 : :
99 : 0 : va_start(args, fmt);
100 : 0 : m_vasprintf(&str, fmt, args);
101 : 0 : va_end(args);
102 : :
103 [ # # ]: 0 : if (err->type == DPKG_MSG_WARN)
104 : 0 : warning("%s: %s", str, err->str);
105 : : else
106 : 0 : ohshit("%s: %s", str, err->str);
107 : :
108 : 0 : free(str);
109 : 0 : }
110 : :
111 : : void
112 : 0 : dpkg_error_move(struct dpkg_error *dst, struct dpkg_error *src)
113 : : {
114 : 0 : dst->type = src->type;
115 : 0 : src->type = DPKG_MSG_NONE;
116 : 0 : dst->str = src->str;
117 : 0 : src->str = NULL;
118 : 0 : }
119 : :
120 : : void
121 : 224 : dpkg_error_destroy(struct dpkg_error *err)
122 : : {
123 : 224 : err->type = DPKG_MSG_NONE;
124 : 224 : err->syserrno = 0;
125 : 224 : free(err->str);
126 : 224 : err->str = NULL;
127 : 224 : }
|