Search in sources :

Example 96 with PosixFilePermission

use of java.nio.file.attribute.PosixFilePermission in project aion by aionnetwork.

the class Keystore method create.

public static String create(String password, ECKey key) {
    Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rwxr-----");
    FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perms);
    if (!Files.exists(PATH)) {
        try {
            Files.createDirectory(PATH, attr);
        } catch (IOException e) {
            LOG.error("keystore folder create failed!");
            return "";
        }
    }
    String address = ByteUtil.toHexString(key.getAddress());
    if (exist(address)) {
        return ADDR_PREFIX;
    } else {
        byte[] content = new KeystoreFormat().toKeystore(key, password);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        df.setTimeZone(TimeZone.getTimeZone("UTC"));
        String iso_date = df.format(new Date(System.currentTimeMillis()));
        String fileName = "UTC--" + iso_date + "--" + address;
        try {
            Path keyFile = PATH.resolve(fileName);
            if (!Files.exists(keyFile))
                keyFile = Files.createFile(keyFile, attr);
            String path = keyFile.toString();
            FileOutputStream fos = new FileOutputStream(path);
            fos.write(content);
            fos.close();
            return StringUtils.toJsonHex(address);
        } catch (IOException e) {
            LOG.error("fail to create keystore");
            return ADDR_PREFIX;
        }
    }
}
Also used : Path(java.nio.file.Path) HashSet(java.util.HashSet) Set(java.util.Set) IOException(java.io.IOException) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) Date(java.util.Date) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) FileOutputStream(java.io.FileOutputStream) SimpleDateFormat(java.text.SimpleDateFormat)

Example 97 with PosixFilePermission

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

the class ArchiveUtilsTest method assertPosixPermissions.

private void assertPosixPermissions(Path path, PosixFilePermission... permissions) throws IOException {
    final PosixFileAttributeView fileAttributeView = Files.getFileAttributeView(path, PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
    if (fileAttributeView == null) {
        return;
    }
    Set<PosixFilePermission> permissionSet = new LinkedHashSet<>(Arrays.asList(permissions));
    assertEquals(permissionSet, fileAttributeView.readAttributes().permissions(), "Permissions for path=" + path + ", don't match expected.");
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView)

Example 98 with PosixFilePermission

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

the class TestPreUpgradeTool method testUpgradeExternalTableNoReadPermissionForTable.

@Test
public void testUpgradeExternalTableNoReadPermissionForTable() throws Exception {
    int[][] data = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
    runStatementOnDriver("drop table if exists TExternal");
    runStatementOnDriver("create table TExternal (a int, b int) stored as orc tblproperties('transactional'='false')");
    // this needs major compaction
    runStatementOnDriver("insert into TExternal" + makeValuesClause(data));
    String tableDir = getWarehouseDir() + "/texternal";
    File tablePath = new File(tableDir);
    try {
        Set<PosixFilePermission> perms = PosixFilePermissions.fromString("-w-------");
        Files.setPosixFilePermissions(tablePath.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(tablePath.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 99 with PosixFilePermission

use of java.nio.file.attribute.PosixFilePermission in project jimfs by google.

the class PosixAttributeProvider method defaultValues.

@SuppressWarnings("unchecked")
@Override
public ImmutableMap<String, ?> defaultValues(Map<String, ?> userProvidedDefaults) {
    Object userProvidedGroup = userProvidedDefaults.get("posix:group");
    UserPrincipal group = DEFAULT_GROUP;
    if (userProvidedGroup != null) {
        if (userProvidedGroup instanceof String) {
            group = createGroupPrincipal((String) userProvidedGroup);
        } else {
            throw new IllegalArgumentException("invalid type " + userProvidedGroup.getClass().getName() + " for attribute 'posix:group': should be one of " + String.class + " or " + GroupPrincipal.class);
        }
    }
    Object userProvidedPermissions = userProvidedDefaults.get("posix:permissions");
    Set<PosixFilePermission> permissions = DEFAULT_PERMISSIONS;
    if (userProvidedPermissions != null) {
        if (userProvidedPermissions instanceof String) {
            permissions = Sets.immutableEnumSet(PosixFilePermissions.fromString((String) userProvidedPermissions));
        } else if (userProvidedPermissions instanceof Set) {
            permissions = toPermissions((Set<?>) userProvidedPermissions);
        } else {
            throw new IllegalArgumentException("invalid type " + userProvidedPermissions.getClass().getName() + " for attribute 'posix:permissions': should be one of " + String.class + " or " + Set.class);
        }
    }
    return ImmutableMap.of("posix:group", group, "posix:permissions", permissions);
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) UserLookupService.createGroupPrincipal(com.google.common.jimfs.UserLookupService.createGroupPrincipal) GroupPrincipal(java.nio.file.attribute.GroupPrincipal) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) UserPrincipal(java.nio.file.attribute.UserPrincipal)

Example 100 with PosixFilePermission

use of java.nio.file.attribute.PosixFilePermission in project spring-boot by spring-projects.

the class AbstractBootArchiveTests method launchScriptCanBePrepended.

@Test
void launchScriptCanBePrepended() throws IOException {
    this.task.getMainClass().set("com.example.Main");
    this.task.launchScript();
    executeTask();
    Map<String, String> properties = new HashMap<>();
    properties.put("initInfoProvides", this.task.getArchiveBaseName().get());
    properties.put("initInfoShortDescription", this.project.getDescription());
    properties.put("initInfoDescription", this.project.getDescription());
    File archiveFile = this.task.getArchiveFile().get().getAsFile();
    assertThat(Files.readAllBytes(archiveFile.toPath())).startsWith(new DefaultLaunchScript(null, properties).toByteArray());
    try (ZipFile zipFile = new ZipFile(archiveFile)) {
        assertThat(zipFile.getEntries().hasMoreElements()).isTrue();
    }
    try {
        Set<PosixFilePermission> permissions = Files.getPosixFilePermissions(archiveFile.toPath());
        assertThat(permissions).contains(PosixFilePermission.OWNER_EXECUTE);
    } catch (UnsupportedOperationException ex) {
    // Windows, presumably. Continue
    }
}
Also used : DefaultLaunchScript(org.springframework.boot.loader.tools.DefaultLaunchScript) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) HashMap(java.util.HashMap) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) JarFile(java.util.jar.JarFile) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) Test(org.junit.jupiter.api.Test)

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