Java Code Examples for java.nio.file.attribute.PosixFileAttributes#permissions()

The following examples show how to use java.nio.file.attribute.PosixFileAttributes#permissions() . 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 File: CompressedDirectory.java    From docker-client with Apache License 2.0 6 votes vote down vote up
private static int getPosixFileMode(Path file) throws IOException {
  final PosixFileAttributes attr = Files.readAttributes(file, PosixFileAttributes.class);
  final Set<PosixFilePermission> perm = attr.permissions();

  // retain permissions, note these values are octal
  //noinspection OctalInteger
  int mode = 0100000;
  //noinspection OctalInteger
  mode += 0100 * getModeFromPermissions(
      perm.contains(PosixFilePermission.OWNER_READ),
      perm.contains(PosixFilePermission.OWNER_WRITE),
      perm.contains(PosixFilePermission.OWNER_EXECUTE));

  //noinspection OctalInteger
  mode += 010 * getModeFromPermissions(
      perm.contains(PosixFilePermission.GROUP_READ),
      perm.contains(PosixFilePermission.GROUP_WRITE),
      perm.contains(PosixFilePermission.GROUP_EXECUTE));

  mode += getModeFromPermissions(
      perm.contains(PosixFilePermission.OTHERS_READ),
      perm.contains(PosixFilePermission.OTHERS_WRITE),
      perm.contains(PosixFilePermission.OTHERS_EXECUTE));

  return mode;
}
 
Example 2
Source File: ZipFSPermissionsTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Validate that the Zip file permissions are as expected after updating the
 * Zip file
 * @param newPerms The permissions to set on the Zip File before updating the
 *                 file
 * @throws Exception If an error occurs
 */
@Test(dataProvider = "posixPermissions")
public void testZipPerms(Set<PosixFilePermission> newPerms) throws Exception {
    if (DEBUG) {
        System.out.printf("Test Run with perms= %s%n", newPerms);
    }

    PosixFileAttributes attrs = getPosixAttributes(zipFile);

    // Permissions used to verify the results of updating the Zip file
    if (newPerms == null) {
        // Use current Zip File permissions;
        newPerms = attrs.permissions();
    }
    displayPermissions("Original permissions", zipFile);

    // Now set the new permissions
    Files.setPosixFilePermissions(zipFile, newPerms);
    displayPermissions("Revised permissions", zipFile);

    // Update the Zip file
    zip(zipFile, Collections.emptyMap(), entry1);

    // Validate that the permissions are as expected after updating the
    // Zip file
    PosixFileAttributes afterAttrs = getPosixAttributes(zipFile);
    displayPermissions("Permissions after updating the Zip File", zipFile);
    assertEquals(afterAttrs.permissions(), newPerms,
            "Permissions were not updated as expected!");
}
 
Example 3
Source File: PosixFileAttributesTest.java    From ParallelGit with Apache License 2.0 5 votes vote down vote up
@Test
public void getPermissionOfFile_shouldContainOwnerRead() throws IOException {
  writeToCache("/file.txt");
  commitToMaster();
  initGitFileSystem();

  PosixFileAttributes attributes = readPosixAttributes("/file.txt");
  Collection permissions = (Collection) attributes.permissions();
  assertTrue(permissions.contains(PosixFilePermission.OWNER_READ));
}
 
Example 4
Source File: PosixFileAttributesTest.java    From ParallelGit with Apache License 2.0 5 votes vote down vote up
@Test
public void getPermissionOfFile_shouldContainOwnerWrite() throws IOException {
  writeToCache("/file.txt");
  commitToMaster();
  initGitFileSystem();

  PosixFileAttributes attributes = readPosixAttributes("/file.txt");
  Collection permissions = (Collection) attributes.permissions();
  assertTrue(permissions.contains(PosixFilePermission.OWNER_WRITE));
}
 
