Files.newOutputStream throws if file is a symlink to a non-existing target.
oldergod opened this issue · 2 comments
For simplicity, here is what I do in bash:
$ ls symlink-target
> ls: symlink-target: No such file or directory
$ ln -s symlink-target symlink-source
$ echo hello > symlink-source
$ cat symlink-target
> hello
Jimfs would throw the following (version: 1.2
)
java.nio.file.FileSystemException: symlink-source: not a regular file
at com.google.common.jimfs.FileSystemView.getOrCreateRegularFileWithWriteLock(FileSystemView.java:336)
at com.google.common.jimfs.FileSystemView.getOrCreateRegularFile(FileSystemView.java:298)
at com.google.common.jimfs.JimfsFileSystemProvider.newOutputStream(JimfsFileSystemProvider.java:201)
at java.base/java.nio.file.Files.newOutputStream(Files.java:228)
The code would work fine if I were to use Filesystems.getDefault()
, and as show at the top, this would also work in the CLI.
Thanks. I traced down to here:
jimfs/jimfs/src/main/java/com/google/common/jimfs/FileTree.java
Lines 132 to 137 in a3ddfdf
I wonder if we'll need to plumb some kind of custom LinkOption
-like "return a link target even if it does not exist" flag through the whole lookUp
family of methods so that we can return a non-null DirectoryEntry
in this case? And then lookUpRegularFile
would have to create in that case? Or maybe we should call readSymbolicLink
up front (I guess while holding the lock) and work from there?
It's interesting behavior.... This is something that @cgdecker will have a more knowledgeable perspective on when he's back from vacation.
Hmm... it does seem like we'd need to be able to represent a non-existent file as a DirectoryEntry
and then change some operations that look for a null
DE. I wouldn't be surprised if it would be possible to simplify some other things by doing that as well, like for example Files.newOutputStream
for a non-existent file, but I haven't looked into it too deeply.