OSDN Git Service

* src/lhext.c (inquire_extract): warning if stdin is not tty,
authorarai <arai@6a8cc165-1e22-0410-a132-eb4e3f353aba>
Fri, 9 May 2003 18:50:12 +0000 (18:50 +0000)
committerarai <arai@6a8cc165-1e22-0410-a132-eb4e3f353aba>
Fri, 9 May 2003 18:50:12 +0000 (18:50 +0000)
because you cannot reply from stdin.
(extract_one): return actual read size from archive.
(cmd_extract): fix a bug: skip correct size when error occurred.

* tests/lha-test5: added tests for above.

* src/lha.h (struct interfacing): added a member `read_size'.

* src/extract.c (decode_lzhuf): added an argument `pointer of
read_size'.

* src/prototypes.h: ditto.

* src/slide.c (decode): set actual read size to interface->read_size.

git-svn-id: svn+ssh://svn.sourceforge.jp/svnroot/lha/lha/trunk@692 6a8cc165-1e22-0410-a132-eb4e3f353aba

src/extract.c
src/lha.h
src/lhext.c
src/prototypes.h
src/slide.c
tests/lha-test5

index 1f716ce..16e8994 100644 (file)
@@ -9,13 +9,14 @@
 #include "lha.h"
 
 int
-decode_lzhuf(infp, outfp, original_size, packed_size, name, method)
+decode_lzhuf(infp, outfp, original_size, packed_size, name, method, read_sizep)
     FILE           *infp;
     FILE           *outfp;
     long            original_size;
     long            packed_size;
     char           *name;
     int             method;
+    size_t         *read_sizep;
 {
     unsigned int crc;
 
@@ -25,13 +26,17 @@ decode_lzhuf(infp, outfp, original_size, packed_size, name, method)
     interface.outfile = outfp;
     interface.original = original_size;
     interface.packed = packed_size;
+    interface.read_size = 0;
+
+    *read_sizep = packed_size;
 
     switch (method) {
     case LZHUFF0_METHOD_NUM:
     case LARC4_METHOD_NUM:
         start_indicator(name, original_size
                   ,verify_mode ? "Testing " : "Melting ", 2048);
-        copyfile(infp, (verify_mode ? NULL : outfp), original_size, 2, &crc);
+        *read_sizep = copyfile(infp, (verify_mode ? NULL : outfp),
+                               original_size, 2, &crc);
         break;
     case LARC_METHOD_NUM:       /* -lzs- */
         interface.dicbit = 11;
@@ -39,6 +44,7 @@ decode_lzhuf(infp, outfp, original_size, packed_size, name, method)
                 ,verify_mode ? "Testing " : "Melting "
                 ,1 << interface.dicbit);
         crc = decode(&interface);
+        *read_sizep = interface.read_size;
         break;
     case LZHUFF1_METHOD_NUM:        /* -lh1- */
     case LZHUFF4_METHOD_NUM:        /* -lh4- */
@@ -48,18 +54,20 @@ decode_lzhuf(infp, outfp, original_size, packed_size, name, method)
                 ,verify_mode ? "Testing " : "Melting "
                 ,1 << interface.dicbit);
         crc = decode(&interface);
+        *read_sizep = interface.read_size;
         break;
     case LZHUFF6_METHOD_NUM:        /* -lh6- */ /* Added N.Watazaki (^_^) */
 #ifdef SUPPORT_LH7
     case LZHUFF7_METHOD_NUM:                /* -lh7- */
 #endif
         interface.dicbit = (method - LZHUFF6_METHOD_NUM) + 15;
-        
+        /* fall through */
     default:
         start_indicator(name, original_size
                 ,verify_mode ? "Testing " : "Melting "
                 ,1 << interface.dicbit);
         crc = decode(&interface);
+        *read_sizep = interface.read_size;
     }
     finish_indicator(name, verify_mode ? "Tested  " : "Melted  ");
 
index 569deb6..6d49182 100644 (file)
--- a/src/lha.h
+++ b/src/lha.h
@@ -260,6 +260,7 @@ struct interfacing {
     FILE            *outfile;
     unsigned long   original;
     unsigned long   packed;
+    size_t          read_size;
     int             dicbit;
     int             method;
 };
index 870394b..83f66b3 100644 (file)
@@ -44,8 +44,10 @@ inquire_extract(name)
             return FALSE;
         }
         else if (!force) {
-            if (!isatty(0))
+            if (!isatty(0)) {
+                warning("skip to extract %s.", name);
                 return FALSE;
+            }
 
             switch (inquire("OverWrite ?(Yes/[No]/All/Skip)", name, "YyNnAaSs\n")) {
             case 0:
@@ -66,6 +68,7 @@ inquire_extract(name)
             }
         }
     }
