Handling File Permissions – Java I/O: Part II

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:

Click here to view code image

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.PosixFilePermissionDescription
OWNER_EXECUTEExecute/search permission, owner
OWNER_READRead permission, owner
OWNER_WRITEWrite permission, owner
GROUP_EXECUTEExecute/search permission, group
GROUP_READRead permission, group
GROUP_WRITEWrite permission, group
OTHERS_EXECUTEExecute/search permission, others
OTHERS_READRead permission, others
OTHERS_WRITEWrite permission, others

Following are methods from the utility class java.nio.file.attribute.PosixFilePermissions:

Click here to view code image

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.

Click here to view code image

static String toString(Set<PosixFilePermission> perms)

Returns the String representation of a set of permissions.

Click here to view code image

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.

Click here to view code image

// 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:

Click here to view code image

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.

Click here to view code image

static Path setPosixFilePermissions(Path path,
                                    Set<PosixFilePermission< perms)
                                    throws IOException

Sets the POSIX permissions of a directory entry, given by the parameter perms.

Categories: , ,

Leave a Reply

Your email address will not be published. Required fields are marked *