Glenn L McGrath | 58a4085 | 2001-01-02 23:49:26 +0000 | [diff] [blame] | 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 | |
Glenn L McGrath | bc9afad | 2001-02-11 03:32:41 +0000 | [diff] [blame] | 17 | /* |
| 18 | * Merge this applet into dpkg when dpkg becomes more stable |
| 19 | */ |
| 20 | |
Glenn L McGrath | 58a4085 | 2001-01-02 23:49:26 +0000 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <fcntl.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 24 | #include <unistd.h> |
| 25 | #include <string.h> |
| 26 | #include <stdlib.h> |
Glenn L McGrath | f608da4 | 2001-04-07 02:40:59 +0000 | [diff] [blame^] | 27 | #include <signal.h> |
Glenn L McGrath | 58a4085 | 2001-01-02 23:49:26 +0000 | [diff] [blame] | 28 | #include "busybox.h" |
| 29 | |
Eric Andersen | fdefbbb | 2001-04-05 06:04:11 +0000 | [diff] [blame] | 30 | /* From gunzip.c */ |
| 31 | extern int gz_open(FILE *compressed_file, int *pid); |
| 32 | extern void gz_close(int gunzip_pid); |
| 33 | |
Glenn L McGrath | 58a4085 | 2001-01-02 23:49:26 +0000 | [diff] [blame] | 34 | typedef struct ar_headers_s { |
| 35 | char *name; |
| 36 | size_t size; |
| 37 | uid_t uid; |
| 38 | gid_t gid; |
| 39 | mode_t mode; |
| 40 | time_t mtime; |
| 41 | off_t offset; |
| 42 | struct ar_headers_s *next; |
| 43 | } ar_headers_t; |
| 44 | |
Glenn L McGrath | bc9afad | 2001-02-11 03:32:41 +0000 | [diff] [blame] | 45 | extern ar_headers_t get_ar_headers(int srcFd); |
Glenn L McGrath | 58a4085 | 2001-01-02 23:49:26 +0000 | [diff] [blame] | 46 | extern int tar_unzip_init(int tarFd); |
| 47 | extern int readTarFile(int tarFd, int extractFlag, int listFlag, |
Glenn L McGrath | bc9afad | 2001-02-11 03:32:41 +0000 | [diff] [blame] | 48 | int tostdoutFlag, int verboseFlag, char** extractList, char** excludeList); |
Glenn L McGrath | 58a4085 | 2001-01-02 23:49:26 +0000 | [diff] [blame] | 49 | |
Glenn L McGrath | 821fbf0 | 2001-02-12 11:16:26 +0000 | [diff] [blame] | 50 | static const int dpkg_deb_contents = 1; |
| 51 | static const int dpkg_deb_control = 2; |
Glenn L McGrath | 59c09d0 | 2001-01-03 01:44:49 +0000 | [diff] [blame] | 52 | // const int dpkg_deb_info = 4; |
Glenn L McGrath | 821fbf0 | 2001-02-12 11:16:26 +0000 | [diff] [blame] | 53 | static const int dpkg_deb_extract = 8; |
| 54 | static const int dpkg_deb_verbose_extract = 16; |
| 55 | static const int dpkg_deb_list = 32; |
Glenn L McGrath | bc9afad | 2001-02-11 03:32:41 +0000 | [diff] [blame] | 56 | |
| 57 | extern int deb_extract(int optflags, const char *dir_name, const char *deb_filename) |
| 58 | { |
| 59 | char **extract_list = NULL; |
| 60 | ar_headers_t *ar_headers = NULL; |
| 61 | char ar_filename[15]; |
Glenn L McGrath | 58a4085 | 2001-01-02 23:49:26 +0000 | [diff] [blame] | 62 | int extract_flag = FALSE; |
| 63 | int list_flag = FALSE; |
| 64 | int verbose_flag = FALSE; |
| 65 | int extract_to_stdout = FALSE; |
Glenn L McGrath | bc9afad | 2001-02-11 03:32:41 +0000 | [diff] [blame] | 66 | int srcFd = 0; |
| 67 | int status; |
Eric Andersen | fdefbbb | 2001-04-05 06:04:11 +0000 | [diff] [blame] | 68 | pid_t pid; |
| 69 | FILE *comp_file = NULL; |
Glenn L McGrath | bc9afad | 2001-02-11 03:32:41 +0000 | [diff] [blame] | 70 | |
| 71 | if (dpkg_deb_contents == (dpkg_deb_contents & optflags)) { |
| 72 | strcpy(ar_filename, "data.tar.gz"); |
| 73 | verbose_flag = TRUE; |
| 74 | list_flag = TRUE; |
| 75 | } |
Glenn L McGrath | 821fbf0 | 2001-02-12 11:16:26 +0000 | [diff] [blame] | 76 | if (dpkg_deb_list == (dpkg_deb_list & optflags)) { |
| 77 | strcpy(ar_filename, "data.tar.gz"); |
| 78 | list_flag = TRUE; |
| 79 | } |
Glenn L McGrath | bc9afad | 2001-02-11 03:32:41 +0000 | [diff] [blame] | 80 | if (dpkg_deb_control == (dpkg_deb_control & optflags)) { |
| 81 | strcpy(ar_filename, "control.tar.gz"); |
| 82 | extract_flag = TRUE; |
| 83 | } |
| 84 | if (dpkg_deb_extract == (dpkg_deb_extract & optflags)) { |
| 85 | strcpy(ar_filename, "data.tar.gz"); |
| 86 | extract_flag = TRUE; |
| 87 | } |
| 88 | if (dpkg_deb_verbose_extract == (dpkg_deb_verbose_extract & optflags)) { |
| 89 | strcpy(ar_filename, "data.tar.gz"); |
| 90 | extract_flag = TRUE; |
| 91 | list_flag = TRUE; |
| 92 | } |
| 93 | |
| 94 | ar_headers = (ar_headers_t *) xmalloc(sizeof(ar_headers_t)); |
| 95 | srcFd = open(deb_filename, O_RDONLY); |
| 96 | |
| 97 | *ar_headers = get_ar_headers(srcFd); |
| 98 | if (ar_headers->next == NULL) { |
| 99 | error_msg_and_die("Couldnt find %s in %s", ar_filename, deb_filename); |
| 100 | } |
| 101 | |
| 102 | while (ar_headers->next != NULL) { |
| 103 | if (strcmp(ar_headers->name, ar_filename) == 0) { |
| 104 | break; |
| 105 | } |
| 106 | ar_headers = ar_headers->next; |
| 107 | } |
| 108 | lseek(srcFd, ar_headers->offset, SEEK_SET); |
Eric Andersen | fdefbbb | 2001-04-05 06:04:11 +0000 | [diff] [blame] | 109 | /* Uncompress the file */ |
| 110 | comp_file = fdopen(srcFd, "r"); |
| 111 | if ((srcFd = gz_open(comp_file, &pid)) == EXIT_FAILURE) { |
| 112 | error_msg_and_die("Couldnt unzip file"); |
| 113 | } |
Glenn L McGrath | bc9afad | 2001-02-11 03:32:41 +0000 | [diff] [blame] | 114 | if ( dir_name != NULL) { |
| 115 | if (is_directory(dir_name, TRUE, NULL)==FALSE) { |
| 116 | mkdir(dir_name, 0755); |
| 117 | } |
| 118 | if (chdir(dir_name)==-1) { |
| 119 | error_msg_and_die("Cannot change to dir %s", dir_name); |
| 120 | } |
| 121 | } |
Eric Andersen | fdefbbb | 2001-04-05 06:04:11 +0000 | [diff] [blame] | 122 | status = readTarFile(srcFd, extract_flag, list_flag, |
| 123 | extract_to_stdout, verbose_flag, NULL, extract_list); |
Glenn L McGrath | f608da4 | 2001-04-07 02:40:59 +0000 | [diff] [blame^] | 124 | |
| 125 | /* we are deliberately terminating the child so we can safely ignore this */ |
| 126 | signal(SIGTERM, SIG_IGN); |
Eric Andersen | fdefbbb | 2001-04-05 06:04:11 +0000 | [diff] [blame] | 127 | gz_close(pid); |
Glenn L McGrath | f608da4 | 2001-04-07 02:40:59 +0000 | [diff] [blame^] | 128 | close(srcFd); |
Eric Andersen | fdefbbb | 2001-04-05 06:04:11 +0000 | [diff] [blame] | 129 | fclose(comp_file); |
Glenn L McGrath | bc9afad | 2001-02-11 03:32:41 +0000 | [diff] [blame] | 130 | |
| 131 | return status; |
| 132 | } |
| 133 | |
| 134 | extern int dpkg_deb_main(int argc, char **argv) |
| 135 | { |
| 136 | char *target_dir = NULL; |
| 137 | int opt = 0; |
| 138 | int optflag = 0; |
Glenn L McGrath | 58a4085 | 2001-01-02 23:49:26 +0000 | [diff] [blame] | 139 | |
Glenn L McGrath | 821fbf0 | 2001-02-12 11:16:26 +0000 | [diff] [blame] | 140 | while ((opt = getopt(argc, argv, "cexXl")) != -1) { |
Glenn L McGrath | 58a4085 | 2001-01-02 23:49:26 +0000 | [diff] [blame] | 141 | switch (opt) { |
| 142 | case 'c': |
| 143 | optflag |= dpkg_deb_contents; |
| 144 | break; |
| 145 | case 'e': |
| 146 | optflag |= dpkg_deb_control; |
| 147 | break; |
Glenn L McGrath | bc9afad | 2001-02-11 03:32:41 +0000 | [diff] [blame] | 148 | case 'X': |
| 149 | optflag |= dpkg_deb_verbose_extract; |
| 150 | break; |
| 151 | case 'x': |
| 152 | optflag |= dpkg_deb_extract; |
| 153 | break; |
Glenn L McGrath | 821fbf0 | 2001-02-12 11:16:26 +0000 | [diff] [blame] | 154 | case 'l': |
| 155 | optflag |= dpkg_deb_list; |
| 156 | break; |
Glenn L McGrath | 58a4085 | 2001-01-02 23:49:26 +0000 | [diff] [blame] | 157 | /* case 'I': |
| 158 | optflag |= dpkg_deb_info; |
| 159 | break; |
| 160 | */ |
Glenn L McGrath | 58a4085 | 2001-01-02 23:49:26 +0000 | [diff] [blame] | 161 | default: |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 162 | show_usage(); |
Glenn L McGrath | 58a4085 | 2001-01-02 23:49:26 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
| 166 | if (((optind + 1 ) > argc) || (optflag == 0)) { |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 167 | show_usage(); |
Glenn L McGrath | 58a4085 | 2001-01-02 23:49:26 +0000 | [diff] [blame] | 168 | } |
Glenn L McGrath | bc9afad | 2001-02-11 03:32:41 +0000 | [diff] [blame] | 169 | if ((optflag & dpkg_deb_control) || (optflag & dpkg_deb_extract) || (optflag & dpkg_deb_verbose_extract)) { |
| 170 | if ( (optind + 1) == argc ) { |
| 171 | target_dir = (char *) xmalloc(7); |
| 172 | strcpy(target_dir, "DEBIAN"); |
| 173 | } else { |
| 174 | target_dir = (char *) xmalloc(strlen(argv[optind + 1])); |
| 175 | strcpy(target_dir, argv[optind + 1]); |
| 176 | } |
Glenn L McGrath | 58a4085 | 2001-01-02 23:49:26 +0000 | [diff] [blame] | 177 | } |
Glenn L McGrath | bc9afad | 2001-02-11 03:32:41 +0000 | [diff] [blame] | 178 | deb_extract(optflag, target_dir, argv[optind]); |
Glenn L McGrath | 58a4085 | 2001-01-02 23:49:26 +0000 | [diff] [blame] | 179 | /* else if (optflag & dpkg_deb_info) { |
| 180 | extract_flag = TRUE; |
| 181 | extract_to_stdout = TRUE; |
| 182 | strcpy(ar_filename, "control.tar.gz"); |
| 183 | extract_list = argv+optind+1; |
| 184 | printf("list one is [%s]\n",extract_list[0]); |
| 185 | } |
| 186 | */ |
Glenn L McGrath | 58a4085 | 2001-01-02 23:49:26 +0000 | [diff] [blame] | 187 | return(EXIT_SUCCESS); |
| 188 | } |