java.nio.file.attribute.FileOwnerAttributeView Java Examples

The following examples show how to use java.nio.file.attribute.FileOwnerAttributeView. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CommandDelegatorMethods.java    From jumbune with GNU Lesser General Public License v3.0 6 votes vote down vote up
/** This method changes the ownership of folder/file to the user provided
 * @param destinationLocation : the location of the path to be owned
 * @param user : the user which needs to own the folder/file
 */
private void changeOwnership(String destinationLocation, String user){
	
	Path path = Paths.get(destinationLocation);
    FileOwnerAttributeView view = Files.getFileAttributeView(path,
        FileOwnerAttributeView.class);
    UserPrincipalLookupService lookupService = FileSystems.getDefault()
        .getUserPrincipalLookupService();
    UserPrincipal userPrincipal;
	try {
		userPrincipal = lookupService.lookupPrincipalByName(user);
		Files.setOwner(path, userPrincipal);
	} catch (IOException e) {
		LOGGER.error("Error while changing ownership of destination location[" + destinationLocation +"]" + "to user[" + user + "]" , e);
	}
}
 
Example #2
Source File: OwnerAttributeProviderTest.java    From jimfs with Apache License 2.0 5 votes vote down vote up
@Test
public void testView() throws IOException {
  FileOwnerAttributeView view = provider.view(fileLookup(), NO_INHERITED_VIEWS);
  assertThat(view).isNotNull();

  assertThat(view.name()).isEqualTo("owner");
  assertThat(view.getOwner()).isEqualTo(createUserPrincipal("user"));

  view.setOwner(createUserPrincipal("root"));
  assertThat(view.getOwner()).isEqualTo(createUserPrincipal("root"));
  assertThat(file.getAttribute("owner", "owner")).isEqualTo(createUserPrincipal("root"));
}
 
Example #3
Source File: SolrCLI.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public static String userForDir(Path pathToDir) {
  try {
    FileOwnerAttributeView ownerAttributeView = Files.getFileAttributeView(pathToDir, FileOwnerAttributeView.class);
    return ownerAttributeView.getOwner().getName();
  } catch (IOException e) {
    return "N/A";
  }
}
 
