1 /*
   2  * Copyright (c) 2007, 2013, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * 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  *     Path file = ...
  64  *     PosixFileAttributes attrs = Files.getFileAttributeView(file, PosixFileAttributeView.class)
  65  *         .readAttributes();
  66  *     System.out.format("%s %s%n",
  67  *         attrs.owner().getName(),
  68  *         PosixFilePermissions.toString(attrs.permissions()));
  69  * </pre>
  70  *
  71  * <h2> Dynamic Access </h2>
  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 class="altrows">
  78  * <caption style="display:none">Supported attributes</caption>
  79  * <thead>
  80  *   <tr>
  81  *     <th> Name </th>
  82  *     <th> Type </th>
  83  *   </tr>
  84  * </thead>
  85  * <tbody>
  86  *  <tr>
  87  *     <td> "permissions" </td>
  88  *     <td> {@link Set}&lt;{@link PosixFilePermission}&gt; </td>
  89  *   </tr>
  90  *   <tr>
  91  *     <td> "group" </td>
  92  *     <td> {@link GroupPrincipal} </td>
  93  *   </tr>
  94  * </tbody>
  95  * </table>
  96  * </blockquote>
  97  *
  98  * <p> The {@link Files#getAttribute getAttribute} method may be used to read
  99  * any of these attributes, or any of the attributes defined by {@link
 100  * BasicFileAttributeView} as if by invoking the {@link #readAttributes
 101  * readAttributes()} method.
 102  *
 103  * <p> The {@link Files#setAttribute setAttribute} method may be used to update
 104  * the file's last modified time, last access time or create time attributes as
 105  * defined by {@link BasicFileAttributeView}. It may also be used to update
 106  * the permissions, owner, or group-owner as if by invoking the {@link
 107  * #setPermissions setPermissions}, {@link #setOwner setOwner}, and {@link
 108  * #setGroup setGroup} methods respectively.
 109  *
 110  * <h2> Setting Initial Permissions </h2>
 111  * <p> Implementations supporting this attribute view may also support setting
 112  * the initial permissions when creating a file or directory. The
 113  * initial permissions are provided to the {@link Files#createFile createFile}
 114  * or {@link Files#createDirectory createDirectory} methods as a {@link
 115  * FileAttribute} with {@link FileAttribute#name name} {@code "posix:permissions"}
 116  * and a {@link FileAttribute#value value} that is the set of permissions. The
 117  * following example uses the {@link PosixFilePermissions#asFileAttribute
 118  * asFileAttribute} method to construct a {@code FileAttribute} when creating a
 119  * file:
 120  *
 121  * <pre>
 122  *     Path path = ...
 123  *     Set&lt;PosixFilePermission&gt; perms =
 124  *         EnumSet.of(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE, GROUP_READ);
 125  *     Files.createFile(path, PosixFilePermissions.asFileAttribute(perms));
 126  * </pre>
 127  *
 128  * <p> When the access permissions are set at file creation time then the actual
 129  * value of the permissions may differ that the value of the attribute object.
 130  * The reasons for this are implementation specific. On UNIX systems, for
 131  * example, a process has a <em>umask</em> that impacts the permission bits
 132  * of newly created files. Where an implementation supports the setting of
 133  * the access permissions, and the underlying file system supports access
 134  * permissions, then it is required that the value of the actual access
 135  * permissions will be equal or less than the value of the attribute
 136  * provided to the {@link Files#createFile createFile} or {@link
 137  * Files#createDirectory createDirectory} methods. In other words, the file may
 138  * be more secure than requested.
 139  *
 140  * @since 1.7
 141  */
 142 
 143 public interface PosixFileAttributeView
 144     extends BasicFileAttributeView, FileOwnerAttributeView
 145 {
 146     /**
 147      * Returns the name of the attribute view. Attribute views of this type
 148      * have the name {@code "posix"}.
 149      */
 150     @Override
 151     String name();
 152 
 153     /**
 154      * @throws  IOException                {@inheritDoc}
 155      * @throws  SecurityException
 156      *          In the case of the default provider, a security manager is
 157      *          installed, and it denies
 158      *          {@link RuntimePermission}{@code ("accessUserInformation")}
 159      *          or its {@link SecurityManager#checkRead(String) checkRead} method
 160      *          denies read access to the file.
 161      */
 162     @Override
 163     PosixFileAttributes readAttributes() throws IOException;
 164 
 165     /**
 166      * Updates the file permissions.
 167      *
 168      * @param   perms
 169      *          the new set of permissions
 170      *
 171      * @throws  ClassCastException
 172      *          if the sets contains elements that are not of type {@code
 173      *          PosixFilePermission}
 174      * @throws  IOException
 175      *          if an I/O error occurs
 176      * @throws  SecurityException
 177      *          In the case of the default provider, a security manager is
 178      *          installed, and it denies
 179      *          {@link RuntimePermission}{@code ("accessUserInformation")}
 180      *          or its {@link SecurityManager#checkWrite(String) checkWrite}
 181      *          method denies write access to the file.
 182      */
 183     void setPermissions(Set<PosixFilePermission> perms) throws IOException;
 184 
 185     /**
 186      * Updates the file group-owner.
 187      *
 188      * @param   group
 189      *          the new file group-owner
 190      *
 191      * @throws  IOException
 192      *          if an I/O error occurs
 193      * @throws  SecurityException
 194      *          In the case of the default provider, and a security manager is
 195      *          installed, it denies
 196      *          {@link RuntimePermission}{@code ("accessUserInformation")}
 197      *          or its {@link SecurityManager#checkWrite(String) checkWrite}
 198      *          method denies write access to the file.
 199      */
 200     void setGroup(GroupPrincipal group) throws IOException;
 201 }