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