Example 5
Source File: PosixFileAttributesTest.java    From ParallelGit with Apache License 2.0 5 votes vote down vote up
@Test
public void getPermissionOfFile_shouldNotContainOwnerExecute() throws IOException {
  writeToCache("/file.txt");
  commitToMaster();
  initGitFileSystem();

  PosixFileAttributes attributes = readPosixAttributes("/file.txt");
  Collection permissions = (Collection) attributes.permissions();
  assertFalse(permissions.contains(OWNER_EXECUTE));
}
 
Example 6
Source File: PosixFileAttributesTest.java    From ParallelGit with Apache License 2.0 5 votes vote down vote up
@Test
public void getPermissionOfExecutableFile_shouldContainOwnerExecute() throws IOException {
  writeToCache("/file.txt", someBytes(), EXECUTABLE_FILE);
  commitToMaster();
  initGitFileSystem();

  PosixFileAttributes attributes = readPosixAttributes("/file.txt");
  Collection permissions = (Collection) attributes.permissions();
  assertTrue(permissions.contains(OWNER_EXECUTE));
}
 
Example 7
Source File: PosixFileAttributesTest.java    From ParallelGit with Apache License 2.0 5 votes vote down vote up
@Test
public void getPermissionOfFile_shouldContainOwnerRead() throws IOException {
  writeToCache("/file.txt");
  commitToMaster();
  initGitFileSystem();

  PosixFileAttributes attributes = readPosixAttributes("/file.txt");
  Collection permissions = (Collection) attributes.permissions();
  assertTrue(permissions.contains(PosixFilePermission.OWNER_READ));
}
 
Example 8
Source File: PosixFileAttributesTest.java    From ParallelGit with Apache License 2.0 5 votes vote down vote up
@Test
public void getPermissionOfFile_shouldContainOwnerWrite() throws IOException {
  writeToCache("/file.txt");
  commitToMaster();
  initGitFileSystem();

  PosixFileAttributes attributes = readPosixAttributes("/file.txt");
  Collection permissions = (Collection) attributes.permissions();
  assertTrue(permissions.contains(PosixFilePermission.OWNER_WRITE));
}
 
Example 9
Source File: PosixFileAttributesTest.java    From ParallelGit with Apache License 2.0 5 votes vote down vote up
@Test
public void getPermissionOfFile_shouldNotContainOwnerExecute() throws IOException {
  writeToCache("/file.txt");
  commitToMaster();
  initGitFileSystem();

  PosixFileAttributes attributes = readPosixAttributes("/file.txt");
  Collection permissions = (Collection) attributes.permissions();
  assertFalse(permissions.contains(OWNER_EXECUTE));
}
 
Example 10
Source File: PosixFileAttributesTest.java    From ParallelGit with Apache License 2.0 5 votes vote down vote up
@Test
public void getPermissionOfExecutableFile_shouldContainOwnerExecute() throws IOException {
  writeToCache("/file.txt", someBytes(), EXECUTABLE_FILE);
  commitToMaster();
  initGitFileSystem();

  PosixFileAttributes attributes = readPosixAttributes("/file.txt");
  Collection permissions = (Collection) attributes.permissions();
  assertTrue(permissions.contains(OWNER_EXECUTE));
}
 
