Handling File Permissions
For a directory entry, POSIX-based file systems (Portable Operating System Interface) typically define read, write, and execute permissions for the owner, the group that the owner belongs to, and for others. In Java, these nine permissions are represented by the enum type PosixFilePermission (Table 21.10).
A human-readable form of file permissions affords interoperability with the enum type PosixFilePermission. This form is specified as a string of nine characters, where characters are interpreted as three permission groups of three characters. From the start of the string, the first permission group, the second permission group, and the third permission group specify the permissions for the owner, the group, and others, respectively. Each permission group is defined by the following pattern:
(r|-)(w|-)(x|-)
that is comprised of three groupings, where each grouping (a|b) is interpreted as either a or b. For example, rwx and — are valid permissions groups, but w_w and xwr are not. The characters r, w, and x stand for read, write, and execute permissions, respectively, and the character – indicates that the permission corresponding to the position of the character – is not set.
The set of file permissions created by the following statement:
Set<PosixFilePermission> permSet1
= EnumSet.of(OWNER_READ, OWNER_WRITE, GROUP_READ, OTHERS_READ);
is equivalent to the permissions in the string “rw-r–r–“.
The utility class PosixFilePermissions provides methods for converting between the two forms of specifying file permissions.
Table 21.10 POSIX File Permissions
Enum type java.nio.file.attribute.PosixFilePermission | Description |
OWNER_EXECUTE | Execute/search permission, owner |
OWNER_READ | Read permission, owner |
OWNER_WRITE | Write permission, owner |
GROUP_EXECUTE | Execute/search permission, group |
GROUP_READ | Read permission, group |
GROUP_WRITE | Write permission, group |
OTHERS_EXECUTE | Execute/search permission, others |
OTHERS_READ | Read permission, others |
OTHERS_WRITE | Write permission, others |
Following are methods from the utility class java.nio.file.attribute.PosixFilePermissions:
static Set<PosixFilePermission> fromString(String permStr)
Returns the set of permissions corresponding to a given String representation. The permStr parameter is a String representing the permissions, as explained earlier.
static String toString(Set<PosixFilePermission> perms)
Returns the String representation of a set of permissions.
static FileAttribute<Set<PosixFilePermission>>
asFileAttribute(Set<PosixFilePermission> perms)
Creates a FileAttribute, encapsulating a copy of the given file permissions, suitable for passing to methods that create files and directories (p. 1339).
The getPosixFilePermissions() and setPosixFilePermissions() methods of the Files class can be used to retrieve and update file permissions of a directory entry, as shown at (11) and (19), respectively. The methods toString() and fromString() of the PosixFilePermissions class at (13) and (18b) convert between a set of PosixFilePermission and a string representation of file permissions, respectively. Note that (18a) and (18b) define the same set of file permissions.
// Get the POSIX file permissions for the directory entry:
Set<PosixFilePermission> filePermissions
= Files.getPosixFilePermissions(fPath); // (11)
out.println(“getPosixFilePermissions (set): ” + filePermissions); // (12)
out.println(“getPosixFilePermissions (string): “
+ PosixFilePermissions.toString(filePermissions)); // (13)
…
// Set POSIX file permissions for the directory entry: (17)
Set<PosixFilePermission> newfilePermissions
= EnumSet.of(OWNER_READ, OWNER_WRITE, GROUP_READ, OTHERS_READ);// (18a)
//Set<PosixFilePermission> newfilePermissions
// = PosixFilePermissions.fromString(“rw-r–r–“); // (18b)
Files.setPosixFilePermissions(fPath, newfilePermissions); // (19)
The following methods from the utility class java.nio.file.Files can be used for retrieving and updating the POSIX-specific file permissions of a directory entry:
static Set<PosixFilePermission>
getPosixFilePermissions(Path path, LinkOption… options)
throws IOException
Returns POSIX permissions of a directory entry as a set of enum type PosixFilePermission. By default, symbolic links are followed, unless the constant LinkOption.NOFOLLOW_LINKS is specified.
static Path setPosixFilePermissions(Path path,
Set<PosixFilePermission< perms)
throws IOException
Sets the POSIX permissions of a directory entry, given by the parameter perms.
Leave a Reply