OSDN Git Service

* src/header.c (copy_path_element, remove_dots, init_header):
authorarai <arai@6a8cc165-1e22-0410-a132-eb4e3f353aba>
Sat, 24 Feb 2007 10:13:22 +0000 (10:13 +0000)
committerarai <arai@6a8cc165-1e22-0410-a132-eb4e3f353aba>
Sat, 24 Feb 2007 10:13:22 +0000 (10:13 +0000)
should treat "foo" and "./foo" as same file, so remove "./" from
archived path name.

* tests/Makefile.am, tests/lha-test.in, tests/lha-test17:
added tests for the removing relative path.

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

ChangeLog
src/header.c
tests/Makefile.am
tests/lha-test.in
tests/lha-test17 [new file with mode: 0755]

index 62a50c0..405e70f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2007-02-24  Koji Arai  <arai@users.sourceforge.jp>
+
+       * src/header.c (copy_path_element, remove_dots, init_header):
+       should treat "foo" and "./foo" as same file, so remove "./" from
+       archived path name.
+
+       * tests/Makefile.am, tests/lha-test.in, tests/lha-test17:
+       added tests for the removing relative path.
+
 2006-10-09  Koji Arai  <arai@users.sourceforge.jp>
 
        * src/lha_macro.h (MIN): newly added a macro for below.
index ef31de5..9ba5613 100644 (file)
@@ -1214,6 +1214,61 @@ remove_leading_dots(char *path)
     return first;
 }
 
+static int
+copy_path_element(char *dst, const char *src, int size)
+{
+    int i;
+
+    if (size < 1) return 0;
+
+    for (i = 0; i < size; i++) {
+        dst[i] = src[i];
+       if (dst[i] == '\0')
+           return i;
+        if (dst[i] == '/') {
+            dst[++i] = 0;
+            return i;
+        }
+    }
+
+    dst[--i] = 0;
+
+    return i;
+}
+
+/* remove leading "xxx/../" and "./" */
+static int
+remove_dots(char *newpath, char *path, size_t size)
+{
+    int len;
+    char *p = newpath;
+
+    path = remove_leading_dots(path);
+
+    while (*path) {
+        if (path[0] == '.' && path[1] == '/')
+            path += 2;
+        else {
+            int len;
+            len = copy_path_element(newpath, path, size);
+
+            path += len;
+            newpath += len;
+            size -= len;
+            if (size <= 1)
+                break;
+        }
+    }
+
+    /* When newpath is empty, set "." */
+    if (newpath == p) {
+        strcpy(newpath, ".");
+        newpath++;
+    }
+
+    return newpath - p;         /* string length */
+}
+
 void
 init_header(name, v_stat, hdr)
     char           *name;
@@ -1232,9 +1287,9 @@ init_header(name, v_stat, hdr)
     hdr->original_size = v_stat->st_size;
     hdr->attribute = GENERIC_ATTRIBUTE;
     hdr->header_level = header_level;
-    len = str_safe_copy(hdr->name,
-                        remove_leading_dots(name),
-                        sizeof(hdr->name));
+
+    len = remove_dots(hdr->name, name, sizeof(hdr->name));
+
     hdr->crc = 0x0000;
     hdr->extend_type = EXTEND_UNIX;
     hdr->unix_last_modified_stamp = v_stat->st_mtime;
index 38671e0..806d0e1 100644 (file)
@@ -19,7 +19,8 @@ EXTRA_DIST =  lha-test1 \
                lha-test16-lg.lzh \
                lha-test16-l0.lzh \
                lha-test16-l1.lzh \
-               lha-test16-l2.lzh
+               lha-test16-l2.lzh \
+               lha-test17
 
 DISTCLEANFILES = test.log
 
index 53c3dad..cbd647c 100644 (file)
@@ -16,6 +16,7 @@ DUALCASE=1; export DUALCASE # for MKS sh
 : ${lha_dir=@top_builddir@/src}
 : ${lha_cmd=$(cd $lha_dir && pwd)/lha}
 : ${srcdir=@srcdir@}
+: ${builddir=$(cd @builddir@ && pwd)}
 
 trap '_stat=$?; rm -rf test-*; exit $_stat' 0 1 2 3 15
 
@@ -115,6 +116,7 @@ case `$lha --version 2>&1` in
 esac
 testsuite lha-test15
 testsuite lha-test16
+testsuite lha-test17
 
 if (( $error_num != 0 )); then
   message $error_num tests failed!
