How to check if the OS is POSIX compliant
Asked Answered
B

1

28

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.

Bukhara answered 4/2, 2014 at 1:41 Comment(2)
You can always try to perform your operation and check the exception. If it is there then non-POSIX.Decussate
@Decussate True. I was hoping for a more elegant solution.Bukhara
E
47

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.

Endodermis answered 4/2, 2014 at 1:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.