Package java.nio.file.attribute

Examples of java.nio.file.attribute.PosixFileAttributes


   * @throws IOException
   */
  public static void main(String[] args) throws IOException {
    Path filePath = Paths.get("file.txt");
   
    PosixFileAttributes attributes =
        Files.readAttributes(filePath, PosixFileAttributes.class);
   
    // To read permissions - file should exist!!! Otherwise
    // java.nio.file.NoSuchFileException is thrown.
    Set<PosixFilePermission> posixPermissions = attributes.permissions();
    for (PosixFilePermission posixPermission : posixPermissions) {
      System.out.println(posixPermission);
    }

    Path newFilePath = Paths.get("new//directory//file.txt");
View Full Code Here


    public Java7FileAttributes( File file, Map<Integer, String> userCache, Map<Integer, String> groupCache )
        throws IOException
    {

        PosixFileAttributes posixFileAttributes = getPosixFileAttributes( file );

        Integer uid = (Integer) Files.readAttributes( file.toPath(), "unix:uid" ).get( "uid" );
        String userName = userCache.get( uid );
        if ( userName != null )
        {
            this.userName = userName;
        }
        else
        {
            this.userName = posixFileAttributes.owner().getName();
            userCache.put( uid, this.userName );
        }
        Integer gid = (Integer) Files.readAttributes( file.toPath(), "unix:gid" ).get( "gid" );
        String groupName = groupCache.get( gid );
        if ( groupName != null )
        {
            this.groupName = groupName;
        }
        else
        {
            this.groupName = posixFileAttributes.group().getName();
            groupCache.put( gid, this.groupName );
        }

        setLsModeParts( PosixFilePermissions.toString( posixFileAttributes.permissions() ).toCharArray() );
    }
View Full Code Here

      Map<String, String[]> attributes = new HashMap<String, String[]>();

      PosixFileAttributeView posixView = Files.getFileAttributeView(path, PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
      if (posixView != null) {
        final PosixFileAttributes attr = posixView.readAttributes();
        if (type.equals(ItemType.LINK)) {
          attributes.put(Item.ATTRIBUTE_POSIX, new String[] { attr.group().getName(), attr.owner().getName() });
        } else {
          attributes.put(Item.ATTRIBUTE_POSIX, new String[] { attr.group().getName(), attr.owner().getName(), fromPermissions(attr.permissions()).toString() });
        }
      } else {

        DosFileAttributeView dosView = Files.getFileAttributeView(path, DosFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
        if (dosView != null) {
          final DosFileAttributes attr = dosView.readAttributes();
          attributes.put(Item.ATTRIBUTE_DOS, new String[] { attr.isArchive() ? "1" : "0", attr.isHidden() ? "1" : "0", attr.isReadOnly() ? "1" : "0", attr.isSystem() ? "1" : "0" });
        }
      }

      if (!type.equals(ItemType.LINK)) {
View Full Code Here

  }

  static Attributes getFileAttributesPosix(FS fs, File path) {
    try {
      Path nioPath = path.toPath();
      PosixFileAttributes readAttributes = nioPath
          .getFileSystem()
          .provider()
          .getFileAttributeView(nioPath,
              PosixFileAttributeView.class,
              LinkOption.NOFOLLOW_LINKS).readAttributes();
      Attributes attributes = new FileUtil.Java7BasicAttributes(
          fs,
          path,
          true, //
          readAttributes.isDirectory(), //
          readAttributes.permissions().contains(
              PosixFilePermission.OWNER_EXECUTE),
          readAttributes.isSymbolicLink(),
          readAttributes.isRegularFile(), //
          readAttributes.creationTime().toMillis(), //
          readAttributes.lastModifiedTime().toMillis(),
          readAttributes.size());
      return attributes;
    } catch (NoSuchFileException e) {
      return new FileUtil.Java7BasicAttributes(fs, path, false, false,
          false, false, false, 0L, 0L, 0L);
    } catch (IOException e) {
View Full Code Here

    assertNotNull(view);

    assertThat(view.name()).isEqualTo("posix");
    assertThat(view.getOwner()).isEqualTo(createUserPrincipal("user"));

    PosixFileAttributes attrs = view.readAttributes();
    assertThat(attrs.fileKey()).isEqualTo(0);
    assertThat(attrs.owner()).isEqualTo(createUserPrincipal("user"));
    assertThat(attrs.group()).isEqualTo(createGroupPrincipal("group"));
    assertThat(attrs.permissions()).isEqualTo(PosixFilePermissions.fromString("rw-r--r--"));

    view.setOwner(createUserPrincipal("root"));
    assertThat(view.getOwner()).isEqualTo(createUserPrincipal("root"));
    assertThat(file.getAttribute("owner", "owner")).isEqualTo(createUserPrincipal("root"));
View Full Code Here

        PosixFilePermissions.fromString("rwx------"));
  }

  @Test
  public void testAttributes() {
    PosixFileAttributes attrs = provider.readAttributes(file);
    assertThat(attrs.permissions()).isEqualTo(PosixFilePermissions.fromString("rw-r--r--"));
    assertThat(attrs.group()).isEqualTo(createGroupPrincipal("group"));
    assertThat(attrs.fileKey()).isEqualTo(0);
  }
View Full Code Here

  public static void main(String[] args) {
    try {
      Path profile = Paths.get("/user/Admin/.profile");

      PosixFileAttributes attrs = Files.readAttributes(profile,
          PosixFileAttributes.class);

      Set<PosixFilePermission> posixPermissions = attrs.permissions();
      posixPermissions.clear();

      String owner = attrs.owner().getName();
      String perms = PosixFilePermissions.toString(posixPermissions);
      System.out.format("%s %s%n", owner, perms);

      posixPermissions.add(OWNER_READ);
      posixPermissions.add(GROUP_READ);
View Full Code Here

        fileProperties.dosAttributes = FileUtil.dosAttrsToString(dosAttrs);

        fileAttributes = dosAttrs;
      }
      else if (EnvironmentUtil.isUnixLikeOperatingSystem()) {
        PosixFileAttributes posixAttrs = Files.readAttributes(filePath, PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
        fileProperties.posixPermissions = PosixFilePermissions.toString(posixAttrs.permissions());

        fileAttributes = posixAttrs;
      }
      else {
        fileAttributes = Files.readAttributes(filePath, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
View Full Code Here

import static org.gradle.internal.nativeintegration.filesystem.jdk7.PosixFilePermissionConverter.convertToPermissionsSet;

public class PosixJdk7FilePermissionHandler implements FileModeAccessor, FileModeMutator {

    public int getUnixMode(File file) throws IOException {
        final PosixFileAttributes posixFileAttributes = Files.readAttributes(file.toPath(), PosixFileAttributes.class);
        return convertToInt(posixFileAttributes.permissions());
    }
View Full Code Here

TOP

Related Classes of java.nio.file.attribute.PosixFileAttributes

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.