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"
17 #define alloca_strdup(_s) strcpy(alloca(strlen(_s)+1), (_s))
21 static int dncmp(const void * a, const void * b)
23 const char *const * first = a;
24 const char *const * second = b;
25 return strcmp(*first, *second);
28 void compressFilelist(Header h)
30 HGE_t hge = (HGE_t)headerGetEntryMinMemory;
31 HAE_t hae = (HAE_t)headerAddEntry;
32 HRE_t hre = (HRE_t)headerRemoveEntry;
33 HFD_t hfd = headerFreeData;
36 const char ** baseNames;
37 uint32_t * dirIndexes;
44 * This assumes the file list is already sorted, and begins with a
45 * single '/'. That assumption isn't critical, but it makes things go
49 if (headerIsEntry(h, RPMTAG_DIRNAMES)) {
50 xx = hre(h, RPMTAG_OLDFILENAMES);
51 return; /* Already converted. */
54 if (!hge(h, RPMTAG_OLDFILENAMES, &fnt, (rpm_data_t *) &fileNames, &count))
55 return; /* no file list */
56 if (fileNames == NULL || count <= 0)
59 dirNames = xmalloc(sizeof(*dirNames) * count); /* worst case */
60 baseNames = xmalloc(sizeof(*dirNames) * count);
61 dirIndexes = xmalloc(sizeof(*dirIndexes) * count);
63 if (fileNames[0][0] != '/') {
64 /* HACK. Source RPM, so just do things differently */
66 dirNames[dirIndex] = xstrdup("");
67 for (i = 0; i < count; i++) {
68 dirIndexes[i] = dirIndex;
69 baseNames[i] = fileNames[i];
74 for (i = 0; i < count; i++) {
80 if (fileNames[i] == NULL) /* XXX can't happen */
82 baseName = strrchr(fileNames[i], '/') + 1;
83 len = baseName - fileNames[i];
88 (needle = bsearch(&fileNames[i], dirNames, dirIndex + 1, sizeof(dirNames[0]), dncmp)) == NULL) {
89 char *s = xmalloc(len + 1);
90 rstrlcpy(s, fileNames[i], len + 1);
91 dirIndexes[i] = ++dirIndex;
92 dirNames[dirIndex] = s;
94 dirIndexes[i] = needle - dirNames;
97 baseNames[i] = baseName;
102 xx = hae(h, RPMTAG_DIRINDEXES, RPM_INT32_TYPE, dirIndexes, count);
103 xx = hae(h, RPMTAG_BASENAMES, RPM_STRING_ARRAY_TYPE,
105 xx = hae(h, RPMTAG_DIRNAMES, RPM_STRING_ARRAY_TYPE,
106 dirNames, (rpm_count_t) dirIndex + 1);
109 fileNames = hfd(fileNames, fnt);
110 for (i = 0; i <= dirIndex; i++) {
117 xx = hre(h, RPMTAG_OLDFILENAMES);
120 void expandFilelist(Header h)
122 HAE_t hae = (HAE_t)headerAddEntry;
123 HRE_t hre = (HRE_t)headerRemoveEntry;
124 const char ** fileNames = NULL;
125 rpm_count_t count = 0;
128 if (!headerIsEntry(h, RPMTAG_OLDFILENAMES)) {
129 rpmfiBuildFNames(h, RPMTAG_BASENAMES, &fileNames, &count);
130 if (fileNames == NULL || count <= 0)
132 xx = hae(h, RPMTAG_OLDFILENAMES, RPM_STRING_ARRAY_TYPE,
134 fileNames = _free(fileNames);
137 xx = hre(h, RPMTAG_DIRNAMES);
138 xx = hre(h, RPMTAG_BASENAMES);
139 xx = hre(h, RPMTAG_DIRINDEXES);
143 * Up to rpm 3.0.4, packages implicitly provided their own name-version-release.
144 * Retrofit an explicit "Provides: name = epoch:version-release.
146 static void providePackageNVR(Header h)
148 HGE_t hge = (HGE_t)headerGetEntryMinMemory;
149 HFD_t hfd = headerFreeData;
152 rpmsenseFlags pFlags = RPMSENSE_EQUAL;
153 const char ** provides = NULL;
154 const char ** providesEVR = NULL;
156 rpmsenseFlags * provideFlags = NULL;
157 rpm_count_t providesCount, i;
161 /* Generate provides for this package name-version-release. */
162 pEVR = headerGetEVR(h, &name);
167 * Rpm prior to 3.0.3 does not have versioned provides.
168 * If no provides at all are available, we can just add.
170 if (!hge(h, RPMTAG_PROVIDENAME, &pnt, (rpm_data_t *) &provides, &providesCount))
174 * Otherwise, fill in entries on legacy packages.
176 if (!hge(h, RPMTAG_PROVIDEVERSION, &pvt, (rpm_data_t *) &providesEVR, NULL)) {
177 for (i = 0; i < providesCount; i++) {
178 const char * vdummy = "";
179 rpmsenseFlags fdummy = RPMSENSE_ANY;
180 xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDEVERSION, RPM_STRING_ARRAY_TYPE,
182 xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDEFLAGS, RPM_INT32_TYPE,
188 xx = hge(h, RPMTAG_PROVIDEFLAGS, NULL, (rpm_data_t *) &provideFlags, NULL);
190 /* LCL: providesEVR is not NULL */
191 if (provides && providesEVR && provideFlags)
192 for (i = 0; i < providesCount; i++) {
193 if (!(provides[i] && providesEVR[i]))
195 if (!(provideFlags[i] == RPMSENSE_EQUAL &&
196 !strcmp(name, provides[i]) && !strcmp(pEVR, providesEVR[i])))
203 provides = hfd(provides, pnt);
204 providesEVR = hfd(providesEVR, pvt);
207 xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDENAME, RPM_STRING_ARRAY_TYPE,
209 xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDEFLAGS, RPM_INT32_TYPE,
211 xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDEVERSION, RPM_STRING_ARRAY_TYPE,
217 void legacyRetrofit(Header h)
222 * We don't use these entries (and rpm >= 2 never has) and they are
223 * pretty misleading. Let's just get rid of them so they don't confuse
226 if (headerIsEntry(h, RPMTAG_FILEUSERNAME))
227 (void) headerRemoveEntry(h, RPMTAG_FILEUIDS);
228 if (headerIsEntry(h, RPMTAG_FILEGROUPNAME))
229 (void) headerRemoveEntry(h, RPMTAG_FILEGIDS);
232 * We switched the way we do relocatable packages. We fix some of
233 * it up here, though the install code still has to be a bit
234 * careful. This fixup makes queries give the new values though,
235 * which is quite handy.
237 if (headerGetEntry(h, RPMTAG_DEFAULTPREFIX, NULL, (rpm_data_t *) &prefix, NULL))
239 char * nprefix = stripTrailingChar(xstrdup(prefix), '/');
240 (void) headerAddEntry(h, RPMTAG_PREFIXES, RPM_STRING_ARRAY_TYPE,
246 * The file list was moved to a more compressed format which not
247 * only saves memory (nice), but gives fingerprinting a nice, fat
248 * speed boost (very nice). Go ahead and convert old headers to
249 * the new style (this is a noop for new headers).
253 /* XXX binary rpms always have RPMTAG_SOURCERPM, source rpms do not */
254 if (headerIsSource(h)) {
256 if (!headerIsEntry(h, RPMTAG_SOURCEPACKAGE))
257 (void) headerAddEntry(h, RPMTAG_SOURCEPACKAGE, RPM_INT32_TYPE,
260 /* Retrofit "Provide: name = EVR" for binary packages. */
261 providePackageNVR(h);