Changed RPMTAB_FILESTATES to INT8 from CHAR
[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 "falloc.h"
8 #include "header.h"
9 #include "misc.h"
10 #include "oldrpmdb.h"
11 #include "oldrpmfile.h"
12 #include "rpmerr.h"
13 #include "rpmlib.h"
14
15 int reindexDB(char * dbprefix) {
16     return 0;
17 }
18
19 int convertDB(char * dbprefix) {
20     struct rpmdb olddb;
21     faFile pkgs;
22     struct rpmdbLabel * packageLabels, * label;
23     struct rpmdbPackageInfo package;
24     Header dbentry;
25     unsigned int dboffset;
26     char * group;
27     char * gif;
28     int gifSize;
29     char ** fileList;
30     char ** fileMD5List;
31     char ** fileLinktoList;
32     int_32 * fileSizeList;
33     int_32 * fileUIDList;
34     int_32 * fileGIDList;
35     int_32 * fileMtimesList;
36     int_32 * fileFlagsList;
37     int_16 * fileModesList;
38     int_16 * fileRDevsList;
39     char * fileStatesList;
40     int i;
41     
42     if (rpmdbOpen(&olddb)) {
43         error(RPMERR_OLDDBMISSING);
44         return 0;
45     }
46
47     if (exists("/var/lib/rpm/packages.rpm")) {
48 #if 0
49         error(RPMERR_NOCREATEDB);
50         return 0;
51 #endif
52         unlink("/var/lib/rpm/packages.rpm");
53     }
54
55     pkgs = faOpen("/var/lib/rpm/packages.rpm", O_RDWR | O_CREAT, 0644);
56     if (!pkgs) {
57         rpmdbClose(&olddb);
58         error(RPMERR_DBOPEN);
59         return 0;
60     }
61
62     packageLabels = rpmdbGetAllLabels(&olddb);
63     if (!packageLabels) {
64         error(RPMERR_OLDDBCORRUPT);
65         faClose(pkgs);
66         unlink("/var/lib/rpm/packages.rpm");
67         rpmdbClose(&olddb);
68         return 0;
69     }
70
71     for (label = packageLabels; label; label = label->next) {
72         if (rpmdbGetPackageInfo(&olddb, *label, &package)) {
73             printf("rpmdbGetPackageInfo failed &olddb = %p olddb.packages = %p\n", &olddb, olddb.packages);
74             exit(1);
75         }
76
77         group = rpmdbGetPackageGroup(&olddb, *label);
78
79         dbentry = newHeader();
80         addEntry(dbentry, RPMTAG_NAME, STRING_TYPE, package.name, 1);
81         addEntry(dbentry, RPMTAG_VERSION, STRING_TYPE, package.version, 1);
82         addEntry(dbentry, RPMTAG_RELEASE, STRING_TYPE, package.release, 1);
83         addEntry(dbentry, RPMTAG_DESCRIPTION, STRING_TYPE, 
84                  package.description, 1);
85         addEntry(dbentry, RPMTAG_BUILDTIME, INT32_TYPE, &package.buildTime, 1);
86         addEntry(dbentry, RPMTAG_BUILDHOST, STRING_TYPE, package.buildHost, 1);
87         addEntry(dbentry, RPMTAG_INSTALLTIME, INT32_TYPE, 
88                  &package.installTime, 1);
89         addEntry(dbentry, RPMTAG_DISTRIBUTION, STRING_TYPE, 
90                  package.distribution, 1);
91         addEntry(dbentry, RPMTAG_VENDOR, STRING_TYPE, package.vendor, 1);
92         addEntry(dbentry, RPMTAG_SIZE, INT32_TYPE, &package.size, 1);
93         addEntry(dbentry, RPMTAG_COPYRIGHT, STRING_TYPE, package.copyright, 1);
94         addEntry(dbentry, RPMTAG_GROUP, STRING_TYPE, group, 1);
95
96         gif = rpmdbGetPackageGif(&olddb, *label, &gifSize);
97         if (gif) {
98             /*addEntry(dbentry, RPMTAG_GIF, BIN_TYPE, gif, gifSize);*/
99             free(gif);
100         }
101
102         if (package.fileCount) {
103             /* some packages have no file lists */
104
105             fileList = malloc(sizeof(char *) * package.fileCount);
106             fileLinktoList = malloc(sizeof(char *) * package.fileCount);
107             fileMD5List = malloc(sizeof(char *) * package.fileCount);
108             fileSizeList = malloc(sizeof(int_32) * package.fileCount);
109             fileUIDList = malloc(sizeof(int_32) * package.fileCount);
110             fileGIDList = malloc(sizeof(int_32) * package.fileCount);
111             fileMtimesList = malloc(sizeof(int_32) * package.fileCount);
112             fileFlagsList = malloc(sizeof(int_32) * package.fileCount);
113             fileModesList = malloc(sizeof(int_16) * package.fileCount);
114             fileRDevsList = malloc(sizeof(int_16) * package.fileCount);
115             fileStatesList = malloc(sizeof(char) * package.fileCount);
116
117             for (i = 0; i < package.fileCount; i++) {
118                 fileList[i] = package.files[i].path;
119                 fileMD5List[i] = package.files[i].md5;
120                 fileSizeList[i] = package.files[i].size;
121                 fileUIDList[i] = package.files[i].uid;
122                 fileGIDList[i] = package.files[i].gid;
123                 fileMtimesList[i] = package.files[i].mtime;
124                 fileModesList[i] = package.files[i].mode;
125                 fileRDevsList[i] = package.files[i].rdev;
126                 fileStatesList[i] = package.files[i].state;
127
128                 if (package.files[i].linkto)
129                     fileLinktoList[i] = package.files[i].linkto;
130                 else
131                     fileLinktoList[i] = "";
132                 
133                 fileFlagsList[i] = 0;
134                 if (package.files[i].isdoc) 
135                     fileFlagsList[i] |= RPMFILE_DOC;
136                 if (package.files[i].isconf)
137                     fileFlagsList[i] |= RPMFILE_CONFIG;
138             }
139
140             addEntry(dbentry, RPMTAG_FILENAMES, STRING_TYPE, fileList, 
141                      package.fileCount);
142             addEntry(dbentry, RPMTAG_FILELINKTOS, STRING_TYPE, fileLinktoList, 
143                      package.fileCount);
144             addEntry(dbentry, RPMTAG_FILEMD5S, STRING_TYPE, fileMD5List, 
145                      package.fileCount);
146             addEntry(dbentry, RPMTAG_FILESIZES, INT32_TYPE, fileSizeList, 
147                      package.fileCount);
148             addEntry(dbentry, RPMTAG_FILEUIDS, INT32_TYPE, fileUIDList, 
149                      package.fileCount);
150             addEntry(dbentry, RPMTAG_FILEGIDS, INT32_TYPE, fileGIDList, 
151                      package.fileCount);
152             addEntry(dbentry, RPMTAG_FILEMTIMES, INT32_TYPE, fileMtimesList, 
153                      package.fileCount);
154             addEntry(dbentry, RPMTAG_FILEFLAGS, INT32_TYPE, fileFlagsList, 
155                      package.fileCount);
156             addEntry(dbentry, RPMTAG_FILEMODES, INT16_TYPE, fileModesList, 
157                      package.fileCount);
158             addEntry(dbentry, RPMTAG_FILERDEVS, INT16_TYPE, fileRDevsList, 
159                      package.fileCount);
160             addEntry(dbentry, RPMTAG_FILESTATES, INT8_TYPE, fileStatesList, 
161                      package.fileCount);
162
163             free(fileList);
164             free(fileLinktoList);
165             free(fileMD5List);
166             free(fileSizeList);
167             free(fileUIDList);
168             free(fileGIDList);
169             free(fileMtimesList);
170             free(fileFlagsList);
171             free(fileModesList);
172             free(fileRDevsList);
173             free(fileStatesList);
174         }
175
176         dboffset = faAlloc(pkgs, sizeofHeader(dbentry));
177         lseek(pkgs->fd, dboffset, SEEK_SET);
178
179         writeHeader(pkgs->fd, dbentry);
180
181         free(group);
182         freeHeader(dbentry);
183
184         rpmdbFreePackageInfo(package);
185     }
186
187     rpmdbClose(&olddb);
188     faClose(pkgs);
189
190     reindexDB(dbprefix);
191     
192     return 1;
193 }