Merge branch 'master' of ssh://login.linux.duke.edu/home/groups/rpm/git/rpm
[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     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;
32     char ** fileNames;
33     char ** dirNames;
34     const char ** baseNames;
35     uint32_t * dirIndexes;
36     rpmTagType fnt;
37     rpm_count_t count, i;
38     int xx;
39     int dirIndex = -1;
40
41     /*
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
44      * a bit faster.
45      */
46
47     if (headerIsEntry(h, RPMTAG_DIRNAMES)) {
48         xx = hre(h, RPMTAG_OLDFILENAMES);
49         return;         /* Already converted. */
50     }
51
52     if (!hge(h, RPMTAG_OLDFILENAMES, &fnt, (rpm_data_t *) &fileNames, &count))
53         return;         /* no file list */
54     if (fileNames == NULL || count <= 0)
55         return;
56
57     dirNames = xmalloc(sizeof(*dirNames) * count);      /* worst case */
58     baseNames = xmalloc(sizeof(*dirNames) * count);
59     dirIndexes = xmalloc(sizeof(*dirIndexes) * count);
60
61     if (fileNames[0][0] != '/') {
62         /* HACK. Source RPM, so just do things differently */
63         dirIndex = 0;
64         dirNames[dirIndex] = xstrdup("");
65         for (i = 0; i < count; i++) {
66             dirIndexes[i] = dirIndex;
67             baseNames[i] = fileNames[i];
68         }
69         goto exit;
70     }
71
72     for (i = 0; i < count; i++) {
73         char ** needle;
74         char savechar;
75         char * baseName;
76         size_t len;
77
78         if (fileNames[i] == NULL)       /* XXX can't happen */
79             continue;
80         baseName = strrchr(fileNames[i], '/') + 1;
81         len = baseName - fileNames[i];
82         needle = dirNames;
83         savechar = *baseName;
84         *baseName = '\0';
85         if (dirIndex < 0 ||
86             (needle = bsearch(&fileNames[i], dirNames, dirIndex + 1, sizeof(dirNames[0]), dncmp)) == NULL) {
87             char *s = xmalloc(len + 1);
88             rstrlcpy(s, fileNames[i], len + 1);
89             dirIndexes[i] = ++dirIndex;
90             dirNames[dirIndex] = s;
91         } else
92             dirIndexes[i] = needle - dirNames;
93
94         *baseName = savechar;
95         baseNames[i] = baseName;
96     }
97
98 exit:
99     if (count > 0) {
100         xx = hae(h, RPMTAG_DIRINDEXES, RPM_INT32_TYPE, dirIndexes, count);
101         xx = hae(h, RPMTAG_BASENAMES, RPM_STRING_ARRAY_TYPE,
102                         baseNames, count);
103         xx = hae(h, RPMTAG_DIRNAMES, RPM_STRING_ARRAY_TYPE,
104                         dirNames, (rpm_count_t) dirIndex + 1);
105     }
106
107     fileNames = hfd(fileNames, fnt);
108     for (i = 0; i <= dirIndex; i++) {
109         free(dirNames[i]);
110     }
111     free(dirNames);
112     free(baseNames);
113     free(dirIndexes);
114
115     xx = hre(h, RPMTAG_OLDFILENAMES);
116 }
117
118 void expandFilelist(Header h)
119 {
120     HAE_t hae = (HAE_t)headerAddEntry;
121     HRE_t hre = (HRE_t)headerRemoveEntry;
122     const char ** fileNames = NULL;
123     rpm_count_t count = 0;
124     int xx;
125
126     if (!headerIsEntry(h, RPMTAG_OLDFILENAMES)) {
127         rpmfiBuildFNames(h, RPMTAG_BASENAMES, &fileNames, &count);
128         if (fileNames == NULL || count <= 0)
129             return;
130         xx = hae(h, RPMTAG_OLDFILENAMES, RPM_STRING_ARRAY_TYPE,
131                         fileNames, count);
132         fileNames = _free(fileNames);
133     }
134
135     xx = hre(h, RPMTAG_DIRNAMES);
136     xx = hre(h, RPMTAG_BASENAMES);
137     xx = hre(h, RPMTAG_DIRINDEXES);
138 }
139
140 /*
141  * Up to rpm 3.0.4, packages implicitly provided their own name-version-release.
142  * Retrofit an explicit "Provides: name = epoch:version-release.
143  */
144 static void providePackageNVR(Header h)
145 {
146     HGE_t hge = (HGE_t)headerGetEntryMinMemory;
147     HFD_t hfd = headerFreeData;
148     const char *name;
149     char *pEVR;
150     rpmsenseFlags pFlags = RPMSENSE_EQUAL;
151     const char ** provides = NULL;
152     const char ** providesEVR = NULL;
153     rpmTagType pnt, pvt;
154     rpmsenseFlags * provideFlags = NULL;
155     rpm_count_t providesCount, i;
156     int xx;
157     int bingo = 1;
158
159     /* Generate provides for this package name-version-release. */
160     pEVR = headerGetEVR(h, &name);
161     if (!(name && pEVR))
162         return;
163
164     /*
165      * Rpm prior to 3.0.3 does not have versioned provides.
166      * If no provides at all are available, we can just add.
167      */
168     if (!hge(h, RPMTAG_PROVIDENAME, &pnt, (rpm_data_t *) &provides, &providesCount))
169         goto exit;
170
171     /*
172      * Otherwise, fill in entries on legacy packages.
173      */
174     if (!hge(h, RPMTAG_PROVIDEVERSION, &pvt, (rpm_data_t *) &providesEVR, NULL)) {
175         for (i = 0; i < providesCount; i++) {
176             const char * vdummy = "";
177             rpmsenseFlags fdummy = RPMSENSE_ANY;
178             xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDEVERSION, RPM_STRING_ARRAY_TYPE,
179                         &vdummy, 1);
180             xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDEFLAGS, RPM_INT32_TYPE,
181                         &fdummy, 1);
182         }
183         goto exit;
184     }
185
186     xx = hge(h, RPMTAG_PROVIDEFLAGS, NULL, (rpm_data_t *) &provideFlags, NULL);
187
188         /* LCL: providesEVR is not NULL */
189     if (provides && providesEVR && provideFlags)
190     for (i = 0; i < providesCount; i++) {
191         if (!(provides[i] && providesEVR[i]))
192             continue;
193         if (!(provideFlags[i] == RPMSENSE_EQUAL &&
194             !strcmp(name, provides[i]) && !strcmp(pEVR, providesEVR[i])))
195             continue;
196         bingo = 0;
197         break;
198     }
199
200 exit:
201     provides = hfd(provides, pnt);
202     providesEVR = hfd(providesEVR, pvt);
203
204     if (bingo) {
205         xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDENAME, RPM_STRING_ARRAY_TYPE,
206                 &name, 1);
207         xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDEFLAGS, RPM_INT32_TYPE,
208                 &pFlags, 1);
209         xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDEVERSION, RPM_STRING_ARRAY_TYPE,
210                 &pEVR, 1);
211     }
212     free(pEVR);
213 }
214
215 void legacyRetrofit(Header h)
216 {
217     const char * prefix;
218
219     /*
220      * We don't use these entries (and rpm >= 2 never has) and they are
221      * pretty misleading. Let's just get rid of them so they don't confuse
222      * anyone.
223      */
224     if (headerIsEntry(h, RPMTAG_FILEUSERNAME))
225         (void) headerRemoveEntry(h, RPMTAG_FILEUIDS);
226     if (headerIsEntry(h, RPMTAG_FILEGROUPNAME))
227         (void) headerRemoveEntry(h, RPMTAG_FILEGIDS);
228
229     /*
230      * We switched the way we do relocatable packages. We fix some of
231      * it up here, though the install code still has to be a bit 
232      * careful. This fixup makes queries give the new values though,
233      * which is quite handy.
234      */
235     if (headerGetEntry(h, RPMTAG_DEFAULTPREFIX, NULL, (rpm_data_t *) &prefix, NULL))
236     {
237         char * nprefix = stripTrailingChar(xstrdup(prefix), '/');
238         (void) headerAddEntry(h, RPMTAG_PREFIXES, RPM_STRING_ARRAY_TYPE,
239                 &nprefix, 1); 
240         free(nprefix);
241     }
242
243     /*
244      * The file list was moved to a more compressed format which not
245      * only saves memory (nice), but gives fingerprinting a nice, fat
246      * speed boost (very nice). Go ahead and convert old headers to
247      * the new style (this is a noop for new headers).
248      */
249      compressFilelist(h);
250
251     /* XXX binary rpms always have RPMTAG_SOURCERPM, source rpms do not */
252     if (headerIsSource(h)) {
253         int32_t one = 1;
254         if (!headerIsEntry(h, RPMTAG_SOURCEPACKAGE))
255             (void) headerAddEntry(h, RPMTAG_SOURCEPACKAGE, RPM_INT32_TYPE,
256                                 &one, 1);
257     } else {
258         /* Retrofit "Provide: name = EVR" for binary packages. */
259         providePackageNVR(h);
260     }
261 }