I am writing a cross-platform application that creates temporary files and copies these to another location, where they need to be readable by everyone. (By default, only the owner has read access to temporary files.) I tried using the POSIX file permissions as follows:
FileAttribute<Set<PosixFilePermission>> attrs = PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rw-r--r--")); Path temp = Files.createTempFile(null, ".tmp", attrs);
But this results in an exception on non-POSIX platforms:
java.lang.UnsupportedOperationException: 'posix:permissions' not supported as initial attribute
I want to add a simple check so that I can use the file permissions where necessary, without breaking compatibility with other platforms.
Examples of some POSIX-compliant systems are AIX, HP-UX, Solaris, and MacOS (since 10.5 Leopard). On the other hand, Android, FreeBSD, Linux Distributions, OpenBSD, VMWare, etc., follow most of the POSIX standard, but they are not certified.
Being POSIX-compliant for an OS means that it supports those standards (e.g., APIs), and thus can either natively run UNIX programs, or at least porting an application from UNIX to the target OS is easy/easier than if it did not support POSIX.
For now, Linux is not POSIX-certified due to high costs, except for the two commercial Linux distributions Inspur K-UX [12] and Huawei EulerOS [6]. Instead, Linux is seen as being mostly POSIX-compliant.
POSIX (Portable Operating System Interface) is a set of standard operating system interfaces based on the Unix operating system.
Digging deeper within JDK code, this is the check being used for POSIX
from java.nio.file.TempFileHelper
private static final boolean isPosix = FileSystems.getDefault().supportedFileAttributeViews().contains("posix");
You can use this check.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With