+
     if (noexec)
         printf("EXTRACT %s\n", name);
 
@@ -201,7 +204,7 @@ adjust_info(name, hdr)
 }
 
 /* ------------------------------------------------------------------------ */
-static void
+static size_t
 extract_one(afp, hdr)
     FILE           *afp;    /* archive file */
     LzHeader       *hdr;
@@ -213,6 +216,7 @@ extract_one(afp, hdr)
     int             method;
     boolean         save_quiet, save_verbose, up_flag;
     char           *q = hdr->name, c;
+    size_t read_size = 0;
 
     if (ignore_directory && strrchr(hdr->name, '/')) {
         q = (char *) strrchr(hdr->name, '/') + 1;
@@ -246,7 +250,7 @@ extract_one(afp, hdr)
         if (methods[method] == NULL) {
             error("Unknown method \"%.*s\"; \"%s\" will be skiped ...",
                   5, hdr->method, name);
-            return;
+            return read_size;
         }
         if (memcmp(hdr->method, methods[method], 5) == 0)
             break;
@@ -260,7 +264,7 @@ extract_one(afp, hdr)
             if (methods[method] == NULL) {
                 error("Unknown method \"%.*s\"; \"%s\" will be skiped ...",
                       5, hdr->method, name);
-                return;
+                return read_size;
             }
             if (memcmp(hdr->method, methods[method], 5) == 0)
                 break;
@@ -272,12 +276,7 @@ extract_one(afp, hdr)
         if (output_to_stdout || verify_mode) {
             if (noexec) {
                 printf("%s %s\n", verify_mode ? "VERIFY" : "EXTRACT", name);
-                if (afp == stdin) {
-                    int             i = hdr->packed_size;
-                    while (i--)
-                        fgetc(afp);
-                }
-                return;
+                return read_size;
             }
 
             save_quiet = quiet;
@@ -299,8 +298,9 @@ extract_one(afp, hdr)
                 old_mode = setmode(fileno(stdout), O_BINARY);
 #endif
 
-            crc = decode_lzhuf
-                (afp, stdout, hdr->original_size, hdr->packed_size, name, method);
+            crc = decode_lzhuf(afp, stdout,
+                               hdr->original_size, hdr->packed_size,
+                               name, method, &read_size);
 #if __MINGW32__
                 fflush(stdout);
                 setmode(fileno(stdout), old_mode);
@@ -313,7 +313,7 @@ extract_one(afp, hdr)
             if (skip_flg == FALSE)  {
                 up_flag = inquire_extract(name);
                 if (up_flag == FALSE && force == FALSE) {
-                    return;
+                    return read_size;
                 }
             }
 
@@ -322,17 +322,12 @@ extract_one(afp, hdr)
                     if (stbuf.st_mtime >= hdr->unix_last_modified_stamp) {
                         if (quiet != TRUE)
                             printf("%s : Skipped...\n", name);
-                        return;
+                        return read_size;
                     }
                 }
             }
             if (noexec) {
-                if (afp == stdin) {
-                    int i = hdr->packed_size;
-                    while (i--)
-                        fgetc(afp);
-                }
-                return;
+                return read_size;
             }
 
             signal(SIGINT, interrupt);
@@ -344,8 +339,9 @@ extract_one(afp, hdr)
             remove_extracting_file_when_interrupt = TRUE;
 
             if ((fp = open_with_make_path(name)) != NULL) {
-                crc = decode_lzhuf
-                    (afp, fp, hdr->original_size, hdr->packed_size, name, method);
+                crc = decode_lzhuf(afp, fp,
+                                   hdr->original_size, hdr->packed_size,
+                                   name, method, &read_size);
                 fclose(fp);
             }
             remove_extracting_file_when_interrupt = FALSE;
@@ -354,7 +350,7 @@ extract_one(afp, hdr)
             signal(SIGHUP, SIG_DFL);
 #endif
             if (!fp)
-                return;
+                return read_size;
         }
 
         if (hdr->has_crc && crc != hdr->crc)
