Search in sources :

Example 31 with PosixFilePermission

use of java.nio.file.attribute.PosixFilePermission in project jdk8u_jdk by JetBrains.

the class PosixFilePermissions method asFileAttribute.

/**
     * Creates a {@link FileAttribute}, encapsulating a copy of the given file
     * permissions, suitable for passing to the {@link java.nio.file.Files#createFile
     * createFile} or {@link java.nio.file.Files#createDirectory createDirectory}
     * methods.
     *
     * @param   perms
     *          the set of permissions
     *
     * @return  an attribute encapsulating the given file permissions with
     *          {@link FileAttribute#name name} {@code "posix:permissions"}
     *
     * @throws  ClassCastException
     *          if the set contains elements that are not of type {@code
     *          PosixFilePermission}
     */
public static FileAttribute<Set<PosixFilePermission>> asFileAttribute(Set<PosixFilePermission> perms) {
    // copy set and check for nulls (CCE will be thrown if an element is not
    // a PosixFilePermission)
    perms = new HashSet<PosixFilePermission>(perms);
    for (PosixFilePermission p : perms) {
        if (p == null)
            throw new NullPointerException();
    }
    final Set<PosixFilePermission> value = perms;
    return new FileAttribute<Set<PosixFilePermission>>() {

        @Override
        public String name() {
            return "posix:permissions";
        }

        @Override
        public Set<PosixFilePermission> value() {
            return Collections.unmodifiableSet(value);
        }
    };
}
Also used : PosixFilePermission(java.nio.file.attribute.PosixFilePermission)

Example 32 with PosixFilePermission

use of java.nio.file.attribute.PosixFilePermission in project ignite by apache.

the class IgfsLocalSecondaryFileSystemTestAdapter method permissions.

/**
 * {@inheritDoc}
 */
@Override
public String permissions(String path) throws IOException {
    Path p = path(path);
    PosixFileAttributeView attrView = Files.getFileAttributeView(p, PosixFileAttributeView.class);
    if (attrView == null)
        throw new UnsupportedOperationException("Posix file attributes not available");
    int perm = 0;
    for (PosixFilePermission pfp : attrView.readAttributes().permissions()) perm |= (1 << 8 - pfp.ordinal());
    return '0' + Integer.toOctalString(perm);
}
Also used : Path(java.nio.file.Path) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView)

Example 33 with PosixFilePermission

use of java.nio.file.attribute.PosixFilePermission in project syncany by syncany.

the class ChangedAttributesScenarioTest method testNewFileWithDifferingAttributes.

@Test
public void testNewFileWithDifferingAttributes() throws Exception {
    // Setup
    TransferSettings testConnection = TestConfigUtil.createTestLocalConnection();
    TestClient clientA = new TestClient("A", testConnection);
    TestClient clientB = new TestClient("B", testConnection);
    // Create new file with differing attributes
    clientA.createNewFile("file1.jpg");
    FileUtils.copyFile(clientA.getLocalFile("file1.jpg"), clientB.getLocalFile("file1.jpg"));
    // Client B's attributes differ!
    File aFile = clientA.getLocalFile("file1.jpg");
    Path aFilePath = Paths.get(aFile.getAbsolutePath());
    Object aReadOnlyAttribute = null;
    Set<PosixFilePermission> aPosixFilePermissions = null;
    // Client B's attributes differ!
    File bFile = clientB.getLocalFile("file1.jpg");
    Path bFilePath = Paths.get(bFile.getAbsolutePath());
    if (EnvironmentUtil.isWindows()) {
        aReadOnlyAttribute = Files.getAttribute(aFilePath, "dos:readonly");
        Files.setAttribute(bFilePath, "dos:readonly", true);
    } else if (EnvironmentUtil.isUnixLikeOperatingSystem()) {
        aPosixFilePermissions = Files.getPosixFilePermissions(aFilePath);
        Files.setPosixFilePermissions(bFilePath, PosixFilePermissions.fromString("rwxrwxrwx"));
    }
    clientA.upWithForceChecksum();
    // This is the key operation
    DownOperationResult downResult = clientB.down();
    // Test 1: Check result sets for inconsistencies
    assertTrue("File should be downloaded.", downResult.getChangeSet().hasChanges());
    // Test 2: file1.jpg permissions (again!
    if (EnvironmentUtil.isWindows()) {
        Object bReadOnlyAttribute = Files.getAttribute(aFilePath, "dos:readonly");
        assertEquals("Read-only should be true.", aReadOnlyAttribute, bReadOnlyAttribute);
    } else if (EnvironmentUtil.isUnixLikeOperatingSystem()) {
        Set<PosixFilePermission> bPosixFilePermissions = Files.getPosixFilePermissions(aFilePath);
        assertEquals("Should be rwxrwxrwx.", PosixFilePermissions.toString(aPosixFilePermissions), PosixFilePermissions.toString(bPosixFilePermissions));
    }
    // Test 3: The rest
    assertFileListEquals(clientA.getLocalFilesExcludeLockedAndNoRead(), clientB.getLocalFilesExcludeLockedAndNoRead());
    assertSqlDatabaseEquals(clientA.getDatabaseFile(), clientB.getDatabaseFile());
    // Tear down
    clientA.deleteTestData();
    clientB.deleteTestData();
}
Also used : Path(java.nio.file.Path) Set(java.util.Set) ChangeSet(org.syncany.operations.ChangeSet) DownOperationResult(org.syncany.operations.down.DownOperationResult) TestClient(org.syncany.tests.util.TestClient) TransferSettings(org.syncany.plugins.transfer.TransferSettings) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) File(java.io.File) Test(org.junit.Test)

