• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 border="1" cellpadding="8" summary="Supported attributes">
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 Files#getAttribute getAttribute} method may be used to read
94  * any of these attributes, or any of the attributes defined by {@link
95  * BasicFileAttributeView} as if by invoking the {@link #readAttributes
96  * readAttributes()} method.
97  *
98  * <p> The {@link Files#setAttribute setAttribute} method may be used to update
99  * the file's last modified time, last access time or create time attributes as
100  * defined by {@link BasicFileAttributeView}. It may also be used to update
101  * the permissions, owner, or group-owner as if by invoking the {@link
102  * #setPermissions setPermissions}, {@link #setOwner setOwner}, and {@link
103  * #setGroup setGroup} methods respectively.
104  *
105  * <h2> Setting Initial Permissions </h2>
106  * <p> Implementations supporting this attribute view may also support setting
107  * the initial permissions when creating a file or directory. The
108  * initial permissions are provided to the {@link Files#createFile createFile}
109  * or {@link Files#createDirectory createDirectory} methods as a {@link
110  * FileAttribute} with {@link FileAttribute#name name} {@code "posix:permissions"}
111  * and a {@link FileAttribute#value value} that is the set of permissions. The
112  * following example uses the {@link PosixFilePermissions#asFileAttribute
113  * asFileAttribute} method to construct a {@code FileAttribute} when creating a
114  * file:
115  *
116  * <pre>
117  *     Path path = ...
118  *     Set&lt;PosixFilePermission&gt; perms =
119  *         EnumSet.of(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE, GROUP_READ);
120  *     Files.createFile(path, PosixFilePermissions.asFileAttribute(perms));
121  * </pre>
122  *
123  * <p> When the access permissions are set at file creation time then the actual
124  * value of the permissions may differ that the value of the attribute object.
125  * The reasons for this are implementation specific. On UNIX systems, for
126  * example, a process has a <em>umask</em> that impacts the permission bits
127  * of newly created files. Where an implementation supports the setting of
128  * the access permissions, and the underlying file system supports access
129  * permissions, then it is required that the value of the actual access
130  * permissions will be equal or less than the value of the attribute
131  * provided to the {@link Files#createFile createFile} or {@link
132  * Files#createDirectory createDirectory} methods. In other words, the file may
133  * be more secure than requested.
134  *
135  * @since 1.7
136  */
137 
138 public interface PosixFileAttributeView
139     extends BasicFileAttributeView, FileOwnerAttributeView
140 {
141     /**
142      * Returns the name of the attribute view. Attribute views of this type
143      * have the name {@code "posix"}.
144      */
145     @Override
name()146     String name();
147 
148     /**
149      * @throws  IOException                {@inheritDoc}
150      * @throws  SecurityException
151      *          In the case of the default provider, a security manager is
152      *          installed, and it denies {@link RuntimePermission}<tt>("accessUserInformation")</tt>
153      *          or its {@link SecurityManager#checkRead(String) checkRead} method
154      *          denies read access to the file.
155      */
156     @Override
readAttributes()157     PosixFileAttributes readAttributes() throws IOException;
158 
159     /**
160      * Updates the file permissions.
161      *
162      * @param   perms
163      *          the new set of permissions
164      *
165      * @throws  ClassCastException
166      *          if the sets contains elements that are not of type {@code
167      *          PosixFilePermission}
168      * @throws  IOException
169      *          if an I/O error occurs
170      * @throws  SecurityException
171      *          In the case of the default provider, a security manager is
172      *          installed, and it denies {@link RuntimePermission}<tt>("accessUserInformation")</tt>
173      *          or its {@link SecurityManager#checkWrite(String) checkWrite}
174      *          method denies write access to the file.
175      */
setPermissions(Set<PosixFilePermission> perms)176     void setPermissions(Set<PosixFilePermission> perms) throws IOException;
177 
178     /**
179      * Updates the file group-owner.
180      *
181      * @param   group
182      *          the new file group-owner
183      *
184      * @throws  IOException
185      *          if an I/O error occurs
186      * @throws  SecurityException
187      *          In the case of the default provider, and a security manager is
188      *          installed, it denies {@link RuntimePermission}<tt>("accessUserInformation")</tt>
189      *          or its {@link SecurityManager#checkWrite(String) checkWrite}
190      *          method denies write access to the file.
191      */
setGroup(GroupPrincipal group)192     void setGroup(GroupPrincipal group) throws IOException;
193 }
194