The {@link #readAttributes() readAttributes} method is used to read thefile's attributes. The file {@link PosixFileAttributes#owner() owner} isrepresented by a {@link UserPrincipal} that is the identity of the file ownerfor the purposes of access control. The {@link PosixFileAttributes#group() group-owner}, represented by a {@link GroupPrincipal}, is the identity of the group owner, where a group is an identity created for administrative purposes so as to determine the access rights for the members of the group.
The {@link PosixFileAttributes#permissions() permissions} attribute is aset of access permissions. This file attribute view provides access to the nine permission defined by the {@link PosixFilePermission} class.These nine permission bits determine the read, write, and execute access for the file owner, group, and others (others meaning identities other than the owner and members of the group). Some operating systems and file systems may provide additional permission bits but access to these other bits is not defined by this class in this release.
Usage Example: Suppose we need to print out the owner and access permissions of a file:
Path file = ... PosixFileAttributes attrs = Files.getFileAttributeView(file, PosixFileAttributeView.class) .readAttributes(); System.out.format("%s %s%n", attrs.owner().getName(), PosixFilePermissions.toString(attrs.permissions()));
Where dynamic access to file attributes is required, the attributes supported by this attribute view are as defined by {@link BasicFileAttributeView} and {@link FileOwnerAttributeView}, and in addition, the following attributes are supported:
Name Type "permissions" {@link Set}< {@link PosixFilePermission}> "group" {@link GroupPrincipal}
The {@link Files#getAttribute getAttribute} method may be used to readany of these attributes, or any of the attributes defined by {@link BasicFileAttributeView} as if by invoking the {@link #readAttributes readAttributes()} method.
The {@link Files#setAttribute setAttribute} method may be used to updatethe file's last modified time, last access time or create time attributes as defined by {@link BasicFileAttributeView}. It may also be used to update the permissions, owner, or group-owner as if by invoking the {@link #setPermissions setPermissions}, {@link #setOwner setOwner}, and {@link #setGroup setGroup} methods respectively.
Implementations supporting this attribute view may also support setting the initial permissions when creating a file or directory. The initial permissions are provided to the {@link Files#createFile createFile}or {@link Files#createDirectory createDirectory} methods as a {@link FileAttribute} with {@link FileAttribute#name name} {@code "posix:permissions"}and a {@link FileAttribute#value value} that is the set of permissions. Thefollowing example uses the {@link PosixFilePermissions#asFileAttribute asFileAttribute} method to construct a {@code FileAttribute} when creating afile:
Path path = ... Set<PosixFilePermission> perms = EnumSet.of(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE, GROUP_READ); Files.createFile(path, PosixFilePermissions.asFileAttribute(perms));
When the access permissions are set at file creation time then the actual value of the permissions may differ that the value of the attribute object. The reasons for this are implementation specific. On UNIX systems, for example, a process has a umask that impacts the permission bits of newly created files. Where an implementation supports the setting of the access permissions, and the underlying file system supports access permissions, then it is required that the value of the actual access permissions will be equal or less than the value of the attribute provided to the {@link Files#createFile createFile} or {@link Files#createDirectory createDirectory} methods. In other words, the file maybe more secure than requested. @since 1.7
|
|
|
|
|
|
|
|
|
|
|
|
|
|