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

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

Introduction

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

Prototype

PosixFilePermission OWNER_EXECUTE

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

Click Source Link

Document

Execute/search 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  w ww .j  ava2 s  .c om

    // 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   w w  w.  j av a 2  s  .c om
        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:com.facebook.buck.util.unarchive.UntarTest.java

private void extractsFiles(ArchiveFormat format, Optional<Boolean> writeSymlinksLast) throws IOException {
    ImmutableList<Path> expectedPaths = ImmutableList.of(getDestPath("root", "echo.sh"),
            getDestPath("root", "alternative", "Main.java"), getDestPath("root", "alternative", "Link.java"),
            getDestPath("root", "src", "com", "facebook", "buck", "Main.java"),
            getDestPath("root_sibling", "Other.java"));

    ImmutableList.Builder<Path> expectedDirsBuilder = ImmutableList.builder();
    expectedDirsBuilder.add(OUTPUT_SUBDIR);
    expectedDirsBuilder.add(getDestPath("root"));
    expectedDirsBuilder.add(getDestPath("root", "alternative"));
    expectedDirsBuilder.add(getDestPath("root", "src"));
    expectedDirsBuilder.add(getDestPath("root", "src", "com"));
    expectedDirsBuilder.add(getDestPath("root", "src", "com", "facebook"));
    expectedDirsBuilder.add(getDestPath("root", "src", "com", "facebook", "buck"));
    expectedDirsBuilder.add(getDestPath("root_sibling"));
    expectedDirsBuilder.add(getDestPath("root", "empty_dir"));
    ImmutableList<Path> expectedDirs = expectedDirsBuilder.build();

    Path archivePath = getTestFilePath(format.getExtension());
    Untar unarchiver = (Untar) format.getUnarchiver();
    ImmutableSet<Path> unarchivedFiles;
    if (writeSymlinksLast.isPresent()) {
        unarchivedFiles = unarchiver.extractArchive(archivePath, filesystem, Paths.get("output_dir"),
                Optional.empty(), ExistingFileMode.OVERWRITE_AND_CLEAN_DIRECTORIES, PatternsMatcher.EMPTY,
                writeSymlinksLast.get());
    } else {/*from w ww. j  a v  a 2s . c  o  m*/
        unarchivedFiles = unarchiver.extractArchive(archivePath, filesystem, Paths.get("output_dir"),
                Optional.empty(), ExistingFileMode.OVERWRITE_AND_CLEAN_DIRECTORIES);
    }

    Assert.assertThat(unarchivedFiles, Matchers.containsInAnyOrder(expectedPaths.toArray()));
    Assert.assertEquals(expectedPaths.size(), unarchivedFiles.size());

    // Make sure we wrote the files
    assertOutputFileExists(expectedPaths.get(0), echoDotSh);
    assertOutputSymlinkExists(expectedPaths.get(1), Paths.get("Link.java"), mainDotJava);
    assertOutputSymlinkExists(expectedPaths.get(2),
            Paths.get("..", "src", "com", "facebook", "buck", "Main.java"), mainDotJava);
    assertOutputFileExists(expectedPaths.get(3), mainDotJava);
    assertOutputFileExists(expectedPaths.get(4), otherDotJava);

    // Make sure we make the dirs
    for (Path dir : expectedDirs) {
        assertOutputDirExists(dir);
        // Dest dir is created by buck, doesn't come from the archive
        if (!dir.equals(OUTPUT_SUBDIR)) {
            assertModifiedTime(dir);
        }
    }

    // Make sure that we set modified time and execute bit properly
    assertModifiedTime(expectedPaths);
    assertExecutable(expectedPaths.get(0), true);
    if (tmpFolder.getRoot().getFileSystem().supportedFileAttributeViews().contains("posix")) {
        Path executablePath = tmpFolder.getRoot().resolve(expectedPaths.get(0));
        Assert.assertThat(Files.getPosixFilePermissions(executablePath),
                Matchers.hasItems(PosixFilePermission.OWNER_EXECUTE, PosixFilePermission.OTHERS_EXECUTE,
                        PosixFilePermission.GROUP_EXECUTE));
    }
    assertExecutable(expectedPaths.subList(1, expectedPaths.size()), false);
}

From source file:com.facebook.buck.util.unarchive.Untar.java

/** Sets the modification time and the execution bit on a file */
private void setAttributes(ProjectFilesystem filesystem, Path path, TarArchiveEntry entry) throws IOException {
    Path filePath = filesystem.getRootPath().resolve(path);
    File file = filePath.toFile();
    file.setLastModified(entry.getModTime().getTime());
    Set<PosixFilePermission> posixPermissions = MorePosixFilePermissions.fromMode(entry.getMode());
    if (posixPermissions.contains(PosixFilePermission.OWNER_EXECUTE)) {
        // setting posix file permissions on a symlink does not work, so use File API instead
        if (entry.isSymbolicLink()) {
            file.setExecutable(true, true);
        } else {//from  w  w  w.j  av a 2 s  . c  om
            MostFiles.makeExecutable(filePath);
        }
    }
}

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));
    }//from  ww  w.  j  a v a  2s  . c  om
}

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  .j  av  a 2 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/* w  ww .  j a v  a 2s. 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:com.diffplug.gradle.FileMisc.java

/** Returns true if any of the bits contain the executable permission. */
public static boolean containsExecutablePermission(Set<PosixFilePermission> permissions) {
    return permissions.contains(PosixFilePermission.OWNER_EXECUTE)
            && permissions.contains(PosixFilePermission.GROUP_EXECUTE)
            && permissions.contains(PosixFilePermission.OTHERS_EXECUTE);
}