diff --git a/tests/lha-test17 b/tests/lha-test17
new file mode 100755 (executable)
index 0000000..06338ef
--- /dev/null
@@ -0,0 +1,168 @@
+# -*- shell-script -*-
+message testing to remove relative path
+
+# remove leading "../"; this behavior will be like the GNU tar.
+
+echo -------------------------------------------------------------------
+# "../xx/xx" -> "xx/xx"
+(cd test-1 &&
+  $lha c $builddir/test-tmp-1.lzh ../test-1/test-a
+  ) 2> test-tmp-stderr
+                                                       check $? $LINENO
+
+cat <<"EOF" | diff - test-tmp-stderr
+LHa: Warning: Removing leading `../' from member name.
+EOF
+                                                       check $? $LINENO
+
+$lha vvq test-tmp-1.lzh | head -1
+$lha vvq test-tmp-1.lzh | head -1 | egrep '^test-1/test-a$'
+                                                       check $? $LINENO
+
+echo -------------------------------------------------------------------
+# ../xx/../xx (result: xx not "../xx")
+(cd test-1 &&
+  $lha c $builddir/test-tmp-2.lzh ../test-1/../test-a
+  ) 2> test-tmp-stderr
+                                                       check $? $LINENO
+
+cat <<"EOF" | diff - test-tmp-stderr
+LHa: Warning: Removing leading `../test-1/../' from member name.
+EOF
+                                                       check $? $LINENO
+
+$lha vvq test-tmp-2.lzh | head -1
+$lha vvq test-tmp-2.lzh | head -1 | egrep '^test-a$'
+                                                       check $? $LINENO
+echo -------------------------------------------------------------------
+# ../xx/xx/../xx (result: xx not "xx/xx")
+mkdir test-tmp-3 &&
+cp -pr test-1 test-tmp-3 &&
+cp -p test-a test-tmp-3
+                                                       check $? $LINENO
+(cd test-tmp-3 &&
+  $lha c $builddir/test-tmp-3.lzh ../test-tmp-3/test-1/../test-a
+  ) 2> test-tmp-stderr
+                                                       check $? $LINENO
+
+cat <<"EOF" | diff - test-tmp-stderr
+LHa: Warning: Removing leading `../test-tmp-3/test-1/../' from member name.
+EOF
+                                                       check $? $LINENO
+
+$lha vvq test-tmp-3.lzh | head -1
+$lha vvq test-tmp-3.lzh | head -1 | egrep '^test-a$'
+                                                       check $? $LINENO
+echo -------------------------------------------------------------------
+# just a ".." (result: ".")
+mkdir test-tmp-4 &&
+cp -pr test-1 test-tmp-4
+                                                       check $? $LINENO
+(cd test-tmp-4/test-1 &&
+  $lha c $builddir/test-tmp-4.lzh ..
+  ) 2> test-tmp-stderr
+                                                       check $? $LINENO
+
+cat <<"EOF" | diff - test-tmp-stderr
+LHa: Warning: Removing leading `..' from member name.
+LHa: Warning: Removing leading `../' from member name.
+LHa: Warning: Removing leading `../' from member name.
+LHa: Warning: Removing leading `../' from member name.
+LHa: Warning: Removing leading `../' from member name.
+EOF
+                                                       check $? $LINENO
+
+$lha vvq test-tmp-4.lzh
+$lha vvq test-tmp-4.lzh > test-tmp-stdout
+                                                       check $? $LINENO
+
+cat <<"EOF" > test-tmp-expect
+./
+test-1/
+test-1/test-a
+test-1/test-b
+test-1/test-c
+EOF
+
+egrep -v -- '-lh' test-tmp-stdout | diff - test-tmp-expect
+                                                       check $? $LINENO
+echo -------------------------------------------------------------------
+# "xx/./xx" -> "xx/xx"
+$lha c test-tmp-5.lzh test-1/./test-a
+                                                       check $? $LINENO
+$lha vvq test-tmp-5.lzh
+$lha vvq test-tmp-5.lzh > test-tmp-stdout
+                                                       check $? $LINENO
+cat <<"EOF" > test-tmp-expect
+test-1/test-a
+EOF
+
+egrep -v -- '-lh' test-tmp-stdout | diff - test-tmp-expect
+                                                       check $? $LINENO
+echo -------------------------------------------------------------------
+# "./xx/././xx" -> "xx/xx"
+$lha c test-tmp-6.lzh ./test-1/././test-a
+                                                       check $? $LINENO
+$lha vvq test-tmp-6.lzh
+$lha vvq test-tmp-6.lzh > test-tmp-stdout
+                                                       check $? $LINENO
+cat <<"EOF" > test-tmp-expect
+test-1/test-a
+EOF
+
+egrep -v -- '-lh' test-tmp-stdout | diff - test-tmp-expect
+                                                       check $? $LINENO
+echo -------------------------------------------------------------------
+# "xx/./xx/./././xx" -> "xx/xx/xx"
+mkdir test-tmp-7 &&
+cp -pr test-1 test-tmp-7 &&
+cp -p test-a test-tmp-7
+                                                       check $? $LINENO
+$lha c test-tmp-7.lzh test-tmp-7/./test-1/./././test-a
+                                                       check $? $LINENO
+$lha vvq test-tmp-7.lzh
+$lha vvq test-tmp-7.lzh > test-tmp-stdout
+                                                       check $? $LINENO
+cat <<"EOF" > test-tmp-expect
+test-tmp-7/test-1/test-a
+EOF
+
+egrep -v -- '-lh' test-tmp-stdout | diff - test-tmp-expect
+                                                       check $? $LINENO
+echo -------------------------------------------------------------------
+# "./" -> "."
+(cd test-1 &&
+  $lha c $builddir/test-tmp-8.lzh ./
+  )
+                                                       check $? $LINENO
+$lha vvq test-tmp-8.lzh
+$lha vvq test-tmp-8.lzh > test-tmp-stdout
+                                                       check $? $LINENO
+cat <<"EOF" > test-tmp-expect
+./
+test-a
+test-b
+test-c
+EOF
+
+egrep -v -- '-lh' test-tmp-stdout | diff - test-tmp-expect
+                                                       check $? $LINENO
+echo -------------------------------------------------------------------
+# "." -> "."
+(cd test-1 &&
+  $lha c $builddir/test-tmp-9.lzh .
+  )
+                                                       check $? $LINENO
+$lha vvq test-tmp-9.lzh
+$lha vvq test-tmp-9.lzh > test-tmp-stdout
+                                                       check $? $LINENO
+cat <<"EOF" > test-tmp-expect
+./
+test-a
+test-b
+test-c
+EOF
+
+egrep -v -- '-lh' test-tmp-stdout | diff - test-tmp-expect
+                                                       check $? $LINENO
+echo -------------------------------------------------------------------