Class FilePermission


  • @PublicAPI(stability=VOLATILE,
               mayInstantiate=true,
               mayExtend=false,
               mayInvoke=true)
    public class FilePermission
    extends Object
    This class provides a mechanism for setting file permissions in a more abstract manner than is provided by the underlying operating system and/or filesystem. It uses a traditional UNIX-style rwx/ugo representation for the permissions and converts them as necessary to the scheme used by the underlying platform. It does not provide any mechanism for getting file permissions, nor does it provide any way of dealing with file ownership or ACLs.
    • Constructor Detail

      • FilePermission

        public FilePermission​(int encodedPermission)
        Creates a new file permission object with the provided encoded representation.
        Parameters:
        encodedPermission - The encoded representation for this file permission.
    • Method Detail

      • isOwnerReadable

        public boolean isOwnerReadable()
        Indicates whether this file permission includes the owner read permission.
        Returns:
        true if this file permission includes the owner read permission, or false if not.
      • isOwnerWritable

        public boolean isOwnerWritable()
        Indicates whether this file permission includes the owner write permission.
        Returns:
        true if this file permission includes the owner write permission, or false if not.
      • isOwnerExecutable

        public boolean isOwnerExecutable()
        Indicates whether this file permission includes the owner execute permission.
        Returns:
        true if this file permission includes the owner execute permission, or false if not.
      • isGroupReadable

        public boolean isGroupReadable()
        Indicates whether this file permission includes the group read permission.
        Returns:
        true if this file permission includes the group read permission, or false if not.
      • isGroupWritable

        public boolean isGroupWritable()
        Indicates whether this file permission includes the group write permission.
        Returns:
        true if this file permission includes the group write permission, or false if not.
      • isGroupExecutable

        public boolean isGroupExecutable()
        Indicates whether this file permission includes the group execute permission.
        Returns:
        true if this file permission includes the group execute permission, or false if not.
      • isOtherReadable

        public boolean isOtherReadable()
        Indicates whether this file permission includes the other read permission.
        Returns:
        true if this file permission includes the other read permission, or false if not.
      • isOtherWritable

        public boolean isOtherWritable()
        Indicates whether this file permission includes the other write permission.
        Returns:
        true if this file permission includes the other write permission, or false if not.
      • isOtherExecutable

        public boolean isOtherExecutable()
        Indicates whether this file permission includes the other execute permission.
        Returns:
        true if this file permission includes the other execute permission, or false if not.
      • setPermissions

        public static boolean setPermissions​(Path f,
                                             FilePermission p)
                                      throws FileNotFoundException,
                                             LdapException
        Attempts to set the given permissions on the specified file. If the underlying platform does not allow the full level of granularity specified in the permissions, then an attempt will be made to set them as closely as possible to the provided permissions, erring on the side of security.
        Parameters:
        f - The file to which the permissions should be applied.
        p - The permissions to apply to the file.
        Returns:
        true if the permissions (or the nearest equivalent) were successfully applied to the specified file, or false if was not possible to set the permissions on the current platform.
        Throws:
        FileNotFoundException - If the specified file does not exist.
        LdapException - If a problem occurs while trying to set the file permissions.
      • setSafePermissions

        public static boolean setSafePermissions​(Path f,
                                                 Integer p)
        Attempts to set the given permissions on the specified file. If the underlying platform does not allow the full level of granularity specified in the permissions, then an attempt will be made to set them as closely as possible to the provided permissions, erring on the side of security.
        Parameters:
        f - The file to which the permissions should be applied.
        p - The permissions to apply to the file.
        Returns:
        true if the permissions (or the nearest equivalent) were successfully applied to the specified file, or false if was not possible to set the permissions on the current platform. The file is known to exist therefore there is no need for exists() checks.
      • decodeUNIXMode

        public static FilePermission decodeUNIXMode​(String modeString)
                                             throws LdapException
        Decodes the provided string as a UNIX mode and retrieves the corresponding file permission. The mode string must contain three digits between zero and seven.
        Parameters:
        modeString - The string representation of the UNIX mode to decode.
        Returns:
        The file permission that is equivalent to the given UNIX mode.
        Throws:
        LdapException - If the provided string is not a valid three-digit UNIX mode.
      • toString

        public String toString()
        Retrieves a string representation of this file permission.
        Overrides:
        toString in class Object
        Returns:
        A string representation of this file permission.