7 #include <rpm/rpmlib.h>
8 #include <rpm/rpmmacro.h>
9 #include <rpm/rpmstring.h>
10 #include <rpm/rpmfi.h>
11 #include <rpm/rpmds.h>
12 #include "lib/legacy.h"
15 #define alloca_strdup(_s) strcpy(alloca(strlen(_s)+1), (_s))
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 HGE_t hge = (HGE_t)headerGetEntryMinMemory;
29 HAE_t hae = (HAE_t)headerAddEntry;
30 HRE_t hre = (HRE_t)headerRemoveEntry;
31 HFD_t hfd = headerFreeData;
33 const char ** dirNames;
34 const char ** baseNames;
35 uint32_t * dirIndexes;
42 * This assumes the file list is already sorted, and begins with a
43 * single '/'. That assumption isn't critical, but it makes things go
47 if (headerIsEntry(h, RPMTAG_DIRNAMES)) {
48 xx = hre(h, RPMTAG_OLDFILENAMES);
49 return; /* Already converted. */
52 if (!hge(h, RPMTAG_OLDFILENAMES, &fnt, (void **) &fileNames, &count))
53 return; /* no file list */
54 if (fileNames == NULL || count <= 0)
57 dirNames = alloca(sizeof(*dirNames) * count); /* worst case */
58 baseNames = alloca(sizeof(*dirNames) * count);
59 dirIndexes = alloca(sizeof(*dirIndexes) * count);
61 if (fileNames[0][0] != '/') {
62 /* HACK. Source RPM, so just do things differently */
64 dirNames[dirIndex] = "";
65 for (i = 0; i < count; i++) {
66 dirIndexes[i] = dirIndex;
67 baseNames[i] = fileNames[i];
72 for (i = 0; i < count; i++) {
78 if (fileNames[i] == NULL) /* XXX can't happen */
80 baseName = strrchr(fileNames[i], '/') + 1;
81 len = baseName - fileNames[i];
86 (needle = bsearch(&fileNames[i], dirNames, dirIndex + 1, sizeof(dirNames[0]), dncmp)) == NULL) {
87 char *s = alloca(len + 1);
88 memcpy(s, fileNames[i], len + 1);
90 dirIndexes[i] = ++dirIndex;
91 dirNames[dirIndex] = s;
93 dirIndexes[i] = needle - dirNames;
96 baseNames[i] = baseName;
101 xx = hae(h, RPMTAG_DIRINDEXES, RPM_INT32_TYPE, dirIndexes, count);
102 xx = hae(h, RPMTAG_BASENAMES, RPM_STRING_ARRAY_TYPE,
104 xx = hae(h, RPMTAG_DIRNAMES, RPM_STRING_ARRAY_TYPE,
105 dirNames, dirIndex + 1);
108 fileNames = hfd(fileNames, fnt);
110 xx = hre(h, RPMTAG_OLDFILENAMES);
113 void expandFilelist(Header h)
115 HAE_t hae = (HAE_t)headerAddEntry;
116 HRE_t hre = (HRE_t)headerRemoveEntry;
117 const char ** fileNames = NULL;
121 if (!headerIsEntry(h, RPMTAG_OLDFILENAMES)) {
122 rpmfiBuildFNames(h, RPMTAG_BASENAMES, &fileNames, &count);
123 if (fileNames == NULL || count <= 0)
125 xx = hae(h, RPMTAG_OLDFILENAMES, RPM_STRING_ARRAY_TYPE,
127 fileNames = _free(fileNames);
130 xx = hre(h, RPMTAG_DIRNAMES);
131 xx = hre(h, RPMTAG_BASENAMES);
132 xx = hre(h, RPMTAG_DIRINDEXES);
136 * Up to rpm 3.0.4, packages implicitly provided their own name-version-release.
137 * Retrofit an explicit "Provides: name = epoch:version-release.
139 void providePackageNVR(Header h)
141 HGE_t hge = (HGE_t)headerGetEntryMinMemory;
142 HFD_t hfd = headerFreeData;
143 const char *name, *version, *release;
147 int32_t pFlags = RPMSENSE_EQUAL;
148 const char ** provides = NULL;
149 const char ** providesEVR = NULL;
151 int32_t * provideFlags = NULL;
156 /* Generate provides for this package name-version-release. */
157 xx = headerNVR(h, &name, &version, &release);
158 if (!(name && version && release))
160 pEVR = p = alloca(21 + strlen(version) + 1 + strlen(release) + 1);
162 if (hge(h, RPMTAG_EPOCH, NULL, (void **) &epoch, NULL)) {
163 sprintf(p, "%d:", *epoch);
167 (void) stpcpy( stpcpy( stpcpy(p, version) , "-") , release);
170 * Rpm prior to 3.0.3 does not have versioned provides.
171 * If no provides at all are available, we can just add.
173 if (!hge(h, RPMTAG_PROVIDENAME, &pnt, (void **) &provides, &providesCount))
177 * Otherwise, fill in entries on legacy packages.
179 if (!hge(h, RPMTAG_PROVIDEVERSION, &pvt, (void **) &providesEVR, NULL)) {
180 for (i = 0; i < providesCount; i++) {
182 int32_t fdummy = RPMSENSE_ANY;
183 xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDEVERSION, RPM_STRING_ARRAY_TYPE,
185 xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDEFLAGS, RPM_INT32_TYPE,
191 xx = hge(h, RPMTAG_PROVIDEFLAGS, NULL, (void **) &provideFlags, NULL);
193 /* LCL: providesEVR is not NULL */
194 if (provides && providesEVR && provideFlags)
195 for (i = 0; i < providesCount; i++) {
196 if (!(provides[i] && providesEVR[i]))
198 if (!(provideFlags[i] == RPMSENSE_EQUAL &&
199 !strcmp(name, provides[i]) && !strcmp(pEVR, providesEVR[i])))
206 provides = hfd(provides, pnt);
207 providesEVR = hfd(providesEVR, pvt);
210 xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDENAME, RPM_STRING_ARRAY_TYPE,
212 xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDEFLAGS, RPM_INT32_TYPE,
214 xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDEVERSION, RPM_STRING_ARRAY_TYPE,
219 void legacyRetrofit(Header h)
224 * We don't use these entries (and rpm >= 2 never has) and they are
225 * pretty misleading. Let's just get rid of them so they don't confuse
228 if (headerIsEntry(h, RPMTAG_FILEUSERNAME))
229 (void) headerRemoveEntry(h, RPMTAG_FILEUIDS);
230 if (headerIsEntry(h, RPMTAG_FILEGROUPNAME))
231 (void) headerRemoveEntry(h, RPMTAG_FILEGIDS);
234 * We switched the way we do relocatable packages. We fix some of
235 * it up here, though the install code still has to be a bit
236 * careful. This fixup makes queries give the new values though,
237 * which is quite handy.
239 if (headerGetEntry(h, RPMTAG_DEFAULTPREFIX, NULL, (void **) &prefix, NULL))
241 const char * nprefix = stripTrailingChar(alloca_strdup(prefix), '/');
242 (void) headerAddEntry(h, RPMTAG_PREFIXES, RPM_STRING_ARRAY_TYPE,
247 * The file list was moved to a more compressed format which not
248 * only saves memory (nice), but gives fingerprinting a nice, fat
249 * speed boost (very nice). Go ahead and convert old headers to
250 * the new style (this is a noop for new headers).
254 /* XXX binary rpms always have RPMTAG_SOURCERPM, source rpms do not */
255 if (headerIsSource(h)) {
257 if (!headerIsEntry(h, RPMTAG_SOURCEPACKAGE))
258 (void) headerAddEntry(h, RPMTAG_SOURCEPACKAGE, RPM_INT32_TYPE,
261 /* Retrofit "Provide: name = EVR" for binary packages. */
262 providePackageNVR(h);