public interfacePosixFileAttributeViewextendsBasicFileAttributeView,FileOwnerAttributeView
Operating systems that implement the POSIX family of standards commonly use file systems that have a fileowner,group-owner, and relatedaccess permissions. This file attribute view provides read and write access to these attributes.
ThereadAttributes method is used to read the file's attributes. The fileowner is represented by aUserPrincipal that is the identity of the file owner for the purposes of access control. Thegroup-owner, represented by aGroupPrincipal, 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.
Thepermissions attribute is a set of access permissions. This file attribute view provides access to the nine permission defined by thePosixFilePermission class. These nine permission bits determine theread,write, andexecute 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 byBasicFileAttributeView andFileOwnerAttributeView, and in addition, the following attributes are supported:
Name Type "permissions" Set<PosixFilePermission>"group" GroupPrincipal
ThegetAttribute method may be used to read any of these attributes, or any of the attributes defined byBasicFileAttributeView as if by invoking thereadAttributes() method.
ThesetAttribute method may be used to update the file's last modified time, last access time or create time attributes as defined byBasicFileAttributeView. It may also be used to update the permissions, owner, or group-owner as if by invoking thesetPermissions,setOwner, andsetGroup 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 thecreateFile orcreateDirectory methods as aFileAttribute withname"posix:permissions" and avalue that is the set of permissions. The following example uses theasFileAttribute method to construct aFileAttribute when creating a file:
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 aumask 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 thecreateFile orcreateDirectory methods. In other words, the file may be more secure than requested.
| Modifier and Type | Method | Description |
|---|---|---|
String | name() | Returns the name of the attribute view. |
PosixFileAttributes | readAttributes() | Reads the basic file attributes as a bulk operation. |
void | setGroup(GroupPrincipal group) | Updates the file group-owner. |
void | setPermissions(Set<PosixFilePermission> perms) | Updates the file permissions. |
setTimesgetOwner,setOwnerString name()
"posix".name in interface AttributeViewname in interface BasicFileAttributeViewname in interface FileOwnerAttributeViewPosixFileAttributes readAttributes() throwsIOException
BasicFileAttributeViewIt is implementation specific if all file attributes are read as an atomic operation with respect to other file system operations.
readAttributes in interface BasicFileAttributeViewIOException - if an I/O error occursSecurityException - In the case of the default provider, a security manager is installed, and it deniesRuntimePermission("accessUserInformation") or itscheckRead method denies read access to the file.void setPermissions(Set<PosixFilePermission> perms) throwsIOException
perms - the new set of permissionsClassCastException - if the sets contains elements that are not of typePosixFilePermissionIOException - if an I/O error occursSecurityException - In the case of the default provider, a security manager is installed, and it deniesRuntimePermission("accessUserInformation") or itscheckWrite method denies write access to the file.void setGroup(GroupPrincipal group) throwsIOException
group - the new file group-ownerIOException - if an I/O error occursSecurityException - In the case of the default provider, and a security manager is installed, it deniesRuntimePermission("accessUserInformation") or itscheckWrite method denies write access to the file.