ssh_sftp
MODULE
MODULE SUMMARY
DESCRIPTION
This module implements an SSH FTP (SFTP) client. SFTP is a secure, encrypted file transfer service available for SSH.
DATA TYPES
Type definitions that are used more than once in this module, or abstractions to indicate the intended use of the data type, or both:
- ssh_connection_ref() =
opaque() - as returned by ssh:connect/3
- timeout()
= infinity | integer() in milliseconds. Default infinity.
Time-outs
If the request functions for the SFTP channel return {error, timeout}, it does not guarantee that the request never reached the server and was not performed. It only means that no answer was received from the server within the expected time.
EXPORTS
apread(ChannelPid, Handle, Position, Len) -> {async, N} | {error, Reason}
Types:
The apread function reads from a specified position, combining the position and aread functions.
apwrite(ChannelPid, Handle, Position, Data) -> ok | {error, Reason}
Types:
apwrite writes on a specified position, combining the position and awrite operations.
aread(ChannelPid, Handle, Len) -> {async, N} | {error, Error}
Types:
Reads from an open file, without waiting for the result. If the handle is valid, the function returns {async, N}, where N is a term guaranteed to be unique between calls of aread. The actual data is sent as a message to the calling process. This message has the form {async_reply, N, Result}, where Result is the result from the read, either {ok, Data}, eof, or {error, Error}.
awrite(ChannelPid, Handle, Data) -> ok | {error, Reason}
Types:
Writes to an open file, without waiting for the result. If the handle is valid, the function returns {async, N}, where N is a term guaranteed to be unique between calls of awrite. The result of the write operation is sent as a message to the calling process. This message has the form {async_reply, N, Result}, where Result is the result from the write, either ok, or {error, Error}.
close(ChannelPid, Handle) ->
close(ChannelPid, Handle, Timeout) -> ok | {error, Reason}
Types:
Closes a handle to an open file or directory on the server.
delete(ChannelPid, Name) ->
delete(ChannelPid, Name, Timeout) -> ok | {error, Reason}
Types:
Deletes the file specified by Name, like file:delete/1
del_dir(ChannelPid, Name) ->
del_dir(ChannelPid, Name, Timeout) -> ok | {error, Reason}
Types:
Deletes a directory specified by Name. The directory must be empty before it can be successfully deleted.
list_dir(ChannelPid, Path) ->
list_dir(ChannelPid, Path, Timeout) -> {ok, Filenames} | {error, Reason}
Types:
Lists the given directory on the server, returning the filenames as a list of strings.
make_dir(ChannelPid, Name) ->
make_dir(ChannelPid, Name, Timeout) -> ok | {error, Reason}
Types:
Creates a directory specified by Name. Name must be a full path to a new directory. The directory can only be created in an existing directory.
make_symlink(ChannelPid, Name, Target) ->
make_symlink(ChannelPid, Name, Target, Timeout) -> ok | {error, Reason}
Types:
Creates a symbolic link pointing to Target with the name Name, like file:make_symlink/2
open(ChannelPid, File, Mode) ->
open(ChannelPid, File, Mode, Timeout) -> {ok, Handle} | {error, Reason}
Types:
Opens a file on the server and returns a handle, which can be used for reading or writing.
opendir(ChannelPid, Path) ->
opendir(ChannelPid, Path, Timeout) -> {ok, Handle} | {error, Reason}
Types:
Opens a handle to a directory on the server. The handle can be used for reading directory contents.
open_tar(ChannelPid, Path, Mode) ->
open_tar(ChannelPid, Path, Mode, Timeout) -> {ok, Handle} | {error, Reason}
Types:
Opens a handle to a tar file on the server, associated with ChannelPid. The handle can be used for remote tar creation and extraction, as defined by the erl_tar:init/3 function.
For code exampel see Section SFTP Client with TAR Compression and Encryption in the ssh Users Guide.
The crypto mode option is applied to the generated stream of bytes prior to sending them to the SFTP server. This is intended for encryption but can be used for other purposes.
The InitFun is applied once prior to any other crypto operation. The returned CryptoState is then folded into repeated applications of the EncryptFun or DecryptFun. The binary returned from those funs are sent further to the remote SFTP server. Finally, if doing encryption, the CloseFun is applied to the last piece of data. The CloseFun is responsible for padding (if needed) and encryption of that last piece.
The ChunkSize defines the size of the PlainBins that EncodeFun is applied to. If the ChunkSize is undefined, the size of the PlainBins varies, because this is intended for stream crypto, whereas a fixed ChunkSize is intended for block crypto. ChunkSizes can be changed in the return from the EncryptFun or DecryptFun. The value can be changed between pos_integer() and undefined.
position(ChannelPid, Handle, Location) ->
position(ChannelPid, Handle, Location, Timeout) -> {ok, NewPosition | {error, Error}
Types:
Sets the file position of the file referenced by Handle. Returns {ok, NewPosition} (as an absolute offset) if successful, otherwise {error, Reason}. Location is one of the following:
- Offset
-
The same as {bof, Offset}.
- {bof, Offset}
-
Absolute offset.
- {cur, Offset}
-
Offset from the current position.
- {eof, Offset}
-
Offset from the end of file.
- bof | cur | eof
-
The same as eariler with Offset 0, that is, {bof, 0} | {cur, 0} | {eof, 0}.
pread(ChannelPid, Handle, Position, Len) ->
pread(ChannelPid, Handle, Position, Len, Timeout) -> {ok, Data} | eof | {error, Error}
Types:
The pread function reads from a specified position, combining the position and read functions.
pwrite(ChannelPid, Handle, Position, Data) -> ok
pwrite(ChannelPid, Handle, Position, Data, Timeout) -> ok | {error, Error}
Types:
The pread function writes to a specified position, combining the position and write functions.
read(ChannelPid, Handle, Len) ->
read(ChannelPid, Handle, Len, Timeout) -> {ok, Data} | eof | {error, Error}
Types:
Reads Len bytes from the file referenced by Handle. Returns {ok, Data}, eof, or {error, Reason}. If the file is opened with binary, Data is a binary, otherwise it is a string.
If the file is read past eof, only the remaining bytes are read and returned. If no bytes are read, eof is returned.
read_file(ChannelPid, File) ->
read_file(ChannelPid, File, Timeout) -> {ok, Data} | {error, Reason}
Types:
Reads a file from the server, and returns the data in a binary, like file:read_file/1
read_file_info(ChannelPid, Name) ->
read_file_info(ChannelPid, Name, Timeout) -> {ok, FileInfo} | {error, Reason}
Types:
Returns a file_info record from the file specified by Name or Handle, like file:read_file_info/2
read_link(ChannelPid, Name) ->
read_link(ChannelPid, Name, Timeout) -> {ok, Target} | {error, Reason}
Types:
Reads the link target from the symbolic link specified by name, like file:read_link/1
read_link_info(ChannelPid, Name) -> {ok, FileInfo} | {error, Reason}
read_link_info(ChannelPid, Name, Timeout) -> {ok, FileInfo} | {error, Reason}
Types:
Returns a file_info record from the symbolic link specified by Name or Handle, like file:read_link_info/2
rename(ChannelPid, OldName, NewName) ->
rename(ChannelPid, OldName, NewName, Timeout) -> ok | {error, Reason}
Types:
Renames a file named OldName and gives it the name NewName, like file:rename/2
start_channel(ConnectionRef) ->
start_channel(ConnectionRef, Options) ->
start_channel(Host, Options) ->
start_channel(Host, Port, Options) -> {ok, Pid} | {ok, Pid, ConnectionRef} |
{error, Reason}
Types:
If no connection reference is provided, a connection is set up, and the new connection is returned. An SSH channel process is started to handle the communication with the SFTP server. The returned pid for this process is to be used as input to all other API functions in this module.
Options:
- {timeout, timeout()}
-
The time-out is passed to the ssh_channel start function, and defaults to infinity.
- {sftp_vsn, integer()}
-
Desired SFTP protocol version. The actual version is the minimum of the desired version and the maximum supported versions by the SFTP server.
All other options are directly passed to ssh:connect/3 or ignored if a connection is already provided.
stop_channel(ChannelPid) -> ok
Types:
Stops an SFTP channel. Does not close the SSH connection. Use ssh:close/1 to close it.
write(ChannelPid, Handle, Data) ->
write(ChannelPid, Handle, Data, Timeout) -> ok | {error, Error}
Types:
Writes data to the file referenced by Handle. The file is to be opened with write or append flag. Returns ok if successful or {error, Reason} otherwise.
Typical error reasons:
- ebadf
-
File is not opened for writing.
- enospc
-
No space is left on the device.
write_file(ChannelPid, File, Iolist) ->
write_file(ChannelPid, File, Iolist, Timeout) -> ok | {error, Reason}
Types:
Writes a file to the server, like file:write_file/2 The file is created if it does not exist. The file is overwritten if it exists.
write_file_info(ChannelPid, Name, Info) ->
write_file_info(ChannelPid, Name, Info, Timeout) -> ok | {error, Reason}
Types:
Writes file information from a file_info record to the file specified by Name, like file:write_file_info/[2,3]