blob: 6c1e341cdbfad41c44731a14c2e1030b27973c2c [file] [log] [blame]
Glenn L McGrathb72a7352002-12-10 00:17:22 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini rpm applet for busybox
4 *
5 * Copyright (C) 2001,2002 by Laurence Anderson
6 *
Bernhard Reutner-Fischer20f40002006-01-30 17:17:14 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrathb72a7352002-12-10 00:17:22 +00008 */
9
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000010#include "libbb.h"
Glenn L McGrath38386d72002-12-10 02:09:12 +000011#include "unarchive.h"
12
Denis Vlasenkof4c52b32006-12-22 15:03:50 +000013#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
Glenn L McGrathb72a7352002-12-10 00:17:22 +000023
Denis Vlasenkof4c52b32006-12-22 15:03:50 +000024#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)
Glenn L McGrathb72a7352002-12-10 00:17:22 +000051
52enum 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
62typedef struct {
Eric Andersendfcb5b02004-01-30 22:54:20 +000063 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 */
Glenn L McGrathb72a7352002-12-10 00:17:22 +000067} rpm_index;
68
69static void *map;
70static rpm_index **mytags;
71static int tagcount;
72
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +000073static void extract_cpio_gz(int fd);
74static rpm_index **rpm_gettags(int fd, int *num_tags);
75static int bsearch_rpmtag(const void *key, const void *item);
Denis Vlasenkof4c52b32006-12-22 15:03:50 +000076static char *rpm_getstr(int tag, int itemindex);
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +000077static int rpm_getint(int tag, int itemindex);
78static int rpm_getcount(int tag);
79static void fileaction_dobackup(char *filename, int fileref);
80static void fileaction_setowngrp(char *filename, int fileref);
81static void loop_through_files(int filetag, void (*fileaction)(char *filename, int fileref));
Glenn L McGrathb72a7352002-12-10 00:17:22 +000082
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000083int rpm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Glenn L McGrathb72a7352002-12-10 00:17:22 +000084int rpm_main(int argc, char **argv)
85{
86 int opt = 0, func = 0, rpm_fd, offset;
Denis Vlasenkof4c52b32006-12-22 15:03:50 +000087 const int pagesize = getpagesize();
Glenn L McGrathb72a7352002-12-10 00:17:22 +000088
89 while ((opt = getopt(argc, argv, "iqpldc")) != -1) {
90 switch (opt) {
Denis Vlasenkof4c52b32006-12-22 15:03:50 +000091 case 'i': /* First arg: Install mode, with q: Information */
92 if (!func) func = rpm_install;
Glenn L McGrathb72a7352002-12-10 00:17:22 +000093 else func |= rpm_query_info;
94 break;
Denis Vlasenkof4c52b32006-12-22 15:03:50 +000095 case 'q': /* First arg: Query mode */
96 if (func) bb_show_usage();
97 func = rpm_query;
Glenn L McGrathb72a7352002-12-10 00:17:22 +000098 break;
Denis Vlasenkof4c52b32006-12-22 15:03:50 +000099 case 'p': /* Query a package */
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000100 func |= rpm_query_package;
101 break;
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000102 case 'l': /* List files in a package */
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000103 func |= rpm_query_list;
104 break;
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000105 case 'd': /* List doc files in a package (implies list) */
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000106 func |= rpm_query_list;
107 func |= rpm_query_list_doc;
108 break;
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000109 case 'c': /* List config files in a package (implies list) */
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000110 func |= rpm_query_list;
111 func |= rpm_query_list_config;
112 break;
113 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000114 bb_show_usage();
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000115 }
116 }
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000117 argv += optind;
Denis Vlasenko1a9e9bd2008-11-01 12:54:56 +0000118 //argc -= optind;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100119 if (!argv[0]) {
120 bb_show_usage();
121 }
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000122
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000123 while (*argv) {
124 rpm_fd = xopen(*argv++, O_RDONLY);
125 mytags = rpm_gettags(rpm_fd, &tagcount);
126 if (!mytags)
127 bb_error_msg_and_die("error reading rpm header");
128 offset = xlseek(rpm_fd, 0, SEEK_CUR);
129 /* Mimimum is one page */
130 map = mmap(0, offset > pagesize ? (offset + offset % pagesize) : pagesize, PROT_READ, MAP_PRIVATE, rpm_fd, 0);
131
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000132 if (func & rpm_install) {
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000133 /* Backup any config files */
134 loop_through_files(TAG_BASENAMES, fileaction_dobackup);
135 /* Extact the archive */
136 extract_cpio_gz(rpm_fd);
137 /* Set the correct file uid/gid's */
138 loop_through_files(TAG_BASENAMES, fileaction_setowngrp);
139 }
140 else if ((func & (rpm_query|rpm_query_package)) == (rpm_query|rpm_query_package)) {
141 if (!(func & (rpm_query_info|rpm_query_list))) {
142 /* If just a straight query, just give package name */
143 printf("%s-%s-%s\n", rpm_getstr(TAG_NAME, 0), rpm_getstr(TAG_VERSION, 0), rpm_getstr(TAG_RELEASE, 0));
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000144 }
145 if (func & rpm_query_info) {
146 /* Do the nice printout */
147 time_t bdate_time;
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100148 struct tm *bdate_ptm;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000149 char bdatestring[50];
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000150 printf("Name : %-29sRelocations: %s\n", rpm_getstr(TAG_NAME, 0), rpm_getstr(TAG_PREFIXS, 0) ? rpm_getstr(TAG_PREFIXS, 0) : "(not relocateable)");
151 printf("Version : %-34sVendor: %s\n", rpm_getstr(TAG_VERSION, 0), rpm_getstr(TAG_VENDOR, 0) ? rpm_getstr(TAG_VENDOR, 0) : "(none)");
152 bdate_time = rpm_getint(TAG_BUILDTIME, 0);
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100153 bdate_ptm = localtime(&bdate_time);
154 strftime(bdatestring, 50, "%a %d %b %Y %T %Z", bdate_ptm);
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000155 printf("Release : %-30sBuild Date: %s\n", rpm_getstr(TAG_RELEASE, 0), bdatestring);
156 printf("Install date: %-30sBuild Host: %s\n", "(not installed)", rpm_getstr(TAG_BUILDHOST, 0));
157 printf("Group : %-30sSource RPM: %s\n", rpm_getstr(TAG_GROUP, 0), rpm_getstr(TAG_SOURCERPM, 0));
158 printf("Size : %-33dLicense: %s\n", rpm_getint(TAG_SIZE, 0), rpm_getstr(TAG_LICENSE, 0));
159 printf("URL : %s\n", rpm_getstr(TAG_URL, 0));
160 printf("Summary : %s\n", rpm_getstr(TAG_SUMMARY, 0));
161 printf("Description :\n%s\n", rpm_getstr(TAG_DESCRIPTION, 0));
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000162 }
163 if (func & rpm_query_list) {
164 int count, it, flags;
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000165 count = rpm_getcount(TAG_BASENAMES);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000166 for (it = 0; it < count; it++) {
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000167 flags = rpm_getint(TAG_FILEFLAGS, it);
168 switch (func & (rpm_query_list_doc|rpm_query_list_config)) {
169 case rpm_query_list_doc:
170 if (!(flags & RPMFILE_DOC)) continue;
171 break;
172 case rpm_query_list_config:
173 if (!(flags & RPMFILE_CONFIG)) continue;
174 break;
175 case rpm_query_list_doc|rpm_query_list_config:
176 if (!(flags & (RPMFILE_CONFIG|RPMFILE_DOC))) continue;
177 break;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000178 }
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000179 printf("%s%s\n",
180 rpm_getstr(TAG_DIRNAMES, rpm_getint(TAG_DIRINDEXES, it)),
181 rpm_getstr(TAG_BASENAMES, it));
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000182 }
183 }
184 }
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000185 free(mytags);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000186 }
187 return 0;
188}
189
Denis Vlasenkoac678ec2007-04-16 22:32:04 +0000190static void extract_cpio_gz(int fd)
191{
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000192 archive_handle_t *archive_handle;
193 unsigned char magic[2];
Denis Vlasenko211f7f82007-09-05 11:48:32 +0000194#if BB_MMU
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000195 IF_DESKTOP(long long) int FAST_FUNC (*xformer)(int src_fd, int dst_fd);
Denis Vlasenko211f7f82007-09-05 11:48:32 +0000196 enum { xformer_prog = 0 };
197#else
198 enum { xformer = 0 };
199 const char *xformer_prog;
200#endif
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000201
Denis Vlasenkoc14d39e2007-06-08 13:05:39 +0000202 /* Initialize */
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000203 archive_handle = init_handle();
Denis Vlasenko13858992006-10-08 12:49:22 +0000204 archive_handle->seek = seek_by_read;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000205 //archive_handle->action_header = header_list;
206 archive_handle->action_data = data_extract_all;
Denys Vlasenkod57d6262009-09-17 02:43:14 +0200207 archive_handle->ah_flags = ARCHIVE_RESTORE_DATE | ARCHIVE_CREATE_LEADING_DIRS
Denis Vlasenkoaa9eb1f2008-10-16 13:29:13 +0000208 /* compat: overwrite existing files.
209 * try "rpm -i foo.src.rpm" few times in a row -
210 * standard rpm will not complain.
211 * (TODO? real rpm creates "file;1234" and then renames it) */
Denys Vlasenkod57d6262009-09-17 02:43:14 +0200212 | ARCHIVE_UNLINK_OLD;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000213 archive_handle->src_fd = fd;
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000214 /*archive_handle->offset = 0; - init_handle() did it */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000215
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +0000216// TODO: open_zipped does the same
217
Rob Landley53437472006-07-16 08:14:35 +0000218 xread(archive_handle->src_fd, &magic, 2);
Denis Vlasenko211f7f82007-09-05 11:48:32 +0000219#if BB_MMU
Denis Vlasenkoc14d39e2007-06-08 13:05:39 +0000220 xformer = unpack_gz_stream;
Denis Vlasenko211f7f82007-09-05 11:48:32 +0000221#else
222 xformer_prog = "gunzip";
223#endif
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +0000224 if (magic[0] != 0x1f || magic[1] != 0x8b) {
225 if (!ENABLE_FEATURE_SEAMLESS_BZ2
226 || magic[0] != 'B' || magic[1] != 'Z'
227 ) {
Denis Vlasenkoc14d39e2007-06-08 13:05:39 +0000228 bb_error_msg_and_die("no gzip"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000229 IF_FEATURE_SEAMLESS_BZ2("/bzip2")
Denis Vlasenkoc14d39e2007-06-08 13:05:39 +0000230 " magic");
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +0000231 }
232#if BB_MMU
233 xformer = unpack_bz2_stream;
234#else
235 xformer_prog = "bunzip2";
236#endif
Denis Vlasenko211f7f82007-09-05 11:48:32 +0000237 } else {
Denis Vlasenko211f7f82007-09-05 11:48:32 +0000238#if !BB_MMU
239 /* NOMMU version of open_transformer execs an external unzipper that should
240 * have the file position at the start of the file */
241 xlseek(archive_handle->src_fd, 0, SEEK_SET);
242#endif
243 }
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000244
Denis Vlasenkoc14d39e2007-06-08 13:05:39 +0000245 xchdir("/"); /* Install RPM's to root */
Denis Vlasenkob6052722008-07-10 17:43:01 +0000246 open_transformer(archive_handle->src_fd, xformer, xformer_prog);
Glenn L McGrath5699b852003-11-15 23:19:05 +0000247 archive_handle->offset = 0;
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000248 while (get_header_cpio(archive_handle) == EXIT_SUCCESS)
Denis Vlasenkoc14d39e2007-06-08 13:05:39 +0000249 continue;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000250}
251
252
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000253static rpm_index **rpm_gettags(int fd, int *num_tags)
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000254{
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000255 /* We should never need mode than 200, and realloc later */
Denis Vlasenko1a9e9bd2008-11-01 12:54:56 +0000256 rpm_index **tags = xzalloc(200 * sizeof(tags[0]));
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000257 int pass, tagindex = 0;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000258
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000259 xlseek(fd, 96, SEEK_CUR); /* Seek past the unused lead */
260
261 /* 1st pass is the signature headers, 2nd is the main stuff */
262 for (pass = 0; pass < 2; pass++) {
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000263 struct {
264 char magic[3]; /* 3 byte magic: 0x8e 0xad 0xe8 */
Eric Andersendfcb5b02004-01-30 22:54:20 +0000265 uint8_t version; /* 1 byte version number */
266 uint32_t reserved; /* 4 bytes reserved */
267 uint32_t entries; /* Number of entries in header (4 bytes) */
268 uint32_t size; /* Size of store (4 bytes) */
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000269 } header;
Denis Vlasenko1a9e9bd2008-11-01 12:54:56 +0000270 struct BUG_header {
271 char BUG_header[sizeof(header) == 16 ? 1 : -1];
272 };
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000273 rpm_index *tmpindex;
274 int storepos;
275
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000276 xread(fd, &header, sizeof(header));
277 if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC, 3) != 0)
278 return NULL; /* Invalid magic */
279 if (header.version != 1)
280 return NULL; /* This program only supports v1 headers */
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000281 header.size = ntohl(header.size);
282 header.entries = ntohl(header.entries);
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000283 storepos = xlseek(fd,0,SEEK_CUR) + header.entries * 16;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000284
285 while (header.entries--) {
Denis Vlasenko1a9e9bd2008-11-01 12:54:56 +0000286 tmpindex = tags[tagindex++] = xmalloc(sizeof(*tmpindex));
287 xread(fd, tmpindex, sizeof(*tmpindex));
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000288 tmpindex->tag = ntohl(tmpindex->tag);
289 tmpindex->type = ntohl(tmpindex->type);
290 tmpindex->count = ntohl(tmpindex->count);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000291 tmpindex->offset = storepos + ntohl(tmpindex->offset);
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000292 if (pass == 0)
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000293 tmpindex->tag -= 743;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000294 }
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000295 xlseek(fd, header.size, SEEK_CUR); /* Seek past store */
296 /* Skip padding to 8 byte boundary after reading signature headers */
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000297 if (pass == 0)
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000298 xlseek(fd, (8 - (xlseek(fd,0,SEEK_CUR) % 8)) % 8, SEEK_CUR);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000299 }
Denis Vlasenko1a9e9bd2008-11-01 12:54:56 +0000300 tags = xrealloc(tags, tagindex * sizeof(tags[0])); /* realloc tags to save space */
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000301 *num_tags = tagindex;
302 return tags; /* All done, leave the file at the start of the gzipped cpio archive */
303}
304
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000305static int bsearch_rpmtag(const void *key, const void *item)
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000306{
Eric Andersen2cdd4d52006-01-30 18:33:12 +0000307 int *tag = (int *)key;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000308 rpm_index **tmp = (rpm_index **) item;
Eric Andersen2cdd4d52006-01-30 18:33:12 +0000309 return (*tag - tmp[0]->tag);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000310}
311
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000312static int rpm_getcount(int tag)
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000313{
314 rpm_index **found;
Eric Andersen2cdd4d52006-01-30 18:33:12 +0000315 found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000316 if (!found)
317 return 0;
318 return found[0]->count;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000319}
320
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000321static char *rpm_getstr(int tag, int itemindex)
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000322{
323 rpm_index **found;
Eric Andersen2cdd4d52006-01-30 18:33:12 +0000324 found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000325 if (!found || itemindex >= found[0]->count)
326 return NULL;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000327 if (found[0]->type == RPM_STRING_TYPE || found[0]->type == RPM_I18NSTRING_TYPE || found[0]->type == RPM_STRING_ARRAY_TYPE) {
328 int n;
Denys Vlasenko606291b2009-09-23 23:15:43 +0200329 char *tmpstr = (char *) map + found[0]->offset;
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000330 for (n=0; n < itemindex; n++)
331 tmpstr = tmpstr + strlen(tmpstr) + 1;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000332 return tmpstr;
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000333 }
334 return NULL;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000335}
336
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000337static int rpm_getint(int tag, int itemindex)
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000338{
339 rpm_index **found;
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000340 int *tmpint; /* NB: using int8_t* would be easier to code */
341
Mike Frysinger19d70212005-04-23 01:43:07 +0000342 /* gcc throws warnings here when sizeof(void*)!=sizeof(int) ...
343 * it's ok to ignore it because tag won't be used as a pointer */
Eric Andersen2cdd4d52006-01-30 18:33:12 +0000344 found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000345 if (!found || itemindex >= found[0]->count)
346 return -1;
347
Denys Vlasenko606291b2009-09-23 23:15:43 +0200348 tmpint = (int *) ((char *) map + found[0]->offset);
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000349
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000350 if (found[0]->type == RPM_INT32_TYPE) {
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000351 tmpint = (int *) ((char *) tmpint + itemindex*4);
352 /*return ntohl(*tmpint);*/
353 /* int can be != int32_t */
354 return ntohl(*(int32_t*)tmpint);
355 }
356 if (found[0]->type == RPM_INT16_TYPE) {
357 tmpint = (int *) ((char *) tmpint + itemindex*2);
358 /* ??? read int, and THEN ntohs() it?? */
359 /*return ntohs(*tmpint);*/
360 return ntohs(*(int16_t*)tmpint);
361 }
362 if (found[0]->type == RPM_INT8_TYPE) {
363 tmpint = (int *) ((char *) tmpint + itemindex);
364 /* ??? why we don't read byte here??? */
365 /*return ntohs(*tmpint);*/
366 return *(int8_t*)tmpint;
367 }
368 return -1;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000369}
370
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000371static void fileaction_dobackup(char *filename, int fileref)
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000372{
373 struct stat oldfile;
374 int stat_res;
375 char *newname;
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000376 if (rpm_getint(TAG_FILEFLAGS, fileref) & RPMFILE_CONFIG) {
377 /* Only need to backup config files */
378 stat_res = lstat(filename, &oldfile);
379 if (stat_res == 0 && S_ISREG(oldfile.st_mode)) {
380 /* File already exists - really should check MD5's etc to see if different */
Denis Vlasenko9cac5212006-09-09 12:24:19 +0000381 newname = xasprintf("%s.rpmorig", filename);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000382 copy_file(filename, newname, FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS);
383 remove_file(filename, FILEUTILS_RECUR | FILEUTILS_FORCE);
384 free(newname);
385 }
386 }
387}
388
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000389static void fileaction_setowngrp(char *filename, int fileref)
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000390{
Denis Vlasenkoaa9eb1f2008-10-16 13:29:13 +0000391 /* real rpm warns: "user foo does not exist - using <you>" */
392 struct passwd *pw = getpwnam(rpm_getstr(TAG_FILEUSERNAME, fileref));
393 int uid = pw ? pw->pw_uid : getuid(); /* or euid? */
394 struct group *gr = getgrnam(rpm_getstr(TAG_FILEGROUPNAME, fileref));
395 int gid = gr ? gr->gr_gid : getgid();
Denis Vlasenkocf944462006-10-03 19:02:20 +0000396 chown(filename, uid, gid);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000397}
398
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000399static void loop_through_files(int filetag, void (*fileaction)(char *filename, int fileref))
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000400{
401 int count = 0;
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000402 while (rpm_getstr(filetag, count)) {
403 char* filename = xasprintf("%s%s",
404 rpm_getstr(TAG_DIRNAMES, rpm_getint(TAG_DIRINDEXES, count)),
405 rpm_getstr(TAG_BASENAMES, count));
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000406 fileaction(filename, count++);
407 free(filename);
408 }
409}