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