Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/nio/file/attribute/PosixFileAttributeView.java
38918 views
/*1* Copyright (c) 2007, 2013, 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 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 border="1" cellpadding="8" summary="Supported attributes">77* <tr>78* <th> Name </th>79* <th> Type </th>80* </tr>81* <tr>82* <td> "permissions" </td>83* <td> {@link Set}<{@link PosixFilePermission}> </td>84* </tr>85* <tr>86* <td> "group" </td>87* <td> {@link GroupPrincipal} </td>88* </tr>89* </table>90* </blockquote>91*92* <p> The {@link Files#getAttribute getAttribute} method may be used to read93* any of these attributes, or any of the attributes defined by {@link94* BasicFileAttributeView} as if by invoking the {@link #readAttributes95* readAttributes()} method.96*97* <p> The {@link Files#setAttribute setAttribute} method may be used to update98* the file's last modified time, last access time or create time attributes as99* defined by {@link BasicFileAttributeView}. It may also be used to update100* the permissions, owner, or group-owner as if by invoking the {@link101* #setPermissions setPermissions}, {@link #setOwner setOwner}, and {@link102* #setGroup setGroup} methods respectively.103*104* <h2> Setting Initial Permissions </h2>105* <p> Implementations supporting this attribute view may also support setting106* the initial permissions when creating a file or directory. The107* initial permissions are provided to the {@link Files#createFile createFile}108* or {@link Files#createDirectory createDirectory} methods as a {@link109* FileAttribute} with {@link FileAttribute#name name} {@code "posix:permissions"}110* and a {@link FileAttribute#value value} that is the set of permissions. The111* following example uses the {@link PosixFilePermissions#asFileAttribute112* asFileAttribute} method to construct a {@code FileAttribute} when creating a113* file:114*115* <pre>116* Path path = ...117* Set<PosixFilePermission> perms =118* EnumSet.of(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE, GROUP_READ);119* Files.createFile(path, PosixFilePermissions.asFileAttribute(perms));120* </pre>121*122* <p> When the access permissions are set at file creation time then the actual123* value of the permissions may differ that the value of the attribute object.124* The reasons for this are implementation specific. On UNIX systems, for125* example, a process has a <em>umask</em> that impacts the permission bits126* of newly created files. Where an implementation supports the setting of127* the access permissions, and the underlying file system supports access128* permissions, then it is required that the value of the actual access129* permissions will be equal or less than the value of the attribute130* provided to the {@link Files#createFile createFile} or {@link131* Files#createDirectory createDirectory} methods. In other words, the file may132* be more secure than requested.133*134* @since 1.7135*/136137public interface PosixFileAttributeView138extends BasicFileAttributeView, FileOwnerAttributeView139{140/**141* Returns the name of the attribute view. Attribute views of this type142* have the name {@code "posix"}.143*/144@Override145String name();146147/**148* @throws IOException {@inheritDoc}149* @throws SecurityException150* In the case of the default provider, a security manager is151* installed, and it denies {@link RuntimePermission}<tt>("accessUserInformation")</tt>152* or its {@link SecurityManager#checkRead(String) checkRead} method153* denies read access to the file.154*/155@Override156PosixFileAttributes readAttributes() throws IOException;157158/**159* Updates the file permissions.160*161* @param perms162* the new set of permissions163*164* @throws ClassCastException165* if the sets contains elements that are not of type {@code166* PosixFilePermission}167* @throws IOException168* if an I/O error occurs169* @throws SecurityException170* In the case of the default provider, a security manager is171* installed, and it denies {@link RuntimePermission}<tt>("accessUserInformation")</tt>172* or its {@link SecurityManager#checkWrite(String) checkWrite}173* method denies write access to the file.174*/175void setPermissions(Set<PosixFilePermission> perms) throws IOException;176177/**178* Updates the file group-owner.179*180* @param group181* the new file group-owner182*183* @throws IOException184* if an I/O error occurs185* @throws SecurityException186* In the case of the default provider, and a security manager is187* installed, it denies {@link RuntimePermission}<tt>("accessUserInformation")</tt>188* or its {@link SecurityManager#checkWrite(String) checkWrite}189* method denies write access to the file.190*/191void setGroup(GroupPrincipal group) throws IOException;192}193194195