deleteIfExists() throws IOException if remote file does not exist
wgtthompson opened this issue · comments
Summary
java.nio.file.spi.FileSystemProvider.deleteIfExists(Path)
implementation will catch a FileNotFoundException
from it's call to delete(Path)
and suppress it and return false
.
//snippet from java.nio.file.spi.FileSystemProvider
public boolean deleteIfExists(Path path) throws IOException {
try {
delete(path);
return true;
} catch (NoSuchFileException ignore) {
return false;
}
}
The problem is that UnixSshFileSystemProvider.delete(Path)
throws an IOException
instead of FileNotFoundException
when the file does not exist. This means that UnixSshFileSystemProvider
does not implement the documented behavior of FileSystemProvider
. Specifically, "
* @return {@code true} if the file was deleted by this method; {@code
* false} if the file could not be deleted because it did not
* exist
Proposed Solution
Override deleteIfExists(Path)
in UnixSshFileSystemProvider
. Add check for file existing before propogating the exception.
@Override
public boolean deleteIfExists(Path path) throws IOException {
try {
delete(path);
return true;
} catch (NoSuchFileException ignore) {
return false;
} catch (IOException ioe) {
try {
//remote calls can result in cases where it is unclear why the remote command
//failed (inconsistent exit codes, etc). such events may be handled by
//throwing IOException when the cause was the file not existing. here, the
//exception is suppressed if the file does not exist (to be consistent with
//the overridden method documentation).
if (!exists(path)) {
return false;
}
} catch (Exception e) {
//suppress so original exception is thrown
}
throw ioe;
}
}
Cause
UnixSshFileSystem.delete(Path)
creates a new BasicFileAttributesImpl
which calls remote stat
command in its initializer. Since the remote file does not exist, the stat
command returns a non-zero error code which results in a thrown IOException
instead of FileNotFoundException
. The IOException
propogates up the call-stack and is not detected as a non-existent file event.
private Map<String, Object> readAttributes( Path path, SupportedAttribute[] attributes, LinkOption... linkOptions ) throws IOException {
UnixSshPath unixPath = checkPath( path ).toAbsolutePath();
String command = statCommand( unixPath, attributes ) + " " + unixPath.toString();
ExecuteResult result = execute( unixPath, command );
if ( result.getExitCode() == 0 ) {
return statParse( result.getStdout(), attributes );
}
else {
throw new IOException( "failed to list directory (" + result.getExitCode() + "): " +
"out='" + result.getStdout() + "', " +
"err='" + result.getStderr() + "'" );
}
}
Example code to generate the problem
//delete file if it exists
try {
Files.deleteIfExists(p);
} catch (Exception e) {
e.printStackTrace();
}
Stack trace
java.io.IOException: failed to list directory (1): out='', err='stat: cannot stat `/home/myname/testfile_jschnio_test.txt': No such file or directory
'
at com.pastdev.jsch.nio.file.UnixSshFileSystemProvider.readAttributes(UnixSshFileSystemProvider.java:540)
at com.pastdev.jsch.nio.file.UnixSshFileSystemProvider.access$600(UnixSshFileSystemProvider.java:61)
at com.pastdev.jsch.nio.file.UnixSshFileSystemProvider$BasicFileAttributesImpl.<init>(UnixSshFileSystemProvider.java:771)
at com.pastdev.jsch.nio.file.UnixSshFileSystemProvider$BasicFileAttributesImpl.<init>(UnixSshFileSystemProvider.java:758)
at com.pastdev.jsch.nio.file.UnixSshFileSystemProvider$BasicFileAttributesImpl.<init>(UnixSshFileSystemProvider.java:750)
at com.pastdev.jsch.nio.file.UnixSshFileSystemProvider.delete(UnixSshFileSystemProvider.java:231)
at java.nio.file.spi.FileSystemProvider.deleteIfExists(FileSystemProvider.java:735)
at java.nio.file.Files.deleteIfExists(Files.java:1116)
at com.mypackage.JschNio_Test.testJschNioFilesystem(JschNio_Test.java:109)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Remote host machine:
Linux homer 3.2.0-4-amd64 #1 SMP Debian 3.2.60-1+deb7u1 x86_64 GNU/Linux
Occurs for both non-existent files and non-existent directories.
@wgtthompson, I fixed this and released a new version for you: 0.1.3. It will take a few hours to propagate to maven central. Thanks for the bug report. On a side note, I wasn't sure anybody other than me was using this. Mind if I ask what you are using it for?
@lucastheisen File storage/retrieval system across multiple machines. The abstraction to FileSystem
provides a consistent interface and re-usable set of algorithms whether the file is local or remote.