Example usage for java.nio.file.attribute PosixFilePermissions toString

List of usage examples for java.nio.file.attribute PosixFilePermissions toString

Introduction

In this page you can find the example usage for java.nio.file.attribute PosixFilePermissions toString.

Prototype

public static String toString(Set<PosixFilePermission> perms) 

Source Link

Document

Returns the String representation of a set of permissions.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {

    Set<PosixFilePermission> permissions = PosixFilePermissions.fromString("rw-r--r--");
    System.out.println(PosixFilePermissions.toString(permissions));

}

From source file:Test.java

public static void main(String[] args) throws Exception {

    Path profile = Paths.get("/user/Admin/.profile");

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

    Set<PosixFilePermission> posixPermissions = attrs.permissions();
    posixPermissions.clear();/*ww  w. j  av  a 2  s  .  co m*/

    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);
    posixPermissions.add(OWNER_READ);
    posixPermissions.add(OWNER_WRITE);
    Files.setPosixFilePermissions(profile, posixPermissions);

}

From source file:Main.java

public static void readPermissions(PosixFileAttributeView posixView) throws Exception {
    PosixFileAttributes attribs;//w w  w .  ja  v a 2  s.  c  o m
    attribs = posixView.readAttributes();
    Set<PosixFilePermission> permissions = attribs.permissions();
    // Convert the set of posix file permissions into rwxrwxrwx form
    String rwxFormPermissions = PosixFilePermissions.toString(permissions);
    System.out.println(rwxFormPermissions);
}

From source file:uk.ac.sanger.cgp.wwdocker.Config.java

public static void protectedFileCheck(String filename) throws AccessControlException, IOException {
    Path path = Paths.get(filename);
    Set<PosixFilePermission> permset = Files.getPosixFilePermissions(path);
    Iterator perm = permset.iterator();
    while (perm.hasNext()) {
        PosixFilePermission p = (PosixFilePermission) perm.next();
        if (p.equals(PosixFilePermission.GROUP_READ) || p.equals(PosixFilePermission.OTHERS_READ)) {
            throw new AccessControlException(
                    "Your configuration file (which contains passwords) is readable to others:\n" + "\tFile: "
                            + filename.toString() + "\tPerm: " + PosixFilePermissions.toString(permset));
        }//from ww w .java 2  s .c o m
    }
}

From source file:com.nsn.squirrel.tab.utils.PathUtils.java

/**
 * @param path/*from  w  ww  . j  a  va 2 s  .c  o m*/
 * @return
 */
public static String getPosixAttributesString(Path path) {

    PosixFileAttributeView posixView = Files.getFileAttributeView(path, PosixFileAttributeView.class);
    StringBuilder attrs = new StringBuilder();
    try {
        // + all basic attributes
        PosixFileAttributes posixAttrs = posixView.readAttributes();
        if (posixAttrs != null) {
            attrs.append(PosixFilePermissions.toString(posixAttrs.permissions()));
        }
    } catch (IOException e) {
        log.warn("unable to read Posix file attributes.", e); //$NON-NLS-1$
    }
    return attrs.toString();
}

From source file:cane.brothers.e4.commander.utils.PathUtils.java

/**
 * @param path//from w w w.j a v a2 s.com
 * @return
 */
public static String getPosixAttributesString(Path path) {
    PosixFileAttributeView posixView = Files.getFileAttributeView(path, PosixFileAttributeView.class);
    StringBuilder attrs = new StringBuilder();

    try {
        // + all basic attributes
        PosixFileAttributes posixAttrs = posixView.readAttributes();

        if (posixAttrs != null) {
            attrs.append(PosixFilePermissions.toString(posixAttrs.permissions()));
        }
    } catch (IOException e) {
        log.warn("unable to read Posix file attributes.", e); //$NON-NLS-1$
    }
    return attrs.toString();
}

From source file:org.fim.internal.hash.FileHasher.java

