jdk/src/share/classes/java/nio/file/attribute/PosixFileAttributeView.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.Set;
       
    30 import java.io.IOException;
       
    31 
       
    32 /**
       
    33  * A file attribute view that provides a view of the file attributes commonly
       
    34  * associated with files on file systems used by operating systems that implement
       
    35  * the Portable Operating System Interface (POSIX) family of standards.
       
    36  *
       
    37  * <p> Operating systems that implement the <a href="http://www.opengroup.org">
       
    38  * POSIX</a> family of standards commonly use file systems that have a
       
    39  * file <em>owner</em>, <em>group-owner</em>, and related <em>access
       
    40  * permissions</em>. This file attribute view provides read and write access
       
    41  * to these attributes.
       
    42  *
       
    43  * <p> The {@link #readAttributes() readAttributes} method is used to read the
       
    44  * file's attributes. The file {@link PosixFileAttributes#owner() owner} is
       
    45  * represented by a {@link UserPrincipal} that is the identity of the file owner
       
    46  * for the purposes of access control. The {@link PosixFileAttributes#group()
       
    47  * group-owner}, represented by a {@link GroupPrincipal}, is the identity of the
       
    48  * group owner, where a group is an identity created for administrative purposes
       
    49  * so as to determine the access rights for the members of the group.
       
    50  *
       
    51  * <p> The {@link PosixFileAttributes#permissions() permissions} attribute is a
       
    52  * set of access permissions. This file attribute view provides access to the nine
       
    53  * permission defined by the {@link PosixFilePermission} class.
       
    54  * These nine permission bits determine the <em>read</em>, <em>write</em>, and
       
    55  * <em>execute</em> access for the file owner, group, and others (others
       
    56  * meaning identities other than the owner and members of the group). Some
       
    57  * operating systems and file systems may provide additional permission bits
       
    58  * but access to these other bits is not defined by this class in this release.
       
    59  *
       
    60  * <p> <b>Usage Example:</b>
       
    61  * Suppose we need to print out the owner and access permissions of a file:
       
    62  * <pre>
       
    63  *     FileRef file = ...
       
    64  *     PosixFileAttributes attrs = file.newFileAttributeView(PosixFileAttributeView.class)
       
    65  *         .readAttributes();
       
    66  *     System.out.format("%s %s%n",
       
    67  *         attrs.owner().getName(),
       
    68  *         PosixFilePermissions.toString(attrs.permissions()));
       
    69  * </pre>
       
    70  *
       
    71  * <h4> Dynamic Access </h4>
       
    72  * <p> Where dynamic access to file attributes is required, the attributes
       
    73  * supported by this attribute view are as defined by {@link
       
    74  * BasicFileAttributeView} and {@link FileOwnerAttributeView}, and in addition,
       
    75  * the following attributes are supported:
       
    76  * <blockquote>
       
    77  * <table border="1" cellpadding="8">
       
    78  *   <tr>
       
    79  *     <th> Name </th>
       
    80  *     <th> Type </th>
       
    81  *   </tr>
       
    82  *  <tr>
       
    83  *     <td> "permissions" </td>
       
    84  *     <td> {@link Set}&lt;{@link PosixFilePermission}&gt; </td>
       
    85  *   </tr>
       
    86  *   <tr>
       
    87  *     <td> "group" </td>
       
    88  *     <td> {@link GroupPrincipal} </td>
       
    89  *   </tr>
       
    90  * </table>
       
    91  * </blockquote>
       
    92  *
       
    93  * <p> The {@link #getAttribute getAttribute} or {@link
       
    94  * #readAttributes(String,String[]) readAttributes(String,String[])} methods may
       
    95  * be used to read any of these attributes, or any of the attributes defined by
       
    96  * {@link BasicFileAttributeView} as if by invoking the {@link #readAttributes
       
    97  * readAttributes()} method.
       
    98  *
       
    99  * <p> The {@link #setAttribute setAttribute} method may be used to update the
       
   100  * file's last modified time, last access time or create time attributes as
       
   101  * defined by {@link BasicFileAttributeView}. It may also be used to update
       
   102  * the permissions, owner, or group-owner as if by invoking the {@link
       
   103  * #setPermissions setPermissions}, {@link #setOwner setOwner}, and {@link
       
   104  * #setGroup setGroup} methods respectively.
       
   105  *
       
   106  * <h4> Setting Initial Permissions </h4>
       
   107  * <p> Implementations supporting this attribute view may also support setting
       
   108  * the initial permissions when creating a file or directory. The
       
   109  * initial permissions are provided to the  {@link Path#createFile createFile}
       
   110  * or {@link Path#createDirectory createDirectory} methods as a {@link
       
   111  * FileAttribute} with {@link FileAttribute#name name} {@code "posix:permissions"}
       
   112  * and a {@link FileAttribute#value value} that is the set of permissions. The
       
   113  * following example uses the {@link PosixFilePermissions#asFileAttribute
       
   114  * asFileAttribute} method to construct a {@code FileAttribute} when creating a
       
   115  * file:
       
   116  *
       
   117  * <pre>
       
   118  *     Path path = ...
       
   119  *     Set&lt;PosixFilePermission&gt; perms =
       
   120  *         EnumSet.of(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE, GROUP_READ);
       
   121  *     path.createFile(PosixFilePermissions.asFileAttribute(perms));
       
   122  * </pre>
       
   123  *
       
   124  * <p> When the access permissions are set at file creation time then the actual
       
   125  * value of the permissions may differ that the value of the attribute object.
       
   126  * The reasons for this are implementation specific. On UNIX systems, for
       
   127  * example, a process has a <em>umask</em> that impacts the permission bits
       
   128  * of newly created files. Where an implementation supports the setting of
       
   129  * the access permissions, and the underlying file system supports access
       
   130  * permissions, then it is required that the value of the actual access
       
   131  * permissions will be equal or less than the value of the attribute
       
   132  * provided to the {@link java.nio.file.Path#createFile createFile} or
       
   133  * {@link java.nio.file.Path#createDirectory createDirectory} methods. In
       
   134  * other words, the file may be more secure than requested.
       
   135  *
       
   136  * @since 1.7
       
   137  *
       
   138  * @see Attributes#readPosixFileAttributes
       
   139  */
       
   140 
       
   141 public interface PosixFileAttributeView
       
   142     extends BasicFileAttributeView, FileOwnerAttributeView
       
   143 {
       
   144     /**
       
   145      * Returns the name of the attribute view. Attribute views of this type
       
   146      * have the name {@code "posix"}.
       
   147      */
       
   148     @Override
       
   149     String name();
       
   150 
       
   151     /**
       
   152      * @throws  IOException                {@inheritDoc}
       
   153      * @throws  SecurityException
       
   154      *          In the case of the default provider, a security manager is
       
   155      *          installed, and it denies {@link RuntimePermission}<tt>("accessUserInformation")</tt>
       
   156      *          or its {@link SecurityManager#checkRead(String) checkRead} method
       
   157      *          denies read access to the file.
       
   158      */
       
   159     @Override
       
   160     PosixFileAttributes readAttributes() throws IOException;
       
   161 
       
   162     /**
       
   163      * Updates the file permissions.
       
   164      *
       
   165      * @param   perms
       
   166      *          The new set of permissions
       
   167      *
       
   168      * @throws  ClassCastException
       
   169      *          If the sets contains elements that are not of type {@code
       
   170      *          PosixFilePermission}
       
   171      * @throws  IOException
       
   172      *          If an I/O error occurs
       
   173      * @throws  SecurityException
       
   174      *          In the case of the default provider, a security manager is
       
   175      *          installed, and it denies {@link RuntimePermission}<tt>("accessUserInformation")</tt>
       
   176      *          or its {@link SecurityManager#checkWrite(String) checkWrite}
       
   177      *          method denies write access to the file.
       
   178      */
       
   179     void setPermissions(Set<PosixFilePermission> perms) throws IOException;
       
   180 
       
   181     /**
       
   182      * Updates the file group-owner.
       
   183      *
       
   184      * @param   group
       
   185      *          The new file group-owner
       
   186 
       
   187      * @throws  IOException
       
   188      *          If an I/O error occurs
       
   189      * @throws  SecurityException
       
   190      *          In the case of the default provider, and a security manager is
       
   191      *          installed, it denies {@link RuntimePermission}<tt>("accessUserInformation")</tt>
       
   192      *          or its {@link SecurityManager#checkWrite(String) checkWrite}
       
   193      *          method denies write access to the file.
       
   194      */
       
   195     void setGroup(GroupPrincipal group) throws IOException;
       
   196 }