下面列出了怎么用java.nio.file.attribute.FileOwnerAttributeView的API类实例代码及写法,或者点击链接到github查看源代码。
/** 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);
}
}
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;
}
public static String userForDir(Path pathToDir) {
try {
FileOwnerAttributeView ownerAttributeView = Files.getFileAttributeView(pathToDir, FileOwnerAttributeView.class);
return ownerAttributeView.getOwner().getName();
} catch (IOException e) {
return "N/A";
}
}
/**
* 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;
}
@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;
}
@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());
}
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);
}
@Override
public PosixFileAttributeView view(
FileLookup lookup, ImmutableMap<String, FileAttributeView> inheritedViews) {
return new View(
lookup,
(BasicFileAttributeView) inheritedViews.get("basic"),
(FileOwnerAttributeView) inheritedViews.get("owner"));
}
@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"));
}
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);
}
}
@Override
public Class<FileOwnerAttributeView> viewType() {
return FileOwnerAttributeView.class;
}
@Override
public FileOwnerAttributeView view(
FileLookup lookup, ImmutableMap<String, FileAttributeView> inheritedViews) {
return new View(lookup);
}
protected View(
FileLookup lookup, BasicFileAttributeView basicView, FileOwnerAttributeView ownerView) {
super(lookup);
this.basicView = checkNotNull(basicView);
this.ownerView = checkNotNull(ownerView);
}
@Override
public AclFileAttributeView view(
FileLookup lookup, ImmutableMap<String, FileAttributeView> inheritedViews) {
return new View(lookup, (FileOwnerAttributeView) inheritedViews.get("owner"));
}
public View(FileLookup lookup, FileOwnerAttributeView ownerView) {
super(lookup);
this.ownerView = checkNotNull(ownerView);
}
/**
* 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();
}
/**
* 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;
}
/**
* 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();
}
/**
* 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;
}
/**
* 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();
}
/**
* 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;
}
/**
* 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();
}
/**
* 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;
}
/**
* 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();
}
/**
* 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;
}
/**
* 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();
}
/**
* 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;
}
/**
* 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();
}
/**
* 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;
}
/**
* 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}{@code ("accessUserInformation")}
* 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();
}