Java Posix givePerm(File f, Set perms)

Here you can find the source of givePerm(File f, Set perms)

Description

Sets specified permissions to a single file or directory.

License

Open Source License

Parameter

Parameter Description
f The file/directory to apply permissions on.
perms The POSIX permissions as string

Return

True if file permissions were applied

Declaration

private static boolean givePerm(File f, Set<PosixFilePermission> perms) 

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.util.Set;

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

    /**/*from   w w w .  j  a  v  a  2  s . c  om*/
     * 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. getPosixFileAttributes(@Nonnull File file)
  2. getPosixFilePermissions(int mode)
  3. getPosixPermissions(File forFile)
  4. getPosixPermissions(int fileMode)
  5. getPosixPermissionsAsInt( Set permissionSet)
  6. givePerms(File f, Set perms, boolean recursive)
  7. intFilePermissions(Set permissions)
  8. mapValue(PosixFilePermission p)
  9. posixFilePermissions(int mode)