The best way to distinguish between estale and enoent in Java

I am trying to write Java applications running in Linux environment on NFS file system

I noticed that when I called Java io. File. Exists(), it returns false for both estale (stand NFS file handle) and enoent (no such file or directory) For my application, I need to be able to distinguish between the two

At present, I'm considering using JNA to implement stat () calls, but this seems to be a bit overkill. I need to implement the whole stat structure and all__ Xstat64 things, they seem to be platform related

There is a simple method that can be used in files like file After a Java call like exists (), simply get the underlying errno, or any other idea to solve the problem?

Solution

With JNA, you don't actually have to implement the stat structure; You only need to allocate enough memory 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();
    ...
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>