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