From 714701c890b5f03253c5ecdb7367c4258ce78715 Mon Sep 17 00:00:00 2001 From: Denis Vlasenko Date: Fri, 22 Dec 2006 00:21:07 +0000 Subject: tar et al: die if bb_copyfd_size copies less than asked for. (we have bb_copyfd_exact_size now for that kind of usage) --- archival/libunarchive/data_extract_all.c | 4 ++-- archival/libunarchive/data_extract_to_buffer.c | 3 +-- archival/libunarchive/data_extract_to_stdout.c | 6 +++-- archival/libunarchive/get_header_ar.c | 3 ++- archival/libunarchive/get_header_tar.c | 2 +- archival/libunarchive/seek_by_read.c | 5 ++-- archival/tar.c | 32 ++++++++++++++------------ archival/unzip.c | 23 +++++++----------- 8 files changed, 37 insertions(+), 41 deletions(-) (limited to 'archival') diff --git a/archival/libunarchive/data_extract_all.c b/archival/libunarchive/data_extract_all.c index 67f8f35..25bf028 100644 --- a/archival/libunarchive/data_extract_all.c +++ b/archival/libunarchive/data_extract_all.c @@ -67,10 +67,10 @@ void data_extract_all(archive_handle_t *archive_handle) /* Regular file */ dst_fd = xopen3(file_header->name, O_WRONLY | O_CREAT | O_EXCL, file_header->mode); - bb_copyfd_size(archive_handle->src_fd, dst_fd, file_header->size); + bb_copyfd_exact_size(archive_handle->src_fd, dst_fd, file_header->size); close(dst_fd); break; - } + } case S_IFDIR: res = mkdir(file_header->name, file_header->mode); if ((errno != EISDIR) && (res == -1) diff --git a/archival/libunarchive/data_extract_to_buffer.c b/archival/libunarchive/data_extract_to_buffer.c index 95cb8f5..d8fcdf3 100644 --- a/archival/libunarchive/data_extract_to_buffer.c +++ b/archival/libunarchive/data_extract_to_buffer.c @@ -10,9 +10,8 @@ void data_extract_to_buffer(archive_handle_t *archive_handle) { - const unsigned int size = archive_handle->file_header->size; + unsigned int size = archive_handle->file_header->size; archive_handle->buffer = xzalloc(size + 1); - xread(archive_handle->src_fd, archive_handle->buffer, size); } diff --git a/archival/libunarchive/data_extract_to_stdout.c b/archival/libunarchive/data_extract_to_stdout.c index 788246c..2e266c0 100644 --- a/archival/libunarchive/data_extract_to_stdout.c +++ b/archival/libunarchive/data_extract_to_stdout.c @@ -4,9 +4,11 @@ */ #include "unarchive.h" -#include +//#include void data_extract_to_stdout(archive_handle_t *archive_handle) { - bb_copyfd_size(archive_handle->src_fd, STDOUT_FILENO, archive_handle->file_header->size); + bb_copyfd_exact_size(archive_handle->src_fd, + STDOUT_FILENO, + archive_handle->file_header->size); } diff --git a/archival/libunarchive/get_header_ar.c b/archival/libunarchive/get_header_ar.c index 7f8c81c..6638c65 100644 --- a/archival/libunarchive/get_header_ar.c +++ b/archival/libunarchive/get_header_ar.c @@ -96,7 +96,8 @@ char get_header_ar(archive_handle_t *archive_handle) if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) { archive_handle->action_header(typed); if (archive_handle->sub_archive) { - while (archive_handle->action_data_subarchive(archive_handle->sub_archive) == EXIT_SUCCESS); + while (archive_handle->action_data_subarchive(archive_handle->sub_archive) == EXIT_SUCCESS) + /* repeat */; } else { archive_handle->action_data(archive_handle); } diff --git a/archival/libunarchive/get_header_tar.c b/archival/libunarchive/get_header_tar.c index 66c3314..beb8687 100644 --- a/archival/libunarchive/get_header_tar.c +++ b/archival/libunarchive/get_header_tar.c @@ -251,7 +251,7 @@ char get_header_tar(archive_handle_t *archive_handle) } /* Strip trailing '/' in directories */ - /* Must be done after mode is set as '/' is used to check if its a directory */ + /* Must be done after mode is set as '/' is used to check if it's a directory */ cp = last_char_is(file_header->name, '/'); if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) { diff --git a/archival/libunarchive/seek_by_read.c b/archival/libunarchive/seek_by_read.c index d56f94b..e46af48 100644 --- a/archival/libunarchive/seek_by_read.c +++ b/archival/libunarchive/seek_by_read.c @@ -13,7 +13,6 @@ */ void seek_by_read(const archive_handle_t *archive_handle, const unsigned int jump_size) { - if (jump_size) { - bb_copyfd_size(archive_handle->src_fd, -1, jump_size); - } + if (jump_size) + bb_copyfd_exact_size(archive_handle->src_fd, -1, jump_size); } diff --git a/archival/tar.c b/archival/tar.c index 7465e88..ee9007c 100644 --- a/archival/tar.c +++ b/archival/tar.c @@ -452,26 +452,28 @@ static int writeFileToTarball(const char *fileName, struct stat *statbuf, /* If it was a regular file, write out the body */ if (inputFileFd >= 0) { - off_t readSize = 0; - - /* write the file to the archive */ - readSize = bb_copyfd_size(inputFileFd, tbInfo->tarFd, statbuf->st_size); - /* readSize < 0 means that error was already reported */ - if (readSize != statbuf->st_size && readSize >= 0) { - /* Deadly. We record size into header first, */ - /* and then write out file. If file shrinks in between, */ - /* tar will be corrupted. So bail out. */ - /* NB: GNU tar 1.16 warns and pads with zeroes */ - /* or even seeks back and updates header */ - bb_error_msg_and_die("short read from %s, aborting", fileName); - } + size_t readSize; + /* Wwrite the file to the archive. */ + /* We record size into header first, */ + /* and then write out file. If file shrinks in between, */ + /* tar will be corrupted. So we don't allow for that. */ + /* NB: GNU tar 1.16 warns and pads with zeroes */ + /* or even seeks back and updates header */ + bb_copyfd_exact_size(inputFileFd, tbInfo->tarFd, statbuf->st_size); + ////off_t readSize; + ////readSize = bb_copyfd_size(inputFileFd, tbInfo->tarFd, statbuf->st_size); + ////if (readSize != statbuf->st_size && readSize >= 0) { + //// bb_error_msg_and_die("short read from %s, aborting", fileName); + ////} + /* Check that file did not grow in between? */ - /* if (safe_read(inputFileFd,1) == 1) warn but continue? */ + /* if (safe_read(inputFileFd, 1) == 1) warn but continue? */ + close(inputFileFd); /* Pad the file up to the tar block size */ /* (a few tricks here in the name of code size) */ - readSize = (-(int)readSize) & (TAR_BLOCK_SIZE-1); + readSize = (-(int)statbuf->st_size) & (TAR_BLOCK_SIZE-1); memset(bb_common_bufsiz1, 0, readSize); xwrite(tbInfo->tarFd, bb_common_bufsiz1, readSize); } diff --git a/archival/unzip.c b/archival/unzip.c index f553eef..1c03a4c 100644 --- a/archival/unzip.c +++ b/archival/unzip.c @@ -54,9 +54,9 @@ typedef union { static void unzip_skip(int fd, off_t skip) { if (lseek(fd, skip, SEEK_CUR) == (off_t)-1) { - if ((errno != ESPIPE) || (bb_copyfd_size(fd, -1, skip) != skip)) { + if (errno != ESPIPE) bb_error_msg_and_die("seek failure"); - } + bb_copyfd_exact_size(fd, -1, skip); } } @@ -75,10 +75,8 @@ static int unzip_extract(zip_header_t *zip_header, int src_fd, int dst_fd) if (zip_header->formatted.method == 0) { /* Method 0 - stored (not compressed) */ off_t size = zip_header->formatted.ucmpsize; - if (size && (bb_copyfd_size(src_fd, dst_fd, size) != size)) { - bb_error_msg_and_die("cannot complete extraction"); - } - + if (size) + bb_copyfd_exact_size(src_fd, dst_fd, size); } else { /* Method 8 - inflate */ inflate_init(zip_header->formatted.cmpsize); @@ -249,8 +247,8 @@ int unzip_main(int argc, char **argv) unzip_skip(src_fd, zip_header.formatted.extra_len); if ((verbosity == v_list) && !list_header_done){ - printf(" Length Date Time Name\n" - " -------- ---- ---- ----\n"); + puts(" Length Date Time Name\n" + " -------- ---- ---- ----"); list_header_done = 1; } @@ -274,10 +272,8 @@ int unzip_main(int argc, char **argv) dst_fn); total_entries++; i = 'n'; - } else if (dst_fd == STDOUT_FILENO) { /* Extracting to STDOUT */ i = -1; - } else if (last_char_is(dst_fn, '/')) { /* Extract directory */ if (stat(dst_fn, &stat_buf) == -1) { if (errno != ENOENT) { @@ -298,17 +294,15 @@ int unzip_main(int argc, char **argv) i = 'n'; } else { /* Extract file */ - _check_file: + _check_file: if (stat(dst_fn, &stat_buf) == -1) { /* File does not exist */ if (errno != ENOENT) { bb_perror_msg_and_die("cannot stat '%s'",dst_fn); } i = 'y'; - } else { /* File already exists */ if (overwrite == o_never) { i = 'n'; - } else if (S_ISREG(stat_buf.st_mode)) { /* File is regular file */ if (overwrite == o_always) { i = 'y'; @@ -319,7 +313,6 @@ int unzip_main(int argc, char **argv) } i = key_buf[0]; } - } else { /* File is not regular file */ bb_error_msg_and_die("'%s' exists but is not regular file",dst_fn); } @@ -338,7 +331,7 @@ int unzip_main(int argc, char **argv) printf(" inflating: %s\n", dst_fn); } if (unzip_extract(&zip_header, src_fd, dst_fd)) { - failed = 1; + failed = 1; } if (dst_fd != STDOUT_FILENO) { /* closing STDOUT is potentially bad for future business */ -- cgit v1.1