Glenn L McGrath | 5b20d02 | 2001-04-11 16:14:24 +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 | * NOTE: This is used by deb_extract to avoid calling the whole tar applet. |
| 17 | * Its functionality should be merged with tar to avoid duplication |
| 18 | */ |
| 19 | |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <unistd.h> |
| 23 | #include "libbb.h" |
| 24 | |
| 25 | extern int untar(FILE *src_tar_file, int untar_function, char *base_path) |
| 26 | { |
| 27 | typedef struct raw_tar_header { |
| 28 | char name[100]; /* 0-99 */ |
| 29 | char mode[8]; /* 100-107 */ |
| 30 | char uid[8]; /* 108-115 */ |
| 31 | char gid[8]; /* 116-123 */ |
| 32 | char size[12]; /* 124-135 */ |
| 33 | char mtime[12]; /* 136-147 */ |
| 34 | char chksum[8]; /* 148-155 */ |
| 35 | char typeflag; /* 156-156 */ |
| 36 | char linkname[100]; /* 157-256 */ |
| 37 | char magic[6]; /* 257-262 */ |
| 38 | char version[2]; /* 263-264 */ |
| 39 | char uname[32]; /* 265-296 */ |
| 40 | char gname[32]; /* 297-328 */ |
| 41 | char devmajor[8]; /* 329-336 */ |
| 42 | char devminor[8]; /* 337-344 */ |
| 43 | char prefix[155]; /* 345-499 */ |
| 44 | char padding[12]; /* 500-512 */ |
| 45 | } raw_tar_header_t; |
| 46 | |
Glenn L McGrath | 5b20d02 | 2001-04-11 16:14:24 +0000 | [diff] [blame] | 47 | raw_tar_header_t raw_tar_header; |
| 48 | unsigned char *temp = (unsigned char *) &raw_tar_header; |
| 49 | long i; |
| 50 | long next_header_offset = 0; |
| 51 | long uncompressed_count = 0; |
| 52 | size_t size; |
| 53 | mode_t mode; |
| 54 | |
| 55 | // signal(SIGCHLD, child_died); |
| 56 | |
Glenn L McGrath | 359c106 | 2001-04-12 10:19:08 +0000 | [diff] [blame^] | 57 | while (fread((char *) &raw_tar_header, 1, 512, src_tar_file) == 512) { |
Glenn L McGrath | 5b20d02 | 2001-04-11 16:14:24 +0000 | [diff] [blame] | 58 | long sum = 0; |
| 59 | char *dir; |
| 60 | |
| 61 | uncompressed_count += 512; |
| 62 | |
| 63 | /* Check header has valid magic */ |
| 64 | if (strncmp(raw_tar_header.magic, "ustar", 5) != 0) { |
| 65 | /* Put this pack after TODO (check child still alive) is done */ |
| 66 | // error_msg("Invalid tar magic"); |
| 67 | break; |
| 68 | } |
| 69 | |
| 70 | /* Do checksum on headers */ |
| 71 | for (i = 0; i < 148 ; i++) { |
| 72 | sum += temp[i]; |
| 73 | } |
| 74 | sum += ' ' * 8; |
| 75 | for (i = 156; i < 512 ; i++) { |
| 76 | sum += temp[i]; |
| 77 | } |
| 78 | if (sum != strtol(raw_tar_header.chksum, NULL, 8)) { |
| 79 | error_msg("Invalid tar header checksum"); |
| 80 | break; |
| 81 | } |
| 82 | |
| 83 | /* convert to type'ed variables */ |
| 84 | size = strtol(raw_tar_header.size, NULL, 8); |
| 85 | parse_mode(raw_tar_header.mode, &mode); |
| 86 | |
| 87 | /* start of next header is at */ |
| 88 | next_header_offset = uncompressed_count + size; |
| 89 | if (size % 512 != 0) { |
| 90 | next_header_offset += (512 - size % 512); |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * seek to start of control file, return length |
| 95 | * |
| 96 | if (dpkg_untar_function & dpkg_untar_seek_control) { |
| 97 | if ((raw_tar_header.typeflag == '0') || (raw_tar_header.typeflag == '\0')) { |
| 98 | char *tar_filename; |
| 99 | |
| 100 | tar_filename = strrchr(raw_tar_header.name, '/'); |
| 101 | if (tar_filename == NULL) { |
| 102 | tar_filename = strdup(raw_tar_header.name); |
| 103 | } else { |
| 104 | tar_filename++; |
| 105 | } |
| 106 | |
| 107 | if (strcmp(tar_filename, "control") == 0) { |
| 108 | return(size); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | } |
| 113 | */ |
| 114 | /* extract files */ |
| 115 | dir = xmalloc(strlen(raw_tar_header.name) + strlen(base_path) + 2); |
| 116 | |
| 117 | sprintf(dir, "%s/%s", base_path, raw_tar_header.name); |
| 118 | create_path(dir, 0777); |
| 119 | switch (raw_tar_header.typeflag ) { |
| 120 | case '0': |
| 121 | case '\0': |
| 122 | /* If the name ends in a '/' then assume it is |
| 123 | * supposed to be a directory, and fall through |
| 124 | */ |
| 125 | if (raw_tar_header.name[strlen(raw_tar_header.name)-1] != '/') { |
Glenn L McGrath | 359c106 | 2001-04-12 10:19:08 +0000 | [diff] [blame^] | 126 | switch (untar_function) { |
| 127 | case (extract_verbose_extract): |
| 128 | printf("%s\n", raw_tar_header.name); |
| 129 | case (extract_extract): { |
| 130 | FILE *dst_file = wfopen(dir, "w"); |
| 131 | copy_file_chunk(src_tar_file, dst_file, size); |
| 132 | fclose(dst_file); |
| 133 | } |
| 134 | break; |
| 135 | default: { |
| 136 | int remaining = size; |
| 137 | while (remaining-- > 0) { |
| 138 | fgetc(src_tar_file); |
| 139 | } |
| 140 | } |
Glenn L McGrath | 5b20d02 | 2001-04-11 16:14:24 +0000 | [diff] [blame] | 141 | } |
Glenn L McGrath | 359c106 | 2001-04-12 10:19:08 +0000 | [diff] [blame^] | 142 | uncompressed_count += size; |
| 143 | break; |
Glenn L McGrath | 5b20d02 | 2001-04-11 16:14:24 +0000 | [diff] [blame] | 144 | } |
| 145 | case '5': |
Glenn L McGrath | 359c106 | 2001-04-12 10:19:08 +0000 | [diff] [blame^] | 146 | switch (untar_function) { |
| 147 | case (extract_verbose_extract): |
| 148 | printf("%s\n", raw_tar_header.name); |
| 149 | case (extract_extract): |
| 150 | if (create_path(dir, mode) != TRUE) { |
| 151 | free(dir); |
| 152 | perror_msg("%s: Cannot mkdir", raw_tar_header.name); |
| 153 | return(EXIT_FAILURE); |
| 154 | } |
| 155 | break; |
| 156 | default: |
Glenn L McGrath | 5b20d02 | 2001-04-11 16:14:24 +0000 | [diff] [blame] | 157 | } |
| 158 | break; |
| 159 | case '1': |
Glenn L McGrath | 359c106 | 2001-04-12 10:19:08 +0000 | [diff] [blame^] | 160 | if (untar_function & extract_extract) { |
Glenn L McGrath | 5b20d02 | 2001-04-11 16:14:24 +0000 | [diff] [blame] | 161 | if (link(raw_tar_header.linkname, raw_tar_header.name) < 0) { |
| 162 | free(dir); |
| 163 | perror_msg("%s: Cannot create hard link to '%s'", raw_tar_header.name, raw_tar_header.linkname); |
| 164 | return(EXIT_FAILURE); |
| 165 | } |
| 166 | } |
| 167 | break; |
| 168 | case '2': |
Glenn L McGrath | 359c106 | 2001-04-12 10:19:08 +0000 | [diff] [blame^] | 169 | if (untar_function & extract_extract) { |
Glenn L McGrath | 5b20d02 | 2001-04-11 16:14:24 +0000 | [diff] [blame] | 170 | if (symlink(raw_tar_header.linkname, raw_tar_header.name) < 0) { |
| 171 | free(dir); |
| 172 | perror_msg("%s: Cannot create symlink to '%s'", raw_tar_header.name, raw_tar_header.linkname); |
| 173 | return(EXIT_FAILURE); |
| 174 | } |
| 175 | } |
| 176 | break; |
| 177 | case '3': |
| 178 | case '4': |
| 179 | case '6': |
| 180 | // if (tarExtractSpecial( &header, extractFlag, tostdoutFlag)==FALSE) |
| 181 | // errorFlag=TRUE; |
| 182 | // break; |
| 183 | default: |
| 184 | error_msg("Unknown file type '%c' in tar file", raw_tar_header.typeflag); |
| 185 | free(dir); |
| 186 | return(EXIT_FAILURE); |
| 187 | } |
| 188 | free(dir); |
| 189 | } |
Glenn L McGrath | 5b20d02 | 2001-04-11 16:14:24 +0000 | [diff] [blame] | 190 | return(EXIT_SUCCESS); |
| 191 | } |