copy_file_chunk uses streams now.
authorGlenn L McGrath <bug1@ihug.co.nz>
Wed, 11 Apr 2001 16:23:35 +0000 (16:23 -0000)
committerGlenn L McGrath <bug1@ihug.co.nz>
Wed, 11 Apr 2001 16:23:35 +0000 (16:23 -0000)
14 files changed:
Makefile
ar.c
archival/ar.c
archival/dpkg.c
archival/dpkg_deb.c
cp_mv.c
dpkg.c
dpkg_deb.c
include/libbb.h
libbb/copy_file.c
libbb/copy_file_chunk.c
libbb/deb_extract.c
libbb/get_ar_headers.c
libbb/libbb.h

index d2a07fa8d5074aaac59f635d614a90a814392625..ed59deb0d116db4c1b10816e390d01c0f34e8152 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -237,16 +237,16 @@ LIBBB       = libbb
 LIBBB_LIB = libbb.a
 LIBBB_CSRC= ask_confirmation.c check_wildcard_match.c chomp.c \
 concat_path_file.c copy_file.c copy_file_chunk.c create_path.c daemon.c \
-deb_extract.c device_open.c error_msg.c error_msg_and_die.c \
-find_mount_point.c find_pid_by_name.c find_root_device.c full_read.c \
-full_write.c get_ar_headers.c get_console.c get_last_path_component.c \
-get_line_from_file.c gz_open.c human_readable.c inode_hash.c \
-isdirectory.c kernel_version.c loop.c mode_string.c module_syscalls.c mtab.c \
-mtab_file.c my_getgrnam.c my_getgrgid.c my_getpwnam.c my_getpwnamegid.c \
-my_getpwuid.c parse_mode.c parse_number.c perror_msg.c perror_msg_and_die.c \
-print_file.c process_escape_sequence.c recursive_action.c safe_read.c \
-safe_strncpy.c syscalls.c syslog_msg_with_name.c time_string.c trim.c unzip.c \
-vdprintf.c verror_msg.c vperror_msg.c wfopen.c xfuncs.c xgetcwd.c xregcomp.c
+deb_extract.c device_open.c error_msg.c error_msg_and_die.c find_mount_point.c \
+find_pid_by_name.c find_root_device.c full_read.c full_write.c \
+get_ar_headers.c get_console.c get_last_path_component.c get_line_from_file.c \
+gz_open.c human_readable.c inode_hash.c isdirectory.c kernel_version.c loop.c \
+mode_string.c module_syscalls.c mtab.c mtab_file.c my_getgrnam.c my_getgrgid.c \
+my_getpwnam.c my_getpwnamegid.c my_getpwuid.c parse_mode.c parse_number.c \
+perror_msg.c perror_msg_and_die.c print_file.c process_escape_sequence.c \
+recursive_action.c safe_read.c safe_strncpy.c seek_ared_file.c syscalls.c \
+syslog_msg_with_name.c time_string.c trim.c untar.c unzip.c vdprintf.c \
+verror_msg.c vperror_msg.c wfopen.c xfuncs.c xgetcwd.c xregcomp.c
 
 LIBBB_OBJS=$(patsubst %.c,$(LIBBB)/%.o, $(LIBBB_CSRC))
 LIBBB_CFLAGS = -I$(LIBBB)
diff --git a/ar.c b/ar.c
index fbbc7aa64df04339b9b8037eb116579ada62f34b..8ab38682139112fc3c89d06eece9987744e53593 100644 (file)
--- a/ar.c
+++ b/ar.c
@@ -36,12 +36,13 @@ extern int ar_main(int argc, char **argv)
        const int extract_to_file = 8;  /* extract contents of archive */
        const int extract_to_stdout = 16;       /* extract to stdout */
 
+       FILE *src_file = NULL, *dst_file = NULL;
        int funct = 0, opt=0;
-       int srcFd=0, dstFd=0;
 
-       ar_headers_t head, *extract_list=NULL;
+       ar_headers_t *head, *extract_list=NULL;
 
