Colleague Max Kanat-Alexander reports:
You can get the jar command to add an already-existing version of a jar to the jar file itself by using -C. This is because the check on "zname" in addFile does not normalize paths to remove "." as a directory entry.
The only reason that this doesn't cause an infinite loop is an accident of the order in which the current code iterates through the entries that it is going to add (an order which is not guaranteed by the APIs it is using to iterate). If you change that order, then you do get an infinite loop.
Here's a repro:
(set -eux; D=$(mktemp -d); ( cd $D && mkdir foo && touch foo/file && jar cMf foo/test.jar -C foo/ . && jar tf foo/test.jar && jar cMf foo/test.jar -C foo/ . && jar tf foo/test.jar ); rm -rf $D)
+/bin/zsh:6> D=+/bin/zsh:6> mktemp -d
+/bin/zsh:6> D=/tmp/tmp.KS4EAnsvjE
+/bin/zsh:6> cd /tmp/tmp.KS4EAnsvjE
+chpwd:1> [[ 10797 != 136379 ]]
+chpwd:2> unfunction chpwd
+/bin/zsh:6> mkdir foo
+/bin/zsh:6> touch foo/file
+/bin/zsh:6> jar cMf foo/test.jar -C foo/ .
+/bin/zsh:6> jar tf foo/test.jar
file
+/bin/zsh:6> jar cMf foo/test.jar -C foo/ .
+/bin/zsh:6> jar tf foo/test.jar
test.jar
file
+/bin/zsh:6> rm -rf /tmp/tmp.KS4EAnsvjE
You can get the jar command to add an already-existing version of a jar to the jar file itself by using -C. This is because the check on "zname" in addFile does not normalize paths to remove "." as a directory entry.
The only reason that this doesn't cause an infinite loop is an accident of the order in which the current code iterates through the entries that it is going to add (an order which is not guaranteed by the APIs it is using to iterate). If you change that order, then you do get an infinite loop.
Here's a repro:
(set -eux; D=$(mktemp -d); ( cd $D && mkdir foo && touch foo/file && jar cMf foo/test.jar -C foo/ . && jar tf foo/test.jar && jar cMf foo/test.jar -C foo/ . && jar tf foo/test.jar ); rm -rf $D)
+/bin/zsh:6> D=+/bin/zsh:6> mktemp -d
+/bin/zsh:6> D=/tmp/tmp.KS4EAnsvjE
+/bin/zsh:6> cd /tmp/tmp.KS4EAnsvjE
+chpwd:1> [[ 10797 != 136379 ]]
+chpwd:2> unfunction chpwd
+/bin/zsh:6> mkdir foo
+/bin/zsh:6> touch foo/file
+/bin/zsh:6> jar cMf foo/test.jar -C foo/ .
+/bin/zsh:6> jar tf foo/test.jar
file
+/bin/zsh:6> jar cMf foo/test.jar -C foo/ .
+/bin/zsh:6> jar tf foo/test.jar
test.jar
file
+/bin/zsh:6> rm -rf /tmp/tmp.KS4EAnsvjE