@Override
public void run() {
    try {//w w w.j a  va 2  s.  c om
        Path file;
        while ((file = filesToHashQueue.poll(500, TimeUnit.MILLISECONDS)) != null) {
            try {
                BasicFileAttributes attributes;
                List<Attribute> fileAttributes = null;

                if (SystemUtils.IS_OS_WINDOWS) {
                    DosFileAttributes dosFileAttributes = Files.readAttributes(file, DosFileAttributes.class);
                    fileAttributes = addAttribute(fileAttributes, FileAttribute.DosFilePermissions,
                            DosFilePermissions.toString(dosFileAttributes));
                    attributes = dosFileAttributes;
                } else {
                    PosixFileAttributes posixFileAttributes = Files.readAttributes(file,
                            PosixFileAttributes.class);
                    fileAttributes = addAttribute(fileAttributes, FileAttribute.PosixFilePermissions,
                            PosixFilePermissions.toString(posixFileAttributes.permissions()));
                    if (SELinux.ENABLED) {
                        fileAttributes = addAttribute(fileAttributes, FileAttribute.SELinuxLabel,
                                SELinux.getLabel(context, file));
                    }
                    attributes = posixFileAttributes;
                }

                hashProgress.updateOutput(attributes.size());

                FileHash fileHash = hashFile(file, attributes.size());
                String normalizedFileName = FileUtil.getNormalizedFileName(file);
                String relativeFileName = FileUtil.getRelativeFileName(rootDir, normalizedFileName);

                fileStates.add(new FileState(relativeFileName, attributes, fileHash, fileAttributes));
            } catch (Exception ex) {
                Console.newLine();
                Logger.error("Skipping - Error hashing file '" + file + "'", ex, context.isDisplayStackTrace());
            }
        }
    } catch (InterruptedException ex) {
        Logger.error("Exception while hashing", ex, context.isDisplayStackTrace());
    }
}

From source file:org.syncany.tests.scenarios.ChangedAttributesScenarioTest.java

@Test
public void testChangeAttributes() throws Exception {
    // Setup //from   w  w  w . j  a v  a 2s  . co m
    TransferSettings testConnection = TestConfigUtil.createTestLocalConnection();
    TestClient clientA = new TestClient("A", testConnection);
    TestClient clientB = new TestClient("B", testConnection);

    // Run 
    clientA.createNewFile("file1.jpg");
    clientA.upWithForceChecksum();

    clientB.down();

    File bFile = clientB.getLocalFile("file1.jpg");
    Path bFilePath = Paths.get(bFile.getAbsolutePath());

    if (EnvironmentUtil.isWindows()) {
        Files.setAttribute(bFilePath, "dos:readonly", true);
    } else if (EnvironmentUtil.isUnixLikeOperatingSystem()) {
        Files.setPosixFilePermissions(bFilePath, PosixFilePermissions.fromString("rwxrwxrwx"));
    }

    StatusOperationResult statusResult = clientB.status();
    assertNotNull(statusResult);

    ChangeSet changes = statusResult.getChangeSet();

    assertTrue("Status-Operation should return changes.", changes.hasChanges());
    UpOperationResult upResult = clientB.up();
    StatusOperationResult statusResultFromUp = upResult.getStatusResult();

    // Test 1: Check result sets for inconsistencies
    assertTrue("Status should return changes.", statusResultFromUp.getChangeSet().hasChanges());
    assertTrue("File should be uploaded.", upResult.getChangeSet().hasChanges());

    // Test 2: Check database for inconsistencies
    SqlDatabase database = clientB.loadLocalDatabase();

    assertNotNull("File should be uploaded.", database.getFileVersionByPath("file1.jpg"));
    assertEquals("There should be a new database version, because file should not have been added.", 2,
            database.getLocalDatabaseBranch().size());

    // B down
    clientA.down();

    // Test 1: file1.jpg permissions
    File aFile = clientA.getLocalFile("file1.jpg");
    Path aFilePath = Paths.get(aFile.getAbsolutePath());

    if (EnvironmentUtil.isWindows()) {
        Object readOnlyAttribute = Files.getAttribute(aFilePath, "dos:readonly");
        assertTrue("Read-only should be true.", (Boolean) readOnlyAttribute);
    } else if (EnvironmentUtil.isUnixLikeOperatingSystem()) {
        Set<PosixFilePermission> posixFilePermissions = Files.getPosixFilePermissions(aFilePath);
        assertEquals("Should be rwxrwxrwx.", "rwxrwxrwx", PosixFilePermissions.toString(posixFilePermissions));
    }

    // Test 2: The rest
    assertFileListEquals(clientA.getLocalFilesExcludeLockedAndNoRead(),
            clientB.getLocalFilesExcludeLockedAndNoRead());
    assertSqlDatabaseEquals(clientA.getDatabaseFile(), clientB.getDatabaseFile());

    // Tear down
    clientA.deleteTestData();
    clientB.deleteTestData();
}

From source file:de.jwi.jfm.FileWrapper.java

