7 #include <rpm/header.h>
8 #include <rpm/rpmmacro.h>
9 #include <rpm/rpmstring.h>
10 #include <rpm/rpmfi.h>
11 #include <rpm/rpmds.h>
13 #include "lib/legacy.h"
19 static int dncmp(const void * a, const void * b)
21 const char *const * first = a;
22 const char *const * second = b;
23 return strcmp(*first, *second);
26 void compressFilelist(Header h)
28 HAE_t hae = (HAE_t)headerAddEntry;
29 HRE_t hre = (HRE_t)headerRemoveEntry;
30 struct rpmtd_s fileNames;
32 const char ** baseNames;
33 uint32_t * dirIndexes;
39 * This assumes the file list is already sorted, and begins with a
40 * single '/'. That assumption isn't critical, but it makes things go
44 if (headerIsEntry(h, RPMTAG_DIRNAMES)) {
45 xx = hre(h, RPMTAG_OLDFILENAMES);
46 return; /* Already converted. */
49 if (!headerGet(h, RPMTAG_OLDFILENAMES, &fileNames, HEADERGET_MINMEM))
51 count = rpmtdCount(&fileNames);
55 dirNames = xmalloc(sizeof(*dirNames) * count); /* worst case */
56 baseNames = xmalloc(sizeof(*dirNames) * count);
57 dirIndexes = xmalloc(sizeof(*dirIndexes) * count);
59 if (headerIsSource(h)) {
60 /* HACK. Source RPM, so just do things differently */
62 dirNames[dirIndex] = xstrdup("");
63 while ((i = rpmtdNext(&fileNames)) >= 0) {
64 dirIndexes[i] = dirIndex;
65 baseNames[i] = rpmtdGetString(&fileNames);
70 while ((i = rpmtdNext(&fileNames)) >= 0) {
75 const char *filename = rpmtdGetString(&fileNames);
77 if (filename == NULL) /* XXX can't happen */
79 baseName = strrchr(filename, '/') + 1;
80 len = baseName - filename;
85 (needle = bsearch(filename, dirNames, dirIndex + 1, sizeof(dirNames[0]), dncmp)) == NULL) {
86 char *s = xmalloc(len + 1);
87 rstrlcpy(s, filename, len + 1);
88 dirIndexes[i] = ++dirIndex;
89 dirNames[dirIndex] = s;
91 dirIndexes[i] = needle - dirNames;
94 baseNames[i] = baseName;
99 xx = hae(h, RPMTAG_DIRINDEXES, RPM_INT32_TYPE, dirIndexes, count);
100 xx = hae(h, RPMTAG_BASENAMES, RPM_STRING_ARRAY_TYPE,
102 xx = hae(h, RPMTAG_DIRNAMES, RPM_STRING_ARRAY_TYPE,
103 dirNames, (rpm_count_t) dirIndex + 1);
106 rpmtdFreeData(&fileNames);
107 for (i = 0; i <= dirIndex; i++) {
114 xx = hre(h, RPMTAG_OLDFILENAMES);
117 void expandFilelist(Header h)
119 HRE_t hre = (HRE_t)headerRemoveEntry;
120 struct rpmtd_s filenames;
122 if (!headerIsEntry(h, RPMTAG_OLDFILENAMES)) {
123 (void) headerGet(h, RPMTAG_FILENAMES, &filenames, HEADERGET_EXT);
124 if (rpmtdCount(&filenames) < 1)
126 rpmtdSetTag(&filenames, RPMTAG_OLDFILENAMES);
127 headerPut(h, &filenames, HEADERPUT_DEFAULT);
128 rpmtdFreeData(&filenames);
131 (void) hre(h, RPMTAG_DIRNAMES);
132 (void) hre(h, RPMTAG_BASENAMES);
133 (void) hre(h, RPMTAG_DIRINDEXES);
137 * Up to rpm 3.0.4, packages implicitly provided their own name-version-release.
138 * Retrofit an explicit "Provides: name = epoch:version-release.
140 static void providePackageNVR(Header h)
144 rpmsenseFlags pFlags = RPMSENSE_EQUAL;
146 struct rpmtd_s pnames;
149 /* Generate provides for this package name-version-release. */
150 pEVR = headerGetEVR(h, &name);
155 * Rpm prior to 3.0.3 does not have versioned provides.
156 * If no provides at all are available, we can just add.
158 if (!headerGet(h, RPMTAG_PROVIDENAME, &pnames, HEADERGET_MINMEM)) {
163 * Otherwise, fill in entries on legacy packages.
165 if (!headerIsEntry(h, RPMTAG_PROVIDEVERSION)) {
166 while (rpmtdNext(&pnames) >= 0) {
167 const char * vdummy = "";
168 rpmsenseFlags fdummy = RPMSENSE_ANY;
169 headerAddOrAppendEntry(h, RPMTAG_PROVIDEVERSION, RPM_STRING_ARRAY_TYPE,
171 headerAddOrAppendEntry(h, RPMTAG_PROVIDEFLAGS, RPM_INT32_TYPE,
177 /* see if we already have this provide */
178 hds = rpmdsNew(h, RPMTAG_PROVIDENAME, 1);
179 nvrds = rpmdsSingle(RPMTAG_PROVIDENAME, name, pEVR, pFlags);
180 if (rpmdsFind(hds, nvrds) >= 0) {
189 headerAddOrAppendEntry(h, RPMTAG_PROVIDENAME, RPM_STRING_ARRAY_TYPE,
191 headerAddOrAppendEntry(h, RPMTAG_PROVIDEFLAGS, RPM_INT32_TYPE,
193 headerAddOrAppendEntry(h, RPMTAG_PROVIDEVERSION, RPM_STRING_ARRAY_TYPE,
196 rpmtdFreeData(&pnames);
200 void legacyRetrofit(Header h)
202 struct rpmtd_s dprefix;
205 * We don't use these entries (and rpm >= 2 never has) and they are
206 * pretty misleading. Let's just get rid of them so they don't confuse
209 if (headerIsEntry(h, RPMTAG_FILEUSERNAME))
210 (void) headerRemoveEntry(h, RPMTAG_FILEUIDS);
211 if (headerIsEntry(h, RPMTAG_FILEGROUPNAME))
212 (void) headerRemoveEntry(h, RPMTAG_FILEGIDS);
215 * We switched the way we do relocatable packages. We fix some of
216 * it up here, though the install code still has to be a bit
217 * careful. This fixup makes queries give the new values though,
218 * which is quite handy.
220 if (headerGet(h, RPMTAG_DEFAULTPREFIX, &dprefix, HEADERGET_MINMEM)) {
221 const char *prefix = rpmtdGetString(&dprefix);
222 char * nprefix = stripTrailingChar(xstrdup(prefix), '/');
223 (void) headerAddEntry(h, RPMTAG_PREFIXES, RPM_STRING_ARRAY_TYPE,
226 rpmtdFreeData(&dprefix);
230 * The file list was moved to a more compressed format which not
231 * only saves memory (nice), but gives fingerprinting a nice, fat
232 * speed boost (very nice). Go ahead and convert old headers to
233 * the new style (this is a noop for new headers).
237 /* XXX binary rpms always have RPMTAG_SOURCERPM, source rpms do not */
238 if (headerIsSource(h)) {
240 if (!headerIsEntry(h, RPMTAG_SOURCEPACKAGE))
241 (void) headerAddEntry(h, RPMTAG_SOURCEPACKAGE, RPM_INT32_TYPE,
244 /* Retrofit "Provide: name = EVR" for binary packages. */
245 providePackageNVR(h);