jdk/src/share/classes/java/nio/file/attribute/AclFileAttributeView.java
changeset 2057 3acf8e5e2ca0
child 2072 80dfe4469bbd
equal deleted inserted replaced
2056:115e09b7a004 2057:3acf8e5e2ca0
       
     1 /*
       
     2  * Copyright 2007-2009 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 package java.nio.file.attribute;
       
    27 
       
    28 import java.nio.file.*;
       
    29 import java.util.List;
       
    30 import java.io.IOException;
       
    31 
       
    32 /**
       
    33  * A file attribute view that supports reading or updating a file's Access
       
    34  * Control Lists (ACL) or file owner attributes.
       
    35  *
       
    36  * <p> ACLs are used to specify access rights to file system objects. An ACL is
       
    37  * an ordered list of {@link AclEntry access-control-entries}, each specifying a
       
    38  * {@link UserPrincipal} and the level of access for that user principal. This
       
    39  * file attribute view defines the {@link #getAcl() getAcl}, and {@link
       
    40  * #setAcl(List) setAcl} methods to read and write ACLs based on the ACL
       
    41  * model specified in <a href="http://www.ietf.org/rfc/rfc3530.txt"><i>RFC&nbsp;3530:
       
    42  * Network File System (NFS) version 4 Protocol</i></a>. This file attribute view
       
    43  * is intended for file system implementations that support the NFSv4 ACL model
       
    44  * or have a <em>well-defined</em> mapping between the NFSv4 ACL model and the ACL
       
    45  * model used by the file system. The details of such mapping are implementation
       
    46  * dependent and are therefore unspecified.
       
    47  *
       
    48  * <p> This class also extends {@code FileOwnerAttributeView} so as to define
       
    49  * methods to get and set the file owner.
       
    50  *
       
    51  * <p> When a file system provides access to a set of {@link FileStore
       
    52  * file-systems} that are not homogeneous then only some of the file systems may
       
    53  * support ACLs. The {@link FileStore#supportsFileAttributeView
       
    54  * supportsFileAttributeView} method can be used to test if a file system
       
    55  * supports ACLs.
       
    56  *
       
    57  * <a name="interop"><h4>Interoperability</h4></a>
       
    58  *
       
    59  * RFC&nbsp;3530 allows for special user identities to be used on platforms that
       
    60  * support the POSIX defined access permissions. The special user identities
       
    61  * are "{@code OWNER@}", "{@code GROUP@}", and "{@code EVERYONE@}". When both
       
    62  * the {@code AclFileAttributeView} and the {@link PosixFileAttributeView}
       
    63  * are supported then these special user identities may be included in ACL {@link
       
    64  * AclEntry entries} that are read or written. The file system's {@link
       
    65  * UserPrincipalLookupService} may be used to obtain a {@link UserPrincipal}
       
    66  * to represent these special identities by invoking the {@link
       
    67  * UserPrincipalLookupService#lookupPrincipalByName lookupPrincipalByName}
       
    68  * method.
       
    69  *
       
    70  * <p> <b>Usage Example:</b>
       
    71  * Suppose we wish to add an entry to an existing ACL to grant "joe" access:
       
    72  * <pre>
       
    73  *     // lookup "joe"
       
    74  *     UserPrincipal joe = file.getFileSystem().getUserPrincipalLookupService()
       
    75  *         .lookupPrincipalByName("joe");
       
    76  *
       
    77  *     // get view
       
    78  *     AclFileAttributeView view = file.newFileAttributeView(AclFileAttributeView.class);
       
    79  *
       
    80  *     // create ACE to give "joe" read access
       
    81  *     AclEntry entry = AclEntry.newBuilder()
       
    82  *         .setType(AclEntryType.ALLOW)
       
    83  *         .setPrincipal(joe)
       
    84  *         .setPermissions(AclEntryPermission.READ_DATA, AclEntryPermission.READ_ATTRIBUTES)
       
    85  *         .build();
       
    86  *
       
    87  *     // read ACL, insert ACE, re-write ACL
       
    88  *     List&lt;AclEntry&gt acl = view.getAcl();
       
    89  *     acl.add(0, entry);   // insert before any DENY entries
       
    90  *     view.setAcl(acl);
       
    91  * </pre>
       
    92  *
       
    93  * <h4> Dynamic Access </h4>
       
    94  * <p> Where dynamic access to file attributes is required, the attributes
       
    95  * supported by this attribute view are as follows:
       
    96  * <blockquote>
       
    97  * <table border="1" cellpadding="8">
       
    98  *   <tr>
       
    99  *     <th> Name </th>
       
   100  *     <th> Type </th>
       
   101  *   </tr>
       
   102  *   <tr>
       
   103  *     <td> "acl" </td>
       
   104  *     <td> {@link List}&lt;{@link AclEntry}&gt; </td>
       
   105  *   </tr>
       
   106  *   <tr>
       
   107  *     <td> "owner" </td>
       
   108  *     <td> {@link UserPrincipal} </td>
       
   109  *   </tr>
       
   110  * </table>
       
   111  * </blockquote>
       
   112  *
       
   113  * <p> The {@link #getAttribute getAttribute} or {@link #readAttributes
       
   114  * readAttributes} methods may be used to read the ACL or owner attributes as if
       
   115  * by invoking the {@link #getAcl getAcl} or {@link #getOwner getOwner} methods.
       
   116  *
       
   117  * <p> The {@link #setAttribute setAttribute} method may be used to update the
       
   118  * ACL or owner attributes as if by invoking the {@link #setAcl setAcl} or {@link
       
   119  * #setOwner setOwner} methods.
       
   120  *
       
   121  * <h4> Setting the ACL when creating a file </h4>
       
   122  *
       
   123  * <p> Implementations supporting this attribute view may also support setting
       
   124  * the initial ACL when creating a file or directory. The initial ACL
       
   125  * may be provided to methods such as {@link Path#createFile createFile} or {@link
       
   126  * Path#createDirectory createDirectory} as an {@link FileAttribute} with {@link
       
   127  * FileAttribute#name name} {@code "acl:acl"} and a {@link FileAttribute#value
       
   128  * value} that is the list of {@code AclEntry} objects.
       
   129  *
       
   130  * <p> Where an implementation supports an ACL model that differs from the NFSv4
       
   131  * defined ACL model then setting the initial ACL when creating the file must
       
   132  * translate the ACL to the model supported by the file system. Methods that
       
   133  * create a file should reject (by throwing {@link IOException IOException})
       
   134  * any attempt to create a file that would be less secure as a result of the
       
   135  * translation.
       
   136  *
       
   137  * @since 1.7
       
   138  * @see Attributes#getAcl
       
   139  * @see Attributes#setAcl
       
   140  */
       
   141 
       
   142 public interface AclFileAttributeView
       
   143     extends FileOwnerAttributeView
       
   144 {
       
   145     /**
       
   146      * Returns the name of the attribute view. Attribute views of this type
       
   147      * have the name {@code "acl"}.
       
   148      */
       
   149     @Override
       
   150     String name();
       
   151 
       
   152     /**
       
   153      * Reads the access control list.
       
   154      *
       
   155      * <p> When the file system uses an ACL model that differs from the NFSv4
       
   156      * defined ACL model, then this method returns an ACL that is the translation
       
   157      * of the ACL to the NFSv4 ACL model.
       
   158      *
       
   159      * <p> The returned list is modifiable so as to facilitate changes to the
       
   160      * existing ACL. The {@link #setAcl setAcl} method is used to update
       
   161      * the file's ACL attribute.
       
   162      *
       
   163      * @return  An ordered list of {@link AclEntry entries} representing the
       
   164      *          ACL
       
   165      *
       
   166      * @throws  IOException
       
   167      *          If an I/O error occurs
       
   168      * @throws  SecurityException
       
   169      *          In the case of the default provider, a security manager is
       
   170      *          installed, and it denies {@link RuntimePermission}<tt>("accessUserInformation")</tt>
       
   171      *          or its {@link SecurityManager#checkRead(String) checkRead} method
       
   172      *          denies read access to the file.
       
   173      */
       
   174     List<AclEntry> getAcl() throws IOException;
       
   175 
       
   176     /**
       
   177      * Updates (replace) the access control list.
       
   178      *
       
   179      * <p> Where the file system supports Access Control Lists, and it uses an
       
   180      * ACL model that differs from the NFSv4 defined ACL model, then this method
       
   181      * must translate the ACL to the model supported by the file system. This
       
   182      * method should reject (by throwing {@link IOException IOException}) any
       
   183      * attempt to write an ACL that would appear to make the file more secure
       
   184      * than would be the case if the ACL were updated. Where an implementation
       
   185      * does not support a mapping of {@link AclEntryType#AUDIT} or {@link
       
   186      * AclEntryType#ALARM} entries, then this method ignores these entries when
       
   187      * writing the ACL.
       
   188      *
       
   189      * <p> If an ACL entry contains a {@link AclEntry#principal user-principal}
       
   190      * that is not associated with the same provider as this attribute view then
       
   191      * {@link ProviderMismatchException} is thrown. Additional validation, if
       
   192      * any, is implementation dependent.
       
   193      *
       
   194      * <p> If the file system supports other security related file attributes
       
   195      * (such as a file {@link PosixFileAttributes#permissions
       
   196      * access-permissions} for example), the updating the access control list
       
   197      * may also cause these security related attributes to be updated.
       
   198      *
       
   199      * @param   acl
       
   200      *          The new access control list
       
   201      *
       
   202      * @throws  IOException
       
   203      *          If an I/O error occurs or the ACL is invalid
       
   204      * @throws  SecurityException
       
   205      *          In the case of the default provider, a security manager is
       
   206      *          installed, it denies {@link RuntimePermission}<tt>("accessUserInformation")</tt>
       
   207      *          or its {@link SecurityManager#checkWrite(String) checkWrite}
       
   208      *          method denies write access to the file.
       
   209      */
       
   210     void setAcl(List<AclEntry> acl) throws IOException;
       
   211 }