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 | * |
Bernhard Reutner-Fischer | 20f4000 | 2006-01-30 17:17:14 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
Glenn L McGrath | 38386d7 | 2002-12-10 02:09:12 +0000 | [diff] [blame] | 11 | #include "unarchive.h" |
| 12 | |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 13 | #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 McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 23 | |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 24 | #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 McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 51 | |
| 52 | enum 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 | |
| 62 | typedef struct { |
Eric Andersen | dfcb5b0 | 2004-01-30 22:54:20 +0000 | [diff] [blame] | 63 | 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 McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 67 | } rpm_index; |
| 68 | |
| 69 | static void *map; |
| 70 | static rpm_index **mytags; |
| 71 | static int tagcount; |
| 72 | |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 73 | static void extract_cpio_gz(int fd); |
| 74 | static rpm_index **rpm_gettags(int fd, int *num_tags); |
| 75 | static int bsearch_rpmtag(const void *key, const void *item); |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 76 | static char *rpm_getstr(int tag, int itemindex); |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 77 | static int rpm_getint(int tag, int itemindex); |
| 78 | static int rpm_getcount(int tag); |
| 79 | static void fileaction_dobackup(char *filename, int fileref); |
| 80 | static void fileaction_setowngrp(char *filename, int fileref); |
| 81 | 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] | 82 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 83 | int rpm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 84 | int rpm_main(int argc, char **argv) |
| 85 | { |
| 86 | int opt = 0, func = 0, rpm_fd, offset; |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 87 | const int pagesize = getpagesize(); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 88 | |
| 89 | while ((opt = getopt(argc, argv, "iqpldc")) != -1) { |
| 90 | switch (opt) { |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 91 | case 'i': /* First arg: Install mode, with q: Information */ |
| 92 | if (!func) func = rpm_install; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 93 | else func |= rpm_query_info; |
| 94 | break; |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 95 | case 'q': /* First arg: Query mode */ |
| 96 | if (func) bb_show_usage(); |
| 97 | func = rpm_query; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 98 | break; |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 99 | case 'p': /* Query a package */ |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 100 | func |= rpm_query_package; |
| 101 | break; |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 102 | case 'l': /* List files in a package */ |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 103 | func |= rpm_query_list; |
| 104 | break; |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 105 | case 'd': /* List doc files in a package (implies list) */ |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 106 | func |= rpm_query_list; |
| 107 | func |= rpm_query_list_doc; |
| 108 | break; |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 109 | case 'c': /* List config files in a package (implies list) */ |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 110 | func |= rpm_query_list; |
| 111 | func |= rpm_query_list_config; |
| 112 | break; |
| 113 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 114 | bb_show_usage(); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 115 | } |
| 116 | } |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 117 | argv += optind; |
Denis Vlasenko | 1a9e9bd | 2008-11-01 12:54:56 +0000 | [diff] [blame] | 118 | //argc -= optind; |
| 119 | if (!argv[0]) bb_show_usage(); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 120 | |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 121 | 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 McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 130 | if (func & rpm_install) { |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 131 | /* 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 McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 142 | } |
| 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 Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 148 | 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 McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 151 | bdate = localtime((time_t *) &bdate_time); |
| 152 | strftime(bdatestring, 50, "%a %d %b %Y %T %Z", bdate); |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 153 | 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 McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 160 | } |
| 161 | if (func & rpm_query_list) { |
| 162 | int count, it, flags; |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 163 | count = rpm_getcount(TAG_BASENAMES); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 164 | for (it = 0; it < count; it++) { |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 165 | 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 McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 176 | } |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 177 | printf("%s%s\n", |
| 178 | rpm_getstr(TAG_DIRNAMES, rpm_getint(TAG_DIRINDEXES, it)), |
| 179 | rpm_getstr(TAG_BASENAMES, it)); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | } |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 183 | free(mytags); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 184 | } |
| 185 | return 0; |
| 186 | } |
| 187 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 188 | static void extract_cpio_gz(int fd) |
| 189 | { |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 190 | archive_handle_t *archive_handle; |
| 191 | unsigned char magic[2]; |
Denis Vlasenko | 211f7f8 | 2007-09-05 11:48:32 +0000 | [diff] [blame] | 192 | #if BB_MMU |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 193 | IF_DESKTOP(long long) int FAST_FUNC (*xformer)(int src_fd, int dst_fd); |
Denis Vlasenko | 211f7f8 | 2007-09-05 11:48:32 +0000 | [diff] [blame] | 194 | enum { xformer_prog = 0 }; |
| 195 | #else |
| 196 | enum { xformer = 0 }; |
| 197 | const char *xformer_prog; |
| 198 | #endif |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 199 | |
Denis Vlasenko | c14d39e | 2007-06-08 13:05:39 +0000 | [diff] [blame] | 200 | /* Initialize */ |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 201 | archive_handle = init_handle(); |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 202 | archive_handle->seek = seek_by_read; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 203 | //archive_handle->action_header = header_list; |
| 204 | archive_handle->action_data = data_extract_all; |
Denys Vlasenko | d57d626 | 2009-09-17 02:43:14 +0200 | [diff] [blame] | 205 | archive_handle->ah_flags = ARCHIVE_RESTORE_DATE | ARCHIVE_CREATE_LEADING_DIRS |
Denis Vlasenko | aa9eb1f | 2008-10-16 13:29:13 +0000 | [diff] [blame] | 206 | /* compat: overwrite existing files. |
| 207 | * try "rpm -i foo.src.rpm" few times in a row - |
| 208 | * standard rpm will not complain. |
| 209 | * (TODO? real rpm creates "file;1234" and then renames it) */ |
Denys Vlasenko | d57d626 | 2009-09-17 02:43:14 +0200 | [diff] [blame] | 210 | | ARCHIVE_UNLINK_OLD; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 211 | archive_handle->src_fd = fd; |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 212 | /*archive_handle->offset = 0; - init_handle() did it */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 213 | |
Denis Vlasenko | e9ad84d | 2008-08-05 13:10:34 +0000 | [diff] [blame] | 214 | // TODO: open_zipped does the same |
| 215 | |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 216 | xread(archive_handle->src_fd, &magic, 2); |
Denis Vlasenko | 211f7f8 | 2007-09-05 11:48:32 +0000 | [diff] [blame] | 217 | #if BB_MMU |
Denis Vlasenko | c14d39e | 2007-06-08 13:05:39 +0000 | [diff] [blame] | 218 | xformer = unpack_gz_stream; |
Denis Vlasenko | 211f7f8 | 2007-09-05 11:48:32 +0000 | [diff] [blame] | 219 | #else |
| 220 | xformer_prog = "gunzip"; |
| 221 | #endif |
Denis Vlasenko | e9ad84d | 2008-08-05 13:10:34 +0000 | [diff] [blame] | 222 | if (magic[0] != 0x1f || magic[1] != 0x8b) { |
| 223 | if (!ENABLE_FEATURE_SEAMLESS_BZ2 |
| 224 | || magic[0] != 'B' || magic[1] != 'Z' |
| 225 | ) { |
Denis Vlasenko | c14d39e | 2007-06-08 13:05:39 +0000 | [diff] [blame] | 226 | bb_error_msg_and_die("no gzip" |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 227 | IF_FEATURE_SEAMLESS_BZ2("/bzip2") |
Denis Vlasenko | c14d39e | 2007-06-08 13:05:39 +0000 | [diff] [blame] | 228 | " magic"); |
Denis Vlasenko | e9ad84d | 2008-08-05 13:10:34 +0000 | [diff] [blame] | 229 | } |
| 230 | #if BB_MMU |
| 231 | xformer = unpack_bz2_stream; |
| 232 | #else |
| 233 | xformer_prog = "bunzip2"; |
| 234 | #endif |
Denis Vlasenko | 211f7f8 | 2007-09-05 11:48:32 +0000 | [diff] [blame] | 235 | } else { |
Denis Vlasenko | 211f7f8 | 2007-09-05 11:48:32 +0000 | [diff] [blame] | 236 | #if !BB_MMU |
| 237 | /* NOMMU version of open_transformer execs an external unzipper that should |
| 238 | * have the file position at the start of the file */ |
| 239 | xlseek(archive_handle->src_fd, 0, SEEK_SET); |
| 240 | #endif |
| 241 | } |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 242 | |
Denis Vlasenko | c14d39e | 2007-06-08 13:05:39 +0000 | [diff] [blame] | 243 | xchdir("/"); /* Install RPM's to root */ |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 244 | open_transformer(archive_handle->src_fd, xformer, xformer_prog); |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 245 | archive_handle->offset = 0; |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 246 | while (get_header_cpio(archive_handle) == EXIT_SUCCESS) |
Denis Vlasenko | c14d39e | 2007-06-08 13:05:39 +0000 | [diff] [blame] | 247 | continue; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 251 | static rpm_index **rpm_gettags(int fd, int *num_tags) |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 252 | { |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 253 | /* We should never need mode than 200, and realloc later */ |
Denis Vlasenko | 1a9e9bd | 2008-11-01 12:54:56 +0000 | [diff] [blame] | 254 | rpm_index **tags = xzalloc(200 * sizeof(tags[0])); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 255 | int pass, tagindex = 0; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 256 | |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 257 | xlseek(fd, 96, SEEK_CUR); /* Seek past the unused lead */ |
| 258 | |
| 259 | /* 1st pass is the signature headers, 2nd is the main stuff */ |
| 260 | for (pass = 0; pass < 2; pass++) { |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 261 | struct { |
| 262 | char magic[3]; /* 3 byte magic: 0x8e 0xad 0xe8 */ |
Eric Andersen | dfcb5b0 | 2004-01-30 22:54:20 +0000 | [diff] [blame] | 263 | uint8_t version; /* 1 byte version number */ |
| 264 | uint32_t reserved; /* 4 bytes reserved */ |
| 265 | uint32_t entries; /* Number of entries in header (4 bytes) */ |
| 266 | uint32_t size; /* Size of store (4 bytes) */ |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 267 | } header; |
Denis Vlasenko | 1a9e9bd | 2008-11-01 12:54:56 +0000 | [diff] [blame] | 268 | struct BUG_header { |
| 269 | char BUG_header[sizeof(header) == 16 ? 1 : -1]; |
| 270 | }; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 271 | rpm_index *tmpindex; |
| 272 | int storepos; |
| 273 | |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 274 | xread(fd, &header, sizeof(header)); |
| 275 | if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC, 3) != 0) |
| 276 | return NULL; /* Invalid magic */ |
| 277 | if (header.version != 1) |
| 278 | return NULL; /* This program only supports v1 headers */ |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 279 | header.size = ntohl(header.size); |
| 280 | header.entries = ntohl(header.entries); |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 281 | storepos = xlseek(fd,0,SEEK_CUR) + header.entries * 16; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 282 | |
| 283 | while (header.entries--) { |
Denis Vlasenko | 1a9e9bd | 2008-11-01 12:54:56 +0000 | [diff] [blame] | 284 | tmpindex = tags[tagindex++] = xmalloc(sizeof(*tmpindex)); |
| 285 | xread(fd, tmpindex, sizeof(*tmpindex)); |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 286 | tmpindex->tag = ntohl(tmpindex->tag); |
| 287 | tmpindex->type = ntohl(tmpindex->type); |
| 288 | tmpindex->count = ntohl(tmpindex->count); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 289 | tmpindex->offset = storepos + ntohl(tmpindex->offset); |
Denis Vlasenko | deeed59 | 2008-07-08 05:14:36 +0000 | [diff] [blame] | 290 | if (pass == 0) |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 291 | tmpindex->tag -= 743; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 292 | } |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 293 | xlseek(fd, header.size, SEEK_CUR); /* Seek past store */ |
| 294 | /* Skip padding to 8 byte boundary after reading signature headers */ |
Denis Vlasenko | deeed59 | 2008-07-08 05:14:36 +0000 | [diff] [blame] | 295 | if (pass == 0) |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 296 | xlseek(fd, (8 - (xlseek(fd,0,SEEK_CUR) % 8)) % 8, SEEK_CUR); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 297 | } |
Denis Vlasenko | 1a9e9bd | 2008-11-01 12:54:56 +0000 | [diff] [blame] | 298 | tags = xrealloc(tags, tagindex * sizeof(tags[0])); /* realloc tags to save space */ |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 299 | *num_tags = tagindex; |
| 300 | return tags; /* All done, leave the file at the start of the gzipped cpio archive */ |
| 301 | } |
| 302 | |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 303 | static int bsearch_rpmtag(const void *key, const void *item) |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 304 | { |
Eric Andersen | 2cdd4d5 | 2006-01-30 18:33:12 +0000 | [diff] [blame] | 305 | int *tag = (int *)key; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 306 | rpm_index **tmp = (rpm_index **) item; |
Eric Andersen | 2cdd4d5 | 2006-01-30 18:33:12 +0000 | [diff] [blame] | 307 | return (*tag - tmp[0]->tag); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 310 | static int rpm_getcount(int tag) |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 311 | { |
| 312 | rpm_index **found; |
Eric Andersen | 2cdd4d5 | 2006-01-30 18:33:12 +0000 | [diff] [blame] | 313 | found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag); |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 314 | if (!found) |
| 315 | return 0; |
| 316 | return found[0]->count; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 319 | static char *rpm_getstr(int tag, int itemindex) |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 320 | { |
| 321 | rpm_index **found; |
Eric Andersen | 2cdd4d5 | 2006-01-30 18:33:12 +0000 | [diff] [blame] | 322 | found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag); |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 323 | if (!found || itemindex >= found[0]->count) |
| 324 | return NULL; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 325 | if (found[0]->type == RPM_STRING_TYPE || found[0]->type == RPM_I18NSTRING_TYPE || found[0]->type == RPM_STRING_ARRAY_TYPE) { |
| 326 | int n; |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 327 | char *tmpstr = (char *) map + found[0]->offset; |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 328 | for (n=0; n < itemindex; n++) |
| 329 | tmpstr = tmpstr + strlen(tmpstr) + 1; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 330 | return tmpstr; |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 331 | } |
| 332 | return NULL; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 335 | static int rpm_getint(int tag, int itemindex) |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 336 | { |
| 337 | rpm_index **found; |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 338 | int *tmpint; /* NB: using int8_t* would be easier to code */ |
| 339 | |
Mike Frysinger | 19d7021 | 2005-04-23 01:43:07 +0000 | [diff] [blame] | 340 | /* gcc throws warnings here when sizeof(void*)!=sizeof(int) ... |
| 341 | * 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] | 342 | found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag); |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 343 | if (!found || itemindex >= found[0]->count) |
| 344 | return -1; |
| 345 | |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 346 | tmpint = (int *) ((char *) map + found[0]->offset); |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 347 | |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 348 | if (found[0]->type == RPM_INT32_TYPE) { |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 349 | tmpint = (int *) ((char *) tmpint + itemindex*4); |
| 350 | /*return ntohl(*tmpint);*/ |
| 351 | /* int can be != int32_t */ |
| 352 | return ntohl(*(int32_t*)tmpint); |
| 353 | } |
| 354 | if (found[0]->type == RPM_INT16_TYPE) { |
| 355 | tmpint = (int *) ((char *) tmpint + itemindex*2); |
| 356 | /* ??? read int, and THEN ntohs() it?? */ |
| 357 | /*return ntohs(*tmpint);*/ |
| 358 | return ntohs(*(int16_t*)tmpint); |
| 359 | } |
| 360 | if (found[0]->type == RPM_INT8_TYPE) { |
| 361 | tmpint = (int *) ((char *) tmpint + itemindex); |
| 362 | /* ??? why we don't read byte here??? */ |
| 363 | /*return ntohs(*tmpint);*/ |
| 364 | return *(int8_t*)tmpint; |
| 365 | } |
| 366 | return -1; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 369 | static void fileaction_dobackup(char *filename, int fileref) |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 370 | { |
| 371 | struct stat oldfile; |
| 372 | int stat_res; |
| 373 | char *newname; |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 374 | if (rpm_getint(TAG_FILEFLAGS, fileref) & RPMFILE_CONFIG) { |
| 375 | /* Only need to backup config files */ |
| 376 | stat_res = lstat(filename, &oldfile); |
| 377 | if (stat_res == 0 && S_ISREG(oldfile.st_mode)) { |
| 378 | /* 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] | 379 | newname = xasprintf("%s.rpmorig", filename); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 380 | copy_file(filename, newname, FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS); |
| 381 | remove_file(filename, FILEUTILS_RECUR | FILEUTILS_FORCE); |
| 382 | free(newname); |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 387 | static void fileaction_setowngrp(char *filename, int fileref) |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 388 | { |
Denis Vlasenko | aa9eb1f | 2008-10-16 13:29:13 +0000 | [diff] [blame] | 389 | /* real rpm warns: "user foo does not exist - using <you>" */ |
| 390 | struct passwd *pw = getpwnam(rpm_getstr(TAG_FILEUSERNAME, fileref)); |
| 391 | int uid = pw ? pw->pw_uid : getuid(); /* or euid? */ |
| 392 | struct group *gr = getgrnam(rpm_getstr(TAG_FILEGROUPNAME, fileref)); |
| 393 | int gid = gr ? gr->gr_gid : getgid(); |
Denis Vlasenko | cf94446 | 2006-10-03 19:02:20 +0000 | [diff] [blame] | 394 | chown(filename, uid, gid); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 397 | 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] | 398 | { |
| 399 | int count = 0; |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 400 | while (rpm_getstr(filetag, count)) { |
| 401 | char* filename = xasprintf("%s%s", |
| 402 | rpm_getstr(TAG_DIRNAMES, rpm_getint(TAG_DIRINDEXES, count)), |
| 403 | rpm_getstr(TAG_BASENAMES, count)); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 404 | fileaction(filename, count++); |
| 405 | free(filename); |
| 406 | } |
| 407 | } |