The libssh SFTP API
Overview
SFTP handling functions. More…
// structs struct sftp_attributes_struct; struct sftp_client_message_struct; struct sftp_dir_struct; struct sftp_file_struct; struct sftp_message_struct; struct sftp_packet_struct; struct sftp_request_queue_struct; struct sftp_session_struct; struct sftp_status_message_struct; struct sftp_statvfs_struct; // global functions int sftp_async_read( sftp_file file, void* data, uint32_t len, uint32_t id ); int sftp_async_read_begin( sftp_file file, uint32_t len ); void sftp_attributes_free(sftp_attributes file); char* sftp_canonicalize_path( sftp_session sftp, const char* path ); int sftp_chmod( sftp_session sftp, const char* file, mode_t mode ); int sftp_chown( sftp_session sftp, const char* file, uid_t owner, gid_t group ); int sftp_close(sftp_file file); int sftp_closedir(sftp_dir dir); int sftp_dir_eof(sftp_dir dir); int sftp_extension_supported( sftp_session sftp, const char* name, const char* data ); unsigned int sftp_extensions_get_count(sftp_session sftp); const char* sftp_extensions_get_data( sftp_session sftp, unsigned int indexn ); const char* sftp_extensions_get_name( sftp_session sftp, unsigned int indexn ); void sftp_file_set_blocking(sftp_file handle); void sftp_file_set_nonblocking(sftp_file handle); void sftp_free(sftp_session sftp); sftp_attributes sftp_fstat(sftp_file file); sftp_statvfs_t sftp_fstatvfs(sftp_file file); int sftp_get_error(sftp_session sftp); int sftp_init(sftp_session sftp); sftp_attributes sftp_lstat( sftp_session session, const char* path ); int sftp_mkdir( sftp_session sftp, const char* directory, mode_t mode ); sftp_session sftp_new(ssh_session session); sftp_session sftp_new_channel( ssh_session session, ssh_channel channel ); sftp_file sftp_open( sftp_session session, const char* file, int accesstype, mode_t mode ); sftp_dir sftp_opendir( sftp_session session, const char* path ); ssize_t sftp_read( sftp_file file, void* buf, size_t count ); sftp_attributes sftp_readdir( sftp_session session, sftp_dir dir ); char* sftp_readlink( sftp_session sftp, const char* path ); int sftp_rename( sftp_session sftp, const char* original, const char* newname ); void sftp_rewind(sftp_file file); int sftp_rmdir( sftp_session sftp, const char* directory ); int sftp_seek( sftp_file file, uint32_t new_offset ); int sftp_seek64( sftp_file file, uint64_t new_offset ); int sftp_server_init(sftp_session sftp); sftp_session sftp_server_new( ssh_session session, ssh_channel chan ); int sftp_server_version(sftp_session sftp); int sftp_setstat( sftp_session sftp, const char* file, sftp_attributes attr ); sftp_attributes sftp_stat( sftp_session session, const char* path ); sftp_statvfs_t sftp_statvfs( sftp_session sftp, const char* path ); void sftp_statvfs_free(sftp_statvfs_t statvfs_o); int sftp_symlink( sftp_session sftp, const char* target, const char* dest ); unsigned long sftp_tell(sftp_file file); uint64_t sftp_tell64(sftp_file file); int sftp_unlink( sftp_session sftp, const char* file ); int sftp_utimes( sftp_session sftp, const char* file, const struct timeval* times ); ssize_t sftp_write( sftp_file file, const void* buf, size_t count ); int buffer_add_attributes( ssh_buffer buffer, sftp_attributes attr ); void sftp_packet_free(sftp_packet packet); sftp_packet sftp_packet_read(sftp_session sftp); int sftp_packet_write( sftp_session sftp, uint8_t type, ssh_buffer payload ); sftp_attributes sftp_parse_attr( sftp_session session, ssh_buffer buf, int expectname ); void sftp_client_message_free(sftp_client_message msg); const char* sftp_client_message_get_data(sftp_client_message msg); const char* sftp_client_message_get_filename(sftp_client_message msg); uint32_t sftp_client_message_get_flags(sftp_client_message msg); uint8_t sftp_client_message_get_type(sftp_client_message msg); void sftp_client_message_set_filename( sftp_client_message msg, const char* newname ); sftp_client_message sftp_get_client_message(sftp_session sftp); void* sftp_handle( sftp_session sftp, ssh_string handle ); ssh_string sftp_handle_alloc( sftp_session sftp, void* info ); void sftp_handle_remove( sftp_session sftp, void* handle ); int sftp_reply_attr( sftp_client_message msg, sftp_attributes attr ); int sftp_reply_data( sftp_client_message msg, const void* data, int len ); int sftp_reply_handle( sftp_client_message msg, ssh_string handle ); int sftp_reply_name( sftp_client_message msg, const char* name, sftp_attributes attr ); int sftp_reply_names(sftp_client_message msg); int sftp_reply_names_add( sftp_client_message msg, const char* file, const char* longname, sftp_attributes attr ); int sftp_reply_status( sftp_client_message msg, uint32_t status, const char* message ); int sftp_send_client_message( sftp_session sftp, sftp_client_message msg ); // macros #define LIBSFTP_VERSION #define SFTP_CLOSE #define SFTP_FSETSTAT #define SFTP_FSTAT #define SFTP_HANDLES #define SFTP_LSTAT #define SFTP_MKDIR #define SFTP_OPEN #define SFTP_OPENDIR #define SFTP_READ #define SFTP_READDIR #define SFTP_READLINK #define SFTP_REALPATH #define SFTP_REMOVE #define SFTP_RENAME #define SFTP_RMDIR #define SFTP_SETSTAT #define SFTP_STAT #define SFTP_SYMLINK #define SFTP_WRITE #define SSH_FILEXFER_ATTR_ACCESSTIME #define SSH_FILEXFER_ATTR_ACL #define SSH_FILEXFER_ATTR_ACMODTIME #define SSH_FILEXFER_ATTR_CREATETIME #define SSH_FILEXFER_ATTR_EXTENDED #define SSH_FILEXFER_ATTR_MODIFYTIME #define SSH_FILEXFER_ATTR_OWNERGROUP #define SSH_FILEXFER_ATTR_PERMISSIONS #define SSH_FILEXFER_ATTR_SIZE #define SSH_FILEXFER_ATTR_SUBSECOND_TIMES #define SSH_FILEXFER_ATTR_UIDGID #define SSH_FILEXFER_TYPE_DIRECTORY #define SSH_FILEXFER_TYPE_REGULAR #define SSH_FILEXFER_TYPE_SPECIAL #define SSH_FILEXFER_TYPE_SYMLINK #define SSH_FILEXFER_TYPE_UNKNOWN #define SSH_FXE_STATVFS_ST_NOSUID #define SSH_FXE_STATVFS_ST_RDONLY #define SSH_FXF_APPEND #define SSH_FXF_CREAT #define SSH_FXF_EXCL #define SSH_FXF_READ #define SSH_FXF_RENAME_ATOMIC #define SSH_FXF_RENAME_NATIVE #define SSH_FXF_RENAME_OVERWRITE #define SSH_FXF_TEXT #define SSH_FXF_TRUNC #define SSH_FXF_WRITE #define SSH_FXP_ATTRS #define SSH_FXP_CLOSE #define SSH_FXP_DATA #define SSH_FXP_EXTENDED #define SSH_FXP_EXTENDED_REPLY #define SSH_FXP_FSETSTAT #define SSH_FXP_FSTAT #define SSH_FXP_HANDLE #define SSH_FXP_INIT #define SSH_FXP_LSTAT #define SSH_FXP_MKDIR #define SSH_FXP_NAME #define SSH_FXP_OPEN #define SSH_FXP_OPENDIR #define SSH_FXP_READ #define SSH_FXP_READDIR #define SSH_FXP_READLINK #define SSH_FXP_REALPATH #define SSH_FXP_REMOVE #define SSH_FXP_RENAME #define SSH_FXP_RMDIR #define SSH_FXP_SETSTAT #define SSH_FXP_STAT #define SSH_FXP_STATUS #define SSH_FXP_SYMLINK #define SSH_FXP_VERSION #define SSH_FXP_WRITE #define SSH_FX_BAD_MESSAGE #define SSH_FX_CONNECTION_LOST #define SSH_FX_EOF #define SSH_FX_FAILURE #define SSH_FX_FILE_ALREADY_EXISTS #define SSH_FX_INVALID_HANDLE #define SSH_FX_NO_CONNECTION #define SSH_FX_NO_MEDIA #define SSH_FX_NO_SUCH_FILE #define SSH_FX_NO_SUCH_PATH #define SSH_FX_OK #define SSH_FX_OP_UNSUPPORTED #define SSH_FX_PERMISSION_DENIED #define SSH_FX_WRITE_PROTECT
Detailed Documentation
SFTP handling functions.
SFTP commands are channeled by the ssh sftp subsystem. Every packet is sent/read using a sftp_packet type structure. Related to these packets, most of the server answers are messages having an ID and a message specific part. It is described by sftp_message when reading a message, the sftp system puts it into the queue, so the process having asked for it can fetch it, while continuing to read for other messages (it is unspecified in which order messages may be sent back to the client
Global Functions
int sftp_async_read( sftp_file file, void* data, uint32_t len, uint32_t id )
Wait for an asynchronous read to complete and save the data.
Warning
A call to this function with an invalid identifier will never return.
Parameters:
file |
The opened sftp file handle to be read from. |
data |
Pointer to buffer to recieve read data. |
len |
Size of the buffer in bytes. It should be bigger or equal to the length parameter of the sftp_async_read_begin() call. |
id |
The identifier returned by the sftp_async_read_begin() function. |
Returns:
Number of bytes read, 0 on EOF, SSH_ERROR if an error occured, SSH_AGAIN if the file is opened in nonblocking mode and the request hasn’t been executed yet.
See also:
int sftp_async_read_begin( sftp_file file, uint32_t len )
Start an asynchronous read from a file using an opened sftp file handle.
Its goal is to avoid the slowdowns related to the request/response pattern of a synchronous read. To do so, you must call 2 functions:
sftp_async_read_begin() and sftp_async_read().
The first step is to call sftp_async_read_begin(). This function returns a request identifier. The second step is to call sftp_async_read() using the returned identifier.
Warning
When calling this function, the internal offset is updated corresponding to the len parameter.
Warning
A call to sftp_async_read_begin() sends a request to the server. When the server answers, libssh allocates memory to store it until sftp_async_read() is called. Not calling sftp_async_read() will lead to memory leaks.
Parameters:
file |
The opened sftp file handle to be read from. |
len |
Size to read in bytes. |
Returns:
An identifier corresponding to the sent request, < 0 on error.
See also:
void sftp_attributes_free(sftp_attributes file)
Free a sftp attribute structure.
Parameters:
file |
The sftp attribute structure to free. |
char* sftp_canonicalize_path( sftp_session sftp, const char* path )
Canonicalize a sftp path.
Parameters:
sftp |
The sftp session handle. |
path |
The path to be canonicalized. |
Returns:
The canonicalize path, NULL on error.
int sftp_chmod( sftp_session sftp, const char* file, mode_t mode )
Change permissions of a file.
Parameters:
sftp |
The sftp session handle. |
file |
The file which owner and group should be changed. |
mode |
Specifies the permissions to use. It is modified by the process’s umask in the usual way: The permissions of the created file are (mode & ~umask) |
Returns:
0 on success, < 0 on error with ssh and sftp error set.
See also:
int sftp_chown( sftp_session sftp, const char* file, uid_t owner, gid_t group )
Change the file owner and group.
Parameters:
sftp |
The sftp session handle. |
file |
The file which owner and group should be changed. |
owner |
The new owner which should be set. |
group |
The new group which should be set. |
Returns:
0 on success, < 0 on error with ssh and sftp error set.
See also:
int sftp_close(sftp_file file)
Close an open file handle.
Parameters:
file |
The open sftp file handle to close. |
Returns:
Returns SSH_NO_ERROR or SSH_ERROR if an error occured.
See also:
int sftp_closedir(sftp_dir dir)
Close a directory handle opened by sftp_opendir().
Parameters:
dir |
The sftp directory handle to close. |
Returns:
Returns SSH_NO_ERROR or SSH_ERROR if an error occured.
int sftp_dir_eof(sftp_dir dir)
Tell if the directory has reached EOF (End Of File).
Parameters:
dir |
The sftp directory handle. |
Returns:
1 if the directory is EOF, 0 if not.
See also:
int sftp_extension_supported( sftp_session sftp, const char* name, const char* data )
Check if the given extension is supported.
Example:
sftp_extension_supported(sftp, "statvfs@openssh.com", "2");
Parameters:
sftp |
The sftp session to use. |
name |
The name of the extension. |
data |
The data of the extension. |
Returns:
1 if supported, 0 if not.
unsigned int sftp_extensions_get_count(sftp_session sftp)
Get the count of extensions provided by the server.
Parameters:
sftp |
The sftp session to use. |
Returns:
The count of extensions provided by the server, 0 on error or not available.
const char* sftp_extensions_get_data( sftp_session sftp, unsigned int indexn )
Get the data of the extension provided by the server.
This is normally the version number of the extension.
Parameters:
sftp |
The sftp session to use. |
indexn |
The index number of the extension data you want. |
Returns:
The data of the extension.
const char* sftp_extensions_get_name( sftp_session sftp, unsigned int indexn )
Get the name of the extension provided by the server.
Parameters:
sftp |
The sftp session to use. |
indexn |
The index number of the extension name you want. |
Returns:
The name of the extension.
void sftp_file_set_blocking(sftp_file handle)
Make the sftp communication for this file handle blocking.
Parameters:
handle |
The file handle to set blocking. |
void sftp_file_set_nonblocking(sftp_file handle)
Make the sftp communication for this file handle non blocking.
Parameters:
handle |
The file handle to set non blocking. |
void sftp_free(sftp_session sftp)
Close and deallocate a sftp session.
Parameters:
sftp |
The sftp session handle to free. |
sftp_attributes sftp_fstat(sftp_file file)
Get information about a file or directory from a file handle.
Parameters:
file |
The sftp file handle to get the stat information. |
Returns:
The sftp attributes structure of the file or directory, NULL on error with ssh and sftp error set.
See also:
sftp_statvfs_t sftp_fstatvfs(sftp_file file)
Get information about a mounted file system.
Parameters:
file |
An opened file. |
Returns:
A statvfs structure or NULL on error.
See also:
int sftp_get_error(sftp_session sftp)
Get the last sftp error.
Use this function to get the latest error set by a posix like sftp function.
Parameters:
sftp |
The sftp session where the error is saved. |
Returns:
The saved error (see server responses), < 0 if an error in the function occured.
See also:
Server responses
int sftp_init(sftp_session sftp)
Initialize the sftp session with the server.
Parameters:
sftp |
The sftp session to initialize. |
Returns:
0 on success, < 0 on error with ssh error set.
See also:
sftp_attributes sftp_lstat( sftp_session session, const char* path )
Get information about a file or directory.
Identical to sftp_stat, but if the file or directory is a symbolic link, then the link itself is stated, not the file that it refers to.
Parameters:
session |
The sftp session handle. |
path |
The path to the file or directory to obtain the information. |
Returns:
The sftp attributes structure of the file or directory, NULL on error with ssh and sftp error set.
See also:
int sftp_mkdir( sftp_session sftp, const char* directory, mode_t mode )
Create a directory.
Parameters:
sftp |
The sftp session handle. |
directory |
The directory to create. |
mode |
Specifies the permissions to use. It is modified by the process’s umask in the usual way: The permissions of the created file are (mode & ~umask) |
Returns:
0 on success, < 0 on error with ssh and sftp error set.
See also:
sftp_session sftp_new(ssh_session session)
Start a new sftp session.
Parameters:
session |
The ssh session to use. |
Returns:
A new sftp session or NULL on error.
See also:
sftp_session sftp_new_channel( ssh_session session, ssh_channel channel )
Start a new sftp session with an existing channel.
Parameters:
session |
The ssh session to use. |
channel |
An open session channel with subsystem already allocated |
Returns:
A new sftp session or NULL on error.
See also:
sftp_file sftp_open( sftp_session session, const char* file, int accesstype, mode_t mode )
Open a file on the server.
Parameters:
session |
The sftp session handle. |
file |
The file to be opened. |
accesstype |
Is one of O_RDONLY, O_WRONLY or O_RDWR which request opening the file read-only,write-only or read/write. Acesss may also be bitwise-or’d with one or more of the following: O_CREAT - If the file does not exist it will be created. O_EXCL - When used with O_CREAT, if the file already exists it is an error and the open will fail. O_TRUNC - If the file already exists it will be truncated. |
mode |
Mode specifies the permissions to use if a new file is created. It is modified by the process’s umask in the usual way: The permissions of the created file are (mode & ~umask) |
Returns:
A sftp file handle, NULL on error with ssh and sftp error set.
See also:
sftp_dir sftp_opendir( sftp_session session, const char* path )
Open a directory used to obtain directory entries.
Parameters:
session |
The sftp session handle to open the directory. |
path |
The path of the directory to open. |
Returns:
A sftp directory handle or NULL on error with ssh and sftp error set.
See also:
ssize_t sftp_read( sftp_file file, void* buf, size_t count )
Read from a file using an opened sftp file handle.
Parameters:
file |
The opened sftp file handle to be read from. |
buf |
Pointer to buffer to recieve read data. |
count |
Size of the buffer in bytes. |
Returns:
Number of bytes written, < 0 on error with ssh and sftp error set.
See also:
sftp_attributes sftp_readdir( sftp_session session, sftp_dir dir )
Get a single file attributes structure of a directory.
Parameters:
session |
The sftp session handle to read the directory entry. |
dir |
The opened sftp directory handle to read from. |
Returns:
A file attribute structure or NULL at the end of the directory.
See also:
sftp_attribute_free()
char* sftp_readlink( sftp_session sftp, const char* path )
Read the value of a symbolic link.
Parameters:
sftp |
The sftp session handle. |
path |
Specifies the path name of the symlink to be read. |
Returns:
The target of the link, NULL on error.
See also:
int sftp_rename( sftp_session sftp, const char* original, const char* newname )
Rename or move a file or directory.
Parameters:
sftp |
The sftp session handle. |
original |
The original url (source url) of file or directory to be moved. |
newname |
The new url (destination url) of the file or directory after the move. |
Returns:
0 on success, < 0 on error with ssh and sftp error set.
See also:
void sftp_rewind(sftp_file file)
Rewinds the position of the file pointer to the beginning of the file.
Parameters:
file |
Open sftp file handle. |
int sftp_rmdir( sftp_session sftp, const char* directory )
Remove a directoy.
Parameters:
sftp |
The sftp session handle. |
directory |
The directory to remove. |
Returns:
0 on success, < 0 on error with ssh and sftp error set.
See also:
int sftp_seek( sftp_file file, uint32_t new_offset )
Seek to a specific location in a file.
Parameters:
file |
Open sftp file handle to seek in. |
new_offset |
Offset in bytes to seek. |
Returns:
0 on success, < 0 on error.
int sftp_seek64( sftp_file file, uint64_t new_offset )
Seek to a specific location in a file.
This is the 64bit version.
Parameters:
file |
Open sftp file handle to seek in. |
new_offset |
Offset in bytes to seek. |
Returns:
0 on success, < 0 on error.
int sftp_server_init(sftp_session sftp)
Intialize the sftp server.
Parameters:
sftp |
The sftp session to init. |
Returns:
0 on success, < 0 on error.
sftp_session sftp_server_new( ssh_session session, ssh_channel chan )
Create a new sftp server session.
Parameters:
session |
The ssh session to use. |
chan |
The ssh channel to use. |
Returns:
A new sftp server session.
int sftp_server_version(sftp_session sftp)
Get the version of the SFTP protocol supported by the server.
Parameters:
sftp |
The sftp session handle. |
Returns:
The server version.
int sftp_setstat( sftp_session sftp, const char* file, sftp_attributes attr )
Set file attributes on a file, directory or symbolic link.
Parameters:
sftp |
The sftp session handle. |
file |
The file which attributes should be changed. |
attr |
The file attributes structure with the attributes set which should be changed. |
Returns:
0 on success, < 0 on error with ssh and sftp error set.
See also:
sftp_attributes sftp_stat( sftp_session session, const char* path )
Get information about a file or directory.
Parameters:
session |
The sftp session handle. |
path |
The path to the file or directory to obtain the information. |
Returns:
The sftp attributes structure of the file or directory, NULL on error with ssh and sftp error set.
See also:
sftp_statvfs_t sftp_statvfs( sftp_session sftp, const char* path )
Get information about a mounted file system.
Parameters:
sftp |
The sftp session handle. |
path |
The pathname of any file within the mounted file system. |
Returns:
A statvfs structure or NULL on error.
See also:
void sftp_statvfs_free(sftp_statvfs_t statvfs_o)
Free the memory of an allocated statvfs.
Parameters:
statvfs_o |
The statvfs to free. |
int sftp_symlink( sftp_session sftp, const char* target, const char* dest )
Create a symbolic link.
Parameters:
sftp |
The sftp session handle. |
target |
Specifies the target of the symlink. |
dest |
Specifies the path name of the symlink to be created. |
Returns:
0 on success, < 0 on error with ssh and sftp error set.
See also:
unsigned long sftp_tell(sftp_file file)
Report current byte position in file.
Parameters:
file |
Open sftp file handle. |
Returns:
The offset of the current byte relative to the beginning of the file associated with the file descriptor. < 0 on error.
uint64_t sftp_tell64(sftp_file file)
Report current byte position in file.
Parameters:
file |
Open sftp file handle. |
Returns:
The offset of the current byte relative to the beginning of the file associated with the file descriptor. < 0 on error.
int sftp_unlink( sftp_session sftp, const char* file )
Unlink (delete) a file.
Parameters:
sftp |
The sftp session handle. |
file |
The file to unlink/delete. |
Returns:
0 on success, < 0 on error with ssh and sftp error set.
See also:
int sftp_utimes( sftp_session sftp, const char* file, const struct timeval* times )
Change the last modification and access time of a file.
Parameters:
sftp |
The sftp session handle. |
file |
The file which owner and group should be changed. |
times |
A timeval structure which contains the desired access and modification time. |
Returns:
0 on success, < 0 on error with ssh and sftp error set.
See also:
ssize_t sftp_write( sftp_file file, const void* buf, size_t count )
Write to a file using an opened sftp file handle.
Parameters:
file |
Open sftp file handle to write to. |
buf |
Pointer to buffer to write data. |
count |
Size of buffer in bytes. |
Returns:
Number of bytes written, < 0 on error with ssh and sftp error set.
See also:
Macros
#define SSH_FX_BAD_MESSAGE
Garbage received from server.
#define SSH_FX_CONNECTION_LOST
There was a connection, but we lost it.
#define SSH_FX_EOF
End-of-file encountered.
#define SSH_FX_FAILURE
Generic failure.
#define SSH_FX_FILE_ALREADY_EXISTS
An attempt to create an already existing file or directory has been made.
#define SSH_FX_INVALID_HANDLE
Invalid file handle.
#define SSH_FX_NO_CONNECTION
No connection has been set up.
#define SSH_FX_NO_MEDIA
No media in remote drive.
#define SSH_FX_NO_SUCH_FILE
File doesn’t exist.
#define SSH_FX_NO_SUCH_PATH
No such file or directory path exists.
#define SSH_FX_OK
No error.
#define SSH_FX_OP_UNSUPPORTED
Operation not supported by the server.
#define SSH_FX_PERMISSION_DENIED
Permission denied.
#define SSH_FX_WRITE_PROTECT
We are trying to write on a write-protected filesystem.