Path: blob/master/src/java.base/share/classes/java/nio/file/attribute/AclFileAttributeView.java
41161 views
/*1* Copyright (c) 2007, 2017, 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.List;29import java.io.IOException;3031/**32* A file attribute view that supports reading or updating a file's Access33* Control Lists (ACL) or file owner attributes.34*35* <p> ACLs are used to specify access rights to file system objects. An ACL is36* an ordered list of {@link AclEntry access-control-entries}, each specifying a37* {@link UserPrincipal} and the level of access for that user principal. This38* file attribute view defines the {@link #getAcl() getAcl}, and {@link39* #setAcl(List) setAcl} methods to read and write ACLs based on the ACL40* model specified in <a href="http://www.ietf.org/rfc/rfc3530.txt"><i>RFC 3530:41* Network File System (NFS) version 4 Protocol</i></a>. This file attribute view42* is intended for file system implementations that support the NFSv4 ACL model43* or have a <em>well-defined</em> mapping between the NFSv4 ACL model and the ACL44* model used by the file system. The details of such mapping are implementation45* dependent and are therefore unspecified.46*47* <p> This class also extends {@code FileOwnerAttributeView} so as to define48* methods to get and set the file owner.49*50* <p> When a file system provides access to a set of {@link FileStore51* file-systems} that are not homogeneous then only some of the file systems may52* support ACLs. The {@link FileStore#supportsFileAttributeView53* supportsFileAttributeView} method can be used to test if a file system54* supports ACLs.55*56* <h2>Interoperability</h2>57*58* RFC 3530 allows for special user identities to be used on platforms that59* support the POSIX defined access permissions. The special user identities60* are "{@code OWNER@}", "{@code GROUP@}", and "{@code EVERYONE@}". When both61* the {@code AclFileAttributeView} and the {@link PosixFileAttributeView}62* are supported then these special user identities may be included in ACL {@link63* AclEntry entries} that are read or written. The file system's {@link64* UserPrincipalLookupService} may be used to obtain a {@link UserPrincipal}65* to represent these special identities by invoking the {@link66* UserPrincipalLookupService#lookupPrincipalByName lookupPrincipalByName}67* method.68*69* <p> <b>Usage Example:</b>70* Suppose we wish to add an entry to an existing ACL to grant "joe" access:71* <pre>72* // lookup "joe"73* UserPrincipal joe = file.getFileSystem().getUserPrincipalLookupService()74* .lookupPrincipalByName("joe");75*76* // get view77* AclFileAttributeView view = Files.getFileAttributeView(file, AclFileAttributeView.class);78*79* // create ACE to give "joe" read access80* AclEntry entry = AclEntry.newBuilder()81* .setType(AclEntryType.ALLOW)82* .setPrincipal(joe)83* .setPermissions(AclEntryPermission.READ_DATA, AclEntryPermission.READ_ATTRIBUTES)84* .build();85*86* // read ACL, insert ACE, re-write ACL87* List<AclEntry> acl = view.getAcl();88* acl.add(0, entry); // insert before any DENY entries89* view.setAcl(acl);90* </pre>91*92* <h2> Dynamic Access </h2>93* <p> Where dynamic access to file attributes is required, the attributes94* supported by this attribute view are as follows:95* <blockquote>96* <table class="striped">97* <caption style="display:none">Supported attributes</caption>98* <thead>99* <tr>100* <th scope="col"> Name </th>101* <th scope="col"> Type </th>102* </tr>103* </thead>104* <tbody>105* <tr>106* <th scope="row"> "acl" </th>107* <td> {@link List}<{@link AclEntry}> </td>108* </tr>109* <tr>110* <th scope="row"> "owner" </th>111* <td> {@link UserPrincipal} </td>112* </tr>113* </tbody>114* </table>115* </blockquote>116*117* <p> The {@link Files#getAttribute getAttribute} method may be used to read118* the ACL or owner attributes as if by invoking the {@link #getAcl getAcl} or119* {@link #getOwner getOwner} methods.120*121* <p> The {@link Files#setAttribute setAttribute} method may be used to122* update the ACL or owner attributes as if by invoking the {@link #setAcl setAcl}123* or {@link #setOwner setOwner} methods.124*125* <h2> Setting the ACL when creating a file </h2>126*127* <p> Implementations supporting this attribute view may also support setting128* the initial ACL when creating a file or directory. The initial ACL129* may be provided to methods such as {@link Files#createFile createFile} or {@link130* Files#createDirectory createDirectory} as an {@link FileAttribute} with {@link131* FileAttribute#name name} {@code "acl:acl"} and a {@link FileAttribute#value132* value} that is the list of {@code AclEntry} objects.133*134* <p> Where an implementation supports an ACL model that differs from the NFSv4135* defined ACL model then setting the initial ACL when creating the file must136* translate the ACL to the model supported by the file system. Methods that137* create a file should reject (by throwing {@link IOException IOException})138* any attempt to create a file that would be less secure as a result of the139* translation.140*141* @since 1.7142*/143144public interface AclFileAttributeView145extends FileOwnerAttributeView146{147/**148* Returns the name of the attribute view. Attribute views of this type149* have the name {@code "acl"}.150*/151@Override152String name();153154/**155* Reads the access control list.156*157* <p> When the file system uses an ACL model that differs from the NFSv4158* defined ACL model, then this method returns an ACL that is the translation159* of the ACL to the NFSv4 ACL model.160*161* <p> The returned list is modifiable so as to facilitate changes to the162* existing ACL. The {@link #setAcl setAcl} method is used to update163* the file's ACL attribute.164*165* @return an ordered list of {@link AclEntry entries} representing the166* ACL167*168* @throws IOException169* if an I/O error occurs170* @throws SecurityException171* In the case of the default provider, a security manager is172* installed, and it denies {@link RuntimePermission}{@code ("accessUserInformation")}173* or its {@link SecurityManager#checkRead(String) checkRead} method174* denies read access to the file.175*/176List<AclEntry> getAcl() throws IOException;177178/**179* Updates (replace) the access control list.180*181* <p> Where the file system supports Access Control Lists, and it uses an182* ACL model that differs from the NFSv4 defined ACL model, then this method183* must translate the ACL to the model supported by the file system. This184* method should reject (by throwing {@link IOException IOException}) any185* attempt to write an ACL that would appear to make the file more secure186* than would be the case if the ACL were updated. Where an implementation187* does not support a mapping of {@link AclEntryType#AUDIT} or {@link188* AclEntryType#ALARM} entries, then this method ignores these entries when189* writing the ACL.190*191* <p> If an ACL entry contains a {@link AclEntry#principal user-principal}192* that is not associated with the same provider as this attribute view then193* {@link ProviderMismatchException} is thrown. Additional validation, if194* any, is implementation dependent.195*196* <p> If the file system supports other security related file attributes197* (such as a file {@link PosixFileAttributes#permissions198* access-permissions} for example), the updating the access control list199* may also cause these security related attributes to be updated.200*201* @param acl202* the new access control list203*204* @throws IOException205* if an I/O error occurs or the ACL is invalid206* @throws SecurityException207* In the case of the default provider, a security manager is208* installed, it denies {@link RuntimePermission}{@code ("accessUserInformation")}209* or its {@link SecurityManager#checkWrite(String) checkWrite}210* method denies write access to the file.211*/212void setAcl(List<AclEntry> acl) throws IOException;213}214215216