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