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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
|
/**
* \file rpmdb/legacy.c
*/
#include "system.h"
#include "rpmio_internal.h"
#include <rpmlib.h>
#include "misc.h"
#include "legacy.h"
#include "debug.h"
#define alloca_strdup(_s) strcpy(alloca(strlen(_s)+1), (_s))
int domd5(const char * fn, unsigned char * digest, int asAscii)
{
const char * path;
unsigned char * md5sum = NULL;
size_t md5len;
int rc = 0;
switch(urlPath(fn, &path)) {
case URL_IS_PATH:
case URL_IS_UNKNOWN:
#if HAVE_MMAP
{ struct stat sb, * st = &sb;
DIGEST_CTX ctx;
void * mapped;
int fdno;
int xx;
if (stat(path, st) < 0
|| (fdno = open(path, O_RDONLY)) < 0)
return 1;
mapped = mmap(NULL, st->st_size, PROT_READ, MAP_SHARED, fdno, 0);
if (mapped == (void *)-1) {
xx = close(fdno);
return 1;
}
xx = madvise(mapped, st->st_size, MADV_SEQUENTIAL);
ctx = rpmDigestInit(PGPHASHALGO_MD5, RPMDIGEST_NONE);
xx = rpmDigestUpdate(ctx, mapped, st->st_size);
xx = rpmDigestFinal(ctx, (void **)&md5sum, &md5len, asAscii);
xx = munmap(mapped, st->st_size);
xx = close(fdno);
} break;
#endif
case URL_IS_FTP:
case URL_IS_HTTP:
case URL_IS_DASH:
default:
{ FD_t fd;
unsigned char buf[32*BUFSIZ];
fd = Fopen(fn, "r.ufdio");
if (fd == NULL || Ferror(fd)) {
if (fd != NULL)
(void) Fclose(fd);
return 1;
}
fdInitDigest(fd, PGPHASHALGO_MD5, 0);
while ((rc = Fread(buf, sizeof(buf[0]), sizeof(buf), fd)) > 0)
{};
fdFiniDigest(fd, PGPHASHALGO_MD5, (void **)&md5sum, &md5len, asAscii);
if (Ferror(fd))
rc = 1;
(void) Fclose(fd);
} break;
}
if (!rc)
memcpy(digest, md5sum, md5len);
md5sum = _free(md5sum);
return rc;
}
/*@-exportheadervar@*/
/*@unchecked@*/
int _noDirTokens = 0;
/*@=exportheadervar@*/
static int dncmp(const void * a, const void * b)
/*@*/
{
const char *const * first = a;
const char *const * second = b;
return strcmp(*first, *second);
}
void compressFilelist(Header h)
{
HGE_t hge = (HGE_t)headerGetEntryMinMemory;
HAE_t hae = (HAE_t)headerAddEntry;
HRE_t hre = (HRE_t)headerRemoveEntry;
HFD_t hfd = headerFreeData;
char ** fileNames;
const char ** dirNames;
const char ** baseNames;
int_32 * dirIndexes;
rpmTagType fnt;
int count;
int i, xx;
int dirIndex = -1;
/*
* This assumes the file list is already sorted, and begins with a
* single '/'. That assumption isn't critical, but it makes things go
* a bit faster.
*/
if (headerIsEntry(h, RPMTAG_DIRNAMES)) {
xx = hre(h, RPMTAG_OLDFILENAMES);
return; /* Already converted. */
}
if (!hge(h, RPMTAG_OLDFILENAMES, &fnt, (void **) &fileNames, &count))
return; /* no file list */
if (fileNames == NULL || count <= 0)
return;
dirNames = alloca(sizeof(*dirNames) * count); /* worst case */
baseNames = alloca(sizeof(*dirNames) * count);
dirIndexes = alloca(sizeof(*dirIndexes) * count);
if (fileNames[0][0] != '/') {
/* HACK. Source RPM, so just do things differently */
dirIndex = 0;
dirNames[dirIndex] = "";
for (i = 0; i < count; i++) {
dirIndexes[i] = dirIndex;
baseNames[i] = fileNames[i];
}
goto exit;
}
/*@-branchstate@*/
for (i = 0; i < count; i++) {
const char ** needle;
char savechar;
char * baseName;
int len;
if (fileNames[i] == NULL) /* XXX can't happen */
continue;
baseName = strrchr(fileNames[i], '/') + 1;
len = baseName - fileNames[i];
needle = dirNames;
savechar = *baseName;
*baseName = '\0';
/*@-compdef@*/
if (dirIndex < 0 ||
(needle = bsearch(&fileNames[i], dirNames, dirIndex + 1, sizeof(dirNames[0]), dncmp)) == NULL) {
char *s = alloca(len + 1);
memcpy(s, fileNames[i], len + 1);
s[len] = '\0';
dirIndexes[i] = ++dirIndex;
dirNames[dirIndex] = s;
} else
dirIndexes[i] = needle - dirNames;
/*@=compdef@*/
*baseName = savechar;
baseNames[i] = baseName;
}
/*@=branchstate@*/
exit:
if (count > 0) {
xx = hae(h, RPMTAG_DIRINDEXES, RPM_INT32_TYPE, dirIndexes, count);
xx = hae(h, RPMTAG_BASENAMES, RPM_STRING_ARRAY_TYPE,
baseNames, count);
xx = hae(h, RPMTAG_DIRNAMES, RPM_STRING_ARRAY_TYPE,
dirNames, dirIndex + 1);
}
fileNames = hfd(fileNames, fnt);
xx = hre(h, RPMTAG_OLDFILENAMES);
}
/*
* This is pretty straight-forward. The only thing that even resembles a trick
* is getting all of this into a single xmalloc'd block.
*/
static void doBuildFileList(Header h, /*@out@*/ const char *** fileListPtr,
/*@out@*/ int * fileCountPtr, rpmTag baseNameTag,
rpmTag dirNameTag, rpmTag dirIndexesTag)
/*@modifies *fileListPtr, *fileCountPtr @*/
{
HGE_t hge = (HGE_t)headerGetEntryMinMemory;
HFD_t hfd = headerFreeData;
const char ** baseNames;
const char ** dirNames;
int * dirIndexes;
int count;
const char ** fileNames;
int size;
rpmTagType bnt, dnt;
char * data;
int i, xx;
if (!hge(h, baseNameTag, &bnt, (void **) &baseNames, &count)) {
if (fileListPtr) *fileListPtr = NULL;
if (fileCountPtr) *fileCountPtr = 0;
return; /* no file list */
}
xx = hge(h, dirNameTag, &dnt, (void **) &dirNames, NULL);
xx = hge(h, dirIndexesTag, NULL, (void **) &dirIndexes, &count);
size = sizeof(*fileNames) * count;
for (i = 0; i < count; i++)
size += strlen(baseNames[i]) + strlen(dirNames[dirIndexes[i]]) + 1;
fileNames = xmalloc(size);
data = ((char *) fileNames) + (sizeof(*fileNames) * count);
/*@-branchstate@*/
for (i = 0; i < count; i++) {
fileNames[i] = data;
data = stpcpy( stpcpy(data, dirNames[dirIndexes[i]]), baseNames[i]);
*data++ = '\0';
}
/*@=branchstate@*/
baseNames = hfd(baseNames, bnt);
dirNames = hfd(dirNames, dnt);
/*@-branchstate@*/
if (fileListPtr)
*fileListPtr = fileNames;
else
fileNames = _free(fileNames);
/*@=branchstate@*/
if (fileCountPtr) *fileCountPtr = count;
}
void expandFilelist(Header h)
{
HAE_t hae = (HAE_t)headerAddEntry;
HRE_t hre = (HRE_t)headerRemoveEntry;
const char ** fileNames = NULL;
int count = 0;
int xx;
/*@-branchstate@*/
if (!headerIsEntry(h, RPMTAG_OLDFILENAMES)) {
doBuildFileList(h, &fileNames, &count, RPMTAG_BASENAMES,
RPMTAG_DIRNAMES, RPMTAG_DIRINDEXES);
if (fileNames == NULL || count <= 0)
return;
xx = hae(h, RPMTAG_OLDFILENAMES, RPM_STRING_ARRAY_TYPE,
fileNames, count);
fileNames = _free(fileNames);
}
/*@=branchstate@*/
xx = hre(h, RPMTAG_DIRNAMES);
xx = hre(h, RPMTAG_BASENAMES);
xx = hre(h, RPMTAG_DIRINDEXES);
}
void rpmBuildFileList(Header h, const char *** fileListPtr, int * fileCountPtr)
{
doBuildFileList(h, fileListPtr, fileCountPtr, RPMTAG_BASENAMES,
RPMTAG_DIRNAMES, RPMTAG_DIRINDEXES);
}
void buildOrigFileList(Header h, const char *** fileListPtr, int * fileCountPtr)
{
doBuildFileList(h, fileListPtr, fileCountPtr, RPMTAG_ORIGBASENAMES,
RPMTAG_ORIGDIRNAMES, RPMTAG_ORIGDIRINDEXES);
}
/*
* Up to rpm 3.0.4, packages implicitly provided their own name-version-release.
* Retrofit an explicit "Provides: name = epoch:version-release.
*/
void providePackageNVR(Header h)
{
HGE_t hge = (HGE_t)headerGetEntryMinMemory;
HFD_t hfd = headerFreeData;
const char *name, *version, *release;
int_32 * epoch;
const char *pEVR;
char *p;
int_32 pFlags = RPMSENSE_EQUAL;
const char ** provides = NULL;
const char ** providesEVR = NULL;
rpmTagType pnt, pvt;
int_32 * provideFlags = NULL;
int providesCount;
int i, xx;
int bingo = 1;
/* Generate provides for this package name-version-release. */
xx = headerNVR(h, &name, &version, &release);
if (!(name && version && release))
return;
pEVR = p = alloca(21 + strlen(version) + 1 + strlen(release) + 1);
*p = '\0';
if (hge(h, RPMTAG_EPOCH, NULL, (void **) &epoch, NULL)) {
sprintf(p, "%d:", *epoch);
while (*p != '\0')
p++;
}
(void) stpcpy( stpcpy( stpcpy(p, version) , "-") , release);
/*
* Rpm prior to 3.0.3 does not have versioned provides.
* If no provides at all are available, we can just add.
*/
if (!hge(h, RPMTAG_PROVIDENAME, &pnt, (void **) &provides, &providesCount))
goto exit;
/*
* Otherwise, fill in entries on legacy packages.
*/
if (!hge(h, RPMTAG_PROVIDEVERSION, &pvt, (void **) &providesEVR, NULL)) {
for (i = 0; i < providesCount; i++) {
char * vdummy = "";
int_32 fdummy = RPMSENSE_ANY;
xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDEVERSION, RPM_STRING_ARRAY_TYPE,
&vdummy, 1);
xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDEFLAGS, RPM_INT32_TYPE,
&fdummy, 1);
}
goto exit;
}
xx = hge(h, RPMTAG_PROVIDEFLAGS, NULL, (void **) &provideFlags, NULL);
/*@-nullderef@*/ /* LCL: providesEVR is not NULL */
if (provides && providesEVR && provideFlags)
for (i = 0; i < providesCount; i++) {
if (!(provides[i] && providesEVR[i]))
continue;
if (!(provideFlags[i] == RPMSENSE_EQUAL &&
!strcmp(name, provides[i]) && !strcmp(pEVR, providesEVR[i])))
continue;
bingo = 0;
break;
}
/*@=nullderef@*/
exit:
provides = hfd(provides, pnt);
providesEVR = hfd(providesEVR, pvt);
if (bingo) {
xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDENAME, RPM_STRING_ARRAY_TYPE,
&name, 1);
xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDEFLAGS, RPM_INT32_TYPE,
&pFlags, 1);
xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDEVERSION, RPM_STRING_ARRAY_TYPE,
&pEVR, 1);
}
}
void legacyRetrofit(Header h, const struct rpmlead * lead)
{
const char * prefix;
/*
* We don't use these entries (and rpm >= 2 never has) and they are
* pretty misleading. Let's just get rid of them so they don't confuse
* anyone.
*/
if (headerIsEntry(h, RPMTAG_FILEUSERNAME))
(void) headerRemoveEntry(h, RPMTAG_FILEUIDS);
if (headerIsEntry(h, RPMTAG_FILEGROUPNAME))
(void) headerRemoveEntry(h, RPMTAG_FILEGIDS);
/*
* We switched the way we do relocateable packages. We fix some of
* it up here, though the install code still has to be a bit
* careful. This fixup makes queries give the new values though,
* which is quite handy.
*/
/*@=branchstate@*/
if (headerGetEntry(h, RPMTAG_DEFAULTPREFIX, NULL, (void **) &prefix, NULL))
{
const char * nprefix = stripTrailingChar(alloca_strdup(prefix), '/');
(void) headerAddEntry(h, RPMTAG_PREFIXES, RPM_STRING_ARRAY_TYPE,
&nprefix, 1);
}
/*@=branchstate@*/
/*
* The file list was moved to a more compressed format which not
* only saves memory (nice), but gives fingerprinting a nice, fat
* speed boost (very nice). Go ahead and convert old headers to
* the new style (this is a noop for new headers).
*/
if (lead->major < 4)
compressFilelist(h);
/* XXX binary rpms always have RPMTAG_SOURCERPM, source rpms do not */
if (lead->type == RPMLEAD_SOURCE) {
int_32 one = 1;
if (!headerIsEntry(h, RPMTAG_SOURCEPACKAGE))
(void) headerAddEntry(h, RPMTAG_SOURCEPACKAGE, RPM_INT32_TYPE,
&one, 1);
} else if (lead->major < 4) {
/* Retrofit "Provide: name = EVR" for binary packages. */
providePackageNVR(h);
}
}
|