20052018 Ericsson AB. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ssh_sftp OTP 2005-09-22 ssh_sftp.sgml
ssh_sftp SFTP client.

This module implements an SSH FTP (SFTP) client. SFTP is a secure, encrypted file transfer service available for SSH.

Error cause

A description of the reason why an operation failed.

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 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 are other errors like for example {exit_status,1}.

Crypto operations for open_tar

Specifies the encryption or decryption applied to tar files when using open_tar/3 or open_tar/4.

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.

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.

If the selected cipher needs to have the input data partioned 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.

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().

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

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.

Reads asynchronously from an open file.

The function reads from a specified position, combining the position/3 and aread/3 functions.

Writes asynchronously to an open file.

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

Reads asynchronously from an open file.

Reads from an open file, without waiting for the result. If the handle is valid, the function returns , where N is a term guaranteed to be unique between calls of . The actual data is sent as a message to the calling process. This message has the form , where is the result from the read, either , , or .

Writes asynchronously to an open file.

Writes to an open file, without waiting for the result. If the handle is valid, the function returns , where N is a term guaranteed to be unique between calls of . The result of the operation is sent as a message to the calling process. This message has the form , where is the result from the write, either , or .

Closes an open handle.

Closes a handle to an open file or directory on the server.

Deletes a file.

Deletes the file specified by .

Deletes an empty directory.

Deletes a directory specified by . The directory must be empty before it can be successfully deleted.

Lists the directory.

Lists the given directory on the server, returning the filenames as a list of strings.

Creates a directory.

Creates a directory specified by . must be a full path to a new directory. The directory can only be created in an existing directory.

Creates a symbolic link.

Creates a symbolic link pointing to with the name .

Opens a file and returns a handle.

Opens a file on the server and returns a handle, which can be used for reading or writing.

Opens a directory and returns a handle.

Opens a handle to a directory on the server. The handle can be used for reading directory contents.

Opens a tar file on the server to which ChannelPid is connected and returns a handle.

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. Note: The erl_tar:init/3 function should not be called, that one is called by this open_tar function.

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.

Sets the file position of a file.

Sets the file position of the file referenced by . Returns (as an absolute offset) if successful, otherwise . is one of the following:

The same as .

Absolute offset.

Offset from the current position.

Offset from the end of file.

The same as eariler with 0, that is, .

Reads from an open file.

The function reads from a specified position, combining the position/3 and read/3,4 functions.

Writes to an open file.

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

Reads from an open file.

Reads bytes from the file referenced by . Returns , , or . If the file is opened with , 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, is returned.

Reads a file.

Reads a file from the server, and returns the data in a binary.

Gets information about a file.

Returns a record from the file system object specified by or . See file:read_file_info/2 for information about the record.

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.

Reads symbolic link.

Reads the link target from the symbolic link specified by .

Gets information about a symbolic link.

Returns a record from the symbolic link specified by or . See file:read_link_info/2 for information about the record.

Renames a file.

Renames a file named and gives it the name .

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 Starts an SFTP client. 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()}

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:

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. Otherwise, this option (timeout) is used as the negotiation timeout only and there is no connection timeout set

The value defaults to infinity.

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.

Stops the SFTP client channel.

Stops an SFTP channel. Does not close the SSH connection. Use ssh:close/1 to close it.

Writes to an open file.

Writes to the file referenced by . The file is to be opened with or flag. Returns if successful or otherwise.

Writes a file.

Writes a file to the server. The file is created if it does not exist but overwritten if it exists.

Writes information for a file.

Writes file information from a record to the file specified by . See file:write_file_info/[2,3] for information about the record.