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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
/** \ingroup rpmio
* \file lib/rpmgi.c
*/
#include "system.h"
#include <errno.h>
#include <rpm/rpmtypes.h>
#include <rpm/rpmlib.h> /* rpmReadPackageFile */
#include <rpm/rpmts.h>
#include <rpm/rpmmacro.h> /* XXX rpmExpand */
#include <rpm/rpmfileutil.h>
#include <rpm/rpmlog.h>
#include "lib/rpmgi.h"
#include "lib/manifest.h"
#include "debug.h"
RPM_GNUC_INTERNAL
rpmgiFlags giFlags = RPMGI_NONE;
/** \ingroup rpmgi
*/
struct rpmgi_s {
rpmts ts; /*!< Iterator transaction set. */
rpmgiFlags flags; /*!< Iterator control bits. */
int i; /*!< Element index. */
int errors;
ARGV_t argv;
int argc;
};
/**
* Open a file after macro expanding path.
* @todo There are two error messages printed on header, then manifest failures.
* @param path file path
* @param fmode open mode
* @return file handle
*/
static FD_t rpmgiOpen(const char * path, const char * fmode)
{
char * fn = rpmExpand(path, NULL);
FD_t fd = Fopen(fn, fmode);
if (fd == NULL || Ferror(fd)) {
rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), fn, Fstrerror(fd));
if (fd != NULL) (void) Fclose(fd);
fd = NULL;
}
free(fn);
return fd;
}
/**
* Load manifest into iterator arg list.
* @param gi generalized iterator
* @param path file path
* @return RPMRC_OK on success
*/
static rpmRC rpmgiLoadManifest(rpmgi gi, const char * path)
{
FD_t fd = rpmgiOpen(path, "r.ufdio");
rpmRC rpmrc = RPMRC_FAIL;
if (fd != NULL) {
rpmrc = rpmReadPackageManifest(fd, &gi->argc, &gi->argv);
(void) Fclose(fd);
}
return rpmrc;
}
/**
* Return header from package.
* @param gi generalized iterator
* @param path file path
* @retval hdrp header (NULL on failure)
* @return 1 if path could be opened, 0 if not
*/
static int rpmgiReadHeader(rpmgi gi, const char * path, Header * hdrp)
{
FD_t fd = rpmgiOpen(path, "r.ufdio");
Header h = NULL;
if (fd != NULL) {
/* XXX what if path needs expansion? */
rpmRC rpmrc = rpmReadPackageFile(gi->ts, fd, path, &h);
(void) Fclose(fd);
switch (rpmrc) {
case RPMRC_NOTFOUND:
/* XXX Read a package manifest. Restart ftswalk on success. */
case RPMRC_FAIL:
default:
h = headerFree(h);
break;
case RPMRC_NOTTRUSTED:
case RPMRC_NOKEY:
case RPMRC_OK:
break;
}
}
*hdrp = h;
return (fd != NULL);
}
/**
* Read next header from package, lazily expanding manifests as found.
* @todo An empty file read as manifest truncates argv returning RPMRC_NOTFOUND.
* @todo Chained manifests lose an arg someplace.
* @param gi generalized iterator
* @return header on success
*/
static Header rpmgiLoadReadHeader(rpmgi gi)
{
Header h = NULL;
if (gi->argv != NULL && gi->argv[gi->i] != NULL)
do {
char * fn = gi->argv[gi->i];
int rc = rpmgiReadHeader(gi, fn, &h);
if (h != NULL || (gi->flags & RPMGI_NOMANIFEST) || rc == 0)
break;
/* Not a header, so try for a manifest. */
gi->argv[gi->i] = NULL; /* Mark the insertion point */
if (rpmgiLoadManifest(gi, fn) != RPMRC_OK) {
gi->argv[gi->i] = fn; /* Manifest failed, restore fn */
rpmlog(RPMLOG_ERR,
_("%s: not an rpm package (or package manifest)\n"), fn);
break;
}
fn = _free(fn);
} while (1);
return h;
}
/**
* Append globbed arg list to iterator.
* @param gi generalized iterator
* @param argv arg list to be globbed (or NULL)
*/
static void rpmgiGlobArgv(rpmgi gi, ARGV_const_t argv)
{
if (argv == NULL) return;
/* XXX Expand globs only if requested */
if ((gi->flags & RPMGI_NOGLOB)) {
argvAppend(&gi->argv, argv);
} else {
const char * arg;
while ((arg = *argv++) != NULL) {
char * t = rpmEscapeSpaces(arg);
char ** av = NULL;
if (rpmGlob(t, NULL, &av) == 0) {
argvAppend(&gi->argv, av);
argvFree(av);
}
free(t);
}
}
gi->argc = argvCount(gi->argv);
return;
}
rpmgi rpmgiFree(rpmgi gi)
{
if (gi == NULL)
return NULL;
gi->ts = rpmtsFree(gi->ts);
gi->argv = argvFree(gi->argv);
memset(gi, 0, sizeof(*gi)); /* XXX trash and burn */
gi = _free(gi);
return NULL;
}
rpmgi rpmgiNew(rpmts ts, rpmgiFlags flags, ARGV_const_t argv)
{
rpmgi gi = xcalloc(1, sizeof(*gi));
gi->ts = rpmtsLink(ts);
gi->flags = flags;
gi->i = -1;
gi->errors = 0;
gi->flags = flags;
gi->argv = argvNew();
gi->argc = 0;
rpmgiGlobArgv(gi, argv);
return gi;
}
Header rpmgiNext(rpmgi gi)
{
Header h = NULL;
if (gi != NULL && ++gi->i >= 0) {
/*
* Read next header, lazily expanding manifests as found,
* count + skip errors.
*/
while (gi->i < gi->argc) {
if ((h = rpmgiLoadReadHeader(gi)) != NULL)
break;
gi->errors++;
gi->i++;
}
/* Out of things to try, end of iteration */
if (h == NULL)
gi->i = -1;
}
return h;
}
int rpmgiNumErrors(rpmgi gi)
{
return (gi != NULL ? gi->errors : -1);
}
|