Example usage for java.nio.file.attribute PosixFilePermission OWNER_READ

List of usage examples for java.nio.file.attribute PosixFilePermission OWNER_READ

Introduction

In this page you can find the example usage for java.nio.file.attribute PosixFilePermission OWNER_READ.

Prototype

PosixFilePermission OWNER_READ

To view the source code for java.nio.file.attribute PosixFilePermission OWNER_READ.

Click Source Link

Document

Read permission, owner.

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   ww  w. ja va 2s  .co 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:org.cryptomator.cryptofs.CryptoFileSystemImpl.java

void checkAccess(CryptoPath cleartextPath, AccessMode... modes) throws IOException {
    if (fileStore.supportsFileAttributeView(PosixFileAttributeView.class)) {
        Set<PosixFilePermission> permissions = readAttributes(cleartextPath, PosixFileAttributes.class)
                .permissions();//from  www.j ava  2  s . co  m
        boolean accessGranted = true;
        for (AccessMode accessMode : modes) {
            switch (accessMode) {
            case READ:
                accessGranted &= permissions.contains(PosixFilePermission.OWNER_READ);
                break;
            case WRITE:
                accessGranted &= permissions.contains(PosixFilePermission.OWNER_WRITE);
                break;
            case EXECUTE:
                accessGranted &= permissions.contains(PosixFilePermission.OWNER_EXECUTE);
                break;
            default:
                throw new UnsupportedOperationException("AccessMode " + accessMode + " not supported.");
            }
        }
        if (!accessGranted) {
            throw new AccessDeniedException(cleartextPath.toString());
        }
    } else if (fileStore.supportsFileAttributeView(DosFileAttributeView.class)) {
        DosFileAttributes attrs = readAttributes(cleartextPath, DosFileAttributes.class);
        if (ArrayUtils.contains(modes, AccessMode.WRITE) && attrs.isReadOnly()) {
            throw new AccessDeniedException(cleartextPath.toString(), null, "read only file");
        }
    } else {
        // read attributes to check for file existence / throws IOException if file does not exist
        readAttributes(cleartextPath, BasicFileAttributes.class);
    }
}

From source file:com.facebook.buck.zip.ZipStepTest.java

@Test
public void zipMaintainsExecutablePermissions() throws IOException {
    assumeTrue(Platform.detect() != Platform.WINDOWS);

    Path parent = tmp.newFolder("zipstep");
    Path toZip = tmp.newFolder("zipdir");
    Path file = toZip.resolve("foo.sh");
    ImmutableSet<PosixFilePermission> filePermissions = ImmutableSet.of(PosixFilePermission.OWNER_READ,
            PosixFilePermission.OWNER_WRITE, PosixFilePermission.OWNER_EXECUTE, PosixFilePermission.GROUP_READ,
            PosixFilePermission.OTHERS_READ);
    Files.createFile(file, PosixFilePermissions.asFileAttribute(filePermissions));
    Path outputZip = parent.resolve("output.zip");
    ZipStep step = new ZipStep(filesystem, outputZip, ImmutableSet.of(), false,
            ZipCompressionLevel.MIN_COMPRESSION_LEVEL, Paths.get("zipdir"));
    assertEquals(0, step.execute(TestExecutionContext.newInstance()).getExitCode());

    Path destination = tmp.newFolder("output");
    Unzip.extractZipFile(outputZip, destination, Unzip.ExistingFileMode.OVERWRITE);
    assertTrue(Files.isExecutable(destination.resolve("foo.sh")));
}

From source file:org.apache.nifi.registry.bootstrap.RunNiFiRegistry.java

private synchronized void savePidProperties(final Properties pidProperties, final Logger logger)
        throws IOException {
    final String pid = pidProperties.getProperty(PID_KEY);
    if (!StringUtils.isBlank(pid)) {
        writePidFile(pid, logger);/*from   w  w  w . ja v  a  2 s  . c  o m*/
    }

    final File statusFile = getStatusFile(logger);
    if (statusFile.exists() && !statusFile.delete()) {
        logger.warn("Failed to delete {}", statusFile);
    }

    if (!statusFile.createNewFile()) {
        throw new IOException("Failed to create file " + statusFile);
    }

    try {
        final Set<PosixFilePermission> perms = new HashSet<>();
        perms.add(PosixFilePermission.OWNER_READ);
        perms.add(PosixFilePermission.OWNER_WRITE);
        Files.setPosixFilePermissions(statusFile.toPath(), perms);
    } catch (final Exception e) {
        logger.warn("Failed to set permissions so that only the owner can read status file {}; "
                + "this may allows others to have access to the key needed to communicate with NiFi Registry. "
                + "Permissions should be changed so that only the owner can read this file", statusFile);
    }

    try (final FileOutputStream fos = new FileOutputStream(statusFile)) {
        pidProperties.store(fos, null);
        fos.getFD().sync();
    }

    logger.debug("Saved Properties {} to {}", new Object[] { pidProperties, statusFile });
}

From source file:org.kitodo.filemanagement.FileManagementTest.java

private static void setFileExecutable(File file) throws IOException {
    Set<PosixFilePermission> perms = new HashSet<>();

    perms.add(PosixFilePermission.OWNER_READ);
    perms.add(PosixFilePermission.OWNER_WRITE);
    perms.add(PosixFilePermission.OWNER_EXECUTE);

    perms.add(PosixFilePermission.OTHERS_READ);
    perms.add(PosixFilePermission.OTHERS_WRITE);
    perms.add(PosixFilePermission.OTHERS_EXECUTE);

    perms.add(PosixFilePermission.GROUP_READ);
    perms.add(PosixFilePermission.GROUP_WRITE);
    perms.add(PosixFilePermission.GROUP_EXECUTE);

    Files.setPosixFilePermissions(file.toPath(), perms);
}

