java.nio.file.attribute.DosFileAttributeView Java Examples

The following examples show how to use java.nio.file.attribute.DosFileAttributeView. 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 Project: zip4j   Author: srikanth-lingala   File: FileUtils.java    License: Apache License 2.0 6 votes vote down vote up
private static void applyWindowsFileAttributes(Path file, byte[] fileAttributes) {
  if (fileAttributes[0] == 0) {
    // No file attributes defined in the archive
    return;
  }

  DosFileAttributeView fileAttributeView = Files.getFileAttributeView(file, DosFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
  try {
    fileAttributeView.setReadOnly(isBitSet(fileAttributes[0], 0));
    fileAttributeView.setHidden(isBitSet(fileAttributes[0], 1));
    fileAttributeView.setSystem(isBitSet(fileAttributes[0], 2));
    fileAttributeView.setArchive(isBitSet(fileAttributes[0], 5));
  } catch (IOException e) {
    //Ignore
  }
}
 
Example #2
Source Project: zip4j   Author: srikanth-lingala   File: FileUtils.java    License: Apache License 2.0 6 votes vote down vote up
private static byte[] getWindowsFileAttributes(Path file) {
  byte[] fileAttributes = new byte[4];

  try {
    DosFileAttributeView dosFileAttributeView = Files.getFileAttributeView(file, DosFileAttributeView.class,
        LinkOption.NOFOLLOW_LINKS);
    DosFileAttributes dosFileAttributes = dosFileAttributeView.readAttributes();

    byte windowsAttribute = 0;

    windowsAttribute = setBitIfApplicable(dosFileAttributes.isReadOnly(), windowsAttribute, 0);
    windowsAttribute = setBitIfApplicable(dosFileAttributes.isHidden(), windowsAttribute, 1);
    windowsAttribute = setBitIfApplicable(dosFileAttributes.isSystem(), windowsAttribute, 2);
    windowsAttribute = setBitIfApplicable(dosFileAttributes.isArchive(), windowsAttribute, 5);
    fileAttributes[0] = windowsAttribute;
  } catch (IOException e) {
    // ignore
  }

  return fileAttributes;
}
 
Example #3
Source Project: zip4j   Author: srikanth-lingala   File: FileUtilsTestWindows.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSetFileAttributesOnWindowsMachineWhenAttributesSet() throws IOException {
  Path mockedPath = mock(Path.class);
  DosFileAttributeView dosFileAttributeView = mockDosFileAttributeView(mockedPath, true, null);

  byte attribute = 0;
  attribute = BitUtils.setBit(attribute, 0);
  attribute = BitUtils.setBit(attribute, 1);
  attribute = BitUtils.setBit(attribute, 2);
  attribute = BitUtils.setBit(attribute, 5);

  FileUtils.setFileAttributes(mockedPath, new byte[]{attribute, 0, 0, 0});

  verify(dosFileAttributeView).setReadOnly(true);
  verify(dosFileAttributeView).setHidden(true);
  verify(dosFileAttributeView).setSystem(true);
  verify(dosFileAttributeView).setArchive(true);
}
 
Example #4
Source Project: zip4j   Author: srikanth-lingala   File: FileUtilsTestWindows.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testGetFileAttributesReturnsAttributesAsDefined() throws IOException {
  File file = mock(File.class);
  Path path = mock(Path.class);
  when(file.toPath()).thenReturn(path);
  when(file.exists()).thenReturn(true);
  DosFileAttributes dosFileAttributes = mock(DosFileAttributes.class);
  DosFileAttributeView dosFileAttributeView = mockDosFileAttributeView(path, true, dosFileAttributes);
  when(dosFileAttributeView.readAttributes()).thenReturn(dosFileAttributes);
  when(dosFileAttributes.isReadOnly()).thenReturn(true);
  when(dosFileAttributes.isHidden()).thenReturn(true);
  when(dosFileAttributes.isSystem()).thenReturn(true);
  when(dosFileAttributes.isArchive()).thenReturn(true);

  byte[] attributes = FileUtils.getFileAttributes(file);

  assertThat(isBitSet(attributes[0], 0)).isTrue();
  assertThat(isBitSet(attributes[0], 1)).isTrue();
  assertThat(isBitSet(attributes[0], 2)).isTrue();
  assertThat(isBitSet(attributes[0], 5)).isTrue();
}
 
Example #5
Source Project: zip4j   Author: srikanth-lingala   File: FileUtilsTestWindows.java    License: Apache License 2.0 6 votes vote down vote up
private DosFileAttributeView mockDosFileAttributeView(Path path, boolean fileExists, DosFileAttributes dosFileAttributes) throws IOException {
  FileSystemProvider fileSystemProvider = mock(FileSystemProvider.class);
  FileSystem fileSystem = mock(FileSystem.class);
  DosFileAttributeView dosFileAttributeView = mock(DosFileAttributeView.class);

  when(fileSystemProvider.readAttributes(path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS)).thenReturn(dosFileAttributes);
  when(path.getFileSystem()).thenReturn(fileSystem);
  when(fileSystemProvider.getFileAttributeView(path, DosFileAttributeView.class, LinkOption.NOFOLLOW_LINKS))
      .thenReturn(dosFileAttributeView);
  when(path.getFileSystem().provider()).thenReturn(fileSystemProvider);

  if (!fileExists) {
    doThrow(new IOException()).when(fileSystemProvider).checkAccess(path);
  }

  return dosFileAttributeView;
}
 
Example #6
Source Project: jsr203-hadoop   Author: damiencarol   File: TestFileStore.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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));
}
 