@@ -368,7 +364,7 @@ extract_one(afp, hdr)
             if (noexec) {
                 if (quiet != TRUE)
                     printf("EXTRACT %s (directory)\n", name);
-                return;
+                return read_size;
             }
             /* NAME has trailing SLASH '/', (^_^) */
             if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_SYMLINK) {
@@ -378,14 +374,14 @@ extract_one(afp, hdr)
                 if (skip_flg == FALSE)  {
                     up_flag = inquire_extract(name);
                     if (up_flag == FALSE && force == FALSE) {
-                        return;
+                        return read_size;
                     }
                 } else {
                     if (GETSTAT(name, &stbuf) == 0 && force != TRUE) {
                         if (stbuf.st_mtime >= hdr->unix_last_modified_stamp) {
                             if (quiet != TRUE)
                                 printf("%s : Skipped...\n", name);
-                            return;
+                            return read_size;
                         }
                     }
                 }
@@ -405,11 +401,11 @@ extract_one(afp, hdr)
 #else
                 warning("Can't make Symbolic Link %s -> %s",
                         hdr->realname, name);
-                return;
+                return read_size;
 #endif
             } else { /* make directory */
                 if (!output_to_stdout && !make_parent_path(name))
-                    return;
+                    return read_size;
             }
         }
     }
@@ -422,6 +418,8 @@ extract_one(afp, hdr)
 
     if (!output_to_stdout)
         adjust_info(name, hdr);
+
+    return read_size;
 }
 
 /* ------------------------------------------------------------------------ */
@@ -433,6 +431,7 @@ cmd_extract()
     LzHeader        hdr;
     long            pos;
     FILE           *afp;
+    size_t read_size;
 
     /* open archive file */
     if ((afp = open_old_archive()) == NULL)
@@ -445,24 +444,23 @@ cmd_extract()
     while (get_header(afp, &hdr)) {
         if (need_file(hdr.name)) {
             pos = ftell(afp);
-            extract_one(afp, &hdr);
-            /* when error occurred in extract_one(), should adjust
-               point of file stream */
-            if (afp != stdin)
-                fseek(afp, pos + hdr.packed_size, SEEK_SET);
-            else {
-                /* FIXME: assume that the extract_one() has read
-                   packed_size bytes from stdin. */
-                long i = 0;
-                while (i--) fgetc(afp);
+            read_size = extract_one(afp, &hdr);
+            if (read_size != hdr.packed_size) {
+                /* when error occurred in extract_one(), should adjust
+                   point of file stream */
+                if (pos != -1 && afp != stdin)
+                    fseek(afp, pos + hdr.packed_size - read_size, SEEK_SET);
+                else {
+                    size_t i = hdr.packed_size - read_size;
+                    while (i--) fgetc(afp);
+                }
             }
         } else {
             if (afp != stdin)
                 fseek(afp, hdr.packed_size, SEEK_CUR);
             else {
-                int             i = hdr.packed_size;
-                while (i--)
-                    fgetc(afp);
+                size_t i = hdr.packed_size;
+                while (i--) fgetc(afp);
             }
         }
     }
index 5c40cc9..934f5d5 100644 (file)
@@ -34,7 +34,7 @@ unsigned short decode_p_dyn P_((void));
 void output_dyn P_((unsigned int code, unsigned int pos));
 void encode_end_dyn P_((void));
 /* extract.c */
-int decode_lzhuf P_((FILE *infp, FILE *outfp, long original_size, long packed_size, char *name, int method));
+int decode_lzhuf P_((FILE *infp, FILE *outfp, long original_size, long packed_size, char *name, int method, size_t *read_sizep));
 /* header.c */
 int calc_sum P_((char *p, int len));
 void convert_filename P_((char *name, int len, int size, int from_code, int to_code, char *from_delim, char *to_delim, int case_to));
index 9ff6194..58e6576 100644 (file)
@@ -461,5 +461,9 @@ decode(interface)
     }
 
     free(dtext);
+
+    /* usually read size is interface->packed */
+    interface->read_size = interface->packed - compsize;
+
     return crc;
 }
index e595522..0c437cf 100644 (file)
@@ -13,3 +13,13 @@ $lha c - test-a test-b test-c | $lha xw=test-tmp2 -
                                                        check $? $LINENO
 diff -r test-1 test-tmp2
                                                        check $? $LINENO
+# skip to extract existent files when archive file is stdin
+rm test-tmp/test-b
+                                                       check $? $LINENO
+cat test-tmp.lzh | $lha xw=test-tmp - 2> test-stderr
+                                                       check $? $LINENO
+diff -r test-1 test-tmp
+                                                       check $? $LINENO
+# 2 files will be skipped.
+test 2 = `grep skip test-stderr | wc -l`
+                                                       check $? $LINENO