diff options
| -rw-r--r-- | libbb/untar.c | 200 |
1 files changed, 200 insertions, 0 deletions
diff --git a/libbb/untar.c b/libbb/untar.c new file mode 100644 index 000000000..1542e390d --- /dev/null +++ b/libbb/untar.c | |||
| @@ -0,0 +1,200 @@ | |||
| 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 | |||
| 47 | // const int dpkg_deb_contents = 1; | ||
| 48 | // const int dpkg_deb_control = 2; | ||
| 49 | // const int dpkg_deb_info = 4; | ||
| 50 | const int untar_extract = 8; | ||
| 51 | // const int dpkg_deb_verbose_extract = 16; | ||
| 52 | // const int dpkg_deb_list = 32; | ||
| 53 | |||
| 54 | raw_tar_header_t raw_tar_header; | ||
| 55 | unsigned char *temp = (unsigned char *) &raw_tar_header; | ||
| 56 | long i; | ||
| 57 | long next_header_offset = 0; | ||
| 58 | long uncompressed_count = 0; | ||
| 59 | size_t size; | ||
| 60 | mode_t mode; | ||
| 61 | |||
| 62 | // signal(SIGCHLD, child_died); | ||
| 63 | |||
| 64 | while (fread((char *) &raw_tar_header, 1, 512, src_tar_file)==512) { | ||
| 65 | long sum = 0; | ||
| 66 | char *dir; | ||
| 67 | |||
| 68 | uncompressed_count += 512; | ||
| 69 | |||
| 70 | /* Check header has valid magic */ | ||
| 71 | if (strncmp(raw_tar_header.magic, "ustar", 5) != 0) { | ||
| 72 | /* Put this pack after TODO (check child still alive) is done */ | ||
| 73 | // error_msg("Invalid tar magic"); | ||
| 74 | break; | ||
| 75 | } | ||
| 76 | |||
| 77 | /* Do checksum on headers */ | ||
| 78 | for (i = 0; i < 148 ; i++) { | ||
| 79 | sum += temp[i]; | ||
| 80 | } | ||
| 81 | sum += ' ' * 8; | ||
| 82 | for (i = 156; i < 512 ; i++) { | ||
| 83 | sum += temp[i]; | ||
| 84 | } | ||
| 85 | if (sum != strtol(raw_tar_header.chksum, NULL, 8)) { | ||
| 86 | error_msg("Invalid tar header checksum"); | ||
| 87 | break; | ||
| 88 | } | ||
| 89 | |||
| 90 | /* convert to type'ed variables */ | ||
| 91 | size = strtol(raw_tar_header.size, NULL, 8); | ||
| 92 | parse_mode(raw_tar_header.mode, &mode); | ||
| 93 | |||
| 94 | /* start of next header is at */ | ||
| 95 | next_header_offset = uncompressed_count + size; | ||
| 96 | if (size % 512 != 0) { | ||
| 97 | next_header_offset += (512 - size % 512); | ||
| 98 | } | ||
| 99 | |||
| 100 | /* | ||
| 101 | * seek to start of control file, return length | ||
| 102 | * | ||
| 103 | if (dpkg_untar_function & dpkg_untar_seek_control) { | ||
| 104 | if ((raw_tar_header.typeflag == '0') || (raw_tar_header.typeflag == '\0')) { | ||
| 105 | char *tar_filename; | ||
| 106 | |||
| 107 | tar_filename = strrchr(raw_tar_header.name, '/'); | ||
| 108 | if (tar_filename == NULL) { | ||
| 109 | tar_filename = strdup(raw_tar_header.name); | ||
| 110 | } else { | ||
| 111 | tar_filename++; | ||
| 112 | } | ||
| 113 | |||
| 114 | if (strcmp(tar_filename, "control") == 0) { | ||
| 115 | return(size); | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | } | ||
| 120 | */ | ||
| 121 | /* extract files */ | ||
| 122 | dir = xmalloc(strlen(raw_tar_header.name) + strlen(base_path) + 2); | ||
| 123 | |||
| 124 | sprintf(dir, "%s/%s", base_path, raw_tar_header.name); | ||
| 125 | create_path(dir, 0777); | ||
| 126 | switch (raw_tar_header.typeflag ) { | ||
| 127 | case '0': | ||
| 128 | case '\0': | ||
| 129 | /* If the name ends in a '/' then assume it is | ||
| 130 | * supposed to be a directory, and fall through | ||
| 131 | */ | ||
| 132 | if (raw_tar_header.name[strlen(raw_tar_header.name)-1] != '/') { | ||
| 133 | if (untar_function & untar_extract) { | ||
| 134 | int out_count=0; | ||
| 135 | FILE *dst_file; | ||
| 136 | |||
| 137 | dst_file = wfopen(dir, "w"); | ||
| 138 | if (copy_file_chunk(src_tar_file, dst_file, size) == FALSE) { | ||
| 139 | error_msg_and_die("Couldnt extract file"); | ||
| 140 | } | ||
| 141 | uncompressed_count += out_count; | ||
| 142 | fclose(dst_file); | ||
| 143 | break; | ||
| 144 | } | ||
| 145 | } | ||
| 146 | case '5': | ||
| 147 | if (untar_function & untar_extract) { | ||
| 148 | if (create_path(dir, mode) != TRUE) { | ||
| 149 | free(dir); | ||
| 150 | perror_msg("%s: Cannot mkdir", raw_tar_header.name); | ||
| 151 | return(EXIT_FAILURE); | ||
| 152 | } | ||
| 153 | } | ||
| 154 | break; | ||
| 155 | case '1': | ||
| 156 | if (untar_function & untar_extract) { | ||
| 157 | if (link(raw_tar_header.linkname, raw_tar_header.name) < 0) { | ||
| 158 | free(dir); | ||
| 159 | perror_msg("%s: Cannot create hard link to '%s'", raw_tar_header.name, raw_tar_header.linkname); | ||
| 160 | return(EXIT_FAILURE); | ||
| 161 | } | ||
| 162 | } | ||
| 163 | break; | ||
| 164 | case '2': | ||
| 165 | if (untar_function & untar_extract) { | ||
| 166 | if (symlink(raw_tar_header.linkname, raw_tar_header.name) < 0) { | ||
| 167 | free(dir); | ||
| 168 | perror_msg("%s: Cannot create symlink to '%s'", raw_tar_header.name, raw_tar_header.linkname); | ||
| 169 | return(EXIT_FAILURE); | ||
| 170 | } | ||
| 171 | } | ||
| 172 | break; | ||
| 173 | case '3': | ||
| 174 | case '4': | ||
| 175 | case '6': | ||
| 176 | // if (tarExtractSpecial( &header, extractFlag, tostdoutFlag)==FALSE) | ||
| 177 | // errorFlag=TRUE; | ||
| 178 | // break; | ||
| 179 | default: | ||
| 180 | error_msg("Unknown file type '%c' in tar file", raw_tar_header.typeflag); | ||
| 181 | free(dir); | ||
| 182 | return(EXIT_FAILURE); | ||
| 183 | } | ||
| 184 | free(dir); | ||
| 185 | } | ||
| 186 | /* skip to start of next header */ | ||
| 187 | while(uncompressed_count < next_header_offset) { | ||
| 188 | char ch[2]; | ||
| 189 | if (fread(ch, 1, 1, src_tar_file) != 1) { | ||
| 190 | error_msg("read error\n"); | ||
| 191 | getchar(); | ||
| 192 | } else { | ||
| 193 | uncompressed_count++; | ||
| 194 | } | ||
| 195 | } | ||
| 196 | /* | ||
| 197 | * TODO: Check that gunzip child hasnt finished | ||
| 198 | */ | ||
| 199 | return(EXIT_SUCCESS); | ||
| 200 | } | ||