public String getAttributes() throws IOException {
    FileSystem fileSystem = FileSystems.getDefault();
    Set<String> fileSystemViews = fileSystem.supportedFileAttributeViews();

    File file = getFile();// w  w  w  .ja  va 2  s .  c  o m
    Path p = file.toPath();

    try {
        if (fileSystemViews.contains("posix")) {
            Set<PosixFilePermission> posixFilePermissions = Files.getPosixFilePermissions(p,
                    LinkOption.NOFOLLOW_LINKS);

            PosixFileAttributes attrs = Files.getFileAttributeView(p, PosixFileAttributeView.class)
                    .readAttributes();

            String owner = attrs.owner().toString();
            String group = attrs.group().toString();

            String permissions = PosixFilePermissions.toString(attrs.permissions());

            String res = String.format("%s %s %s", permissions, owner, group);
            return res;
        } else if (fileSystemViews.contains("dos")) {
            StringWriter sw = new StringWriter();
            DosFileAttributeView attributeView = Files.getFileAttributeView(p, DosFileAttributeView.class);
            DosFileAttributes dosFileAttributes = null;

            dosFileAttributes = attributeView.readAttributes();
            if (dosFileAttributes.isArchive()) {
                sw.append('A');
            }
            if (dosFileAttributes.isHidden()) {
                sw.append('H');
            }
            if (dosFileAttributes.isReadOnly()) {
                sw.append('R');
            }
            if (dosFileAttributes.isSystem()) {
                sw.append('S');
            }

            return sw.toString();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return "";
}

From source file:org.syncany.tests.integration.scenarios.ChangedAttributesScenarioTest.java

@Test
public void testChangeAttributes() throws Exception {
    // Setup //from   ww  w .j  ava 2s .  c  om
    TransferSettings testConnection = TestConfigUtil.createTestLocalConnection();
    TestClient clientA = new TestClient("A", testConnection);
    TestClient clientB = new TestClient("B", testConnection);

    // Run 
    clientA.createNewFile("file1.jpg");
    clientA.upWithForceChecksum();

    clientB.down();

    File bFile = clientB.getLocalFile("file1.jpg");
    Path bFilePath = Paths.get(bFile.getAbsolutePath());

    if (EnvironmentUtil.isWindows()) {
        Files.setAttribute(bFilePath, "dos:readonly", true);
    } else if (EnvironmentUtil.isUnixLikeOperatingSystem()) {
        Files.setPosixFilePermissions(bFilePath, PosixFilePermissions.fromString("rwxrwxrwx"));
    }

    StatusOperationResult statusResult = clientB.status();
    assertNotNull(statusResult);

    ChangeSet changes = statusResult.getChangeSet();

    assertTrue("Status-Operation should return changes.", changes.hasChanges());
    UpOperationResult upResult = clientB.up();
    StatusOperationResult statusResultFromUp = upResult.getStatusResult();

    // Test 1: Check result sets for inconsistencies
    assertTrue("Status should return changes.", statusResultFromUp.getChangeSet().hasChanges());
    assertTrue("File should be uploaded.", upResult.getChangeSet().hasChanges());

    // Test 2: Check database for inconsistencies
    SqlDatabase database = clientB.loadLocalDatabase();

    assertEquals("File should be uploaded.", 1,
            database.getFileList("file1.jpg", null, false, false, false, null).size());
    assertEquals("There should be a new database version, because file should not have been added.", 2,
            database.getLocalDatabaseBranch().size());

    // B down
    clientA.down();

    // Test 1: file1.jpg permissions
    File aFile = clientA.getLocalFile("file1.jpg");
    Path aFilePath = Paths.get(aFile.getAbsolutePath());

    if (EnvironmentUtil.isWindows()) {
        Object readOnlyAttribute = Files.getAttribute(aFilePath, "dos:readonly");
        assertTrue("Read-only should be true.", (Boolean) readOnlyAttribute);
    } else if (EnvironmentUtil.isUnixLikeOperatingSystem()) {
        Set<PosixFilePermission> posixFilePermissions = Files.getPosixFilePermissions(aFilePath);
        assertEquals("Should be rwxrwxrwx.", "rwxrwxrwx", PosixFilePermissions.toString(posixFilePermissions));
    }

    // Test 2: The rest
    assertFileListEquals(clientA.getLocalFilesExcludeLockedAndNoRead(),
            clientB.getLocalFilesExcludeLockedAndNoRead());
    assertSqlDatabaseEquals(clientA.getDatabaseFile(), clientB.getDatabaseFile());

    // Tear down
    clientA.deleteTestData();
    clientB.deleteTestData();
}