Path: blob/master/src/java.base/share/classes/java/nio/file/attribute/PosixFileAttributeView.java
41161 views
/*1* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.nio.file.attribute;2627import java.nio.file.*;28import java.util.Set;29import java.io.IOException;3031/**32* A file attribute view that provides a view of the file attributes commonly33* associated with files on file systems used by operating systems that implement34* the Portable Operating System Interface (POSIX) family of standards.35*36* <p> Operating systems that implement the <a href="http://www.opengroup.org">37* POSIX</a> family of standards commonly use file systems that have a38* file <em>owner</em>, <em>group-owner</em>, and related <em>access39* permissions</em>. This file attribute view provides read and write access40* to these attributes.41*42* <p> The {@link #readAttributes() readAttributes} method is used to read the43* file's attributes. The file {@link PosixFileAttributes#owner() owner} is44* represented by a {@link UserPrincipal} that is the identity of the file owner45* for the purposes of access control. The {@link PosixFileAttributes#group()46* group-owner}, represented by a {@link GroupPrincipal}, is the identity of the47* group owner, where a group is an identity created for administrative purposes48* so as to determine the access rights for the members of the group.49*50* <p> The {@link PosixFileAttributes#permissions() permissions} attribute is a51* set of access permissions. This file attribute view provides access to the nine52* permission bits defined by the {@link PosixFilePermission} class.53* These nine permission bits determine the <em>read</em>, <em>write</em>, and54* <em>execute</em> access for the file owner, group, and others (others55* meaning identities other than the owner and members of the group). Some56* operating systems and file systems may provide additional permission bits57* but access to these other bits is not defined by this class in this release.58*59* <p> <b>Usage Example:</b>60* Suppose we need to print out the owner and access permissions of a file:61* <pre>62* Path file = ...63* PosixFileAttributes attrs = Files.getFileAttributeView(file, PosixFileAttributeView.class)64* .readAttributes();65* System.out.format("%s %s%n",66* attrs.owner().getName(),67* PosixFilePermissions.toString(attrs.permissions()));68* </pre>69*70* <h2> Dynamic Access </h2>71* <p> Where dynamic access to file attributes is required, the attributes72* supported by this attribute view are as defined by {@link73* BasicFileAttributeView} and {@link FileOwnerAttributeView}, and in addition,74* the following attributes are supported:75* <blockquote>76* <table class="striped">77* <caption style="display:none">Supported attributes</caption>78* <thead>79* <tr>80* <th scope="col"> Name </th>81* <th scope="col"> Type </th>82* </tr>83* </thead>84* <tbody>85* <tr>86* <th scope="row"> "permissions" </th>87* <td> {@link Set}<{@link PosixFilePermission}> </td>88* </tr>89* <tr>90* <th scope="row"> "group" </th>91* <td> {@link GroupPrincipal} </td>92* </tr>93* </tbody>94* </table>95* </blockquote>96*97* <p> The {@link Files#getAttribute getAttribute} method may be used to read98* any of these attributes, or any of the attributes defined by {@link99* BasicFileAttributeView} as if by invoking the {@link #readAttributes100* readAttributes()} method.101*102* <p> The {@link Files#setAttribute setAttribute} method may be used to update103* the file's last modified time, last access time or create time attributes as104* defined by {@link BasicFileAttributeView}. It may also be used to update105* the permissions, owner, or group-owner as if by invoking the {@link106* #setPermissions setPermissions}, {@link #setOwner setOwner}, and {@link107* #setGroup setGroup} methods respectively.108*109* <h2> Setting Initial Permissions </h2>110* <p> Implementations supporting this attribute view may also support setting111* the initial permissions when creating a file or directory. The112* initial permissions are provided to the {@link Files#createFile createFile}113* or {@link Files#createDirectory createDirectory} methods as a {@link114* FileAttribute} with {@link FileAttribute#name name} {@code "posix:permissions"}115* and a {@link FileAttribute#value value} that is the set of permissions. The116* following example uses the {@link PosixFilePermissions#asFileAttribute117* asFileAttribute} method to construct a {@code FileAttribute} when creating a118* file:119*120* <pre>121* Path path = ...122* Set<PosixFilePermission> perms =123* EnumSet.of(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE, GROUP_READ);124* Files.createFile(path, PosixFilePermissions.asFileAttribute(perms));125* </pre>126*127* <p> When the access permissions are set at file creation time then the actual128* value of the permissions may differ from the value of the attribute object.129* The reasons for this are implementation specific. On UNIX systems, for130* example, a process has a <em>umask</em> that impacts the permission bits131* of newly created files. Where an implementation supports the setting of132* the access permissions, and the underlying file system supports access133* permissions, then it is required that the value of the actual access134* permissions will be equal or less than the value of the attribute135* provided to the {@link Files#createFile createFile} or {@link136* Files#createDirectory createDirectory} methods. In other words, the file may137* be more secure than requested.138*139* @since 1.7140*/141142public interface PosixFileAttributeView143extends BasicFileAttributeView, FileOwnerAttributeView144{145/**146* Returns the name of the attribute view. Attribute views of this type147* have the name {@code "posix"}.148*/149@Override150String name();151152/**153* @throws IOException {@inheritDoc}154* @throws SecurityException155* In the case of the default provider, a security manager is156* installed, and it denies157* {@link RuntimePermission}{@code ("accessUserInformation")}158* or its {@link SecurityManager#checkRead(String) checkRead} method159* denies read access to the file.160*/161@Override162PosixFileAttributes readAttributes() throws IOException;163164/**165* Updates the file permissions.166*167* @param perms168* the new set of permissions169*170* @throws ClassCastException171* if the sets contains elements that are not of type {@code172* PosixFilePermission}173* @throws IOException174* if an I/O error occurs175* @throws SecurityException176* In the case of the default provider, a security manager is177* installed, and it denies178* {@link RuntimePermission}{@code ("accessUserInformation")}179* or its {@link SecurityManager#checkWrite(String) checkWrite}180* method denies write access to the file.181*/182void setPermissions(Set<PosixFilePermission> perms) throws IOException;183184/**185* Updates the file group-owner.186*187* @param group188* the new file group-owner189*190* @throws IOException191* if an I/O error occurs192* @throws SecurityException193* In the case of the default provider, and a security manager is194* installed, it denies195* {@link RuntimePermission}{@code ("accessUserInformation")}196* or its {@link SecurityManager#checkWrite(String) checkWrite}197* method denies write access to the file.198*/199void setGroup(GroupPrincipal group) throws IOException;200}201202203