As we no longer use function pointers for read in common archiving code
authorGlenn L McGrath <bug1@ihug.co.nz>
Fri, 21 Nov 2003 22:24:57 +0000 (22:24 -0000)
committerGlenn L McGrath <bug1@ihug.co.nz>
Fri, 21 Nov 2003 22:24:57 +0000 (22:24 -0000)
archive_xread can be replaced with bb_full_read, and archive_copy_file
with bb_copyfd*
bb_copyfd is split into two functions bb_copyfd_size and bb_copyfd_eof,
they share a common backend.

18 files changed:
archival/ar.c
archival/libunarchive/Makefile.in
archival/libunarchive/archive_copy_file.c [deleted file]
archival/libunarchive/archive_xread.c [deleted file]
archival/libunarchive/archive_xread_all.c
archival/libunarchive/archive_xread_all_eof.c
archival/libunarchive/data_extract_all.c
archival/libunarchive/data_extract_to_stdout.c
archival/libunarchive/get_header_tar.c
archival/libunarchive/seek_by_char.c
archival/tar.c
coreutils/cat.c
include/libbb.h
include/unarchive.h
libbb/copy_file.c
libbb/copyfd.c
libbb/print_file.c
networking/ftpgetput.c

index 57ec927191c229ec8d937a3c67dcb342cb7ba70a..32ecd57364fccdf6764e56eca36acbcfcf60486a 100644 (file)
@@ -59,7 +59,7 @@ static void data_extract_regular_file(archive_handle_t *archive_handle)
 
        file_header = archive_handle->file_header;
        dst_fd = bb_xopen(file_header->name, O_WRONLY | O_CREAT);
-       archive_copy_file(archive_handle, dst_fd);      
+       bb_copyfd_eof(archive_handle->src_fd, dst_fd, file_header->size);       
        close(dst_fd);
 
        chmod(file_header->name, file_header->mode);
index c9dec09ad59e28023dd53ef0e878811c287495c2..09b0571edd5bc97edccc4288c15cd14d1e4390f2 100644 (file)
@@ -37,14 +37,11 @@ LIBUNARCHIVE-y:= \
        header_list.o \
        header_verbose_list.o \
 \
-       archive_xread.o \
        archive_xread_all.o \
        archive_xread_all_eof.o \
 \
        seek_by_char.o \
        seek_by_jump.o \
-\
-       archive_copy_file.o \
 \
        data_align.o \
        find_list_entry.o \
