public static String toString(DosFileAttributes dosFileAttributes) { StringBuilder sb = new StringBuilder(); sb.append(dosFileAttributes.isReadOnly() ? "r" : "-"); sb.append(dosFileAttributes.isHidden() ? "h" : "-"); sb.append(dosFileAttributes.isArchive() ? "a" : "-"); sb.append(dosFileAttributes.isSystem() ? "s" : "-"); return sb.toString(); }
Path file = ...; try { DosFileAttributes attr = Attributes.readDosFileAttributes(file); System.out.println("isReadOnly is " + attr.isReadOnly()); System.out.println("isHidden is " + attr.isHidden()); System.out.println("isArchive is " + attr.isArchive()); System.out.println("isSystem is " + attr.isSystem()); } catch (IOException x) { System.err.println("DOS file attributes not supported:" + x); }
Files.setAttribute(filePath, "dos:readonly", dosAttrs.isReadOnly()); Files.setAttribute(filePath, "dos:hidden", dosAttrs.isHidden()); Files.setAttribute(filePath, "dos:archive", dosAttrs.isArchive());
@Test public void testFileDosAttrs() throws Exception { DosFileAttributes dosAttrsRHAS = FileUtil.dosAttrsFromString("rhas"); assertTrue(dosAttrsRHAS.isReadOnly()); assertTrue(dosAttrsRHAS.isHidden()); assertTrue(dosAttrsRHAS.isArchive()); assertTrue(dosAttrsRHA.isReadOnly()); assertTrue(dosAttrsRHA.isHidden()); assertTrue(dosAttrsRHA.isArchive()); assertTrue(dosAttrsRH.isReadOnly()); assertTrue(dosAttrsRH.isHidden()); assertFalse(dosAttrsRH.isArchive()); assertTrue(dosAttrsR.isReadOnly()); assertFalse(dosAttrsR.isHidden()); assertFalse(dosAttrsR.isArchive()); assertFalse(dosAttrsNone.isReadOnly()); assertFalse(dosAttrsNone.isHidden()); assertFalse(dosAttrsNone.isArchive()); assertFalse(dosAttrsH.isReadOnly()); assertTrue(dosAttrsH.isHidden()); assertFalse(dosAttrsH.isArchive()); assertFalse(dosAttrsCorrect.isReadOnly()); assertFalse(dosAttrsCorrect.isHidden()); assertFalse(dosAttrsCorrect.isArchive());
private boolean isReadOnlyFile(Path file) { if(Platform.isWindows()) { if (!file.toFile().canWrite()) { DosFileAttributes dosAttr; try { dosAttr = Files.readAttributes(file, DosFileAttributes.class); if(dosAttr.isReadOnly()) { return true; } } catch (IOException e) { } } } return false; }
@Override public Object readAttribute(AttributeReadContext context) throws IOException { return context.getDosFileAttributes().isReadOnly(); }
static int attributes_to_mode(DosFileAttributes attr) { int m = 0; if (attr.isDirectory()) { m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ } else { m |= _S_IFREG; } if (attr.isReadOnly()) { m |= 0444; } else { m |= 0666; } return m; }
public static boolean canWrite(String path) { if (SystemInfo.isWindows) { try { DosFileAttributes attributes = Files.readAttributes(Paths.get(path), DosFileAttributes.class, LinkOption.NOFOLLOW_LINKS); return attributes.isDirectory() || !attributes.isReadOnly(); } catch (IOException e) { ExceptionUtil.throwRuntime(e); } } return new File(path).canWrite(); }
public static void checkReadOnly(File file) throws IOException { // This logic check if the file attribute was mark as read only because File#setReadOnly is platform dependent (JDK-6728842) Path repositoryPath = file.toPath(); FileStore fileStore = Files.getFileStore(repositoryPath); if (fileStore.supportsFileAttributeView(DosFileAttributeView.class)) { DosFileAttributeView fileAttributeView = Files.getFileAttributeView(repositoryPath, DosFileAttributeView.class); if (fileAttributeView.readAttributes().isReadOnly()) { throw new IOException("File is not writable"); } } }
linkedHashMap.put("Is Archive?", dosFileAttr.isArchive() ? "True" : "False"); linkedHashMap.put("Is Hidden File?", dosFileAttr.isHidden() ? "True" : "False"); linkedHashMap.put("Is ReadOnly?", dosFileAttr.isReadOnly() ? "True" : "False"); linkedHashMap.put("Is System File?", dosFileAttr.isSystem() ? "True" : "False");
/** * Get permissions operation * * @param path Path * @return Permissions * @throws ServletException Error in transmission * @throws IOException Error in file */ private String getPermissions(Path path) throws IOException, ServletException { String permissionsStr = ""; if (isUnix) { // Unix environment PosixFileAttributeView fileAttributeView = Files.getFileAttributeView(path, PosixFileAttributeView.class); PosixFileAttributes readAttributes = fileAttributeView.readAttributes(); Set<PosixFilePermission> permissions = readAttributes.permissions(); permissionsStr = PosixFilePermissions.toString(permissions); } else if (isDOS) { // Windows environment DosFileAttributeView fileAttributeView = Files.getFileAttributeView(path, DosFileAttributeView.class); if (fileAttributeView.readAttributes().isReadOnly()) { permissionsStr = "readonly"; } } else { throw new ServletException("Error get permissions. Unknown filesystem"); } return permissionsStr; }
dosView.setHidden(dosAttributes.isHidden()); dosView.setSystem(dosAttributes.isSystem()); dosView.setReadOnly(dosAttributes.isReadOnly());
info.setLength(attrs.size()); info.setAttribute(EFS.ATTRIBUTE_ARCHIVE, attrs.isArchive()); info.setAttribute(EFS.ATTRIBUTE_READ_ONLY, attrs.isReadOnly()); info.setAttribute(EFS.ATTRIBUTE_HIDDEN, attrs.isHidden()); if (attrs.isSymbolicLink()) {
info.setLength(attrs.size()); info.setAttribute(EFS.ATTRIBUTE_ARCHIVE, attrs.isArchive()); info.setAttribute(EFS.ATTRIBUTE_READ_ONLY, attrs.isReadOnly()); info.setAttribute(EFS.ATTRIBUTE_HIDDEN, attrs.isHidden()); if (attrs.isSymbolicLink()) {
info.setLength(attrs.size()); info.setAttribute(EFS.ATTRIBUTE_ARCHIVE, attrs.isArchive()); info.setAttribute(EFS.ATTRIBUTE_READ_ONLY, attrs.isReadOnly()); info.setAttribute(EFS.ATTRIBUTE_HIDDEN, attrs.isHidden()); if (attrs.isSymbolicLink()) {
fileAttribute |= FileAttributeFlags.FILE_ATTRIBUTE_HIDDEN .getValue(); if (attr.isReadOnly()) fileAttribute |= FileAttributeFlags.FILE_ATTRIBUTE_READONLY .getValue();
targetDosAttrs.setArchive(sourceDosAttrs.isArchive()); targetDosAttrs.setHidden(sourceDosAttrs.isHidden()); targetDosAttrs.setReadOnly(sourceDosAttrs.isReadOnly()); targetDosAttrs.setSystem(sourceDosAttrs.isSystem());
targetDosAttrs.setArchive(sourceDosAttrs.isArchive()); targetDosAttrs.setHidden(sourceDosAttrs.isHidden()); targetDosAttrs.setReadOnly(sourceDosAttrs.isReadOnly()); targetDosAttrs.setSystem(sourceDosAttrs.isSystem());
targetDosAttrs.setArchive(sourceDosAttrs.isArchive()); targetDosAttrs.setHidden(sourceDosAttrs.isHidden()); targetDosAttrs.setReadOnly(sourceDosAttrs.isReadOnly()); targetDosAttrs.setSystem(sourceDosAttrs.isSystem());