Search in sources :

Example 6 with PosixFilePermission

use of java.nio.file.attribute.PosixFilePermission in project buck by facebook.

the class ProjectFilesystemTest method testCreateReadOnlyFileSetsPermissions.

@Test
public void testCreateReadOnlyFileSetsPermissions() throws IOException {
    Assume.assumeTrue(FileSystems.getDefault().supportedFileAttributeViews().contains("posix"));
    Path path = Paths.get("hello.txt");
    ImmutableSet<PosixFilePermission> permissions = ImmutableSet.of(PosixFilePermission.OWNER_READ, PosixFilePermission.GROUP_READ, PosixFilePermission.OTHERS_READ);
    filesystem.writeContentsToPath("hello world", path, PosixFilePermissions.asFileAttribute(permissions));
    // The umask may restrict the actual permissions on the filesystem:
    // https://fburl.com/26569549
    // So the best we can do is to check that the actual permissions are a
    // strict subset of the expected permissions.
    PosixFileAttributes attrs = filesystem.readAttributes(path, PosixFileAttributes.class);
    assertTrue(permissions.containsAll(attrs.permissions()));
}
Also used : Path(java.nio.file.Path) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) PosixFileAttributes(java.nio.file.attribute.PosixFileAttributes) Test(org.junit.Test)

Example 7 with PosixFilePermission

use of java.nio.file.attribute.PosixFilePermission in project helios by spotify.

the class X509CertificateFactory method saveToCache.

private static void saveToCache(final Path cacheDirectory, final Path cacheCertPath, final Path cacheKeyPath, final CertificateAndPrivateKey certificateAndPrivateKey) {
    try {
        Files.createDirectories(cacheDirectory);
        final String certPem = asPemString(certificateAndPrivateKey.getCertificate());
        final String keyPem = asPemString(certificateAndPrivateKey.getPrivateKey());
        // overwrite any existing file, and make sure it's only readable by the current user
        final Set<StandardOpenOption> options = ImmutableSet.of(CREATE, WRITE);
        final Set<PosixFilePermission> perms = ImmutableSet.of(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE);
        final FileAttribute<Set<PosixFilePermission>> attrs = PosixFilePermissions.asFileAttribute(perms);
        try (final SeekableByteChannel sbc = Files.newByteChannel(cacheCertPath, options, attrs)) {
            sbc.write(ByteBuffer.wrap(certPem.getBytes()));
        }
        try (final SeekableByteChannel sbc = Files.newByteChannel(cacheKeyPath, options, attrs)) {
            sbc.write(ByteBuffer.wrap(keyPem.getBytes()));
        }
        log.debug("cached generated certificate to {}", cacheCertPath);
    } catch (IOException e) {
        // couldn't save to the cache, oh well
        log.warn("error caching generated certificate", e);
    }
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) StandardOpenOption(java.nio.file.StandardOpenOption) IOException(java.io.IOException) PosixFilePermission(java.nio.file.attribute.PosixFilePermission)

Example 8 with PosixFilePermission

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

the class FileLockedScenarioTest method testPermissionDeniedNotReadable.

@Test
public void testPermissionDeniedNotReadable() throws Exception {
    if (EnvironmentUtil.isWindows()) {
        // Not possible in windows
        return;
    }
    // Setup
    TransferSettings testConnection = TestConfigUtil.createTestLocalConnection();
    TestClient clientA = new TestClient("A", testConnection);
    // Run
    File noReadPermissionFile = clientA.createNewFile("no-read-permission-file");
    Path filePath = Paths.get(noReadPermissionFile.getAbsolutePath());
    Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
    perms.add(PosixFilePermission.OWNER_READ);
    perms.add(PosixFilePermission.GROUP_READ);
    perms.add(PosixFilePermission.OTHERS_READ);
    Files.setPosixFilePermissions(filePath, perms);
    runUpAndTestForConsistentDatabase(testConnection, clientA);
    // Tear down
    clientA.deleteTestData();
}
Also used : Path(java.nio.file.Path) TestClient(org.syncany.tests.util.TestClient) LocalTransferSettings(org.syncany.plugins.local.LocalTransferSettings) TransferSettings(org.syncany.plugins.transfer.TransferSettings) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) RandomAccessFile(java.io.RandomAccessFile) LockFile(org.syncany.tests.integration.scenarios.framework.LockFile) File(java.io.File) UnlockFile(org.syncany.tests.integration.scenarios.framework.UnlockFile) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 9 with PosixFilePermission

use of java.nio.file.attribute.PosixFilePermission in project buck by facebook.

the class FakeProjectFilesystemTest method testWriteContentsWithSpecifiedFileAttributes.

@Test
public void testWriteContentsWithSpecifiedFileAttributes() throws IOException {
    FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
    ImmutableSet<PosixFilePermission> permissions = ImmutableSet.of(PosixFilePermission.OWNER_READ, PosixFilePermission.GROUP_READ, PosixFilePermission.OTHERS_READ);
    FileAttribute<?> attribute = PosixFilePermissions.asFileAttribute(permissions);
    Path path = Paths.get("hello.txt");
    filesystem.writeContentsToPath("hello world", Paths.get("hello.txt"), attribute);
    assertEquals(ImmutableSet.of(attribute), filesystem.getFileAttributesAtPath(path));
}
Also used : Path(java.nio.file.Path) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) Test(org.junit.Test)

Example 10 with PosixFilePermission

use of java.nio.file.attribute.PosixFilePermission in project java-chassis by ServiceComb.

the class FilePerm method setFilePerm.

public static void setFilePerm(File file, String perm) {
    if (filePermSupported()) {
        try {
            Set<PosixFilePermission> perms = PosixFilePermissions.fromString(perm);
            PosixFileAttributes attr = Files.readAttributes(file.toPath(), PosixFileAttributes.class);
            attr.permissions().clear();
            Files.setPosixFilePermissions(file.toPath(), perms);
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }
}
Also used : IOException(java.io.IOException) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) PosixFileAttributes(java.nio.file.attribute.PosixFileAttributes)

Aggregations

PosixFilePermission (java.nio.file.attribute.PosixFilePermission)33 Path (java.nio.file.Path)18 IOException (java.io.IOException)10 Test (org.junit.Test)7 File (java.io.File)6 HashSet (java.util.HashSet)6 PosixFileAttributes (java.nio.file.attribute.PosixFileAttributes)5 Set (java.util.Set)4 GroupPrincipal (java.nio.file.attribute.GroupPrincipal)3 PosixFileAttributeView (java.nio.file.attribute.PosixFileAttributeView)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