-       extract_list = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));
+       extract_list = (ar_headers_t *) xcalloc(1, sizeof(ar_headers_t));
+       head = (ar_headers_t *) xcalloc(1, sizeof(ar_headers_t));
 
        while ((opt = getopt(argc, argv, "ovtpx")) != -1) {
                switch (opt) {
@@ -66,21 +67,22 @@ extern int ar_main(int argc, char **argv)
        }
  
        /* check the src filename was specified */
-       if (optind == argc)
+       if (optind == argc) {
                show_usage();
-       
-       if ( (srcFd = open(argv[optind], O_RDONLY)) < 0)
+       }
+
+       if ( (src_file = wfopen(argv[optind], "r")) < 0) {
                error_msg_and_die("Cannot read %s", argv[optind]);
+       }
 
        optind++;       
-       head = get_ar_headers(srcFd);
-
+       head = get_ar_headers(src_file);
        /* find files to extract or display */
        /* search through argv and build extract list */
-       for (;optind<argc; optind++) {
+       for (;optind < argc; optind++) {
                ar_headers_t *ar_entry;
-               ar_entry = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));
-               ar_entry = &head;
+               ar_entry = (ar_headers_t *) xcalloc(1, sizeof(ar_headers_t));
+               ar_entry = head;
                while (ar_entry->next != NULL) {
                        if (strcmp(argv[optind], ar_entry->name) == 0) {
                                ar_headers_t *tmp;
@@ -96,20 +98,20 @@ extern int ar_main(int argc, char **argv)
 
        /* if individual files not found extract all files */   
        if (extract_list->next==NULL) {
-               extract_list = &head;
+               extract_list = head;
        }
-       
+
        /* find files to extract or display */  
        while (extract_list->next != NULL) {
                if (funct & extract_to_file) {
-                       dstFd = open(extract_list->name, O_WRONLY | O_CREAT, extract_list->mode);                               
+                       dst_file = wfopen(extract_list->name, "w");                             
                }
                else if (funct & extract_to_stdout) {
-                       dstFd = fileno(stdout);
+                       dst_file = stdout;
                }
                if ((funct & extract_to_file) || (funct & extract_to_stdout)) {
-                       lseek(srcFd, extract_list->offset, SEEK_SET);
-                       copy_file_chunk(srcFd, dstFd, (off_t) extract_list->size);                      
+                       fseek(src_file, extract_list->offset, SEEK_SET);
+                       copy_file_chunk(src_file, dst_file, (off_t) extract_list->size);                        
                }
                if (funct & verbose) {
                        printf("%s %d/%d %8d %s ", mode_string(extract_list->mode), 
index fbbc7aa64df04339b9b8037eb116579ada62f34b..8ab38682139112fc3c89d06eece9987744e53593 100644 (file)
@@ -36,12 +36,13 @@ extern int ar_main(int argc, char **argv)
        const int extract_to_file = 8;  /* extract contents of archive */
        const int extract_to_stdout = 16;       /* extract to stdout */
 
+       FILE *src_file = NULL, *dst_file = NULL;
        int funct = 0, opt=0;
-       int srcFd=0, dstFd=0;
 
-       ar_headers_t head, *extract_list=NULL;
+       ar_headers_t *head, *extract_list=NULL;
 
-       extract_list = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));
+       extract_list = (ar_headers_t *) xcalloc(1, sizeof(ar_headers_t));
+       head = (ar_headers_t *) xcalloc(1, sizeof(ar_headers_t));
 
        while ((opt = getopt(argc, argv, "ovtpx")) != -1) {
                switch (opt) {
@@ -66,21 +67,22 @@ extern int ar_main(int argc, char **argv)
        }
  
        /* check the src filename was specified */
-       if (optind == argc)
+       if (optind == argc) {
                show_usage();
-       
-       if ( (srcFd = open(argv[optind], O_RDONLY)) < 0)
+       }
+
+       if ( (src_file = wfopen(argv[optind], "r")) < 0) {
                error_msg_and_die("Cannot read %s", argv[optind]);
+       }
 
        optind++;       
-       head = get_ar_headers(srcFd);
-
+       head = get_ar_headers(src_file);
        /* find files to extract or display */
        /* search through argv and build extract list */
-       for (;optind<argc; optind++) {
+       for (;optind < argc; optind++) {
                ar_headers_t *ar_entry;
-               ar_entry = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));
-               ar_entry = &head;
+               ar_entry = (ar_headers_t *) xcalloc(1, sizeof(ar_headers_t));
+               ar_entry = head;
                while (ar_entry->next != NULL) {
                        if (strcmp(argv[optind], ar_entry->name) == 0) {
                                ar_headers_t *tmp;
@@ -96,20 +98,20 @@ extern int ar_main(int argc, char **argv)
 
        /* if individual files not found extract all files */   
        if (extract_list->next==NULL) {
-               extract_list = &head;
+               extract_list = head;
        }
-       
+
        /* find files to extract or display */  
        while (extract_list->next != NULL) {
                if (funct & extract_to_file) {
-                       dstFd = open(extract_list->name, O_WRONLY | O_CREAT, extract_list->mode);                               
+                       dst_file = wfopen(extract_list->name, "w");                             
                }
                else if (funct & extract_to_stdout) {
-                       dstFd = fileno(stdout);
+                       dst_file = stdout;
                }
                if ((funct & extract_to_file) || (funct & extract_to_stdout)) {
-                       lseek(srcFd, extract_list->offset, SEEK_SET);
-                       copy_file_chunk(srcFd, dstFd, (off_t) extract_list->size);                      
+                       fseek(src_file, extract_list->offset, SEEK_SET);
+                       copy_file_chunk(src_file, dst_file, (off_t) extract_list->size);                        
                }
                if (funct & verbose) {
                        printf("%s %d/%d %8d %s ", mode_string(extract_list->mode), 
index 55d97adda0590ffe43dfd13349119c0c6cbd680b..d0728d995c934bd8b70041662241c16d27490c2c 100644 (file)
@@ -565,8 +565,8 @@ static int dpkg_dounpack(package_t *pkg)
        int r = 0, i;
        int status = TRUE;
        char *cwd = xgetcwd(0);
-       char *src_file = NULL;
-       char *dst_file = NULL;
+       char *src_filename = NULL;
+       char *dst_filename = NULL;
 //     char *lst_file = NULL;
        char *adminscripts[] = { "prerm", "postrm", "preinst", "postinst",
                        "conffiles", "md5sums", "shlibs", "templates" };
@@ -576,37 +576,37 @@ static int dpkg_dounpack(package_t *pkg)
        if(cwd==NULL)
                exit(EXIT_FAILURE);
        chdir("/");
-       deb_extract(dpkg_deb_extract, "/", pkg->file);
+       deb_extract(pkg->file, dpkg_deb_extract, "/");
 
        /* Installs the package scripts into the info directory */
        for (i = 0; i < sizeof(adminscripts) / sizeof(adminscripts[0]); i++) {
                struct stat src_stat_buf;
-               int src_fd = 0, dst_fd = 0;
+               FILE *src_file = NULL, *dst_file = NULL;
 
                /* The full path of the current location of the admin file */
-               src_file = xrealloc(src_file, strlen(dpkgcidir) + strlen(pkg->package) + strlen(adminscripts[i]) + 1);
-               sprintf(src_file, "%s%s/%s", dpkgcidir, pkg->package, adminscripts[i]);
+               src_filename = xrealloc(src_filename, strlen(dpkgcidir) + strlen(pkg->package) + strlen(adminscripts[i]) + 1);
+               sprintf(src_filename, "%s%s/%s", dpkgcidir, pkg->package, adminscripts[i]);
 
                /* the full path of where we want the file to be copied to */
-               dst_file = xrealloc(dst_file, strlen(infodir) + strlen(pkg->package) + strlen(adminscripts[i]) + 1);
-               sprintf(dst_file, "%s%s.%s", infodir, pkg->package, adminscripts[i]);
+               dst_filename = xrealloc(dst_filename, strlen(infodir) + strlen(pkg->package) + strlen(adminscripts[i]) + 1);
+               sprintf(dst_filename, "%s%s.%s", infodir, pkg->package, adminscripts[i]);
 
                /*
                 * copy admin file to permanent home
                 * NOTE: Maybe merge this behaviour into libb/copy_file.c
                 */
-               if (lstat(src_file, &src_stat_buf) == 0) {
-                       if ((src_fd = open(src_file, O_RDONLY)) != -1) {
-                               if ((dst_fd = open(dst_file, O_WRONLY | O_CREAT, 0644)) == -1) {
+               if (lstat(src_filename, &src_stat_buf) == 0) {
+                       if ((src_file = wfopen(src_filename, "r")) != NULL) {
+                               if ((dst_file = wfopen(dst_filename, "w")) == NULL) {
                                        status = FALSE;
-                                       perror_msg("Opening %s", dst_file);
+                                       perror_msg("Opening %s", dst_filename);
                                }
-                               copy_file_chunk(src_fd, dst_fd, src_stat_buf.st_size);
-                               close(src_fd);
-                               close(dst_fd);
+                               copy_file_chunk(src_file, dst_file, src_stat_buf.st_size);
+                               fclose(src_file);
+                               fclose(dst_file);
                        } else {
                                status = FALSE;
-                               error_msg("couldnt open [%s]\n", src_file);
+                               error_msg("couldnt open [%s]\n", src_filename);
                        }
                }
        }
@@ -671,7 +671,7 @@ static int dpkg_unpackcontrol(package_t *pkg)
        strcat(tmp_name, pkg->package);
 
        /* extract control.tar.gz to the full extraction path */
-       deb_extract(dpkg_deb_control, tmp_name, pkg->file);
+       deb_extract(pkg->file, dpkg_deb_control, tmp_name);
 
        /* parse the extracted control file */
        strcat(tmp_name, "/control");
index 450b04b8ed943365bf0673f607c3d0c1d541d9cc..3cdd4ffaed10f0bd6f6493dc428ce0d73ad144c2 100644 (file)
@@ -68,7 +68,7 @@ extern int dpkg_deb_main(int argc, char **argv)
                        strcpy(target_dir, argv[optind + 1]);
                }
        }
-       deb_extract(optflag, target_dir, argv[optind]);
+       deb_extract(argv[optind], optflag, target_dir);
 /*     else if (optflag & dpkg_deb_info) {
                extract_flag = TRUE;
                extract_to_stdout = TRUE;
diff --git a/cp_mv.c b/cp_mv.c
index f4b5cd0d63515e8f8734fcc195ba8001e302a461..3c19f1a0495bcb1f6b1989ee140ae480a0f254cf 100644 (file)
--- a/cp_mv.c
+++ b/cp_mv.c
@@ -147,7 +147,7 @@ cp_mv_Action(const char *fileName, struct stat *statbuf, void* junk)
                        add_to_ino_dev_hashtable(statbuf, destName);
                }
        }
-       return copy_file(fileName, destName, preserveFlag, followLinks, forceFlag);
+       return copy_file(fileName, destName, preserveFlag, followLinks, forceFlag, FALSE);
 }
 
 static int
diff --git a/dpkg.c b/dpkg.c
index 55d97adda0590ffe43dfd13349119c0c6cbd680b..d0728d995c934bd8b70041662241c16d27490c2c 100644 (file)
--- a/dpkg.c
+++ b/dpkg.c
@@ -565,8 +565,8 @@ static int dpkg_dounpack(package_t *pkg)
        int r = 0, i;
        int status = TRUE;
        char *cwd = xgetcwd(0);
-       char *src_file = NULL;
-       char *dst_file = NULL;
+       char *src_filename = NULL;
+       char *dst_filename = NULL;
 //     char *lst_file = NULL;
        char *adminscripts[] = { "prerm", "postrm", "preinst", "postinst",
                        "conffiles", "md5sums", "shlibs", "templates" };
@@ -576,37 +576,37 @@ static int dpkg_dounpack(package_t *pkg)
        if(cwd==NULL)
                exit(EXIT_FAILURE);
        chdir("/");
-       deb_extract(dpkg_deb_extract, "/", pkg->file);
+       deb_extract(pkg->file, dpkg_deb_extract, "/");
 
        /* Installs the package scripts into the info directory */
        for (i = 0; i < sizeof(adminscripts) / sizeof(adminscripts[0]); i++) {
                struct stat src_stat_buf;
-               int src_fd = 0, dst_fd = 0;
+               FILE *src_file = NULL, *dst_file = NULL;
 
                /* The full path of the current location of the admin file */
-               src_file = xrealloc(src_file, strlen(dpkgcidir) + strlen(pkg->package) + strlen(adminscripts[i]) + 1);
-               sprintf(src_file, "%s%s/%s", dpkgcidir, pkg->package, adminscripts[i]);
+               src_filename = xrealloc(src_filename, strlen(dpkgcidir) + strlen(pkg->package) + strlen(adminscripts[i]) + 1);
+               sprintf(src_filename, "%s%s/%s", dpkgcidir, pkg->package, adminscripts[i]);
 
                /* the full path of where we want the file to be copied to */
-               dst_file = xrealloc(dst_file, strlen(infodir) + strlen(pkg->package) + strlen(adminscripts[i]) + 1);
-               sprintf(dst_file, "%s%s.%s", infodir, pkg->package, adminscripts[i]);
+               dst_filename = xrealloc(dst_filename, strlen(infodir) + strlen(pkg->package) + strlen(adminscripts[i]) + 1);
+               sprintf(dst_filename, "%s%s.%s", infodir, pkg->package, adminscripts[i]);
 
                /*
                 * copy admin file to permanent home
                 * NOTE: Maybe merge this behaviour into libb/copy_file.c
                 */
-               if (lstat(src_file, &src_stat_buf) == 0) {
-                       if ((src_fd = open(src_file, O_RDONLY)) != -1) {
-                               if ((dst_fd = open(dst_file, O_WRONLY | O_CREAT, 0644)) == -1) {
+               if (lstat(src_filename, &src_stat_buf) == 0) {
+                       if ((src_file = wfopen(src_filename, "r")) != NULL) {
+                               if ((dst_file = wfopen(dst_filename, "w")) == NULL) {
                                        status = FALSE;
-                                       perror_msg("Opening %s", dst_file);
+                                       perror_msg("Opening %s", dst_filename);
                                }
-                               copy_file_chunk(src_fd, dst_fd, src_stat_buf.st_size);
-                               close(src_fd);
-                               close(dst_fd);
+                               copy_file_chunk(src_file, dst_file, src_stat_buf.st_size);
+                               fclose(src_file);
+                               fclose(dst_file);
                        } else {
                                status = FALSE;
-                               error_msg("couldnt open [%s]\n", src_file);
+                               error_msg("couldnt open [%s]\n", src_filename);
                        }
                }
        }
@@ -671,7 +671,7 @@ static int dpkg_unpackcontrol(package_t *pkg)
        strcat(tmp_name, pkg->package);
 
        /* extract control.tar.gz to the full extraction path */
-       deb_extract(dpkg_deb_control, tmp_name, pkg->file);
+       deb_extract(pkg->file, dpkg_deb_control, tmp_name);
 
        /* parse the extracted control file */
        strcat(tmp_name, "/control");
index 450b04b8ed943365bf0673f607c3d0c1d541d9cc..3cdd4ffaed10f0bd6f6493dc428ce0d73ad144c2 100644 (file)
@@ -68,7 +68,7 @@ extern int dpkg_deb_main(int argc, char **argv)
                        strcpy(target_dir, argv[optind + 1]);
                }
        }
-       deb_extract(optflag, target_dir, argv[optind]);
+       deb_extract(argv[optind], optflag, target_dir);
 /*     else if (optflag & dpkg_deb_info) {
                extract_flag = TRUE;
                extract_to_stdout = TRUE;
index 279b4e78bc7b92d9b788be50a7e20d074afc6c78..54e8525127338ce1646b26b0661cc95fd8adf289 100644 (file)
@@ -93,9 +93,9 @@ int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name);
 void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name);
 void reset_ino_dev_hashtable(void);
 
-int copy_file(const char *srcName, const char *destName,
-                int setModes, int followLinks, int forceFlag);
-int copy_file_chunk(int srcFd, int dstFd, off_t remaining);
+int copy_file(const char *src_name, const char *dst_name,
+                int set_modes, int follow_links, int force_flag, int quiet_flag);
+int copy_file_chunk(FILE *src_file, FILE *dst_file, off_t chunksize);
 char *buildName(const char *dirName, const char *fileName);
 int makeString(int argc, const char **argv, char *buf, int bufLen);
 char *getChunk(int size);
@@ -225,10 +225,12 @@ typedef struct ar_headers_s {
        off_t offset;
        struct ar_headers_s *next;
 } ar_headers_t;
-extern ar_headers_t get_ar_headers(int srcFd);
-extern int deb_extract(int optflags, const char *dir_name, const char *deb_filename);
-
+extern ar_headers_t *get_ar_headers(FILE *in_file);
+extern int seek_ared_file(FILE *in_file, ar_headers_t *headers, const char *tar_gz_file);
+extern int deb_extract(const char *package_filename, const int function, char *target_dir);
+extern int untar(FILE *src_tar_file, int untar_function, char *base_path);
 extern int unzip(FILE *l_in_file, FILE *l_out_file);
 extern void gz_close(int gunzip_pid);
 extern int gz_open(FILE *compressed_file, int *pid);
+
 #endif /* __LIBBB_H__ */
index 64b65b9faf69a92343bdeb762c151790552d875b..eb9cb1a16b6bec90458465340ead4b39a80d97dd 100644 (file)
  * -Erik Andersen
  */
 int
-copy_file(const char *srcName, const char *destName,
-                int setModes, int followLinks, int forceFlag)
+copy_file(const char *src_name, const char *dst_name,
+                int set_modes, int follow_links, int force_flag, int quiet_flag)
 {
-       int rfd;
-       int wfd;
-       int status;
+       FILE *src_file = NULL;
+       FILE *dst_file = NULL;
        struct stat srcStatBuf;
        struct stat dstStatBuf;
        struct utimbuf times;
+       int src_status;
+       int dst_status;
+
+       if (follow_links == TRUE) {
+               src_status = stat(src_name, &srcStatBuf);
+               dst_status = stat(dst_name, &dstStatBuf);
+       } else {
+               src_status = lstat(src_name, &srcStatBuf);
+               dst_status = lstat(dst_name, &dstStatBuf);
+       }
 
-       if (followLinks == TRUE)
-               status = stat(srcName, &srcStatBuf);
-       else
-               status = lstat(srcName, &srcStatBuf);
-
-       if (status < 0) {
-               perror_msg("%s", srcName);
+       if (src_status < 0) {
+               if (!quiet_flag) {
+                       perror_msg("%s", src_name);
+               }
                return FALSE;
        }
 
-       if (followLinks == TRUE)
-               status = stat(destName, &dstStatBuf);
-       else
-               status = lstat(destName, &dstStatBuf);
-
-       if (status < 0 || forceFlag==TRUE) {
-               unlink(destName);
+       if ((dst_status < 0) || force_flag) {
+               unlink(dst_name);
                dstStatBuf.st_ino = -1;
                dstStatBuf.st_dev = -1;
        }
 
        if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
                (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
-               error_msg("Copying file \"%s\" to itself", srcName);
+               if (!quiet_flag) {
+                       error_msg("Copying file \"%s\" to itself", src_name);
+               }
                return FALSE;
        }
 
        if (S_ISDIR(srcStatBuf.st_mode)) {
                //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
                /* Make sure the directory is writable */
-               status = mkdir(destName, 0777777 ^ umask(0));
-               if (status < 0 && errno != EEXIST) {
-                       perror_msg("%s", destName);
+               dst_status = create_path(dst_name, 0777777 ^ umask(0));
+               if ((dst_status < 0) && (errno != EEXIST)) {
+                       if (!quiet_flag) {
+                               perror_msg("%s", dst_name);
+                       }
                        return FALSE;
                }
        } else if (S_ISLNK(srcStatBuf.st_mode)) {
@@ -92,80 +97,92 @@ copy_file(const char *srcName, const char *destName,
 
                //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
                /* Warning: This could possibly truncate silently, to BUFSIZ chars */
-               link_size = readlink(srcName, &link_val[0], BUFSIZ);
+               link_size = readlink(src_name, &link_val[0], BUFSIZ);
                if (link_size < 0) {
-                       perror_msg("%s", srcName);
+                       if (quiet_flag) {
+                               perror_msg("%s", src_name);
+                       }
                        return FALSE;
                }
                link_val[link_size] = '\0';
-               status = symlink(link_val, destName);
-               if (status < 0) {
-                       perror_msg("%s", destName);
+               src_status = symlink(link_val, dst_name);
+               if (src_status < 0) {
+                       if (!quiet_flag) {
+                               perror_msg("%s", dst_name);
+                       }
                        return FALSE;
                }
 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
-               if (setModes == TRUE) {
+               if (set_modes == TRUE) {
                        /* Try to set owner, but fail silently like GNU cp */
-                       lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
+                       lchown(dst_name, srcStatBuf.st_uid, srcStatBuf.st_gid);
                }
 #endif
                return TRUE;
        } else if (S_ISFIFO(srcStatBuf.st_mode)) {
                //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
-               if (mkfifo(destName, 0644) < 0) {
-                       perror_msg("%s", destName);
+               if (mkfifo(dst_name, 0644) < 0) {
+                       if (!quiet_flag) {
+                               perror_msg("%s", dst_name);
+                       }
                        return FALSE;
                }
        } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
                           || S_ISSOCK(srcStatBuf.st_mode)) {
                //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
-               if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev) < 0) {
-                       perror_msg("%s", destName);
+               if (mknod(dst_name, srcStatBuf.st_mode, srcStatBuf.st_rdev) < 0) {
+                       if (!quiet_flag) {
+                               perror_msg("%s", dst_name);
+                       }
                        return FALSE;
                }
        } else if (S_ISREG(srcStatBuf.st_mode)) {
                //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
-               rfd = open(srcName, O_RDONLY);
-               if (rfd < 0) {
-                       perror_msg("%s", srcName);
+               src_file = fopen(src_name, "r");
+               if (src_file == NULL) {
+                       if (!quiet_flag) {
+                               perror_msg("%s", src_name);
+                       }
                        return FALSE;
                }
 
-               wfd = open(destName, O_WRONLY | O_CREAT | O_TRUNC,
-                                srcStatBuf.st_mode);
-               if (wfd < 0) {
-                       perror_msg("%s", destName);
-                       close(rfd);
+               dst_file = fopen(dst_name, "w");
+               if (dst_file == NULL) {
+                       if (!quiet_flag) {
+                               perror_msg("%s", dst_name);
+                       }
+                       fclose(src_file);
                        return FALSE;
                }
 
-               if (copy_file_chunk(rfd, wfd, srcStatBuf.st_size)==FALSE)
-                       goto error_exit;        
-               
-               close(rfd);
-               if (close(wfd) < 0) {
+               if (copy_file_chunk(src_file, dst_file, srcStatBuf.st_size)==FALSE) {
+                       goto error_exit;
+               }
+
+               fclose(src_file);
+               if (fclose(dst_file) < 0) {
                        return FALSE;
                }
        }
 
-       if (setModes == TRUE) {
+       if (set_modes == TRUE) {
                /* This is fine, since symlinks never get here */
-               if (chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0)
-                       perror_msg_and_die("%s", destName);
-               if (chmod(destName, srcStatBuf.st_mode) < 0)
-                       perror_msg_and_die("%s", destName);
+               if (chown(dst_name, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0)
+                       perror_msg("%s", dst_name);
+               if (chmod(dst_name, srcStatBuf.st_mode) < 0)
+                       perror_msg("%s", dst_name);
                times.actime = srcStatBuf.st_atime;
                times.modtime = srcStatBuf.st_mtime;
-               if (utime(destName, &times) < 0)
-                       perror_msg_and_die("%s", destName);
+               if (utime(dst_name, &times) < 0)
+                       perror_msg("%s", dst_name);
        }
 
        return TRUE;
 
-  error_exit:
-       perror_msg("%s", destName);
-       close(rfd);
-       close(wfd);
+error_exit:
+       perror_msg("%s", dst_name);
+       fclose(src_file);
+       fclose(dst_file);
 
        return FALSE;
 }
index bf0b06e1a89fe6a0c6e1204ca8ed58082e88cfa2..e887cbe9f9ede9f03e660eeda80105d2a33772e6 100644 (file)
 /*
  * Copy chunksize bytes between two file descriptors
  */
-int copy_file_chunk(int srcfd, int dstfd, off_t chunksize)
+extern int copy_file_chunk(FILE *src_file, FILE *dst_file, off_t chunksize)
 {
-        off_t size;
-        char buffer[BUFSIZ]; /* BUFSIZ is declared in stdio.h */
-
-        while (chunksize > 0) {
-                if (chunksize > BUFSIZ)
-                        size = BUFSIZ;
-                else
-                        size = chunksize;
-                if (full_write(dstfd, buffer, full_read(srcfd, buffer, size)) < size)
-                        return(FALSE);
-                chunksize -= size;
-        }
-        return (TRUE);
+       off_t size, amount_written;
+       char buffer[BUFSIZ]; /* BUFSIZ is declared in stdio.h */
+       
+       clearerr(src_file);
+       clearerr(dst_file);
+       while (chunksize > 0) {
+               if (chunksize > BUFSIZ) {
+                       size = BUFSIZ;
+               } else {
+                       size = chunksize;
+               }
+               amount_written = fwrite(buffer, 1, fread(buffer, 1, size, src_file), dst_file);
+               if (amount_written != size) {
+                       error_msg("Couldnt write correct amount");
+                       return(FALSE);
+               }
+               chunksize -= amount_written;
+       }
+       return (TRUE);
 }
 
-
 /* END CODE */
 /*
 Local Variables:
index 358c4d0a8a6e3f01b80ed3bc369e3d39b7d3618d..35ff99671b92f264991158425531fc2b63a511bc 100644 (file)
 #include <signal.h>
 #include "libbb.h"
 
-/* From gunzip.c */
-extern int gz_open(FILE *compressed_file, int *pid);
-extern void gz_close(int gunzip_pid);
+const int dpkg_deb_contents = 1;
+const int dpkg_deb_control = 2;
+const int dpkg_deb_info = 4;
+const int dpkg_deb_extract = 8;
+const int dpkg_deb_verbose_extract = 16;
+const int dpkg_deb_list = 32;
 
-extern int tar_unzip_init(int tarFd);
-extern int readTarFile(int tarFd, int extractFlag, int listFlag, 
-       int tostdoutFlag, int verboseFlag, char** extractList, char** excludeList);
-
-extern int deb_extract(int optflags, const char *dir_name, const char *deb_filename)
+extern int deb_extract(const char *package_filename, const int function, char *target_dir)
 {
-       const int dpkg_deb_contents = 1;
-       const int dpkg_deb_control = 2;
-//     const int dpkg_deb_info = 4;
-       const int dpkg_deb_extract = 8;
-       const int dpkg_deb_verbose_extract = 16;
-       const int dpkg_deb_list = 32;
 
-       char **extract_list = NULL;
-       ar_headers_t *ar_headers = NULL;
-       char ar_filename[15];
-       int extract_flag = FALSE;
-       int list_flag = FALSE;
-       int verbose_flag = FALSE;
-       int extract_to_stdout = FALSE;
-       int srcFd = 0;
-       int status;
-       pid_t pid;
-       FILE *comp_file = NULL;
+       FILE *deb_file, *uncompressed_file;
+       ar_headers_t *headers = NULL;
+       char *ared_file;
+       int gunzip_pid;
 
-       if (dpkg_deb_contents == (dpkg_deb_contents & optflags)) {
-               strcpy(ar_filename, "data.tar.gz");
-               verbose_flag = TRUE;
-               list_flag = TRUE;
-       }
-       if (dpkg_deb_list == (dpkg_deb_list & optflags)) {
-               strcpy(ar_filename, "data.tar.gz");
-               list_flag = TRUE;
-       }
-       if (dpkg_deb_control == (dpkg_deb_control & optflags)) {
-               strcpy(ar_filename, "control.tar.gz");  
-               extract_flag = TRUE;
-       }
-       if (dpkg_deb_extract == (dpkg_deb_extract & optflags)) {
-               strcpy(ar_filename, "data.tar.gz");
-               extract_flag = TRUE;
-       }
-       if (dpkg_deb_verbose_extract == (dpkg_deb_verbose_extract & optflags)) {
-               strcpy(ar_filename, "data.tar.gz");
-               extract_flag = TRUE;
-               list_flag = TRUE;
+       if ((function == dpkg_deb_info) || (function == dpkg_deb_control)) {
+               ared_file = xstrdup("control.tar.gz");
+       } else {
+               ared_file = xstrdup("data.tar.gz");
        }
 
-       ar_headers = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));    
-       srcFd = open(deb_filename, O_RDONLY);
+       /* open the debian package to be worked on */
+       deb_file = wfopen(package_filename, "r");
+
+       headers = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));       
        
-       *ar_headers = get_ar_headers(srcFd);
-       if (ar_headers->next == NULL) {
-               error_msg_and_die("Couldnt find %s in %s", ar_filename, deb_filename);
+       /* get a linked list of all ar entries */
+       if ((headers = get_ar_headers(deb_file)) == NULL) {
+               error_msg("Couldnt get ar headers\n");
+               return(EXIT_FAILURE);
        }
 
-       while (ar_headers->next != NULL) {
-               if (strcmp(ar_headers->name, ar_filename) == 0) {
-                       break;
-               }
-               ar_headers = ar_headers->next;
+       /* seek to the start of the .tar.gz file within the ar file*/
+       if (seek_ared_file(deb_file, headers, ared_file) == EXIT_FAILURE) {
+               error_msg("Couldnt seek to ar file");
        }
-       lseek(srcFd, ar_headers->offset, SEEK_SET);
-       /* Uncompress the file */
-       comp_file = fdopen(srcFd, "r");
-       if ((srcFd = gz_open(comp_file, &pid)) == EXIT_FAILURE) {
-           error_msg_and_die("Couldnt unzip file");
-       }
-       if ( dir_name != NULL) { 
-               if (is_directory(dir_name, TRUE, NULL)==FALSE) {
-                       mkdir(dir_name, 0755);
-               }
-               if (chdir(dir_name)==-1) {
-                       error_msg_and_die("Cannot change to dir %s", dir_name);
-               }
-       }
-       status = readTarFile(srcFd, extract_flag, list_flag, 
-               extract_to_stdout, verbose_flag, NULL, extract_list);
+
+       /* open a stream of decompressed data */
+       uncompressed_file = fdopen(gz_open(deb_file, &gunzip_pid), "r");
+
+       /* get a list of all tar headers inside the .gz file */
+       untar(uncompressed_file, dpkg_deb_extract, target_dir);
 
        /* we are deliberately terminating the child so we can safely ignore this */
        signal(SIGTERM, SIG_IGN);
-       gz_close(pid);
-       close(srcFd);
-       fclose(comp_file);
-
-       return status;
+       gz_close(gunzip_pid);
+       fclose(deb_file);
+       fclose(uncompressed_file);
+       return(EXIT_SUCCESS);
 }
\ No newline at end of file
index cf7de5c2a6012fbb02d375a5a81dc7c13f72d092..151caafe1e88020f6fe4284ecd7f545e030b3021 100644 (file)
@@ -30,7 +30,7 @@
 #include <unistd.h>
 #include "libbb.h"
 
-extern ar_headers_t get_ar_headers(int srcFd)
+extern ar_headers_t *get_ar_headers(FILE *in_file)
 {
        typedef struct raw_ar_header_s {        /* Byte Offset */
                char name[16];  /*  0-15 */
@@ -44,31 +44,33 @@ extern ar_headers_t get_ar_headers(int srcFd)
 
        raw_ar_header_t raw_ar_header;
 
-       ar_headers_t *head, *entry;
+       ar_headers_t *ar_list, *ar_tmp, *ar_entry;
        char ar_magic[8];
        char *long_name=NULL;
        
-       head = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));
-       entry = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));
-       
        /* check ar magic */
-       if (full_read(srcFd, ar_magic, 8) != 8) {
-               error_msg_and_die("cannot read magic");
+       if (fread(ar_magic, 1, 8, in_file) != 8) {
+               error_msg("cannot read magic");
+               return(NULL);
        }
 
        if (strncmp(ar_magic,"!<arch>",7) != 0) {
-               error_msg_and_die("invalid magic");
+               error_msg("invalid magic");
+               return(NULL);
        }
+       
+       ar_list = (ar_headers_t *) xcalloc(1, sizeof(ar_headers_t));
 
-       while (full_read(srcFd, (char *) &raw_ar_header, 60)==60) {
+       while (fread((char *) &raw_ar_header, 1, 60, in_file) == 60) {
+               ar_entry = (ar_headers_t *) xcalloc(1, sizeof(ar_headers_t));
                /* check the end of header markers are valid */
-               if ((raw_ar_header.fmag[0]!='`') || (raw_ar_header.fmag[1]!='\n')) {
+               if ((raw_ar_header.fmag[0] != '`') || (raw_ar_header.fmag[1] != '\n')) {
                        char newline;
-                       if (raw_ar_header.fmag[1]!='`') {
+                       if (raw_ar_header.fmag[1] != '`') {
                                break;
                        }
                        /* some version of ar, have an extra '\n' after each entry */
-                       read(srcFd, &newline, 1);
+                       fread(&newline, 1, 1, in_file);
                        if (newline!='\n') {
                                break;
                        }
@@ -77,42 +79,46 @@ extern ar_headers_t get_ar_headers(int srcFd)
                        /* dont worry about adding the last '\n', we dont need it now */
                }
                
-               entry->size = (off_t) atoi(raw_ar_header.size);
+               ar_entry->size = (size_t) atoi(raw_ar_header.size);
                /* long filenames have '/' as the first character */
                if (raw_ar_header.name[0] == '/') {
                        if (raw_ar_header.name[1] == '/') {
                                /* multiple long filenames are stored as data in one entry */
-                               long_name = (char *) xrealloc(long_name, entry->size);
-                               full_read(srcFd, long_name, entry->size);
+                               long_name = (char *) xrealloc(long_name, ar_entry->size);
+                               fread(long_name, 1, ar_entry->size, in_file);
                                continue;
                        }
                        else {
                                /* The number after the '/' indicates the offset in the ar data section
                                        (saved in variable long_name) that conatains the real filename */
                                const int long_name_offset = (int) atoi((char *) &raw_ar_header.name[1]);
-                               entry->name = xmalloc(strlen(&long_name[long_name_offset]));
-                               strcpy(entry->name, &long_name[long_name_offset]);
+                               ar_entry->name = xmalloc(strlen(&long_name[long_name_offset]));
+                               strcpy(ar_entry->name, &long_name[long_name_offset]);
                        }
                }
                else {
                        /* short filenames */
-                       entry->name = xmalloc(16);
-                       strncpy(entry->name, raw_ar_header.name, 16);
+                       ar_entry->name = xmalloc(16);
+                       ar_entry->name = strncpy(ar_entry->name, raw_ar_header.name, 16);
                }
-               entry->name[strcspn(entry->name, " /")]='\0';
+               ar_entry->name[strcspn(ar_entry->name, " /")]='\0';
 
                /* convert the rest of the now valid char header to its typed struct */ 
-               parse_mode(raw_ar_header.mode, &entry->mode);
-               entry->mtime = atoi(raw_ar_header.date);
-               entry->uid = atoi(raw_ar_header.uid);
-               entry->gid = atoi(raw_ar_header.gid);
-               entry->offset = lseek(srcFd, 0, SEEK_CUR);
+               parse_mode(raw_ar_header.mode, &ar_entry->mode);
+               ar_entry->mtime = atoi(raw_ar_header.date);
+               ar_entry->uid = atoi(raw_ar_header.uid);
+               ar_entry->gid = atoi(raw_ar_header.gid);
+               ar_entry->offset = ftell(in_file);
+               ar_entry->next = NULL;
 
-               /* add this entries header to our combined list */
-               entry->next = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));
-               *entry->next = *head;
-               *head = *entry;
-               lseek(srcFd, (off_t) entry->size, SEEK_CUR);
+               fseek(in_file, (off_t) ar_entry->size, SEEK_CUR);
+
+               ar_tmp = (ar_headers_t *) xcalloc(1, sizeof(ar_headers_t));
+               *ar_tmp = *ar_list;
+               *ar_list = *ar_entry;
+               free(ar_entry);
+               ar_list->next = ar_tmp;
        }
-       return(*head);
-}
\ No newline at end of file
+
+       return(ar_list);
+}
index 279b4e78bc7b92d9b788be50a7e20d074afc6c78..54e8525127338ce1646b26b0661cc95fd8adf289 100644 (file)
@@ -93,9 +93,9 @@ int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name);
 void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name);
 void reset_ino_dev_hashtable(void);
 
-int copy_file(const char *srcName, const char *destName,
-                int setModes, int followLinks, int forceFlag);
-int copy_file_chunk(int srcFd, int dstFd, off_t remaining);
+int copy_file(const char *src_name, const char *dst_name,
+                int set_modes, int follow_links, int force_flag, int quiet_flag);
+int copy_file_chunk(FILE *src_file, FILE *dst_file, off_t chunksize);
 char *buildName(const char *dirName, const char *fileName);
 int makeString(int argc, const char **argv, char *buf, int bufLen);
 char *getChunk(int size);
@@ -225,10 +225,12 @@ typedef struct ar_headers_s {
        off_t offset;
        struct ar_headers_s *next;
 } ar_headers_t;
-extern ar_headers_t get_ar_headers(int srcFd);
-extern int deb_extract(int optflags, const char *dir_name, const char *deb_filename);
-
+extern ar_headers_t *get_ar_headers(FILE *in_file);
+extern int seek_ared_file(FILE *in_file, ar_headers_t *headers, const char *tar_gz_file);
+extern int deb_extract(const char *package_filename, const int function, char *target_dir);
+extern int untar(FILE *src_tar_file, int untar_function, char *base_path);
 extern int unzip(FILE *l_in_file, FILE *l_out_file);
 extern void gz_close(int gunzip_pid);
 extern int gz_open(FILE *compressed_file, int *pid);
+
 #endif /* __LIBBB_H__ */