OSDN Git Service

Change skip behavior on extracting.
[lha/lha.git] / src / lhext.c
index 38447c2..842b3e0 100644 (file)
@@ -30,7 +30,7 @@ static void add_dirinfo(char* name, LzHeader* hdr);
 static void adjust_dirinfo();
 
 #ifdef HAVE_LIBAPPLEFILE
-static boolean decode_macbinary(FILE *ofp, size_t size, const char *outPath);
+static boolean decode_macbinary(FILE *ofp, off_t size, const char *outPath);
 #endif
 
 /* ------------------------------------------------------------------------ */
@@ -198,6 +198,23 @@ open_with_make_path(name)
 }
 
 /* ------------------------------------------------------------------------ */
+static int
+symlink_with_make_path(realname, name)
+    const char     *realname;
+    const char     *name;
+{
+    int l_code;
+
+    l_code = symlink(realname, name);
+    if (l_code < 0) {
+        make_parent_path(name);
+        l_code = symlink(realname, name);
+    }
+
+    return l_code;
+}
+
+/* ------------------------------------------------------------------------ */
 static void
 adjust_info(name, hdr)
     char           *name;
@@ -415,11 +432,11 @@ extract_one(afp, hdr)
 
             if (skip_flg == TRUE) { /* if skip_flg */
                 if (stat(name, &stbuf) == 0 && force != TRUE) {
-                    if (stbuf.st_mtime >= hdr->unix_last_modified_stamp) {
+                    /* if (stbuf.st_mtime >= hdr->unix_last_modified_stamp) {*/
                         if (quiet != TRUE)
                             printf("%s : Skipped...\n", name);
                         return read_size;
-                    }
+                    /* } */
                 }
             }
             if (noexec) {
@@ -482,7 +499,7 @@ extract_one(afp, hdr)
              || (hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_SYMLINK
              || method == LZHDIRS_METHOD_NUM) {
         /* ↑これで、Symbolic Link は、大丈夫か? */
-        if (!ignore_directory && !verify_mode) {
+        if (!ignore_directory && !verify_mode && !output_to_stdout) {
             if (noexec) {
                 if (quiet != TRUE)
                     printf("EXTRACT %s (directory)\n", name);
@@ -498,19 +515,20 @@ extract_one(afp, hdr)
                     if (up_flag == FALSE && force == FALSE) {
                         return read_size;
                     }
-                } else {
+                }
+
+                if (skip_flg == TRUE) { /* if skip_flg */
                     if (GETSTAT(name, &stbuf) == 0 && force != TRUE) {
-                        if (stbuf.st_mtime >= hdr->unix_last_modified_stamp) {
+                        /* if (stbuf.st_mtime >= hdr->unix_last_modified_stamp) { */
                             if (quiet != TRUE)
                                 printf("%s : Skipped...\n", name);
                             return read_size;
-                        }
+                        /* } */
                     }
                 }
 
                 unlink(name);
-                make_parent_path(name);
-                l_code = symlink(hdr->realname, name);
+                l_code = symlink_with_make_path(hdr->realname, name);
                 if (l_code < 0) {
                     if (quiet != TRUE)
                         warning("Can't make Symbolic Link \"%s\" -> \"%s\"",
@@ -527,7 +545,7 @@ extract_one(afp, hdr)
 #endif
             }
             else { /* make directory */
-                if (!output_to_stdout && !make_parent_path(name))
+                if (!make_parent_path(name))
                     return read_size;
                 /* save directory information */
                 add_dirinfo(name, hdr);
@@ -549,6 +567,25 @@ extract_one(afp, hdr)
     return read_size;
 }
 
+static int
+skip_to_nextpos(FILE *fp, off_t pos, off_t off, off_t read_size)
+{
+    if (pos != -1) {
+        if (fseeko(fp, pos + off, SEEK_SET) != 0) {
+            return -1;
+        }
+    }
+    else {
+        off_t i = off - read_size;
+        while (i--) {
+            if (fgetc(fp) == EOF) {
+                return -1;
+            }
+        }
+    }
+    return 0;
+}
+
 /* ------------------------------------------------------------------------ */
 /* EXTRACT COMMAND MAIN                                                     */
 /* ------------------------------------------------------------------------ */
@@ -569,25 +606,19 @@ cmd_extract()
 
     /* extract each files */
     while (get_header(afp, &hdr)) {
+        pos = ftello(afp);
         if (need_file(hdr.name)) {
-            pos = ftello(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)
-                    fseeko(afp, pos + hdr.packed_size, SEEK_SET);
-                else {
-                    off_t i = hdr.packed_size - read_size;
-                    while (i--) fgetc(afp);
+                if (skip_to_nextpos(afp, pos, hdr.packed_size, read_size) == -1) {
+                    fatal_error("Cannot seek to next header position from \"%s\"", hdr.name);
                 }
             }
         } else {
-            if (afp != stdin)
-                fseeko(afp, hdr.packed_size, SEEK_CUR);
-            else {
-                off_t i = hdr.packed_size;
-                while (i--) fgetc(afp);
+            if (skip_to_nextpos(afp, pos, hdr.packed_size, 0) == -1) {
+                fatal_error("Cannot seek to next header position from \"%s\"", hdr.name);
             }
         }
     }
@@ -723,7 +754,7 @@ decode_macbinary(ofp, size, outPath)
     af_file_t *afp = NULL;
     FILE *ifp = NULL;
     unsigned char *datap;
-    off_t dlen;
+    size_t dlen;
 
     if ((afp = af_open(temporary_name)) != NULL) {
         /* fetch datafork */