ssh_sftp(3erl)

okay so this is the ssh_sftp(3erl) glow-up β€” the Erlang module definition page for the SFTP client β€” and we're getting into it together bestie πŸ’–

πŸ’– NAME πŸ’–

ssh_sftp β€” SFTP client. slay.

🌸 DESCRIPTION 🌸

This module implements an SSH FTP (SFTP) client, no cap. SFTP is a secure, encrypted file transfer service available for SSH. it's giving secure file-transfer realness ✨

πŸ¦‹ DATA TYPES πŸ¦‹

<purple>sftp_option()</purple> is literally right here:

sftp_option() =
    {timeout, timeout()} |
    {sftp_vsn, integer() >= 1} |
    {window_size, integer() >= 1} |
    {packet_size, integer() >= 1}

πŸ’€ Error cause πŸ’€

reason() = atom() | string() | tuple()

A description of the reason why an operation failed. iykyk, errors gotta explain themselves 🫢

The atom() value is formed from the sftp error codes in the protocol-level responses as defined in draft-ietf-secsh-filexfer-13 section 9.1. The codes are named as SSH_FX_*, which are transformed into lowercase of the star-part. E.g. the error code SSH_FX_NO_SUCH_FILE will cause the reason() to be no_such_file. the lore is in the error code πŸ’…

The string() reason is the error information from the server in case of an exit-signal. If that information is empty, the reason is the exit signal name.

The tuple() reason is for other errors, like for example {exit_status,1}.

πŸŽ€ Crypto operations for open_tar πŸŽ€

tar_crypto_spec() = encrypt_spec() | decrypt_spec()

encrypt_spec() = {init_fun(), crypto_fun(), final_fun()}

decrypt_spec() = {init_fun(), crypto_fun()}

Specifies the encryption or decryption applied to tar files when using open_tar/3 or open_tar/4. tar files getting the crypto treatment, periodt πŸŽ€

The encryption or decryption is applied to the generated stream of bytes prior to sending the resulting stream to the SFTP server.

For code examples see Section Example with encryption in the ssh Users Guide.

init_fun() =
    fun(() -> {ok, crypto_state()}) |
    fun(() -> {ok, crypto_state(), chunk_size()})

chunk_size() = undefined | integer() >= 1

crypto_state() = any()

The init_fun() in the tar_crypto_spec is applied once prior to any other crypto operation. The intention is that this function initiates the encryption or decryption for example by calling crypto:crypto_init/4 or similar. The crypto_state() is the state such a function may return. main character setup function energy ✨

If the selected cipher needs to have the input data partitioned into blocks of a certain size, the init_fun() should return the second form of return value with the chunk_size() set to the block size. If the chunk_size() is undefined, the size of the PlainBins varies, because this is intended for stream crypto, whereas a fixed chunk_size() is intended for block crypto. A chunk_size() can be changed in the return from the crypto_fun(). The value can be changed between pos_integer() and undefined.

crypto_fun() =
    fun((TextIn :: binary(), crypto_state()) -> crypto_result())

crypto_result() =
    {ok, TextOut :: binary(), crypto_state()} |
    {ok, TextOut :: binary(), crypto_state(), chunk_size()}

The initial crypto_state() returned from the init_fun() is folded into repeated applications of the crypto_fun() in the tar_crypto_spec. The binary returned from that fun is sent to the remote SFTP server and the new crypto_state() is used in the next call of the crypto_fun(). folded state, but make it cryptographic πŸ¦‹

If the crypto_fun() returns a chunk_size(), that value is used as the block size for further blocks in calls to crypto_fun().

final_fun() =
    fun((FinalTextIn :: binary(), crypto_state()) ->
            {ok, FinalTextOut :: binary()})

If doing encryption, the final_fun() in the tar_crypto_spec is applied to the last piece of data. The final_fun() is responsible for padding (if needed) and encryption of that last piece. the last-piece closer, iconic πŸ’…

✨ EXPORTS ✨

apread/4

apread(ChannelPid, Handle, Position, Len) -> {async, N} | Error

Types:

ChannelPid = pid()
Handle = term()
Position = Len = integer()
Error = {error, reason()}
N = term()

The apread/4 function reads from a specified position, combining the position/3 and aread/3 functions. combo move, lowkey efficient ⭐

apwrite/4

apwrite(ChannelPid, Handle, Position, Data) -> {async, N} | Error

Types:

