Example usage for java.nio.file Files setPosixFilePermissions

List of usage examples for java.nio.file Files setPosixFilePermissions

Introduction

In this page you can find the example usage for java.nio.file Files setPosixFilePermissions.

Prototype

public static Path setPosixFilePermissions(Path path, Set<PosixFilePermission> perms) throws IOException 

Source Link

Document

Sets a file's POSIX permissions.

Usage

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

private void setPermissionsTo700() {
    try {/*from   w w  w . ja  v  a2s. c  om*/
        Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
        perms.add(PosixFilePermission.OWNER_READ);
        perms.add(PosixFilePermission.OWNER_WRITE);
        perms.add(PosixFilePermission.OWNER_EXECUTE);
        Files.setPosixFilePermissions(testPermissionsFile.toPath(), perms);
    } catch (Exception e) {
        Assume.assumeNoException("skip test on filesystems that do not support POSIX file permissions", e);
    }
}

From source file:com.github.blindpirate.gogradle.util.IOUtils.java

public static void chmodAddX(Path filePath) {
    try {//from   w w  w  .ja  va  2s.  c  o  m
        if (Os.getHostOs() != Os.WINDOWS) {
            Files.setPosixFilePermissions(filePath, PosixFilePermissions.fromString("rwx------"));
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}

From source file:org.kitodo.services.command.CommandServiceTest.java

public static void setFileExecuteable(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.phoenicis.tools.archive.Tar.java

/**
 * Uncompress a tar/*from w  w  w  .j  a v a2 s  .  co m*/
 *
 * @param countingInputStream
 *            to count the number of byte extracted
 * @param outputDir
 *            The directory where files should be extracted
 * @return A list of extracted files
 * @throws ArchiveException
 *             if the process fails
 */
private List<File> uncompress(final InputStream inputStream, CountingInputStream countingInputStream,
        final File outputDir, long finalSize, Consumer<ProgressEntity> stateCallback) {
    final List<File> uncompressedFiles = new LinkedList<>();
    try (ArchiveInputStream debInputStream = new ArchiveStreamFactory().createArchiveInputStream("tar",
            inputStream)) {
        TarArchiveEntry entry;
        while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) {
            final File outputFile = new File(outputDir, entry.getName());
            if (entry.isDirectory()) {
                LOGGER.info(String.format("Attempting to write output directory %s.",
                        outputFile.getAbsolutePath()));

                if (!outputFile.exists()) {
                    LOGGER.info(String.format("Attempting to createPrefix output directory %s.",
                            outputFile.getAbsolutePath()));
                    Files.createDirectories(outputFile.toPath());
                }
            } else {
                LOGGER.info(String.format("Creating output file %s (%s).", outputFile.getAbsolutePath(),
                        entry.getMode()));

                if (entry.isSymbolicLink()) {
                    Files.createSymbolicLink(Paths.get(outputFile.getAbsolutePath()),
                            Paths.get(entry.getLinkName()));
                } else {
                    try (final OutputStream outputFileStream = new FileOutputStream(outputFile)) {
                        IOUtils.copy(debInputStream, outputFileStream);

                        Files.setPosixFilePermissions(Paths.get(outputFile.getPath()),
                                fileUtilities.octToPosixFilePermission(entry.getMode()));
                    }
                }

            }
            uncompressedFiles.add(outputFile);

            stateCallback.accept(new ProgressEntity.Builder()
                    .withPercent((double) countingInputStream.getCount() / (double) finalSize * (double) 100)
                    .withProgressText("Extracting " + outputFile.getName()).build());

        }
        return uncompressedFiles;
    } catch (IOException | org.apache.commons.compress.archivers.ArchiveException e) {
        throw new ArchiveException("Unable to extract the file", e);
    }
}

From source file:com.spectralogic.ds3client.metadata.MetadataReceivedListenerImpl_Test.java

@Test
public void testGettingMetadataFailureHandler() throws IOException, InterruptedException {
    Assume.assumeFalse(Platform.isWindows());

    try {//from ww w  .j a v  a  2  s .c o  m
        final String tempPathPrefix = null;
        final Path tempDirectory = Files.createTempDirectory(Paths.get("."), tempPathPrefix);

        final String fileName = "Gracie.txt";

        final Path filePath = Files.createFile(Paths.get(tempDirectory.toString(), fileName));

        try {
            // set permissions
            if (!Platform.isWindows()) {
                final PosixFileAttributes attributes = Files.readAttributes(filePath,
                        PosixFileAttributes.class);
                final Set<PosixFilePermission> permissions = attributes.permissions();
                permissions.clear();
                permissions.add(PosixFilePermission.OWNER_READ);
                permissions.add(PosixFilePermission.OWNER_WRITE);
                Files.setPosixFilePermissions(filePath, permissions);
            }

            // get permissions
            final ImmutableMap.Builder<String, Path> fileMapper = ImmutableMap.builder();
            fileMapper.put(filePath.toString(), filePath);
            final Map<String, String> metadataFromFile = new MetadataAccessImpl(fileMapper.build())
                    .getMetadataValue(filePath.toString());

            FileUtils.deleteDirectory(tempDirectory.toFile());

            // put old permissions back
            final Metadata metadata = new MetadataImpl(new MockedHeadersReturningKeys(metadataFromFile));

            final AtomicInteger numTimesFailureHandlerCalled = new AtomicInteger(0);

            new MetadataReceivedListenerImpl(tempDirectory.toString(), new FailureEventListener() {
                @Override
                public void onFailure(final FailureEvent failureEvent) {
                    numTimesFailureHandlerCalled.incrementAndGet();
                    assertEquals(FailureEvent.FailureActivity.RestoringMetadata, failureEvent.doingWhat());
                }
            }, "localhost").metadataReceived(fileName, metadata);

            assertEquals(1, numTimesFailureHandlerCalled.get());
        } finally {
            FileUtils.deleteDirectory(tempDirectory.toFile());
        }
    } catch (final Throwable t) {
        fail("Throwing exceptions from metadata est verbotten");
    }
}

From source file:org.eclipse.cbi.maven.plugins.macsigner.SignMojo.java

/**
 * Decompresses zip files./*from w w w  .  j av a 2 s. c o  m*/
 * @param zipFile           The zip file to decompress.
 * @throws IOException
 * @throws MojoExecutionException
 */
private static void unZip(File zipFile, File output_dir) throws IOException, MojoExecutionException {

    ZipArchiveInputStream zis = new ZipArchiveInputStream(new FileInputStream(zipFile));
    ZipArchiveEntry ze;
    String name, parent;
    try {
        ze = zis.getNextZipEntry();
        // check for at least one zip entry
        if (ze == null) {
            throw new MojoExecutionException("Could not decompress " + zipFile);
        }

        while (ze != null) {
            name = ze.getName();

            //make directories
            if (ze.isDirectory()) {
                mkdirs(output_dir, name);
            } else {
                parent = getParentDirAbsolutePath(name);
                mkdirs(output_dir, parent);

                File outFile = new File(output_dir, name);
                outFile.createNewFile();

                // check for match in executable list
                if (executableFiles.contains(name)) {
                    Files.setPosixFilePermissions(outFile.toPath(),
                            PosixFilePermissions.fromString("rwxr-x---"));
                }

                FileOutputStream fos = new FileOutputStream(outFile);

                copyInputStreamToOutputStream(zis, fos);
                fos.close();
            }
            ze = zis.getNextZipEntry();
        }
    } finally {
        zis.close();
    }
}

From source file:com.ibm.ecm.extension.aspera.AsperaPlugin.java

private Path makeFileExecutable(final Path file) throws AsperaPluginException {
    final Set<PosixFilePermission> perms = new HashSet<>();
    perms.add(PosixFilePermission.OWNER_READ);
    perms.add(PosixFilePermission.OWNER_WRITE);
    perms.add(PosixFilePermission.OWNER_EXECUTE);
    perms.add(PosixFilePermission.GROUP_READ);
    perms.add(PosixFilePermission.GROUP_EXECUTE);
    try {//  w  ww  . j a v a 2 s  .  com
        Files.setPosixFilePermissions(file, perms);
    } catch (final IOException e) {
        throw new AsperaPluginException("Failed to make the file executable: " + file, e);
    }

    return file;
}

From source file:org.kitodo.services.command.CommandServiceTest.java

public static void setFileNotExecuteable(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.esa.s2tbx.dataio.gdal.GDALInstaller.java

private static void setExecutablePermissions(Path executablePathName) {
    if (IS_OS_UNIX) {
        Set<PosixFilePermission> permissions = new HashSet<>(Arrays.asList(PosixFilePermission.OWNER_READ,
                PosixFilePermission.OWNER_WRITE, PosixFilePermission.OWNER_EXECUTE,
                PosixFilePermission.GROUP_READ, PosixFilePermission.GROUP_EXECUTE,
                PosixFilePermission.OTHERS_READ, PosixFilePermission.OTHERS_EXECUTE));
        try {/*from  w ww .  ja  va  2s  .c  om*/
            Files.setPosixFilePermissions(executablePathName, permissions);
        } catch (IOException e) {
            // can't set the permissions for this file, eg. the file was installed as root
            // send a warning message, user will have to do that by hand.
            logger.log(Level.SEVERE,
                    "Can't set execution permissions for executable " + executablePathName.toString()
                            + ". If required, please ask an authorised user to make the file executable.",
                    e);
        }
    }
}

From source file:io.specto.hoverfly.junit.HoverflyRule.java

private Path extractBinary(final String binaryName) throws IOException, URISyntaxException {
    final URI sourceHoverflyUrl = findResourceOnClasspath(binaryName);
    final Path temporaryHoverflyPath = Files.createTempFile(binaryName, "");
    LOGGER.info("Storing binary in temporary directory " + temporaryHoverflyPath);
    final File temporaryHoverflyFile = temporaryHoverflyPath.toFile();
    FileUtils.copyURLToFile(sourceHoverflyUrl.toURL(), temporaryHoverflyFile);
    if (SystemUtils.IS_OS_WINDOWS) {
        temporaryHoverflyFile.setExecutable(true);
        temporaryHoverflyFile.setReadable(true);
        temporaryHoverflyFile.setWritable(true);
    } else {/*from  w ww.  j a va 2 s .  com*/
        Files.setPosixFilePermissions(temporaryHoverflyPath, new HashSet<>(asList(OWNER_EXECUTE, OWNER_READ)));
    }

    return temporaryHoverflyPath;
}