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); }
@Test public void testSetGetSystem() throws Exception { for(Path path : Arrays.asList( Files.createDirectory(root.resolve("dir")), Files.createFile(root.resolve("file")))) { DosFileAttributes attributes = Files.readAttributes(path, DosFileAttributes.class); assertFalse(attributes.isSystem()); DosFileAttributeView view = Files.getFileAttributeView(path, DosFileAttributeView.class); view.setSystem(true); attributes = Files.readAttributes(path, DosFileAttributes.class); assertTrue(attributes.isSystem()); } }
@IgnoreIf(FsType.WINDOWS) @Test public void testSetGetArchiveUnix() throws Exception { for(Path path : Arrays.asList( Files.createDirectory(root.resolve("dir")), Files.createFile(root.resolve("file")))) { DosFileAttributes attributes = Files.readAttributes(path, DosFileAttributes.class); assertFalse(attributes.isArchive()); DosFileAttributeView view = Files.getFileAttributeView(path, DosFileAttributeView.class); view.setArchive(true); attributes = Files.readAttributes(path, DosFileAttributes.class); assertTrue(attributes.isArchive()); } }
@Test public void testSetGetReadOnly() throws Exception { for(Path path : Arrays.asList( Files.createDirectory(root.resolve("dir")), Files.createFile(root.resolve("file")))) { DosFileAttributes attributes = Files.readAttributes(path, DosFileAttributes.class); assertFalse(attributes.isReadOnly()); DosFileAttributeView view = Files.getFileAttributeView(path, DosFileAttributeView.class); try { view.setReadOnly(true); attributes = Files.readAttributes(path, DosFileAttributes.class); assertTrue(attributes.isReadOnly()); } finally { view.setReadOnly(false); } } }
@Test public void testSetGetHidden() throws Exception { for(Path path : Arrays.asList( Files.createDirectory(root.resolve("dir")), Files.createFile(root.resolve("file")))) { DosFileAttributes attributes = Files.readAttributes(path, DosFileAttributes.class); assertFalse(attributes.isHidden()); DosFileAttributeView view = Files.getFileAttributeView(path, DosFileAttributeView.class); view.setHidden(true); attributes = Files.readAttributes(path, DosFileAttributes.class); assertTrue(attributes.isHidden()); } }
private void tryDos(Path p, boolean failIfDosIsNotSupported) { log("Falling back to DosFileAttributeView"); boolean readOnly = !isWritable(); DosFileAttributeView view = Files.getFileAttributeView(p, DosFileAttributeView.class); if (view != null) { try { view.setReadOnly(readOnly); } catch (IOException ioe) { maybeThrowException(ioe, "Failed to set permissions on '%s' due to %s", p, ioe.getMessage()); } catch (SecurityException uoe) { maybeThrowException(null, "the SecurityManager denies role accessUserInformation or write access for SecurityManager.checkWrite for resource '%s'", p); } } else { String msg = String.format( "the associated path '%s' does not support the DosFileAttributeView", p); if (failIfDosIsNotSupported) { throw new BuildException(msg); } log("Warning: " + msg, Project.MSG_ERR); } }
/** * @param path * @return */ public static String getDosAttributesString(Path path) { DosFileAttributeView basicView = Files.getFileAttributeView(path, DosFileAttributeView.class, LinkOption.NOFOLLOW_LINKS); StringBuilder attrs = new StringBuilder(); try { // + all basic attributes DosFileAttributes dosAttrs = basicView.readAttributes(); attrs.append(dosAttrs.isReadOnly() ? "r" : "-"); //$NON-NLS-1$ //$NON-NLS-2$ attrs.append(dosAttrs.isHidden() ? "h" : "-");//$NON-NLS-1$ //$NON-NLS-2$ attrs.append(dosAttrs.isArchive() ? "a" : "-");//$NON-NLS-1$ //$NON-NLS-2$ attrs.append(dosAttrs.isSystem() ? "s" : "-");//$NON-NLS-1$ //$NON-NLS-2$ } catch (IOException e) { log.warn("unable to read DOS attributes.", e); //$NON-NLS-1$ } return attrs.toString(); }
/** * Test: File and FileStore attributes */ @Test public void testFileStoreAttributes() throws URISyntaxException, IOException { URI uri = clusterUri.resolve("/tmp/testFileStore"); Path path = Paths.get(uri); if (Files.exists(path)) Files.delete(path); assertFalse(Files.exists(path)); Files.createFile(path); assertTrue(Files.exists(path)); FileStore store1 = Files.getFileStore(path); assertNotNull(store1); assertTrue(store1.supportsFileAttributeView("basic")); assertTrue(store1.supportsFileAttributeView(BasicFileAttributeView.class)); assertTrue(store1.supportsFileAttributeView("posix") == store1 .supportsFileAttributeView(PosixFileAttributeView.class)); assertTrue(store1.supportsFileAttributeView("dos") == store1 .supportsFileAttributeView(DosFileAttributeView.class)); assertTrue(store1.supportsFileAttributeView("acl") == store1 .supportsFileAttributeView(AclFileAttributeView.class)); assertTrue(store1.supportsFileAttributeView("user") == store1 .supportsFileAttributeView(UserDefinedFileAttributeView.class)); }
private void makeWritable(Path file) throws IOException { FileStore fileStore = Files.getFileStore(file); if (IS_WINDOWS && fileStore.supportsFileAttributeView(DosFileAttributeView.class)) { DosFileAttributeView dosAttribs = Files.getFileAttributeView(file, DosFileAttributeView.class); if (dosAttribs != null) { dosAttribs.setReadOnly(false); } } else if (fileStore.supportsFileAttributeView(PosixFileAttributeView.class)) { PosixFileAttributeView posixAttribs = Files.getFileAttributeView(file, PosixFileAttributeView.class); if (posixAttribs != null) { posixAttribs.setPermissions(EnumSet.of(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE)); } } }
public static void removeReadOnly(Path path) throws IOException { DosFileAttributeView att = Files.getFileAttributeView(path, DosFileAttributeView.class); if (att != null) { att.setReadOnly(false); } Files.delete(path); }
public static void setPermissions(Context context, Path file, String permissions) { DosFileAttributeView fileAttributeView = Files.getFileAttributeView(file, DosFileAttributeView.class); try { fileAttributeView.setArchive(permissions.contains("A")); fileAttributeView.setHidden(permissions.contains("H")); fileAttributeView.setReadOnly(permissions.contains("R")); fileAttributeView.setSystem(permissions.contains("S")); } catch (IOException ex) { Logger.error("Error setting permissions for '" + file + "'", ex, context.isDisplayStackTrace()); } }
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"); } }
private Class<? extends BasicFileAttributeView> viewClassFor( Class<? extends BasicFileAttributes> attributeClass) { if (attributeClass == BasicFileAttributes.class) { return BasicFileAttributeView.class; } else if (attributeClass == PosixFileAttributes.class) { return PosixFileAttributeView.class; } else if (attributeClass == DosFileAttributes.class) { return DosFileAttributeView.class; } else { throw new IllegalArgumentException("unrecognized view class:" + attributeClass); } }
@IgnoreIf(FsType.WINDOWS) @Test public void testReadOnlyCanStillWriteNotWindows() throws Exception { Path path = Files.createFile(root.resolve("file")); DosFileAttributeView view = Files.getFileAttributeView(path, DosFileAttributeView.class); view.setReadOnly(true); Files.write(path, new byte[] {1}); }
@IgnoreUnless(FsType.WINDOWS) @Test public void testReadOnlyCanStillWriteWindowsDir() throws Exception { Path path = Files.createDirectory(root.resolve("dir")); DosFileAttributeView view = Files.getFileAttributeView(path, DosFileAttributeView.class); view.setReadOnly(true); try { Files.delete(path); fail(); } catch(AccessDeniedException e) { //pass } Files.move(path, root.resolve("moveTo")); }
@IgnoreUnless(FsType.WINDOWS) @Test public void testSupportsFileStoreWin() throws Exception { for(FileStore fs : root.getFileSystem().getFileStores()) { assertTrue(fs.supportsFileAttributeView(BasicFileAttributeView.class)); assertFalse(fs.supportsFileAttributeView(PosixFileAttributeView.class)); assertTrue(fs.supportsFileAttributeView(DosFileAttributeView.class)); assertTrue(fs.supportsFileAttributeView("basic")); assertTrue(fs.supportsFileAttributeView("dos")); } }
@Override public boolean isHidden( @Nullable Path path ) throws IOException { EightyPath ep = checkProvider( path ); AttributeProvider attributeProvider = ep._getFileSystem().getAttributeProvider(); if( attributeProvider.isViewSupported( DosFileAttributeView.class ) ) { return _nn( readAttributes( path, DosFileAttributes.class ) ).isHidden(); } @Nullable EightyPath fname = ep.getFileName(); return fname != null && fname.toString().startsWith( "." ); }
public RWAttributesBuilder addDos() { attributes( "dos", DosFileAttributeView.class, DosFileAttributes.class ). attribute( "hidden", DosFileAttributes::isHidden, BiConsumerE.u( ( v, b ) -> v.setHidden( (Boolean) b ) ) ). attribute( "readonly", DosFileAttributes::isReadOnly ). attribute( "system", DosFileAttributes::isSystem ). attribute( "archive", DosFileAttributes::isArchive ); return this; }
@Test @Category( { Windows.class, DosAttributesT.class, Writable.class } ) public void testWindowsIsHidden() throws IOException { assertThat( Files.isHidden( fileTA() ) ).isFalse(); Files.getFileAttributeView( absTA(), DosFileAttributeView.class ).setHidden( true ); assertThat( Files.isHidden( fileTA() ) ).isTrue(); Files.setAttribute( absTA(), "dos:hidden", false ); assertThat( Files.isHidden( fileTA() ) ).isFalse(); }
public WindowsBuilder( FSDescription descr, T t ) { super( descr, t ); descr.removeTopic( Unix.class ); PathLimits pathLimits = new PathLimits( OS.WINDOWS ); PathSpec pathSpec = new PathSpecWindows(); descr.props.put( Tests10PathWithContent.ONE_CHAR_COUNT, pathLimits.getBigChar() ); descr.props.put( Tests10PathWithContent.MAX_FILENAME_LENGTH, pathSpec.getMaxFilenameLength() ); descr.props.put( Tests10PathWithContent.MAX_PATH_LENGTH, pathSpec.getMaxPathLength() ); descr.props.put( Tests10PathWithContent.GET_FILENAME_LENGTH, (Function<String,Integer>)pathSpec::getFilenameLength ); descr.props.put( Tests10PathWithContent.GET_PATH_LENGTH, (Function<String,Integer>)pathSpec::getPathLength ); descr.removeTopic( Posix.class ); descr.removeTopic( MoveWhile.class ); descr.removeTopic( NonCasePreserving.class ); descr.removeTopic( FileKeyT.class ); descr.attributeDescriptions.put( "dos", attributeBuilding( DosAttributesT.class, "dos", DosFileAttributeView.class, DosFileAttributes.class ). addAttribute( "hidden", DosFileAttributes::isHidden ). addAttribute( "archive", DosFileAttributes::isArchive ). addAttribute( "system", DosFileAttributes::isSystem ). addAttribute( "readonly", DosFileAttributes::isReadOnly ). build() ); }
@Test public void testView() throws IOException { DosFileAttributeView view = provider.view( fileLookup(), ImmutableMap.<String, FileAttributeView>of( "basic", new BasicAttributeProvider().view(fileLookup(), NO_INHERITED_VIEWS))); assertNotNull(view); assertThat(view.name()).isEqualTo("dos"); DosFileAttributes attrs = view.readAttributes(); assertThat(attrs.isHidden()).isFalse(); assertThat(attrs.isArchive()).isFalse(); assertThat(attrs.isReadOnly()).isFalse(); assertThat(attrs.isSystem()).isFalse(); view.setArchive(true); view.setReadOnly(true); view.setHidden(true); view.setSystem(false); assertThat(attrs.isHidden()).isFalse(); assertThat(attrs.isArchive()).isFalse(); assertThat(attrs.isReadOnly()).isFalse(); attrs = view.readAttributes(); assertThat(attrs.isHidden()).isTrue(); assertThat(attrs.isArchive()).isTrue(); assertThat(attrs.isReadOnly()).isTrue(); assertThat(attrs.isSystem()).isFalse(); view.setTimes(FileTime.fromMillis(0L), null, null); assertThat(view.readAttributes().lastModifiedTime()).isEqualTo(FileTime.fromMillis(0L)); }
private Stat statPath(Path p, long inodeNumber) throws IOException { Class<? extends BasicFileAttributeView> attributeClass = IS_UNIX ? PosixFileAttributeView.class : DosFileAttributeView.class; BasicFileAttributes attrs = Files.getFileAttributeView(p, attributeClass, NOFOLLOW_LINKS).readAttributes(); Stat stat = new Stat(); stat.setATime(attrs.lastAccessTime().toMillis()); stat.setCTime(attrs.creationTime().toMillis()); stat.setMTime(attrs.lastModifiedTime().toMillis()); if (IS_UNIX) { stat.setGid((Integer) Files.getAttribute(p, "unix:gid", NOFOLLOW_LINKS)); stat.setUid((Integer) Files.getAttribute(p, "unix:uid", NOFOLLOW_LINKS)); stat.setMode((Integer) Files.getAttribute(p, "unix:mode", NOFOLLOW_LINKS)); stat.setNlink((Integer) Files.getAttribute(p, "unix:nlink", NOFOLLOW_LINKS)); } else { DosFileAttributes dosAttrs = (DosFileAttributes)attrs; stat.setGid(0); stat.setUid(0); int type = dosAttrs.isDirectory() ? Stat.S_IFDIR : Stat.S_IFREG; stat.setMode( type |(dosAttrs.isReadOnly()? 0600 : 0400)); stat.setNlink(1); } stat.setDev(17); stat.setIno((int) inodeNumber); stat.setRdev(17); stat.setSize(attrs.size()); stat.setFileid((int) inodeNumber); stat.setGeneration(attrs.lastModifiedTime().toMillis()); return stat; }
private static void setHidden(File f, boolean value) throws IOException { Files.getFileAttributeView(f.toPath(), DosFileAttributeView.class).setHidden(value); }
@IgnoreUnless(FsType.WINDOWS) @Test public void testSetGetArchiveWindowsFile() throws Exception { Path path = Files.createFile(root.resolve("file")); DosFileAttributes attributes = Files.readAttributes(path, DosFileAttributes.class); assertTrue(attributes.isArchive()); DosFileAttributeView view = Files.getFileAttributeView(path, DosFileAttributeView.class); view.setArchive(false); attributes = Files.readAttributes(path, DosFileAttributes.class); assertFalse(attributes.isArchive()); Files.write(path, new byte[] {1}); attributes = Files.readAttributes(path, DosFileAttributes.class); assertTrue(attributes.isArchive()); }
@IgnoreUnless(FsType.WINDOWS) @Test public void testSetGetArchiveWindowsDir() throws Exception { Path path = Files.createDirectory(root.resolve("dir")); DosFileAttributes attributes = Files.readAttributes(path, DosFileAttributes.class); assertFalse(attributes.isArchive()); DosFileAttributeView view = Files.getFileAttributeView(path, DosFileAttributeView.class); view.setArchive(true); attributes = Files.readAttributes(path, DosFileAttributes.class); assertTrue(attributes.isArchive()); }
private void clearReadOnly(Path path) throws IOException { DosFileAttributeView view = Files.getFileAttributeView(path, DosFileAttributeView.class); view.setReadOnly(false); }
public static MemoryFSBuilder windows() { return new MemoryFSBuilder(). pathSpec( new PathSpecWindows() ). primaryStoreName( "c:"). attributeView( DosFileAttributeView.class ); }
public static EightyFS create( Object s, RWAttributesBuilder ab, Map<String, Object> env ) { List<?> attributeViews = (List<?>) env.getOrDefault( ATTRIBUTE_VIEWS, Collections.emptyList() ); ab.addOwner(); if ( attributeViews.contains( PosixFileAttributeView.class )) { ab.addPosix(); } if ( attributeViews.contains( DosFileAttributeView.class )) { ab.addDos(); } return new MemoryFS( s, env ); }