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)
dpkg_error_set(struct dpkg_error * err,enum dpkg_msg_type type,int syserrno,const char * fmt,va_list args)33 dpkg_error_set(struct dpkg_error *err, enum dpkg_msg_type type, int syserrno,
34 const char *fmt, va_list args)
35 {
36 struct varbuf str = VARBUF_INIT;
37
38 if (err == NULL)
39 return;
40
41 err->type = type;
42 err->syserrno = syserrno;
43
44 varbuf_vprintf(&str, fmt, args);
45 err->str = str.buf;
46 }
47
48 bool
dpkg_has_error(struct dpkg_error * err)49 dpkg_has_error(struct dpkg_error *err)
50 {
51 return err != NULL && err->type != DPKG_MSG_NONE;
52 }
53
54 int
dpkg_put_warn(struct dpkg_error * err,const char * fmt,...)55 dpkg_put_warn(struct dpkg_error *err, const char *fmt, ...)
56 {
57 va_list args;
58
59 va_start(args, fmt);
60 dpkg_error_set(err, DPKG_MSG_WARN, 0, fmt, args);
61 va_end(args);
62
63 return -1;
64 }
65
66 int
dpkg_put_error(struct dpkg_error * err,const char * fmt,...)67 dpkg_put_error(struct dpkg_error *err, const char *fmt, ...)
68 {
69 va_list args;
70
71 va_start(args, fmt);
72 dpkg_error_set(err, DPKG_MSG_ERROR, 0, fmt, args);
73 va_end(args);
74
75 return -1;
76 }
77
78 int
dpkg_put_errno(struct dpkg_error * err,const char * fmt,...)79 dpkg_put_errno(struct dpkg_error *err, const char *fmt, ...)
80 {
81 va_list args;
82 char *new_fmt;
83 int syserrno = errno;
84
85 new_fmt = str_fmt("%s (%s)", fmt, strerror(errno));
86
87 va_start(args, fmt);
88 dpkg_error_set(err, DPKG_MSG_ERROR, syserrno, new_fmt, args);
89 va_end(args);
90
91 free(new_fmt);
92
93 return -1;
94 }
95
96 void
dpkg_error_print(struct dpkg_error * err,const char * fmt,...)97 dpkg_error_print(struct dpkg_error *err, const char *fmt, ...)
98 {
99 va_list args;
100 char *str;
101
102 va_start(args, fmt);
103 m_vasprintf(&str, fmt, args);
104 va_end(args);
105
106 if (err->type == DPKG_MSG_WARN)
107 warning("%s: %s", str, err->str);
108 else
109 ohshit("%s: %s", str, err->str);
110
111 free(str);
112 }
113
114 void
dpkg_error_move(struct dpkg_error * dst,struct dpkg_error * src)115 dpkg_error_move(struct dpkg_error *dst, struct dpkg_error *src)
116 {
117 dst->type = src->type;
118 src->type = DPKG_MSG_NONE;
119 dst->str = src->str;
120 src->str = NULL;
121 }
122
123 void
dpkg_error_destroy(struct dpkg_error * err)124 dpkg_error_destroy(struct dpkg_error *err)
125 {
126 err->type = DPKG_MSG_NONE;
127 err->syserrno = 0;
128 free(err->str);
129 err->str = NULL;
130 }
131