Example 11
Source File: LocalFileSystemOperations.java    From ats-framework with Apache License 2.0 4 votes vote down vote up
private String getPermissions(
        String sourceFile ) {

    int ownerPermissions = 0;
    int groupPermissions = 0;
    int othersPermissions = 0;

    try {
        Path path = Paths.get(sourceFile);
        PosixFileAttributes attr;
        attr = Files.readAttributes(path, PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
        Set<PosixFilePermission> filePermissions = attr.permissions();

        if (filePermissions.contains(PosixFilePermission.OWNER_READ)) {
            ownerPermissions += 4;
        }
        if (filePermissions.contains(PosixFilePermission.OWNER_WRITE)) {
            ownerPermissions += 2;
        }
        if (filePermissions.contains(PosixFilePermission.OWNER_EXECUTE)) {
            ownerPermissions += 1;
        }
        if (filePermissions.contains(PosixFilePermission.GROUP_READ)) {
            groupPermissions += 4;
        }
        if (filePermissions.contains(PosixFilePermission.GROUP_WRITE)) {
            groupPermissions += 2;
        }
        if (filePermissions.contains(PosixFilePermission.GROUP_EXECUTE)) {
            groupPermissions += 1;
        }
        if (filePermissions.contains(PosixFilePermission.OTHERS_READ)) {
            othersPermissions += 4;
        }
        if (filePermissions.contains(PosixFilePermission.OTHERS_WRITE)) {
            othersPermissions += 2;
        }
        if (filePermissions.contains(PosixFilePermission.OTHERS_EXECUTE)) {
            othersPermissions += 1;
        }

    } catch (IOException ioe) {
        throw new FileSystemOperationException("Could not get permissions for file '" + sourceFile + "'",
                                               ioe);
    }

    return "0" + String.valueOf(ownerPermissions) + String.valueOf(groupPermissions)
           + String.valueOf(othersPermissions);
}
 
Example 12
Source File: LocalFileSystemUtils.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * Convert POSIX attributes to property map.
 *
 * @param attrs Attributes view.
 * @return IGFS properties map.
 */
public static Map<String, String> posixAttributesToMap(PosixFileAttributes attrs) {
    if (attrs == null)
        return null;

    Map<String, String> props = U.newHashMap(3);

    props.put(IgfsUtils.PROP_USER_NAME, attrs.owner().getName());
    props.put(IgfsUtils.PROP_GROUP_NAME, attrs.group().getName());

    int perm = 0;

    for (PosixFilePermission p : attrs.permissions())
        perm |= (1 << 8 - p.ordinal());

    props.put(IgfsUtils.PROP_PERMISSION, '0' + Integer.toOctalString(perm));

    return props;
}
 
Example 13
Source File: PosixFileAttributeManager.java    From yajsync with GNU General Public License v3.0 4 votes vote down vote up
private static int toMode(PosixFileAttributes attrs)
{
    int mode = 0;

    if (attrs.isDirectory()) {
        mode |= FileOps.S_IFDIR;
    } else if (attrs.isRegularFile()) {
        mode |= FileOps.S_IFREG;
    } else if (attrs.isSymbolicLink()) {
        mode |= FileOps.S_IFLNK;
    } else {
        mode |= FileOps.S_IFUNK;
    }

    Set<PosixFilePermission> perms = attrs.permissions();
    if (perms.contains(PosixFilePermission.OWNER_READ)) {
        mode |= FileOps.S_IRUSR;
    }
    if (perms.contains(PosixFilePermission.OWNER_WRITE)) {
        mode |= FileOps.S_IWUSR;
    }
    if (perms.contains(PosixFilePermission.OWNER_EXECUTE)) {
        mode |= FileOps.S_IXUSR;
    }
    if (perms.contains(PosixFilePermission.GROUP_READ)) {
        mode |= FileOps.S_IRGRP;
    }
    if (perms.contains(PosixFilePermission.GROUP_WRITE)) {
        mode |= FileOps.S_IWGRP;
    }
    if (perms.contains(PosixFilePermission.GROUP_EXECUTE)) {
        mode |= FileOps.S_IXGRP;
    }
    if (perms.contains(PosixFilePermission.OTHERS_READ)) {
        mode |= FileOps.S_IROTH;
    }
    if (perms.contains(PosixFilePermission.OTHERS_WRITE)) {
        mode |= FileOps.S_IWOTH;
    }
    if (perms.contains(PosixFilePermission.OTHERS_EXECUTE)) {
        mode |= FileOps.S_IXOTH;
    }

    return mode;
}