Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Symbolic link without actual file link caused FileNotFoundException #122

Open
tingung opened this issue May 7, 2019 · 6 comments
Open

Comments

@tingung
Copy link

tingung commented May 7, 2019

At https://github.com/zeroturnaround/zt-zip/blob/master/src/main/java/org/zeroturnaround/zip/ZipUtil.java#L1736 -
String[] filenames = dir.list(); doesn't guarantee return the list of files which are exists in the directory. If there is a symbolic link which link to not exists file, dir.list will return the file name but access the file will throw FileNotFoundException.

Steps to reproduce (must at Linux environment)

  1. touch file
  2. ln -s file link
  3. run ZipUtil::pack to pack the directory contains file create above
  4. rm file
  5. observe the exception. FileNotFoundException

The solution should always ensure the file is exists.

for (int i = 0; i < filenames.length; i++) { String filename = filenames[i]; File file = new File(dir, filename) if(!file.exists()) { continue; }

@toomasr
Copy link
Contributor

toomasr commented May 14, 2019

I took a look and I'm not sure what is the best solution here. You are trying to pack a folder with broken links, right? Best result is that the links itself end up in the archive and just continue being broken?

This is very similar functionality to zip --symlink option of the zip utility. The solution that you provided will skip the broken links though and not sure if optimal.

We use Apache Commons for copying but since Java 7 there is the Files.copy method https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#copy(java.io.InputStream,%20java.nio.file.Path,%20java.nio.file.CopyOption...) which actually will copy the symbolic links and not the destination.

Currently we are supporting Java 6 but not sure preserving this is a great idea.

I think the most correct solution would be update to Java 7 and start preserving the links.

@tingung
Copy link
Author

tingung commented May 15, 2019

I am not sure if the CopyOption is actually can resolve the issue because it is only able to support either follow the symlink or not follow. https://docs.oracle.com/javase/7/docs/api/java/nio/file/LinkOption.html.

For this issue, we should either preserving the links as it is or discard the symlink if preserving is not possible.

@toomasr
Copy link
Contributor

toomasr commented May 15, 2019

Quick test on my Mojave showed that it works in a way where it copies over the link - whether broken or not. I see that in later versions of Commons IO it also relies on this https://docs.oracle.com/javase/7/docs/api/java/nio/file/StandardCopyOption.html#REPLACE_EXISTING

@tingung
Copy link
Author

tingung commented May 15, 2019

o...okay. great. I think I used the wrong option (LinkOption).

@toomasr
Copy link
Contributor

toomasr commented May 15, 2019

I think I'll throw away the current copying, start relying on Java 7 and bump the requirements. Anybody still on Java 6 will have to do with the current functionality.

@goxr3plus
Copy link

goxr3plus commented Jul 9, 2019

Yes let's throw Java 6 and move on Java 7 . Very few companies are still using Java 6 .
@toomasr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants