blob: 41b8c81b22b7c059540e2e527998019c79e54162 [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;
118 argc -= optind;
119 if (!argc) bb_show_usage();
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000120
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000121 while (*argv) {
122 rpm_fd = xopen(*argv++, O_RDONLY);
123 mytags = rpm_gettags(rpm_fd, &tagcount);
124 if (!mytags)
125 bb_error_msg_and_die("error reading rpm header");
126 offset = xlseek(rpm_fd, 0, SEEK_CUR);
127 /* Mimimum is one page */
128 map = mmap(0, offset > pagesize ? (offset + offset % pagesize) : pagesize, PROT_READ, MAP_PRIVATE, rpm_fd, 0);
129
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000130 if (func & rpm_install) {
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000131 /* Backup any config files */
132 loop_through_files(TAG_BASENAMES, fileaction_dobackup);
133 /* Extact the archive */
134 extract_cpio_gz(rpm_fd);
135 /* Set the correct file uid/gid's */
136 loop_through_files(TAG_BASENAMES, fileaction_setowngrp);
137 }
138 else if ((func & (rpm_query|rpm_query_package)) == (rpm_query|rpm_query_package)) {
139 if (!(func & (rpm_query_info|rpm_query_list))) {
140 /* If just a straight query, just give package name */
141 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 +0000142 }
143 if (func & rpm_query_info) {
144 /* Do the nice printout */
145 time_t bdate_time;
146 struct tm *bdate;
147 char bdatestring[50];
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000148 printf("Name : %-29sRelocations: %s\n", rpm_getstr(TAG_NAME, 0), rpm_getstr(TAG_PREFIXS, 0) ? rpm_getstr(TAG_PREFIXS, 0) : "(not relocateable)");
149 printf("Version : %-34sVendor: %s\n", rpm_getstr(TAG_VERSION, 0), rpm_getstr(TAG_VENDOR, 0) ? rpm_getstr(TAG_VENDOR, 0) : "(none)");
150 bdate_time = rpm_getint(TAG_BUILDTIME, 0);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000151 bdate = localtime((time_t *) &bdate_time);
152 strftime(bdatestring, 50, "%a %d %b %Y %T %Z", bdate);
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000153 printf("Release : %-30sBuild Date: %s\n", rpm_getstr(TAG_RELEASE, 0), bdatestring);
154 printf("Install date: %-30sBuild Host: %s\n", "(not installed)", rpm_getstr(TAG_BUILDHOST, 0));
155 printf("Group : %-30sSource RPM: %s\n", rpm_getstr(TAG_GROUP, 0), rpm_getstr(TAG_SOURCERPM, 0));
156 printf("Size : %-33dLicense: %s\n", rpm_getint(TAG_SIZE, 0), rpm_getstr(TAG_LICENSE, 0));
157 printf("URL : %s\n", rpm_getstr(TAG_URL, 0));
158 printf("Summary : %s\n", rpm_getstr(TAG_SUMMARY, 0));
159 printf("Description :\n%s\n", rpm_getstr(TAG_DESCRIPTION, 0));
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000160 }
161 if (func & rpm_query_list) {
162 int count, it, flags;
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000163 count = rpm_getcount(TAG_BASENAMES);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000164 for (it = 0; it < count; it++) {
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000165 flags = rpm_getint(TAG_FILEFLAGS, it);
166 switch (func & (rpm_query_list_doc|rpm_query_list_config)) {
167 case rpm_query_list_doc:
168 if (!(flags & RPMFILE_DOC)) continue;
169 break;
170 case rpm_query_list_config:
171 if (!(flags & RPMFILE_CONFIG)) continue;
172 break;
173 case rpm_query_list_doc|rpm_query_list_config:
174 if (!(flags & (RPMFILE_CONFIG|RPMFILE_DOC))) continue;
175 break;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000176 }
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000177 printf("%s%s\n",
178 rpm_getstr(TAG_DIRNAMES, rpm_getint(TAG_DIRINDEXES, it)),
179 rpm_getstr(TAG_BASENAMES, it));
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000180 }
181 }
182 }
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000183 free(mytags);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000184 }
185 return 0;
186}
187
Denis Vlasenkoac678ec2007-04-16 22:32:04 +0000188static void extract_cpio_gz(int fd)
189{
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000190 archive_handle_t *archive_handle;
191 unsigned char magic[2];
Denis Vlasenko211f7f82007-09-05 11:48:32 +0000192#if BB_MMU
193 USE_DESKTOP(long long) int (*xformer)(int src_fd, int dst_fd);
194 enum { xformer_prog = 0 };
195#else
196 enum { xformer = 0 };
197 const char *xformer_prog;
198#endif
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000199
Denis Vlasenkoc14d39e2007-06-08 13:05:39 +0000200 /* Initialize */
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000201 archive_handle = init_handle();
Denis Vlasenko13858992006-10-08 12:49:22 +0000202 archive_handle->seek = seek_by_read;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000203 //archive_handle->action_header = header_list;
204 archive_handle->action_data = data_extract_all;
205 archive_handle->flags |= ARCHIVE_PRESERVE_DATE;
206 archive_handle->flags |= ARCHIVE_CREATE_LEADING_DIRS;
207 archive_handle->src_fd = fd;
208 archive_handle->offset = 0;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000209
Rob Landley53437472006-07-16 08:14:35 +0000210 xread(archive_handle->src_fd, &magic, 2);
Denis Vlasenko211f7f82007-09-05 11:48:32 +0000211#if BB_MMU
Denis Vlasenkoc14d39e2007-06-08 13:05:39 +0000212 xformer = unpack_gz_stream;
Denis Vlasenko211f7f82007-09-05 11:48:32 +0000213#else
214 xformer_prog = "gunzip";
215#endif
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000216 if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) {
Denis Vlasenkoc14d39e2007-06-08 13:05:39 +0000217 if (ENABLE_FEATURE_RPM_BZ2
218 && (magic[0] == 0x42) && (magic[1] == 0x5a)) {
Denis Vlasenko211f7f82007-09-05 11:48:32 +0000219#if BB_MMU
Denis Vlasenkoc14d39e2007-06-08 13:05:39 +0000220 xformer = unpack_bz2_stream;
Denis Vlasenko211f7f82007-09-05 11:48:32 +0000221#else
222 xformer_prog = "bunzip2";
223#endif
Denis Vlasenkoc14d39e2007-06-08 13:05:39 +0000224 /* We can do better, need modifying unpack_bz2_stream to not require
225 * first 2 bytes. Not very hard to do... I mean, TODO :) */
226 xlseek(archive_handle->src_fd, -2, SEEK_CUR);
227 } else
228 bb_error_msg_and_die("no gzip"
229 USE_FEATURE_RPM_BZ2("/bzip")
230 " magic");
Denis Vlasenko211f7f82007-09-05 11:48:32 +0000231 } else {
Denis Vlasenko211f7f82007-09-05 11:48:32 +0000232#if !BB_MMU
233 /* NOMMU version of open_transformer execs an external unzipper that should
234 * have the file position at the start of the file */
235 xlseek(archive_handle->src_fd, 0, SEEK_SET);
236#endif
237 }
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000238
Denis Vlasenkoc14d39e2007-06-08 13:05:39 +0000239 xchdir("/"); /* Install RPM's to root */
Denis Vlasenko059c9172007-11-12 02:13:12 +0000240 archive_handle->src_fd = open_transformer(archive_handle->src_fd, xformer, xformer_prog);
Glenn L McGrath5699b852003-11-15 23:19:05 +0000241 archive_handle->offset = 0;
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000242 while (get_header_cpio(archive_handle) == EXIT_SUCCESS)
Denis Vlasenkoc14d39e2007-06-08 13:05:39 +0000243 continue;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000244}
245
246
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000247static rpm_index **rpm_gettags(int fd, int *num_tags)
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000248{
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000249 /* We should never need mode than 200, and realloc later */
250 rpm_index **tags = xzalloc(200 * sizeof(struct rpmtag *));
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000251 int pass, tagindex = 0;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000252
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000253 xlseek(fd, 96, SEEK_CUR); /* Seek past the unused lead */
254
255 /* 1st pass is the signature headers, 2nd is the main stuff */
256 for (pass = 0; pass < 2; pass++) {
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000257 struct {
258 char magic[3]; /* 3 byte magic: 0x8e 0xad 0xe8 */
Eric Andersendfcb5b02004-01-30 22:54:20 +0000259 uint8_t version; /* 1 byte version number */
260 uint32_t reserved; /* 4 bytes reserved */
261 uint32_t entries; /* Number of entries in header (4 bytes) */
262 uint32_t size; /* Size of store (4 bytes) */
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000263 } header;
264 rpm_index *tmpindex;
265 int storepos;
266
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000267 xread(fd, &header, sizeof(header));
268 if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC, 3) != 0)
269 return NULL; /* Invalid magic */
270 if (header.version != 1)
271 return NULL; /* This program only supports v1 headers */
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000272 header.size = ntohl(header.size);
273 header.entries = ntohl(header.entries);
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000274 storepos = xlseek(fd,0,SEEK_CUR) + header.entries * 16;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000275
276 while (header.entries--) {
Mike Frysinger1eef0c42005-08-16 05:32:42 +0000277 tmpindex = tags[tagindex++] = xmalloc(sizeof(rpm_index));
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000278 xread(fd, tmpindex, sizeof(rpm_index));
279 tmpindex->tag = ntohl(tmpindex->tag);
280 tmpindex->type = ntohl(tmpindex->type);
281 tmpindex->count = ntohl(tmpindex->count);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000282 tmpindex->offset = storepos + ntohl(tmpindex->offset);
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000283 if (pass==0)
284 tmpindex->tag -= 743;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000285 }
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000286 xlseek(fd, header.size, SEEK_CUR); /* Seek past store */
287 /* Skip padding to 8 byte boundary after reading signature headers */
288 if (pass==0)
289 xlseek(fd, (8 - (xlseek(fd,0,SEEK_CUR) % 8)) % 8, SEEK_CUR);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000290 }
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000291 tags = xrealloc(tags, tagindex * sizeof(struct rpmtag *)); /* realloc tags to save space */
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000292 *num_tags = tagindex;
293 return tags; /* All done, leave the file at the start of the gzipped cpio archive */
294}
295
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000296static int bsearch_rpmtag(const void *key, const void *item)
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000297{
Eric Andersen2cdd4d52006-01-30 18:33:12 +0000298 int *tag = (int *)key;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000299 rpm_index **tmp = (rpm_index **) item;
Eric Andersen2cdd4d52006-01-30 18:33:12 +0000300 return (*tag - tmp[0]->tag);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000301}
302
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000303static int rpm_getcount(int tag)
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000304{
305 rpm_index **found;
Eric Andersen2cdd4d52006-01-30 18:33:12 +0000306 found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000307 if (!found)
308 return 0;
309 return found[0]->count;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000310}
311
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000312static char *rpm_getstr(int tag, int itemindex)
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 || itemindex >= found[0]->count)
317 return NULL;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000318 if (found[0]->type == RPM_STRING_TYPE || found[0]->type == RPM_I18NSTRING_TYPE || found[0]->type == RPM_STRING_ARRAY_TYPE) {
319 int n;
320 char *tmpstr = (char *) (map + found[0]->offset);
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000321 for (n=0; n < itemindex; n++)
322 tmpstr = tmpstr + strlen(tmpstr) + 1;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000323 return tmpstr;
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000324 }
325 return NULL;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000326}
327
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000328static int rpm_getint(int tag, int itemindex)
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000329{
330 rpm_index **found;
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000331 int *tmpint; /* NB: using int8_t* would be easier to code */
332
Mike Frysinger19d70212005-04-23 01:43:07 +0000333 /* gcc throws warnings here when sizeof(void*)!=sizeof(int) ...
334 * it's ok to ignore it because tag won't be used as a pointer */
Eric Andersen2cdd4d52006-01-30 18:33:12 +0000335 found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000336 if (!found || itemindex >= found[0]->count)
337 return -1;
338
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000339 tmpint = (int *) (map + found[0]->offset);
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000340
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000341 if (found[0]->type == RPM_INT32_TYPE) {
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000342 tmpint = (int *) ((char *) tmpint + itemindex*4);
343 /*return ntohl(*tmpint);*/
344 /* int can be != int32_t */
345 return ntohl(*(int32_t*)tmpint);
346 }
347 if (found[0]->type == RPM_INT16_TYPE) {
348 tmpint = (int *) ((char *) tmpint + itemindex*2);
349 /* ??? read int, and THEN ntohs() it?? */
350 /*return ntohs(*tmpint);*/
351 return ntohs(*(int16_t*)tmpint);
352 }
353 if (found[0]->type == RPM_INT8_TYPE) {
354 tmpint = (int *) ((char *) tmpint + itemindex);
355 /* ??? why we don't read byte here??? */
356 /*return ntohs(*tmpint);*/
357 return *(int8_t*)tmpint;
358 }
359 return -1;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000360}
361
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000362static void fileaction_dobackup(char *filename, int fileref)
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000363{
364 struct stat oldfile;
365 int stat_res;
366 char *newname;
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000367 if (rpm_getint(TAG_FILEFLAGS, fileref) & RPMFILE_CONFIG) {
368 /* Only need to backup config files */
369 stat_res = lstat(filename, &oldfile);
370 if (stat_res == 0 && S_ISREG(oldfile.st_mode)) {
371 /* File already exists - really should check MD5's etc to see if different */
Denis Vlasenko9cac5212006-09-09 12:24:19 +0000372 newname = xasprintf("%s.rpmorig", filename);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000373 copy_file(filename, newname, FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS);
374 remove_file(filename, FILEUTILS_RECUR | FILEUTILS_FORCE);
375 free(newname);
376 }
377 }
378}
379
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000380static void fileaction_setowngrp(char *filename, int fileref)
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000381{
382 int uid, gid;
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +0000383 uid = xuname2uid(rpm_getstr(TAG_FILEUSERNAME, fileref));
384 gid = xgroup2gid(rpm_getstr(TAG_FILEGROUPNAME, fileref));
Denis Vlasenkocf944462006-10-03 19:02:20 +0000385 chown(filename, uid, gid);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000386}
387
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000388static void loop_through_files(int filetag, void (*fileaction)(char *filename, int fileref))
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000389{
390 int count = 0;
Denis Vlasenkof4c52b32006-12-22 15:03:50 +0000391 while (rpm_getstr(filetag, count)) {
392 char* filename = xasprintf("%s%s",
393 rpm_getstr(TAG_DIRNAMES, rpm_getint(TAG_DIRINDEXES, count)),
394 rpm_getstr(TAG_BASENAMES, count));
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000395 fileaction(filename, count++);
396 free(filename);
397 }
398}