turned on gif support during db conversion
[tools/librpm-tizen.git] / convertdb.c
1 /* This converts an old style (rpm 1.x) database to the new style */
2
3 #include <fcntl.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6
7 #include "dbindex.h"
8 #include "falloc.h"
9 #include "header.h"
10 #include "misc.h"
11 #include "oldrpmdb.h"
12 #include "oldheader.h"
13 #include "rpmerr.h"
14 #include "rpmlib.h"
15
16 int convertDB(void);
17
18 int convertDB(void) {
19     struct oldrpmdb olddb;
20     rpmdb db;
21     struct oldrpmdbLabel * packageLabels, * label;
22     struct oldrpmdbPackageInfo package;
23     Header dbentry;
24     char * group;
25     char * gif;
26     int gifSize;
27     char ** fileList;
28     char ** fileMD5List;
29     char ** fileLinktoList;
30     int_32 * fileSizeList;
31     int_32 * fileUIDList;
32     int_32 * fileGIDList;
33     int_32 * fileMtimesList;
34     int_32 * fileFlagsList;
35     int_16 * fileModesList;
36     int_16 * fileRDevsList;
37     char * fileStatesList;
38     char * preun, * postun;
39     int i, j;
40     
41     if (oldrpmdbOpen(&olddb)) {
42         error(RPMERR_OLDDBMISSING, "");
43         return 0;
44     }
45
46     if (exists("/var/lib/rpm/packages.rpm")) {
47 #if 0
48         error(RPMERR_NOCREATEDB, "RPM database already exists");
49         return 0;
50 #endif
51         unlink("/var/lib/rpm/packages.rpm");
52     }
53
54     /* if any of the indexes exist, get rid of them */
55     unlink("/var/lib/rpm/nameindex.rpm");
56     unlink("/var/lib/rpm/groupindex.rpm");
57     unlink("/var/lib/rpm/packageindex.rpm");
58     unlink("/var/lib/rpm/fileindex.rpm");
59
60     if (!rpmdbOpen("", &db, O_RDWR | O_EXCL | O_CREAT, 0644)) {
61         error(RPMERR_DBOPEN, "failed to create RPM database /var/lib/rpm");
62         return 0;
63     }
64
65     packageLabels = oldrpmdbGetAllLabels(&olddb);
66     if (!packageLabels) {
67         error(RPMERR_OLDDBCORRUPT, "");
68         rpmdbClose(db);
69         unlink("/var/lib/rpm/packages.rpm");
70         oldrpmdbClose(&olddb);
71         return 0;
72     }
73
74     for (label = packageLabels; label; label = label->next) {
75         if (oldrpmdbGetPackageInfo(&olddb, *label, &package)) {
76             fprintf(stderr, "oldrpmdbGetPackageInfo failed &olddb = %p olddb.packages = %p\n", &olddb, olddb.packages);
77             exit(1);
78         }
79
80         group = oldrpmdbGetPackageGroup(&olddb, *label);
81         preun = oldrpmdbGetPackagePreun(&olddb, *label);
82         postun = oldrpmdbGetPackagePostun(&olddb, *label);
83
84         dbentry = newHeader();
85         addEntry(dbentry, RPMTAG_NAME, STRING_TYPE, package.name, 1);
86         addEntry(dbentry, RPMTAG_VERSION, STRING_TYPE, package.version, 1);
87         addEntry(dbentry, RPMTAG_RELEASE, STRING_TYPE, package.release, 1);
88         addEntry(dbentry, RPMTAG_DESCRIPTION, STRING_TYPE, 
89                  package.description, 1);
90         addEntry(dbentry, RPMTAG_BUILDTIME, INT32_TYPE, &package.buildTime, 1);
91         addEntry(dbentry, RPMTAG_BUILDHOST, STRING_TYPE, package.buildHost, 1);
92         addEntry(dbentry, RPMTAG_INSTALLTIME, INT32_TYPE, 
93                  &package.installTime, 1);
94         addEntry(dbentry, RPMTAG_DISTRIBUTION, STRING_TYPE, 
95                  package.distribution, 1);
96         addEntry(dbentry, RPMTAG_VENDOR, STRING_TYPE, package.vendor, 1);
97         addEntry(dbentry, RPMTAG_SIZE, INT32_TYPE, &package.size, 1);
98         addEntry(dbentry, RPMTAG_COPYRIGHT, STRING_TYPE, package.copyright, 1);
99         addEntry(dbentry, RPMTAG_GROUP, STRING_TYPE, group, 1);
100
101         if (preun) {
102             addEntry(dbentry, RPMTAG_PREUN, STRING_TYPE, preun, 1);
103             free(preun);
104         }
105         if (postun) {
106             addEntry(dbentry, RPMTAG_POSTUN, STRING_TYPE, postun, 1);
107             free(postun);
108         }
109
110         gif = oldrpmdbGetPackageGif(&olddb, *label, &gifSize);
111         if (gif) {
112             addEntry(dbentry, RPMTAG_GIF, BIN_TYPE, gif, gifSize);
113             free(gif);
114         }
115
116         if (package.fileCount) {
117             /* some packages have no file lists */
118
119             fileList = malloc(sizeof(char *) * package.fileCount);
120             fileLinktoList = malloc(sizeof(char *) * package.fileCount);
121             fileMD5List = malloc(sizeof(char *) * package.fileCount);
122             fileSizeList = malloc(sizeof(int_32) * package.fileCount);
123             fileUIDList = malloc(sizeof(int_32) * package.fileCount);
124             fileGIDList = malloc(sizeof(int_32) * package.fileCount);
125             fileMtimesList = malloc(sizeof(int_32) * package.fileCount);
126             fileFlagsList = malloc(sizeof(int_32) * package.fileCount);
127             fileModesList = malloc(sizeof(int_16) * package.fileCount);
128             fileRDevsList = malloc(sizeof(int_16) * package.fileCount);
129             fileStatesList = malloc(sizeof(char) * package.fileCount);
130
131             /* reverse the file list while we're at it */
132             j = package.fileCount - 1;
133             for (i = 0; i < package.fileCount; i++, j--) {
134                 fileList[j] = package.files[i].path;
135                 fileMD5List[j] = package.files[i].md5;
136                 fileSizeList[j] = package.files[i].size;
137                 fileUIDList[j] = package.files[i].uid;
138                 fileGIDList[j] = package.files[i].gid;
139                 fileMtimesList[j] = package.files[i].mtime;
140                 fileModesList[j] = package.files[i].mode;
141                 fileRDevsList[j] = package.files[i].rdev;
142                 fileStatesList[j] = package.files[i].state;
143
144                 if (package.files[i].linkto)
145                     fileLinktoList[j] = package.files[i].linkto;
146                 else
147                     fileLinktoList[j] = "";
148                 
149                 fileFlagsList[j] = 0;
150                 if (package.files[i].isdoc) 
151                     fileFlagsList[j] |= RPMFILE_DOC;
152                 if (package.files[i].isconf)
153                     fileFlagsList[j] |= RPMFILE_CONFIG;
154             }
155
156             addEntry(dbentry, RPMTAG_FILENAMES, STRING_ARRAY_TYPE, fileList, 
157                      package.fileCount);
158             addEntry(dbentry, RPMTAG_FILELINKTOS, STRING_ARRAY_TYPE, 
159                      fileLinktoList, package.fileCount);
160             addEntry(dbentry, RPMTAG_FILEMD5S, STRING_ARRAY_TYPE, fileMD5List, 
161                      package.fileCount);
162             addEntry(dbentry, RPMTAG_FILESIZES, INT32_TYPE, fileSizeList, 
163                      package.fileCount);
164             addEntry(dbentry, RPMTAG_FILEUIDS, INT32_TYPE, fileUIDList, 
165                      package.fileCount);
166             addEntry(dbentry, RPMTAG_FILEGIDS, INT32_TYPE, fileGIDList, 
167                      package.fileCount);
168             addEntry(dbentry, RPMTAG_FILEMTIMES, INT32_TYPE, fileMtimesList, 
169                      package.fileCount);
170             addEntry(dbentry, RPMTAG_FILEFLAGS, INT32_TYPE, fileFlagsList, 
171                      package.fileCount);
172             addEntry(dbentry, RPMTAG_FILEMODES, INT16_TYPE, fileModesList, 
173                      package.fileCount);
174             addEntry(dbentry, RPMTAG_FILERDEVS, INT16_TYPE, fileRDevsList, 
175                      package.fileCount);
176             addEntry(dbentry, RPMTAG_FILESTATES, INT8_TYPE, fileStatesList, 
177                      package.fileCount);
178
179             free(fileList);
180             free(fileLinktoList);
181             free(fileMD5List);
182             free(fileSizeList);
183             free(fileUIDList);
184             free(fileGIDList);
185             free(fileMtimesList);
186             free(fileFlagsList);
187             free(fileModesList);
188             free(fileRDevsList);
189             free(fileStatesList);
190         }
191
192         rpmdbAdd(db, dbentry);
193
194         free(group);
195         freeHeader(dbentry);
196
197         oldrpmdbFreePackageInfo(package);
198     }
199
200     oldrpmdbClose(&olddb);
201     rpmdbClose(db);
202
203     return 1;
204 }
205
206 int main(int argc, char ** argv) {
207     if (argc != 1) {
208         fprintf(stderr, "rpmconvert: no arguments expected");
209         exit(1);
210     }
211
212     printf("rpmconvert 1.0 - converting database in /var/lib/rpm\n");
213     convertDB();
214
215     exit(0);
216 }