OSDN Git Service

Fix warning message on extracting symlink.
[lha/lha.git] / src / lhext.c
index d386bac..ad6ec1b 100644 (file)
@@ -99,6 +99,7 @@ make_name_with_pathcheck(char *name, size_t namesz, const char *q)
         offset += sz;
     }
 
+#ifdef S_IFLNK
     while ((p = strchr(q, '/')) != NULL) {
         if (namesz - offset < (p - q) + 2) {
             return FALSE;
@@ -118,6 +119,7 @@ make_name_with_pathcheck(char *name, size_t namesz, const char *q)
         }
         name[offset++] = '/';
     }
+#endif
 
     str_safe_copy(name + offset, q, namesz - offset);
 
@@ -196,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;
@@ -480,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);
@@ -507,8 +526,7 @@ extract_one(afp, hdr)
                 }
 
                 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\"",
@@ -525,7 +543,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);
@@ -547,6 +565,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                                                     */
 /* ------------------------------------------------------------------------ */
@@ -567,25 +604,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);
             }
         }
     }