SFTP
A subset of the SFTP API is wrapped and available in LibSSH.jl. See the SFTP example for an example of basic usage.
When it comes to handling paths, the library currently assumes that the server is running on a *NIX system. Some functions will not work when connecting to a Windows server.
Unlike the rest of the API, the SFTP C functions are blocking and only work with blocking Session's. This means that the library has to lock the session while calling them and no other operations (blocking or unblocking) can occur while they're being called. In practice this restriction may not be too onerous since most calls shouldn't take long anyway, and the read/write implementations use SFTP's asynchronous IO API so they shouldn't block for long. If it's critical that SFTP operations don't interfere with other operations (e.g. port forwarding) a workaround would be to open a separate Session for SFTP.
Note that we call all blocking C functions using @threadcall so that they don't block the scheduler, hence as a programmer you don't need to worry about them hogging a whole thread until they complete.
SftpSession
LibSSH.SftpSession — Typemutable struct SftpSessionThis represents a SFTP session, through which one can do SFTP operations. It is only usable while its parent Session is still open, and it must be closed explicitly with Base.close(::SftpSession) or it will leak memory.
LibSSH.SftpSession — MethodSftpSession(session::LibSSH.Session) -> LibSSH.SftpSession
Create a SftpSession from an existing Session.
Throws
ArgumentError: Ifsessionisn't open.LibSSHException: If creating the SFTP session fails.
LibSSH.SftpSession — MethodSftpSession(f::Function, args...; kwargs...) -> Any
Do-constructor, the function f will be called like f(sftp) with the new SftpSession.
Base.close — Methodclose(sftp::LibSSH.SftpSession)
Close an SftpSession. This will also close any open files.
Base.isopen — Methodisopen(sftp::LibSSH.SftpSession) -> Bool
Check if sftp is open.
Base.lock — Methodlock(sftp::LibSSH.SftpSession)
Lock an SftpSession.
Base.unlock — Methodunlock(sftp::LibSSH.SftpSession)
Unlock an SftpSession.
Base.Filesystem.readdir — Methodreaddir(
dir::AbstractString,
sftp::LibSSH.SftpSession;
only_names,
join,
sort
) -> Union{Vector{String}, Vector{LibSSH.SftpAttributes}}
Read the contents of a remote directory. By default this will behave the same as Base.readdir() and return a list of names, but if only_names=false it will return a list of SftpAttributes. The join and sort arguments are the same as in Base.readdir() but only apply when only_names=true.
Throws
ArgumentError: Ifsftpis closed.SftpException: If retrieving the directory contents failed.
Base.Filesystem.rm — Methodrm(
path::AbstractString,
sftp::LibSSH.SftpSession;
attrs,
recursive,
force
)
Delete remote file and directories. This has the same behaviour as Base.rm(), and the recursive and force options mean the same thing.
Internally the function will call Base.stat(::String, ::SftpSession) to determine how to delete path, but if you already have the result of that it can be passed to the attrs keyword argument to avoid the extra blocking call.
Throws
ArgumentError: Ifsftpis closed.Base.IOError: Ifpathis a non-empty directory andrecursive=false.SftpExceptionif deletion fails for some reason.
Base.Filesystem.mkdir — Methodmkdir(
path::AbstractString,
sftp::LibSSH.SftpSession;
mode
) -> AbstractString
Make a remote directory. This behaves in exactly the same way as Base.mkdir().
Throws
ArgumentError: Ifsftpis closed.SftpException: If making the directory fails.
Base.Filesystem.mkpath — Methodmkpath(
path::AbstractString,
sftp::LibSSH.SftpSession;
mode
) -> AbstractString
Create a remote path. This behaves in exactly the same way as Base.mkpath().
Throws
ArgumentError: Ifsftpis closed.SftpException: If making the path fails for some reason, such as part ofpathbeing an existing file.
Base.Filesystem.mv — Methodmv(
src::AbstractString,
dst::AbstractString,
sftp::LibSSH.SftpSession;
force
) -> AbstractString
Move src to dst remotely. Has the same behaviour as Base.mv().
Throws
ArgumentError: Ifsftpis closed.SftpException: If the operation fails for some reason.
Base.stat — Methodstat(
path::String,
sftp::LibSSH.SftpSession
) -> LibSSH.SftpAttributes
Get information about the file object at path as a SftpAttributes.
Note: the Demo.DemoServer does not support setting all of these properties.
Throws
ArgumentError: Ifsftpis closed.SftpException: If retrieving the file object information failed (e.g. if the path doesn't exist).
LibSSH.get_extensions — Methodget_extensions(
sftp::LibSSH.SftpSession
) -> Dict{String, String}
Get a list of supported server extensions and their versions.
Throws
ArgumentError: Ifsftpis closed.
LibSSH.get_limits — Methodget_limits(
sftp::LibSSH.SftpSession
) -> LibSSH.lib.sftp_limits_struct
Get the server limits. The returned object has the following fields:
max_packet_lengthmax_read_lengthmax_write_lengthmax_open_handles
Throws
ArgumentError: Ifsftpis closed.SftpException: If getting the limits failed.
LibSSH.get_error — Methodget_error(sftp::LibSSH.SftpSession) -> LibSSH.SftpError
Get the current error code for sftp.
Throws
ArgumentError: Ifsftpis closed.
SftpFile
LibSSH.SftpFile — Typemutable struct SftpFileptr::Union{Nothing, Ptr{LibSSH.lib.sftp_file_struct}}sftp::LibSSH.SftpSessionpath::Stringfullpath::Stringflags::@NamedTuple{read::Bool, write::Bool, create::Bool, truncate::Bool, append::Bool, exclusive::Bool}
Represents a remote file. This object must be explicitly closed with close() or it will leak memory. Don't create one of these yourself, use Base.open(::String, ::SftpSession).
Base.open — Methodopen(
path::String,
sftp::LibSSH.SftpSession;
read,
write,
create,
truncate,
append,
exclusive,
mode
) -> LibSSH.SftpFile
Open a remote file. Most of the keyword arguments behave in exactly the same way as their counterparts in Base.open(::String), except for exclusive and mode, which are unique to this method:
exclusive: Open a file with theO_EXCLflag.mode: Ifcreate=trueand the file doesn't exist, it will be created with permissions(mode & ~umask).
Throws
ArgumentError: Ifsftpis closed.SftpException: If opening the file fails.
Base.open — Methodopen(
f::Function,
path::String,
sftp::LibSSH.SftpSession;
kwargs...
) -> Any
Do-constructor for SftpFile's, the function f will be called like f(file).
Base.close — Methodclose(file::LibSSH.SftpFile)
Close file. This must be called explicitly, and not in a finalizer because it may cause a task switch.
Base.read — Methodread(file::LibSSH.SftpFile) -> Vector{UInt8}
read(file::LibSSH.SftpFile, nb::Integer) -> Vector{UInt8}
Read at most nb bytes from the remote SftpFile. Uses Base.read!(::SftpFile, ::Vector{UInt8}) internally.
Throws
ArgumentError: Iffileis closed.SftpException: If reading failed.
Base.read — Methodread(
filename::AbstractString,
sftp::LibSSH.SftpSession
) -> Vector{UInt8}
Base.read — Methodread(file::LibSSH.SftpFile, _::Type{String}) -> String
Read the whole file as a String.
Base.read — Methodread(
filename::AbstractString,
sftp::LibSSH.SftpSession,
_::Type{String}
) -> String
Base.read! — Methodread!(
file::LibSSH.SftpFile,
out::Vector{UInt8}
) -> Vector{UInt8}
Read length(out) bytes from the remote SftpFile into out. This uses libssh's asynchronous IO functions under the hood so it may launch multiple parallel requests.
Throws
ArgumentError: Iffileis closed.SftpException: If reading failed.
Base.write — Methodwrite(file::LibSSH.SftpFile, data::DenseVector) -> Any
Write data to the remote file and returns the number of bytes written. This uses libssh's asynchronous IO API so it may launch multiple parallel requests.
Throws
ArgumentError: Iffileis closed.SftpException: If writing fails.
Base.write — Methodwrite(file::LibSSH.SftpFile, data::AbstractString) -> Any
Write a string directly to file. Uses Base.write(::SftpFile, ::DenseVector) internally.
Base.isopen — Methodisopen(file::LibSSH.SftpFile) -> Bool
Check if file is open.
Base.isreadable — Methodisreadable(file::LibSSH.SftpFile) -> Bool
Check if file is open and readable.
Base.isreadonly — Methodisreadonly(file::LibSSH.SftpFile) -> Bool
Check if file is open and readonly.
Base.iswritable — Methodiswritable(file::LibSSH.SftpFile) -> Bool
Check if file is open and writable.
Base.position — Methodposition(file::LibSSH.SftpFile) -> UInt64
Get the current position in file.
Throws
ArgumentError: Iffileis closed.
Base.seek — Methodseek(file::LibSSH.SftpFile, pos::Integer)
Go to position pos in file. Note that this will not validate pos.
Throws
ArgumentError: Iffileis closed.
Base.seekstart — Methodseekstart(file::LibSSH.SftpFile)
Go to the beginning of file.
Base.seekend — Methodseekend(file::LibSSH.SftpFile)
Go to the end of file.
File type helpers
Base.Filesystem.ispath — Methodispath(
path::AbstractString,
sftp::LibSSH.SftpSession
) -> Bool
Base.Filesystem.ispath — Methodispath(_::LibSSH.SftpAttributes) -> Bool
Base.Filesystem.isdir — Methodisdir(
path::AbstractString,
sftp::LibSSH.SftpSession
) -> Bool
Base.Filesystem.isdir — Methodisdir(attrs::LibSSH.SftpAttributes) -> Bool
Base.Filesystem.isfile — Methodisfile(
path::AbstractString,
sftp::LibSSH.SftpSession
) -> Bool
Base.Filesystem.isfile — Methodisfile(attrs::LibSSH.SftpAttributes) -> Bool
Base.Filesystem.issocket — Methodissocket(
path::AbstractString,
sftp::LibSSH.SftpSession
) -> Bool
Base.Filesystem.issocket — Methodissocket(attrs::LibSSH.SftpAttributes) -> Bool
Base.Filesystem.islink — Methodislink(
path::AbstractString,
sftp::LibSSH.SftpSession
) -> Bool
Base.Filesystem.islink — Methodislink(attrs::LibSSH.SftpAttributes) -> Bool
Base.Filesystem.isblockdev — Methodisblockdev(
path::AbstractString,
sftp::LibSSH.SftpSession
) -> Bool
Base.Filesystem.isblockdev — Methodisblockdev(attrs::LibSSH.SftpAttributes) -> Bool
Base.Filesystem.ischardev — Methodischardev(
path::AbstractString,
sftp::LibSSH.SftpSession
) -> Bool
Base.Filesystem.ischardev — Methodischardev(attrs::LibSSH.SftpAttributes) -> Bool
Base.Filesystem.isfifo — Methodisfifo(
path::AbstractString,
sftp::LibSSH.SftpSession
) -> Bool
Base.Filesystem.isfifo — Methodisfifo(attrs::LibSSH.SftpAttributes) -> Bool
Other types
LibSSH.SftpAttributes — Typemutable struct SftpAttributesAttributes of remote file objects. This has the following (read-only) properties:
name::Stringlongname::Stringflags::UInt32type::UInt8size::UInt64uid::UInt32gid::UInt32owner::Stringgroup::Stringpermissions::UInt32atime64::UInt64atime::UInt32atime_nseconds::UInt32createtime::UInt64createtime_nseconds::UInt32mtime64::UInt64mtime::UInt32mtime_nseconds::UInt32acl::Stringextended_count::UInt32extended_type::Stringextended_data::String
LibSSH.SftpError — Typeprimitive type SftpError <: Enum{Int32} 32Enum for possible SFTP error codes. Note that despite its name, SftpError_Ok does not indicate an error.
SftpError_OkSftpError_EofSftpError_NoSuchFileSftpError_PermissionDeniedSftpError_GenericFailureSftpError_BadMessageSftpError_NoConnectionSftpError_ConnectionLostSftpError_OpUnsupportedSftpError_InvalidHandleSftpError_NoSuchPathSftpError_FileAlreadyExistsSftpError_WriteProtectSftpError_NoMedia
LibSSH.SftpException — Typestruct SftpException <: Exceptionmsg::Stringpath::Union{Nothing, String}error_code::LibSSH.SftpErrorsession_error::Stringsession_userhost::String
Represents an error from the SFTP subsystem.
When an SftpException is printed it will be displayed like this:
julia> import LibSSH as sshjulia> ssh.SftpException("Failure", "/tmp/foo", ssh.SftpError_GenericFailure, "SFTP failed", "foo@bar")SftpException: Failure (SftpError_GenericFailure) Session error: 'SFTP failed' User/host: foo@bar Remote path: /tmp/foo
Note that the SFTP subsystem doesn't always set an error on the Session, so take the Session error with a grain of salt.