Simplify providePackageNVR()
[platform/upstream/rpm.git] / lib / legacy.c
1 /**
2  * \file lib/legacy.c
3  */
4
5 #include "system.h"
6
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>
12
13 #include "lib/legacy.h"
14
15 #include "debug.h"
16
17 int _noDirTokens = 0;
18
19 static int dncmp(const void * a, const void * b)
20 {
21     const char *const * first = a;
22     const char *const * second = b;
23     return strcmp(*first, *second);
24 }
25
26 void compressFilelist(Header h)
27 {
28     HAE_t hae = (HAE_t)headerAddEntry;
29     HRE_t hre = (HRE_t)headerRemoveEntry;
30     struct rpmtd_s fileNames;
31     char ** dirNames;
32     const char ** baseNames;
33     uint32_t * dirIndexes;
34     rpm_count_t count;
35     int xx, i;
36     int dirIndex = -1;
37
38     /*
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
41      * a bit faster.
42      */
43
44     if (headerIsEntry(h, RPMTAG_DIRNAMES)) {
45         xx = hre(h, RPMTAG_OLDFILENAMES);
46         return;         /* Already converted. */
47     }
48
49     if (!headerGet(h, RPMTAG_OLDFILENAMES, &fileNames, HEADERGET_MINMEM)) 
50         return;
51     count = rpmtdCount(&fileNames);
52     if (count < 1) 
53         return;
54
55     dirNames = xmalloc(sizeof(*dirNames) * count);      /* worst case */
56     baseNames = xmalloc(sizeof(*dirNames) * count);
57     dirIndexes = xmalloc(sizeof(*dirIndexes) * count);
58
59     if (headerIsSource(h)) {
60         /* HACK. Source RPM, so just do things differently */
61         dirIndex = 0;
62         dirNames[dirIndex] = xstrdup("");
63         while ((i = rpmtdNext(&fileNames)) >= 0) {
64             dirIndexes[i] = dirIndex;
65             baseNames[i] = rpmtdGetString(&fileNames);
66         }
67         goto exit;
68     }
69
70     while ((i = rpmtdNext(&fileNames)) >= 0) {
71         char ** needle;
72         char savechar;
73         char * baseName;
74         size_t len;
75         const char *filename = rpmtdGetString(&fileNames);
76
77         if (filename == NULL)   /* XXX can't happen */
78             continue;
79         baseName = strrchr(filename, '/') + 1;
80         len = baseName - filename;
81         needle = dirNames;
82         savechar = *baseName;
83         *baseName = '\0';
84         if (dirIndex < 0 ||
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;
90         } else
91             dirIndexes[i] = needle - dirNames;
92
93         *baseName = savechar;
94         baseNames[i] = baseName;
95     }
96
97 exit:
98     if (count > 0) {
99         xx = hae(h, RPMTAG_DIRINDEXES, RPM_INT32_TYPE, dirIndexes, count);
100         xx = hae(h, RPMTAG_BASENAMES, RPM_STRING_ARRAY_TYPE,
101                         baseNames, count);
102         xx = hae(h, RPMTAG_DIRNAMES, RPM_STRING_ARRAY_TYPE,
103                         dirNames, (rpm_count_t) dirIndex + 1);
104     }
105
106     rpmtdFreeData(&fileNames);
107     for (i = 0; i <= dirIndex; i++) {
108         free(dirNames[i]);
109     }
110     free(dirNames);
111     free(baseNames);
112     free(dirIndexes);
113
114     xx = hre(h, RPMTAG_OLDFILENAMES);
115 }
116
117 void expandFilelist(Header h)
118 {
119     HRE_t hre = (HRE_t)headerRemoveEntry;
120     struct rpmtd_s filenames;
121
122     if (!headerIsEntry(h, RPMTAG_OLDFILENAMES)) {
123         (void) headerGet(h, RPMTAG_FILENAMES, &filenames, HEADERGET_EXT);
124         if (rpmtdCount(&filenames) < 1)
125             return;
126         rpmtdSetTag(&filenames, RPMTAG_OLDFILENAMES);
127         headerPut(h, &filenames, HEADERPUT_DEFAULT);
128         rpmtdFreeData(&filenames);
129     }
130
131     (void) hre(h, RPMTAG_DIRNAMES);
132     (void) hre(h, RPMTAG_BASENAMES);
133     (void) hre(h, RPMTAG_DIRINDEXES);
134 }
135
136 /*
137  * Up to rpm 3.0.4, packages implicitly provided their own name-version-release.
138  * Retrofit an explicit "Provides: name = epoch:version-release.
139  */
140 static void providePackageNVR(Header h)
141 {
142     const char *name;
143     char *pEVR;
144     rpmsenseFlags pFlags = RPMSENSE_EQUAL;
145     int bingo = 1;
146     struct rpmtd_s pnames;
147     rpmds hds, nvrds;
148
149     /* Generate provides for this package name-version-release. */
150     pEVR = headerGetEVR(h, &name);
151     if (!(name && pEVR))
152         return;
153
154     /*
155      * Rpm prior to 3.0.3 does not have versioned provides.
156      * If no provides at all are available, we can just add.
157      */
158     if (!headerGet(h, RPMTAG_PROVIDENAME, &pnames, HEADERGET_MINMEM)) {
159         goto exit;
160     }
161
162     /*
163      * Otherwise, fill in entries on legacy packages.
164      */
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,
170                         &vdummy, 1);
171             headerAddOrAppendEntry(h, RPMTAG_PROVIDEFLAGS, RPM_INT32_TYPE,
172                         &fdummy, 1);
173         }
174         goto exit;
175     }
176
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) {
181         bingo = 0;
182     }
183     rpmdsFree(hds);
184     rpmdsFree(nvrds);
185     
186
187 exit:
188     if (bingo) {
189         headerAddOrAppendEntry(h, RPMTAG_PROVIDENAME, RPM_STRING_ARRAY_TYPE,
190                 &name, 1);
191         headerAddOrAppendEntry(h, RPMTAG_PROVIDEFLAGS, RPM_INT32_TYPE,
192                 &pFlags, 1);
193         headerAddOrAppendEntry(h, RPMTAG_PROVIDEVERSION, RPM_STRING_ARRAY_TYPE,
194                 &pEVR, 1);
195     }
196     rpmtdFreeData(&pnames);
197     free(pEVR);
198 }
199
200 void legacyRetrofit(Header h)
201 {
202     struct rpmtd_s dprefix;
203
204     /*
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
207      * anyone.
208      */
209     if (headerIsEntry(h, RPMTAG_FILEUSERNAME))
210         (void) headerRemoveEntry(h, RPMTAG_FILEUIDS);
211     if (headerIsEntry(h, RPMTAG_FILEGROUPNAME))
212         (void) headerRemoveEntry(h, RPMTAG_FILEGIDS);
213
214     /*
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.
219      */
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,
224                 &nprefix, 1); 
225         free(nprefix);
226         rpmtdFreeData(&dprefix);
227     }
228
229     /*
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).
234      */
235      compressFilelist(h);
236
237     /* XXX binary rpms always have RPMTAG_SOURCERPM, source rpms do not */
238     if (headerIsSource(h)) {
239         int32_t one = 1;
240         if (!headerIsEntry(h, RPMTAG_SOURCEPACKAGE))
241             (void) headerAddEntry(h, RPMTAG_SOURCEPACKAGE, RPM_INT32_TYPE,
242                                 &one, 1);
243     } else {
244         /* Retrofit "Provide: name = EVR" for binary packages. */
245         providePackageNVR(h);
246     }
247 }