ChannelPid = pid()
Handle = term()
Position = integer()
Data = binary()
Error = {error, reason()}
N = term()

The apwrite/4 function writes to a specified position, combining the position/3 and awrite/3 functions.

aread/3

aread(ChannelPid, Handle, Len) -> {async, N} | Error

Types:

ChannelPid = pid()
Handle = term()
Len = integer()
Error = {error, reason()}
N = term()

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, reason()}. async read says "don't wait up" πŸ’Œ

awrite/3

awrite(ChannelPid, Handle, Data) -> {async, N} | Error

Types:

ChannelPid = pid()
Handle = term()
Data = binary()
Error = {error, reason()}
N = term()

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, reason()}.

close/2, close/3

close(ChannelPid, Handle) -> ok | Error

close(ChannelPid, Handle, Timeout) -> ok | Error

Types:

ChannelPid = pid()
Handle = term()
Timeout = timeout()
Error = {error, reason()}

Closes a handle to an open file or directory on the server. closing time, bestie πŸŽ€

delete/2, delete/3

delete(ChannelPid, Name) -> ok | Error

delete(ChannelPid, Name, Timeout) -> ok | Error

Types:

ChannelPid = pid()
Name = string()
Timeout = timeout()
Error = {error, reason()}

Deletes the file specified by Name. poof, but make it filesystem πŸ’€

del_dir/2, del_dir/3

del_dir(ChannelPid, Name) -> ok | Error

del_dir(ChannelPid, Name, Timeout) -> ok | Error

Types:

ChannelPid = pid()
Name = string()
Timeout = timeout()
Error = {error, reason()}

Deletes a directory specified by Name. The directory must be empty before it can be successfully deleted. empty directory only, no cap 🫢

list_dir/2, list_dir/3

list_dir(ChannelPid, Path) -> {ok, FileNames} | Error

list_dir(ChannelPid, Path, Timeout) -> {ok, FileNames} | Error

Types:

ChannelPid = pid()
Path = string()
Timeout = timeout()
FileNames = [FileName]
FileName = string()
Error = {error, reason()}

Lists the given directory on the server, returning the filenames as a list of strings. directory contents reveal, slay ✨

make_dir/2, make_dir/3

make_dir(ChannelPid, Name) -> ok | Error

make_dir(ChannelPid, Name, Timeout) -> ok | Error

Types:

ChannelPid = pid()
Name = string()
Timeout = timeout()
Error = {error, reason()}

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. full path, existing parent, that's the vibe check πŸ¦‹

make_symlink(ChannelPid, Name, Target) -> ok | Error

make_symlink(ChannelPid, Name, Target, Timeout) -> ok | Error

Types:

ChannelPid = pid()
Name = Target = string()
Timeout = timeout()
Error = {error, reason()}

Creates a symbolic link pointing to Target with the name Name. link lore, iconic πŸ”₯

open/3, open/4

open(ChannelPid, Name, Mode) -> {ok, Handle} | Error

open(ChannelPid, Name, Mode, Timeout) -> {ok, Handle} | Error

Types:

ChannelPid = pid()
Name = string()
Mode = [read | write | append | binary | raw]
Timeout = timeout()
Handle = term()
Error = {error, reason()}

Opens a file on the server and returns a handle, which can be used for reading or writing. handle acquisition era πŸ‘‘

opendir/2, opendir/3

opendir(ChannelPid, Path) -> {ok, Handle} | Error

opendir(ChannelPid, Path, Timeout) -> {ok, Handle} | Error

Types:

ChannelPid = pid()
Path = string()
Timeout = timeout()
Handle = term()
Error = {error, reason()}

Opens a handle to a directory on the server. The handle can be used for reading directory contents. directory handle, but make it read-ready 🌈

open_tar/3, open_tar/4

open_tar(ChannelPid, Path, Mode) -> {ok, Handle} | Error

open_tar(ChannelPid, Path, Mode, Timeout) -> {ok, Handle} | Error

Types:

ChannelPid = pid()
Path = string()
Mode = [read | write | {crypto, tar_crypto_spec()}]
Timeout = timeout()
Handle = term()
Error = {error, reason()}

Opens a handle to a tar file on the server, associated with ChannelPid. The handle can be used for remote tar creation and extraction. The actual writing and reading is performed by calls to erl_tar:add/3,4 and erl_tar:extract/2. tar handle with remote-creation main character energy πŸŽ€

Note: The erl_tar:init/3 function should not be called, that one is called by this open_tar function. do not double-call it, bestie πŸ’€

