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
|
#if defined(HAVE_CONFIG_H)
#include "system.h"
const char *__progname;
#else
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
#endif
#include <rpmlib.h>
#include <rpmdb.h>
#include <rpmio.h>
#include "rpmmacro.h"
#include <rpmdb/legacy.h>
#define FA_MAGIC 0x02050920
struct faFileHeader{
unsigned int magic;
unsigned int firstFree;
};
struct faHeader {
unsigned int size;
unsigned int freeNext; /* offset of the next free block, 0 if none */
unsigned int freePrev;
unsigned int isFree;
/* note that the u16's appear last for alignment/space reasons */
};
static int fadFileSize;
static ssize_t Pread(FD_t fd, void * buf, size_t count, off_t offset) {
if (Fseek(fd, offset, SEEK_SET) < 0)
return -1;
return Fread(buf, sizeof(char), count, fd);
}
static FD_t fadOpen(const char * path)
{
struct faFileHeader newHdr;
FD_t fd;
struct stat stb;
fd = Fopen(path, "r.fdio");
if (!fd || Ferror(fd))
return NULL;
if (fstat(Fileno(fd), &stb)) {
Fclose(fd);
return NULL;
}
fadFileSize = stb.st_size;
/* is this file brand new? */
if (fadFileSize == 0) {
Fclose(fd);
return NULL;
}
if (Pread(fd, &newHdr, sizeof(newHdr), 0) != sizeof(newHdr)) {
Fclose(fd);
return NULL;
}
if (newHdr.magic != FA_MAGIC) {
Fclose(fd);
return NULL;
}
return fd ;
}
static int fadNextOffset(FD_t fd, unsigned int lastOffset)
{
struct faHeader header;
int offset;
offset = (lastOffset)
? (lastOffset - sizeof(header))
: sizeof(struct faFileHeader);
if (offset >= fadFileSize)
return 0;
if (Pread(fd, &header, sizeof(header), offset) != sizeof(header))
return 0;
if (!lastOffset && !header.isFree)
return (offset + sizeof(header));
do {
offset += header.size;
if (Pread(fd, &header, sizeof(header), offset) != sizeof(header))
return 0;
if (!header.isFree) break;
} while (offset < fadFileSize && header.isFree);
if (offset < fadFileSize) {
/* Sanity check this to make sure we're not going in loops */
offset += sizeof(header);
if (offset <= lastOffset) return -1;
return offset;
} else
return 0;
}
static int fadFirstOffset(FD_t fd)
{
return fadNextOffset(fd, 0);
}
static rpmdb db;
int
main(int argc, char *argv[])
{
FD_t fd;
int offset;
Header h;
const char *name;
const char *version;
const char *release;
if (argc != 2)
{
fprintf(stderr, "usage: %s <packages.rpm>\n", argv[0]);
exit(1);
}
if ((fd = fadOpen(argv[1])) == 0)
{
fprintf(stderr, "could not open %s\n", argv[1]);
exit(1);
}
rpmInitMacros(NULL, "/usr/lib/rpm/macros");
/* speed things up */
(void) rpmDefineMacro(NULL, "_rpmdb_rebuild %{nil}", -1);
if (rpmdbOpen("/", &db, O_RDWR, 0644)) {
fprintf(stderr, "could not open rpm database\n");
exit(1);
}
for (offset = fadFirstOffset(fd); offset; offset = fadNextOffset(fd, offset))
{
rpmdbMatchIterator mi;
/* have to use lseek instead of Fseek because headerRead
* uses low level IO
*/
if (lseek(Fileno(fd), (off_t)offset, SEEK_SET) == -1)
{
perror("lseek");
continue;
}
h = headerRead(fd, HEADER_MAGIC_NO);
if (!h)
continue;
compressFilelist(h);
providePackageNVR(h);
headerNVR(h, &name, &version, &release);
mi = rpmdbInitIterator(db, RPMTAG_NAME, name, 0);
rpmdbSetIteratorRE(mi, RPMTAG_VERSION, RPMMIRE_DEFAULT, version);
rpmdbSetIteratorRE(mi, RPMTAG_RELEASE, RPMMIRE_DEFAULT, release);
if (rpmdbNextIterator(mi))
{
printf("%s-%s-%s is already in database\n", name, version, release);
rpmdbFreeIterator(mi);
headerFree(h);
continue;
}
rpmdbFreeIterator(mi);
if (rpmdbAdd(db, -1, h, 0, 0))
{
fprintf(stderr, "could not add %s-%s-%s!\n", name, version, release);
}
headerFree(h);
}
Fclose(fd);
rpmdbClose(db);
return 0;
}
|