21 KiB
SFTP events
Client/Server events
- ready() - Emitted after initial protocol version check has passed.
Server-only events
Responses to these client requests are sent using one of the methods listed further in this document under Server-only methods. The valid response(s) for each request are documented below.
-
OPEN(< integer >reqID, < string >filename, < integer >flags, < ATTRS >attrs)
flagsis a bitfield containing any of the flags defined inOPEN_MODE. Use the static methodflagsToString()to convert the value to a mode string to be used byfs.open()(e.g.'r').Respond using one of the following:
-
handle()- This indicates a successful opening of the file and passes the given handle back to the client to use to refer to this open file for future operations (e.g. reading, writing, closing). -
status()- Use this to indicate a failure to open the requested file.
-
-
READ(< integer >reqID, < Buffer >handle, < integer >offset, < integer >length)
Respond using one of the following:
-
data()- Use this to send the requested chunk of data back to the client. The amount of data sent is allowed to be less than thelengthrequested, for example if the file ends betweenoffsetandoffset + length. -
status()- Use this to indicate either end of file (STATUS_CODE.EOF) has been reached (offsetis past the end of the file) or if an error occurred while reading the requested part of the file.
-
-
WRITE(< integer >reqID, < Buffer >handle, < integer >offset, < Buffer >data)
Respond using:
status()- Use this to indicate success/failure of the write to the file.
-
FSTAT(< integer >reqID, < Buffer >handle)
Respond using one of the following:
-
attrs()- Use this to send the attributes for the requested file/directory back to the client. -
status()- Use this to indicate an error occurred while accessing the file/directory.
-
-
FSETSTAT(< integer >reqID, < Buffer >handle, < ATTRS >attrs)
Respond using:
status()- Use this to indicates success/failure of the setting of the given file/directory attributes.
-
CLOSE(< integer >reqID, < Buffer >handle)
Respond using:
status()- Use this to indicate success (STATUS_CODE.OK) or failure of the closing of the file identified byhandle.
-
OPENDIR(< integer >reqID, < string >path)
Respond using one of the following:
-
handle()- This indicates a successful opening of the directory and passes the given handle back to the client to use to refer to this open directory for future operations (e.g. reading directory contents, closing). -
status()- Use this to indicate a failure to open the requested directory.
-
-
READDIR(< integer >reqID, < Buffer >handle)
Respond using one of the following:
-
name()- Use this to send one or more directory listings for the open directory back to the client. -
status()- Use this to indicate either end of directory contents (STATUS_CODE.EOF) or if an error occurred while reading the directory contents.
-
-
LSTAT(< integer >reqID, < string >path)
Respond using one of the following:
-
attrs()- Use this to send the attributes for the requested file/directory back to the client. -
status()- Use this to indicate an error occurred while accessing the file/directory.
-
-
STAT(< integer >reqID, < string >path)
Respond using one of the following:
-
attrs()- Use this to send the attributes for the requested file/directory back to the client. -
status()- Use this to indicate an error occurred while accessing the file/directory.
-
-
REMOVE(< integer >reqID, < string >path)
Respond using:
status()- Use this to indicate success/failure of the removal of the file atpath.
-
RMDIR(< integer >reqID, < string >path)
Respond using:
status()- Use this to indicate success/failure of the removal of the directory atpath.
-
REALPATH(< integer >reqID, < string >path)
Respond using one of the following:
-
name()- Use this to respond with a normalized version ofpath. No file/directory attributes are required to be sent in this response. -
status()- Use this to indicate a failure in normalizingpath.
-
-
READLINK(< integer >reqID, < string >path)
Respond using one of the following:
-
name()- Use this to respond with the target of the symlink atpath. No file/directory attributes are required to be sent in this response. -
status()- Use this to indicate a failure in reading the symlink atpath.
-
-
SETSTAT(< integer >reqID, < string >path, < ATTRS >attrs)
Respond using:
status()- Use this to indicates success/failure of the setting of the given file/directory attributes.
-
MKDIR(< integer >reqID, < string >path, < ATTRS >attrs)
Respond using:
status()- Use this to indicate success/failure of the creation of the directory atpath.
-
RENAME(< integer >reqID, < string >oldPath, < string >newPath)
Respond using:
status()- Use this to indicate success/failure of the renaming of the file/directory atoldPathtonewPath.
-
SYMLINK(< integer >reqID, < string >linkPath, < string >targetPath)
Respond using:
status()- Use this to indicate success/failure of the symlink creation.
Useful standalone data structures
-
STATUS_CODE - object - Contains the various status codes (for use especially with
status()):-
OK -
EOF -
NO_SUCH_FILE -
PERMISSION_DENIED -
FAILURE -
BAD_MESSAGE -
OP_UNSUPPORTED
-
-
OPEN_MODE - object - Contains the various open file flags:
-
READ -
WRITE -
APPEND -
CREAT -
TRUNC -
EXCL
-
Useful standalone methods
-
stringToFlags(< string >flagsStr) - integer - Converts string flags (e.g.
'r','a+', etc.) to the appropriateOPEN_MODEflag mask. Returnsnullif conversion failed. -
flagsToString(< integer >flagsMask) - string - Converts flag mask (e.g. number containing
OPEN_MODEvalues) to the appropriate string value. Returnsnullif conversion failed.
SFTP methods
-
(constructor)(< object >config[, < string >remoteIdentRaw]) - Creates and returns a new SFTP instance.
remoteIdentRawcan be the raw SSH identification string of the remote party. This is used to change internal behavior based on particular SFTP implementations.configcan contain:-
server - boolean - Set to
trueto create an instance in server mode. Default:false -
debug - function - Set this to a function that receives a single string argument to get detailed (local) debug information. Default: (none)
-
Client-only methods
-
fastGet(< string >remotePath, < string >localPath[, < object >options], < function >callback) - (void) - Downloads a file at
remotePathtolocalPathusing parallel reads for faster throughput.optionscan have the following properties:-
concurrency - integer - Number of concurrent reads Default:
64 -
chunkSize - integer - Size of each read in bytes Default:
32768 -
step - function(< integer >total_transferred, < integer >chunk, < integer >total) - Called every time a part of a file was transferred
callbackhas 1 parameter: < Error >err. -
-
fastPut(< string >localPath, < string >remotePath[, < object >options], < function >callback) - (void) - Uploads a file from
localPathtoremotePathusing parallel reads for faster throughput.optionscan have the following properties:-
concurrency - integer - Number of concurrent reads Default:
64 -
chunkSize - integer - Size of each read in bytes Default:
32768 -
step - function(< integer >total_transferred, < integer >chunk, < integer >total) - Called every time a part of a file was transferred
-
mode - mixed - Integer or string representing the file mode to set for the uploaded file.
callbackhas 1 parameter: < Error >err. -
-
createReadStream(< string >path[, < object >options]) - ReadStream - Returns a new readable stream for
path.optionshas the following defaults:{ flags: 'r', encoding: null, handle: null, mode: 0o666, autoClose: true }optionscan includestartandendvalues to read a range of bytes from the file instead of the entire file. Bothstartandendare inclusive and start at 0. Theencodingcan be'utf8','ascii', or'base64'.If
autoCloseis false, then the file handle won't be closed, even if there's an error. It is your responsibility to close it and make sure there's no file handle leak. IfautoCloseis set to true (default behavior), onerrororendthe file handle will be closed automatically.An example to read the last 10 bytes of a file which is 100 bytes long:
sftp.createReadStream('sample.txt', {start: 90, end: 99}); -
createWriteStream(< string >path[, < object >options]) - WriteStream - Returns a new writable stream for
path.optionshas the following defaults:{ flags: 'w', encoding: null, mode: 0o666, autoClose: true }optionsmay also include astartoption to allow writing data at some position past the beginning of the file. Modifying a file rather than replacing it may require a flags mode of 'r+' rather than the default mode 'w'.If 'autoClose' is set to false and you pipe to this stream, this stream will not automatically close after there is no more data upstream -- allowing future pipes and/or manual writes.
-
open(< string >filename, < string >flags, [< mixed >attrs_mode, ]< function >callback) - (void) - Opens a file
filenamewithflagswith optional ATTRS object or file modeattrs_mode.flagsis any of the flags supported byfs.open(except sync flag).callbackhas 2 parameters: < Error >err, < Buffer >handle. -
close(< Buffer >handle, < function >callback) - (void) - Closes the resource associated with
handlegiven by open() or opendir().callbackhas 1 parameter: < Error >err. -
read(< Buffer >handle, < Buffer >buffer, < integer >offset, < integer >length, < integer >position, < function >callback) - (void) - Reads
lengthbytes from the resource associated withhandlestarting atpositionand stores the bytes inbufferstarting atoffset.callbackhas 4 parameters: < Error >err, < integer >bytesRead, < Buffer >buffer (offset adjusted), < integer >position. -
write(< Buffer >handle, < Buffer >buffer, < integer >offset, < integer >length, < integer >position, < function >callback) - (void) - Writes
lengthbytes frombufferstarting atoffsetto the resource associated withhandlestarting atposition.callbackhas 1 parameter: < Error >err. -
fstat(< Buffer >handle, < function >callback) - (void) - Retrieves attributes for the resource associated with
handle.callbackhas 2 parameters: < Error >err, < Stats >stats. -
fsetstat(< Buffer >handle, < ATTRS >attributes, < function >callback) - (void) - Sets the attributes defined in
attributesfor the resource associated withhandle.callbackhas 1 parameter: < Error >err. -
futimes(< Buffer >handle, < mixed >atime, < mixed >mtime, < function >callback) - (void) - Sets the access time and modified time for the resource associated with
handle.atimeandmtimecan be Date instances or UNIX timestamps.callbackhas 1 parameter: < Error >err. -
fchown(< Buffer >handle, < integer >uid, < integer >gid, < function >callback) - (void) - Sets the owner for the resource associated with
handle.callbackhas 1 parameter: < Error >err. -
fchmod(< Buffer >handle, < mixed >mode, < function >callback) - (void) - Sets the mode for the resource associated with
handle.modecan be an integer or a string containing an octal number.callbackhas 1 parameter: < Error >err. -
opendir(< string >path, < function >callback) - (void) - Opens a directory
path.callbackhas 2 parameters: < Error >err, < Buffer >handle. -
readdir(< mixed >location, < function >callback) - (void) - Retrieves a directory listing.
locationcan either be a Buffer containing a valid directory handle from opendir() or a string containing the path to a directory.callbackhas 2 parameters: < Error >err, < mixed >list.listis an Array of{ filename: 'foo', longname: '....', attrs: {...} }style objects (attrs is of type ATTR). Iflocationis a directory handle, this function may need to be called multiple times untillistis boolean false, which indicates that no more directory entries are available for that directory handle. -
unlink(< string >path, < function >callback) - (void) - Removes the file/symlink at
path.callbackhas 1 parameter: < Error >err. -
rename(< string >srcPath, < string >destPath, < function >callback) - (void) - Renames/moves
srcPathtodestPath.callbackhas 1 parameter: < Error >err. -
mkdir(< string >path, [< ATTRS >attributes, ]< function >callback) - (void) - Creates a new directory
path.callbackhas 1 parameter: < Error >err. -
rmdir(< string >path, < function >callback) - (void) - Removes the directory at
path.callbackhas 1 parameter: < Error >err. -
stat(< string >path, < function >callback) - (void) - Retrieves attributes for
path.callbackhas 2 parameter: < Error >err, < Stats >stats. -
lstat(< string >path, < function >callback) - (void) - Retrieves attributes for
path. Ifpathis a symlink, the link itself is stat'ed instead of the resource it refers to.callbackhas 2 parameters: < Error >err, < Stats >stats. -
setstat(< string >path, < ATTRS >attributes, < function >callback) - (void) - Sets the attributes defined in
attributesforpath.callbackhas 1 parameter: < Error >err. -
utimes(< string >path, < mixed >atime, < mixed >mtime, < function >callback) - (void) - Sets the access time and modified time for
path.atimeandmtimecan be Date instances or UNIX timestamps.callbackhas 1 parameter: < Error >err. -
chown(< string >path, < integer >uid, < integer >gid, < function >callback) - (void) - Sets the owner for
path.callbackhas 1 parameter: < Error >err. -
chmod(< string >path, < mixed >mode, < function >callback) - (void) - Sets the mode for
path.modecan be an integer or a string containing an octal number.callbackhas 1 parameter: < Error >err. -
readlink(< string >path, < function >callback) - (void) - Retrieves the target for a symlink at
path.callbackhas 2 parameters: < Error >err, < string >target. -
symlink(< string >targetPath, < string >linkPath, < function >callback) - (void) - Creates a symlink at
linkPathtotargetPath.callbackhas 1 parameter: < Error >err. -
realpath(< string >path, < function >callback) - (void) - Resolves
pathto an absolute path.callbackhas 2 parameters: < Error >err, < string >absPath. -
ext_openssh_rename(< string >srcPath, < string >destPath, < function >callback) - (void) - OpenSSH extension Performs POSIX rename(3) from
srcPathtodestPath.callbackhas 1 parameter: < Error >err. -
ext_openssh_statvfs(< string >path, < function >callback) - (void) - OpenSSH extension Performs POSIX statvfs(2) on
path.callbackhas 2 parameters: < Error >err, < object >fsInfo.fsInfocontains the information as found in the statvfs struct. -
ext_openssh_fstatvfs(< Buffer >handle, < function >callback) - (void) - OpenSSH extension Performs POSIX fstatvfs(2) on open handle
handle.callbackhas 2 parameters: < Error >err, < object >fsInfo.fsInfocontains the information as found in the statvfs struct. -
ext_openssh_hardlink(< string >targetPath, < string >linkPath, < function >callback) - (void) - OpenSSH extension Performs POSIX link(2) to create a hard link to
targetPathatlinkPath.callbackhas 1 parameter: < Error >err. -
ext_openssh_fsync(< Buffer >handle, < function >callback) - (void) - OpenSSH extension Performs POSIX fsync(3) on the open handle
handle.callbackhas 1 parameter: < Error >err. -
ext_openssh_lsetstat(< string >path, < ATTRS >attributes, < function >callback) - (void) - OpenSSH extension Similar to
setstat(), but instead sets attributes on symlinks.callbackhas 1 parameter: < Error >err. -
ext_openssh_expandPath(< string >path, < function >callback) - (void) - OpenSSH extension Similar to
realpath(), but supports tilde-expansion, i.e. "~", "~/..." and "~user/...". These paths are expanded using shell-like rules.callbackhas 2 parameters: < Error >err, < string >expandedPath. -
ext_copy_data(< Buffer >srcHandle, < number >srcOffset, < number >length, < Buffer >dstHandle, < number >dstOffset, < function >callback) - (void) - Performs a remote file copy. If
lengthis 0, then the server will read fromsrcHandleuntil EOF is reached.callbackhas 1 parameter: < Error >err. -
ext_home_dir(< string >username, < function >callback) - (void) - Retrieves the home directory of the user identified by
username. Use an empty string to refer to the current user.callbackhas 2 parameters: < Error >err, < string >homeDirectory. -
ext_users_groups(< array >uids, < array >gids, < function >callback) - (void) - Retrieves the user names and group names associated with the user IDs in
uidsand group IDs ingidsrespectively. Either array can be empty or contain one or more 32-bit unsigned integers. The retrieved user names and group names match the same order as the IDs inuidsandgidsrespectively. If the server was unable to find a name for a given ID, it will use an empty string.callbackhas 3 parameters: < Error >err, < array >userNames, < array >groupNames.
Server-only methods
-
status(< integer >reqID, < integer >statusCode[, < string >message]) - (void) - Sends a status response for the request identified by
id. -
handle(< integer >reqID, < Buffer >handle) - (void) - Sends a handle response for the request identified by
id.handlemust be less than 256 bytes and is an opaque value that could merely contain the value of a backing file descriptor or some other unique, custom value. -
data(< integer >reqID, < mixed >data[, < string >encoding]) - (void) - Sends a data response for the request identified by
id.datacan be a Buffer or string. Ifdatais a string,encodingis the encoding ofdata. -
name(< integer >reqID, < array >names) - (void) - Sends a name response for the request identified by
id.namesmust be an array of object where each object can contain:-
filename - string - The entry's name.
-
longname - string - This is the
ls -l-style format for the entry (e.g.-rwxr--r-- 1 bar bar 718 Dec 8 2009 foo) -
attrs - ATTRS - This is an optional ATTRS object that contains requested/available attributes for the entry.
-
-
attrs(< integer >reqID, < ATTRS >attrs) - (void) - Sends an attrs response for the request identified by
id.attrscontains the requested/available attributes.
ATTRS
An object with the following valid properties:
-
mode - integer - Mode/permissions for the resource.
-
uid - integer - User ID of the resource.
-
gid - integer - Group ID of the resource.
-
size - integer - Resource size in bytes.
-
atime - integer - UNIX timestamp of the access time of the resource.
-
mtime - integer - UNIX timestamp of the modified time of the resource.
When supplying an ATTRS object to one of the SFTP methods:
-
atimeandmtimecan be either a Date instance or a UNIX timestamp. -
modecan either be an integer or a string containing an octal number.
Stats
An object with the same attributes as an ATTRS object with the addition of the following methods:
-
stats.isDirectory() -
stats.isFile() -
stats.isBlockDevice() -
stats.isCharacterDevice() -
stats.isSymbolicLink() -
stats.isFIFO() -
stats.isSocket()