Example #7
Source Project: bazel   Author: bazelbuild   File: ScopedTemporaryDirectory.java    License: Apache License 2.0 6 votes vote down vote up
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));
    }
  }
}
 
Example #8
Source Project: code   Author: lastwhispers   File: TestNIO_2.java    License: Apache License 2.0 5 votes vote down vote up
@Test
    public void test6() throws IOException {
        Path path = Paths.get("e:/nio/hello7.txt");
//		System.out.println(Files.exists(path, LinkOption.NOFOLLOW_LINKS));

        BasicFileAttributes readAttributes = Files.readAttributes(path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
        System.out.println(readAttributes.creationTime());
        System.out.println(readAttributes.lastModifiedTime());

        DosFileAttributeView fileAttributeView = Files.getFileAttributeView(path, DosFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);

        fileAttributeView.setHidden(false);
    }
 
Example #9
Source Project: zip4j   Author: srikanth-lingala   File: FileUtilsTestWindows.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testSetFileAttributesOnWindowsWhenNoAttributesSetDoesNothing() throws IOException {
  Path mockedPath = mock(Path.class);
  DosFileAttributeView dosFileAttributeView = mockDosFileAttributeView(mockedPath, true, null);

  FileUtils.setFileAttributes(mockedPath, new byte[]{0, 0, 0, 0});

  verifyZeroInteractions(dosFileAttributeView);
}
 
Example #10
Source Project: jimfs   Author: google   File: DosAttributeProviderTest.java    License: Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example #11
Source Project: simple-nfs   Author: kofemann   File: LocalFileSystem.java    License: Apache License 2.0 5 votes vote down vote up
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.isSymbolicLink() ? Stat.S_IFLNK : dosAttrs.isDirectory() ? Stat.S_IFDIR : Stat.S_IFREG;
            stat.setMode( type |(dosAttrs.isReadOnly()? 0400 : 0600));
            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;
    }
 
Example #12
Source Project: dragonwell8_jdk   Author: alibaba   File: IsHidden.java    License: GNU General Public License v2.0 4 votes vote down vote up
private static void setHidden(File f, boolean value) throws IOException {
    Files.getFileAttributeView(f.toPath(), DosFileAttributeView.class).setHidden(value);
}
 
Example #13
Source Project: TencentKona-8   Author: Tencent   File: IsHidden.java    License: GNU General Public License v2.0 4 votes vote down vote up
private static void setHidden(File f, boolean value) throws IOException {
    Files.getFileAttributeView(f.toPath(), DosFileAttributeView.class).setHidden(value);
}
 