diff --git a/archival/libunarchive/archive_copy_file.c b/archival/libunarchive/archive_copy_file.c
deleted file mode 100644 (file)
index 675bc6f..0000000
+++ /dev/null
@@ -1,44 +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 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 <unistd.h>
-
-#include "libbb.h"
-#include "unarchive.h"
-
-extern void archive_copy_file(const archive_handle_t *archive_handle, const int dst_fd)
-{
-       char buffer[512];
-       off_t chunksize = archive_handle->file_header->size;
-
-       while (chunksize != 0) {
-               size_t size;
-               if (chunksize > 512) {
-                       size = 512;
-               } else {
-                       size = chunksize;
-               }
-//             archive_xread_all(archive_handle, buffer, size);
-               size = archive_xread(archive_handle, buffer, size);
-
-               if (write(dst_fd, buffer, size) != size) {
-                       bb_error_msg_and_die ("Short write");
-               }
-               chunksize -= size;
-       }
-
-       return;
-}
diff --git a/archival/libunarchive/archive_xread.c b/archival/libunarchive/archive_xread.c
deleted file mode 100644 (file)
index 59b4d77..0000000
+++ /dev/null
@@ -1,33 +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.
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include "unarchive.h"
-#include "libbb.h"
-
-extern ssize_t archive_xread(const archive_handle_t *archive_handle, unsigned char *buf, const size_t count)
-{
-       ssize_t size;
-
-       size = bb_full_read(archive_handle->src_fd, buf, count);
-       if (size < 0) {
-               bb_perror_msg_and_die("Read error");
-       }
-
-       return(size);
-}
index cfe046b2701c3fbcd6b501c6fa5d2db77a1d0ba2..ba9ade2d5777c1b1424c89d62dca7dae8e708402 100644 (file)
@@ -24,7 +24,7 @@ extern void archive_xread_all(const archive_handle_t *archive_handle, void *buf,
 {
        ssize_t size;
 
-       size = archive_xread(archive_handle, buf, count);
+       size = bb_full_read(archive_handle->src_fd, buf, count);
        if (size != count) {
                bb_error_msg_and_die("Short read");
        }
index 23719cd7b7754bdd86a1cf3d2f53ac94e0f1c637..8084e352433e24ff176a9933404258bba912e288 100644 (file)
@@ -24,7 +24,7 @@ extern ssize_t archive_xread_all_eof(archive_handle_t *archive_handle, unsigned
 {
        ssize_t size;
 
-       size = archive_xread(archive_handle, buf, count);
+       size = bb_full_read(archive_handle->src_fd, buf, count);
        if ((size != 0) && (size != count)) {
                bb_perror_msg_and_die("Short read, read %d of %d", size, count);
        }
index 4dccb815dc4bd2cadb5c6006f12bb26f472592dd..bf3be5b354799d1c83dad7af1ec26f6f98dad19a 100644 (file)
@@ -79,7 +79,7 @@ extern void data_extract_all(archive_handle_t *archive_handle)
                        case S_IFREG: {
                                /* Regular file */
                                dst_fd = bb_xopen(file_header->name, O_WRONLY | O_CREAT | O_EXCL);
-                               archive_copy_file(archive_handle, dst_fd);
+                               bb_copyfd_size(archive_handle->src_fd, dst_fd, file_header->size);
                                close(dst_fd);
                                break;
                                }
index 8be2fa2e9553755bbe22e78de80c45454ced8071..ce5d4b8d03955634b028d2ff447eac93d933800c 100644 (file)
@@ -18,5 +18,5 @@
 
 extern void data_extract_to_stdout(archive_handle_t *archive_handle)
 {
-       archive_copy_file(archive_handle, fileno(stdout));
+       bb_copyfd_eof(archive_handle->src_fd, fileno(stdout));
 }
index d55189f42fc47f264d9efedf3f7ecc1a97d749e0..603535a4cbd20f229dadcfa6258becde05b5610a 100644 (file)
@@ -57,7 +57,7 @@ extern char get_header_tar(archive_handle_t *archive_handle)
        /* Align header */
        data_align(archive_handle, 512);
 
-       if (archive_xread(archive_handle, tar.raw, 512) != 512) {
+       if (bb_full_read(archive_handle->src_fd, tar.raw, 512) != 512) {
                /* Assume end of file */
                return(EXIT_FAILURE);
        }
index 77da4ef2eede4e1fe5faa4df39164bec15f002b6..c0315616e27f72cdef8c085594f04520e997bbf0 100644 (file)
  */
 extern void seek_by_char(const archive_handle_t *archive_handle, const unsigned int jump_size)
 {
-       unsigned int remaining = jump_size;
-       unsigned int read_amount;
-       RESERVE_CONFIG_BUFFER(buf, BUFSIZ);
-
-       while (remaining > 0) {
-               if (remaining > BUFSIZ) {
-                       read_amount = BUFSIZ;
-               } else {
-                       read_amount = remaining;
-               }
-               read_amount = archive_xread(archive_handle, buf, read_amount);
-               remaining -= read_amount;
+       if (jump_size) {
+               bb_full_fd_action(archive_handle->src_fd, -1, jump_size, NULL);
        }
-
-       RELEASE_CONFIG_BUFFER(buf);
 }
index b3fa447f25376180add14c1e7546111bff0b3b57..cf9bc04d3b12694f59dbf2f8cf71150cc70fcd34 100644 (file)
@@ -414,8 +414,7 @@ static int writeFileToTarball(const char *fileName, struct stat *statbuf,
        if ((tbInfo->hlInfo == NULL)
                && (S_ISREG(statbuf->st_mode))) {
                int inputFileFd;
-               char buffer[BUFSIZ];
-               ssize_t size = 0, readSize = 0;
+               ssize_t readSize = 0;
 
                /* open the file we want to archive, and make sure all is well */
                if ((inputFileFd = open(fileName, O_RDONLY)) < 0) {
@@ -424,18 +423,8 @@ static int writeFileToTarball(const char *fileName, struct stat *statbuf,
                }
 
                /* write the file to the archive */
-               while ((size = bb_full_read(inputFileFd, buffer, sizeof(buffer))) > 0) {
-                       if (bb_full_write(tbInfo->tarFd, buffer, size) != size) {
-                               /* Output file seems to have a problem */
-                               bb_error_msg(bb_msg_io_error, fileName);
-                               return (FALSE);
-                       }
-                       readSize += size;
-               }
-               if (size == -1) {
-                       bb_error_msg(bb_msg_io_error, fileName);
-                       return (FALSE);
-               }
+               readSize = bb_copyfd_eof(inputFileFd, tbInfo->tarFd);
+
                /* Pad the file up to the tar block size */
                for (; (readSize % TAR_BLOCK_SIZE) != 0; readSize++) {
                        write(tbInfo->tarFd, "\0", 1);
index 86527576728f23f837ad2a316fa47f5311bbcb87..62af6c5d54495c384df9f6f2fb65fc393f4698b9 100644 (file)
@@ -54,7 +54,7 @@ extern int cat_main(int argc, char **argv)
 
        do {
                if ((f = bb_wfopen_input(*argv)) != NULL) {
-                       int r = bb_copyfd(fileno(f), STDOUT_FILENO, 0);
+                       int r = bb_copyfd_eof(fileno(f), STDOUT_FILENO);
                        bb_fclose_nonstdin(f);
                        if (r >= 0) {
                                continue;
index 22a77e610bbe8ead54b4423af01ada8b4de67028..5ff49114bd0e08e787f9c5af4d4f6c2eccadf020 100644 (file)
@@ -136,7 +136,8 @@ extern long *find_pid_by_name( const char* pidName);
 extern char *find_real_root_device_name(const char* name);
 extern char *bb_get_line_from_file(FILE *file);
 extern char *bb_get_chomped_line_from_file(FILE *file);
-extern int   bb_copyfd(int fd1, int fd2, const off_t chunksize);
+extern int bb_copyfd_size(int fd1, int fd2, const off_t size);
+extern int bb_copyfd_eof(int fd1, int fd2);
 extern void  bb_xprint_and_close_file(FILE *file);
 extern int   bb_xprint_file_by_name(const char *filename);
 extern char  bb_process_escape_sequence(const char **ptr);
@@ -480,6 +481,6 @@ extern void xregcomp(regex_t *preg, const char *regex, int cflags);
 #define HASH_SHA1      1
 #define HASH_MD5       2
 extern int hash_fd(int fd, const size_t size, const uint8_t hash_algo, uint8_t *hashval);
-
+extern size_t bb_full_fd_action(int src_fd, int dst_fd, const size_t size, ssize_t (*action)(int fd, const void *, size_t));
 
 #endif /* __LIBCONFIG_H__ */
index 79ee99e37811bb2a5112f15d3d0d9fbf2faba8e9..bbf11b5570690e24ea9419dee37d0e84b45e4097 100644 (file)
@@ -90,12 +90,10 @@ extern char get_header_tar_gz(archive_handle_t *archive_handle);
 extern void seek_by_jump(const archive_handle_t *archive_handle, const unsigned int amount);
 extern void seek_by_char(const archive_handle_t *archive_handle, const unsigned int amount);
 
-extern ssize_t archive_xread(const archive_handle_t *archive_handle, unsigned char *buf, const size_t count);
 extern void archive_xread_all(const archive_handle_t *archive_handle, void *buf, const size_t count);
 extern ssize_t archive_xread_all_eof(archive_handle_t *archive_handle, unsigned char *buf, size_t count);
 
 extern void data_align(archive_handle_t *archive_handle, const unsigned short boundary);
-extern void archive_copy_file(const archive_handle_t *archive_handle, const int dst_fd);
 extern const llist_t *find_list_entry(const llist_t *list, const char *filename);
 
 extern int uncompressStream(int src_fd, int dst_fd);
index 5808ca48cd16ad339a7d3b6252e74f3a6b9d00a9..c9d239f9a48bbabbfc426fe4f91f035c16f9e130 100644 (file)
@@ -181,7 +181,7 @@ int copy_file(const char *source, const char *dest, int flags)
                        }
                }
 
-               if (bb_copyfd(fileno(sfp), fileno(dfp), 0) == -1)
+               if (bb_copyfd_eof(fileno(sfp), fileno(dfp)) == -1)
                        status = -1;
 
                if (fclose(dfp) < 0) {
index 05bed6b733a8f49c624f721c7275a54e0c23d170..0787c812f3b9bbfbb7a5dbcd5ffd81833b3abb01 100644 (file)
 #define BUFSIZ 4096
 #endif
 
-/* If chunksize is 0 copy until EOF */
-extern int bb_copyfd(int fd1, int fd2, const off_t chunksize)
+/* If size is 0 copy until EOF */
+extern size_t bb_full_fd_action(int src_fd, int dst_fd, const size_t size, ssize_t (*action)(int fd, const void *, size_t))
 {
-       ssize_t nread;
-       size_t size;
-       off_t remaining;
+       size_t read_total = 0;
        RESERVE_CONFIG_BUFFER(buffer,BUFSIZ);
 
-       remaining = size = BUFSIZ;
-       if (chunksize) {
-               remaining = chunksize;
-       }
+       while ((size == 0) || (read_total < size)) {
+               size_t read_try;
+               ssize_t read_actual;
 
-       do {
-               if (size > remaining) {
-                       size = remaining;
+               if ((size == 0) || (size - read_total > BUFSIZ)) {
+                       read_try = BUFSIZ;
+               } else {
+                       read_try = size - read_total;
                }
 
-               if ((nread = safe_read(fd1, buffer, size)) > 0) {
-                       if (bb_full_write(fd2, buffer, nread) < 0) {
+               read_actual = safe_read(src_fd, buffer, read_try);
+               if (read_actual > 0) {
+                       if (action && (action(dst_fd, buffer, (size_t) read_actual) != read_actual)) {
                                bb_perror_msg(bb_msg_write_error);      /* match Read error below */
                                break;
                        }
-                       if (chunksize && ((remaining -= nread) == 0)) {
-                               return 0;
-                       }
-               } else if (!nread) {
-                       if (chunksize) {
+               }
+               else if (read_actual == 0) {
+                       if (size) {
                                bb_error_msg("Unable to read all data");
-                               break;
                        }
-                       return 0;
-               } else {                                /* nread < 0 */
-                       bb_perror_msg("Read error");    /* match bb_msg_write_error above */
+                       break;
+               } else {
+                       /* read_actual < 0 */
+                       bb_perror_msg("Read error");
                        break;
                }
 
-       } while (1);
+               read_total += read_actual;
+       }
+
+       RELEASE_CONFIG_BUFFER(buffer);
 
-       return -1;
+       return(read_total);
+}
+
+
+extern int bb_copyfd_size(int fd1, int fd2, const off_t size)
+{
+       return(bb_full_fd_action(fd1, fd2, size, bb_full_write));
+}
+
+extern int bb_copyfd_eof(int fd1, int fd2)
+{
+       return(bb_full_fd_action(fd1, fd2, 0, bb_full_write));
 }
index 6d3667b60a282333d87b4ac3779302b424cc045c..161b398fad819106b7433926fcb9e7e270cf99fe 100644 (file)
@@ -28,7 +28,7 @@ extern void bb_xprint_and_close_file(FILE *file)
        bb_xfflush_stdout();
        /* Note: Do not use STDOUT_FILENO here, as this is a lib routine
         *       and the calling code may have reassigned stdout. */
-       if (bb_copyfd(fileno(file), fileno(stdout), 0) == -1) {
+       if (bb_copyfd_eof(fileno(file), fileno(stdout)) == -1) {
                /* bb_copyfd outputs any needed messages, so just die. */
                exit(bb_default_error_retval);
        }
index 56223dccb66d32ffba9e2df7497773d9bc8dffdc..2fd50e4c4aca0fc7851e38319f1bd1a4c4025dc3 100644 (file)
@@ -210,7 +210,7 @@ static int ftp_recieve(ftp_host_info_t *server, FILE *control_stream,
        }
 
        /* Copy the file */
-       if (bb_copyfd(fd_data, fd_local, filesize) == -1) {
+       if (bb_copyfd_size(fd_data, fd_local, filesize) == -1) {
                exit(EXIT_FAILURE);
        }
 
@@ -272,7 +272,7 @@ static int ftp_send(ftp_host_info_t *server, FILE *control_stream,
        }
 
        /* transfer the file  */
-       if (bb_copyfd(fd_local, fd_data, 0) == -1) {
+       if (bb_copyfd_eof(fd_local, fd_data) == -1) {
                exit(EXIT_FAILURE);
        }