Example 34 with PosixFilePermission

use of java.nio.file.attribute.PosixFilePermission in project syncany by syncany.

the class FolderPermissionChangeScenarioTest method testFolderPermissionChange.

@Test
public void testFolderPermissionChange() throws Exception {
    // Setup
    TransferSettings testConnection = TestConfigUtil.createTestLocalConnection();
    TestClient clientA = new TestClient("A", testConnection);
    TestClient clientB = new TestClient("B", testConnection);
    // Run
    clientA.createNewFolder("Somefolder");
    clientA.upWithForceChecksum();
    clientB.createNewFolder("Somefolder");
    if (EnvironmentUtil.isUnixLikeOperatingSystem()) {
        Set<PosixFilePermission> expectedPosixPermissions = Files.getPosixFilePermissions(clientB.getLocalFile("Somefolder").toPath());
        Set<PosixFilePermission> changedPosixPermissions = PosixFilePermissions.fromString("rwxrwxrwx");
        Files.setPosixFilePermissions(clientB.getLocalFile("Somefolder").toPath(), changedPosixPermissions);
        clientB.down();
        assertEquals(1, clientB.getLocalFiles().size());
        assertEquals(PosixFilePermissions.toString(expectedPosixPermissions), PosixFilePermissions.toString(Files.getPosixFilePermissions(clientB.getLocalFile("Somefolder").toPath())));
    } else if (EnvironmentUtil.isWindows()) {
        DosFileAttributes expectedDosAttrs = Files.readAttributes(clientB.getLocalFile("Somefolder").toPath(), DosFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
        Files.setAttribute(clientB.getLocalFile("Somefolder").toPath(), "dos:hidden", true);
        Files.setAttribute(clientB.getLocalFile("Somefolder").toPath(), "dos:archive", true);
        Files.setAttribute(clientB.getLocalFile("Somefolder").toPath(), "dos:system", true);
        Files.setAttribute(clientB.getLocalFile("Somefolder").toPath(), "dos:readonly", true);
        clientB.down();
        assertEquals(1, clientB.getLocalFiles().size());
        DosFileAttributes actualDosAttrs = Files.readAttributes(clientB.getLocalFile("Somefolder").toPath(), DosFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
        assertEquals(LimitedDosFileAttributes.toString(expectedDosAttrs), LimitedDosFileAttributes.toString(actualDosAttrs));
    }
    // Tear down
    clientA.deleteTestData();
    clientB.deleteTestData();
}
Also used : LimitedDosFileAttributes(org.syncany.util.LimitedDosFileAttributes) DosFileAttributes(java.nio.file.attribute.DosFileAttributes) TestClient(org.syncany.tests.util.TestClient) TransferSettings(org.syncany.plugins.transfer.TransferSettings) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) Test(org.junit.Test)

Example 35 with PosixFilePermission

use of java.nio.file.attribute.PosixFilePermission in project gradle by gradle.

the class ChmodBenchmark method createFileJava7SetMixedPermission.

@Benchmark
public void createFileJava7SetMixedPermission(Blackhole blackhole) throws IOException {
    int incrementAndGet = counter.incrementAndGet();
    Path file = Files.createFile(tempDirPath.resolve("file-" + incrementAndGet));
    Set<PosixFilePermission> permissionsToSet;
    if (incrementAndGet % 2 == 0) {
        permissionsToSet = DEFAULT_JAVA7_FILE_PERMISSIONS;
    } else {
        permissionsToSet = WEIRD_JAVA7_FILE_PERMISSIONS;
    }
    Files.setPosixFilePermissions(file, permissionsToSet);
    blackhole.consume(file);
}
Also used : Path(java.nio.file.Path) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) Benchmark(org.openjdk.jmh.annotations.Benchmark)

Aggregations

PosixFilePermission (java.nio.file.attribute.PosixFilePermission)36 Path (java.nio.file.Path)21 IOException (java.io.IOException)10 Test (org.junit.Test)7 File (java.io.File)6 PosixFileAttributes (java.nio.file.attribute.PosixFileAttributes)6 HashSet (java.util.HashSet)6 PosixFileAttributeView (java.nio.file.attribute.PosixFileAttributeView)4 Set (java.util.Set)4 GroupPrincipal (java.nio.file.attribute.GroupPrincipal)3 TransferSettings (org.syncany.plugins.transfer.TransferSettings)3 TestClient (org.syncany.tests.util.TestClient)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 FileOutputStream (java.io.FileOutputStream)2 InputStream (java.io.InputStream)2 RandomAccessFile (java.io.RandomAccessFile)2 UserPrincipal (java.nio.file.attribute.UserPrincipal)2 Date (java.util.Date)2 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)2 ZipArchiveEntry (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)2