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

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

Introduction

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

Prototype

void setPermissions(Set<PosixFilePermission> perms) throws IOException;

Source Link

Document

Updates the file permissions.

Usage

From source file:Main.java

public static void updatePermissions(PosixFileAttributeView posixView) throws Exception {
    Set<PosixFilePermission> permissions = EnumSet.of(PosixFilePermission.OWNER_READ,
            PosixFilePermission.OWNER_WRITE, PosixFilePermission.GROUP_READ);
    posixView.setPermissions(permissions);
    System.out.println("Permissions set successfully.");
}

From source file:Test.java

private static void removePermission(Path path, PosixFilePermission permission) throws Exception {
    System.out.println("\nRemoving permission for " + path.getFileName());
    PosixFileAttributeView view = Files.getFileAttributeView(path, PosixFileAttributeView.class);

    PosixFileAttributes attributes = view.readAttributes();

    Set<PosixFilePermission> permissions = attributes.permissions();
    permissions.remove(permission);//from  w w w. ja v a2s . c o m

    view.setPermissions(permissions);
    System.out.println();
}

From source file:com.arturmkrtchyan.kafka.util.TarUnpacker.java

protected void setPermissions(final int mode, final Path path) throws IOException {
    final PosixFileAttributeView attributeView = Files.getFileAttributeView(path, PosixFileAttributeView.class);
    if (attributeView != null) {
        attributeView.setPermissions(PosixFilePermissionConverter.convertToPermissionsSet(mode));
    }/*  w  ww.j a va 2  s. co m*/
}

From source file:com.google.cloud.tools.managedcloudsdk.install.TarGzExtractorProvider.java

@Override
public void extract(Path archive, Path destination, ProgressListener progressListener) throws IOException {

    progressListener.start("Extracting archive: " + archive.getFileName(), ProgressListener.UNKNOWN);

    String canonicalDestination = destination.toFile().getCanonicalPath();

    GzipCompressorInputStream gzipIn = new GzipCompressorInputStream(Files.newInputStream(archive));
    try (TarArchiveInputStream in = new TarArchiveInputStream(gzipIn)) {
        TarArchiveEntry entry;// ww  w .j  av  a2  s  . com
        while ((entry = in.getNextTarEntry()) != null) {
            Path entryTarget = destination.resolve(entry.getName());

            String canonicalTarget = entryTarget.toFile().getCanonicalPath();
            if (!canonicalTarget.startsWith(canonicalDestination + File.separator)) {
                throw new IOException("Blocked unzipping files outside destination: " + entry.getName());
            }

            progressListener.update(1);
            logger.fine(entryTarget.toString());

            if (entry.isDirectory()) {
                if (!Files.exists(entryTarget)) {
                    Files.createDirectories(entryTarget);
                }
            } else if (entry.isFile()) {
                if (!Files.exists(entryTarget.getParent())) {
                    Files.createDirectories(entryTarget.getParent());
                }
                try (OutputStream out = new BufferedOutputStream(Files.newOutputStream(entryTarget))) {
                    IOUtils.copy(in, out);
                    PosixFileAttributeView attributeView = Files.getFileAttributeView(entryTarget,
                            PosixFileAttributeView.class);
                    if (attributeView != null) {
                        attributeView.setPermissions(PosixUtil.getPosixFilePermissions(entry.getMode()));
                    }
                }
            } else {
                // we don't know what kind of entry this is (we only process directories and files).
                logger.warning("Skipping entry (unknown type): " + entry.getName());
            }
        }
        progressListener.done();
    }
}

From source file:com.google.cloud.tools.managedcloudsdk.install.ZipExtractorProvider.java

@Override
public void extract(Path archive, Path destination, ProgressListener progressListener) throws IOException {

    progressListener.start("Extracting archive: " + archive.getFileName(), ProgressListener.UNKNOWN);

    String canonicalDestination = destination.toFile().getCanonicalPath();

    // Use ZipFile instead of ZipArchiveInputStream so that we can obtain file permissions
    // on unix-like systems via getUnixMode(). ZipArchiveInputStream doesn't have access to
    // all the zip file data and will return "0" for any call to getUnixMode().
    try (ZipFile zipFile = new ZipFile(archive.toFile())) {
        // TextProgressBar progressBar = textBarFactory.newProgressBar(messageListener, count);
        Enumeration<ZipArchiveEntry> zipEntries = zipFile.getEntries();
        while (zipEntries.hasMoreElements()) {
            ZipArchiveEntry entry = zipEntries.nextElement();
            Path entryTarget = destination.resolve(entry.getName());

            String canonicalTarget = entryTarget.toFile().getCanonicalPath();
            if (!canonicalTarget.startsWith(canonicalDestination + File.separator)) {
                throw new IOException("Blocked unzipping files outside destination: " + entry.getName());
            }//ww w  .  j a v  a2 s. c  om

            progressListener.update(1);
            logger.fine(entryTarget.toString());

            if (entry.isDirectory()) {
                if (!Files.exists(entryTarget)) {
                    Files.createDirectories(entryTarget);
                }
            } else {
                if (!Files.exists(entryTarget.getParent())) {
                    Files.createDirectories(entryTarget.getParent());
                }
                try (OutputStream out = new BufferedOutputStream(Files.newOutputStream(entryTarget))) {
                    try (InputStream in = zipFile.getInputStream(entry)) {
                        IOUtils.copy(in, out);
                        PosixFileAttributeView attributeView = Files.getFileAttributeView(entryTarget,
                                PosixFileAttributeView.class);
                        if (attributeView != null) {
                            attributeView
                                    .setPermissions(PosixUtil.getPosixFilePermissions(entry.getUnixMode()));
                        }
                    }
                }
            }
        }
    }
    progressListener.done();
}

From source file:io.jmnarloch.cd.go.plugin.gradle.GradleTaskConfigParser.java

/**
 * Adds the executable file permission./*from  w  w  w . ja v  a 2s .co  m*/
 *
 * @param file the path to the file
 */
private void addExecutablePermission(String file) {
    final Path path = Paths.get(file);
    if (Files.exists(path)) {
        try {
            PosixFileAttributeView attr = Files.getFileAttributeView(path, PosixFileAttributeView.class);
            Set<PosixFilePermission> permissions = attr.readAttributes().permissions();
            if (permissions.add(PosixFilePermission.OWNER_EXECUTE)) {
                logger.info(String.format("Added +x permission to file: %s", file));
            }
            attr.setPermissions(permissions);
        } catch (IOException e) {
            logger.error(String.format("Failed to add the executable permissions to file: %s", file));
        }
    }
}