Magellan Linux

Contents of /trunk/mkinitrd-magellan/busybox/archival/rpm.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 532 - (show annotations) (download)
Sat Sep 1 22:45:15 2007 UTC (16 years, 8 months ago) by niro
File MIME type: text/plain
File size: 12143 byte(s)
-import if magellan mkinitrd; it is a fork of redhats mkinitrd-5.0.8 with all magellan patches and features; deprecates magellan-src/mkinitrd

1 /* vi: set sw=4 ts=4: */
2 /*
3 * Mini rpm applet for busybox
4 *
5 * Copyright (C) 2001,2002 by Laurence Anderson
6 *
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8 */
9
10 #include "busybox.h"
11 #include "unarchive.h"
12
13 #define RPM_HEADER_MAGIC "\216\255\350"
14 #define RPM_CHAR_TYPE 1
15 #define RPM_INT8_TYPE 2
16 #define RPM_INT16_TYPE 3
17 #define RPM_INT32_TYPE 4
18 /* #define RPM_INT64_TYPE 5 ---- These aren't supported (yet) */
19 #define RPM_STRING_TYPE 6
20 #define RPM_BIN_TYPE 7
21 #define RPM_STRING_ARRAY_TYPE 8
22 #define RPM_I18NSTRING_TYPE 9
23
24 #define TAG_NAME 1000
25 #define TAG_VERSION 1001
26 #define TAG_RELEASE 1002
27 #define TAG_SUMMARY 1004
28 #define TAG_DESCRIPTION 1005
29 #define TAG_BUILDTIME 1006
30 #define TAG_BUILDHOST 1007
31 #define TAG_SIZE 1009
32 #define TAG_VENDOR 1011
33 #define TAG_LICENSE 1014
34 #define TAG_PACKAGER 1015
35 #define TAG_GROUP 1016
36 #define TAG_URL 1020
37 #define TAG_PREIN 1023
38 #define TAG_POSTIN 1024
39 #define TAG_FILEFLAGS 1037
40 #define TAG_FILEUSERNAME 1039
41 #define TAG_FILEGROUPNAME 1040
42 #define TAG_SOURCERPM 1044
43 #define TAG_PREINPROG 1085
44 #define TAG_POSTINPROG 1086
45 #define TAG_PREFIXS 1098
46 #define TAG_DIRINDEXES 1116
47 #define TAG_BASENAMES 1117
48 #define TAG_DIRNAMES 1118
49 #define RPMFILE_CONFIG (1 << 0)
50 #define RPMFILE_DOC (1 << 1)
51
52 enum rpm_functions_e {
53 rpm_query = 1,
54 rpm_install = 2,
55 rpm_query_info = 4,
56 rpm_query_package = 8,
57 rpm_query_list = 16,
58 rpm_query_list_doc = 32,
59 rpm_query_list_config = 64
60 };
61
62 typedef struct {
63 uint32_t tag; /* 4 byte tag */
64 uint32_t type; /* 4 byte type */
65 uint32_t offset; /* 4 byte offset */
66 uint32_t count; /* 4 byte count */
67 } rpm_index;
68
69 static void *map;
70 static rpm_index **mytags;
71 static int tagcount;
72
73 static void extract_cpio_gz(int fd);
74 static rpm_index **rpm_gettags(int fd, int *num_tags);
75 static int bsearch_rpmtag(const void *key, const void *item);
76 static char *rpm_getstr(int tag, int itemindex);
77 static int rpm_getint(int tag, int itemindex);
78 static int rpm_getcount(int tag);
79 static void fileaction_dobackup(char *filename, int fileref);
80 static void fileaction_setowngrp(char *filename, int fileref);
81 static void loop_through_files(int filetag, void (*fileaction)(char *filename, int fileref));
82
83 int rpm_main(int argc, char **argv)
84 {
85 int opt = 0, func = 0, rpm_fd, offset;
86 const int pagesize = getpagesize();
87
88 while ((opt = getopt(argc, argv, "iqpldc")) != -1) {
89 switch (opt) {
90 case 'i': /* First arg: Install mode, with q: Information */
91 if (!func) func = rpm_install;
92 else func |= rpm_query_info;
93 break;
94 case 'q': /* First arg: Query mode */
95 if (func) bb_show_usage();
96 func = rpm_query;
97 break;
98 case 'p': /* Query a package */
99 func |= rpm_query_package;
100 break;
101 case 'l': /* List files in a package */
102 func |= rpm_query_list;
103 break;
104 case 'd': /* List doc files in a package (implies list) */
105 func |= rpm_query_list;
106 func |= rpm_query_list_doc;
107 break;
108 case 'c': /* List config files in a package (implies list) */
109 func |= rpm_query_list;
110 func |= rpm_query_list_config;
111 break;
112 default:
113 bb_show_usage();
114 }
115 }
116 argv += optind;
117 argc -= optind;
118 if (!argc) bb_show_usage();
119
120 while (*argv) {
121 rpm_fd = xopen(*argv++, O_RDONLY);
122 mytags = rpm_gettags(rpm_fd, &tagcount);
123 if (!mytags)
124 bb_error_msg_and_die("error reading rpm header");
125 offset = xlseek(rpm_fd, 0, SEEK_CUR);
126 /* Mimimum is one page */
127 map = mmap(0, offset > pagesize ? (offset + offset % pagesize) : pagesize, PROT_READ, MAP_PRIVATE, rpm_fd, 0);
128
129 if (func & rpm_install) {
130 /* Backup any config files */
131 loop_through_files(TAG_BASENAMES, fileaction_dobackup);
132 /* Extact the archive */
133 extract_cpio_gz(rpm_fd);
134 /* Set the correct file uid/gid's */
135 loop_through_files(TAG_BASENAMES, fileaction_setowngrp);
136 }
137 else if ((func & (rpm_query|rpm_query_package)) == (rpm_query|rpm_query_package)) {
138 if (!(func & (rpm_query_info|rpm_query_list))) {
139 /* If just a straight query, just give package name */
140 printf("%s-%s-%s\n", rpm_getstr(TAG_NAME, 0), rpm_getstr(TAG_VERSION, 0), rpm_getstr(TAG_RELEASE, 0));
141 }
142 if (func & rpm_query_info) {
143 /* Do the nice printout */
144 time_t bdate_time;
145 struct tm *bdate;
146 char bdatestring[50];
147 printf("Name : %-29sRelocations: %s\n", rpm_getstr(TAG_NAME, 0), rpm_getstr(TAG_PREFIXS, 0) ? rpm_getstr(TAG_PREFIXS, 0) : "(not relocateable)");
148 printf("Version : %-34sVendor: %s\n", rpm_getstr(TAG_VERSION, 0), rpm_getstr(TAG_VENDOR, 0) ? rpm_getstr(TAG_VENDOR, 0) : "(none)");
149 bdate_time = rpm_getint(TAG_BUILDTIME, 0);
150 bdate = localtime((time_t *) &bdate_time);
151 strftime(bdatestring, 50, "%a %d %b %Y %T %Z", bdate);
152 printf("Release : %-30sBuild Date: %s\n", rpm_getstr(TAG_RELEASE, 0), bdatestring);
153 printf("Install date: %-30sBuild Host: %s\n", "(not installed)", rpm_getstr(TAG_BUILDHOST, 0));
154 printf("Group : %-30sSource RPM: %s\n", rpm_getstr(TAG_GROUP, 0), rpm_getstr(TAG_SOURCERPM, 0));
155 printf("Size : %-33dLicense: %s\n", rpm_getint(TAG_SIZE, 0), rpm_getstr(TAG_LICENSE, 0));
156 printf("URL : %s\n", rpm_getstr(TAG_URL, 0));
157 printf("Summary : %s\n", rpm_getstr(TAG_SUMMARY, 0));
158 printf("Description :\n%s\n", rpm_getstr(TAG_DESCRIPTION, 0));
159 }
160 if (func & rpm_query_list) {
161 int count, it, flags;
162 count = rpm_getcount(TAG_BASENAMES);
163 for (it = 0; it < count; it++) {
164 flags = rpm_getint(TAG_FILEFLAGS, it);
165 switch (func & (rpm_query_list_doc|rpm_query_list_config)) {
166 case rpm_query_list_doc:
167 if (!(flags & RPMFILE_DOC)) continue;
168 break;
169 case rpm_query_list_config:
170 if (!(flags & RPMFILE_CONFIG)) continue;
171 break;
172 case rpm_query_list_doc|rpm_query_list_config:
173 if (!(flags & (RPMFILE_CONFIG|RPMFILE_DOC))) continue;
174 break;
175 }
176 printf("%s%s\n",
177 rpm_getstr(TAG_DIRNAMES, rpm_getint(TAG_DIRINDEXES, it)),
178 rpm_getstr(TAG_BASENAMES, it));
179 }
180 }
181 }
182 free(mytags);
183 }
184 return 0;
185 }
186
187 static void extract_cpio_gz(int fd) {
188 archive_handle_t *archive_handle;
189 unsigned char magic[2];
190
191 /* Initialise */
192 archive_handle = init_handle();
193 archive_handle->seek = seek_by_read;
194 //archive_handle->action_header = header_list;
195 archive_handle->action_data = data_extract_all;
196 archive_handle->flags |= ARCHIVE_PRESERVE_DATE;
197 archive_handle->flags |= ARCHIVE_CREATE_LEADING_DIRS;
198 archive_handle->src_fd = fd;
199 archive_handle->offset = 0;
200
201 xread(archive_handle->src_fd, &magic, 2);
202 if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) {
203 bb_error_msg_and_die("invalid gzip magic");
204 }
205 check_header_gzip(archive_handle->src_fd);
206 xchdir("/"); /* Install RPM's to root */
207
208 archive_handle->src_fd = open_transformer(archive_handle->src_fd, inflate_gunzip);
209 archive_handle->offset = 0;
210 while (get_header_cpio(archive_handle) == EXIT_SUCCESS)
211 /* loop */;
212 }
213
214
215 static rpm_index **rpm_gettags(int fd, int *num_tags)
216 {
217 /* We should never need mode than 200, and realloc later */
218 rpm_index **tags = xzalloc(200 * sizeof(struct rpmtag *));
219 int pass, tagindex = 0;
220
221 xlseek(fd, 96, SEEK_CUR); /* Seek past the unused lead */
222
223 /* 1st pass is the signature headers, 2nd is the main stuff */
224 for (pass = 0; pass < 2; pass++) {
225 struct {
226 char magic[3]; /* 3 byte magic: 0x8e 0xad 0xe8 */
227 uint8_t version; /* 1 byte version number */
228 uint32_t reserved; /* 4 bytes reserved */
229 uint32_t entries; /* Number of entries in header (4 bytes) */
230 uint32_t size; /* Size of store (4 bytes) */
231 } header;
232 rpm_index *tmpindex;
233 int storepos;
234
235 xread(fd, &header, sizeof(header));
236 if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC, 3) != 0)
237 return NULL; /* Invalid magic */
238 if (header.version != 1)
239 return NULL; /* This program only supports v1 headers */
240 header.size = ntohl(header.size);
241 header.entries = ntohl(header.entries);
242 storepos = xlseek(fd,0,SEEK_CUR) + header.entries * 16;
243
244 while (header.entries--) {
245 tmpindex = tags[tagindex++] = xmalloc(sizeof(rpm_index));
246 xread(fd, tmpindex, sizeof(rpm_index));
247 tmpindex->tag = ntohl(tmpindex->tag);
248 tmpindex->type = ntohl(tmpindex->type);
249 tmpindex->count = ntohl(tmpindex->count);
250 tmpindex->offset = storepos + ntohl(tmpindex->offset);
251 if (pass==0)
252 tmpindex->tag -= 743;
253 }
254 xlseek(fd, header.size, SEEK_CUR); /* Seek past store */
255 /* Skip padding to 8 byte boundary after reading signature headers */
256 if (pass==0)
257 xlseek(fd, (8 - (xlseek(fd,0,SEEK_CUR) % 8)) % 8, SEEK_CUR);
258 }
259 tags = xrealloc(tags, tagindex * sizeof(struct rpmtag *)); /* realloc tags to save space */
260 *num_tags = tagindex;
261 return tags; /* All done, leave the file at the start of the gzipped cpio archive */
262 }
263
264 static int bsearch_rpmtag(const void *key, const void *item)
265 {
266 int *tag = (int *)key;
267 rpm_index **tmp = (rpm_index **) item;
268 return (*tag - tmp[0]->tag);
269 }
270
271 static int rpm_getcount(int tag)
272 {
273 rpm_index **found;
274 found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
275 if (!found)
276 return 0;
277 return found[0]->count;
278 }
279
280 static char *rpm_getstr(int tag, int itemindex)
281 {
282 rpm_index **found;
283 found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
284 if (!found || itemindex >= found[0]->count)
285 return NULL;
286 if (found[0]->type == RPM_STRING_TYPE || found[0]->type == RPM_I18NSTRING_TYPE || found[0]->type == RPM_STRING_ARRAY_TYPE) {
287 int n;
288 char *tmpstr = (char *) (map + found[0]->offset);
289 for (n=0; n < itemindex; n++)
290 tmpstr = tmpstr + strlen(tmpstr) + 1;
291 return tmpstr;
292 }
293 return NULL;
294 }
295
296 static int rpm_getint(int tag, int itemindex)
297 {
298 rpm_index **found;
299 int *tmpint; /* NB: using int8_t* would be easier to code */
300
301 /* gcc throws warnings here when sizeof(void*)!=sizeof(int) ...
302 * it's ok to ignore it because tag won't be used as a pointer */
303 found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
304 if (!found || itemindex >= found[0]->count)
305 return -1;
306
307 tmpint = (int *) (map + found[0]->offset);
308
309 if (found[0]->type == RPM_INT32_TYPE) {
310 tmpint = (int *) ((char *) tmpint + itemindex*4);
311 /*return ntohl(*tmpint);*/
312 /* int can be != int32_t */
313 return ntohl(*(int32_t*)tmpint);
314 }
315 if (found[0]->type == RPM_INT16_TYPE) {
316 tmpint = (int *) ((char *) tmpint + itemindex*2);
317 /* ??? read int, and THEN ntohs() it?? */
318 /*return ntohs(*tmpint);*/
319 return ntohs(*(int16_t*)tmpint);
320 }
321 if (found[0]->type == RPM_INT8_TYPE) {
322 tmpint = (int *) ((char *) tmpint + itemindex);
323 /* ??? why we don't read byte here??? */
324 /*return ntohs(*tmpint);*/
325 return *(int8_t*)tmpint;
326 }
327 return -1;
328 }
329
330 static void fileaction_dobackup(char *filename, int fileref)
331 {
332 struct stat oldfile;
333 int stat_res;
334 char *newname;
335 if (rpm_getint(TAG_FILEFLAGS, fileref) & RPMFILE_CONFIG) {
336 /* Only need to backup config files */
337 stat_res = lstat(filename, &oldfile);
338 if (stat_res == 0 && S_ISREG(oldfile.st_mode)) {
339 /* File already exists - really should check MD5's etc to see if different */
340 newname = xasprintf("%s.rpmorig", filename);
341 copy_file(filename, newname, FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS);
342 remove_file(filename, FILEUTILS_RECUR | FILEUTILS_FORCE);
343 free(newname);
344 }
345 }
346 }
347
348 static void fileaction_setowngrp(char *filename, int fileref)
349 {
350 int uid, gid;
351 uid = xuname2uid(rpm_getstr(TAG_FILEUSERNAME, fileref));
352 gid = xgroup2gid(rpm_getstr(TAG_FILEGROUPNAME, fileref));
353 chown(filename, uid, gid);
354 }
355
356 static void loop_through_files(int filetag, void (*fileaction)(char *filename, int fileref))
357 {
358 int count = 0;
359 while (rpm_getstr(filetag, count)) {
360 char* filename = xasprintf("%s%s",
361 rpm_getstr(TAG_DIRNAMES, rpm_getint(TAG_DIRINDEXES, count)),
362 rpm_getstr(TAG_BASENAMES, count));
363 fileaction(filename, count++);
364 free(filename);
365 }
366 }