1e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera/* 2e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved. 3e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * 5e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * This code is free software; you can redistribute it and/or modify it 6e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * under the terms of the GNU General Public License version 2 only, as 7e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * published by the Free Software Foundation. Oracle designates this 8e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * particular file as subject to the "Classpath" exception as provided 9e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * by Oracle in the LICENSE file that accompanied this code. 10e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * 11e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * This code is distributed in the hope that it will be useful, but WITHOUT 12e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * version 2 for more details (a copy is included in the LICENSE file that 15e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * accompanied this code). 16e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * 17e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * You should have received a copy of the GNU General Public License version 18e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * 2 along with this work; if not, write to the Free Software Foundation, 19e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * 21e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * or visit www.oracle.com if you need additional information or have any 23e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * questions. 24e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera */ 25e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera 26e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmerapackage java.nio.file.attribute; 27e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera 28e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.nio.file.*; 29e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.util.Set; 30e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.io.IOException; 31e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera 32e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera/** 33e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * A file attribute view that provides a view of the file attributes commonly 34e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * associated with files on file systems used by operating systems that implement 35e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * the Portable Operating System Interface (POSIX) family of standards. 36e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * 37e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * <p> Operating systems that implement the <a href="http://www.opengroup.org"> 38e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * POSIX</a> family of standards commonly use file systems that have a 39e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * file <em>owner</em>, <em>group-owner</em>, and related <em>access 40e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * permissions</em>. This file attribute view provides read and write access 41e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * to these attributes. 42e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * 43e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * <p> The {@link #readAttributes() readAttributes} method is used to read the 44e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * file's attributes. The file {@link PosixFileAttributes#owner() owner} is 45e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * represented by a {@link UserPrincipal} that is the identity of the file owner 46e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * for the purposes of access control. The {@link PosixFileAttributes#group() 47e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * group-owner}, represented by a {@link GroupPrincipal}, is the identity of the 48e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * group owner, where a group is an identity created for administrative purposes 49e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * so as to determine the access rights for the members of the group. 50e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * 51e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * <p> The {@link PosixFileAttributes#permissions() permissions} attribute is a 52e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * set of access permissions. This file attribute view provides access to the nine 53e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * permission defined by the {@link PosixFilePermission} class. 54e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * These nine permission bits determine the <em>read</em>, <em>write</em>, and 55e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * <em>execute</em> access for the file owner, group, and others (others 56e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * meaning identities other than the owner and members of the group). Some 57e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * operating systems and file systems may provide additional permission bits 58e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * but access to these other bits is not defined by this class in this release. 59e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * 60e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * <p> <b>Usage Example:</b> 61e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * Suppose we need to print out the owner and access permissions of a file: 62e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * <pre> 63e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * Path file = ... 64e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * PosixFileAttributes attrs = Files.getFileAttributeView(file, PosixFileAttributeView.class) 65e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * .readAttributes(); 66e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * System.out.format("%s %s%n", 67e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * attrs.owner().getName(), 68e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * PosixFilePermissions.toString(attrs.permissions())); 69e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * </pre> 70e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * 71e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * <h2> Dynamic Access </h2> 72e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * <p> Where dynamic access to file attributes is required, the attributes 73e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * supported by this attribute view are as defined by {@link 74e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * BasicFileAttributeView} and {@link FileOwnerAttributeView}, and in addition, 75e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * the following attributes are supported: 76e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * <blockquote> 77e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * <table border="1" cellpadding="8" summary="Supported attributes"> 78e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * <tr> 79e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * <th> Name </th> 80e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * <th> Type </th> 81e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * </tr> 82e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * <tr> 83e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * <td> "permissions" </td> 84e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * <td> {@link Set}<{@link PosixFilePermission}> </td> 85e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * </tr> 86e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * <tr> 87e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * <td> "group" </td> 88e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * <td> {@link GroupPrincipal} </td> 89e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * </tr> 90e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * </table> 91e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * </blockquote> 92e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * 93e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * <p> The {@link Files#getAttribute getAttribute} method may be used to read 94e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * any of these attributes, or any of the attributes defined by {@link 95e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * BasicFileAttributeView} as if by invoking the {@link #readAttributes 96e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * readAttributes()} method. 97e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * 98e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * <p> The {@link Files#setAttribute setAttribute} method may be used to update 99e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * the file's last modified time, last access time or create time attributes as 100e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * defined by {@link BasicFileAttributeView}. It may also be used to update 101e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * the permissions, owner, or group-owner as if by invoking the {@link 102e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * #setPermissions setPermissions}, {@link #setOwner setOwner}, and {@link 103e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * #setGroup setGroup} methods respectively. 104e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * 105e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * <h2> Setting Initial Permissions </h2> 106e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * <p> Implementations supporting this attribute view may also support setting 107e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * the initial permissions when creating a file or directory. The 108e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * initial permissions are provided to the {@link Files#createFile createFile} 109e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * or {@link Files#createDirectory createDirectory} methods as a {@link 110e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * FileAttribute} with {@link FileAttribute#name name} {@code "posix:permissions"} 111e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * and a {@link FileAttribute#value value} that is the set of permissions. The 112e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * following example uses the {@link PosixFilePermissions#asFileAttribute 113e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * asFileAttribute} method to construct a {@code FileAttribute} when creating a 114e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * file: 115e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * 116e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * <pre> 117e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * Path path = ... 118e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * Set<PosixFilePermission> perms = 119e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * EnumSet.of(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE, GROUP_READ); 120e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * Files.createFile(path, PosixFilePermissions.asFileAttribute(perms)); 121e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * </pre> 122e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * 123e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * <p> When the access permissions are set at file creation time then the actual 124e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * value of the permissions may differ that the value of the attribute object. 125e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * The reasons for this are implementation specific. On UNIX systems, for 126e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * example, a process has a <em>umask</em> that impacts the permission bits 127e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * of newly created files. Where an implementation supports the setting of 128e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * the access permissions, and the underlying file system supports access 129e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * permissions, then it is required that the value of the actual access 130e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * permissions will be equal or less than the value of the attribute 131e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * provided to the {@link Files#createFile createFile} or {@link 132e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * Files#createDirectory createDirectory} methods. In other words, the file may 133e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * be more secure than requested. 134e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * 135e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * @since 1.7 136e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera */ 137e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera 138e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmerapublic interface PosixFileAttributeView 139e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera extends BasicFileAttributeView, FileOwnerAttributeView 140e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera{ 141e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera /** 142e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * Returns the name of the attribute view. Attribute views of this type 143e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * have the name {@code "posix"}. 144e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera */ 145e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera @Override 146e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera String name(); 147e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera 148e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera /** 149e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * @throws IOException {@inheritDoc} 150e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * @throws SecurityException 151e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * In the case of the default provider, a security manager is 152e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * installed, and it denies {@link RuntimePermission}<tt>("accessUserInformation")</tt> 153e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * or its {@link SecurityManager#checkRead(String) checkRead} method 154e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * denies read access to the file. 155e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera */ 156e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera @Override 157e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera PosixFileAttributes readAttributes() throws IOException; 158e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera 159e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera /** 160e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * Updates the file permissions. 161e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * 162e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * @param perms 163e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * the new set of permissions 164e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * 165e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * @throws ClassCastException 166e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * if the sets contains elements that are not of type {@code 167e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * PosixFilePermission} 168e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * @throws IOException 169e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * if an I/O error occurs 170e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * @throws SecurityException 171e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * In the case of the default provider, a security manager is 172e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * installed, and it denies {@link RuntimePermission}<tt>("accessUserInformation")</tt> 173e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * or its {@link SecurityManager#checkWrite(String) checkWrite} 174e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * method denies write access to the file. 175e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera */ 176e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera void setPermissions(Set<PosixFilePermission> perms) throws IOException; 177e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera 178e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera /** 179e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * Updates the file group-owner. 180e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * 181e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * @param group 182e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * the new file group-owner 183e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * 184e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * @throws IOException 185e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * if an I/O error occurs 186e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * @throws SecurityException 187e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * In the case of the default provider, and a security manager is 188e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * installed, it denies {@link RuntimePermission}<tt>("accessUserInformation")</tt> 189e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * or its {@link SecurityManager#checkWrite(String) checkWrite} 190e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * method denies write access to the file. 191e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera */ 192e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera void setGroup(GroupPrincipal group) throws IOException; 193e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera} 194