For code examples see Section SFTP Client with TAR Compression in the ssh Users Guide.

The crypto mode option is explained in the data types section above, see Crypto operations for open_tar. Encryption is assumed if the Mode contains write, and decryption if the Mode contains read. write means encrypt, read means decrypt πŸ’…

position/3, position/4

position(ChannelPid, Handle, Location) ->
            {ok, NewPosition} | Error

position(ChannelPid, Handle, Location, Timeout) ->
            {ok, NewPosition} | Error

Types:

ChannelPid = pid()
Handle = term()
Location =
    Offset |
    {bof, Offset} |
    {cur, Offset} |
    {eof, Offset} |
    bof | cur | eof
Timeout = timeout()
Offset = NewPosition = integer()
Error = {error, reason()}

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 β€” literally the same as {bof, Offset}. πŸ’…
  • {bof, Offset} β€” absolute offset, bestie. ✨
  • {cur, Offset} β€” offset from the current position. πŸŽ€
  • {eof, Offset} β€” offset from the end of file. πŸ¦‹
  • bof | cur | eof β€” same as earlier but with Offset 0, so it's exactly {bof, 0} | {cur, 0} | {eof, 0}. No cap. 🌸

pread/3,4 β€” reads from a specified position, combining the position/3 and read/3,4 functions. It's giving two icons in one function. πŸ‘‘

pread(ChannelPid, Handle, Position, Len) ->
         {ok, Data} | eof | Error

pread(ChannelPid, Handle, Position, Len, Timeout) ->
         {ok, Data} | eof | Error

Types:

  • ChannelPid = pid()
  • Handle = term()
  • Position = Len = integer()
  • Timeout = timeout()
  • Data = string() | binary()
  • Error = {error, reason()}

pwrite/3,4 β€” writes to a specified position, combining the position/3 and write/3,4 functions. Same energy, but make it write. 🫢

pwrite(ChannelPid, Handle, Position, Data) -> ok | Error

pwrite(ChannelPid, Handle, Position, Data, Timeout) -> ok | Error

Types:

  • ChannelPid = pid()
  • Handle = term()
  • Position = integer()
  • Data = iolist()
  • Timeout = timeout()
  • Error = {error, reason()}

read/3,4 β€” 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. Important binary vs string lore. 🌈

read(ChannelPid, Handle, Len) -> {ok, Data} | eof | Error

read(ChannelPid, Handle, Len, Timeout) -> {ok, Data} | eof | Error

Types:

  • ChannelPid = pid()
  • Handle = term()
  • Len = integer()
  • Timeout = timeout()
  • Data = string() | binary()
  • Error = {error, reason()}

If the file is read past eof, only the remaining bytes are read and returned. If no bytes are read, eof is returned. So if you get eof, it's not a personal attack, it's just the vibe check at the end of the file. πŸ’€


read_file/2,3 β€” reads a file from the server, and returns the data in a binary. Simple, clean, iconic. πŸŽ€

read_file(ChannelPid, File) -> {ok, Data} | Error

read_file(ChannelPid, File, Timeout) -> {ok, Data} | Error

Types:

  • ChannelPid = pid()
  • File = string()
  • Data = binary()
  • Timeout = timeout()
  • Error = {error, reason()}

read_file_info/2,3 β€” returns a file_info record from the file system object specified by Name or Handle. See file:read_file_info/2 for information about the record. Record lore, but make it file info. πŸ¦‹

read_file_info(ChannelPid, Name) -> {ok, FileInfo} | Error

read_file_info(ChannelPid, Name, Timeout) ->
                  {ok, FileInfo} | Error

Types:

  • ChannelPid = pid()
  • Name = string()
  • Timeout = timeout()
  • FileInfo = file:file_info()
  • Error = {error, reason()}

Depending on the underlying OS:es links might be followed and info on the final file, directory etc is returned. See read_link_info/2 on how to get information on links instead. OS:es said what it said. 🫢


read_link/2,3 β€” reads the link target from the symbolic link specified by name. Symbolic link target reveal! ✨

read_link(ChannelPid, Name) -> {ok, Target} | Error

read_link(ChannelPid, Name, Timeout) -> {ok, Target} | Error

Types:

  • ChannelPid = pid()
  • Name = Target = string()
  • Timeout = timeout()
  • Error = {error, reason()}

