Class SftpHelper

java.lang.Object
org.apache.sshd.sftp.common.SftpHelper

public final class SftpHelper extends Object
  • Field Details

    • DEFAULT_SUBSTATUS_MESSAGE

      public static final Map<Integer,String> DEFAULT_SUBSTATUS_MESSAGE
    • UNIX_PERMISSIONS_START

      private static final Pattern UNIX_PERMISSIONS_START
  • Constructor Details

    • SftpHelper

      private SftpHelper()
  • Method Details

    • getEndOfFileIndicatorValue

      public static Boolean getEndOfFileIndicatorValue(Buffer buffer, int version)
      Retrieves the end-of-file indicator for SSH_FXP_DATA responses, provided the version is at least 6, and the buffer has enough available data
      Parameters:
      buffer - The Buffer to retrieve the data from
      version - The SFTP version being used
      Returns:
      The indicator value - null if none retrieved
      See Also:
    • getEndOfListIndicatorValue

      public static Boolean getEndOfListIndicatorValue(Buffer buffer, int version)
      Retrieves the end-of-list indicator for SSH_FXP_NAME responses, provided the version is at least 6, and the buffer has enough available data
      Parameters:
      buffer - The Buffer to retrieve the data from
      version - The SFTP version being used
      Returns:
      The indicator value - null if none retrieved
      See Also:
    • indicateEndOfNamesList

      public static Boolean indicateEndOfNamesList(Buffer buffer, int version, PropertyResolver resolver)
      Appends the end-of-list=TRUE indicator for SSH_FXP_NAME responses, provided the version is at least 6 and the feature is enabled
      Parameters:
      buffer - The Buffer to append the indicator
      version - The SFTP version being used
      resolver - The PropertyResolver to query whether to enable the feature
      Returns:
      The actual indicator value used - null if none appended
      See Also:
    • indicateEndOfNamesList

      public static Boolean indicateEndOfNamesList(Buffer buffer, int version, PropertyResolver resolver, boolean indicatorValue)
      Appends the end-of-list indicator for SSH_FXP_NAME responses, provided the version is at least 6, the feature is enabled and the indicator value is not null
      Parameters:
      buffer - The Buffer to append the indicator
      version - The SFTP version being used
      resolver - The PropertyResolver to query whether to enable the feature
      indicatorValue - The indicator value - null means don't append the indicator
      Returns:
      The actual indicator value used - null if none appended
      See Also:
    • writeAttrs

      public static <B extends Buffer> B writeAttrs(B buffer, int version, Map<String,?> attributes)
      Writes a file / folder's attributes to a buffer
      Type Parameters:
      B - Type of Buffer being updated
      Parameters:
      buffer - The target buffer instance
      version - The output encoding version
      attributes - The Map of attributes
      Returns:
      The updated buffer
      See Also:
    • writeAttrsV3

      public static <B extends Buffer> B writeAttrsV3(B buffer, int version, Map<String,?> attributes)
      Writes the retrieved file / directory attributes in V3 format
      Type Parameters:
      B - Type of Buffer being updated
      Parameters:
      buffer - The target buffer instance
      version - The actual version - must be SftpConstants.SFTP_V3
      attributes - The Map of attributes
      Returns:
      The updated buffer
    • writeAttrsV4

      public static <B extends Buffer> B writeAttrsV4(B buffer, int version, Map<String,?> attributes)
      Writes the retrieved file / directory attributes in V4+ format
      Type Parameters:
      B - Type of Buffer being updated
      Parameters:
      buffer - The target buffer instance
      version - The actual version - must be at least SftpConstants.SFTP_V4
      attributes - The Map of attributes
      Returns:
      The updated buffer
    • writeAttributes

      public static <B extends Buffer> B writeAttributes(B buffer, SftpClient.Attributes attributes, int sftpVersion)
    • getBool

      public static boolean getBool(Boolean bool)
      Parameters:
      bool - The Boolean value
      Returns:
      true it the argument is non-null and its Boolean.booleanValue() is true
    • attributesToPermissions

      public static int attributesToPermissions(boolean isReg, boolean isDir, boolean isLnk, Collection<PosixFilePermission> perms)
      Converts a file / folder's attributes into a mask
      Parameters:
      isReg - true if this is a normal file
      isDir - true if this is a directory
      isLnk - true if this is a symbolic link
      perms - The file / folder's access PosixFilePermissions
      Returns:
      A mask encoding the file / folder's attributes
    • permissionsToFileType

      public static int permissionsToFileType(int perms)
      Converts a POSIX permissions mask to a file type value
      Parameters:
      perms - The POSIX permissions mask
      Returns:
      The file type - see SSH_FILEXFER_TYPE_xxx values
    • fileTypeToPermission

      public static int fileTypeToPermission(int type)
      Converts a file type into a POSIX permission mask value
      Parameters:
      type - File type - see SSH_FILEXFER_TYPE_xxx values
      Returns:
      The matching POSIX permission mask value
    • fileTypeFromChar

      public static int fileTypeFromChar(char ch)
      Converts a POSIX/Linux file type indicator (as if obtained by "ls -l") to a file type.
      Parameters:
      ch - character to convert
      Returns:
      the file type
    • complete

      public static SftpClient.Attributes complete(SftpClient.Attributes attrs, String longName)
      Fills in missing information in the attributes if an SFTP v3 long name is available. If missing information cannot be extracted from the long name, it is not filled in, but no error or exception is generated.

      The SFTP draft RFC discourages parsing a long name to extract information and states the attributes should be used instead. But some SFTP v3 servers do not send all information in the attributes... for instance the SolarWinds SFTP server on Windows does not include the file type flags in the permissions. The only way to determine the file type is then to look at the permissions string in the long name.

      Parameters:
      attrs - SftpClient.Attributes to complete, if necessary
      longName - to use to find missing information, may be null or empty.
      Returns:
      attrs
    • isUnixPermissions

      private static boolean isUnixPermissions(String longName)
    • permissionsToAttributes

      public static Set<PosixFilePermission> permissionsToAttributes(int perms)
      Translates a mask of permissions into its enumeration values equivalents
      Parameters:
      perms - The permissions mask
      Returns:
      A Set of the equivalent PosixFilePermissions
    • resolveSubstatus

      public static int resolveSubstatus(Throwable t)
      Returns the most adequate sub-status for the provided exception
      Parameters:
      t - The thrown Throwable
      Returns:
      The matching sub-status
    • resolveStatusMessage

      public static String resolveStatusMessage(int subStatus)
    • readAttrs

      public static NavigableMap<String,Object> readAttrs(Buffer buffer, int version)
    • readExtensions

      public static NavigableMap<String,byte[]> readExtensions(Buffer buffer)
    • writeExtensions

      public static <B extends Buffer> B writeExtensions(B buffer, Map<?,?> extensions)
    • toStringExtensions

      public static NavigableMap<String,String> toStringExtensions(Map<String,?> extensions)
    • toBinaryExtensions

      public static NavigableMap<String,byte[]> toBinaryExtensions(Map<String,String> extensions)
    • readACLs

      public static List<AclEntry> readACLs(Buffer buffer, int version)
    • decodeACLs

      public static List<AclEntry> decodeACLs(Buffer buffer, int version)
    • buildAclEntry

      public static AclEntry buildAclEntry(int aclType, int aclFlag, int aclMask, String aclWho)
    • decodeAclEntryType

      public static AclEntryType decodeAclEntryType(int aclType)
      Parameters:
      aclType - The ACE4_ACCESS_xxx_ACE_TYPE value
      Returns:
      The matching AclEntryType or null if unknown value
    • decodeAclFlags

      public static Set<AclEntryFlag> decodeAclFlags(int aclFlag)
    • decodeAclMask

      public static Set<AclEntryPermission> decodeAclMask(int aclMask)
    • writeACLs

      public static <B extends Buffer> B writeACLs(B buffer, int version, Collection<AclEntry> acl)
    • encodeACLs

      public static <B extends Buffer> B encodeACLs(B buffer, int version, Collection<AclEntry> acl)
    • writeAclEntry

      public static <B extends Buffer> B writeAclEntry(B buffer, AclEntry acl)
    • encodeAclEntryType

      public static int encodeAclEntryType(AclEntryType type)
      Returns the equivalent SFTP value for the ACL type
      Parameters:
      type - The AclEntryType
      Returns:
      The equivalent ACE_SYSTEM_xxx_TYPE or negative if null or unknown type
    • encodeAclFlags

      public static long encodeAclFlags(Collection<AclEntryFlag> flags)
    • encodeAclMask

      public static long encodeAclMask(Collection<AclEntryPermission> mask)
    • writeTime

      public static <B extends Buffer> B writeTime(B buffer, int version, int flags, FileTime time)
      Encodes a FileTime value into a buffer
      Type Parameters:
      B - Type of Buffer being updated
      Parameters:
      buffer - The target buffer instance
      version - The encoding version
      flags - The encoding flags
      time - The value to encode
      Returns:
      The updated buffer
    • readTime

      public static FileTime readTime(Buffer buffer, int version, int flags)
      Decodes a FileTime value from a buffer
      Parameters:
      buffer - The source Buffer
      version - The encoding version
      flags - The encoding flags
      Returns:
      The decoded value
    • getLongName

      public static String getLongName(String shortName, Map<String,?> attributes)
      Creates an "ls -l" compatible long name string
      Parameters:
      shortName - The short file name - can also be "." or ".."
      attributes - The file's attributes - e.g., size, owner, permissions, etc.
      Returns:
      A String representing the "long" file name as per SFTP version 3 - section 7