diff options
author | vda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2006-12-22 15:03:50 +0000 |
---|---|---|
committer | vda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2006-12-22 15:03:50 +0000 |
commit | a066356638a1338054ae9af92d503e573e2d1d61 (patch) | |
tree | 381e3567c18d76dc906257f2d0fc35cb25ffb24c | |
parent | 2c888ab84fbb9fb450577217c4354a2e677357bd (diff) | |
download | busybox-w32-a066356638a1338054ae9af92d503e573e2d1d61.tar.gz busybox-w32-a066356638a1338054ae9af92d503e573e2d1d61.tar.bz2 busybox-w32-a066356638a1338054ae9af92d503e573e2d1d61.zip |
rpm: reformat. was really hard to read
git-svn-id: svn://busybox.net/trunk/busybox@17056 69ca8d6d-28ef-0310-b511-8ec308f3f277
-rw-r--r-- | archival/rpm.c | 289 |
1 files changed, 168 insertions, 121 deletions
diff --git a/archival/rpm.c b/archival/rpm.c index 2ef7232f4..c0a1deee2 100644 --- a/archival/rpm.c +++ b/archival/rpm.c | |||
@@ -10,44 +10,44 @@ | |||
10 | #include "busybox.h" | 10 | #include "busybox.h" |
11 | #include "unarchive.h" | 11 | #include "unarchive.h" |
12 | 12 | ||
13 | #define RPM_HEADER_MAGIC "\216\255\350" | 13 | #define RPM_HEADER_MAGIC "\216\255\350" |
14 | #define RPM_CHAR_TYPE 1 | 14 | #define RPM_CHAR_TYPE 1 |
15 | #define RPM_INT8_TYPE 2 | 15 | #define RPM_INT8_TYPE 2 |
16 | #define RPM_INT16_TYPE 3 | 16 | #define RPM_INT16_TYPE 3 |
17 | #define RPM_INT32_TYPE 4 | 17 | #define RPM_INT32_TYPE 4 |
18 | /* #define RPM_INT64_TYPE 5 ---- These aren't supported (yet) */ | 18 | /* #define RPM_INT64_TYPE 5 ---- These aren't supported (yet) */ |
19 | #define RPM_STRING_TYPE 6 | 19 | #define RPM_STRING_TYPE 6 |
20 | #define RPM_BIN_TYPE 7 | 20 | #define RPM_BIN_TYPE 7 |
21 | #define RPM_STRING_ARRAY_TYPE 8 | 21 | #define RPM_STRING_ARRAY_TYPE 8 |
22 | #define RPM_I18NSTRING_TYPE 9 | 22 | #define RPM_I18NSTRING_TYPE 9 |
23 | 23 | ||
24 | #define RPMTAG_NAME 1000 | 24 | #define TAG_NAME 1000 |
25 | #define RPMTAG_VERSION 1001 | 25 | #define TAG_VERSION 1001 |
26 | #define RPMTAG_RELEASE 1002 | 26 | #define TAG_RELEASE 1002 |
27 | #define RPMTAG_SUMMARY 1004 | 27 | #define TAG_SUMMARY 1004 |
28 | #define RPMTAG_DESCRIPTION 1005 | 28 | #define TAG_DESCRIPTION 1005 |
29 | #define RPMTAG_BUILDTIME 1006 | 29 | #define TAG_BUILDTIME 1006 |
30 | #define RPMTAG_BUILDHOST 1007 | 30 | #define TAG_BUILDHOST 1007 |
31 | #define RPMTAG_SIZE 1009 | 31 | #define TAG_SIZE 1009 |
32 | #define RPMTAG_VENDOR 1011 | 32 | #define TAG_VENDOR 1011 |
33 | #define RPMTAG_LICENSE 1014 | 33 | #define TAG_LICENSE 1014 |
34 | #define RPMTAG_PACKAGER 1015 | 34 | #define TAG_PACKAGER 1015 |
35 | #define RPMTAG_GROUP 1016 | 35 | #define TAG_GROUP 1016 |
36 | #define RPMTAG_URL 1020 | 36 | #define TAG_URL 1020 |
37 | #define RPMTAG_PREIN 1023 | 37 | #define TAG_PREIN 1023 |
38 | #define RPMTAG_POSTIN 1024 | 38 | #define TAG_POSTIN 1024 |
39 | #define RPMTAG_FILEFLAGS 1037 | 39 | #define TAG_FILEFLAGS 1037 |
40 | #define RPMTAG_FILEUSERNAME 1039 | 40 | #define TAG_FILEUSERNAME 1039 |
41 | #define RPMTAG_FILEGROUPNAME 1040 | 41 | #define TAG_FILEGROUPNAME 1040 |
42 | #define RPMTAG_SOURCERPM 1044 | 42 | #define TAG_SOURCERPM 1044 |
43 | #define RPMTAG_PREINPROG 1085 | 43 | #define TAG_PREINPROG 1085 |
44 | #define RPMTAG_POSTINPROG 1086 | 44 | #define TAG_POSTINPROG 1086 |
45 | #define RPMTAG_PREFIXS 1098 | 45 | #define TAG_PREFIXS 1098 |
46 | #define RPMTAG_DIRINDEXES 1116 | 46 | #define TAG_DIRINDEXES 1116 |
47 | #define RPMTAG_BASENAMES 1117 | 47 | #define TAG_BASENAMES 1117 |
48 | #define RPMTAG_DIRNAMES 1118 | 48 | #define TAG_DIRNAMES 1118 |
49 | #define RPMFILE_CONFIG (1 << 0) | 49 | #define RPMFILE_CONFIG (1 << 0) |
50 | #define RPMFILE_DOC (1 << 1) | 50 | #define RPMFILE_DOC (1 << 1) |
51 | 51 | ||
52 | enum rpm_functions_e { | 52 | enum rpm_functions_e { |
53 | rpm_query = 1, | 53 | rpm_query = 1, |
@@ -73,7 +73,7 @@ static int tagcount; | |||
73 | static void extract_cpio_gz(int fd); | 73 | static void extract_cpio_gz(int fd); |
74 | static rpm_index **rpm_gettags(int fd, int *num_tags); | 74 | static rpm_index **rpm_gettags(int fd, int *num_tags); |
75 | static int bsearch_rpmtag(const void *key, const void *item); | 75 | static int bsearch_rpmtag(const void *key, const void *item); |
76 | static char *rpm_getstring(int tag, int itemindex); | 76 | static char *rpm_getstr(int tag, int itemindex); |
77 | static int rpm_getint(int tag, int itemindex); | 77 | static int rpm_getint(int tag, int itemindex); |
78 | static int rpm_getcount(int tag); | 78 | static int rpm_getcount(int tag); |
79 | static void fileaction_dobackup(char *filename, int fileref); | 79 | static void fileaction_dobackup(char *filename, int fileref); |
@@ -83,28 +83,29 @@ static void loop_through_files(int filetag, void (*fileaction)(char *filename, i | |||
83 | int rpm_main(int argc, char **argv) | 83 | int rpm_main(int argc, char **argv) |
84 | { | 84 | { |
85 | int opt = 0, func = 0, rpm_fd, offset; | 85 | int opt = 0, func = 0, rpm_fd, offset; |
86 | const int pagesize = getpagesize(); | ||
86 | 87 | ||
87 | while ((opt = getopt(argc, argv, "iqpldc")) != -1) { | 88 | while ((opt = getopt(argc, argv, "iqpldc")) != -1) { |
88 | switch (opt) { | 89 | switch (opt) { |
89 | case 'i': // First arg: Install mode, with q: Information | 90 | case 'i': /* First arg: Install mode, with q: Information */ |
90 | if (!func) func |= rpm_install; | 91 | if (!func) func = rpm_install; |
91 | else func |= rpm_query_info; | 92 | else func |= rpm_query_info; |
92 | break; | 93 | break; |
93 | case 'q': // First arg: Query mode | 94 | case 'q': /* First arg: Query mode */ |
94 | if (!func) func |= rpm_query; | 95 | if (func) bb_show_usage(); |
95 | else bb_show_usage(); | 96 | func = rpm_query; |
96 | break; | 97 | break; |
97 | case 'p': // Query a package | 98 | case 'p': /* Query a package */ |
98 | func |= rpm_query_package; | 99 | func |= rpm_query_package; |
99 | break; | 100 | break; |
100 | case 'l': // List files in a package | 101 | case 'l': /* List files in a package */ |
101 | func |= rpm_query_list; | 102 | func |= rpm_query_list; |
102 | break; | 103 | break; |
103 | case 'd': // List doc files in a package (implies list) | 104 | case 'd': /* List doc files in a package (implies list) */ |
104 | func |= rpm_query_list; | 105 | func |= rpm_query_list; |
105 | func |= rpm_query_list_doc; | 106 | func |= rpm_query_list_doc; |
106 | break; | 107 | break; |
107 | case 'c': // List config files in a package (implies list) | 108 | case 'c': /* List config files in a package (implies list) */ |
108 | func |= rpm_query_list; | 109 | func |= rpm_query_list; |
109 | func |= rpm_query_list_config; | 110 | func |= rpm_query_list_config; |
110 | break; | 111 | break; |
@@ -112,57 +113,73 @@ int rpm_main(int argc, char **argv) | |||
112 | bb_show_usage(); | 113 | bb_show_usage(); |
113 | } | 114 | } |
114 | } | 115 | } |
116 | argv += optind; | ||
117 | argc -= optind; | ||
118 | if (!argc) bb_show_usage(); | ||
119 | |||
120 | while (*argv) { | ||
121 | rpm_fd = xopen(*argv++, O_RDONLY); | ||
122 | mytags = rpm_gettags(rpm_fd, &tagcount); | ||
123 | if (!mytags) | ||
124 | bb_error_msg_and_die("error reading rpm header"); | ||
125 | offset = xlseek(rpm_fd, 0, SEEK_CUR); | ||
126 | /* Mimimum is one page */ | ||
127 | map = mmap(0, offset > pagesize ? (offset + offset % pagesize) : pagesize, PROT_READ, MAP_PRIVATE, rpm_fd, 0); | ||
115 | 128 | ||
116 | if (optind == argc) bb_show_usage(); | ||
117 | while (optind < argc) { | ||
118 | rpm_fd = xopen(argv[optind], O_RDONLY); | ||
119 | mytags = rpm_gettags(rpm_fd, (int *) &tagcount); | ||
120 | offset = lseek(rpm_fd, 0, SEEK_CUR); | ||
121 | if (!mytags) { printf("Error reading rpm header\n"); exit(-1); } | ||
122 | map = mmap(0, offset > getpagesize() ? (offset + offset % getpagesize()) : getpagesize(), PROT_READ, MAP_PRIVATE, rpm_fd, 0); // Mimimum is one page | ||
123 | if (func & rpm_install) { | 129 | if (func & rpm_install) { |
124 | loop_through_files(RPMTAG_BASENAMES, fileaction_dobackup); /* Backup any config files */ | 130 | /* Backup any config files */ |
125 | extract_cpio_gz(rpm_fd); // Extact the archive | 131 | loop_through_files(TAG_BASENAMES, fileaction_dobackup); |
126 | loop_through_files(RPMTAG_BASENAMES, fileaction_setowngrp); /* Set the correct file uid/gid's */ | 132 | /* Extact the archive */ |
127 | } else if (func & rpm_query && func & rpm_query_package) { | 133 | extract_cpio_gz(rpm_fd); |
128 | if (!((func & rpm_query_info) || (func & rpm_query_list))) { // If just a straight query, just give package name | 134 | /* Set the correct file uid/gid's */ |
129 | printf("%s-%s-%s\n", rpm_getstring(RPMTAG_NAME, 0), rpm_getstring(RPMTAG_VERSION, 0), rpm_getstring(RPMTAG_RELEASE, 0)); | 135 | loop_through_files(TAG_BASENAMES, fileaction_setowngrp); |
136 | } | ||
137 | else if ((func & (rpm_query|rpm_query_package)) == (rpm_query|rpm_query_package)) { | ||
138 | if (!(func & (rpm_query_info|rpm_query_list))) { | ||
139 | /* If just a straight query, just give package name */ | ||
140 | printf("%s-%s-%s\n", rpm_getstr(TAG_NAME, 0), rpm_getstr(TAG_VERSION, 0), rpm_getstr(TAG_RELEASE, 0)); | ||
130 | } | 141 | } |
131 | if (func & rpm_query_info) { | 142 | if (func & rpm_query_info) { |
132 | /* Do the nice printout */ | 143 | /* Do the nice printout */ |
133 | time_t bdate_time; | 144 | time_t bdate_time; |
134 | struct tm *bdate; | 145 | struct tm *bdate; |
135 | char bdatestring[50]; | 146 | char bdatestring[50]; |
136 | printf("Name : %-29sRelocations: %s\n", rpm_getstring(RPMTAG_NAME, 0), rpm_getstring(RPMTAG_PREFIXS, 0) ? rpm_getstring(RPMTAG_PREFIXS, 0) : "(not relocateable)"); | 147 | printf("Name : %-29sRelocations: %s\n", rpm_getstr(TAG_NAME, 0), rpm_getstr(TAG_PREFIXS, 0) ? rpm_getstr(TAG_PREFIXS, 0) : "(not relocateable)"); |
137 | printf("Version : %-34sVendor: %s\n", rpm_getstring(RPMTAG_VERSION, 0), rpm_getstring(RPMTAG_VENDOR, 0) ? rpm_getstring(RPMTAG_VENDOR, 0) : "(none)"); | 148 | printf("Version : %-34sVendor: %s\n", rpm_getstr(TAG_VERSION, 0), rpm_getstr(TAG_VENDOR, 0) ? rpm_getstr(TAG_VENDOR, 0) : "(none)"); |
138 | bdate_time = rpm_getint(RPMTAG_BUILDTIME, 0); | 149 | bdate_time = rpm_getint(TAG_BUILDTIME, 0); |
139 | bdate = localtime((time_t *) &bdate_time); | 150 | bdate = localtime((time_t *) &bdate_time); |
140 | strftime(bdatestring, 50, "%a %d %b %Y %T %Z", bdate); | 151 | strftime(bdatestring, 50, "%a %d %b %Y %T %Z", bdate); |
141 | printf("Release : %-30sBuild Date: %s\n", rpm_getstring(RPMTAG_RELEASE, 0), bdatestring); | 152 | printf("Release : %-30sBuild Date: %s\n", rpm_getstr(TAG_RELEASE, 0), bdatestring); |
142 | printf("Install date: %-30sBuild Host: %s\n", "(not installed)", rpm_getstring(RPMTAG_BUILDHOST, 0)); | 153 | printf("Install date: %-30sBuild Host: %s\n", "(not installed)", rpm_getstr(TAG_BUILDHOST, 0)); |
143 | printf("Group : %-30sSource RPM: %s\n", rpm_getstring(RPMTAG_GROUP, 0), rpm_getstring(RPMTAG_SOURCERPM, 0)); | 154 | printf("Group : %-30sSource RPM: %s\n", rpm_getstr(TAG_GROUP, 0), rpm_getstr(TAG_SOURCERPM, 0)); |
144 | printf("Size : %-33dLicense: %s\n", rpm_getint(RPMTAG_SIZE, 0), rpm_getstring(RPMTAG_LICENSE, 0)); | 155 | printf("Size : %-33dLicense: %s\n", rpm_getint(TAG_SIZE, 0), rpm_getstr(TAG_LICENSE, 0)); |
145 | printf("URL : %s\n", rpm_getstring(RPMTAG_URL, 0)); | 156 | printf("URL : %s\n", rpm_getstr(TAG_URL, 0)); |
146 | printf("Summary : %s\n", rpm_getstring(RPMTAG_SUMMARY, 0)); | 157 | printf("Summary : %s\n", rpm_getstr(TAG_SUMMARY, 0)); |
147 | printf("Description :\n%s\n", rpm_getstring(RPMTAG_DESCRIPTION, 0)); | 158 | printf("Description :\n%s\n", rpm_getstr(TAG_DESCRIPTION, 0)); |
148 | } | 159 | } |
149 | if (func & rpm_query_list) { | 160 | if (func & rpm_query_list) { |
150 | int count, it, flags; | 161 | int count, it, flags; |
151 | count = rpm_getcount(RPMTAG_BASENAMES); | 162 | count = rpm_getcount(TAG_BASENAMES); |
152 | for (it = 0; it < count; it++) { | 163 | for (it = 0; it < count; it++) { |
153 | flags = rpm_getint(RPMTAG_FILEFLAGS, it); | 164 | flags = rpm_getint(TAG_FILEFLAGS, it); |
154 | switch ((func & rpm_query_list_doc) + (func & rpm_query_list_config)) | 165 | switch (func & (rpm_query_list_doc|rpm_query_list_config)) { |
155 | { | 166 | case rpm_query_list_doc: |
156 | case rpm_query_list_doc: if (!(flags & RPMFILE_DOC)) continue; break; | 167 | if (!(flags & RPMFILE_DOC)) continue; |
157 | case rpm_query_list_config: if (!(flags & RPMFILE_CONFIG)) continue; break; | 168 | break; |
158 | case rpm_query_list_doc + rpm_query_list_config: if (!((flags & RPMFILE_CONFIG) || (flags & RPMFILE_DOC))) continue; break; | 169 | case rpm_query_list_config: |
170 | if (!(flags & RPMFILE_CONFIG)) continue; | ||
171 | break; | ||
172 | case rpm_query_list_doc|rpm_query_list_config: | ||
173 | if (!(flags & (RPMFILE_CONFIG|RPMFILE_DOC))) continue; | ||
174 | break; | ||
159 | } | 175 | } |
160 | printf("%s%s\n", rpm_getstring(RPMTAG_DIRNAMES, rpm_getint(RPMTAG_DIRINDEXES, it)), rpm_getstring(RPMTAG_BASENAMES, it)); | 176 | printf("%s%s\n", |
177 | rpm_getstr(TAG_DIRNAMES, rpm_getint(TAG_DIRINDEXES, it)), | ||
178 | rpm_getstr(TAG_BASENAMES, it)); | ||
161 | } | 179 | } |
162 | } | 180 | } |
163 | } | 181 | } |
164 | optind++; | 182 | free(mytags); |
165 | free (mytags); | ||
166 | } | 183 | } |
167 | return 0; | 184 | return 0; |
168 | } | 185 | } |
@@ -186,21 +203,25 @@ static void extract_cpio_gz(int fd) { | |||
186 | bb_error_msg_and_die("invalid gzip magic"); | 203 | bb_error_msg_and_die("invalid gzip magic"); |
187 | } | 204 | } |
188 | check_header_gzip(archive_handle->src_fd); | 205 | check_header_gzip(archive_handle->src_fd); |
189 | xchdir("/"); // Install RPM's to root | 206 | xchdir("/"); /* Install RPM's to root */ |
190 | 207 | ||
191 | archive_handle->src_fd = open_transformer(archive_handle->src_fd, inflate_gunzip); | 208 | archive_handle->src_fd = open_transformer(archive_handle->src_fd, inflate_gunzip); |
192 | archive_handle->offset = 0; | 209 | archive_handle->offset = 0; |
193 | while (get_header_cpio(archive_handle) == EXIT_SUCCESS); | 210 | while (get_header_cpio(archive_handle) == EXIT_SUCCESS) |
211 | /* loop */; | ||
194 | } | 212 | } |
195 | 213 | ||
196 | 214 | ||
197 | static rpm_index **rpm_gettags(int fd, int *num_tags) | 215 | static rpm_index **rpm_gettags(int fd, int *num_tags) |
198 | { | 216 | { |
199 | rpm_index **tags = xzalloc(200 * sizeof(struct rpmtag *)); /* We should never need mode than 200, and realloc later */ | 217 | /* We should never need mode than 200, and realloc later */ |
218 | rpm_index **tags = xzalloc(200 * sizeof(struct rpmtag *)); | ||
200 | int pass, tagindex = 0; | 219 | int pass, tagindex = 0; |
201 | lseek(fd, 96, SEEK_CUR); /* Seek past the unused lead */ | ||
202 | 220 | ||
203 | for (pass = 0; pass < 2; pass++) { /* 1st pass is the signature headers, 2nd is the main stuff */ | 221 | xlseek(fd, 96, SEEK_CUR); /* Seek past the unused lead */ |
222 | |||
223 | /* 1st pass is the signature headers, 2nd is the main stuff */ | ||
224 | for (pass = 0; pass < 2; pass++) { | ||
204 | struct { | 225 | struct { |
205 | char magic[3]; /* 3 byte magic: 0x8e 0xad 0xe8 */ | 226 | char magic[3]; /* 3 byte magic: 0x8e 0xad 0xe8 */ |
206 | uint8_t version; /* 1 byte version number */ | 227 | uint8_t version; /* 1 byte version number */ |
@@ -211,24 +232,31 @@ static rpm_index **rpm_gettags(int fd, int *num_tags) | |||
211 | rpm_index *tmpindex; | 232 | rpm_index *tmpindex; |
212 | int storepos; | 233 | int storepos; |
213 | 234 | ||
214 | read(fd, &header, sizeof(header)); | 235 | xread(fd, &header, sizeof(header)); |
215 | if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC, 3) != 0) return NULL; /* Invalid magic */ | 236 | if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC, 3) != 0) |
216 | if (header.version != 1) return NULL; /* This program only supports v1 headers */ | 237 | return NULL; /* Invalid magic */ |
238 | if (header.version != 1) | ||
239 | return NULL; /* This program only supports v1 headers */ | ||
217 | header.size = ntohl(header.size); | 240 | header.size = ntohl(header.size); |
218 | header.entries = ntohl(header.entries); | 241 | header.entries = ntohl(header.entries); |
219 | storepos = lseek(fd,0,SEEK_CUR) + header.entries * 16; | 242 | storepos = xlseek(fd,0,SEEK_CUR) + header.entries * 16; |
220 | 243 | ||
221 | while (header.entries--) { | 244 | while (header.entries--) { |
222 | tmpindex = tags[tagindex++] = xmalloc(sizeof(rpm_index)); | 245 | tmpindex = tags[tagindex++] = xmalloc(sizeof(rpm_index)); |
223 | read(fd, tmpindex, sizeof(rpm_index)); | 246 | xread(fd, tmpindex, sizeof(rpm_index)); |
224 | tmpindex->tag = ntohl(tmpindex->tag); tmpindex->type = ntohl(tmpindex->type); tmpindex->count = ntohl(tmpindex->count); | 247 | tmpindex->tag = ntohl(tmpindex->tag); |
248 | tmpindex->type = ntohl(tmpindex->type); | ||
249 | tmpindex->count = ntohl(tmpindex->count); | ||
225 | tmpindex->offset = storepos + ntohl(tmpindex->offset); | 250 | tmpindex->offset = storepos + ntohl(tmpindex->offset); |
226 | if (pass==0) tmpindex->tag -= 743; | 251 | if (pass==0) |
252 | tmpindex->tag -= 743; | ||
227 | } | 253 | } |
228 | lseek(fd, header.size, SEEK_CUR); /* Seek past store */ | 254 | xlseek(fd, header.size, SEEK_CUR); /* Seek past store */ |
229 | if (pass==0) lseek(fd, (8 - (lseek(fd,0,SEEK_CUR) % 8)) % 8, SEEK_CUR); /* Skip padding to 8 byte boundary after reading signature headers */ | 255 | /* Skip padding to 8 byte boundary after reading signature headers */ |
256 | if (pass==0) | ||
257 | xlseek(fd, (8 - (xlseek(fd,0,SEEK_CUR) % 8)) % 8, SEEK_CUR); | ||
230 | } | 258 | } |
231 | tags = realloc(tags, tagindex * sizeof(struct rpmtag *)); /* realloc tags to save space */ | 259 | tags = xrealloc(tags, tagindex * sizeof(struct rpmtag *)); /* realloc tags to save space */ |
232 | *num_tags = tagindex; | 260 | *num_tags = tagindex; |
233 | return tags; /* All done, leave the file at the start of the gzipped cpio archive */ | 261 | return tags; /* All done, leave the file at the start of the gzipped cpio archive */ |
234 | } | 262 | } |
@@ -244,42 +272,59 @@ static int rpm_getcount(int tag) | |||
244 | { | 272 | { |
245 | rpm_index **found; | 273 | rpm_index **found; |
246 | found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag); | 274 | found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag); |
247 | if (!found) return 0; | 275 | if (!found) |
248 | else return found[0]->count; | 276 | return 0; |
277 | return found[0]->count; | ||
249 | } | 278 | } |
250 | 279 | ||
251 | static char *rpm_getstring(int tag, int itemindex) | 280 | static char *rpm_getstr(int tag, int itemindex) |
252 | { | 281 | { |
253 | rpm_index **found; | 282 | rpm_index **found; |
254 | found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag); | 283 | found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag); |
255 | if (!found || itemindex >= found[0]->count) return NULL; | 284 | if (!found || itemindex >= found[0]->count) |
285 | return NULL; | ||
256 | if (found[0]->type == RPM_STRING_TYPE || found[0]->type == RPM_I18NSTRING_TYPE || found[0]->type == RPM_STRING_ARRAY_TYPE) { | 286 | if (found[0]->type == RPM_STRING_TYPE || found[0]->type == RPM_I18NSTRING_TYPE || found[0]->type == RPM_STRING_ARRAY_TYPE) { |
257 | int n; | 287 | int n; |
258 | char *tmpstr = (char *) (map + found[0]->offset); | 288 | char *tmpstr = (char *) (map + found[0]->offset); |
259 | for (n=0; n < itemindex; n++) tmpstr = tmpstr + strlen(tmpstr) + 1; | 289 | for (n=0; n < itemindex; n++) |
290 | tmpstr = tmpstr + strlen(tmpstr) + 1; | ||
260 | return tmpstr; | 291 | return tmpstr; |
261 | } else return NULL; | 292 | } |
293 | return NULL; | ||
262 | } | 294 | } |
263 | 295 | ||
264 | static int rpm_getint(int tag, int itemindex) | 296 | static int rpm_getint(int tag, int itemindex) |
265 | { | 297 | { |
266 | rpm_index **found; | 298 | rpm_index **found; |
267 | int n, *tmpint; | 299 | int *tmpint; /* NB: using int8_t* would be easier to code */ |
300 | |||
268 | /* gcc throws warnings here when sizeof(void*)!=sizeof(int) ... | 301 | /* gcc throws warnings here when sizeof(void*)!=sizeof(int) ... |
269 | * it's ok to ignore it because tag won't be used as a pointer */ | 302 | * it's ok to ignore it because tag won't be used as a pointer */ |
270 | found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag); | 303 | found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag); |
271 | if (!found || itemindex >= found[0]->count) return -1; | 304 | if (!found || itemindex >= found[0]->count) |
305 | return -1; | ||
306 | |||
272 | tmpint = (int *) (map + found[0]->offset); | 307 | tmpint = (int *) (map + found[0]->offset); |
308 | |||
273 | if (found[0]->type == RPM_INT32_TYPE) { | 309 | if (found[0]->type == RPM_INT32_TYPE) { |
274 | for (n=0; n<itemindex; n++) tmpint = (int *) ((void *) tmpint + 4); | 310 | tmpint = (int *) ((char *) tmpint + itemindex*4); |
275 | return ntohl(*tmpint); | 311 | /*return ntohl(*tmpint);*/ |
276 | } else if (found[0]->type == RPM_INT16_TYPE) { | 312 | /* int can be != int32_t */ |
277 | for (n=0; n<itemindex; n++) tmpint = (int *) ((void *) tmpint + 2); | 313 | return ntohl(*(int32_t*)tmpint); |
278 | return ntohs(*tmpint); | 314 | } |
279 | } else if (found[0]->type == RPM_INT8_TYPE) { | 315 | if (found[0]->type == RPM_INT16_TYPE) { |
280 | for (n=0; n<itemindex; n++) tmpint = (int *) ((void *) tmpint + 1); | 316 | tmpint = (int *) ((char *) tmpint + itemindex*2); |
281 | return ntohs(*tmpint); | 317 | /* ??? read int, and THEN ntohs() it?? */ |
282 | } else return -1; | 318 | /*return ntohs(*tmpint);*/ |
319 | return ntohs(*(int16_t*)tmpint); | ||
320 | } | ||
321 | if (found[0]->type == RPM_INT8_TYPE) { | ||
322 | tmpint = (int *) ((char *) tmpint + itemindex); | ||
323 | /* ??? why we don't read byte here??? */ | ||
324 | /*return ntohs(*tmpint);*/ | ||
325 | return *(int8_t*)tmpint; | ||
326 | } | ||
327 | return -1; | ||
283 | } | 328 | } |
284 | 329 | ||
285 | static void fileaction_dobackup(char *filename, int fileref) | 330 | static void fileaction_dobackup(char *filename, int fileref) |
@@ -287,9 +332,11 @@ static void fileaction_dobackup(char *filename, int fileref) | |||
287 | struct stat oldfile; | 332 | struct stat oldfile; |
288 | int stat_res; | 333 | int stat_res; |
289 | char *newname; | 334 | char *newname; |
290 | if (rpm_getint(RPMTAG_FILEFLAGS, fileref) & RPMFILE_CONFIG) { /* Only need to backup config files */ | 335 | if (rpm_getint(TAG_FILEFLAGS, fileref) & RPMFILE_CONFIG) { |
291 | stat_res = lstat (filename, &oldfile); | 336 | /* Only need to backup config files */ |
292 | if (stat_res == 0 && S_ISREG(oldfile.st_mode)) { /* File already exists - really should check MD5's etc to see if different */ | 337 | stat_res = lstat(filename, &oldfile); |
338 | if (stat_res == 0 && S_ISREG(oldfile.st_mode)) { | ||
339 | /* File already exists - really should check MD5's etc to see if different */ | ||
293 | newname = xasprintf("%s.rpmorig", filename); | 340 | newname = xasprintf("%s.rpmorig", filename); |
294 | copy_file(filename, newname, FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS); | 341 | copy_file(filename, newname, FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS); |
295 | remove_file(filename, FILEUTILS_RECUR | FILEUTILS_FORCE); | 342 | remove_file(filename, FILEUTILS_RECUR | FILEUTILS_FORCE); |
@@ -301,18 +348,18 @@ static void fileaction_dobackup(char *filename, int fileref) | |||
301 | static void fileaction_setowngrp(char *filename, int fileref) | 348 | static void fileaction_setowngrp(char *filename, int fileref) |
302 | { | 349 | { |
303 | int uid, gid; | 350 | int uid, gid; |
304 | uid = bb_xgetpwnam(rpm_getstring(RPMTAG_FILEUSERNAME, fileref)); | 351 | uid = bb_xgetpwnam(rpm_getstr(TAG_FILEUSERNAME, fileref)); |
305 | gid = bb_xgetgrnam(rpm_getstring(RPMTAG_FILEGROUPNAME, fileref)); | 352 | gid = bb_xgetgrnam(rpm_getstr(TAG_FILEGROUPNAME, fileref)); |
306 | chown(filename, uid, gid); | 353 | chown(filename, uid, gid); |
307 | } | 354 | } |
308 | 355 | ||
309 | static void loop_through_files(int filetag, void (*fileaction)(char *filename, int fileref)) | 356 | static void loop_through_files(int filetag, void (*fileaction)(char *filename, int fileref)) |
310 | { | 357 | { |
311 | int count = 0; | 358 | int count = 0; |
312 | while (rpm_getstring(filetag, count)) { | 359 | while (rpm_getstr(filetag, count)) { |
313 | char * filename = xasprintf("%s%s", | 360 | char* filename = xasprintf("%s%s", |
314 | rpm_getstring(RPMTAG_DIRNAMES, rpm_getint(RPMTAG_DIRINDEXES, | 361 | rpm_getstr(TAG_DIRNAMES, rpm_getint(TAG_DIRINDEXES, count)), |
315 | count)), rpm_getstring(RPMTAG_BASENAMES, count)); | 362 | rpm_getstr(TAG_BASENAMES, count)); |
316 | fileaction(filename, count++); | 363 | fileaction(filename, count++); |
317 | free(filename); | 364 | free(filename); |
318 | } | 365 | } |