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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
|
/** \ingroup python
* \file python/upgrade.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <glob.h> /* XXX rpmio.h */
#include <dirent.h> /* XXX rpmio.h */
#include <rpmlib.h>
#include "hash.h"
#include "upgrade.h"
#define MAXPKGS 1024
#define USEDEBUG 0
#define DEBUG(x) { \
if (USEDEBUG) \
printf x; \
}
#if 0
static void printMemStats(char *mess)
{
char buf[1024];
printf("%s\n", mess);
sprintf(buf, "cat /proc/%d/status | grep VmSize", getpid());
system(buf);
}
#endif
int pkgCompare(void * first, void * second); /* XXX make gcc shut up. */
int pkgCompare(void * first, void * second) {
struct packageInfo ** a = first;
struct packageInfo ** b = second;
/* put packages w/o names at the end */
if (!(*a)->name) return 1;
if (!(*b)->name) return -1;
return xstrcasecmp((*a)->name, (*b)->name);
}
/* Adds all files in the second file list which are not in the first
file list to the hash table. */
static void compareFileList(int availFileCount, char **availBaseNames,
char ** availDirNames, int * availDirIndexes,
int instFileCount, char **instBaseNames,
char ** instDirNames, int * instDirIndexes,
struct hash_table *ht)
{
int installedX, availX, rc;
char * availDir, * availBase;
char * instDir, * instBase;
static int i = 0;
availX = 0;
installedX = 0;
while (installedX < instFileCount) {
instBase = instBaseNames[installedX];
instDir = instDirNames[instDirIndexes[installedX]];
if (availX == availFileCount) {
/* All the rest have moved */
DEBUG(("=> %d: %s%s\n", i++, instDir, instBase))
if (strncmp(instDir, "/etc/rc.d/", 10))
htAddToTable(ht, instDir, instBase);
installedX++;
} else {
availBase = availBaseNames[availX];
availDir = availDirNames[availDirIndexes[availX]];
rc = strcmp(availDir, instDir);
if (!rc)
rc = strcmp(availBase, instBase);
if (rc > 0) {
/* Avail > Installed -- file has moved */
DEBUG(("=> %d: %s%s\n", i++, instDir, instBase))
if (strncmp(instDir, "/etc/rc.d/", 10))
htAddToTable(ht, instDir, instBase);
installedX++;
} else if (rc < 0) {
/* Avail < Installed -- avail has some new files */
availX++;
} else {
/* Files are equal -- file not moved */
availX++;
installedX++;
}
}
}
}
static void addLostFiles(rpmdb db, struct pkgSet *psp, struct hash_table *ht)
{
char *name;
struct packageInfo **pack;
struct packageInfo key;
struct packageInfo *keyaddr = &key;
char **installedFiles;
char **installedDirs;
int_32 * installedDirIndexes;
int installedFileCount;
Header h = NULL;
rpmdbMatchIterator mi;
mi = rpmdbInitIterator(db, RPMDBI_PACKAGES, NULL, 0);
while ((h = rpmdbNextIterator(mi)) != NULL) {
headerGetEntry(h, RPMTAG_NAME, NULL, (void **) &name, NULL);
if (name && !strcmp(name, "metroess")) {
/* metro was removed from 5.1, but leave it if it's already
installed */
continue;
}
key.name = name;
pack = bsearch(&keyaddr, psp->packages, psp->numPackages,
sizeof(*psp->packages), (void *)pkgCompare);
if (!pack) {
if (headerGetEntryMinMemory(h, RPMTAG_BASENAMES, NULL,
(const void **) &installedFiles, &installedFileCount)) {
headerGetEntryMinMemory(h, RPMTAG_DIRINDEXES, NULL,
(const void **) &installedDirIndexes, NULL);
headerGetEntryMinMemory(h, RPMTAG_DIRNAMES, NULL,
(const void **) &installedDirs, NULL);
compareFileList(0, NULL, NULL, NULL, installedFileCount,
installedFiles, installedDirs,
installedDirIndexes, ht);
free(installedFiles);
free(installedDirs);
}
}
}
rpmdbFreeIterator(mi);
}
static int findPackagesWithObsoletes(rpmdb db, struct pkgSet *psp)
{
int count, obsoletesCount;
struct packageInfo **pip;
char **obsoletes;
count = psp->numPackages;
pip = psp->packages;
while (count--) {
if ((*pip)->selected) {
pip++;
continue;
}
if (headerGetEntryMinMemory((*pip)->h, RPMTAG_OBSOLETENAME, NULL,
(const void **) &obsoletes, &obsoletesCount)) {
while (obsoletesCount--) {
if (rpmdbCountPackages(db, obsoletes[obsoletesCount]) > 0) {
(*pip)->selected = 1;
break;
}
}
free(obsoletes);
}
pip++;
}
return 0;
}
static void errorFunction(void)
{
}
static int findUpgradePackages(rpmdb db, struct pkgSet *psp,
struct hash_table *ht)
{
int skipThis;
Header h, installedHeader;
char *name;
int count;
char **installedFiles, **availFiles;
char **installedDirs, ** availDirs;
int_32 * installedDirIndexes, * availDirIndexes;
int installedFileCount, availFileCount;
struct packageInfo **pip;
count = psp->numPackages;
pip = psp->packages;
while (count--) {
h = (*pip)->h;
name = NULL;
headerGetEntry(h, RPMTAG_NAME, NULL, (void **) &name, NULL);
if (!name) {
/* bum header */
/*logMessage("Failed with bad header");*/
return(-1);
}
DEBUG (("Avail: %s\n", name));
{ rpmdbMatchIterator mi;
mi = rpmdbInitIterator(db, RPMTAG_NAME, name, 0);
skipThis = (mi ? 0 : 1);
rpmErrorSetCallback(errorFunction);
while((installedHeader = rpmdbNextIterator(mi)) != NULL) {
if (rpmVersionCompare(installedHeader, h) >= 0) {
/* already have a newer version installed */
DEBUG (("Already have newer version\n"))
skipThis = 1;
break;
}
}
rpmdbFreeIterator(mi);
rpmErrorSetCallback(NULL);
if (! skipThis) {
DEBUG (("No newer version installed\n"))
}
}
if (skipThis) {
DEBUG (("DO NOT INSTALL\n"))
} else {
DEBUG (("UPGRADE\n"))
(*pip)->selected = 1;
if (!headerGetEntryMinMemory(h, RPMTAG_BASENAMES, NULL,
(const void **) &availFiles, &availFileCount)) {
availFiles = NULL;
availFileCount = 0;
} else {
headerGetEntryMinMemory(h, RPMTAG_DIRNAMES, NULL,
(const void **) &availDirs, NULL);
headerGetEntryMinMemory(h, RPMTAG_DIRINDEXES, NULL,
(const void **) &availDirIndexes, NULL);
}
{ rpmdbMatchIterator mi;
mi = rpmdbInitIterator(db, RPMTAG_NAME, name, 0);
while((installedHeader = rpmdbNextIterator(mi)) != NULL) {
if (headerGetEntryMinMemory(installedHeader, RPMTAG_BASENAMES,
NULL, (const void **) &installedFiles,
&installedFileCount)) {
headerGetEntryMinMemory(installedHeader, RPMTAG_DIRNAMES,
NULL, (const void **) &installedDirs, NULL);
headerGetEntryMinMemory(installedHeader, RPMTAG_DIRINDEXES,
NULL, (const void **) &installedDirIndexes, NULL);
compareFileList(availFileCount, availFiles,
availDirs, availDirIndexes,
installedFileCount, installedFiles,
installedDirs, installedDirIndexes,
ht);
free(installedFiles);
free(installedDirs);
}
}
rpmdbFreeIterator(mi);
}
if (availFiles) {
free(availFiles);
free(availDirs);
}
}
DEBUG (("\n\n"))
pip++;
}
return 0;
}
static int removeMovedFilesAlreadyHandled(struct pkgSet *psp,
struct hash_table *ht)
{
char *name;
int i, count;
Header h;
char **availFiles, ** availDirs;
int_32 * availDirIndexes;
int availFileCount;
struct packageInfo **pip;
count = psp->numPackages;
pip = psp->packages;
while (count--) {
h = (*pip)->h;
if ((*pip)->selected) {
name = NULL;
headerGetEntry(h, RPMTAG_NAME, NULL, (void **) &name, NULL);
if (headerGetEntryMinMemory(h, RPMTAG_BASENAMES, NULL,
(const void **) &availFiles, &availFileCount)) {
headerGetEntryMinMemory(h, RPMTAG_DIRNAMES, NULL,
(const void **) &availDirs, NULL);
headerGetEntryMinMemory(h, RPMTAG_DIRINDEXES, NULL,
(const void **) &availDirIndexes, NULL);
for (i = 0; i < availFileCount; i++) {
if (htInTable(ht, availDirs[availDirIndexes[i]],
availFiles[i])) {
htRemoveFromTable(ht, availDirs[availDirIndexes[i]],
availFiles[i]);
DEBUG (("File already in %s: %s%s\n", name,
availDirs[availDirIndexes[i]], availFiles[i]))
break;
}
}
free(availFiles);
free(availDirs);
}
}
pip++;
}
return 0;
}
static int findPackagesWithRelocatedFiles(struct pkgSet *psp,
struct hash_table *ht)
{
char *name;
int i, count;
Header h;
char **availFiles, **availDirs;
int_32 * availDirIndexes;
int availFileCount;
struct packageInfo **pip;
int_16 * availFileModes;
count = psp->numPackages;
pip = psp->packages;
while (count--) {
h = (*pip)->h;
if (! (*pip)->selected) {
name = NULL;
headerGetEntry(h, RPMTAG_NAME, NULL, (void **) &name, NULL);
if (headerGetEntry(h, RPMTAG_BASENAMES, NULL,
(void **) &availFiles, &availFileCount)) {
headerGetEntryMinMemory(h, RPMTAG_DIRNAMES, NULL,
(const void **) &availDirs, NULL);
headerGetEntryMinMemory(h, RPMTAG_DIRINDEXES, NULL,
(const void **) &availDirIndexes, NULL);
headerGetEntryMinMemory(h, RPMTAG_FILEMODES, NULL,
(const void **) &availFileModes, NULL);
for (i = 0; i < availFileCount; i++) {
if (S_ISDIR(availFileModes[i])) continue;
if (htInTable(ht, availDirs[availDirIndexes[i]],
availFiles[i])) {
htRemoveFromTable(ht, availDirs[availDirIndexes[i]],
availFiles[i]);
DEBUG (("Found file in %s: %s%s\n", name,
availDirs[availDirIndexes[i]], availFiles[i]))
(*pip)->selected = 1;
}
}
free(availFiles);
free(availDirs);
}
}
pip++;
}
return 0;
}
/*
static void printCount(struct pkgSet *psp)
{
int i, upgradeCount;
struct packageInfo *pip;
upgradeCount = 0;
pip = psp->packages;
i = psp->numPackages;
while (i--) {
if (pip->selected) {
upgradeCount++;
}
pip++;
}
logMessage("marked %d packages for upgrade", upgradeCount);
}
*/
static int unmarkPackagesAlreadyInstalled(rpmdb db, struct pkgSet *psp)
{
Header h, installedHeader;
char *name;
struct packageInfo **pip;
int count;
count = psp->numPackages;
pip = psp->packages;
while (count--) {
if ((*pip)->selected) {
h = (*pip)->h;
/* If this package is already installed, don't bother */
name = NULL;
headerGetEntry(h, RPMTAG_NAME, NULL, (void **) &name, NULL);
if (!name) {
/* bum header */
/*logMessage("Failed with bad header");*/
return(-1);
}
{ rpmdbMatchIterator mi;
mi = rpmdbInitIterator(db, RPMTAG_NAME, name, 0);
rpmErrorSetCallback(errorFunction);
while((installedHeader = rpmdbNextIterator(mi)) != NULL) {
if (rpmVersionCompare(installedHeader, h) >= 0) {
/* already have a newer version installed */
DEBUG (("Already have newer version\n"))
(*pip)->selected = 0;
break;
}
}
rpmdbFreeIterator(mi);
rpmErrorSetCallback(NULL);
}
}
pip++;
}
return 0;
}
static void emptyErrorCallback(void) {
}
int ugFindUpgradePackages(struct pkgSet *psp, char *installRoot)
{
rpmdb db;
struct hash_table *hashTable;
rpmErrorCallBackType old;
/*logDebugMessage(("ugFindUpgradePackages() ..."));*/
/* rpmReadConfigFiles(NULL, NULL); */
rpmSetVerbosity(RPMMESS_FATALERROR);
old = rpmErrorSetCallback(emptyErrorCallback);
if (rpmdbOpen(installRoot, &db, O_RDONLY, 0644)) {
/*logMessage("failed opening %s/var/lib/rpm/packages.rpm",
installRoot);*/
return(-1);
}
rpmErrorSetCallback(old);
rpmSetVerbosity(RPMMESS_NORMAL);
hashTable = htNewTable(1103);
/* For all packages that are installed, if there is no package */
/* available by that name, add the package's files to the hash table */
addLostFiles(db, psp, hashTable);
/*logDebugMessage(("added lost files"));
printCount(psp);*/
/* Find packges that are new, and mark them in installThisPackage, */
/* updating availPkgs with the count. Also add files to the hash */
/* table that do not exist in the new package - they may have moved */
if (findUpgradePackages(db, psp, hashTable)) {
rpmdbClose(db);
return(-1);
}
/*logDebugMessage(("found basic packages to upgrade"));
printCount(psp);
hash_stats(hashTable);*/
/* Remove any files that were added to the hash table that are in */
/* some other package marked for upgrade. */
removeMovedFilesAlreadyHandled(psp, hashTable);
/*logDebugMessage(("removed extra files which have moved"));
printCount(psp);*/
findPackagesWithRelocatedFiles(psp, hashTable);
/*logDebugMessage(("found packages with relocated files"));
printCount(psp);*/
findPackagesWithObsoletes(db, psp);
/*logDebugMessage(("found packages that obsolete installed packages"));
printCount(psp);*/
unmarkPackagesAlreadyInstalled(db, psp);
/*logDebugMessage(("unmarked packages already installed"));
printCount(psp);*/
htFreeHashTable(hashTable);
/*printMemStats("Done");*/
rpmdbClose(db);
return 0;
}
|