Top Fields Constructors Methods
org.apache.sshd.sftp.common

public final Class SftpHelper

extends Object
Class Inheritance
Imports
java.io.EOFException, .FileNotFoundException, java.net.UnknownServiceException, java.nio.channels.OverlappingFileLockException, java.nio.charset.StandardCharsets, java.nio.file.AccessDeniedException, .DirectoryNotEmptyException, .FileAlreadyExistsException, .FileSystemLoopException, .InvalidPathException, .NoSuchFileException, .NotDirectoryException, java.nio.file.attribute.AclEntry, .AclEntryFlag, .AclEntryPermission, .AclEntryType, .FileTime, .PosixFilePermission, .PosixFilePermissions, .UserPrincipal, .UserPrincipalNotFoundException, java.security.Principal, java.time.DateTimeException, .Instant, java.util.ArrayList, .Collection, .Collections, .Comparator, .EnumSet, .List, .Map, .NavigableMap, .Objects, .Set, .TreeMap, java.util.concurrent.TimeUnit, java.util.regex.Pattern, org.apache.sshd.common.PropertyResolver, .SshConstants, org.apache.sshd.common.util.GenericUtils, .MapEntryUtils, .OsUtils, .ValidateUtils, org.apache.sshd.common.util.buffer.Buffer, .BufferUtils, .ByteArrayBuffer, org.apache.sshd.common.util.io.IoUtils, org.apache.sshd.sftp.SftpModuleProperties, org.apache.sshd.sftp.client.SftpClient.Attribute, .SftpClient.Attributes, org.apache.sshd.sftp.server.DefaultGroupPrincipal, .InvalidHandleException, .UnixDateFormat

Author
Apache MINA SSHD Project

Field Summary

Modifier and TypeField and Description
public static final Map<Integer, String>
private static final Pattern

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static int

Returns:

A mask encoding the file / folder's attributes
attributesToPermissions
(boolean
true if this is a normal file
isReg
,
boolean
true if this is a directory
isDir
,
boolean
true if this is a symbolic link
isLnk
,
Collection<PosixFilePermission>
The file / folder's access PosixFilePermissions
perms
)

Converts a file / folder's attributes into a mask

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

public static SftpClient.Attributes

Returns:

attrs
complete
(SftpClient.Attributes
Attributes to complete, if necessary
attrs
,
String
to use to find missing information, may be null or empty.
longName
)

Fills in missing information in the attributes if an SFTP v3 long name is available.

public static AclEntryType

Returns:

The matching AclEntryType or null if unknown value
decodeAclEntryType
(int
The ACE4_ACCESS_xxx_ACE_TYPE value
aclType
)

public static Set<AclEntryFlag>
decodeAclFlags(int aclFlag)

public static Set<AclEntryPermission>
decodeAclMask(int aclMask)

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

public static int

Returns:

The equivalent ACE_SYSTEM_xxx_TYPE or negative if null or unknown type
encodeAclEntryType
(AclEntryType type)

Returns the equivalent SFTP value for the ACL type

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

public static int

Returns:

the file type
fileTypeFromChar
(char
character to convert
ch
)

Converts a POSIX/Linux file type indicator (as if obtained by "ls -l") to a file type.

public static int

Returns:

The matching POSIX permission mask value
fileTypeToPermission
(int
File type - see SSH_FILEXFER_TYPE_xxx values
type
)

Converts a file type into a POSIX permission mask value

public static boolean

Returns:

true it the argument is non-null and its Boolean#booleanValue() is true
getBool
(Boolean
The Boolean value
bool
)

public static Boolean

Returns:

