diff options
author | Glenn L McGrath <bug1@ihug.co.nz> | 2002-12-10 00:17:22 +0000 |
---|---|---|
committer | Glenn L McGrath <bug1@ihug.co.nz> | 2002-12-10 00:17:22 +0000 |
commit | b72a735a320eb60d29bc1580138c06ae7c9d321c (patch) | |
tree | 4923918fadcaac4fdaa5d4197a4e5bafd22b9b2e | |
parent | 62fae308a3d92d987711d08f3d36b0ad753c2e18 (diff) | |
download | busybox-w32-b72a735a320eb60d29bc1580138c06ae7c9d321c.tar.gz busybox-w32-b72a735a320eb60d29bc1580138c06ae7c9d321c.tar.bz2 busybox-w32-b72a735a320eb60d29bc1580138c06ae7c9d321c.zip |
rpm applet by Laurence Anderson
-rw-r--r-- | AUTHORS | 2 | ||||
-rw-r--r-- | archival/Config.in | 8 | ||||
-rw-r--r-- | archival/Makefile.in | 1 | ||||
-rw-r--r-- | archival/cpio.c | 160 | ||||
-rw-r--r-- | archival/libunarchive/Makefile.in | 1 | ||||
-rw-r--r-- | archival/libunarchive/data_extract_all.c | 2 | ||||
-rw-r--r-- | archival/libunarchive/get_header_cpio.c | 185 | ||||
-rw-r--r-- | archival/rpm.c | 351 | ||||
-rw-r--r-- | include/applets.h | 3 | ||||
-rw-r--r-- | include/unarchive.h | 1 | ||||
-rw-r--r-- | include/usage.h | 13 |
11 files changed, 565 insertions, 162 deletions
@@ -16,7 +16,7 @@ Erik Andersen <andersen@codepoet.org>, <andersee@debian.org> | |||
16 | core apps, tons of new apps as noted in header files. | 16 | core apps, tons of new apps as noted in header files. |
17 | 17 | ||
18 | Laurence Anderson <l.d.anderson@warwick.ac.uk> | 18 | Laurence Anderson <l.d.anderson@warwick.ac.uk> |
19 | rpm2cpio, unzip, get_header_cpio, read_gz interface | 19 | rpm2cpio, unzip, get_header_cpio, read_gz interface, rpm |
20 | 20 | ||
21 | Edward Betts <edward@debian.org> | 21 | Edward Betts <edward@debian.org> |
22 | expr, hostid, logname, tty, wc, whoami, yes | 22 | expr, hostid, logname, tty, wc, whoami, yes |
diff --git a/archival/Config.in b/archival/Config.in index bbd3fa0d5..affde32c8 100644 --- a/archival/Config.in +++ b/archival/Config.in | |||
@@ -139,7 +139,13 @@ config CONFIG_RPM2CPIO | |||
139 | bool "rpm2cpio" | 139 | bool "rpm2cpio" |
140 | default n | 140 | default n |
141 | help | 141 | help |
142 | Please submit a patch to add help text for this item. | 142 | Converts an RPM file into a CPIO archive. |
143 | |||
144 | config CONFIG_RPM | ||
145 | bool "rpm" | ||
146 | default n | ||
147 | help | ||
148 | Mini RPM applet - querys and extracts | ||
143 | 149 | ||
144 | config CONFIG_TAR | 150 | config CONFIG_TAR |
145 | bool "tar" | 151 | bool "tar" |
diff --git a/archival/Makefile.in b/archival/Makefile.in index 226cde690..38f5e6292 100644 --- a/archival/Makefile.in +++ b/archival/Makefile.in | |||
@@ -32,6 +32,7 @@ ARCHIVAL-$(CONFIG_DPKG_DEB) += dpkg_deb.o | |||
32 | ARCHIVAL-$(CONFIG_GUNZIP) += gunzip.o | 32 | ARCHIVAL-$(CONFIG_GUNZIP) += gunzip.o |
33 | ARCHIVAL-$(CONFIG_GZIP) += gzip.o | 33 | ARCHIVAL-$(CONFIG_GZIP) += gzip.o |
34 | ARCHIVAL-$(CONFIG_RPM2CPIO) += rpm2cpio.o | 34 | ARCHIVAL-$(CONFIG_RPM2CPIO) += rpm2cpio.o |
35 | ARCHIVAL-$(CONFIG_RPM) += rpm.o | ||
35 | ARCHIVAL-$(CONFIG_TAR) += tar.o | 36 | ARCHIVAL-$(CONFIG_TAR) += tar.o |
36 | ARCHIVAL-$(CONFIG_UNCOMPRESS) += uncompress.o | 37 | ARCHIVAL-$(CONFIG_UNCOMPRESS) += uncompress.o |
37 | ARCHIVAL-$(CONFIG_UNZIP) += unzip.o | 38 | ARCHIVAL-$(CONFIG_UNZIP) += unzip.o |
diff --git a/archival/cpio.c b/archival/cpio.c index 8b4cc2d3e..49f3f88e4 100644 --- a/archival/cpio.c +++ b/archival/cpio.c | |||
@@ -31,12 +31,6 @@ | |||
31 | #include "unarchive.h" | 31 | #include "unarchive.h" |
32 | #include "busybox.h" | 32 | #include "busybox.h" |
33 | 33 | ||
34 | typedef struct hardlinks_s { | ||
35 | file_header_t *entry; | ||
36 | int inode; | ||
37 | struct hardlinks_s *next; | ||
38 | } hardlinks_t; | ||
39 | |||
40 | extern int cpio_main(int argc, char **argv) | 34 | extern int cpio_main(int argc, char **argv) |
41 | { | 35 | { |
42 | archive_handle_t *archive_handle; | 36 | archive_handle_t *archive_handle; |
@@ -83,159 +77,7 @@ extern int cpio_main(int argc, char **argv) | |||
83 | optind++; | 77 | optind++; |
84 | } | 78 | } |
85 | 79 | ||
86 | while (1) { | 80 | while (get_header_cpio(archive_handle) == EXIT_SUCCESS); |
87 | static hardlinks_t *saved_hardlinks = NULL; | ||
88 | static unsigned short pending_hardlinks = 0; | ||
89 | file_header_t *file_header = archive_handle->file_header; | ||
90 | char cpio_header[110]; | ||
91 | int namesize; | ||
92 | char dummy[16]; | ||
93 | int major, minor, nlink, inode; | ||
94 | char extract_flag; | ||
95 | |||
96 | if (pending_hardlinks) { /* Deal with any pending hardlinks */ | ||
97 | hardlinks_t *tmp; | ||
98 | hardlinks_t *oldtmp; | ||
99 | |||
100 | tmp = saved_hardlinks; | ||
101 | oldtmp = NULL; | ||
102 | |||
103 | while (tmp) { | ||
104 | error_msg_and_die("need to fix this\n"); | ||
105 | if (tmp->entry->link_name) { /* Found a hardlink ready to be extracted */ | ||
106 | file_header = tmp->entry; | ||
107 | if (oldtmp) { | ||
108 | oldtmp->next = tmp->next; /* Remove item from linked list */ | ||
109 | } else { | ||
110 | saved_hardlinks = tmp->next; | ||
111 | } | ||
112 | free(tmp); | ||
113 | continue; | ||
114 | } | ||
115 | oldtmp = tmp; | ||
116 | tmp = tmp->next; | ||
117 | } | ||
118 | pending_hardlinks = 0; /* No more pending hardlinks, read next file entry */ | ||
119 | } | ||
120 | |||
121 | /* There can be padding before archive header */ | ||
122 | data_align(archive_handle, 4); | ||
123 | |||
124 | if (archive_xread_all_eof(archive_handle, cpio_header, 110) == 0) { | ||
125 | return(EXIT_FAILURE); | ||
126 | } | ||
127 | archive_handle->offset += 110; | ||
128 | |||
129 | if (strncmp(&cpio_header[0], "07070", 5) != 0) { | ||
130 | printf("cpio header is %x-%x-%x-%x-%x\n", | ||
131 | cpio_header[0], | ||
132 | cpio_header[1], | ||
133 | cpio_header[2], | ||
134 | cpio_header[3], | ||
135 | cpio_header[4]); | ||
136 | error_msg_and_die("Unsupported cpio format"); | ||
137 | } | ||
138 | |||
139 | if ((cpio_header[5] != '1') && (cpio_header[5] != '2')) { | ||
140 | error_msg_and_die("Unsupported cpio format, use newc or crc"); | ||
141 | } | ||
142 | |||
143 | sscanf(cpio_header, "%6c%8x%8x%8x%8x%8x%8lx%8lx%16c%8x%8x%8x%8c", | ||
144 | dummy, &inode, (unsigned int*)&file_header->mode, | ||
145 | (unsigned int*)&file_header->uid, (unsigned int*)&file_header->gid, | ||
146 | &nlink, &file_header->mtime, &file_header->size, | ||
147 | dummy, &major, &minor, &namesize, dummy); | ||
148 | |||
149 | file_header->name = (char *) xmalloc(namesize + 1); | ||
150 | archive_xread_all(archive_handle, file_header->name, namesize); /* Read in filename */ | ||
151 | file_header->name[namesize] = '\0'; | ||
152 | archive_handle->offset += namesize; | ||
153 | |||
154 | /* Update offset amount and skip padding before file contents */ | ||
155 | data_align(archive_handle, 4); | ||
156 | |||
157 | if (strcmp(file_header->name, "TRAILER!!!") == 0) { | ||
158 | printf("%d blocks\n", (int) (archive_handle->offset % 512 ? (archive_handle->offset / 512) + 1 : archive_handle->offset / 512)); /* Always round up */ | ||
159 | if (saved_hardlinks) { /* Bummer - we still have unresolved hardlinks */ | ||
160 | hardlinks_t *tmp = saved_hardlinks; | ||
161 | hardlinks_t *oldtmp = NULL; | ||
162 | while (tmp) { | ||
163 | error_msg("%s not created: cannot resolve hardlink", tmp->entry->name); | ||
164 | oldtmp = tmp; | ||
165 | tmp = tmp->next; | ||
166 | free (oldtmp->entry->name); | ||
167 | free (oldtmp->entry); | ||
168 | free (oldtmp); | ||
169 | } | ||
170 | saved_hardlinks = NULL; | ||
171 | pending_hardlinks = 0; | ||
172 | } | ||
173 | return(EXIT_FAILURE); | ||
174 | } | ||
175 | |||
176 | if (S_ISLNK(file_header->mode)) { | ||
177 | file_header->link_name = (char *) xmalloc(file_header->size + 1); | ||
178 | archive_xread_all(archive_handle, file_header->link_name, file_header->size); | ||
179 | file_header->link_name[file_header->size] = '\0'; | ||
180 | archive_handle->offset += file_header->size; | ||
181 | file_header->size = 0; /* Stop possible seeks in future */ | ||
182 | } | ||
183 | if (nlink > 1 && !S_ISDIR(file_header->mode)) { | ||
184 | if (file_header->size == 0) { /* Put file on a linked list for later */ | ||
185 | hardlinks_t *new = xmalloc(sizeof(hardlinks_t)); | ||
186 | new->next = saved_hardlinks; | ||
187 | new->inode = inode; | ||
188 | new->entry = file_header; | ||
189 | saved_hardlinks = new; | ||
190 | continue; | ||
191 | } else { /* Found the file with data in */ | ||
192 | hardlinks_t *tmp = saved_hardlinks; | ||
193 | pending_hardlinks = 1; | ||
194 | while (tmp) { | ||
195 | if (tmp->inode == inode) { | ||
196 | tmp->entry->link_name = xstrdup(file_header->name); | ||
197 | nlink--; | ||
198 | } | ||
199 | tmp = tmp->next; | ||
200 | } | ||
201 | if (nlink > 1) { | ||
202 | error_msg("error resolving hardlink: did you create the archive with GNU cpio 2.0-2.2?"); | ||
203 | } | ||
204 | } | ||
205 | } | ||
206 | file_header->device = (major << 8) | minor; | ||
207 | |||
208 | extract_flag = FALSE; | ||
209 | if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) { | ||
210 | struct stat statbuf; | ||
211 | |||
212 | extract_flag = TRUE; | ||
213 | |||
214 | /* Check if the file already exists */ | ||
215 | if (lstat (file_header->name, &statbuf) == 0) { | ||
216 | if ((archive_handle->flags & ARCHIVE_EXTRACT_UNCONDITIONAL) || (statbuf.st_mtime < file_header->mtime)) { | ||
217 | /* Remove file if flag set or its older than the file to be extracted */ | ||
218 | if (unlink(file_header->name) == -1) { | ||
219 | perror_msg_and_die("Couldnt remove old file"); | ||
220 | } | ||
221 | } else { | ||
222 | if (! archive_handle->flags & ARCHIVE_EXTRACT_QUIET) { | ||
223 | error_msg("%s not created: newer or same age file exists", file_header->name); | ||
224 | } | ||
225 | extract_flag = FALSE; | ||
226 | } | ||
227 | } | ||
228 | archive_handle->action_header(file_header); | ||
229 | } | ||
230 | |||
231 | archive_handle->action_header(file_header); | ||
232 | if (extract_flag) { | ||
233 | archive_handle->action_data(archive_handle); | ||
234 | } else { | ||
235 | data_skip(archive_handle); | ||
236 | } | ||
237 | archive_handle->offset += file_header->size; | ||
238 | } | ||
239 | 81 | ||
240 | return(EXIT_SUCCESS); | 82 | return(EXIT_SUCCESS); |
241 | } | 83 | } |
diff --git a/archival/libunarchive/Makefile.in b/archival/libunarchive/Makefile.in index 28e2955b9..b59048612 100644 --- a/archival/libunarchive/Makefile.in +++ b/archival/libunarchive/Makefile.in | |||
@@ -35,6 +35,7 @@ LIBUNARCHIVE-y:= \ | |||
35 | filter_accept_reject_list.o \ | 35 | filter_accept_reject_list.o \ |
36 | \ | 36 | \ |
37 | get_header_ar.o \ | 37 | get_header_ar.o \ |
38 | get_header_cpio.o \ | ||
38 | get_header_tar.o \ | 39 | get_header_tar.o \ |
39 | get_header_tar_bz2.o \ | 40 | get_header_tar_bz2.o \ |
40 | get_header_tar_gz.o \ | 41 | get_header_tar_gz.o \ |
diff --git a/archival/libunarchive/data_extract_all.c b/archival/libunarchive/data_extract_all.c index 7ab87e73e..9c4510e6b 100644 --- a/archival/libunarchive/data_extract_all.c +++ b/archival/libunarchive/data_extract_all.c | |||
@@ -43,7 +43,7 @@ extern void data_extract_all(archive_handle_t *archive_handle) | |||
43 | switch(file_header->mode & S_IFMT) { | 43 | switch(file_header->mode & S_IFMT) { |
44 | case S_IFREG: { | 44 | case S_IFREG: { |
45 | #ifdef CONFIG_CPIO | 45 | #ifdef CONFIG_CPIO |
46 | if (file_header->link_name) { | 46 | if (file_header->link_name && file_header->size == 0) { |
47 | /* hard link */ | 47 | /* hard link */ |
48 | res = link(file_header->link_name, file_header->name); | 48 | res = link(file_header->link_name, file_header->name); |
49 | if ((res == -1) && !(archive_handle->flags & ARCHIVE_EXTRACT_QUIET)) { | 49 | if ((res == -1) && !(archive_handle->flags & ARCHIVE_EXTRACT_QUIET)) { |
diff --git a/archival/libunarchive/get_header_cpio.c b/archival/libunarchive/get_header_cpio.c new file mode 100644 index 000000000..87e971588 --- /dev/null +++ b/archival/libunarchive/get_header_cpio.c | |||
@@ -0,0 +1,185 @@ | |||
1 | /* | ||
2 | * This program is free software; you can redistribute it and/or modify | ||
3 | * it under the terms of the GNU General Public License as published by | ||
4 | * the Free Software Foundation; either version 2 of the License, or | ||
5 | * (at your option) any later version. | ||
6 | * | ||
7 | * This program is distributed in the hope that it will be useful, | ||
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
10 | * GNU Library General Public License for more details. | ||
11 | * | ||
12 | * You should have received a copy of the GNU General Public License | ||
13 | * along with this program; if not, write to the Free Software | ||
14 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
15 | */ | ||
16 | |||
17 | #include <stdio.h> | ||
18 | #include <stdlib.h> | ||
19 | #include <string.h> | ||
20 | #include <unistd.h> | ||
21 | #include "unarchive.h" | ||
22 | #include "libbb.h" | ||
23 | |||
24 | typedef struct hardlinks_s { | ||
25 | file_header_t *entry; | ||
26 | int inode; | ||
27 | struct hardlinks_s *next; | ||
28 | } hardlinks_t; | ||
29 | |||
30 | extern char get_header_cpio(archive_handle_t *archive_handle) | ||
31 | { | ||
32 | static hardlinks_t *saved_hardlinks = NULL; | ||
33 | static unsigned short pending_hardlinks = 0; | ||
34 | file_header_t *file_header = archive_handle->file_header; | ||
35 | char cpio_header[110]; | ||
36 | int namesize; | ||
37 | char dummy[16]; | ||
38 | int major, minor, nlink, inode; | ||
39 | char extract_flag; | ||
40 | |||
41 | if (pending_hardlinks) { /* Deal with any pending hardlinks */ | ||
42 | hardlinks_t *tmp; | ||
43 | hardlinks_t *oldtmp; | ||
44 | |||
45 | tmp = saved_hardlinks; | ||
46 | oldtmp = NULL; | ||
47 | |||
48 | while (tmp) { | ||
49 | error_msg_and_die("need to fix this\n"); | ||
50 | if (tmp->entry->link_name) { /* Found a hardlink ready to be extracted */ | ||
51 | file_header = tmp->entry; | ||
52 | if (oldtmp) { | ||
53 | oldtmp->next = tmp->next; /* Remove item from linked list */ | ||
54 | } else { | ||
55 | saved_hardlinks = tmp->next; | ||
56 | } | ||
57 | free(tmp); | ||
58 | continue; | ||
59 | } | ||
60 | oldtmp = tmp; | ||
61 | tmp = tmp->next; | ||
62 | } | ||
63 | pending_hardlinks = 0; /* No more pending hardlinks, read next file entry */ | ||
64 | } | ||
65 | |||
66 | /* There can be padding before archive header */ | ||
67 | data_align(archive_handle, 4); | ||
68 | |||
69 | if (archive_xread_all_eof(archive_handle, cpio_header, 110) == 0) { | ||
70 | return(EXIT_FAILURE); | ||
71 | } | ||
72 | archive_handle->offset += 110; | ||
73 | |||
74 | if (strncmp(&cpio_header[0], "07070", 5) != 0) { | ||
75 | printf("cpio header is %x-%x-%x-%x-%x\n", | ||
76 | cpio_header[0], | ||
77 | cpio_header[1], | ||
78 | cpio_header[2], | ||
79 | cpio_header[3], | ||
80 | cpio_header[4]); | ||
81 | error_msg_and_die("Unsupported cpio format"); | ||
82 | } | ||
83 | |||
84 | if ((cpio_header[5] != '1') && (cpio_header[5] != '2')) { | ||
85 | error_msg_and_die("Unsupported cpio format, use newc or crc"); | ||
86 | } | ||
87 | |||
88 | sscanf(cpio_header, "%6c%8x%8x%8x%8x%8x%8lx%8lx%16c%8x%8x%8x%8c", | ||
89 | dummy, &inode, (unsigned int*)&file_header->mode, | ||
90 | (unsigned int*)&file_header->uid, (unsigned int*)&file_header->gid, | ||
91 | &nlink, &file_header->mtime, &file_header->size, | ||
92 | dummy, &major, &minor, &namesize, dummy); | ||
93 | |||
94 | file_header->name = (char *) xmalloc(namesize + 1); | ||
95 | archive_xread_all(archive_handle, file_header->name, namesize); /* Read in filename */ | ||
96 | file_header->name[namesize] = '\0'; | ||
97 | archive_handle->offset += namesize; | ||
98 | |||
99 | /* Update offset amount and skip padding before file contents */ | ||
100 | data_align(archive_handle, 4); | ||
101 | |||
102 | if (strcmp(file_header->name, "TRAILER!!!") == 0) { | ||
103 | printf("%d blocks\n", (int) (archive_handle->offset % 512 ? (archive_handle->offset / 512) + 1 : archive_handle->offset / 512)); /* Always round up */ | ||
104 | if (saved_hardlinks) { /* Bummer - we still have unresolved hardlinks */ | ||
105 | hardlinks_t *tmp = saved_hardlinks; | ||
106 | hardlinks_t *oldtmp = NULL; | ||
107 | while (tmp) { | ||
108 | error_msg("%s not created: cannot resolve hardlink", tmp->entry->name); | ||
109 | oldtmp = tmp; | ||
110 | tmp = tmp->next; | ||
111 | free (oldtmp->entry->name); | ||
112 | free (oldtmp->entry); | ||
113 | free (oldtmp); | ||
114 | } | ||
115 | saved_hardlinks = NULL; | ||
116 | pending_hardlinks = 0; | ||
117 | } | ||
118 | return(EXIT_FAILURE); | ||
119 | } | ||
120 | |||
121 | if (S_ISLNK(file_header->mode)) { | ||
122 | file_header->link_name = (char *) xmalloc(file_header->size + 1); | ||
123 | archive_xread_all(archive_handle, file_header->link_name, file_header->size); | ||
124 | file_header->link_name[file_header->size] = '\0'; | ||
125 | archive_handle->offset += file_header->size; | ||
126 | file_header->size = 0; /* Stop possible seeks in future */ | ||
127 | } | ||
128 | if (nlink > 1 && !S_ISDIR(file_header->mode)) { | ||
129 | if (file_header->size == 0) { /* Put file on a linked list for later */ | ||
130 | hardlinks_t *new = xmalloc(sizeof(hardlinks_t)); | ||
131 | new->next = saved_hardlinks; | ||
132 | new->inode = inode; | ||
133 | new->entry = file_header; | ||
134 | saved_hardlinks = new; | ||
135 | return(EXIT_SUCCESS); // Skip this one | ||
136 | } else { /* Found the file with data in */ | ||
137 | hardlinks_t *tmp = saved_hardlinks; | ||
138 | pending_hardlinks = 1; | ||
139 | while (tmp) { | ||
140 | if (tmp->inode == inode) { | ||
141 | tmp->entry->link_name = xstrdup(file_header->name); | ||
142 | nlink--; | ||
143 | } | ||
144 | tmp = tmp->next; | ||
145 | } | ||
146 | if (nlink > 1) { | ||
147 | error_msg("error resolving hardlink: did you create the archive with GNU cpio 2.0-2.2?"); | ||
148 | } | ||
149 | } | ||
150 | } | ||
151 | file_header->device = (major << 8) | minor; | ||
152 | |||
153 | extract_flag = FALSE; | ||
154 | if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) { | ||
155 | struct stat statbuf; | ||
156 | |||
157 | extract_flag = TRUE; | ||
158 | |||
159 | /* Check if the file already exists */ | ||
160 | if (lstat (file_header->name, &statbuf) == 0) { | ||
161 | if ((archive_handle->flags & ARCHIVE_EXTRACT_UNCONDITIONAL) || (statbuf.st_mtime < file_header->mtime)) { | ||
162 | /* Remove file if flag set or its older than the file to be extracted */ | ||
163 | if (unlink(file_header->name) == -1) { | ||
164 | perror_msg_and_die("Couldnt remove old file"); | ||
165 | } | ||
166 | } else { | ||
167 | if (! archive_handle->flags & ARCHIVE_EXTRACT_QUIET) { | ||
168 | error_msg("%s not created: newer or same age file exists", file_header->name); | ||
169 | } | ||
170 | extract_flag = FALSE; | ||
171 | } | ||
172 | } | ||
173 | archive_handle->action_header(file_header); | ||
174 | } | ||
175 | |||
176 | archive_handle->action_header(file_header); | ||
177 | if (extract_flag) { | ||
178 | archive_handle->action_data(archive_handle); | ||
179 | } else { | ||
180 | data_skip(archive_handle); | ||
181 | } | ||
182 | archive_handle->offset += file_header->size; | ||
183 | return (EXIT_SUCCESS); | ||
184 | } | ||
185 | |||
diff --git a/archival/rpm.c b/archival/rpm.c new file mode 100644 index 000000000..ee5087f0a --- /dev/null +++ b/archival/rpm.c | |||
@@ -0,0 +1,351 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Mini rpm applet for busybox | ||
4 | * | ||
5 | * Copyright (C) 2001,2002 by Laurence Anderson | ||
6 | * | ||
7 | * This program is free software; you can redistribute it and/or modify | ||
8 | * it under the terms of the GNU General Public License as published by | ||
9 | * the Free Software Foundation; either version 2 of the License, or | ||
10 | * (at your option) any later version. | ||
11 | * | ||
12 | * This program is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
15 | * General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with this program; if not, write to the Free Software | ||
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
20 | */ | ||
21 | |||
22 | #include "busybox.h" | ||
23 | #include "unarchive.h" | ||
24 | #include <stdio.h> | ||
25 | #include <unistd.h> | ||
26 | #include <signal.h> | ||
27 | #include <stdlib.h> | ||
28 | #include <fcntl.h> | ||
29 | #include <netinet/in.h> /* For ntohl & htonl function */ | ||
30 | #include <string.h> /* For strncmp */ | ||
31 | #include <sys/mman.h> /* For mmap */ | ||
32 | #include <time.h> /* For ctime */ | ||
33 | |||
34 | #define RPM_HEADER_MAGIC "\216\255\350" | ||
35 | #define RPM_CHAR_TYPE 1 | ||
36 | #define RPM_INT8_TYPE 2 | ||
37 | #define RPM_INT16_TYPE 3 | ||
38 | #define RPM_INT32_TYPE 4 | ||
39 | /* #define RPM_INT64_TYPE 5 ---- These aren't supported (yet) */ | ||
40 | #define RPM_STRING_TYPE 6 | ||
41 | #define RPM_BIN_TYPE 7 | ||
42 | #define RPM_STRING_ARRAY_TYPE 8 | ||
43 | #define RPM_I18NSTRING_TYPE 9 | ||
44 | |||
45 | #define RPMTAG_NAME 1000 | ||
46 | #define RPMTAG_VERSION 1001 | ||
47 | #define RPMTAG_RELEASE 1002 | ||
48 | #define RPMTAG_SUMMARY 1004 | ||
49 | #define RPMTAG_DESCRIPTION 1005 | ||
50 | #define RPMTAG_BUILDTIME 1006 | ||
51 | #define RPMTAG_BUILDHOST 1007 | ||
52 | #define RPMTAG_SIZE 1009 | ||
53 | #define RPMTAG_VENDOR 1011 | ||
54 | #define RPMTAG_LICENSE 1014 | ||
55 | #define RPMTAG_PACKAGER 1015 | ||
56 | #define RPMTAG_GROUP 1016 | ||
57 | #define RPMTAG_URL 1020 | ||
58 | #define RPMTAG_PREIN 1023 | ||
59 | #define RPMTAG_POSTIN 1024 | ||
60 | #define RPMTAG_FILEFLAGS 1037 | ||
61 | #define RPMTAG_FILEUSERNAME 1039 | ||
62 | #define RPMTAG_FILEGROUPNAME 1040 | ||
63 | #define RPMTAG_SOURCERPM 1044 | ||
64 | #define RPMTAG_PREINPROG 1085 | ||
65 | #define RPMTAG_POSTINPROG 1086 | ||
66 | #define RPMTAG_PREFIXS 1098 | ||
67 | #define RPMTAG_DIRINDEXES 1116 | ||
68 | #define RPMTAG_BASENAMES 1117 | ||
69 | #define RPMTAG_DIRNAMES 1118 | ||
70 | #define RPMFILE_CONFIG (1 << 0) | ||
71 | #define RPMFILE_DOC (1 << 1) | ||
72 | |||
73 | enum rpm_functions_e { | ||
74 | rpm_query = 1, | ||
75 | rpm_install = 2, | ||
76 | rpm_query_info = 4, | ||
77 | rpm_query_package = 8, | ||
78 | rpm_query_list = 16, | ||
79 | rpm_query_list_doc = 32, | ||
80 | rpm_query_list_config = 64 | ||
81 | }; | ||
82 | |||
83 | typedef struct { | ||
84 | u_int32_t tag; /* 4 byte tag */ | ||
85 | u_int32_t type; /* 4 byte type */ | ||
86 | u_int32_t offset; /* 4 byte offset */ | ||
87 | u_int32_t count; /* 4 byte count */ | ||
88 | } rpm_index; | ||
89 | |||
90 | static void *map; | ||
91 | static rpm_index **mytags; | ||
92 | static int tagcount; | ||
93 | |||
94 | void extract_cpio_gz(int fd); | ||
95 | rpm_index **rpm_gettags(int fd, int *num_tags); | ||
96 | int bsearch_rpmtag(const void *key, const void *item); | ||
97 | char *rpm_getstring(int tag, int itemindex); | ||
98 | int rpm_getint(int tag, int itemindex); | ||
99 | int rpm_getcount(int tag); | ||
100 | void exec_script(int progtag, int datatag, char *prefix); | ||
101 | void fileaction_dobackup(char *filename, int fileref); | ||
102 | void fileaction_setowngrp(char *filename, int fileref); | ||
103 | void fileaction_list(char *filename, int itemno); | ||
104 | void loop_through_files(int filetag, void (*fileaction)(char *filename, int fileref)); | ||
105 | |||
106 | int rpm_main(int argc, char **argv) | ||
107 | { | ||
108 | int opt = 0, func = 0, rpm_fd, offset; | ||
109 | |||
110 | while ((opt = getopt(argc, argv, "iqpldc")) != -1) { | ||
111 | switch (opt) { | ||
112 | case 'i': // First arg: Install mode, with q: Information | ||
113 | if (!func) func |= rpm_install; | ||
114 | else func |= rpm_query_info; | ||
115 | break; | ||
116 | case 'q': // First arg: Query mode | ||
117 | if (!func) func |= rpm_query; | ||
118 | else show_usage(); | ||
119 | break; | ||
120 | case 'p': // Query a package | ||
121 | func |= rpm_query_package; | ||
122 | break; | ||
123 | case 'l': // List files in a package | ||
124 | func |= rpm_query_list; | ||
125 | break; | ||
126 | case 'd': // List doc files in a package (implies list) | ||
127 | func |= rpm_query_list; | ||
128 | func |= rpm_query_list_doc; | ||
129 | break; | ||
130 | case 'c': // List config files in a package (implies list) | ||
131 | func |= rpm_query_list; | ||
132 | func |= rpm_query_list_config; | ||
133 | break; | ||
134 | default: | ||
135 | show_usage(); | ||
136 | } | ||
137 | } | ||
138 | |||
139 | if (optind == argc) show_usage(); | ||
140 | while (optind < argc) { | ||
141 | rpm_fd = xopen(argv[optind], O_RDONLY); | ||
142 | mytags = rpm_gettags(rpm_fd, (int *) &tagcount); | ||
143 | offset = lseek(rpm_fd, 0, SEEK_CUR); | ||
144 | if (!mytags) { printf("Error reading rpm header\n"); exit(-1); } | ||
145 | map = mmap(0, offset > getpagesize() ? (offset + offset % getpagesize()) : getpagesize(), PROT_READ, MAP_SHARED, rpm_fd, 0); // Mimimum is one page | ||
146 | if (func & rpm_install) { | ||
147 | loop_through_files(RPMTAG_BASENAMES, fileaction_dobackup); /* Backup any config files */ | ||
148 | extract_cpio_gz(rpm_fd); // Extact the archive | ||
149 | loop_through_files(RPMTAG_BASENAMES, fileaction_setowngrp); /* Set the correct file uid/gid's */ | ||
150 | } else if (func & rpm_query && func & rpm_query_package) { | ||
151 | if (!((func & rpm_query_info) || (func & rpm_query_list))) { // If just a straight query, just give package name | ||
152 | printf("%s-%s-%s\n", rpm_getstring(RPMTAG_NAME, 0), rpm_getstring(RPMTAG_VERSION, 0), rpm_getstring(RPMTAG_RELEASE, 0)); | ||
153 | } | ||
154 | if (func & rpm_query_info) { | ||
155 | /* Do the nice printout */ | ||
156 | time_t bdate_time; | ||
157 | struct tm *bdate; | ||
158 | char bdatestring[50]; | ||
159 | printf("Name : %-29sRelocations: %s\n", rpm_getstring(RPMTAG_NAME, 0), rpm_getstring(RPMTAG_PREFIXS, 0) ? rpm_getstring(RPMTAG_PREFIXS, 0) : "(not relocateable)"); | ||
160 | printf("Version : %-34sVendor: %s\n", rpm_getstring(RPMTAG_VERSION, 0), rpm_getstring(RPMTAG_VENDOR, 0) ? rpm_getstring(RPMTAG_VENDOR, 0) : "(none)"); | ||
161 | bdate_time = rpm_getint(RPMTAG_BUILDTIME, 0); | ||
162 | bdate = localtime((time_t *) &bdate_time); | ||
163 | strftime(bdatestring, 50, "%a %d %b %Y %T %Z", bdate); | ||
164 | printf("Release : %-30sBuild Date: %s\n", rpm_getstring(RPMTAG_RELEASE, 0), bdatestring); | ||
165 | printf("Install date: %-30sBuild Host: %s\n", "(not installed)", rpm_getstring(RPMTAG_BUILDHOST, 0)); | ||
166 | printf("Group : %-30sSource RPM: %s\n", rpm_getstring(RPMTAG_GROUP, 0), rpm_getstring(RPMTAG_SOURCERPM, 0)); | ||
167 | printf("Size : %-33dLicense: %s\n", rpm_getint(RPMTAG_SIZE, 0), rpm_getstring(RPMTAG_LICENSE, 0)); | ||
168 | printf("URL : %s\n", rpm_getstring(RPMTAG_URL, 0)); | ||
169 | printf("Summary : %s\n", rpm_getstring(RPMTAG_SUMMARY, 0)); | ||
170 | printf("Description :\n%s\n", rpm_getstring(RPMTAG_DESCRIPTION, 0)); | ||
171 | } | ||
172 | if (func & rpm_query_list) { | ||
173 | int count, it, flags; | ||
174 | count = rpm_getcount(RPMTAG_BASENAMES); | ||
175 | for (it = 0; it < count; it++) { | ||
176 | flags = rpm_getint(RPMTAG_FILEFLAGS, it); | ||
177 | switch ((func & rpm_query_list_doc) + (func & rpm_query_list_config)) | ||
178 | { | ||
179 | case rpm_query_list_doc: if (!(flags & RPMFILE_DOC)) continue; break; | ||
180 | case rpm_query_list_config: if (!(flags & RPMFILE_CONFIG)) continue; break; | ||
181 | case rpm_query_list_doc + rpm_query_list_config: if (!((flags & RPMFILE_CONFIG) || (flags & RPMFILE_DOC))) continue; break; | ||
182 | } | ||
183 | printf("%s%s\n", rpm_getstring(RPMTAG_DIRNAMES, rpm_getint(RPMTAG_DIRINDEXES, it)), rpm_getstring(RPMTAG_BASENAMES, it)); | ||
184 | } | ||
185 | } | ||
186 | } | ||
187 | optind++; | ||
188 | free (mytags); | ||
189 | } | ||
190 | return 0; | ||
191 | } | ||
192 | |||
193 | void extract_cpio_gz(int fd) { | ||
194 | archive_handle_t *archive_handle; | ||
195 | unsigned char magic[2]; | ||
196 | |||
197 | /* Initialise */ | ||
198 | archive_handle = init_handle(); | ||
199 | archive_handle->read = read_gz; | ||
200 | archive_handle->seek = seek_by_char; | ||
201 | //archive_handle->action_header = header_list; | ||
202 | archive_handle->action_data = data_extract_all; | ||
203 | archive_handle->flags |= ARCHIVE_PRESERVE_DATE; | ||
204 | archive_handle->flags |= ARCHIVE_CREATE_LEADING_DIRS; | ||
205 | archive_handle->src_fd = fd; | ||
206 | archive_handle->offset = 0; | ||
207 | |||
208 | xread_all(archive_handle->src_fd, &magic, 2); | ||
209 | if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) { | ||
210 | error_msg_and_die("Invalid gzip magic"); | ||
211 | } | ||
212 | check_header_gzip(archive_handle->src_fd); | ||
213 | chdir("/"); // Install RPM's to root | ||
214 | |||
215 | GZ_gzReadOpen(archive_handle->src_fd, 0, 0); | ||
216 | while (get_header_cpio(archive_handle) == EXIT_SUCCESS); | ||
217 | GZ_gzReadClose(); | ||
218 | |||
219 | check_trailer_gzip(archive_handle->src_fd); | ||
220 | } | ||
221 | |||
222 | |||
223 | rpm_index **rpm_gettags(int fd, int *num_tags) | ||
224 | { | ||
225 | rpm_index **tags = calloc(200, sizeof(struct rpmtag *)); /* We should never need mode than 200, and realloc later */ | ||
226 | int pass, tagindex = 0; | ||
227 | lseek(fd, 96, SEEK_CUR); /* Seek past the unused lead */ | ||
228 | |||
229 | for (pass = 0; pass < 2; pass++) { /* 1st pass is the signature headers, 2nd is the main stuff */ | ||
230 | struct { | ||
231 | char magic[3]; /* 3 byte magic: 0x8e 0xad 0xe8 */ | ||
232 | u_int8_t version; /* 1 byte version number */ | ||
233 | u_int32_t reserved; /* 4 bytes reserved */ | ||
234 | u_int32_t entries; /* Number of entries in header (4 bytes) */ | ||
235 | u_int32_t size; /* Size of store (4 bytes) */ | ||
236 | } header; | ||
237 | rpm_index *tmpindex; | ||
238 | int storepos; | ||
239 | |||
240 | read(fd, &header, sizeof(header)); | ||
241 | if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC, 3) != 0) return NULL; /* Invalid magic */ | ||
242 | if (header.version != 1) return NULL; /* This program only supports v1 headers */ | ||
243 | header.size = ntohl(header.size); | ||
244 | header.entries = ntohl(header.entries); | ||
245 | storepos = lseek(fd,0,SEEK_CUR) + header.entries * 16; | ||
246 | |||
247 | while (header.entries--) { | ||
248 | tmpindex = tags[tagindex++] = malloc(sizeof(rpm_index)); | ||
249 | read(fd, tmpindex, sizeof(rpm_index)); | ||
250 | tmpindex->tag = ntohl(tmpindex->tag); tmpindex->type = ntohl(tmpindex->type); tmpindex->count = ntohl(tmpindex->count); | ||
251 | tmpindex->offset = storepos + ntohl(tmpindex->offset); | ||
252 | if (pass==0) tmpindex->tag -= 743; | ||
253 | } | ||
254 | lseek(fd, header.size, SEEK_CUR); /* Seek past store */ | ||
255 | 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 */ | ||
256 | } | ||
257 | tags = realloc(tags, tagindex * sizeof(struct rpmtag *)); /* realloc tags to save space */ | ||
258 | *num_tags = tagindex; | ||
259 | return tags; /* All done, leave the file at the start of the gzipped cpio archive */ | ||
260 | } | ||
261 | |||
262 | int bsearch_rpmtag(const void *key, const void *item) | ||
263 | { | ||
264 | rpm_index **tmp = (rpm_index **) item; | ||
265 | return ((int) key - tmp[0]->tag); | ||
266 | } | ||
267 | |||
268 | int rpm_getcount(int tag) | ||
269 | { | ||
270 | rpm_index **found; | ||
271 | found = bsearch((void *) tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag); | ||
272 | if (!found) return 0; | ||
273 | else return found[0]->count; | ||
274 | } | ||
275 | |||
276 | char *rpm_getstring(int tag, int itemindex) | ||
277 | { | ||
278 | rpm_index **found; | ||
279 | found = bsearch((void *) tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag); | ||
280 | if (!found || itemindex >= found[0]->count) return NULL; | ||
281 | if (found[0]->type == RPM_STRING_TYPE || found[0]->type == RPM_I18NSTRING_TYPE || found[0]->type == RPM_STRING_ARRAY_TYPE) { | ||
282 | int n; | ||
283 | char *tmpstr = (char *) (map + found[0]->offset); | ||
284 | for (n=0; n < itemindex; n++) tmpstr = tmpstr + strlen(tmpstr) + 1; | ||
285 | return tmpstr; | ||
286 | } else return NULL; | ||
287 | } | ||
288 | |||
289 | int rpm_getint(int tag, int itemindex) | ||
290 | { | ||
291 | rpm_index **found; | ||
292 | int n, *tmpint; | ||
293 | found = bsearch((void *) tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag); | ||
294 | if (!found || itemindex >= found[0]->count) return -1; | ||
295 | tmpint = (int *) (map + found[0]->offset); | ||
296 | if (found[0]->type == RPM_INT32_TYPE) { | ||
297 | for (n=0; n<itemindex; n++) tmpint = (int *) ((void *) tmpint + 4); | ||
298 | return ntohl(*tmpint); | ||
299 | } else if (found[0]->type == RPM_INT16_TYPE) { | ||
300 | for (n=0; n<itemindex; n++) tmpint = (int *) ((void *) tmpint + 2); | ||
301 | return ntohs(*tmpint); | ||
302 | } else if (found[0]->type == RPM_INT8_TYPE) { | ||
303 | for (n=0; n<itemindex; n++) tmpint = (int *) ((void *) tmpint + 1); | ||
304 | return ntohs(*tmpint); | ||
305 | } else return -1; | ||
306 | } | ||
307 | |||
308 | void fileaction_dobackup(char *filename, int fileref) | ||
309 | { | ||
310 | struct stat oldfile; | ||
311 | int stat_res; | ||
312 | char *newname; | ||
313 | if (rpm_getint(RPMTAG_FILEFLAGS, fileref) & RPMFILE_CONFIG) { /* Only need to backup config files */ | ||
314 | stat_res = lstat (filename, &oldfile); | ||
315 | if (stat_res == 0 && S_ISREG(oldfile.st_mode)) { /* File already exists - really should check MD5's etc to see if different */ | ||
316 | newname = xstrdup(filename); | ||
317 | newname = strcat(newname, ".rpmorig"); | ||
318 | copy_file(filename, newname, FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS); | ||
319 | remove_file(filename, FILEUTILS_RECUR | FILEUTILS_FORCE); | ||
320 | free(newname); | ||
321 | } | ||
322 | } | ||
323 | } | ||
324 | |||
325 | void fileaction_setowngrp(char *filename, int fileref) | ||
326 | { | ||
327 | int uid, gid; | ||
328 | uid = my_getpwnam(rpm_getstring(RPMTAG_FILEUSERNAME, fileref)); | ||
329 | gid = my_getgrnam(rpm_getstring(RPMTAG_FILEGROUPNAME, fileref)); | ||
330 | chown (filename, uid, gid); | ||
331 | } | ||
332 | |||
333 | void fileaction_list(char *filename, int fileref) | ||
334 | { | ||
335 | printf("%s\n", filename); | ||
336 | } | ||
337 | |||
338 | void loop_through_files(int filetag, void (*fileaction)(char *filename, int fileref)) | ||
339 | { | ||
340 | int count = 0; | ||
341 | char *filename, *tmp_dirname, *tmp_basename; | ||
342 | while (rpm_getstring(filetag, count)) { | ||
343 | tmp_dirname = rpm_getstring(RPMTAG_DIRNAMES, rpm_getint(RPMTAG_DIRINDEXES, count)); /* 1st put on the directory */ | ||
344 | tmp_basename = rpm_getstring(RPMTAG_BASENAMES, count); | ||
345 | filename = xmalloc(strlen(tmp_basename) + strlen(tmp_dirname) + 1); | ||
346 | strcpy(filename, tmp_dirname); /* First the directory name */ | ||
347 | strcat(filename, tmp_basename); /* then the filename */ | ||
348 | fileaction(filename, count++); | ||
349 | free(filename); | ||
350 | } | ||
351 | } | ||
diff --git a/include/applets.h b/include/applets.h index b7171f7a1..9257fc940 100644 --- a/include/applets.h +++ b/include/applets.h | |||
@@ -440,6 +440,9 @@ | |||
440 | #ifdef CONFIG_ROUTE | 440 | #ifdef CONFIG_ROUTE |
441 | APPLET(route, route_main, _BB_DIR_SBIN, _BB_SUID_NEVER) | 441 | APPLET(route, route_main, _BB_DIR_SBIN, _BB_SUID_NEVER) |
442 | #endif | 442 | #endif |
443 | #ifdef CONFIG_RPM | ||
444 | APPLET(rpm, rpm_main, _BB_DIR_BIN, _BB_SUID_NEVER) | ||
445 | #endif | ||
443 | #ifdef CONFIG_RPM2CPIO | 446 | #ifdef CONFIG_RPM2CPIO |
444 | APPLET(rpm2cpio, rpm2cpio_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER) | 447 | APPLET(rpm2cpio, rpm2cpio_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER) |
445 | #endif | 448 | #endif |
diff --git a/include/unarchive.h b/include/unarchive.h index aca685915..8396f878c 100644 --- a/include/unarchive.h +++ b/include/unarchive.h | |||
@@ -85,6 +85,7 @@ extern void check_header_gzip(int src_fd); | |||
85 | extern void check_trailer_gzip(int src_fd); | 85 | extern void check_trailer_gzip(int src_fd); |
86 | 86 | ||
87 | extern char get_header_ar(archive_handle_t *archive_handle); | 87 | extern char get_header_ar(archive_handle_t *archive_handle); |
88 | extern char get_header_cpio(archive_handle_t *archive_handle); | ||
88 | extern char get_header_tar(archive_handle_t *archive_handle); | 89 | extern char get_header_tar(archive_handle_t *archive_handle); |
89 | extern char get_header_tar_bz2(archive_handle_t *archive_handle); | 90 | extern char get_header_tar_bz2(archive_handle_t *archive_handle); |
90 | extern char get_header_tar_gz(archive_handle_t *archive_handle); | 91 | extern char get_header_tar_gz(archive_handle_t *archive_handle); |
diff --git a/include/usage.h b/include/usage.h index 17fd4073a..5b357a218 100644 --- a/include/usage.h +++ b/include/usage.h | |||
@@ -1696,6 +1696,19 @@ | |||
1696 | "\t-n\tDont resolve names.\n" \ | 1696 | "\t-n\tDont resolve names.\n" \ |
1697 | "\t-e\tDisplay other/more information" | 1697 | "\t-e\tDisplay other/more information" |
1698 | 1698 | ||
1699 | #define rpm_trivial_usage \ | ||
1700 | "-i -q[ildc]p package.rpm" | ||
1701 | #define rpm_full_usage \ | ||
1702 | "Manipulates RPM packages" \ | ||
1703 | "Options:" \ | ||
1704 | "\t-i Install package" \ | ||
1705 | "\t-q Query package" \ | ||
1706 | "\t\t-p Query uninstalled package" \ | ||
1707 | "\t\t-i Show information" \ | ||
1708 | "\t\t-l List contents" \ | ||
1709 | "\t\t-d List documents" \ | ||
1710 | "\t\t-c List config files" | ||
1711 | |||
1699 | #define rpm2cpio_trivial_usage \ | 1712 | #define rpm2cpio_trivial_usage \ |
1700 | "package.rpm" | 1713 | "package.rpm" |
1701 | #define rpm2cpio_full_usage \ | 1714 | #define rpm2cpio_full_usage \ |