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