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" |
Pere Orga | 1f4447b | 2011-03-27 22:40:30 +0200 | [diff] [blame] | 17 | //usage: "\n -i Show information" |
| 18 | //usage: "\n -l List contents" |
| 19 | //usage: "\n -d List documents" |
| 20 | //usage: "\n -c List config files" |
| 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 | |
| 82 | static void *map; |
| 83 | static rpm_index **mytags; |
| 84 | static int tagcount; |
| 85 | |
Denys Vlasenko | 5dfd9c4 | 2010-05-06 16:56:38 +0200 | [diff] [blame] | 86 | static void extract_cpio(int fd, const char *source_rpm); |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 87 | static rpm_index **rpm_gettags(int fd, int *num_tags); |
| 88 | static int bsearch_rpmtag(const void *key, const void *item); |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 89 | static char *rpm_getstr(int tag, int itemindex); |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 90 | static int rpm_getint(int tag, int itemindex); |
| 91 | static int rpm_getcount(int tag); |
| 92 | static void fileaction_dobackup(char *filename, int fileref); |
| 93 | static void fileaction_setowngrp(char *filename, int fileref); |
| 94 | 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] | 95 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 96 | int rpm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 97 | int rpm_main(int argc, char **argv) |
| 98 | { |
| 99 | int opt = 0, func = 0, rpm_fd, offset; |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 100 | const int pagesize = getpagesize(); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 101 | |
| 102 | while ((opt = getopt(argc, argv, "iqpldc")) != -1) { |
| 103 | switch (opt) { |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 104 | case 'i': /* First arg: Install mode, with q: Information */ |
| 105 | if (!func) func = rpm_install; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 106 | else func |= rpm_query_info; |
| 107 | break; |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 108 | case 'q': /* First arg: Query mode */ |
| 109 | if (func) bb_show_usage(); |
| 110 | func = rpm_query; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 111 | break; |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 112 | case 'p': /* Query a package */ |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 113 | func |= rpm_query_package; |
| 114 | break; |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 115 | case 'l': /* List files in a package */ |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 116 | func |= rpm_query_list; |
| 117 | break; |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 118 | case 'd': /* List doc files in a package (implies list) */ |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 119 | func |= rpm_query_list; |
| 120 | func |= rpm_query_list_doc; |
| 121 | break; |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 122 | case 'c': /* List config files in a package (implies list) */ |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 123 | func |= rpm_query_list; |
| 124 | func |= rpm_query_list_config; |
| 125 | break; |
| 126 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 127 | bb_show_usage(); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 130 | argv += optind; |
Denis Vlasenko | 1a9e9bd | 2008-11-01 12:54:56 +0000 | [diff] [blame] | 131 | //argc -= optind; |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 132 | if (!argv[0]) { |
| 133 | bb_show_usage(); |
| 134 | } |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 135 | |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 136 | while (*argv) { |
Denys Vlasenko | 5dfd9c4 | 2010-05-06 16:56:38 +0200 | [diff] [blame] | 137 | const char *source_rpm; |
| 138 | |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 139 | rpm_fd = xopen(*argv++, O_RDONLY); |
| 140 | mytags = rpm_gettags(rpm_fd, &tagcount); |
| 141 | if (!mytags) |
| 142 | bb_error_msg_and_die("error reading rpm header"); |
| 143 | offset = xlseek(rpm_fd, 0, SEEK_CUR); |
| 144 | /* Mimimum is one page */ |
| 145 | map = mmap(0, offset > pagesize ? (offset + offset % pagesize) : pagesize, PROT_READ, MAP_PRIVATE, rpm_fd, 0); |
| 146 | |
Denys Vlasenko | 5dfd9c4 | 2010-05-06 16:56:38 +0200 | [diff] [blame] | 147 | source_rpm = rpm_getstr(TAG_SOURCERPM, 0); |
| 148 | |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 149 | if (func & rpm_install) { |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 150 | /* Backup any config files */ |
| 151 | loop_through_files(TAG_BASENAMES, fileaction_dobackup); |
| 152 | /* Extact the archive */ |
Denys Vlasenko | 5dfd9c4 | 2010-05-06 16:56:38 +0200 | [diff] [blame] | 153 | extract_cpio(rpm_fd, source_rpm); |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 154 | /* Set the correct file uid/gid's */ |
| 155 | loop_through_files(TAG_BASENAMES, fileaction_setowngrp); |
| 156 | } |
| 157 | else if ((func & (rpm_query|rpm_query_package)) == (rpm_query|rpm_query_package)) { |
| 158 | if (!(func & (rpm_query_info|rpm_query_list))) { |
| 159 | /* If just a straight query, just give package name */ |
| 160 | printf("%s-%s-%s\n", rpm_getstr(TAG_NAME, 0), rpm_getstr(TAG_VERSION, 0), rpm_getstr(TAG_RELEASE, 0)); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 161 | } |
| 162 | if (func & rpm_query_info) { |
| 163 | /* Do the nice printout */ |
| 164 | time_t bdate_time; |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 165 | struct tm *bdate_ptm; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 166 | char bdatestring[50]; |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 167 | const char *p; |
| 168 | |
| 169 | p = rpm_getstr(TAG_PREFIXS, 0); |
| 170 | if (!p) p = "(not relocateable)"; |
| 171 | printf("Name : %-29sRelocations: %s\n", rpm_getstr(TAG_NAME, 0), p); |
| 172 | p = rpm_getstr(TAG_VENDOR, 0); |
| 173 | if (!p) p = "(none)"; |
| 174 | printf("Version : %-34sVendor: %s\n", rpm_getstr(TAG_VERSION, 0), p); |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 175 | bdate_time = rpm_getint(TAG_BUILDTIME, 0); |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 176 | bdate_ptm = localtime(&bdate_time); |
| 177 | strftime(bdatestring, 50, "%a %d %b %Y %T %Z", bdate_ptm); |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 178 | printf("Release : %-30sBuild Date: %s\n", rpm_getstr(TAG_RELEASE, 0), bdatestring); |
| 179 | printf("Install date: %-30sBuild Host: %s\n", "(not installed)", rpm_getstr(TAG_BUILDHOST, 0)); |
Denys Vlasenko | 5dfd9c4 | 2010-05-06 16:56:38 +0200 | [diff] [blame] | 180 | printf("Group : %-30sSource RPM: %s\n", rpm_getstr(TAG_GROUP, 0), source_rpm); |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 181 | printf("Size : %-33dLicense: %s\n", rpm_getint(TAG_SIZE, 0), rpm_getstr(TAG_LICENSE, 0)); |
| 182 | printf("URL : %s\n", rpm_getstr(TAG_URL, 0)); |
| 183 | printf("Summary : %s\n", rpm_getstr(TAG_SUMMARY, 0)); |
| 184 | printf("Description :\n%s\n", rpm_getstr(TAG_DESCRIPTION, 0)); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 185 | } |
| 186 | if (func & rpm_query_list) { |
| 187 | int count, it, flags; |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 188 | count = rpm_getcount(TAG_BASENAMES); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 189 | for (it = 0; it < count; it++) { |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 190 | flags = rpm_getint(TAG_FILEFLAGS, it); |
| 191 | switch (func & (rpm_query_list_doc|rpm_query_list_config)) { |
| 192 | case rpm_query_list_doc: |
| 193 | if (!(flags & RPMFILE_DOC)) continue; |
| 194 | break; |
| 195 | case rpm_query_list_config: |
| 196 | if (!(flags & RPMFILE_CONFIG)) continue; |
| 197 | break; |
| 198 | case rpm_query_list_doc|rpm_query_list_config: |
| 199 | if (!(flags & (RPMFILE_CONFIG|RPMFILE_DOC))) continue; |
| 200 | break; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 201 | } |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 202 | printf("%s%s\n", |
| 203 | rpm_getstr(TAG_DIRNAMES, rpm_getint(TAG_DIRINDEXES, it)), |
| 204 | rpm_getstr(TAG_BASENAMES, it)); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | } |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 208 | free(mytags); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 209 | } |
| 210 | return 0; |
| 211 | } |
| 212 | |
Denys Vlasenko | 5dfd9c4 | 2010-05-06 16:56:38 +0200 | [diff] [blame] | 213 | static void extract_cpio(int fd, const char *source_rpm) |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 214 | { |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 215 | archive_handle_t *archive_handle; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 216 | |
Denys Vlasenko | 5dfd9c4 | 2010-05-06 16:56:38 +0200 | [diff] [blame] | 217 | if (source_rpm != NULL) { |
| 218 | /* Binary rpm (it was built from some SRPM), install to root */ |
| 219 | xchdir("/"); |
| 220 | } /* else: SRPM, install to current dir */ |
| 221 | |
Denis Vlasenko | c14d39e | 2007-06-08 13:05:39 +0000 | [diff] [blame] | 222 | /* Initialize */ |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 223 | archive_handle = init_handle(); |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 224 | archive_handle->seek = seek_by_read; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 225 | archive_handle->action_data = data_extract_all; |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 226 | #if 0 /* For testing (rpm -i only lists the files in internal cpio): */ |
| 227 | archive_handle->action_header = header_list; |
| 228 | archive_handle->action_data = data_skip; |
| 229 | #endif |
Denys Vlasenko | d57d626 | 2009-09-17 02:43:14 +0200 | [diff] [blame] | 230 | archive_handle->ah_flags = ARCHIVE_RESTORE_DATE | ARCHIVE_CREATE_LEADING_DIRS |
Denis Vlasenko | aa9eb1f | 2008-10-16 13:29:13 +0000 | [diff] [blame] | 231 | /* compat: overwrite existing files. |
| 232 | * try "rpm -i foo.src.rpm" few times in a row - |
| 233 | * standard rpm will not complain. |
| 234 | * (TODO? real rpm creates "file;1234" and then renames it) */ |
Denys Vlasenko | d57d626 | 2009-09-17 02:43:14 +0200 | [diff] [blame] | 235 | | ARCHIVE_UNLINK_OLD; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 236 | archive_handle->src_fd = fd; |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 237 | /*archive_handle->offset = 0; - init_handle() did it */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 238 | |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 239 | 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] | 240 | while (get_header_cpio(archive_handle) == EXIT_SUCCESS) |
Denis Vlasenko | c14d39e | 2007-06-08 13:05:39 +0000 | [diff] [blame] | 241 | continue; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 244 | static rpm_index **rpm_gettags(int fd, int *num_tags) |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 245 | { |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 246 | /* We should never need more than 200 (shrink via realloc later) */ |
Denis Vlasenko | 1a9e9bd | 2008-11-01 12:54:56 +0000 | [diff] [blame] | 247 | rpm_index **tags = xzalloc(200 * sizeof(tags[0])); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 248 | int pass, tagindex = 0; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 249 | |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 250 | xlseek(fd, 96, SEEK_CUR); /* Seek past the unused lead */ |
| 251 | |
| 252 | /* 1st pass is the signature headers, 2nd is the main stuff */ |
| 253 | for (pass = 0; pass < 2; pass++) { |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 254 | struct rpm_header header; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 255 | rpm_index *tmpindex; |
| 256 | int storepos; |
| 257 | |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 258 | xread(fd, &header, sizeof(header)); |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 259 | if (header.magic_and_ver != htonl(RPM_HEADER_MAGICnVER)) |
| 260 | return NULL; /* Invalid magic, or not version 1 */ |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 261 | header.size = ntohl(header.size); |
| 262 | header.entries = ntohl(header.entries); |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 263 | storepos = xlseek(fd, 0, SEEK_CUR) + header.entries * 16; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 264 | |
| 265 | while (header.entries--) { |
Denis Vlasenko | 1a9e9bd | 2008-11-01 12:54:56 +0000 | [diff] [blame] | 266 | tmpindex = tags[tagindex++] = xmalloc(sizeof(*tmpindex)); |
| 267 | xread(fd, tmpindex, sizeof(*tmpindex)); |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 268 | tmpindex->tag = ntohl(tmpindex->tag); |
| 269 | tmpindex->type = ntohl(tmpindex->type); |
| 270 | tmpindex->count = ntohl(tmpindex->count); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 271 | tmpindex->offset = storepos + ntohl(tmpindex->offset); |
Denis Vlasenko | deeed59 | 2008-07-08 05:14:36 +0000 | [diff] [blame] | 272 | if (pass == 0) |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 273 | tmpindex->tag -= 743; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 274 | } |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 275 | storepos = xlseek(fd, header.size, SEEK_CUR); /* Seek past store */ |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 276 | /* Skip padding to 8 byte boundary after reading signature headers */ |
Denis Vlasenko | deeed59 | 2008-07-08 05:14:36 +0000 | [diff] [blame] | 277 | if (pass == 0) |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 278 | xlseek(fd, (-storepos) & 0x7, SEEK_CUR); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 279 | } |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 280 | /* realloc tags to save space */ |
| 281 | tags = xrealloc(tags, tagindex * sizeof(tags[0])); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 282 | *num_tags = tagindex; |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 283 | /* All done, leave the file at the start of the gzipped cpio archive */ |
| 284 | return tags; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 287 | static int bsearch_rpmtag(const void *key, const void *item) |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 288 | { |
Eric Andersen | 2cdd4d5 | 2006-01-30 18:33:12 +0000 | [diff] [blame] | 289 | int *tag = (int *)key; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 290 | rpm_index **tmp = (rpm_index **) item; |
Eric Andersen | 2cdd4d5 | 2006-01-30 18:33:12 +0000 | [diff] [blame] | 291 | return (*tag - tmp[0]->tag); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 294 | static int rpm_getcount(int tag) |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 295 | { |
| 296 | rpm_index **found; |
Eric Andersen | 2cdd4d5 | 2006-01-30 18:33:12 +0000 | [diff] [blame] | 297 | found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag); |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 298 | if (!found) |
| 299 | return 0; |
| 300 | return found[0]->count; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 303 | static char *rpm_getstr(int tag, int itemindex) |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 304 | { |
| 305 | rpm_index **found; |
Eric Andersen | 2cdd4d5 | 2006-01-30 18:33:12 +0000 | [diff] [blame] | 306 | found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag); |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 307 | if (!found || itemindex >= found[0]->count) |
| 308 | return NULL; |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 309 | if (found[0]->type == RPM_STRING_TYPE |
| 310 | || found[0]->type == RPM_I18NSTRING_TYPE |
| 311 | || found[0]->type == RPM_STRING_ARRAY_TYPE |
| 312 | ) { |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 313 | int n; |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 314 | char *tmpstr = (char *) map + found[0]->offset; |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 315 | for (n = 0; n < itemindex; n++) |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 316 | tmpstr = tmpstr + strlen(tmpstr) + 1; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 317 | return tmpstr; |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 318 | } |
| 319 | return NULL; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 322 | static int rpm_getint(int tag, int itemindex) |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 323 | { |
| 324 | rpm_index **found; |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 325 | int *tmpint; /* NB: using int8_t* would be easier to code */ |
| 326 | |
Mike Frysinger | 19d7021 | 2005-04-23 01:43:07 +0000 | [diff] [blame] | 327 | /* gcc throws warnings here when sizeof(void*)!=sizeof(int) ... |
| 328 | * it's ok to ignore it because tag won't be used as a pointer */ |
Eric Andersen | 2cdd4d5 | 2006-01-30 18:33:12 +0000 | [diff] [blame] | 329 | found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag); |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 330 | if (!found || itemindex >= found[0]->count) |
| 331 | return -1; |
| 332 | |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 333 | tmpint = (int *) ((char *) map + found[0]->offset); |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 334 | |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 335 | if (found[0]->type == RPM_INT32_TYPE) { |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 336 | tmpint = (int *) ((char *) tmpint + itemindex*4); |
| 337 | /*return ntohl(*tmpint);*/ |
| 338 | /* int can be != int32_t */ |
| 339 | return ntohl(*(int32_t*)tmpint); |
| 340 | } |
| 341 | if (found[0]->type == RPM_INT16_TYPE) { |
| 342 | tmpint = (int *) ((char *) tmpint + itemindex*2); |
| 343 | /* ??? read int, and THEN ntohs() it?? */ |
| 344 | /*return ntohs(*tmpint);*/ |
| 345 | return ntohs(*(int16_t*)tmpint); |
| 346 | } |
| 347 | if (found[0]->type == RPM_INT8_TYPE) { |
| 348 | tmpint = (int *) ((char *) tmpint + itemindex); |
| 349 | /* ??? why we don't read byte here??? */ |
| 350 | /*return ntohs(*tmpint);*/ |
| 351 | return *(int8_t*)tmpint; |
| 352 | } |
| 353 | return -1; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 356 | static void fileaction_dobackup(char *filename, int fileref) |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 357 | { |
| 358 | struct stat oldfile; |
| 359 | int stat_res; |
| 360 | char *newname; |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 361 | if (rpm_getint(TAG_FILEFLAGS, fileref) & RPMFILE_CONFIG) { |
| 362 | /* Only need to backup config files */ |
| 363 | stat_res = lstat(filename, &oldfile); |
| 364 | if (stat_res == 0 && S_ISREG(oldfile.st_mode)) { |
| 365 | /* 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] | 366 | newname = xasprintf("%s.rpmorig", filename); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 367 | copy_file(filename, newname, FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS); |
| 368 | remove_file(filename, FILEUTILS_RECUR | FILEUTILS_FORCE); |
| 369 | free(newname); |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 374 | static void fileaction_setowngrp(char *filename, int fileref) |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 375 | { |
Denis Vlasenko | aa9eb1f | 2008-10-16 13:29:13 +0000 | [diff] [blame] | 376 | /* real rpm warns: "user foo does not exist - using <you>" */ |
| 377 | struct passwd *pw = getpwnam(rpm_getstr(TAG_FILEUSERNAME, fileref)); |
| 378 | int uid = pw ? pw->pw_uid : getuid(); /* or euid? */ |
| 379 | struct group *gr = getgrnam(rpm_getstr(TAG_FILEGROUPNAME, fileref)); |
| 380 | int gid = gr ? gr->gr_gid : getgid(); |
Denis Vlasenko | cf94446 | 2006-10-03 19:02:20 +0000 | [diff] [blame] | 381 | chown(filename, uid, gid); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 384 | 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] | 385 | { |
| 386 | int count = 0; |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 387 | while (rpm_getstr(filetag, count)) { |
| 388 | char* filename = xasprintf("%s%s", |
| 389 | rpm_getstr(TAG_DIRNAMES, rpm_getint(TAG_DIRINDEXES, count)), |
| 390 | rpm_getstr(TAG_BASENAMES, count)); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 391 | fileaction(filename, count++); |
| 392 | free(filename); |
| 393 | } |
| 394 | } |