Search in sources :

Example 41 with PosixFilePermission

use of java.nio.file.attribute.PosixFilePermission in project packr by libgdx.

the class ArchiveUtils method getUnixMode.

private static int getUnixMode(Path file) throws IOException {
    PosixFileAttributeView fileAttributeView = Files.getFileAttributeView(file, PosixFileAttributeView.class, NOFOLLOW_LINKS);
    if (fileAttributeView == null) {
        if (Files.isSymbolicLink(file)) {
            return DEFAULT_LINK_MODE;
        } else if (Files.isDirectory(file)) {
            return DEFAULT_DIRECTORY_MODE;
        }
        return DEFAULT_FILE_MODE;
    }
    int mode = 0;
    Set<PosixFilePermission> permissions = fileAttributeView.readAttributes().permissions();
    if (permissions.contains(PosixFilePermission.OWNER_READ)) {
        mode |= OWNER_READ_BIT_MASK;
    }
    if (permissions.contains(PosixFilePermission.OWNER_WRITE)) {
        mode |= OWNER_WRITE_BIT_MASK;
    }
    if (permissions.contains(PosixFilePermission.OWNER_EXECUTE)) {
        mode |= OWNER_EXECUTE_BIT_MASK;
    }
    if (permissions.contains(PosixFilePermission.GROUP_READ)) {
        mode |= GROUP_READ_BIT_MASK;
    }
    if (permissions.contains(PosixFilePermission.GROUP_WRITE)) {
        mode |= GROUP_WRITE_BIT_MASK;
    }
    if (permissions.contains(PosixFilePermission.GROUP_EXECUTE)) {
        mode |= GROUP_EXECUTE_BIT_MASK;
    }
    if (permissions.contains(PosixFilePermission.OTHERS_READ)) {
        mode |= OTHERS_READ_BIT_MASK;
    }
    if (permissions.contains(PosixFilePermission.OTHERS_WRITE)) {
        mode |= OTHERS_WRITE_BIT_MASK;
    }
    if (permissions.contains(PosixFilePermission.OTHERS_EXECUTE)) {
        mode |= OTHERS_EXECUTE_BIT_MASK;
    }
    LOG.trace("Unix mode of file=" + file + ", mode=" + Integer.toOctalString(mode) + ", permissions=" + permissions);
    return mode;
}
Also used : PosixFilePermission(java.nio.file.attribute.PosixFilePermission) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView)

Example 42 with PosixFilePermission

use of java.nio.file.attribute.PosixFilePermission in project packr by libgdx.

the class ArchiveUtils method extractTarArchive.

/**
 * Extracts a TAR archive. If the current platform supports POSIX permissions, the archive entry permissions are applied to the create file or directory.
 * Symbolic and "hard" links are also support.
 *
 * @param inputStream        the archive input stream
 * @param extractToDirectory the directory to extract the archive into
 * @throws IOException if an IO error occurs
 */
private static void extractTarArchive(InputStream inputStream, Path extractToDirectory) throws IOException {
    final TarArchiveInputStream archiveInputStream = new TarArchiveInputStream(inputStream);
    TarArchiveEntry entry;
    while ((entry = archiveInputStream.getNextTarEntry()) != null) {
        if (!archiveInputStream.canReadEntryData(entry)) {
            LOG.error("Failed to read archive entry " + entry);
            continue;
        }
        Path entryExtractPath = extractToDirectory.resolve(getEntryAsPath(entry));
        if (entry.isLink()) {
            Path linkTarget = Paths.get(entry.getLinkName());
            Files.deleteIfExists(entryExtractPath);
            Files.createLink(entryExtractPath, linkTarget);
        } else if (entry.isSymbolicLink()) {
            Path linkTarget = Paths.get(entry.getLinkName());
            Files.deleteIfExists(entryExtractPath);
            Files.createSymbolicLink(entryExtractPath, linkTarget);
        } else {
            if (entry.isDirectory()) {
                Files.createDirectories(entryExtractPath);
            } else {
                Files.createDirectories(entryExtractPath.getParent());
                Files.copy(archiveInputStream, entryExtractPath, StandardCopyOption.REPLACE_EXISTING);
            }
        }
        setLastModifiedTime(entryExtractPath, FileTime.fromMillis(entry.getLastModifiedDate().getTime()));
        Set<PosixFilePermission> permissions = getPosixFilePermissions(entry);
        setPosixPermissions(entryExtractPath, permissions);
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) Path(java.nio.file.Path) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 43 with PosixFilePermission

use of java.nio.file.attribute.PosixFilePermission in project packr by libgdx.

the class ArchiveUtils method extractZipEntry.

/**
 * Extracts a {@link ZipArchiveEntry}, creating files and directories that match the date modified, POSIX permissions, and symbolic link properties of the
 * archive entry.
 *
 * @param extractToDirectory the directory to extract to
 * @param archiveInputStream the archive input stream
 * @param entry              the entry to extract
 * @throws IOException if an IO error occurs
 */
private static void extractZipEntry(Path extractToDirectory, InputStream archiveInputStream, ZipArchiveEntry entry) throws IOException {
    Path entryExtractPath = extractToDirectory.resolve(getEntryAsPath(entry));
    if (entry.isUnixSymlink()) {
        final byte[] contentBuffer = new byte[8192];
        final int contentLength = IOUtils.readFully(archiveInputStream, contentBuffer);
        Path linkTarget = Paths.get(new String(contentBuffer, 0, contentLength, StandardCharsets.UTF_8));
        Files.deleteIfExists(entryExtractPath);
        Files.createSymbolicLink(entryExtractPath, linkTarget);
    } else {
        if (entry.isDirectory()) {
            Files.createDirectories(entryExtractPath);
        } else {
            Files.createDirectories(entryExtractPath.getParent());
            Files.copy(archiveInputStream, entryExtractPath, StandardCopyOption.REPLACE_EXISTING);
        }
    }
    setLastModifiedTime(entryExtractPath, entry.getLastModifiedTime());
    Set<PosixFilePermission> permissions = getPosixFilePermissions(entry);
    setPosixPermissions(entryExtractPath, permissions);
}
Also used : Path(java.nio.file.Path) PosixFilePermission(java.nio.file.attribute.PosixFilePermission)

Example 44 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 45 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)

Aggregations

PosixFilePermission (java.nio.file.attribute.PosixFilePermission)145 Path (java.nio.file.Path)73 IOException (java.io.IOException)43 HashSet (java.util.HashSet)41 File (java.io.File)40 Test (org.junit.Test)38 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 FileSubjects.assertThatPath (com.google.copybara.testing.FileSubjects.assertThatPath)6 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