private Set<String> getSupportedFileAttributes(FileStore fs) { Set<String> attrs = new HashSet<String>(); if (fs.supportsFileAttributeView(AclFileAttributeView.class)) { attrs.add("acl"); } if (fs.supportsFileAttributeView(BasicFileAttributeView.class)) { attrs.add("basic"); } if (fs.supportsFileAttributeView(FileOwnerAttributeView.class)) { attrs.add("owner"); } if (fs.supportsFileAttributeView(UserDefinedFileAttributeView.class)) { attrs.add("user"); } if (fs.supportsFileAttributeView(DosFileAttributeView.class)) { attrs.add("dos"); } if (fs.supportsFileAttributeView(PosixFileAttributeView.class)) { attrs.add("posix"); } if (fs.supportsFileAttributeView(FileAttributeView.class)) { attrs.add("file"); } return attrs; }
public boolean supportsFileAttributeView( Class<? extends FileAttributeView> type) { String name = "notFound"; if(type == BasicFileAttributeView.class) { name = "basic"; } else if(type == DosFileAttributeView.class) { name = "dos"; } else if(type == PosixFileAttributeView.class) { name = "posix"; } else if(type == FileOwnerAttributeView.class) { name = "owner"; } return attributeSets.containsKey(name); }
private String getOwnerName(File file) { String directory = file.getAbsolutePath(); Path path = Paths.get(directory); FileOwnerAttributeView ownerAttributeView = Files.getFileAttributeView(path, FileOwnerAttributeView.class); UserPrincipal owner = null; try { owner = ownerAttributeView.getOwner(); } catch (IOException e) { throw new PersonifilerException(e); } return owner.getName(); }
public <V extends FileAttributeView> V build(Class<V> type) { if (type == BasicFileAttributeView.class) { return (V) new EphemeralFsBasicFileAttributesView(); } else if (type == PosixFileAttributeView.class) { return (V) new EphemeralFsPosixFileAttributesView(); } else if (type == DosFileAttributeView.class) { return (V) new EphemeralFsDosFileAttributesView(); } else if (type == FileOwnerAttributeView.class) { return (V) new EphemeralFsFileOwnerAttributeView(); } else { throw new UnsupportedOperationException("type:" + type + " is not supported"); } }
@Test public void testReadOwner() throws Exception { for(Path path : Arrays.asList( Files.createDirectory(root.resolve("dir")), Files.createFile(root.resolve("file")))) { FileOwnerAttributeView view = Files.getFileAttributeView(path, FileOwnerAttributeView.class); assertNotNull(view.getOwner()); view.setOwner(view.getOwner()); UserPrincipal owner = (UserPrincipal) Files.getAttribute(path, "owner:owner"); assertNotNull(owner); } }
public RWAttributesBuilder addOwner() { attributes( "owner", FileOwnerAttributeView.class ). attribute( "owner", FunctionE.u( FileOwnerAttributeView::getOwner ), BiConsumerE.u( ( v, p ) -> v.setOwner( (UserPrincipal) p ) ) ); return this; }
public static EightyFS roCreate( Object hostRoot, RWAttributesBuilder ab, Map<String, Object> env ) { ab.attributes( "owner", FileOwnerAttributeView.class ). attribute( "owner", FunctionE.u( FileOwnerAttributeView::getOwner )); ab.addPosix(); return new ReadonlyFS((Path) hostRoot); }
@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")); }
public FileOwnerAttributeView ownerHost() { return (FileOwnerAttributeView)host; }
@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}{@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(); }
/** * 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 given 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}{@code ("accessUserInformation")} * 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; }