Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to differentiate between ESTALE and ENOENT in Java

Tags:

java

nfs

jna

I'm trying to write a Java application running on a Linux environment on an NFS filesystem.

I noticed that when I call java.io.File.exists(), it returns false for both ESTALE (Stale NFS file handle) and ENOENT (No such file or directory). For my application, I need to be able to differentiate between the two.

Currently I am considering implementing the stat() call using JNA, but that seems to be overkill, what with having to implement the whole stat structure and all the __xstat64 stuff which seems so platform dependent.

Is there a simple way to simply obtain the underlying errno after a Java call like File.exists() or any other ideas to solve this problem?

like image 969
Ian Su Avatar asked Oct 21 '22 08:10

Ian Su


1 Answers

With JNA, you don't actually have to implement the stat structure; you just need to allocate memory for it sufficiently large to represent the native structure.

You can then call Native.getLastError() to retrieve the errno value.

Pointer fake_stat = new Memory(2048); // big enough
if (clib.stat(filename, fake_stat) != 0) {
    int errno = Native.getLastError();
    ...
}
like image 86
technomage Avatar answered Oct 29 '22 23:10

technomage