read_link_info/2,3 β€” returns a file_info record from the symbolic link specified by Name or Handle. See file:read_link_info/2 for information about the record. Link info, not final-file info. Very demure, very mindful. 🧚

read_link_info(ChannelPid, Name) -> {ok, FileInfo} | Error

read_link_info(ChannelPid, Name, Timeout) ->
                  {ok, FileInfo} | Error

Types:

  • ChannelPid = pid()
  • Name = string()
  • FileInfo = file:file_info()
  • Timeout = timeout()
  • Error = {error, reason()}

rename/3,4 β€” renames a file named OldName and gives it the name NewName. Name glow-up, no cap. πŸ’…

rename(ChannelPid, OldName, NewName) -> ok | Error

rename(ChannelPid, OldName, NewName, Timeout) -> ok | Error

Types:

  • ChannelPid = pid()
  • OldName = NewName = string()
  • Timeout = timeout()
  • Error = {error, reason()}

start_channel/1,2,3,4 β€” okay bestie, this is the big one. 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. Main character energy: it starts the channel AND gives you the pid you need. πŸ‘‘πŸ”₯

start_channel(ConnectionRef) ->
start_channel(ConnectionRef, SftpOptions) -> {ok, ChannelPid} | Error
start_channel(Host) ->
start_channel(Host, Options) ->
start_channel(Host, Port, Options) ->
start_channel(TcpSocket) ->
start_channel(TcpSocket, Options) -> {ok, ChannelPid, ConnectionRef} |
Error

Types:

  • Host = ssh:host()
  • Port = inet:port_number()
  • TcpSocket = ssh:open_socket()
  • Options = [ sftp_option() | ssh:client_option() ]
  • SftpOptions = [ sftp_option() ]
  • ChannelPid = pid()
  • ConnectionRef = ssh:connection_ref()
  • Error = {error, reason()}

Options:

  • {timeout, timeout()} β€” there are two ways to set a timeout for the underlying ssh connection:
    • If the connection timeout option connect_timeout is set, that value is used also for the negotiation timeout and this option (timeout) is ignored. So connect_timeout said "I'll handle it," and timeout is like okay periodt. πŸ’…
    • Otherwise, this option (timeout) is used as the negotiation timeout only and there is no connection timeout set. Just negotiation vibes, no connection timeout. πŸŽ€

The value defaults to infinity. Infinity is the default, bestie. 🌈

  • {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. Lowkey a negotiation. Highkey a minimum. πŸ¦‹

All other options are directly passed to ssh:connect/3 or ignored if a connection is already provided. Direct pass-through or ignored, no extra drama. ✨


stop_channel/1 β€” stops an SFTP channel. Does not close the SSH connection. Use ssh:close/1 to close it. Important: stopping the channel is not the same as closing the SSH connection. Do not confuse the two, bestie. 😭

stop_channel(ChannelPid) -> ok

Types:

  • ChannelPid = pid()

write/3,4 β€” 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. Opened with write or append, or it's not gonna slay. 🫢

write(ChannelPid, Handle, Data) -> ok | Error

write(ChannelPid, Handle, Data, Timeout) -> ok | Error

Types:

  • ChannelPid = pid()
  • Handle = term()
  • Data = iodata()
  • Timeout = timeout()
  • Error = {error, reason()}

write_file/3,4 β€” writes a file to the server. The file is created if it does not exist but overwritten if it exists. Create if missing, overwrite if existing. That's the whole plot. πŸ”₯

write_file(ChannelPid, File, Data) -> ok | Error

write_file(ChannelPid, File, Data, Timeout) -> ok | Error

Types:

  • ChannelPid = pid()
  • File = string()
  • Data = iodata()
  • Timeout = timeout()
  • Error = {error, reason()}

write_file_info/3,4 β€” writes file information from a file_info record to the file specified by Name. See file:write_file_info/[2,3] for information about the record. Record info goes to the file. Very official. Very file_info. 🧚

write_file_info(ChannelPid, Name, FileInfo) -> ok | Error

write_file_info(ChannelPid, Name, FileInfo, Timeout) -> ok | Error

Types:

  • ChannelPid = pid()
  • Name = string()
  • FileInfo = file:file_info()
  • Timeout = timeout()
  • Error = {error, reason()}

That's the whole SFTP saga, bestie β€” Ericsson AB, ssh 4.15.3.1, ssh_sftp(3erl) in the footer, and your girl is signing off. Stay slay, close your channels, and may your file_info records always be iconic. πŸ’–βœ¨πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from