Search in sources :

Example 46 with PosixFilePermission

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

the class TestPreUpgradeTool method testUpgradeExternalTableNoReadPermissionForDatabase.

@Test
public void testUpgradeExternalTableNoReadPermissionForDatabase() throws Exception {
    int[][] data = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
    runStatementOnDriver("drop database if exists test cascade");
    runStatementOnDriver("drop table if exists TExternal");
    runStatementOnDriver("create database test");
    runStatementOnDriver("create table test.TExternal (a int, b int) stored as orc tblproperties" + "('transactional'='false')");
    // this needs major compaction
    runStatementOnDriver("insert into test.TExternal" + makeValuesClause(data));
    String dbDir = getWarehouseDir() + "/test.db";
    File dbPath = new File(dbDir);
    try {
        Set<PosixFilePermission> perms = PosixFilePermissions.fromString("-w-------");
        Files.setPosixFilePermissions(dbPath.toPath(), perms);
        String[] args = { "-location", getTestDataDir(), "-execute" };
        PreUpgradeTool.pollIntervalMs = 1;
        PreUpgradeTool.hiveConf = hiveConf;
        Exception expected = null;
        try {
            PreUpgradeTool.main(args);
        } catch (Exception e) {
            expected = e;
        }
        Assert.assertNotNull(expected);
        Assert.assertTrue(expected instanceof HiveException);
        Assert.assertTrue(expected.getMessage().contains("Pre-upgrade tool requires " + "read-access to databases and tables to determine if a table has to be compacted."));
    } finally {
        Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rwxrw----");
        Files.setPosixFilePermissions(dbPath.toPath(), perms);
    }
}
Also used : HiveException(org.apache.hadoop.hive.ql.metadata.HiveException) StringContains.containsString(org.hamcrest.core.StringContains.containsString) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) File(java.io.File) MetaException(org.apache.hadoop.hive.metastore.api.MetaException) IOException(java.io.IOException) HiveException(org.apache.hadoop.hive.ql.metadata.HiveException) Test(org.junit.Test)

Example 47 with PosixFilePermission

use of java.nio.file.attribute.PosixFilePermission in project SSM by Intel-bigdata.

the class InterpreterSettingManager method saveToFile.

public void saveToFile() throws IOException {
    String jsonString;
    synchronized (interpreterSettings) {
        InterpreterInfoSaving info = new InterpreterInfoSaving();
        info.interpreterBindings = interpreterBindings;
        info.interpreterSettings = interpreterSettings;
        info.interpreterRepositories = interpreterRepositories;
        jsonString = gson.toJson(info);
    }
    if (!Files.exists(interpreterBindingPath)) {
        Files.createFile(interpreterBindingPath);
        try {
            Set<PosixFilePermission> permissions = EnumSet.of(OWNER_READ, OWNER_WRITE);
            Files.setPosixFilePermissions(interpreterBindingPath, permissions);
        } catch (UnsupportedOperationException e) {
            // File system does not support Posix file permissions (likely windows) - continue anyway.
            logger.warn("unable to setPosixFilePermissions on '{}'.", interpreterBindingPath);
        }
        ;
    }
    FileOutputStream fos = new FileOutputStream(interpreterBindingPath.toFile(), false);
    OutputStreamWriter out = new OutputStreamWriter(fos);
    out.append(jsonString);
    out.close();
    fos.close();
}
Also used : FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) PosixFilePermission(java.nio.file.attribute.PosixFilePermission)

Example 48 with PosixFilePermission

use of java.nio.file.attribute.PosixFilePermission in project ats-framework by Axway.

the class LocalFileSystemOperations method getPermissions.

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" + ownerPermissions + groupPermissions + othersPermissions;
}
Also used : Path(java.nio.file.Path) FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) IOException(java.io.IOException) PosixFileAttributes(java.nio.file.attribute.PosixFileAttributes) PosixFilePermission(java.nio.file.attribute.PosixFilePermission)

Example 49 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 50 with PosixFilePermission

use of java.nio.file.attribute.PosixFilePermission in project jPOS by jpos.

the class SshService method checkAuthorizedKeys.

private void checkAuthorizedKeys(String s) throws IOException {
    String OS = System.getProperty("os.name").toLowerCase();
    if ((OS.indexOf("win") >= 0)) {
        log.info("Windows Detected, ignoring file permissions check: " + OS);
        return;
    }
    Path file = Paths.get(s);
    PosixFileAttributes attrs = Files.getFileAttributeView(file, PosixFileAttributeView.class).readAttributes();
    Set<PosixFilePermission> perms = attrs.permissions();
    if (perms.contains(GROUP_WRITE) || perms.contains(OTHERS_WRITE) || (perms.contains(OWNER_WRITE) && !Files.isWritable(file)))
        throw new IllegalArgumentException(String.format("Invalid permissions '%s' for file '%s'", PosixFilePermissions.toString(perms), s));
}
Also used : Path(java.nio.file.Path) PosixFileAttributes(java.nio.file.attribute.PosixFileAttributes) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView)

Aggregations

PosixFilePermission (java.nio.file.attribute.PosixFilePermission)139 Path (java.nio.file.Path)69 IOException (java.io.IOException)41 File (java.io.File)40 HashSet (java.util.HashSet)40 Test (org.junit.Test)35 Set (java.util.Set)22 FileOutputStream (java.io.FileOutputStream)17 PosixFileAttributeView (java.nio.file.attribute.PosixFileAttributeView)14 PosixFileAttributes (java.nio.file.attribute.PosixFileAttributes)11 FileNotFoundException (java.io.FileNotFoundException)8 ArrayList (java.util.ArrayList)7 RandomAccessFile (java.io.RandomAccessFile)6 UserPrincipal (java.nio.file.attribute.UserPrincipal)6 OutputStream (java.io.OutputStream)5 SimpleDateFormat (java.text.SimpleDateFormat)5 BufferedWriter (java.io.BufferedWriter)4 InputStream (java.io.InputStream)4 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)4 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)4