Class PosixPermissions

java.lang.Object
com.sshtools.common.sftp.PosixPermissions

public final class PosixPermissions extends Object
PosixPermissions and PosixPermissions.PosixPermissionsBuilder allow querying and creation of an immutable set of posix file permissions. Natively, this is a stored as a set of PosixFilePermission, but may be expressed and parsed in a variety of different ways, such as octal strings, "file mode strings", raw unsigned values. A mode may also be created by combining user, group and other elements.

Note that these classes only deal with the basic user, group; and other file access mode elements e.g. rw-r--r--. Other common Posix indicators such as the 'd' for directory prefix, 's' for Set UID indicators, 'l' for links etc, are not parsed or generated here. Neither are the extended SFTP bitmask flags.

This makes use of the existing core Java classes PosixFilePermission and PosixFilePermissions, adding additional functionality and a builder pattern for creation.

For example, to set the full set of permissions in one go :-

 sftp.chmod(PosixPermissionsBuilder.create().
                fromFileModeString("rw-rw-rw-").
                build(), "/path/to/file");
 

Or to retrieve permission, remove write permissions and set them back :-

 sftp.chmod(PosixPermissionsBuilder.create().
                fromPosixPermissions(sftp.stat("/path/to/file").getPosixPermissions).
                withoutWritePermissions().
                build(), "/path/to/file");
 
  • Field Details

    • EMPTY

      public static final PosixPermissions EMPTY
      Empty set of permissions (has a bitmask value of zero)
  • Method Details

    • toMask

      public static int toMask(PosixFilePermission permission)
      Get the bitmask flag value for a given permission.
      Parameters:
      permission - permission
      Returns:
      bitmask flag value
    • asFileModesString

      public String asFileModesString()
      Get the String representation of these permissions. It is guaranteed that the returned String can be parsed by the PosixPermissions.PosixPermissionsBuilder.fromFileModeString(String) method.
      Returns:
      the string representation of the permission set
    • asInt

      public int asInt()
      Get the mode as a signed integer. Note, you would never usually want to use this value without further manipulation to account for the sign bit. It is recommended you use asLong() or asUInt32() which guarantee the value will never be negative.
      Returns:
      permissions as unsigned long
    • asLong

      public long asLong()
      Get the mode as an unsigned long. Note, the value will never be greater than the 32 bits needs to store a mode natively.
      Returns:
      permissions as unsigned long
    • asMaskString

      public String asMaskString()
      Return the UNIX style mode mask. This will be in the same format as that which may be parsed by PosixPermissions.PosixPermissionsBuilder.fromMaskString(String).
      Returns:
      mask string
    • asPermissions

      public Set<PosixFilePermission> asPermissions()
      Get the set of individual permissions.
      Returns:
      permissions
    • asUInt32

      public com.sshtools.common.util.UnsignedInteger32 asUInt32()
      Get the mode as an unsigned 32 bit integer.
      Returns:
      permissions as unsigned 32 bit integer
    • has

      public boolean has(PosixFilePermission... permissions)
      Get if these permissions contain all of the provided permissions.
      Parameters:
      perms - permissions to test for
      Returns:
      true if all permissions are contained
    • isEmpty

      public boolean isEmpty()
      Get if this is an empty set of permissions.
      Returns:
      empty
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object