Improve the poor man's fseek in unarchive.c
[oweals/opkg-lede.git] / libbb / unarchive.c
index 24877e75452785aa426efc91a4f1ca26de987dd2..a2ae0cb4cbb0d34b8de7d721044559ed63461ef1 100644 (file)
@@ -38,6 +38,7 @@ static char *linkname = NULL;
 extern void seek_sub_file(FILE *src_stream, const int count);
 extern char *extract_archive(FILE *src_stream, FILE *out_stream, const file_header_t *file_entry,
                               const int function, const char *prefix);
+extern ssize_t seek_by_read(FILE* fd, size_t len);
 
 
 #ifdef L_archive_offset
@@ -47,16 +48,31 @@ extern off_t archive_offset;
 #endif 
 
 #ifdef L_seek_sub_file
+#define SEEK_BUF 4096
+ssize_t seek_by_read(FILE* fd, size_t len)
+{
+        ssize_t cc, total = 0;
+        char buf[SEEK_BUF];
+
+        while (len) {
+                cc = fread(buf, sizeof(buf[0]), 
+                                len > SEEK_BUF ? SEEK_BUF : len, 
+                                fd);
+
+                total += cc;
+                len -= cc;
+
+                if(feof(fd) || ferror(fd))
+                        break;
+        }
+        return total;
+}
+
 void seek_sub_file(FILE *src_stream, const int count)
 {
-       int i;
        /* Try to fseek as faster */
        archive_offset += count;
-       if (fseek(src_stream, count, SEEK_CUR) != 0 && errno == ESPIPE) {
-       for (i = 0; i < count; i++) {
-               fgetc(src_stream);
-               }
-       }
+        seek_by_read(src_stream, count);
        return;
 }
 #endif 
@@ -600,16 +616,18 @@ file_header_t *get_header_tar(FILE *tar_stream)
                 linkname = NULL;
         } else
 #endif
-        if (tar.formated.prefix[0] == 0) {
-                tar_entry->name = strdup(tar.formated.name);
-        } else {                                              
-                tar_entry->name = concat_path_file(tar.formated.prefix, tar.formated.name);
+        {
+                tar_entry->name = xstrndup(tar.formated.name, 100);
+
+                if (tar.formated.prefix[0]) {
+                        char *temp = tar_entry->name;
+                        char *prefixTemp = strndup(tar.formated.prefix, 155);
+                        tar_entry->name = concat_path_file(prefixTemp, temp);
+                        free(temp);
+                        free(prefixTemp);
+                }
         }
 
-       if (strlen(tar_entry->name) > 100) {
-               tar_entry->name[100] = 0;
-       }
-
        // tar_entry->name = xstrdup(tar.formated.name);
 
 /*
@@ -621,8 +639,7 @@ file_header_t *get_header_tar(FILE *tar_stream)
        tar_entry->gid   = strtol(tar.formated.gid, NULL, 8);
        tar_entry->size  = strtol(tar.formated.size, NULL, 8);
        tar_entry->mtime = strtol(tar.formated.mtime, NULL, 8);
-       tar_entry->link_name  = strlen(tar.formated.linkname) ? 
-           xstrdup(tar.formated.linkname) : NULL;
+       tar_entry->link_name  = *tar.formated.linkname != '\0' ? xstrndup(tar.formated.linkname, 100) : NULL;
        tar_entry->device = (strtol(tar.formated.devmajor, NULL, 8) << 8) +
                strtol(tar.formated.devminor, NULL, 8);
 
@@ -754,6 +771,10 @@ char *deb_extract(const char *package_filename, FILE *out_stream,
                        if (strcmp(ared_file, ar_header->name) == 0) {
                                /* open a stream of decompressed data */
                                uncompressed_stream = gz_open(deb_stream, &gunzip_pid);
+                               if (uncompressed_stream == NULL) {
+                                       return(NULL);
+                               }
+
                                archive_offset = 0;
                                output_buffer = unarchive(uncompressed_stream, out_stream, get_header_tar, free_header_tar, extract_function, prefix, file_list);
                        }
@@ -774,6 +795,9 @@ char *deb_extract(const char *package_filename, FILE *out_stream,
                archive_offset = 0;
                fseek(deb_stream, 0, SEEK_SET);
                unzipped_opkg_stream = gz_open(deb_stream, &unzipped_opkg_pid);
+               if (unzipped_opkg_stream == NULL) {
+                       return(NULL);
+               }
                
                 /*fprintf(stderr, __FUNCTION__ ": processing opkg %s -- ared_file=%s\n", package_filename, ared_file);*/
                /* walk through outer tar file to find ared_file */
@@ -784,6 +808,9 @@ char *deb_extract(const char *package_filename, FILE *out_stream,
                        if (strcmp(ared_file, tar_header->name+name_offset) == 0) {
                                /* open a stream of decompressed data */
                                uncompressed_stream = gz_open(unzipped_opkg_stream, &gunzip_pid);
+                               if (uncompressed_stream == NULL) {
+                                       return(NULL);
+                               }
                                archive_offset = 0;
                                 /*fprintf(stderr, __FUNCTION__ ":%d: here -- found file\n", __LINE__);*/
                                output_buffer = unarchive(uncompressed_stream,