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