Example #14
Source Project: jdk8u60   Author: chenghanpeng   File: IsHidden.java    License: GNU General Public License v2.0 4 votes vote down vote up
private static void setHidden(File f, boolean value) throws IOException {
    Files.getFileAttributeView(f.toPath(), DosFileAttributeView.class).setHidden(value);
}
 
Example #15
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: IsHidden.java    License: GNU General Public License v2.0 4 votes vote down vote up
private static void setHidden(File f, boolean value) throws IOException {
    Files.getFileAttributeView(f.toPath(), DosFileAttributeView.class).setHidden(value);
}
 
Example #16
Source Project: openjdk-jdk8u-backup   Author: AdoptOpenJDK   File: IsHidden.java    License: GNU General Public License v2.0 4 votes vote down vote up
private static void setHidden(File f, boolean value) throws IOException {
    Files.getFileAttributeView(f.toPath(), DosFileAttributeView.class).setHidden(value);
}
 
Example #17
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: IsHidden.java    License: GNU General Public License v2.0 4 votes vote down vote up
private static void setHidden(File f, boolean value) throws IOException {
    Files.getFileAttributeView(f.toPath(), DosFileAttributeView.class).setHidden(value);
}
 
Example #18
Source Project: jdk8u-jdk   Author: lambdalab-mirror   File: IsHidden.java    License: GNU General Public License v2.0 4 votes vote down vote up
private static void setHidden(File f, boolean value) throws IOException {
    Files.getFileAttributeView(f.toPath(), DosFileAttributeView.class).setHidden(value);
}
 
Example #19
Source Project: hottub   Author: dsrg-uoft   File: IsHidden.java    License: GNU General Public License v2.0 4 votes vote down vote up
private static void setHidden(File f, boolean value) throws IOException {
    Files.getFileAttributeView(f.toPath(), DosFileAttributeView.class).setHidden(value);
}
 
Example #20
Source Project: openjdk-8-source   Author: keerath   File: IsHidden.java    License: GNU General Public License v2.0 4 votes vote down vote up
private static void setHidden(File f, boolean value) throws IOException {
    Files.getFileAttributeView(f.toPath(), DosFileAttributeView.class).setHidden(value);
}
 
Example #21
Source Project: openjdk-8   Author: bpupadhyaya   File: IsHidden.java    License: GNU General Public License v2.0 4 votes vote down vote up
private static void setHidden(File f, boolean value) throws IOException {
    Files.getFileAttributeView(f.toPath(), DosFileAttributeView.class).setHidden(value);
}
 
Example #22
Source Project: jdk8u_jdk   Author: JetBrains   File: IsHidden.java    License: GNU General Public License v2.0 4 votes vote down vote up
private static void setHidden(File f, boolean value) throws IOException {
    Files.getFileAttributeView(f.toPath(), DosFileAttributeView.class).setHidden(value);
}
 
Example #23
Source Project: jdk8u-jdk   Author: frohoff   File: IsHidden.java    License: GNU General Public License v2.0 4 votes vote down vote up
private static void setHidden(File f, boolean value) throws IOException {
    Files.getFileAttributeView(f.toPath(), DosFileAttributeView.class).setHidden(value);
}
 
Example #24
Source Project: jdk8u-dev-jdk   Author: frohoff   File: IsHidden.java    License: GNU General Public License v2.0 4 votes vote down vote up
private static void setHidden(File f, boolean value) throws IOException {
    Files.getFileAttributeView(f.toPath(), DosFileAttributeView.class).setHidden(value);
}
 
Example #25
Source Project: jimfs   Author: google   File: DosAttributeProvider.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Class<DosFileAttributeView> viewType() {
  return DosFileAttributeView.class;
}
 
Example #26
Source Project: jimfs   Author: google   File: DosAttributeProvider.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public DosFileAttributeView view(
    FileLookup lookup, ImmutableMap<String, FileAttributeView> inheritedViews) {
  return new View(lookup, (BasicFileAttributeView) inheritedViews.get("basic"));
}