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()));
}
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);
}
}
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();
}
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));
}
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);
}
}
}
Aggregations