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