The indicator value - null if none retrieved
getEndOfFileIndicatorValue
(Buffer
The Buffer to retrieve the data from
buffer
,
int
The SFTP version being used
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

public static Boolean

Returns:

The indicator value - null if none retrieved
getEndOfListIndicatorValue
(Buffer
The Buffer to retrieve the data from
buffer
,
int
The SFTP version being used
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

public static String

Returns:

A String representing the "long" file name as per SFTP version 3 - section 7
getLongName
(String
The short file name - can also be "." or ".."
shortName
,
Map<String, ?>
The file's attributes - e.g., size, owner, permissions, etc.
attributes
)

Creates an "ls -l" compatible long name string

public static Boolean

Returns:

The actual indicator value used - null if none appended
indicateEndOfNamesList
(Buffer
The Buffer to append the indicator
buffer
,
int
The SFTP version being used
version
,
PropertyResolver
The PropertyResolver to query whether to enable the feature
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

public static Boolean

Returns:

The actual indicator value used - null if none appended
indicateEndOfNamesList
(Buffer
The Buffer to append the indicator
buffer
,
int
The SFTP version being used
version
,
PropertyResolver
The PropertyResolver to query whether to enable the feature
resolver
,
boolean
The indicator value - null means don't append the indicator
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

private static boolean
public static Set<PosixFilePermission>

Returns:

A Set of the equivalent PosixFilePermissions
permissionsToAttributes
(int
The permissions mask
perms
)

Translates a mask of permissions into its enumeration values equivalents

public static int

Returns:

The file type - see SSH_FILEXFER_TYPE_xxx values
permissionsToFileType
(int
The POSIX permissions mask
perms
)

Converts a POSIX permissions mask to a file type value

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

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

public static NavigableMap<String, byte[]>
public static FileTime

Returns:

The decoded value
readTime
(Buffer
The source Buffer
buffer
,
int
The encoding version
version
,
int
The encoding flags
flags
)

Decodes a FileTime value from a buffer

public static String
resolveStatusMessage(int subStatus)

public static int

Returns:

The matching sub-status
resolveSubstatus
(Throwable
The thrown Throwable
t
)

Returns the most adequate sub-status for the provided exception

public static NavigableMap<String, byte[]>
public static NavigableMap<String, String>
toStringExtensions(Map<String, ?> extensions)

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

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

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

public static <
Type of Buffer being updated
B extends Buffer
>
B

Returns:

The updated buffer
writeAttrs
(B
The target buffer instance
buffer
,
int
The output encoding version
version
,
Map<String, ?>
The Map of attributes
attributes
)

Writes a file / folder's attributes to a buffer

public static <
Type of Buffer being updated
B extends Buffer
>
B

Returns:

The updated buffer
writeAttrsV3
(B
The target buffer instance
buffer
,
int
The actual version - must be SftpConstants#SFTP_V3
version
,
Map<String, ?>
The Map of attributes
attributes
)

Writes the retrieved file / directory attributes in V3 format

public static <
Type of Buffer being updated
B extends Buffer
>
B

Returns:

The updated buffer
writeAttrsV4
(B
The target buffer instance
buffer
,
int
The actual version - must be at least SftpConstants#SFTP_V4
version
,
Map<String, ?>
The Map of attributes
attributes
)

Writes the retrieved file / directory attributes in V4+ format

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

public static <
Type of Buffer being updated
B extends Buffer
>
B

Returns:

The updated buffer
writeTime
(B
The target buffer instance
buffer
,
int
The encoding version
version
,
int
The encoding flags
flags
,
FileTime
The value to encode
time
)

Encodes a FileTime value into a buffer

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DEFAULT_SUBSTATUS_MESSAGEback to summary
public static final Map<Integer, String> DEFAULT_SUBSTATUS_MESSAGE
UNIX_PERMISSIONS_STARTback to summary
private static final Pattern UNIX_PERMISSIONS_START

Constructor Detail

SftpHelperback to summary
private SftpHelper()

Method Detail

attributesToPermissionsback to summary
public static int attributesToPermissions(boolean isReg, boolean isDir, boolean isLnk, Collection<PosixFilePermission> perms)

Converts a file / folder's attributes into a mask

Parameters
isReg:boolean

true if this is a normal file

isDir:boolean

true if this is a directory

isLnk:boolean

true if this is a symbolic link

perms:Collection<PosixFilePermission>

The file / folder's access PosixFilePermissions

Returns:int

A mask encoding the file / folder's attributes

buildAclEntryback to summary
public static AclEntry buildAclEntry(int aclType, int aclFlag, int aclMask, String aclWho)
completeback to summary
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

Attributes to complete, if necessary

longName:String

to use to find missing information, may be null or empty.

Returns:SftpClient.Attributes

attrs

decodeAclEntryTypeback to summary
public static AclEntryType decodeAclEntryType(int aclType)
Parameters
aclType:int

The ACE4_ACCESS_xxx_ACE_TYPE value

Returns:AclEntryType

The matching AclEntryType or null if unknown value

decodeAclFlagsback to summary
public static Set<AclEntryFlag> decodeAclFlags(int aclFlag)
decodeAclMaskback to summary
public static Set<AclEntryPermission> decodeAclMask(int aclMask)
decodeACLsback to summary
public static List<AclEntry> decodeACLs(Buffer buffer, int version)
encodeAclEntryTypeback to summary
public static int encodeAclEntryType(AclEntryType type)

Returns the equivalent SFTP value for the ACL type

Parameters
type:AclEntryType

The AclEntryType

Returns:int

The equivalent ACE_SYSTEM_xxx_TYPE or negative if null or unknown type

encodeAclFlagsback to summary
public static long encodeAclFlags(Collection<AclEntryFlag> flags)
encodeAclMaskback to summary
public static long encodeAclMask(Collection<AclEntryPermission> mask)
encodeACLsback to summary
public static <B extends Buffer> B encodeACLs(B buffer, int version, Collection<AclEntry> acl)
fileTypeFromCharback to summary
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:char

character to convert

Returns:int

the file type

fileTypeToPermissionback to summary
public static int fileTypeToPermission(int type)

Converts a file type into a POSIX permission mask value

Parameters
type:int

File type - see SSH_FILEXFER_TYPE_xxx values

Returns:int

The matching POSIX permission mask value

getBoolback to summary
public static boolean getBool(Boolean bool)
Parameters
bool:Boolean

The Boolean value

Returns:boolean

true it the argument is non-null and its Boolean#booleanValue() is true

getEndOfFileIndicatorValueback to summary
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:Buffer

The Buffer to retrieve the data from

version:int

The SFTP version being used

Returns:Boolean

The indicator value - null if none retrieved

See Also
SFTP v6 - section 9.3
getEndOfListIndicatorValueback to summary
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:Buffer

The Buffer to retrieve the data from

version:int

The SFTP version being used

Returns:Boolean

The indicator value - null if none retrieved

See Also
SFTP v6 - section 9.4, indicateEndOfNamesList(Buffer, int, PropertyResolver, boolean)
getLongNameback to summary
public static String getLongName(String shortName, Map<String, ?> attributes)

Creates an "ls -l" compatible long name string

Parameters
shortName:String

The short file name - can also be "." or ".."

attributes:Map<String, ?>

The file's attributes - e.g., size, owner, permissions, etc.

Returns:String

A String representing the "long" file name as per SFTP version 3 - section 7

indicateEndOfNamesListback to summary
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:Buffer

The Buffer to append the indicator

version:int

The SFTP version being used

resolver:PropertyResolver

The PropertyResolver to query whether to enable the feature

Returns:Boolean

The actual indicator value used - null if none appended

See Also
indicateEndOfNamesList(Buffer, int, PropertyResolver, boolean)
indicateEndOfNamesListback to summary
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:Buffer

The Buffer to append the indicator

version:int

The SFTP version being used

resolver:PropertyResolver

The PropertyResolver to query whether to enable the feature

indicatorValue:boolean

The indicator value - null means don't append the indicator

Returns:Boolean

The actual indicator value used - null if none appended

See Also
SFTP v6 - section 9.4, SftpModuleProperties#APPEND_END_OF_LIST_INDICATOR
isUnixPermissionsback to summary
private static boolean isUnixPermissions(String longName)
permissionsToAttributesback to summary
public static Set<PosixFilePermission> permissionsToAttributes(int perms)

Translates a mask of permissions into its enumeration values equivalents

Parameters
perms:int

The permissions mask

Returns:Set<PosixFilePermission>

A Set of the equivalent PosixFilePermissions

permissionsToFileTypeback to summary
public static int permissionsToFileType(int perms)

Converts a POSIX permissions mask to a file type value

Parameters
perms:int

The POSIX permissions mask

Returns:int

The file type - see SSH_FILEXFER_TYPE_xxx values

readACLsback to summary
public static List<AclEntry> readACLs(Buffer buffer, int version)
readAttrsback to summary
public static NavigableMap<String, Object> readAttrs(Buffer buffer, int version)
readExtensionsback to summary
public static NavigableMap<String, byte[]> readExtensions(Buffer buffer)
readTimeback to summary
public static FileTime readTime(Buffer buffer, int version, int flags)

Decodes a FileTime value from a buffer

Parameters
buffer:Buffer

The source Buffer

version:int

The encoding version

flags:int

The encoding flags

Returns:FileTime

The decoded value

resolveStatusMessageback to summary
public static String resolveStatusMessage(int subStatus)
resolveSubstatusback to summary
public static int resolveSubstatus(Throwable t)

Returns the most adequate sub-status for the provided exception

Parameters
t:Throwable

The thrown Throwable

Returns:int

The matching sub-status

Annotations
@SuppressWarnings:checkstyle:ReturnCount
toBinaryExtensionsback to summary
public static NavigableMap<String, byte[]> toBinaryExtensions(Map<String, String> extensions)
toStringExtensionsback to summary
public static NavigableMap<String, String> toStringExtensions(Map<String, ?> extensions)
writeAclEntryback to summary
public static <B extends Buffer> B writeAclEntry(B buffer, AclEntry acl)
writeACLsback to summary
public static <B extends Buffer> B writeACLs(B buffer, int version, Collection<AclEntry> acl)
writeAttributesback to summary
public static <B extends Buffer> B writeAttributes(B buffer, SftpClient.Attributes attributes, int sftpVersion)
writeAttrsback to summary
public static <B extends Buffer> B writeAttrs(B buffer, int version, Map<String, ?> attributes)

Writes a file / folder's attributes to a buffer

Parameters
<B>
Type of Buffer being updated
buffer:B

The target buffer instance

version:int

The output encoding version

attributes:Map<String, ?>

The Map of attributes

Returns:B

The updated buffer

See Also
writeAttrsV3(Buffer, int, Map), writeAttrsV4(Buffer, int, Map)
writeAttrsV3back to summary
public static <B extends Buffer> B writeAttrsV3(B buffer, int version, Map<String, ?> attributes)

Writes the retrieved file / directory attributes in V3 format

Parameters
<B>
Type of Buffer being updated
buffer:B

The target buffer instance

version:int

The actual version - must be SftpConstants#SFTP_V3

attributes:Map<String, ?>

The Map of attributes

Returns:B

The updated buffer

writeAttrsV4back to summary
public static <B extends Buffer> B writeAttrsV4(B buffer, int version, Map<String, ?> attributes)

Writes the retrieved file / directory attributes in V4+ format

Parameters
<B>
Type of Buffer being updated
buffer:B

The target buffer instance

version:int

The actual version - must be at least SftpConstants#SFTP_V4

attributes:Map<String, ?>

The Map of attributes

Returns:B

The updated buffer

writeExtensionsback to summary
public static <B extends Buffer> B writeExtensions(B buffer, Map<?, ?> extensions)
writeTimeback to summary
public static <B extends Buffer> B writeTime(B buffer, int version, int flags, FileTime time)

Encodes a FileTime value into a buffer

Parameters
<B>
Type of Buffer being updated
buffer:B

The target buffer instance

version:int

The encoding version

flags:int

The encoding flags

time:FileTime

The value to encode

Returns:B

The updated buffer