Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Mini rpm applet for busybox |
| 4 | * |
| 5 | * Copyright (C) 2001,2002 by Laurence Anderson |
| 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Pere Orga | 1f4447b | 2011-03-27 22:40:30 +0200 | [diff] [blame] | 10 | //usage:#define rpm_trivial_usage |
| 11 | //usage: "-i PACKAGE.rpm; rpm -qp[ildc] PACKAGE.rpm" |
| 12 | //usage:#define rpm_full_usage "\n\n" |
| 13 | //usage: "Manipulate RPM packages\n" |
| 14 | //usage: "\nCommands:" |
| 15 | //usage: "\n -i Install package" |
| 16 | //usage: "\n -qp Query package" |
Denys Vlasenko | 10f5f9b | 2013-02-20 15:57:39 +0100 | [diff] [blame] | 17 | //usage: "\n -qpi Show information" |
| 18 | //usage: "\n -qpl List contents" |
| 19 | //usage: "\n -qpd List documents" |
| 20 | //usage: "\n -qpc List config files" |
Pere Orga | 1f4447b | 2011-03-27 22:40:30 +0200 | [diff] [blame] | 21 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 22 | #include "libbb.h" |
Denys Vlasenko | d184a72 | 2011-09-22 12:45:14 +0200 | [diff] [blame] | 23 | #include "bb_archive.h" |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 24 | #include "rpm.h" |
Glenn L McGrath | 38386d7 | 2002-12-10 02:09:12 +0000 | [diff] [blame] | 25 | |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 26 | #define RPM_CHAR_TYPE 1 |
| 27 | #define RPM_INT8_TYPE 2 |
| 28 | #define RPM_INT16_TYPE 3 |
| 29 | #define RPM_INT32_TYPE 4 |
| 30 | /* #define RPM_INT64_TYPE 5 ---- These aren't supported (yet) */ |
| 31 | #define RPM_STRING_TYPE 6 |
| 32 | #define RPM_BIN_TYPE 7 |
| 33 | #define RPM_STRING_ARRAY_TYPE 8 |
| 34 | #define RPM_I18NSTRING_TYPE 9 |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 35 | |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 36 | #define TAG_NAME 1000 |
| 37 | #define TAG_VERSION 1001 |
| 38 | #define TAG_RELEASE 1002 |
| 39 | #define TAG_SUMMARY 1004 |
| 40 | #define TAG_DESCRIPTION 1005 |
| 41 | #define TAG_BUILDTIME 1006 |
| 42 | #define TAG_BUILDHOST 1007 |
| 43 | #define TAG_SIZE 1009 |
| 44 | #define TAG_VENDOR 1011 |
| 45 | #define TAG_LICENSE 1014 |
| 46 | #define TAG_PACKAGER 1015 |
| 47 | #define TAG_GROUP 1016 |
| 48 | #define TAG_URL 1020 |
| 49 | #define TAG_PREIN 1023 |
| 50 | #define TAG_POSTIN 1024 |
| 51 | #define TAG_FILEFLAGS 1037 |
| 52 | #define TAG_FILEUSERNAME 1039 |
| 53 | #define TAG_FILEGROUPNAME 1040 |
| 54 | #define TAG_SOURCERPM 1044 |
| 55 | #define TAG_PREINPROG 1085 |
| 56 | #define TAG_POSTINPROG 1086 |
| 57 | #define TAG_PREFIXS 1098 |
| 58 | #define TAG_DIRINDEXES 1116 |
| 59 | #define TAG_BASENAMES 1117 |
| 60 | #define TAG_DIRNAMES 1118 |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 61 | |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 62 | #define RPMFILE_CONFIG (1 << 0) |
| 63 | #define RPMFILE_DOC (1 << 1) |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 64 | |
| 65 | enum rpm_functions_e { |
| 66 | rpm_query = 1, |
| 67 | rpm_install = 2, |
| 68 | rpm_query_info = 4, |
| 69 | rpm_query_package = 8, |
| 70 | rpm_query_list = 16, |
| 71 | rpm_query_list_doc = 32, |
| 72 | rpm_query_list_config = 64 |
| 73 | }; |
| 74 | |
| 75 | typedef struct { |
Eric Andersen | dfcb5b0 | 2004-01-30 22:54:20 +0000 | [diff] [blame] | 76 | uint32_t tag; /* 4 byte tag */ |
| 77 | uint32_t type; /* 4 byte type */ |
| 78 | uint32_t offset; /* 4 byte offset */ |
| 79 | uint32_t count; /* 4 byte count */ |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 80 | } rpm_index; |
| 81 | |
Denys Vlasenko | d4d4f35 | 2013-02-20 16:01:10 +0100 | [diff] [blame] | 82 | struct globals { |
| 83 | void *map; |
| 84 | rpm_index **mytags; |
| 85 | int tagcount; |
| 86 | } FIX_ALIASING; |
| 87 | #define G (*(struct globals*)&bb_common_bufsiz1) |
| 88 | #define INIT_G() do { } while (0) |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 89 | |
Denys Vlasenko | 5dfd9c4 | 2010-05-06 16:56:38 +0200 | [diff] [blame] | 90 | static void extract_cpio(int fd, const char *source_rpm) |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 91 | { |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 92 | archive_handle_t *archive_handle; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 93 | |
Denys Vlasenko | 5dfd9c4 | 2010-05-06 16:56:38 +0200 | [diff] [blame] | 94 | if (source_rpm != NULL) { |
| 95 | /* Binary rpm (it was built from some SRPM), install to root */ |
| 96 | xchdir("/"); |
| 97 | } /* else: SRPM, install to current dir */ |
| 98 | |
Denis Vlasenko | c14d39e | 2007-06-08 13:05:39 +0000 | [diff] [blame] | 99 | /* Initialize */ |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 100 | archive_handle = init_handle(); |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 101 | archive_handle->seek = seek_by_read; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 102 | archive_handle->action_data = data_extract_all; |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 103 | #if 0 /* For testing (rpm -i only lists the files in internal cpio): */ |
| 104 | archive_handle->action_header = header_list; |
| 105 | archive_handle->action_data = data_skip; |
| 106 | #endif |
Denys Vlasenko | d57d626 | 2009-09-17 02:43:14 +0200 | [diff] [blame] | 107 | archive_handle->ah_flags = ARCHIVE_RESTORE_DATE | ARCHIVE_CREATE_LEADING_DIRS |
Denis Vlasenko | aa9eb1f | 2008-10-16 13:29:13 +0000 | [diff] [blame] | 108 | /* compat: overwrite existing files. |
| 109 | * try "rpm -i foo.src.rpm" few times in a row - |
| 110 | * standard rpm will not complain. |
Denys Vlasenko | 2aec773 | 2013-02-20 15:58:42 +0100 | [diff] [blame] | 111 | */ |
| 112 | | ARCHIVE_REPLACE_VIA_RENAME; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 113 | archive_handle->src_fd = fd; |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 114 | /*archive_handle->offset = 0; - init_handle() did it */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 115 | |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 116 | setup_unzip_on_fd(archive_handle->src_fd, /*fail_if_not_detected:*/ 1); |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 117 | while (get_header_cpio(archive_handle) == EXIT_SUCCESS) |
Denis Vlasenko | c14d39e | 2007-06-08 13:05:39 +0000 | [diff] [blame] | 118 | continue; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 121 | static rpm_index **rpm_gettags(int fd, int *num_tags) |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 122 | { |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 123 | /* We should never need more than 200 (shrink via realloc later) */ |
Denis Vlasenko | 1a9e9bd | 2008-11-01 12:54:56 +0000 | [diff] [blame] | 124 | rpm_index **tags = xzalloc(200 * sizeof(tags[0])); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 125 | int pass, tagindex = 0; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 126 | |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 127 | xlseek(fd, 96, SEEK_CUR); /* Seek past the unused lead */ |
| 128 | |
| 129 | /* 1st pass is the signature headers, 2nd is the main stuff */ |
| 130 | for (pass = 0; pass < 2; pass++) { |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 131 | struct rpm_header header; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 132 | rpm_index *tmpindex; |
| 133 | int storepos; |
| 134 | |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 135 | xread(fd, &header, sizeof(header)); |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 136 | if (header.magic_and_ver != htonl(RPM_HEADER_MAGICnVER)) |
| 137 | return NULL; /* Invalid magic, or not version 1 */ |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 138 | header.size = ntohl(header.size); |
| 139 | header.entries = ntohl(header.entries); |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 140 | storepos = xlseek(fd, 0, SEEK_CUR) + header.entries * 16; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 141 | |
| 142 | while (header.entries--) { |
Denis Vlasenko | 1a9e9bd | 2008-11-01 12:54:56 +0000 | [diff] [blame] | 143 | tmpindex = tags[tagindex++] = xmalloc(sizeof(*tmpindex)); |
| 144 | xread(fd, tmpindex, sizeof(*tmpindex)); |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 145 | tmpindex->tag = ntohl(tmpindex->tag); |
| 146 | tmpindex->type = ntohl(tmpindex->type); |
| 147 | tmpindex->count = ntohl(tmpindex->count); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 148 | tmpindex->offset = storepos + ntohl(tmpindex->offset); |
Denis Vlasenko | deeed59 | 2008-07-08 05:14:36 +0000 | [diff] [blame] | 149 | if (pass == 0) |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 150 | tmpindex->tag -= 743; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 151 | } |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 152 | storepos = xlseek(fd, header.size, SEEK_CUR); /* Seek past store */ |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 153 | /* Skip padding to 8 byte boundary after reading signature headers */ |
Denis Vlasenko | deeed59 | 2008-07-08 05:14:36 +0000 | [diff] [blame] | 154 | if (pass == 0) |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 155 | xlseek(fd, (-storepos) & 0x7, SEEK_CUR); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 156 | } |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 157 | /* realloc tags to save space */ |
| 158 | tags = xrealloc(tags, tagindex * sizeof(tags[0])); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 159 | *num_tags = tagindex; |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 160 | /* All done, leave the file at the start of the gzipped cpio archive */ |
| 161 | return tags; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 164 | static int bsearch_rpmtag(const void *key, const void *item) |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 165 | { |
Eric Andersen | 2cdd4d5 | 2006-01-30 18:33:12 +0000 | [diff] [blame] | 166 | int *tag = (int *)key; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 167 | rpm_index **tmp = (rpm_index **) item; |
Eric Andersen | 2cdd4d5 | 2006-01-30 18:33:12 +0000 | [diff] [blame] | 168 | return (*tag - tmp[0]->tag); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 171 | static int rpm_getcount(int tag) |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 172 | { |
| 173 | rpm_index **found; |
Denys Vlasenko | d4d4f35 | 2013-02-20 16:01:10 +0100 | [diff] [blame] | 174 | found = bsearch(&tag, G.mytags, G.tagcount, sizeof(struct rpmtag *), bsearch_rpmtag); |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 175 | if (!found) |
| 176 | return 0; |
| 177 | return found[0]->count; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 180 | static char *rpm_getstr(int tag, int itemindex) |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 181 | { |
| 182 | rpm_index **found; |
Denys Vlasenko | d4d4f35 | 2013-02-20 16:01:10 +0100 | [diff] [blame] | 183 | found = bsearch(&tag, G.mytags, G.tagcount, sizeof(struct rpmtag *), bsearch_rpmtag); |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 184 | if (!found || itemindex >= found[0]->count) |
| 185 | return NULL; |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 186 | if (found[0]->type == RPM_STRING_TYPE |
| 187 | || found[0]->type == RPM_I18NSTRING_TYPE |
| 188 | || found[0]->type == RPM_STRING_ARRAY_TYPE |
| 189 | ) { |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 190 | int n; |
Denys Vlasenko | d4d4f35 | 2013-02-20 16:01:10 +0100 | [diff] [blame] | 191 | char *tmpstr = (char *) G.map + found[0]->offset; |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 192 | for (n = 0; n < itemindex; n++) |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 193 | tmpstr = tmpstr + strlen(tmpstr) + 1; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 194 | return tmpstr; |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 195 | } |
| 196 | return NULL; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 199 | static int rpm_getint(int tag, int itemindex) |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 200 | { |
| 201 | rpm_index **found; |
Denys Vlasenko | 10f5f9b | 2013-02-20 15:57:39 +0100 | [diff] [blame] | 202 | char *tmpint; |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 203 | |
Mike Frysinger | 19d7021 | 2005-04-23 01:43:07 +0000 | [diff] [blame] | 204 | /* gcc throws warnings here when sizeof(void*)!=sizeof(int) ... |
| 205 | * it's ok to ignore it because tag won't be used as a pointer */ |
Denys Vlasenko | d4d4f35 | 2013-02-20 16:01:10 +0100 | [diff] [blame] | 206 | found = bsearch(&tag, G.mytags, G.tagcount, sizeof(struct rpmtag *), bsearch_rpmtag); |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 207 | if (!found || itemindex >= found[0]->count) |
| 208 | return -1; |
| 209 | |
Denys Vlasenko | d4d4f35 | 2013-02-20 16:01:10 +0100 | [diff] [blame] | 210 | tmpint = (char *) G.map + found[0]->offset; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 211 | if (found[0]->type == RPM_INT32_TYPE) { |
Denys Vlasenko | 10f5f9b | 2013-02-20 15:57:39 +0100 | [diff] [blame] | 212 | tmpint += itemindex*4; |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 213 | return ntohl(*(int32_t*)tmpint); |
| 214 | } |
| 215 | if (found[0]->type == RPM_INT16_TYPE) { |
Denys Vlasenko | 10f5f9b | 2013-02-20 15:57:39 +0100 | [diff] [blame] | 216 | tmpint += itemindex*2; |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 217 | return ntohs(*(int16_t*)tmpint); |
| 218 | } |
| 219 | if (found[0]->type == RPM_INT8_TYPE) { |
Denys Vlasenko | 10f5f9b | 2013-02-20 15:57:39 +0100 | [diff] [blame] | 220 | tmpint += itemindex; |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 221 | return *(int8_t*)tmpint; |
| 222 | } |
| 223 | return -1; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 226 | static void fileaction_dobackup(char *filename, int fileref) |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 227 | { |
| 228 | struct stat oldfile; |
| 229 | int stat_res; |
| 230 | char *newname; |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 231 | if (rpm_getint(TAG_FILEFLAGS, fileref) & RPMFILE_CONFIG) { |
| 232 | /* Only need to backup config files */ |
| 233 | stat_res = lstat(filename, &oldfile); |
| 234 | if (stat_res == 0 && S_ISREG(oldfile.st_mode)) { |
| 235 | /* File already exists - really should check MD5's etc to see if different */ |
Denis Vlasenko | 9cac521 | 2006-09-09 12:24:19 +0000 | [diff] [blame] | 236 | newname = xasprintf("%s.rpmorig", filename); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 237 | copy_file(filename, newname, FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS); |
| 238 | remove_file(filename, FILEUTILS_RECUR | FILEUTILS_FORCE); |
| 239 | free(newname); |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 244 | static void fileaction_setowngrp(char *filename, int fileref) |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 245 | { |
Denis Vlasenko | aa9eb1f | 2008-10-16 13:29:13 +0000 | [diff] [blame] | 246 | /* real rpm warns: "user foo does not exist - using <you>" */ |
| 247 | struct passwd *pw = getpwnam(rpm_getstr(TAG_FILEUSERNAME, fileref)); |
| 248 | int uid = pw ? pw->pw_uid : getuid(); /* or euid? */ |
| 249 | struct group *gr = getgrnam(rpm_getstr(TAG_FILEGROUPNAME, fileref)); |
| 250 | int gid = gr ? gr->gr_gid : getgid(); |
Denis Vlasenko | cf94446 | 2006-10-03 19:02:20 +0000 | [diff] [blame] | 251 | chown(filename, uid, gid); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 254 | static void loop_through_files(int filetag, void (*fileaction)(char *filename, int fileref)) |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 255 | { |
| 256 | int count = 0; |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 257 | while (rpm_getstr(filetag, count)) { |
| 258 | char* filename = xasprintf("%s%s", |
| 259 | rpm_getstr(TAG_DIRNAMES, rpm_getint(TAG_DIRINDEXES, count)), |
| 260 | rpm_getstr(TAG_BASENAMES, count)); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 261 | fileaction(filename, count++); |
| 262 | free(filename); |
| 263 | } |
| 264 | } |
Denys Vlasenko | d4d4f35 | 2013-02-20 16:01:10 +0100 | [diff] [blame] | 265 | |
| 266 | int rpm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 267 | int rpm_main(int argc, char **argv) |
| 268 | { |
| 269 | int opt, func = 0; |
| 270 | const unsigned pagesize = getpagesize(); |
| 271 | |
| 272 | while ((opt = getopt(argc, argv, "iqpldc")) != -1) { |
| 273 | switch (opt) { |
| 274 | case 'i': /* First arg: Install mode, with q: Information */ |
| 275 | if (!func) func = rpm_install; |
| 276 | else func |= rpm_query_info; |
| 277 | break; |
| 278 | case 'q': /* First arg: Query mode */ |
| 279 | if (func) bb_show_usage(); |
| 280 | func = rpm_query; |
| 281 | break; |
| 282 | case 'p': /* Query a package */ |
| 283 | func |= rpm_query_package; |
| 284 | break; |
| 285 | case 'l': /* List files in a package */ |
| 286 | func |= rpm_query_list; |
| 287 | break; |
| 288 | case 'd': /* List doc files in a package (implies list) */ |
| 289 | func |= rpm_query_list; |
| 290 | func |= rpm_query_list_doc; |
| 291 | break; |
| 292 | case 'c': /* List config files in a package (implies list) */ |
| 293 | func |= rpm_query_list; |
| 294 | func |= rpm_query_list_config; |
| 295 | break; |
| 296 | default: |
| 297 | bb_show_usage(); |
| 298 | } |
| 299 | } |
| 300 | argv += optind; |
| 301 | //argc -= optind; |
| 302 | if (!argv[0]) { |
| 303 | bb_show_usage(); |
| 304 | } |
| 305 | |
| 306 | while (*argv) { |
| 307 | int rpm_fd; |
Denys Vlasenko | 8e6a1ea | 2013-02-20 16:01:48 +0100 | [diff] [blame] | 308 | unsigned mapsize; |
Denys Vlasenko | d4d4f35 | 2013-02-20 16:01:10 +0100 | [diff] [blame] | 309 | const char *source_rpm; |
| 310 | |
| 311 | rpm_fd = xopen(*argv++, O_RDONLY); |
| 312 | G.mytags = rpm_gettags(rpm_fd, &G.tagcount); |
| 313 | if (!G.mytags) |
| 314 | bb_error_msg_and_die("error reading rpm header"); |
Denys Vlasenko | 8e6a1ea | 2013-02-20 16:01:48 +0100 | [diff] [blame] | 315 | mapsize = xlseek(rpm_fd, 0, SEEK_CUR); |
| 316 | mapsize = (mapsize + pagesize) & -(int)pagesize; |
Denys Vlasenko | d4d4f35 | 2013-02-20 16:01:10 +0100 | [diff] [blame] | 317 | /* Some NOMMU systems prefer MAP_PRIVATE over MAP_SHARED */ |
Denys Vlasenko | 8e6a1ea | 2013-02-20 16:01:48 +0100 | [diff] [blame] | 318 | G.map = mmap(0, mapsize, PROT_READ, MAP_PRIVATE, rpm_fd, 0); |
Denys Vlasenko | d4d4f35 | 2013-02-20 16:01:10 +0100 | [diff] [blame] | 319 | //FIXME: error check? |
| 320 | |
| 321 | source_rpm = rpm_getstr(TAG_SOURCERPM, 0); |
| 322 | |
| 323 | if (func & rpm_install) { |
| 324 | /* Backup any config files */ |
| 325 | loop_through_files(TAG_BASENAMES, fileaction_dobackup); |
| 326 | /* Extact the archive */ |
| 327 | extract_cpio(rpm_fd, source_rpm); |
| 328 | /* Set the correct file uid/gid's */ |
| 329 | loop_through_files(TAG_BASENAMES, fileaction_setowngrp); |
| 330 | } |
| 331 | else if ((func & (rpm_query|rpm_query_package)) == (rpm_query|rpm_query_package)) { |
| 332 | if (!(func & (rpm_query_info|rpm_query_list))) { |
| 333 | /* If just a straight query, just give package name */ |
| 334 | printf("%s-%s-%s\n", rpm_getstr(TAG_NAME, 0), rpm_getstr(TAG_VERSION, 0), rpm_getstr(TAG_RELEASE, 0)); |
| 335 | } |
| 336 | if (func & rpm_query_info) { |
| 337 | /* Do the nice printout */ |
| 338 | time_t bdate_time; |
| 339 | struct tm *bdate_ptm; |
| 340 | char bdatestring[50]; |
| 341 | const char *p; |
| 342 | |
| 343 | printf("%-12s: %s\n", "Name" , rpm_getstr(TAG_NAME, 0)); |
| 344 | /* TODO compat: add "Epoch" here */ |
| 345 | printf("%-12s: %s\n", "Version" , rpm_getstr(TAG_VERSION, 0)); |
| 346 | printf("%-12s: %s\n", "Release" , rpm_getstr(TAG_RELEASE, 0)); |
| 347 | /* add "Architecture" */ |
| 348 | printf("%-12s: %s\n", "Install Date", "(not installed)"); |
| 349 | printf("%-12s: %s\n", "Group" , rpm_getstr(TAG_GROUP, 0)); |
| 350 | printf("%-12s: %d\n", "Size" , rpm_getint(TAG_SIZE, 0)); |
| 351 | printf("%-12s: %s\n", "License" , rpm_getstr(TAG_LICENSE, 0)); |
| 352 | /* add "Signature" */ |
| 353 | printf("%-12s: %s\n", "Source RPM" , source_rpm ? source_rpm : "(none)"); |
| 354 | bdate_time = rpm_getint(TAG_BUILDTIME, 0); |
| 355 | bdate_ptm = localtime(&bdate_time); |
| 356 | strftime(bdatestring, 50, "%a %d %b %Y %T %Z", bdate_ptm); |
| 357 | printf("%-12s: %s\n", "Build Date" , bdatestring); |
| 358 | printf("%-12s: %s\n", "Build Host" , rpm_getstr(TAG_BUILDHOST, 0)); |
| 359 | p = rpm_getstr(TAG_PREFIXS, 0); |
| 360 | printf("%-12s: %s\n", "Relocations" , p ? p : "(not relocatable)"); |
| 361 | /* add "Packager" */ |
| 362 | p = rpm_getstr(TAG_VENDOR, 0); |
| 363 | printf("%-12s: %s\n", "Vendor" , p ? p : "(none)"); |
| 364 | printf("%-12s: %s\n", "URL" , rpm_getstr(TAG_URL, 0)); |
| 365 | printf("%-12s: %s\n", "Summary" , rpm_getstr(TAG_SUMMARY, 0)); |
| 366 | printf("Description :\n%s\n", rpm_getstr(TAG_DESCRIPTION, 0)); |
| 367 | } |
| 368 | if (func & rpm_query_list) { |
| 369 | int count, it, flags; |
| 370 | count = rpm_getcount(TAG_BASENAMES); |
| 371 | for (it = 0; it < count; it++) { |
| 372 | flags = rpm_getint(TAG_FILEFLAGS, it); |
| 373 | switch (func & (rpm_query_list_doc|rpm_query_list_config)) { |
| 374 | case rpm_query_list_doc: |
| 375 | if (!(flags & RPMFILE_DOC)) continue; |
| 376 | break; |
| 377 | case rpm_query_list_config: |
| 378 | if (!(flags & RPMFILE_CONFIG)) continue; |
| 379 | break; |
| 380 | case rpm_query_list_doc|rpm_query_list_config: |
| 381 | if (!(flags & (RPMFILE_CONFIG|RPMFILE_DOC))) continue; |
| 382 | break; |
| 383 | } |
| 384 | printf("%s%s\n", |
| 385 | rpm_getstr(TAG_DIRNAMES, rpm_getint(TAG_DIRINDEXES, it)), |
| 386 | rpm_getstr(TAG_BASENAMES, it)); |
| 387 | } |
| 388 | } |
| 389 | } |
Denys Vlasenko | 8e6a1ea | 2013-02-20 16:01:48 +0100 | [diff] [blame] | 390 | munmap(G.map, mapsize); |
Denys Vlasenko | d4d4f35 | 2013-02-20 16:01:10 +0100 | [diff] [blame] | 391 | free(G.mytags); |
| 392 | close(rpm_fd); |
| 393 | } |
| 394 | return 0; |
| 395 | } |