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 struct rpmtd_s fileNames;
30 const char ** baseNames;
31 uint32_t * dirIndexes;
37 * This assumes the file list is already sorted, and begins with a
38 * single '/'. That assumption isn't critical, but it makes things go
42 if (headerIsEntry(h, RPMTAG_DIRNAMES)) {
43 xx = headerDel(h, RPMTAG_OLDFILENAMES);
44 return; /* Already converted. */
47 if (!headerGet(h, RPMTAG_OLDFILENAMES, &fileNames, HEADERGET_MINMEM))
49 count = rpmtdCount(&fileNames);
53 dirNames = xmalloc(sizeof(*dirNames) * count); /* worst case */
54 baseNames = xmalloc(sizeof(*dirNames) * count);
55 dirIndexes = xmalloc(sizeof(*dirIndexes) * count);
57 /* HACK. Source RPM, so just do things differently */
58 { const char *fn = rpmtdGetString(&fileNames);
59 if (fn && *fn != '/') {
61 dirNames[dirIndex] = xstrdup("");
62 while ((i = rpmtdNext(&fileNames)) >= 0) {
63 dirIndexes[i] = dirIndex;
64 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 headerPutUint32(h, RPMTAG_DIRINDEXES, dirIndexes, count);
100 headerPutStringArray(h, RPMTAG_BASENAMES, baseNames, count);
101 headerPutStringArray(h, RPMTAG_DIRNAMES,
102 (const char **) dirNames, dirIndex + 1);
105 rpmtdFreeData(&fileNames);
106 for (i = 0; i <= dirIndex; i++) {
113 xx = headerDel(h, RPMTAG_OLDFILENAMES);
116 void expandFilelist(Header h)
118 struct rpmtd_s filenames;
120 if (!headerIsEntry(h, RPMTAG_OLDFILENAMES)) {
121 (void) headerGet(h, RPMTAG_FILENAMES, &filenames, HEADERGET_EXT);
122 if (rpmtdCount(&filenames) < 1)
124 rpmtdSetTag(&filenames, RPMTAG_OLDFILENAMES);
125 headerPut(h, &filenames, HEADERPUT_DEFAULT);
126 rpmtdFreeData(&filenames);
129 (void) headerDel(h, RPMTAG_DIRNAMES);
130 (void) headerDel(h, RPMTAG_BASENAMES);
131 (void) headerDel(h, RPMTAG_DIRINDEXES);
135 * Up to rpm 3.0.4, packages implicitly provided their own name-version-release.
136 * Retrofit an explicit "Provides: name = epoch:version-release.
138 static void providePackageNVR(Header h)
142 rpmsenseFlags pFlags = RPMSENSE_EQUAL;
144 struct rpmtd_s pnames;
147 /* Generate provides for this package name-version-release. */
148 pEVR = headerGetEVR(h, &name);
153 * Rpm prior to 3.0.3 does not have versioned provides.
154 * If no provides at all are available, we can just add.
156 if (!headerGet(h, RPMTAG_PROVIDENAME, &pnames, HEADERGET_MINMEM)) {
161 * Otherwise, fill in entries on legacy packages.
163 if (!headerIsEntry(h, RPMTAG_PROVIDEVERSION)) {
164 while (rpmtdNext(&pnames) >= 0) {
165 rpmsenseFlags fdummy = RPMSENSE_ANY;
167 headerPutString(h, RPMTAG_PROVIDEVERSION, "");
168 headerPutUint32(h, RPMTAG_PROVIDEFLAGS, &fdummy, 1);
173 /* see if we already have this provide */
174 hds = rpmdsNew(h, RPMTAG_PROVIDENAME, 1);
175 nvrds = rpmdsSingle(RPMTAG_PROVIDENAME, name, pEVR, pFlags);
176 if (rpmdsFind(hds, nvrds) >= 0) {
185 const char *evr = pEVR;
186 headerPutString(h, RPMTAG_PROVIDENAME, name);
187 headerPutString(h, RPMTAG_PROVIDEVERSION, evr);
188 headerPutUint32(h, RPMTAG_PROVIDEFLAGS, &pFlags, 1);
190 rpmtdFreeData(&pnames);
194 void legacyRetrofit(Header h)
196 struct rpmtd_s dprefix;
199 * We don't use these entries (and rpm >= 2 never has) and they are
200 * pretty misleading. Let's just get rid of them so they don't confuse
203 if (headerIsEntry(h, RPMTAG_FILEUSERNAME))
204 (void) headerDel(h, RPMTAG_FILEUIDS);
205 if (headerIsEntry(h, RPMTAG_FILEGROUPNAME))
206 (void) headerDel(h, RPMTAG_FILEGIDS);
209 * We switched the way we do relocatable packages. We fix some of
210 * it up here, though the install code still has to be a bit
211 * careful. This fixup makes queries give the new values though,
212 * which is quite handy.
214 if (headerGet(h, RPMTAG_DEFAULTPREFIX, &dprefix, HEADERGET_MINMEM)) {
215 const char *prefix = rpmtdGetString(&dprefix);
216 char * nprefix = stripTrailingChar(xstrdup(prefix), '/');
218 headerPutString(h, RPMTAG_PREFIXES, nprefix);
220 rpmtdFreeData(&dprefix);
224 * The file list was moved to a more compressed format which not
225 * only saves memory (nice), but gives fingerprinting a nice, fat
226 * speed boost (very nice). Go ahead and convert old headers to
227 * the new style (this is a noop for new headers).
231 /* XXX binary rpms always have RPMTAG_SOURCERPM, source rpms do not */
232 if (headerIsSource(h)) {
235 if (!headerIsEntry(h, RPMTAG_SOURCEPACKAGE))
236 headerPutUint32(h, RPMTAG_SOURCEPACKAGE, &one, 1);
238 /* Retrofit "Provide: name = EVR" for binary packages. */
239 providePackageNVR(h);
243 int headerConvert(Header h, headerConvOps op)
251 case HEADERCONV_EXPANDFILELIST:
254 case HEADERCONV_COMPRESSFILELIST:
257 case HEADERCONV_RETROFIT_V3: