diff options
-rw-r--r-- | libbb/add_from_archive_list.c | 31 | ||||
-rw-r--r-- | libbb/append_archive_list.c | 39 | ||||
-rw-r--r-- | libbb/extract_archive.c | 132 | ||||
-rw-r--r-- | libbb/fgets_str.c | 64 | ||||
-rw-r--r-- | libbb/get_tar_gz_headers.c | 39 | ||||
-rw-r--r-- | libbb/get_tar_headers.c | 111 | ||||
-rw-r--r-- | libbb/read_text_file_to_buffer.c | 38 | ||||
-rw-r--r-- | libbb/untar.c | 225 |
8 files changed, 416 insertions, 263 deletions
diff --git a/libbb/add_from_archive_list.c b/libbb/add_from_archive_list.c new file mode 100644 index 000000000..7e4e07159 --- /dev/null +++ b/libbb/add_from_archive_list.c @@ -0,0 +1,31 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include "libbb.h" + +file_headers_t *add_from_archive_list(file_headers_t *master_list, file_headers_t *new_list, const char *filename) +{ + file_headers_t *list_ptr; + + list_ptr = master_list; + while (list_ptr != NULL) { + if (strcmp(filename, list_ptr->name) == 0) { + return(append_archive_list(new_list, list_ptr)); + } + list_ptr = list_ptr->next; + } + return(NULL); +}
\ No newline at end of file diff --git a/libbb/append_archive_list.c b/libbb/append_archive_list.c new file mode 100644 index 000000000..c955d08d2 --- /dev/null +++ b/libbb/append_archive_list.c @@ -0,0 +1,39 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include <stdlib.h> +#include "libbb.h" + +file_headers_t *append_archive_list(file_headers_t *head, file_headers_t *tail_entry) +{ + file_headers_t *tail_ptr; + file_headers_t *new_node = (file_headers_t *) malloc(sizeof(file_headers_t)); + + *new_node = *tail_entry; + new_node->next = NULL; + + if (head->name == NULL) { + head = new_node; + } else { + tail_ptr = head; + while(tail_ptr->next != NULL) { + tail_ptr = tail_ptr->next; + } + tail_ptr->next = new_node; + } + + return(head); +}
\ No newline at end of file diff --git a/libbb/extract_archive.c b/libbb/extract_archive.c new file mode 100644 index 000000000..f22cbb619 --- /dev/null +++ b/libbb/extract_archive.c @@ -0,0 +1,132 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include <stdlib.h> +#include <string.h> +#include <time.h> +#include <unistd.h> +#include <utime.h> +#include "libbb.h" + +/* Extract files in the linear linked list 'extract_headers' to either + * filesystem, stdout or buffer depending on the value of 'function' which is + * described in extract_files.h + * + * prefix doesnt have to be just a directory, it may prefix the filename as well. + * + * e.g. '/var/lib/dpkg/info/dpkg.' will extract all files to the base bath + * '/var/lib/dpkg/info/' and all files/dirs created in that dir will have + * 'dpkg.' as their prefix + * + * For this reason if prefix does point to a dir then it must end with a + * trailing '/' or else the last dir will be assumed to be the file prefix + */ + +char *extract_archive(FILE *src_stream, FILE *out_stream, file_headers_t *extract_headers, int function, const char *prefix) +{ + FILE *dst_stream = NULL; + char *full_name = NULL; + char *buffer = NULL; + size_t uncompressed_count = 0; + struct utimbuf t; + + /* find files to extract or display */ + while (extract_headers != NULL) { + /* prefix doesnt have to be a proper path it may prepend + * the filename as well */ + if (prefix != NULL) { + /* strip leading '/' in filename to extract as prefix may not be dir */ + char *tmp_str = strchr(extract_headers->name, '/'); + if (tmp_str == NULL) { + tmp_str = extract_headers->name; + } else { + tmp_str++; + } + /* Cant use concat_path_file here as prefix might not be a directory */ + full_name = xmalloc(strlen(prefix) + strlen(tmp_str) + 1); + strcpy(full_name, prefix); + strcat(full_name, tmp_str); + } else { + full_name = extract_headers->name; + } + + /* Seek to start of file, have to use fgetc as src_stream may be a pipe + * you cant [lf]seek on pipes */ + while (uncompressed_count < extract_headers->offset) { + fgetc(src_stream); + uncompressed_count++; + } + + if (S_ISREG(extract_headers->mode)) { + if (function & extract_to_stdout) { + copy_file_chunk(src_stream, out_stream, extract_headers->size); + uncompressed_count += extract_headers->size; + } + else if (function & extract_all_to_fs) { + dst_stream = wfopen(full_name, "w"); + copy_file_chunk(src_stream, dst_stream, extract_headers->size); + uncompressed_count += extract_headers->size; + fclose(dst_stream); + } + else if (function & extract_one_to_buffer) { + buffer = (char *) xmalloc(extract_headers->size + 1); + fread(buffer, 1, extract_headers->size, src_stream); + break; + } + } +#if defined BB_DPKG | defined BB_DPKG_DEB + else if (S_ISDIR(extract_headers->mode)) { + if (function & extract_all_to_fs) { + if (create_path(full_name, extract_headers->mode) == FALSE) { + perror_msg("Cannot mkdir %s", extract_headers->name); + return NULL; + } + } + } + else if (S_ISLNK(extract_headers->mode)) { + if (function & extract_all_to_fs) { + if (symlink(extract_headers->link_name, full_name) < 0) { + perror_msg("Cannot create hard link from %s to '%s'", extract_headers->name, extract_headers->link_name); + return NULL; + } + } + } +#endif + if (function & extract_verbose_list) { + fprintf(out_stream, "%s %d/%d %8d %s ", mode_string(extract_headers->mode), + extract_headers->uid, extract_headers->gid, + (int) extract_headers->size, time_string(extract_headers->mtime)); + } + + if ((function & extract_list) || (function & extract_verbose_list)){ + /* fputs doesnt add a trailing \n, so use fprintf */ + fprintf(out_stream, "%s\n", extract_headers->name); + } + + if (function & extract_preserve_date) { + t.actime = extract_headers->mtime; + t.modtime = extract_headers->mtime; + utime(full_name, &t); + } + chmod(full_name, extract_headers->mode); + free(full_name); + + extract_headers = extract_headers->next; + } + + return(buffer); +} + diff --git a/libbb/fgets_str.c b/libbb/fgets_str.c new file mode 100644 index 000000000..33d8d00cc --- /dev/null +++ b/libbb/fgets_str.c @@ -0,0 +1,64 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +/* + * Continue reading from file until the terminating string is encountered. + * Return data as string. + * e.g. fgets_str(file, "\n"); will read till end of file + */ + +char *fgets_str(FILE *file, const char *terminating_string) +{ + char *linebuf = NULL; + const int term_length = strlen(terminating_string); + int end_string_offset; + int linebufsz = 0; + int idx = 0; + int ch; + + while (1) { + ch = fgetc(file); + if (ch == EOF) { + break; + } + + /* grow the line buffer as necessary */ + while (idx > linebufsz - 2) { + linebuf = realloc(linebuf, linebufsz += 1000); /* GROWBY */ + } + + linebuf[idx] = ch; + idx++; + + /* Check for terminating string */ + end_string_offset = idx - term_length; + if ((end_string_offset > 0) && (memcmp(&linebuf[end_string_offset], terminating_string, term_length) == 0)) { + idx -= term_length; + break; + } + } + if (idx == 0) { + return NULL; + } + linebuf[idx] = '\0'; + return(linebuf); +} + diff --git a/libbb/get_tar_gz_headers.c b/libbb/get_tar_gz_headers.c new file mode 100644 index 000000000..fb9b7b63f --- /dev/null +++ b/libbb/get_tar_gz_headers.c @@ -0,0 +1,39 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ +#include <stdio.h> +#include <unistd.h> +#include "libbb.h" + +file_headers_t *get_tar_gz_headers(FILE *compressed_stream) +{ + FILE *uncompressed_stream; + file_headers_t *tar_headers_tree; + int gunzip_pid; + int gz_fd ; + + /* open a stream of decompressed data */ + gz_fd = gz_open(compressed_stream, &gunzip_pid); + if (gz_fd == -1) { + error_msg("Couldnt initialise gzip stream"); + } + uncompressed_stream = fdopen(gz_fd, "r"); + tar_headers_tree = get_tar_headers(uncompressed_stream); + fclose(uncompressed_stream); + close(gz_fd); + gz_close(gunzip_pid); + + return(tar_headers_tree); +}
\ No newline at end of file diff --git a/libbb/get_tar_headers.c b/libbb/get_tar_headers.c new file mode 100644 index 000000000..5ddf4d9c3 --- /dev/null +++ b/libbb/get_tar_headers.c @@ -0,0 +1,111 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include "libbb.h" + +file_headers_t *get_tar_headers(FILE *tar_stream) +{ + typedef struct raw_tar_header { + char name[100]; /* 0-99 */ + char mode[8]; /* 100-107 */ + char uid[8]; /* 108-115 */ + char gid[8]; /* 116-123 */ + char size[12]; /* 124-135 */ + char mtime[12]; /* 136-147 */ + char chksum[8]; /* 148-155 */ + char typeflag; /* 156-156 */ + char linkname[100]; /* 157-256 */ + char magic[6]; /* 257-262 */ + char version[2]; /* 263-264 */ + char uname[32]; /* 265-296 */ + char gname[32]; /* 297-328 */ + char devmajor[8]; /* 329-336 */ + char devminor[8]; /* 337-344 */ + char prefix[155]; /* 345-499 */ + char padding[12]; /* 500-512 */ + } raw_tar_header_t; + + raw_tar_header_t raw_tar_header; + unsigned char *temp = (unsigned char *) &raw_tar_header; + file_headers_t *tar_headers_list = (file_headers_t *) calloc(1, sizeof(file_headers_t)); + file_headers_t *tar_entry; + long i; + long next_header_offset = 0; + long current_offset = 0; + + while (fread((char *) &raw_tar_header, 1, 512, tar_stream) == 512) { + long sum = 0; + current_offset += 512; + + /* Check header has valid magic, unfortunately some tar files + * have empty (0'ed) tar entries at the end, which will + * cause this to fail, so fail silently for now + */ + if (strncmp(raw_tar_header.magic, "ustar", 5) != 0) { +// error_msg("invalid magic, [%s]\n", raw_tar_header.magic); + break; + } + + /* Do checksum on headers */ + for (i = 0; i < 148 ; i++) { + sum += temp[i]; + } + sum += ' ' * 8; + for (i = 156; i < 512 ; i++) { + sum += temp[i]; + } + if (sum != strtol(raw_tar_header.chksum, NULL, 8)) { + error_msg("Invalid tar header checksum"); + break; + } + + /* convert to type'ed variables */ + tar_entry = xcalloc(1, sizeof(file_headers_t)); + tar_entry->name = xstrdup(raw_tar_header.name); + + tar_entry->offset = (off_t) current_offset; + parse_mode(raw_tar_header.mode, &tar_entry->mode); + tar_entry->uid = strtol(raw_tar_header.uid, NULL, 8); + tar_entry->gid = strtol(raw_tar_header.gid, NULL, 8); + tar_entry->size = strtol(raw_tar_header.size, NULL, 8); + tar_entry->mtime = strtol(raw_tar_header.mtime, NULL, 8); + tar_entry->link_name = xstrdup(raw_tar_header.linkname); +// tar_entry->devmajor = strtol(rawHeader->devmajor, NULL, 8); +// tar_entry->devminor = strtol(rawHeader->devminor, NULL, 8); + + tar_headers_list = append_archive_list(tar_headers_list, tar_entry); + + /* start of next header is at */ + next_header_offset = current_offset + tar_entry->size; + + if (tar_entry->size % 512 != 0) { + next_header_offset += (512 - tar_entry->size % 512); + } + /* + * Seek to start of next block, cant use fseek as unzip() does support it + */ + while (current_offset < next_header_offset) { + if (fgetc(tar_stream) == EOF) { + break; + } + current_offset++; + } + } + return(tar_headers_list); +} + diff --git a/libbb/read_text_file_to_buffer.c b/libbb/read_text_file_to_buffer.c deleted file mode 100644 index ef64ad712..000000000 --- a/libbb/read_text_file_to_buffer.c +++ /dev/null @@ -1,38 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include "libbb.h" - -/* - * Reads consecutive lines from file line that start with end_string - * read finishes at an empty line or eof - */ -extern char *read_text_file_to_buffer(FILE *src_file) -{ - char *line = NULL; - char *buffer = NULL; - int buffer_length = 0; - int line_length = 0; - - buffer = xmalloc(1); - strcpy(buffer, ""); - - /* Loop until line is empty, or just one char, which will be '\n' */ - do { - line = get_line_from_file(src_file); - if (line == NULL) { - break; - } - line_length = strlen(line); - buffer_length += line_length + 1; - buffer = (char *) xrealloc(buffer, buffer_length + 1); - strcat(buffer, line); - free(line); - } while (line_length > 1); - - if (strlen(buffer) == 0) { - return(NULL); - } else { - return(strdup(buffer)); - } -}
\ No newline at end of file diff --git a/libbb/untar.c b/libbb/untar.c deleted file mode 100644 index b6b44967f..000000000 --- a/libbb/untar.c +++ /dev/null @@ -1,225 +0,0 @@ -/* - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Library General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - * NOTE: This is used by deb_extract to avoid calling the whole tar applet. - * Its functionality should be merged with tar to avoid duplication - */ - -#include <stdlib.h> -#include <string.h> -#include <unistd.h> -#include "libbb.h" - -/* - * stdout can be redircted to FILE *output - * If const char *file_prefix isnt NULL is prepended to all the extracted filenames. - * The purpose of const char *argument depends on the value of const int untar_function - */ - -extern char *untar(FILE *src_tar_file, FILE *output, const int untar_function, - const char *argument, const char *file_prefix) -{ - typedef struct raw_tar_header { - char name[100]; /* 0-99 */ - char mode[8]; /* 100-107 */ - char uid[8]; /* 108-115 */ - char gid[8]; /* 116-123 */ - char size[12]; /* 124-135 */ - char mtime[12]; /* 136-147 */ - char chksum[8]; /* 148-155 */ - char typeflag; /* 156-156 */ - char linkname[100]; /* 157-256 */ - char magic[6]; /* 257-262 */ - char version[2]; /* 263-264 */ - char uname[32]; /* 265-296 */ - char gname[32]; /* 297-328 */ - char devmajor[8]; /* 329-336 */ - char devminor[8]; /* 337-344 */ - char prefix[155]; /* 345-499 */ - char padding[12]; /* 500-512 */ - } raw_tar_header_t; - - raw_tar_header_t raw_tar_header; - unsigned char *temp = (unsigned char *) &raw_tar_header; - long i; - long next_header_offset = 0; - long uncompressed_count = 0; - size_t size; - mode_t mode; - - while (fread((char *) &raw_tar_header, 1, 512, src_tar_file) == 512) { - long sum = 0; - - if (ferror(src_tar_file) || feof(src_tar_file)) { - perror_msg("untar: "); - break; - } - - uncompressed_count += 512; - - /* Check header has valid magic */ - if (strncmp(raw_tar_header.magic, "ustar", 5) != 0) { -/* - * FIXME, Need HELP with this - * - * This has to fail silently or it incorrectly reports errors when called from - * deb_extract. - * The problem is deb_extract decompresses the .gz file in a child process and - * untar reads from the child proccess. The child process finishes and exits, - * but fread reads 0's from the src_tar_file even though the child - * closed its handle. - */ -// error_msg("Invalid tar magic"); - break; - } - - /* Do checksum on headers */ - for (i = 0; i < 148 ; i++) { - sum += temp[i]; - } - sum += ' ' * 8; - for (i = 156; i < 512 ; i++) { - sum += temp[i]; - } - if (sum != strtol(raw_tar_header.chksum, NULL, 8)) { - error_msg("Invalid tar header checksum"); - break; - } - - /* convert to type'ed variables */ - size = strtol(raw_tar_header.size, NULL, 8); - parse_mode(raw_tar_header.mode, &mode); - - /* start of next header is at */ - next_header_offset = uncompressed_count + size; - if (size % 512 != 0) { - next_header_offset += (512 - size % 512); - } - - /* If an exclude list is specified check current file against list */ -#if 0 - if (*exclude_list != NULL) { - i = 0; - while (exclude_list[i] != 0) { - if (strncmp(exclude_list[i], raw_tar_header.name, strlen(raw_tar_header.name)) == 0) { - break; - } - i++; - } - if (exclude_list[i] != 0) { - continue; - } - } -#endif - if (untar_function & (extract_contents | extract_verbose_extract | extract_contents_to_file)) { - fprintf(output, "%s\n", raw_tar_header.name); - } - - switch (raw_tar_header.typeflag ) { - case '0': - case '\0': { - /* If the name ends in a '/' then assume it is - * supposed to be a directory, and fall through - */ - int name_length = strlen(raw_tar_header.name); - if (raw_tar_header.name[name_length - 1] != '/') { - switch (untar_function) { - case (extract_extract): - case (extract_verbose_extract): - case (extract_control): { - FILE *dst_file = NULL; - char *full_name; - - if (file_prefix != NULL) { - full_name = xmalloc(strlen(argument) + strlen(file_prefix) + name_length + 3); - sprintf(full_name, "%s/%s.%s", argument, file_prefix, strrchr(raw_tar_header.name, '/') + 1); - } else { - full_name = concat_path_file(argument, raw_tar_header.name); - } - dst_file = wfopen(full_name, "w"); - copy_file_chunk(src_tar_file, dst_file, (unsigned long long) size); - uncompressed_count += size; - fclose(dst_file); - chmod(full_name, mode); - free(full_name); - } - break; - case (extract_info): - if (strstr(raw_tar_header.name, argument) != NULL) { - copy_file_chunk(src_tar_file, stdout, (unsigned long long) size); - uncompressed_count += size; - } - break; - case (extract_field): - if (strstr(raw_tar_header.name, "./control") != NULL) { - return(read_text_file_to_buffer(src_tar_file)); - } - break; - } - break; - } - } - case '5': - if (untar_function & (extract_extract | extract_verbose_extract | extract_control)) { - int ret; - char *dir; - dir = concat_path_file(argument, raw_tar_header.name); - ret = create_path(dir, mode); - free(dir); - if (ret == FALSE) { - perror_msg("%s: Cannot mkdir", raw_tar_header.name); - return NULL; - } - chmod(dir, mode); - } - break; - case '1': - if (untar_function & (extract_extract | extract_verbose_extract | extract_control)) { - if (link(raw_tar_header.linkname, raw_tar_header.name) < 0) { - perror_msg("%s: Cannot create hard link to '%s'", raw_tar_header.name, raw_tar_header.linkname); - return NULL; - } - } - break; - case '2': - if (untar_function & (extract_extract | extract_verbose_extract | extract_control)) { - if (symlink(raw_tar_header.linkname, raw_tar_header.name) < 0) { - perror_msg("%s: Cannot create symlink to '%s'", raw_tar_header.name, raw_tar_header.linkname); - return NULL; - } - } - break; - case '3': - case '4': - case '6': -// if (tarExtractSpecial( &header, extractFlag, tostdoutFlag)==FALSE) -// errorFlag=TRUE; -// break; - default: - error_msg("Unknown file type '%c' in tar file", raw_tar_header.typeflag); - return NULL; - } - /* - * Seek to start of next block, cant use fseek as unzip() does support it - */ - while (uncompressed_count < next_header_offset) { - if (fgetc(src_tar_file) == EOF) { - break; - } - uncompressed_count++; - } - } - return NULL; -} |