Java Posix givePerms(File f, Set perms, boolean recursive)

Here you can find the source of givePerms(File f, Set perms, boolean recursive)

Description

Sets permissions to file or directory given.

License

Open Source License

Parameter

Parameter Description
f The file or directory
perms The set of POSIX permissions
recursive Change permissions recursively for directory

Return

True if all files/directory were set to permissions

Declaration

public static boolean givePerms(File f, Set<PosixFilePermission> perms, boolean recursive) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.Set;

public class Main {
    private static String OS = System.getProperty("os.name").toLowerCase();

    /**//from ww w.  j  ava  2s.  co m
     * Sets permissions to file or directory given. Fails silently and returns false if an exception occurred at some
     * point.
     * 
     * @param f
     *            The file or directory
     * @param perms
     *            The set of POSIX permissions
     * @param recursive
     *            Change permissions recursively for directory
     * @return True if all files/directory were set to permissions
     */
    public static boolean givePerms(File f, Set<PosixFilePermission> perms, boolean recursive) {
        boolean success = givePerm(f, perms);
        if (f.isDirectory() && recursive) {
            File[] files = f.listFiles();
            for (File file : files) {
                if (!givePerms(file, perms, recursive)) {
                    success = false;
                }
            }
        }
        return success;
    }

    /**
     * Sets permissions to file or directory given. Fails silently and returns false if an exception occurred at some
     * point.
     * 
     * @param f
     *            The file or directory
     * @param perms
     *            The POSIX permissions as string
     * @param recursive
     *            Change permissions recursively for directory
     * @return True if all files/directory were set to permissions
     */
    public static boolean givePerms(File f, String perms, boolean recursive) {
        return givePerms(f, PosixFilePermissions.fromString(perms), recursive);
    }

    /**
     * Sets permissions rwxrwxrwx to file or directory given. Fails silently and returns false if an exception occurred
     * at some point.
     * 
     * @param f
     *            The file or directory
     * @param recursive
     *            Change permissions recursively for directory
     * @return True if all files/directory were set to permissions
     */
    public static boolean givePerms(File f, boolean recursive) {
        return givePerms(f, "rwxrwxrwx", recursive);
    }

    /**
     * Sets specified permissions to a single file or directory.
     * 
     * @param f
     *            The file/directory to apply permissions on.
     * @param perms
     *            The POSIX permissions as string
     * @return True if file permissions were applied
     */
    private static boolean givePerm(File f, Set<PosixFilePermission> perms) {
        try {
            Path p = Paths.get(f.toURI());
            if (OS.indexOf("win") >= 0) {
                // just set writable on windows I guess...
                p.toFile().setWritable(true);
            } else {
                Files.setPosixFilePermissions(p, perms);
            }
        } catch (IOException e) {
            System.err.printf("Could not set permissions '%s' to %s\n", perms, f.toString());
            return false;
        }
        return true;
    }
}

Related

  1. getPosixFilePermissions(int mode)
  2. getPosixPermissions(File forFile)
  3. getPosixPermissions(int fileMode)
  4. getPosixPermissionsAsInt( Set permissionSet)
  5. givePerm(File f, Set perms)
  6. intFilePermissions(Set permissions)
  7. mapValue(PosixFilePermission p)
  8. posixFilePermissions(int mode)
  9. setPermissionsToFile(File f, Collection perms)