Example usage for java.nio.file.attribute PosixFileAttributeView readAttributes

List of usage examples for java.nio.file.attribute PosixFileAttributeView readAttributes

Introduction

In this page you can find the example usage for java.nio.file.attribute PosixFileAttributeView readAttributes.

Prototype

@Override
PosixFileAttributes readAttributes() throws IOException;

Source Link

Usage

From source file:org.elasticsearch.plugins.PluginManagerIT.java

public void testLocalPluginInstallWithBinAndConfig() throws Exception {
    String pluginName = "fake-plugin";
    Path pluginDir = createTempDir().resolve(pluginName);
    // create bin/tool and config/file
    Files.createDirectories(pluginDir.resolve("bin"));
    Files.createFile(pluginDir.resolve("bin").resolve("tool"));
    Files.createDirectories(pluginDir.resolve("config"));
    Files.createFile(pluginDir.resolve("config").resolve("file"));

    String pluginUrl = createPlugin(pluginDir, "description", "fake desc", "name", pluginName, "version", "1.0",
            "elasticsearch.version", Version.CURRENT.toString(), "java.version",
            System.getProperty("java.specification.version"), "jvm", "true", "classname", "FakePlugin");

    Path binDir = environment.binFile();
    Path pluginBinDir = binDir.resolve(pluginName);

    Path pluginConfigDir = environment.configFile().resolve(pluginName);
    assertStatusOk("install " + pluginUrl + " --verbose");

    terminal.getTerminalOutput().clear();
    assertStatusOk("list");
    assertThat(terminal.getTerminalOutput(), hasItem(containsString(pluginName)));

    assertDirectoryExists(pluginBinDir);
    assertDirectoryExists(pluginConfigDir);
    Path toolFile = pluginBinDir.resolve("tool");
    assertFileExists(toolFile);/*from   w ww .  j  a  v a 2s .  c o m*/

    // check that the file is marked executable, without actually checking that we can execute it.
    PosixFileAttributeView view = Files.getFileAttributeView(toolFile, PosixFileAttributeView.class);
    // the view might be null, on e.g. windows, there is nothing to check there!
    if (view != null) {
        PosixFileAttributes attributes = view.readAttributes();
        assertThat(attributes.permissions(), hasItem(PosixFilePermission.OWNER_EXECUTE));
        assertThat(attributes.permissions(), hasItem(PosixFilePermission.OWNER_READ));
    }
}

From source file:io.hops.hopsworks.common.security.CertificateMaterializer.java

@PostConstruct
public void init() {
    File tmpDir = new File(settings.getHopsworksTmpCertDir());
    if (!tmpDir.exists()) {
        throw new IllegalStateException(
                "Transient certificates directory <" + tmpDir.getAbsolutePath() + "> does NOT exist!");
    }//from  w w w .j a  va  2s .c  o m

    try {
        PosixFileAttributeView fileView = Files.getFileAttributeView(tmpDir.toPath(),
                PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
        Set<PosixFilePermission> permissions = fileView.readAttributes().permissions();
        boolean ownerRead = permissions.contains(PosixFilePermission.OWNER_READ);
        boolean ownerWrite = permissions.contains(PosixFilePermission.OWNER_WRITE);
        boolean ownerExecute = permissions.contains(PosixFilePermission.OWNER_EXECUTE);

        boolean groupRead = permissions.contains(PosixFilePermission.GROUP_READ);
        boolean groupWrite = permissions.contains(PosixFilePermission.GROUP_WRITE);
        boolean groupExecute = permissions.contains(PosixFilePermission.GROUP_EXECUTE);

        boolean othersRead = permissions.contains(PosixFilePermission.OTHERS_READ);
        boolean othersWrite = permissions.contains(PosixFilePermission.OTHERS_WRITE);
        boolean othersExecute = permissions.contains(PosixFilePermission.OTHERS_EXECUTE);

        // Permissions should be 750
        if ((ownerRead && ownerWrite && ownerExecute) && (groupRead && !groupWrite && groupExecute)
                && (!othersRead && !othersWrite & !othersExecute)) {
            String owner = fileView.readAttributes().owner().getName();
            String group = fileView.readAttributes().group().getName();
            String permStr = PosixFilePermissions.toString(permissions);
            LOG.log(Level.INFO, "Passed permissions check for " + tmpDir.getAbsolutePath() + ". Owner: " + owner
                    + " Group: " + group + " permissions: " + permStr);
        } else {
            throw new IllegalStateException(
                    "Wrong permissions for " + tmpDir.getAbsolutePath() + ", it should be 0750");
        }
    } catch (UnsupportedOperationException ex) {
        LOG.log(Level.WARNING,
                "Associated filesystem is not POSIX compliant. "
                        + "Continue without checking the permissions of " + tmpDir.getAbsolutePath()
                        + " This might be a security problem.");
    } catch (IOException ex) {
        throw new IllegalStateException(
                "Error while getting filesystem " + "permissions of " + tmpDir.getAbsolutePath(), ex);
    }

    try {
        FileUtils.cleanDirectory(tmpDir);
    } catch (IOException ex) {
        LOG.log(Level.WARNING, "Could not clean directory " + tmpDir.getAbsolutePath()
                + " during startup, there might be stale " + "certificates", ex);
    }
    transientDir = tmpDir.getAbsolutePath();
    String delayRaw = settings.getCertificateMaterializerDelay();
    DELAY_VALUE = settings.getConfTimeValue(delayRaw);
    DELAY_TIMEUNIT = settings.getConfTimeTimeUnit(delayRaw);

    try {
        String hostAddress = InetAddress.getLocalHost().getHostAddress();
        long threadId = Thread.currentThread().getId();
        String lock_identifier = hostAddress + "_" + threadId;
        lock_id = lock_identifier.length() <= 30 ? lock_identifier : lock_identifier.substring(0, 30);
    } catch (UnknownHostException ex) {
        throw new IllegalStateException(ex);
    }
}

From source file:org.eclipse.tycho.plugins.tar.TarGzArchiver.java

private PosixFileAttributes getAttributes(File source) {
    PosixFileAttributeView fileAttributeView = Files.getFileAttributeView(source.toPath(),
            PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
    if (fileAttributeView == null) {
        return null;
    }/*from   w  ww.  j av  a  2 s.c o  m*/
    PosixFileAttributes attrs;
    try {
        attrs = fileAttributeView.readAttributes();
    } catch (IOException e) {
        return null;
    }
    return attrs;
}

From source file:org.eclipse.tycho.plugins.tar.TarGzArchiverTest.java

private PosixFileAttributes getPosixFileAttributes(File file) {
    try {/*from  ww w .j ava 2 s  . c o m*/
        PosixFileAttributeView attributeView = Files.getFileAttributeView(file.toPath(),
                PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
        return attributeView.readAttributes();
    } catch (Exception e) {
        Assume.assumeNoException("skip test on filesystems that do not support POSIX file attributes", e);
    }
    // never reached
    return null;
}