From source file:org.artifactory.security.crypto.CryptoHelper.java

public static void setPermissionsOnSecurityFolder(File securityFolder) throws IOException {
    // The security folder should accessible only by the owner
    if (FileSystems.getDefault().supportedFileAttributeViews().contains("posix")) {
        Files.setPosixFilePermissions(securityFolder.toPath(), EnumSet.of(PosixFilePermission.OWNER_EXECUTE,
                PosixFilePermission.OWNER_WRITE, PosixFilePermission.OWNER_READ));
    }//  ww w. j  a  v  a 2  s  . c o  m
}

From source file:org.kitodo.filemanagement.FileManagementTest.java

private static void setFileNotExecutable(File file) throws IOException {
    Set<PosixFilePermission> perms = new HashSet<>();

    perms.add(PosixFilePermission.OWNER_READ);
    perms.add(PosixFilePermission.OWNER_WRITE);

    perms.add(PosixFilePermission.OTHERS_READ);
    perms.add(PosixFilePermission.OTHERS_WRITE);

    perms.add(PosixFilePermission.GROUP_READ);
    perms.add(PosixFilePermission.GROUP_WRITE);

    Files.setPosixFilePermissions(file.toPath(), perms);
}

From source file:org.osate.atsv.integration.preparser.EngineConfigGenerator.java

private void setPermissions() {
    Set<PosixFilePermission> perms = new HashSet<>();
    perms.add(PosixFilePermission.OWNER_READ);
    perms.add(PosixFilePermission.OWNER_WRITE); // Required to allow overwriting on subsequent runs
    perms.add(PosixFilePermission.OWNER_EXECUTE);

    java.nio.file.Path parseJarPath = FileSystems.getDefault().getPath(targetDirStr + "parser.jar");
    java.nio.file.Path connectJarPath = FileSystems.getDefault().getPath(targetDirStr + "connector.jar");
    java.nio.file.Path runScriptPath = FileSystems.getDefault()
            .getPath(targetDirStr + (SystemUtils.IS_OS_WINDOWS ? "run.bat" : "run.sh"));

    try {//from   w w  w . ja  va2 s  . c  o m
        java.nio.file.Files.setPosixFilePermissions(parseJarPath, perms);
        java.nio.file.Files.setPosixFilePermissions(connectJarPath, perms);
        java.nio.file.Files.setPosixFilePermissions(runScriptPath, perms);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:info.pancancer.arch3.worker.WorkerRunnable.java

/**
 * Write the content of the job object to an INI file which will be used by the workflow.
 *
 * @param job/*from   w w w  . java2s  .  c o m*/
 *            - the job object which must contain a HashMap, which will be used to write an INI file.
 * @return A Path object pointing to the new file will be returned.
 * @throws IOException
 */
private Path writeINIFile(Job job) throws IOException {
    log.info("INI is: " + job.getIniStr());
    EnumSet<PosixFilePermission> perms = EnumSet.of(PosixFilePermission.OWNER_READ,
            PosixFilePermission.OWNER_WRITE, PosixFilePermission.OWNER_EXECUTE, PosixFilePermission.GROUP_READ,
            PosixFilePermission.GROUP_WRITE, PosixFilePermission.OTHERS_READ, PosixFilePermission.OTHERS_WRITE);
    FileAttribute<?> attrs = PosixFilePermissions.asFileAttribute(perms);
    Path pathToINI = Files.createTempFile("seqware_", ".ini", attrs);
    log.info("INI file: " + pathToINI.toString());
    try (BufferedWriter bw = new BufferedWriter(
            new OutputStreamWriter(new FileOutputStream(pathToINI.toFile()), StandardCharsets.UTF_8))) {
        bw.write(job.getIniStr());
        bw.flush();
    }
    return pathToINI;
}

From source file:org.apache.nifi.registry.bootstrap.RunNiFiRegistry.java

private synchronized void writePidFile(final String pid, final Logger logger) throws IOException {
    final File pidFile = getPidFile(logger);
    if (pidFile.exists() && !pidFile.delete()) {
        logger.warn("Failed to delete {}", pidFile);
    }/*from   w  ww  .j av a  2  s . co  m*/

    if (!pidFile.createNewFile()) {
        throw new IOException("Failed to create file " + pidFile);
    }

    try {
        final Set<PosixFilePermission> perms = new HashSet<>();
        perms.add(PosixFilePermission.OWNER_WRITE);
        perms.add(PosixFilePermission.OWNER_READ);
        perms.add(PosixFilePermission.GROUP_READ);
        perms.add(PosixFilePermission.OTHERS_READ);
        Files.setPosixFilePermissions(pidFile.toPath(), perms);
    } catch (final Exception e) {
        logger.warn("Failed to set permissions so that only the owner can read pid file {}; "
                + "this may allows others to have access to the key needed to communicate with NiFi Registry. "
                + "Permissions should be changed so that only the owner can read this file", pidFile);
    }

    try (final FileOutputStream fos = new FileOutputStream(pidFile)) {
        fos.write(pid.getBytes(StandardCharsets.UTF_8));
        fos.getFD().sync();
    }

    logger.debug("Saved Pid {} to {}", new Object[] { pid, pidFile });
}