blob: 38ec20ef5d9d61284c929ea1693bd319244fde88 [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"
Denys Vlasenko27653ad2010-05-06 14:19:19 +000012#include "rpm.h"
Glenn L McGrath38386d72002-12-10 02:09:12 +000013
Denis Vlasenkof4c52b32006-12-22 15:03:50 +000014#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
Denys Vlasenko27653ad2010-05-06 14:19:19 +000049
Denis Vlasenkof4c52b32006-12-22 15:03:50 +000050#define RPMFILE_CONFIG (1 << 0)
51#define RPMFILE_DOC (1 << 1)
Glenn L McGrathb72a7352002-12-10 00:17:22 +000052
53enum rpm_functions_e {
54 rpm_query = 1,
55 rpm_install = 2,
56 rpm_query_info = 4,
57 rpm_query_package = 8,
58 rpm_query_list = 16,
59 rpm_query_list_doc = 32,
60 rpm_query_list_config = 64
61};
62
63typedef struct {
Eric Andersendfcb5b02004-01-30 22:54:20 +000064 uint32_t tag; /* 4 byte tag */
65 uint32_t type; /* 4 byte type */
66 uint32_t offset; /* 4 byte offset */
67 uint32_t count; /* 4 byte count */
Glenn L McGrathb72a7352002-12-10 00:17:22 +000068} rpm_index;
69
70static void *map;
71static rpm_index **mytags;
72static int tagcount;
73
Denys Vlasenko5dfd9c42010-05-06 16:56:38 +020074static void extract_cpio(int fd, const char *source_rpm);
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +000075static rpm_index **rpm_gettags(int fd, int *num_tags);
76static int bsearch_rpmtag(const void *key, const void *item);
Denis Vlasenkof4c52b32006-12-22 15:03:50 +000077static char *rpm_getstr(int tag, int itemindex);
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +000078static int rpm_getint(int tag, int itemindex);
79static int rpm_getcount(int tag);
80static void fileaction_dobackup(char *filename, int fileref);
81static void fileaction_setowngrp(char *filename, int fileref);
82static void loop_through_files(int filetag, void (*fileaction)(char *filename, int fileref));
Glenn L McGrathb72a7352002-12-10 00:17:22 +000083
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000084int rpm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Glenn L McGrathb72a7352002-12-10 00:17:22 +000085int rpm_main(int argc, char **argv)
86{
87 int opt = 0, func = 0, rpm_fd, offset;
Denis Vlasenkof4c52b32006-12-22 15:03:50 +000088 const int pagesize = getpagesize();
Glenn L McGrathb72a7352002-12-10 00:17:22 +000089
90 while ((opt = getopt(argc, argv, "iqpldc")) != -1) {
91 switch (opt) {
Denis Vlasenkof4c52b32006-12-22 15:03:50 +000092 case 'i': /* First arg: Install mode, with q: Information */
93 if (!func) func = rpm_install;
Glenn L McGrathb72a7352002-12-10 00:17:22 +000094 else func |= rpm_query_info;
95 break;
Denis Vlasenkof4c52b32006-12-22 15:03:50 +000096 case 'q': /* First arg: Query mode */
97 if (func) bb_show_usage();
98 func = rpm_query;
Glenn L McGrathb72a7352002-12-10 00:17:22 +000099 break;
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000100 case 'p': /* Query a package */
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000101 func |= rpm_query_package;
102 break;
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000103 case 'l': /* List files in a package */
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000104 func |= rpm_query_list;
105 break;
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000106 case 'd': /* List doc files in a package (implies list) */
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000107 func |= rpm_query_list;
108 func |= rpm_query_list_doc;
109 break;
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000110 case 'c': /* List config files in a package (implies list) */
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000111 func |= rpm_query_list;
112 func |= rpm_query_list_config;
113 break;
114 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000115 bb_show_usage();
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000116 }
117 }
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000118 argv += optind;
Denis Vlasenko1a9e9bd2008-11-01 12:54:56 +0000119 //argc -= optind;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100120 if (!argv[0]) {
121 bb_show_usage();
122 }
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000123
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000124 while (*argv) {
Denys Vlasenko5dfd9c42010-05-06 16:56:38 +0200125 const char *source_rpm;
126
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000127 rpm_fd = xopen(*argv++, O_RDONLY);
128 mytags = rpm_gettags(rpm_fd, &tagcount);
129 if (!mytags)
130 bb_error_msg_and_die("error reading rpm header");
131 offset = xlseek(rpm_fd, 0, SEEK_CUR);
132 /* Mimimum is one page */
133 map = mmap(0, offset > pagesize ? (offset + offset % pagesize) : pagesize, PROT_READ, MAP_PRIVATE, rpm_fd, 0);
134
Denys Vlasenko5dfd9c42010-05-06 16:56:38 +0200135 source_rpm = rpm_getstr(TAG_SOURCERPM, 0);
136
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000137 if (func & rpm_install) {
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000138 /* Backup any config files */
139 loop_through_files(TAG_BASENAMES, fileaction_dobackup);
140 /* Extact the archive */
Denys Vlasenko5dfd9c42010-05-06 16:56:38 +0200141 extract_cpio(rpm_fd, source_rpm);
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000142 /* Set the correct file uid/gid's */
143 loop_through_files(TAG_BASENAMES, fileaction_setowngrp);
144 }
145 else if ((func & (rpm_query|rpm_query_package)) == (rpm_query|rpm_query_package)) {
146 if (!(func & (rpm_query_info|rpm_query_list))) {
147 /* If just a straight query, just give package name */
148 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 +0000149 }
150 if (func & rpm_query_info) {
151 /* Do the nice printout */
152 time_t bdate_time;
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100153 struct tm *bdate_ptm;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000154 char bdatestring[50];
Denys Vlasenko27653ad2010-05-06 14:19:19 +0000155 const char *p;
156
157 p = rpm_getstr(TAG_PREFIXS, 0);
158 if (!p) p = "(not relocateable)";
159 printf("Name : %-29sRelocations: %s\n", rpm_getstr(TAG_NAME, 0), p);
160 p = rpm_getstr(TAG_VENDOR, 0);
161 if (!p) p = "(none)";
162 printf("Version : %-34sVendor: %s\n", rpm_getstr(TAG_VERSION, 0), p);
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000163 bdate_time = rpm_getint(TAG_BUILDTIME, 0);
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100164 bdate_ptm = localtime(&bdate_time);
165 strftime(bdatestring, 50, "%a %d %b %Y %T %Z", bdate_ptm);
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000166 printf("Release : %-30sBuild Date: %s\n", rpm_getstr(TAG_RELEASE, 0), bdatestring);
167 printf("Install date: %-30sBuild Host: %s\n", "(not installed)", rpm_getstr(TAG_BUILDHOST, 0));
Denys Vlasenko5dfd9c42010-05-06 16:56:38 +0200168 printf("Group : %-30sSource RPM: %s\n", rpm_getstr(TAG_GROUP, 0), source_rpm);
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000169 printf("Size : %-33dLicense: %s\n", rpm_getint(TAG_SIZE, 0), rpm_getstr(TAG_LICENSE, 0));
170 printf("URL : %s\n", rpm_getstr(TAG_URL, 0));
171 printf("Summary : %s\n", rpm_getstr(TAG_SUMMARY, 0));
172 printf("Description :\n%s\n", rpm_getstr(TAG_DESCRIPTION, 0));
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000173 }
174 if (func & rpm_query_list) {
175 int count, it, flags;
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000176 count = rpm_getcount(TAG_BASENAMES);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000177 for (it = 0; it < count; it++) {
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000178 flags = rpm_getint(TAG_FILEFLAGS, it);
179 switch (func & (rpm_query_list_doc|rpm_query_list_config)) {
180 case rpm_query_list_doc:
181 if (!(flags & RPMFILE_DOC)) continue;
182 break;
183 case rpm_query_list_config:
184 if (!(flags & RPMFILE_CONFIG)) continue;
185 break;
186 case rpm_query_list_doc|rpm_query_list_config:
187 if (!(flags & (RPMFILE_CONFIG|RPMFILE_DOC))) continue;
188 break;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000189 }
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000190 printf("%s%s\n",
191 rpm_getstr(TAG_DIRNAMES, rpm_getint(TAG_DIRINDEXES, it)),
192 rpm_getstr(TAG_BASENAMES, it));
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000193 }
194 }
195 }
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000196 free(mytags);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000197 }
198 return 0;
199}
200
Denys Vlasenko5dfd9c42010-05-06 16:56:38 +0200201static void extract_cpio(int fd, const char *source_rpm)
Denis Vlasenkoac678ec2007-04-16 22:32:04 +0000202{
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000203 archive_handle_t *archive_handle;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000204
Denys Vlasenko5dfd9c42010-05-06 16:56:38 +0200205 if (source_rpm != NULL) {
206 /* Binary rpm (it was built from some SRPM), install to root */
207 xchdir("/");
208 } /* else: SRPM, install to current dir */
209
Denis Vlasenkoc14d39e2007-06-08 13:05:39 +0000210 /* Initialize */
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000211 archive_handle = init_handle();
Denis Vlasenko13858992006-10-08 12:49:22 +0000212 archive_handle->seek = seek_by_read;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000213 archive_handle->action_data = data_extract_all;
Denys Vlasenko27653ad2010-05-06 14:19:19 +0000214#if 0 /* For testing (rpm -i only lists the files in internal cpio): */
215 archive_handle->action_header = header_list;
216 archive_handle->action_data = data_skip;
217#endif
Denys Vlasenkod57d6262009-09-17 02:43:14 +0200218 archive_handle->ah_flags = ARCHIVE_RESTORE_DATE | ARCHIVE_CREATE_LEADING_DIRS
Denis Vlasenkoaa9eb1f2008-10-16 13:29:13 +0000219 /* compat: overwrite existing files.
220 * try "rpm -i foo.src.rpm" few times in a row -
221 * standard rpm will not complain.
222 * (TODO? real rpm creates "file;1234" and then renames it) */
Denys Vlasenkod57d6262009-09-17 02:43:14 +0200223 | ARCHIVE_UNLINK_OLD;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000224 archive_handle->src_fd = fd;
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000225 /*archive_handle->offset = 0; - init_handle() did it */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000226
Denys Vlasenko27653ad2010-05-06 14:19:19 +0000227 setup_unzip_on_fd(archive_handle->src_fd /*, fail_if_not_detected: 1*/);
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000228 while (get_header_cpio(archive_handle) == EXIT_SUCCESS)
Denis Vlasenkoc14d39e2007-06-08 13:05:39 +0000229 continue;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000230}
231
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000232static rpm_index **rpm_gettags(int fd, int *num_tags)
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000233{
Denys Vlasenko27653ad2010-05-06 14:19:19 +0000234 /* We should never need more than 200 (shrink via realloc later) */
Denis Vlasenko1a9e9bd2008-11-01 12:54:56 +0000235 rpm_index **tags = xzalloc(200 * sizeof(tags[0]));
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000236 int pass, tagindex = 0;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000237
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000238 xlseek(fd, 96, SEEK_CUR); /* Seek past the unused lead */
239
240 /* 1st pass is the signature headers, 2nd is the main stuff */
241 for (pass = 0; pass < 2; pass++) {
Denys Vlasenko27653ad2010-05-06 14:19:19 +0000242 struct rpm_header header;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000243 rpm_index *tmpindex;
244 int storepos;
245
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000246 xread(fd, &header, sizeof(header));
Denys Vlasenko27653ad2010-05-06 14:19:19 +0000247 if (header.magic_and_ver != htonl(RPM_HEADER_MAGICnVER))
248 return NULL; /* Invalid magic, or not version 1 */
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000249 header.size = ntohl(header.size);
250 header.entries = ntohl(header.entries);
Denys Vlasenko27653ad2010-05-06 14:19:19 +0000251 storepos = xlseek(fd, 0, SEEK_CUR) + header.entries * 16;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000252
253 while (header.entries--) {
Denis Vlasenko1a9e9bd2008-11-01 12:54:56 +0000254 tmpindex = tags[tagindex++] = xmalloc(sizeof(*tmpindex));
255 xread(fd, tmpindex, sizeof(*tmpindex));
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000256 tmpindex->tag = ntohl(tmpindex->tag);
257 tmpindex->type = ntohl(tmpindex->type);
258 tmpindex->count = ntohl(tmpindex->count);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000259 tmpindex->offset = storepos + ntohl(tmpindex->offset);
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000260 if (pass == 0)
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000261 tmpindex->tag -= 743;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000262 }
Denys Vlasenko27653ad2010-05-06 14:19:19 +0000263 storepos = xlseek(fd, header.size, SEEK_CUR); /* Seek past store */
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000264 /* Skip padding to 8 byte boundary after reading signature headers */
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000265 if (pass == 0)
Denys Vlasenko27653ad2010-05-06 14:19:19 +0000266 xlseek(fd, (-storepos) & 0x7, SEEK_CUR);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000267 }
Denys Vlasenko27653ad2010-05-06 14:19:19 +0000268 /* realloc tags to save space */
269 tags = xrealloc(tags, tagindex * sizeof(tags[0]));
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000270 *num_tags = tagindex;
Denys Vlasenko27653ad2010-05-06 14:19:19 +0000271 /* All done, leave the file at the start of the gzipped cpio archive */
272 return tags;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000273}
274
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000275static int bsearch_rpmtag(const void *key, const void *item)
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000276{
Eric Andersen2cdd4d52006-01-30 18:33:12 +0000277 int *tag = (int *)key;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000278 rpm_index **tmp = (rpm_index **) item;
Eric Andersen2cdd4d52006-01-30 18:33:12 +0000279 return (*tag - tmp[0]->tag);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000280}
281
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000282static int rpm_getcount(int tag)
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000283{
284 rpm_index **found;
Eric Andersen2cdd4d52006-01-30 18:33:12 +0000285 found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000286 if (!found)
287 return 0;
288 return found[0]->count;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000289}
290
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000291static char *rpm_getstr(int tag, int itemindex)
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000292{
293 rpm_index **found;
Eric Andersen2cdd4d52006-01-30 18:33:12 +0000294 found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000295 if (!found || itemindex >= found[0]->count)
296 return NULL;
Denys Vlasenko27653ad2010-05-06 14:19:19 +0000297 if (found[0]->type == RPM_STRING_TYPE
298 || found[0]->type == RPM_I18NSTRING_TYPE
299 || found[0]->type == RPM_STRING_ARRAY_TYPE
300 ) {
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000301 int n;
Denys Vlasenko606291b2009-09-23 23:15:43 +0200302 char *tmpstr = (char *) map + found[0]->offset;
Denys Vlasenko27653ad2010-05-06 14:19:19 +0000303 for (n = 0; n < itemindex; n++)
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000304 tmpstr = tmpstr + strlen(tmpstr) + 1;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000305 return tmpstr;
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000306 }
307 return NULL;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000308}
309
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000310static int rpm_getint(int tag, int itemindex)
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000311{
312 rpm_index **found;
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000313 int *tmpint; /* NB: using int8_t* would be easier to code */
314
Mike Frysinger19d70212005-04-23 01:43:07 +0000315 /* gcc throws warnings here when sizeof(void*)!=sizeof(int) ...
316 * it's ok to ignore it because tag won't be used as a pointer */
Eric Andersen2cdd4d52006-01-30 18:33:12 +0000317 found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000318 if (!found || itemindex >= found[0]->count)
319 return -1;
320
Denys Vlasenko606291b2009-09-23 23:15:43 +0200321 tmpint = (int *) ((char *) map + found[0]->offset);
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000322
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000323 if (found[0]->type == RPM_INT32_TYPE) {
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000324 tmpint = (int *) ((char *) tmpint + itemindex*4);
325 /*return ntohl(*tmpint);*/
326 /* int can be != int32_t */
327 return ntohl(*(int32_t*)tmpint);
328 }
329 if (found[0]->type == RPM_INT16_TYPE) {
330 tmpint = (int *) ((char *) tmpint + itemindex*2);
331 /* ??? read int, and THEN ntohs() it?? */
332 /*return ntohs(*tmpint);*/
333 return ntohs(*(int16_t*)tmpint);
334 }
335 if (found[0]->type == RPM_INT8_TYPE) {
336 tmpint = (int *) ((char *) tmpint + itemindex);
337 /* ??? why we don't read byte here??? */
338 /*return ntohs(*tmpint);*/
339 return *(int8_t*)tmpint;
340 }
341 return -1;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000342}
343
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000344static void fileaction_dobackup(char *filename, int fileref)
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000345{
346 struct stat oldfile;
347 int stat_res;
348 char *newname;
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000349 if (rpm_getint(TAG_FILEFLAGS, fileref) & RPMFILE_CONFIG) {
350 /* Only need to backup config files */
351 stat_res = lstat(filename, &oldfile);
352 if (stat_res == 0 && S_ISREG(oldfile.st_mode)) {
353 /* File already exists - really should check MD5's etc to see if different */
Denis Vlasenko9cac5212006-09-09 12:24:19 +0000354 newname = xasprintf("%s.rpmorig", filename);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000355 copy_file(filename, newname, FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS);
356 remove_file(filename, FILEUTILS_RECUR | FILEUTILS_FORCE);
357 free(newname);
358 }
359 }
360}
361
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000362static void fileaction_setowngrp(char *filename, int fileref)
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000363{
Denis Vlasenkoaa9eb1f2008-10-16 13:29:13 +0000364 /* real rpm warns: "user foo does not exist - using <you>" */
365 struct passwd *pw = getpwnam(rpm_getstr(TAG_FILEUSERNAME, fileref));
366 int uid = pw ? pw->pw_uid : getuid(); /* or euid? */
367 struct group *gr = getgrnam(rpm_getstr(TAG_FILEGROUPNAME, fileref));
368 int gid = gr ? gr->gr_gid : getgid();
Denis Vlasenkocf944462006-10-03 19:02:20 +0000369 chown(filename, uid, gid);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000370}
371
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000372static void loop_through_files(int filetag, void (*fileaction)(char *filename, int fileref))
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000373{
374 int count = 0;
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000375 while (rpm_getstr(filetag, count)) {
376 char* filename = xasprintf("%s%s",
377 rpm_getstr(TAG_DIRNAMES, rpm_getint(TAG_DIRINDEXES, count)),
378 rpm_getstr(TAG_BASENAMES, count));
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000379 fileaction(filename, count++);
380 free(filename);
381 }
382}