Example #4
Source File: SFTPFileSystemProvider.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a file attribute view of a given type.
 * This method works in exactly the manner specified by the {@link Files#getFileAttributeView(Path, Class, LinkOption...)} method.
 * <p>
 * This provider supports {@link BasicFileAttributeView}, {@link FileOwnerAttributeView} and {@link PosixFileAttributeView}.
 * All other classes will result in a {@code null} return value.
 * <p>
 * Note: if the type is {@link BasicFileAttributeView} or a sub type, the last access time and creation time must be {@code null} when calling
 * {@link BasicFileAttributeView#setTimes(FileTime, FileTime, FileTime)}, otherwise an exception will be thrown.
 * When setting the owner or group for the path, the name must be the UID/GID of the owner/group.
 */
@Override
public <V extends FileAttributeView> V getFileAttributeView(Path path, Class<V> type, LinkOption... options) {
    Objects.requireNonNull(type);
    if (type == BasicFileAttributeView.class) {
        return type.cast(new AttributeView("basic", toSFTPPath(path))); //$NON-NLS-1$
    }
    if (type == FileOwnerAttributeView.class) {
        return type.cast(new AttributeView("owner", toSFTPPath(path))); //$NON-NLS-1$
    }
    if (type == PosixFileAttributeView.class) {
        return type.cast(new AttributeView("posix", toSFTPPath(path))); //$NON-NLS-1$
    }
    return null;
}
 
Example #5
Source File: HadoopFileSystem.java    From jsr203-hadoop with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
<V extends FileAttributeView> V getView(HadoopPath path, Class<V> type) {
       if (type == null)
           throw new NullPointerException();
       if (type == BasicFileAttributeView.class)
           return (V)new HadoopBasicFileAttributeView(path, false);
       if (type == HadoopBasicFileAttributeView.class)
           return (V)new HadoopBasicFileAttributeView(path, true);
       if (type == FileOwnerAttributeView.class)
           return (V)new HadoopPosixFileAttributeView(path, false);
       if (type == PosixFileAttributeView.class)
           return (V)new HadoopPosixFileAttributeView(path, true);
       return null;
   }
 
Example #6
Source File: CWLParser.java    From cwlexec with Apache License 2.0 5 votes vote down vote up
private static String getFileOwner(File file) {
    String owner = null;
    if (file != null) {
        Path path = Paths.get(file.getAbsolutePath());
        try {
            UserPrincipal user = Files.getFileAttributeView(path, FileOwnerAttributeView.class).getOwner();
            return user.getName();
        } catch (IOException e) {
            logger.warn("Fail to get the owner of {}, ({})", path, e.getMessage());
        }
    }
    return owner;
}
 
Example #7
Source File: TestFiles.java    From jsr203-hadoop with Apache License 2.0 5 votes vote down vote up
@Test
public void getFileAttributeViewFileOwnerAttributeView() throws IOException {
  Path rootPath = Paths.get(clusterUri);
  Path path = Files.createTempFile(rootPath, "test", "tmp");
  FileOwnerAttributeView view = Files.getFileAttributeView(path,
      FileOwnerAttributeView.class);
  Assert.assertNotNull(view);
  Assert.assertEquals("owner", view.name());
}
 
Example #8
Source File: FilePersistenceUtils.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static boolean supportsFileOwnerAttributeView(Path path, Class<? extends FileOwnerAttributeView> view) {
    FileStore store;
    try {
        store = Files.getFileStore(path);
    } catch (IOException e) {
        return false;
    }
    return store.supportsFileAttributeView(view);
}
 
Example #9
Source File: PosixAttributeProvider.java    From jimfs with Apache License 2.0 5 votes vote down vote up
@Override
public PosixFileAttributeView view(
    FileLookup lookup, ImmutableMap<String, FileAttributeView> inheritedViews) {
  return new View(
      lookup,
      (BasicFileAttributeView) inheritedViews.get("basic"),
      (FileOwnerAttributeView) inheritedViews.get("owner"));
}
 
Example #10
Source File: OwnerAttributeProvider.java    From jimfs with Apache License 2.0 4 votes vote down vote up
@Override
public FileOwnerAttributeView view(
    FileLookup lookup, ImmutableMap<String, FileAttributeView> inheritedViews) {
  return new View(lookup);
}
 
Example #11
Source File: PosixAttributeProvider.java    From jimfs with Apache License 2.0 4 votes vote down vote up
protected View(
    FileLookup lookup, BasicFileAttributeView basicView, FileOwnerAttributeView ownerView) {
  super(lookup);
  this.basicView = checkNotNull(basicView);
  this.ownerView = checkNotNull(ownerView);
}
 
Example #12
Source File: AclAttributeProvider.java    From jimfs with Apache License 2.0 4 votes vote down vote up
@Override
public AclFileAttributeView view(
    FileLookup lookup, ImmutableMap<String, FileAttributeView> inheritedViews) {
  return new View(lookup, (FileOwnerAttributeView) inheritedViews.get("owner"));
}
 
Example #13
Source File: FileDataStoreFactory.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
private static void setPermissionsToOwnerOnlyWindows(File file) throws IOException {
  Path path = Paths.get(file.getAbsolutePath());
  FileOwnerAttributeView fileAttributeView =
      Files.getFileAttributeView(path, FileOwnerAttributeView.class);
  UserPrincipal owner = fileAttributeView.getOwner();

  // get view
  AclFileAttributeView view = Files.getFileAttributeView(path, AclFileAttributeView.class);

  // All available entries
  Set<AclEntryPermission> permissions =
      ImmutableSet.of(
          AclEntryPermission.APPEND_DATA,
          AclEntryPermission.DELETE,
          AclEntryPermission.DELETE_CHILD,
          AclEntryPermission.READ_ACL,
          AclEntryPermission.READ_ATTRIBUTES,
          AclEntryPermission.READ_DATA,
          AclEntryPermission.READ_NAMED_ATTRS,
          AclEntryPermission.SYNCHRONIZE,
          AclEntryPermission.WRITE_ACL,
          AclEntryPermission.WRITE_ATTRIBUTES,
          AclEntryPermission.WRITE_DATA,
          AclEntryPermission.WRITE_NAMED_ATTRS,
          AclEntryPermission.WRITE_OWNER);

  // create ACL to give owner everything
  AclEntry entry =
      AclEntry.newBuilder()
          .setType(AclEntryType.ALLOW)
          .setPrincipal(owner)
          .setPermissions(permissions)
          .build();

  // Overwrite the ACL with only this permission
  try {
    view.setAcl(ImmutableList.of(entry));
  } catch (SecurityException ex) {
    throw new IOException("Unable to set permissions for " + file, ex);
  }
}
 
Example #14
Source File: OwnerAttributeProvider.java    From jimfs with Apache License 2.0 4 votes vote down vote up
@Override
public Class<FileOwnerAttributeView> viewType() {
  return FileOwnerAttributeView.class;
}
 
Example #15
Source File: AclAttributeProvider.java    From jimfs with Apache License 2.0 4 votes vote down vote up
public View(FileLookup lookup, FileOwnerAttributeView ownerView) {
  super(lookup);
  this.ownerView = checkNotNull(ownerView);
}
 
Example #16
Source File: Files.java    From openjdk-8-source with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Updates the file owner.
 *
 * <p> The {@code path} parameter is associated with a file system that
 * supports {@link FileOwnerAttributeView}. This file attribute view provides
 * access to a file attribute that is the owner of the file.
 *
 * <p> <b>Usage Example:</b>
 * Suppose we want to make "joe" the owner of a file:
 * <pre>
 *     Path path = ...
 *     UserPrincipalLookupService lookupService =
 *         provider(path).getUserPrincipalLookupService();
 *     UserPrincipal joe = lookupService.lookupPrincipalByName("joe");
 *     Files.setOwner(path, joe);
 * </pre>
 *
 * @param   path
 *          The path to the file
 * @param   owner
 *          The new file owner
 *
 * @return  The path
 *
 * @throws  UnsupportedOperationException
 *          if the associated file system does not support the {@code
 *          FileOwnerAttributeView}
 * @throws  IOException
 *          if an I/O error occurs
 * @throws  SecurityException
 *          In the case of the default provider, and a security manager is
 *          installed, it denies {@link RuntimePermission}<tt>("accessUserInformation")</tt>
 *          or its {@link SecurityManager#checkWrite(String) checkWrite}
 *          method denies write access to the file.
 *
 * @see FileSystem#getUserPrincipalLookupService
 * @see java.nio.file.attribute.UserPrincipalLookupService
 */
public static Path setOwner(Path path, UserPrincipal owner)
    throws IOException
{
    FileOwnerAttributeView view =
        getFileAttributeView(path, FileOwnerAttributeView.class);
    if (view == null)
        throw new UnsupportedOperationException();
    view.setOwner(owner);
    return path;
}
 
Example #17
Source File: Files.java    From hottub with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Updates the file owner.
 *
 * <p> The {@code path} parameter is associated with a file system that
 * supports {@link FileOwnerAttributeView}. This file attribute view provides
 * access to a file attribute that is the owner of the file.
 *
 * <p> <b>Usage Example:</b>
 * Suppose we want to make "joe" the owner of a file:
 * <pre>
 *     Path path = ...
 *     UserPrincipalLookupService lookupService =
 *         provider(path).getUserPrincipalLookupService();
 *     UserPrincipal joe = lookupService.lookupPrincipalByName("joe");
 *     Files.setOwner(path, joe);
 * </pre>
 *
 * @param   path
 *          The path to the file
 * @param   owner
 *          The new file owner
 *
 * @return  The path
 *
 * @throws  UnsupportedOperationException
 *          if the associated file system does not support the {@code
 *          FileOwnerAttributeView}
 * @throws  IOException
 *          if an I/O error occurs
 * @throws  SecurityException
 *          In the case of the default provider, and a security manager is
 *          installed, it denies {@link RuntimePermission}<tt>("accessUserInformation")</tt>
 *          or its {@link SecurityManager#checkWrite(String) checkWrite}
 *          method denies write access to the file.
 *
 * @see FileSystem#getUserPrincipalLookupService
 * @see java.nio.file.attribute.UserPrincipalLookupService
 */
public static Path setOwner(Path path, UserPrincipal owner)
    throws IOException
{
    FileOwnerAttributeView view =
        getFileAttributeView(path, FileOwnerAttributeView.class);
    if (view == null)
        throw new UnsupportedOperationException();
    view.setOwner(owner);
    return path;
}
 
Example #18
Source File: Files.java    From openjdk-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the owner of a file.
 *
 * <p> The {@code path} parameter is associated with a file system that
 * supports {@link FileOwnerAttributeView}. This file attribute view provides
 * access to a file attribute that is the owner of the file.
 *
 * @param   path
 *          The path to the file
 * @param   options
 *          options indicating how symbolic links are handled
 *
 * @return  A user principal representing the owner of the file
 *
 * @throws  UnsupportedOperationException
 *          if the associated file system does not support the {@code
 *          FileOwnerAttributeView}
 * @throws  IOException
 *          if an I/O error occurs
 * @throws  SecurityException
 *          In the case of the default provider, and a security manager is
 *          installed, it denies {@link RuntimePermission}<tt>("accessUserInformation")</tt>
 *          or its {@link SecurityManager#checkRead(String) checkRead} method
 *          denies read access to the file.
 */
public static UserPrincipal getOwner(Path path, LinkOption... options) throws IOException {
    FileOwnerAttributeView view =
        getFileAttributeView(path, FileOwnerAttributeView.class, options);
    if (view == null)
        throw new UnsupportedOperationException();
    return view.getOwner();
}
 
Example #19
Source File: Files.java    From openjdk-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Updates the file owner.
 *
 * <p> The {@code path} parameter is associated with a file system that
 * supports {@link FileOwnerAttributeView}. This file attribute view provides
 * access to a file attribute that is the owner of the file.
 *
 * <p> <b>Usage Example:</b>
 * Suppose we want to make "joe" the owner of a file:
 * <pre>
 *     Path path = ...
 *     UserPrincipalLookupService lookupService =
 *         provider(path).getUserPrincipalLookupService();
 *     UserPrincipal joe = lookupService.lookupPrincipalByName("joe");
 *     Files.setOwner(path, joe);
 * </pre>
 *
 * @param   path
 *          The path to the file
 * @param   owner
 *          The new file owner
 *
 * @return  The path
 *
 * @throws  UnsupportedOperationException
 *          if the associated file system does not support the {@code
 *          FileOwnerAttributeView}
 * @throws  IOException
 *          if an I/O error occurs
 * @throws  SecurityException
 *          In the case of the default provider, and a security manager is
 *          installed, it denies {@link RuntimePermission}<tt>("accessUserInformation")</tt>
 *          or its {@link SecurityManager#checkWrite(String) checkWrite}
 *          method denies write access to the file.
 *
 * @see FileSystem#getUserPrincipalLookupService
 * @see java.nio.file.attribute.UserPrincipalLookupService
 */
public static Path setOwner(Path path, UserPrincipal owner)
    throws IOException
{
    FileOwnerAttributeView view =
        getFileAttributeView(path, FileOwnerAttributeView.class);
    if (view == null)
        throw new UnsupportedOperationException();
    view.setOwner(owner);
    return path;
}
 
Example #20
Source File: Files.java    From jdk8u_jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the owner of a file.
 *
 * <p> The {@code path} parameter is associated with a file system that
 * supports {@link FileOwnerAttributeView}. This file attribute view provides
 * access to a file attribute that is the owner of the file.
 *
 * @param   path
 *          The path to the file
 * @param   options
 *          options indicating how symbolic links are handled
 *
 * @return  A user principal representing the owner of the file
 *
 * @throws  UnsupportedOperationException
 *          if the associated file system does not support the {@code
 *          FileOwnerAttributeView}
 * @throws  IOException
 *          if an I/O error occurs
 * @throws  SecurityException
 *          In the case of the default provider, and a security manager is
 *          installed, it denies {@link RuntimePermission}<tt>("accessUserInformation")</tt>
 *          or its {@link SecurityManager#checkRead(String) checkRead} method
 *          denies read access to the file.
 */
public static UserPrincipal getOwner(Path path, LinkOption... options) throws IOException {
    FileOwnerAttributeView view =
        getFileAttributeView(path, FileOwnerAttributeView.class, options);
    if (view == null)
        throw new UnsupportedOperationException();
    return view.getOwner();
}
 
Example #21
Source File: Files.java    From jdk8u_jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Updates the file owner.
 *
 * <p> The {@code path} parameter is associated with a file system that
 * supports {@link FileOwnerAttributeView}. This file attribute view provides
 * access to a file attribute that is the owner of the file.
 *
 * <p> <b>Usage Example:</b>
 * Suppose we want to make "joe" the owner of a file:
 * <pre>
 *     Path path = ...
 *     UserPrincipalLookupService lookupService =
 *         provider(path).getUserPrincipalLookupService();
 *     UserPrincipal joe = lookupService.lookupPrincipalByName("joe");
 *     Files.setOwner(path, joe);
 * </pre>
 *
 * @param   path
 *          The path to the file
 * @param   owner
 *          The new file owner
 *
 * @return  The path
 *
 * @throws  UnsupportedOperationException
 *          if the associated file system does not support the {@code
 *          FileOwnerAttributeView}
 * @throws  IOException
 *          if an I/O error occurs
 * @throws  SecurityException
 *          In the case of the default provider, and a security manager is
 *          installed, it denies {@link RuntimePermission}<tt>("accessUserInformation")</tt>
 *          or its {@link SecurityManager#checkWrite(String) checkWrite}
 *          method denies write access to the file.
 *
 * @see FileSystem#getUserPrincipalLookupService
 * @see java.nio.file.attribute.UserPrincipalLookupService
 */
public static Path setOwner(Path path, UserPrincipal owner)
    throws IOException
{
    FileOwnerAttributeView view =
        getFileAttributeView(path, FileOwnerAttributeView.class);
    if (view == null)
        throw new UnsupportedOperationException();
    view.setOwner(owner);
    return path;
}
 
Example #22
Source File: Files.java    From jdk8u-jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the owner of a file.
 *
 * <p> The {@code path} parameter is associated with a file system that
 * supports {@link FileOwnerAttributeView}. This file attribute view provides
 * access to a file attribute that is the owner of the file.
 *
 * @param   path
 *          The path to the file
 * @param   options
 *          options indicating how symbolic links are handled
 *
 * @return  A user principal representing the owner of the file
 *
 * @throws  UnsupportedOperationException
 *          if the associated file system does not support the {@code
 *          FileOwnerAttributeView}
 * @throws  IOException
 *          if an I/O error occurs
 * @throws  SecurityException
 *          In the case of the default provider, and a security manager is
 *          installed, it denies {@link RuntimePermission}<tt>("accessUserInformation")</tt>
 *          or its {@link SecurityManager#checkRead(String) checkRead} method
 *          denies read access to the file.
 */
public static UserPrincipal getOwner(Path path, LinkOption... options) throws IOException {
    FileOwnerAttributeView view =
        getFileAttributeView(path, FileOwnerAttributeView.class, options);
    if (view == null)
        throw new UnsupportedOperationException();
    return view.getOwner();
}
 
Example #23
Source File: Files.java    From jdk8u-jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Updates the file owner.
 *
 * <p> The {@code path} parameter is associated with a file system that
 * supports {@link FileOwnerAttributeView}. This file attribute view provides
 * access to a file attribute that is the owner of the file.
 *
 * <p> <b>Usage Example:</b>
 * Suppose we want to make "joe" the owner of a file:
 * <pre>
 *     Path path = ...
 *     UserPrincipalLookupService lookupService =
 *         provider(path).getUserPrincipalLookupService();
 *     UserPrincipal joe = lookupService.lookupPrincipalByName("joe");
 *     Files.setOwner(path, joe);
 * </pre>
 *
 * @param   path
 *          The path to the file
 * @param   owner
 *          The new file owner
 *
 * @return  The path
 *
 * @throws  UnsupportedOperationException
 *          if the associated file system does not support the {@code
 *          FileOwnerAttributeView}
 * @throws  IOException
 *          if an I/O error occurs
 * @throws  SecurityException
 *          In the case of the default provider, and a security manager is
 *          installed, it denies {@link RuntimePermission}<tt>("accessUserInformation")</tt>
 *          or its {@link SecurityManager#checkWrite(String) checkWrite}
 *          method denies write access to the file.
 *
 * @see FileSystem#getUserPrincipalLookupService
 * @see java.nio.file.attribute.UserPrincipalLookupService
 */
public static Path setOwner(Path path, UserPrincipal owner)
    throws IOException
{
    FileOwnerAttributeView view =
        getFileAttributeView(path, FileOwnerAttributeView.class);
    if (view == null)
        throw new UnsupportedOperationException();
    view.setOwner(owner);
    return path;
}
 
Example #24
Source File: Files.java    From jdk8u-dev-jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the owner of a file.
 *
 * <p> The {@code path} parameter is associated with a file system that
 * supports {@link FileOwnerAttributeView}. This file attribute view provides
 * access to a file attribute that is the owner of the file.
 *
 * @param   path
 *          The path to the file
 * @param   options
 *          options indicating how symbolic links are handled
 *
 * @return  A user principal representing the owner of the file
 *
 * @throws  UnsupportedOperationException
 *          if the associated file system does not support the {@code
 *          FileOwnerAttributeView}
 * @throws  IOException
 *          if an I/O error occurs
 * @throws  SecurityException
 *          In the case of the default provider, and a security manager is
 *          installed, it denies {@link RuntimePermission}<tt>("accessUserInformation")</tt>
 *          or its {@link SecurityManager#checkRead(String) checkRead} method
 *          denies read access to the file.
 */
public static UserPrincipal getOwner(Path path, LinkOption... options) throws IOException {
    FileOwnerAttributeView view =
        getFileAttributeView(path, FileOwnerAttributeView.class, options);
    if (view == null)
        throw new UnsupportedOperationException();
    return view.getOwner();
}
 
Example #25
Source File: Files.java    From jdk8u-dev-jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Updates the file owner.
 *
 * <p> The {@code path} parameter is associated with a file system that
 * supports {@link FileOwnerAttributeView}. This file attribute view provides
 * access to a file attribute that is the owner of the file.
 *
 * <p> <b>Usage Example:</b>
 * Suppose we want to make "joe" the owner of a file:
 * <pre>
 *     Path path = ...
 *     UserPrincipalLookupService lookupService =
 *         provider(path).getUserPrincipalLookupService();
 *     UserPrincipal joe = lookupService.lookupPrincipalByName("joe");
 *     Files.setOwner(path, joe);
 * </pre>
 *
 * @param   path
 *          The path to the file
 * @param   owner
 *          The new file owner
 *
 * @return  The path
 *
 * @throws  UnsupportedOperationException
 *          if the associated file system does not support the {@code
 *          FileOwnerAttributeView}
 * @throws  IOException
 *          if an I/O error occurs
 * @throws  SecurityException
 *          In the case of the default provider, and a security manager is
 *          installed, it denies {@link RuntimePermission}<tt>("accessUserInformation")</tt>
 *          or its {@link SecurityManager#checkWrite(String) checkWrite}
 *          method denies write access to the file.
 *
 * @see FileSystem#getUserPrincipalLookupService
 * @see java.nio.file.attribute.UserPrincipalLookupService
 */
public static Path setOwner(Path path, UserPrincipal owner)
    throws IOException
{
    FileOwnerAttributeView view =
        getFileAttributeView(path, FileOwnerAttributeView.class);
    if (view == null)
        throw new UnsupportedOperationException();
    view.setOwner(owner);
    return path;
}
 
Example #26
Source File: Files.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the owner of a file.
 *
 * <p> The {@code path} parameter is associated with a file system that
 * supports {@link FileOwnerAttributeView}. This file attribute view provides
 * access to a file attribute that is the owner of the file.
 *
 * @param   path
 *          The path to the file
 * @param   options
 *          options indicating how symbolic links are handled
 *
 * @return  A user principal representing the owner of the file
 *
 * @throws  UnsupportedOperationException
 *          if the associated file system does not support the {@code
 *          FileOwnerAttributeView}
 * @throws  IOException
 *          if an I/O error occurs
 * @throws  SecurityException
 *          In the case of the default provider, and a security manager is
 *          installed, it denies {@link RuntimePermission}<tt>("accessUserInformation")</tt>
 *          or its {@link SecurityManager#checkRead(String) checkRead} method
 *          denies read access to the file.
 */
public static UserPrincipal getOwner(Path path, LinkOption... options) throws IOException {
    FileOwnerAttributeView view =
        getFileAttributeView(path, FileOwnerAttributeView.class, options);
    if (view == null)
        throw new UnsupportedOperationException();
    return view.getOwner();
}
 
Example #27
Source File: Files.java    From jdk1.8-source-analysis with Apache License 2.0 3 votes vote down vote up
/**
 * Updates the file owner.
 *
 * <p> The {@code path} parameter is associated with a file system that
 * supports {@link FileOwnerAttributeView}. This file attribute view provides
 * access to a file attribute that is the owner of the file.
 *
 * <p> <b>Usage Example:</b>
 * Suppose we want to make "joe" the owner of a file:
 * <pre>
 *     Path path = ...
 *     UserPrincipalLookupService lookupService =
 *         provider(path).getUserPrincipalLookupService();
 *     UserPrincipal joe = lookupService.lookupPrincipalByName("joe");
 *     Files.setOwner(path, joe);
 * </pre>
 *
 * @param   path
 *          The path to the file
 * @param   owner
 *          The new file owner
 *
 * @return  The path
 *
 * @throws  UnsupportedOperationException
 *          if the associated file system does not support the {@code
 *          FileOwnerAttributeView}
 * @throws  IOException
 *          if an I/O error occurs
 * @throws  SecurityException
 *          In the case of the default provider, and a security manager is
 *          installed, it denies {@link RuntimePermission}<tt>("accessUserInformation")</tt>
 *          or its {@link SecurityManager#checkWrite(String) checkWrite}
 *          method denies write access to the file.
 *
 * @see FileSystem#getUserPrincipalLookupService
 * @see java.nio.file.attribute.UserPrincipalLookupService
 */
public static Path setOwner(Path path, UserPrincipal owner)
    throws IOException
{
    FileOwnerAttributeView view =
        getFileAttributeView(path, FileOwnerAttributeView.class);
    if (view == null)
        throw new UnsupportedOperationException();
    view.setOwner(owner);
    return path;
}
 
Example #28
Source File: Files.java    From dragonwell8_jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the owner of a file.
 *
 * <p> The {@code path} parameter is associated with a file system that
 * supports {@link FileOwnerAttributeView}. This file attribute view provides
 * access to a file attribute that is the owner of the file.
 *
 * @param   path
 *          The path to the file
 * @param   options
 *          options indicating how symbolic links are handled
 *
 * @return  A user principal representing the owner of the file
 *
 * @throws  UnsupportedOperationException
 *          if the associated file system does not support the {@code
 *          FileOwnerAttributeView}
 * @throws  IOException
 *          if an I/O error occurs
 * @throws  SecurityException
 *          In the case of the default provider, and a security manager is
 *          installed, it denies {@link RuntimePermission}<tt>("accessUserInformation")</tt>
 *          or its {@link SecurityManager#checkRead(String) checkRead} method
 *          denies read access to the file.
 */
public static UserPrincipal getOwner(Path path, LinkOption... options) throws IOException {
    FileOwnerAttributeView view =
        getFileAttributeView(path, FileOwnerAttributeView.class, options);
    if (view == null)
        throw new UnsupportedOperationException();
    return view.getOwner();
}
 
Example #29
Source File: Files.java    From dragonwell8_jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Updates the file owner.
 *
 * <p> The {@code path} parameter is associated with a file system that
 * supports {@link FileOwnerAttributeView}. This file attribute view provides
 * access to a file attribute that is the owner of the file.
 *
 * <p> <b>Usage Example:</b>
 * Suppose we want to make "joe" the owner of a file:
 * <pre>
 *     Path path = ...
 *     UserPrincipalLookupService lookupService =
 *         provider(path).getUserPrincipalLookupService();
 *     UserPrincipal joe = lookupService.lookupPrincipalByName("joe");
 *     Files.setOwner(path, joe);
 * </pre>
 *
 * @param   path
 *          The path to the file
 * @param   owner
 *          The new file owner
 *
 * @return  The path
 *
 * @throws  UnsupportedOperationException
 *          if the associated file system does not support the {@code
 *          FileOwnerAttributeView}
 * @throws  IOException
 *          if an I/O error occurs
 * @throws  SecurityException
 *          In the case of the default provider, and a security manager is
 *          installed, it denies {@link RuntimePermission}<tt>("accessUserInformation")</tt>
 *          or its {@link SecurityManager#checkWrite(String) checkWrite}
 *          method denies write access to the file.
 *
 * @see FileSystem#getUserPrincipalLookupService
 * @see java.nio.file.attribute.UserPrincipalLookupService
 */
public static Path setOwner(Path path, UserPrincipal owner)
    throws IOException
{
    FileOwnerAttributeView view =
        getFileAttributeView(path, FileOwnerAttributeView.class);
    if (view == null)
        throw new UnsupportedOperationException();
    view.setOwner(owner);
    return path;
}
 
Example #30
Source File: Files.java    From TencentKona-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the owner of a file.
 *
 * <p> The {@code path} parameter is associated with a file system that
 * supports {@link FileOwnerAttributeView}. This file attribute view provides
 * access to a file attribute that is the owner of the file.
 *
 * @param   path
 *          The path to the file
 * @param   options
 *          options indicating how symbolic links are handled
 *
 * @return  A user principal representing the owner of the file
 *
 * @throws  UnsupportedOperationException
 *          if the associated file system does not support the {@code
 *          FileOwnerAttributeView}
 * @throws  IOException
 *          if an I/O error occurs
 * @throws  SecurityException
 *          In the case of the default provider, and a security manager is
 *          installed, it denies {@link RuntimePermission}<tt>("accessUserInformation")</tt>
 *          or its {@link SecurityManager#checkRead(String) checkRead} method
 *          denies read access to the file.
 */
public static UserPrincipal getOwner(Path path, LinkOption... options) throws IOException {
    FileOwnerAttributeView view =
        getFileAttributeView(path, FileOwnerAttributeView.class, options);
    if (view == null)
        throw new UnsupportedOperationException();
    return view.getOwner();
}