Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
UppHub
Status & Roadmap
FAQ
Authors & License
Forums
Funding U++
Search on this site











SourceForge.net Logo

SourceForge.net Logo

GitHub Logo

Discord Logo

SFtp

 

class SFtp public Ssh

This class encapsulates an SSH2 SFTP subsystem. It allows for a range of operations on remote file system objects, such as file/directory creation, deletion, renaming, file transfers, directory listings etc. SFtp class is derived from Ssh base class, and has pick semantics. See also SFtp::DirEntry, SFtpFileSystemInfo, and SFtpStream  classes.

 

 

Public Method List

 

SFtp& Timeout(int ms)

Sets timeout value in miliseconds. Setting the timeout value to Null puts the SFtp object into blocking mode

 


 

SFtp& ChunkSize(int sz)

Sets the chunk size to sz for data reads and writes. The default chunk size is 64K Returns *this for method chaining.

 


 

LIBSSH2_SFTP_HANDLE* GetHandle() const

Returns a pointer to the libssh2 sftp session handle on success, NULL on failure.

 


 

SFtpHandle Open(const String& path, dword flags, long mode)

Opens the remote file or directory at path with access flags and mode. Returns a file handle on success, and NULL on failure. File handles obtained using this method must be released using the Close() method when finished.

 

Filesystem access flags:

 

READ

Opens file in read mode.

WRITE

Opens file in write mode.

APPEND

Opens file in append mode.

CREATE

Creates file if it doesn't exist.

EXCLUSIVE

Causes  the request to fail if the named file already exists. CREATE flag must also be specified if this flag is used.

TRUNCATE

Opens file and truncates it to zero (0) bytes. Must be used with CREATE flag.

 

Filesystem access modes (permissions):

 

IRUSR, IWUSR, IXUSR, IRWXU

User permissions.

IRGRP, IWGRP, IXGRP, IRWXG

Group permissions

IROTH, IWOTH, IXOTH, IRWXO

Permissions for others.

IRALL

Read access for all.

IWALL

Write access for all.

IXALL

Executable by all.

IRWXA

Unrestricted access for all.

 


 

SFtpHandle OpenRead(const String& path)

Opens remote path for reading. Returns a file handle on success, and NULL on failure. File handles obtained using this method must be released using the Close() method when finished.

 


 

SFtpHandle OpenWrite(const String& path)

Opens remote path for writing. File will be created if it doesn't exist. Returns a file handle on success, and NULL on failure. File handles obtained using this method must be released using the Close() method when finished.

 


 

void Close(SFtpHandle handle)

Closes the file handle. File handles obtained with Open() or OpenDir() methods must be released using this method when finished.

 


 

bool Rename(const String& oldpath, const String& newpath)

Renames a file or directory from oldpath to newpath. Returns true on success.

 


 

bool Delete(const String& path)

Deletes the file at path. Returns true on success.

 


 

bool Sync(SFtpHandle handle)

Synchronizes the file pointed by handle to the remote storage device. Returns true on success.

 


 

SFtp& Seek(SFtpHandle handle, int64 position)

Seeks to position in a remote file pointed by handle. Returns *this for method chaining.

 


 

int64 GetPos(SFtpHandle handle)

Returns the current position (in bytes) from the beginning of a remote file pointed by handle.

 


 

SFtpHandle OpenDir(const String& path)

Opens the directory at path. Returns a file handle on success, and NULL on failure. File handles obtained using this method must be released using Close() method when finished.

 


 

bool MakeDir(const String& path, long mode)

Creates a directory at path with access mode. Returns true on success.

 


 

bool RemoveDir(const String& path)

Deletes a remote directory at path. Return true on success.

 


 

bool ListDir(SFtpHandle handle, DirList& list)

lists the content of remote directory pointed by the file handle. Returns true on success. This method will return true even when the remote directory is empty.

 


 

bool ListDir(const String& path, DirList& list)

Overload. lists the content of remote path. Returns true on success. This method will return true even when the remote directory is empty.

 


 

bool MakeLink(const String& orig, String& target)

Creates a symbolic link to the originating file system object. Returns true on success.

 


 

bool ReadLink(const String& path, String& target)

Resoves a symbolic link at path to its next target. Returns true on success.

 


 

bool RealizePath(const String& path, String& target)

Resolves a complex, relative or symlinked path to its effective target. Returns true on success.

 


 

String GetDefaultDir()

Returns the path of default directory  (e.g. /home/username) on success, and String::GetVoid() on failure.

 


 

bool GetAttrs(SFtpHandle handle, SFtpAttrs& attrs)

Gets the attributes of file system object pointed by handle to attrs. Returns true on success.

 


 

bool GetAttrs(const String& path, SFtpAttrs& attrs)

Gets the attributes of file system object at path to attrs. Returns true on success.

 


 

bool SetAttrs(SFtpHandle handle, const SFtpAttrs& attrs)

Sets the attributes of the file system object pointed by handle.to attrs. Returns true on success.

 


 

bool SetAttrs(const String& path, const SFtpAttrs& attrs)

Sets the attributes of the file system object at path to attrs. Returns true on success.

 


 

SFtp::DirEntry GetInfo(const String& path)

Returns a DirEntry object that contains information about path on success, and Null on failure.

 


 

bool SetInfo(const DirEntry& entry)

Sets the attributes of file system object represented by entry. Returns true on succes, and Null on failure.

 


 

int64 GetSize(const String& path)

Returns the size of path on success, -1 on failure.

 


 

bool SetSize(const String& path, int64 size)

Sets the size of path. Returns true on success.

 


 

Time GetLastModifyTime(const String& path)

Returns the last modification time of path on success and Null on failure.

 


 

bool SetLastModifyTime(const String& path, const Time& time)

Sets the last modification time of path. Returns true on success.

 


 

Time GetLastAccessTime(const String& path)

Returns the last access time of path on success and Null on failure.

 


 

bool SetLastAccessTime(const String& path, const Time& time)

Sets the last access time of path. Returns true on success.

 


 

bool FileExists(const String& path)

Returns true if path represents a file.

 


 

bool DirectoryExists(const String& path)

Returns true if path represents a directory.

 


 

bool SymLinkExists(const String& path)

Returns true if path represents a symbolic link.

 


 

bool SocketExists(const String& path)

Returns true if path represents a socket.

 


 

bool PipeExists(const String& path)

Returns true if path represents a named pipe.

 


 

bool BlockExists(const String& path)

Returns true if path represents a block special file.

 


 

bool SpecialFileExists(const String& path)

Returns true if path represents a character special file.

 


 

int GetDone() const

Returns the number of bytes processed during a data transfer. Intended to be called from WhenWait routine.

 


 

Gate<int64, int64WhenProgress

If defined, this gate allows tracking of data transfers. The first parameter provides the amount of data that has already been transferred. The second parameter may provide the total amount of data to be transferred, but is allowed to be 0. Returning true will abort the current data transfer.

 

Transfer Methods

int Get(SFtpHandle handle, void *ptr, int size = INT_MAX)

Reads at most size bytes data from the remote file object associated with handle into the buffer pointed by ptr, trying to do so at most for a specified timeout. Returns the number of bytes actually written.

 


 

int Put(SFtpHandle handle, const void *ptr, int size)

Writes at most size bytes data from the buffer pointed by ptr into the remote file object associated with handle, trying to do so at most for a specified timeout. Returns the number of bytes actually written.

 


 

bool SaveFile(const char *path, const String& data)

Saves the content of data to remote path. Returns true on success. WhenProgress gate can be used to track data transfer.

 


 

String LoadFile(const char *path)

Returns the content of the remote path. WhenProgress gate can be used to track data transfer.

 


 

bool SaveFile(const char *path, Stream& in)

Saves the content of in to remote path . Returns true on success. WhenProgress gate can be used to track data transfer.

 


 

bool LoadFile(Stream& out, const char *path)

Returns the content of remote path into out. Returns true on success. WhenProgress gate can be used to track data transfer.

 

Constructor detail

 

SFtp(SshSession& session)

Constructor. Binds the SFtp instance to session.

 

 

SFtp::DirEntry

 

class DirEntry : public Moveable<DirEntry

This nested helper class is intended to simplify the parsing of file system objects (files, directories, symbolic links, etc.) returned by the SFtp class. DirEntry class has pick semantics.

 

Public Method List

 

String GetName() const

Returns the name (path) of the file system object on success, and Null on failure..

 


 

int64 GetUid() const

Returns the user id associated with the file system object on success, and -1 on failure.

 


 

int64 GetGid() const

Returns the group id associated with the file system object on success, and -1 on failure.

 


 

int64 GetSize() const

Returns the size of file system object in bytes on success, and -1 on falure.

 


 

Time GetLastModified() const

Returns the last modification time of the file system object on success, and Null on failure.

 


 

Time GetLastAccessed() const

Returns the last access time of the file system object on success, and Null on failure.

 


 

SFtpAttrs& GetAttrs()

SFtpAttrs& operator*()

Returns the libssh2 file attributes structure for the file system object.

 


 

const SFtpAttrs& operator~() const

Allows constant access  to the file attributes structure.

 


 

bool IsFile() const

Returns true if the file system object is a file.

 


 

bool IsDirectory() const

Returns true if the file system object is a directory.

 


 

bool IsSymLink() const

Returns true if the file system object is a symbolic link.

 


 

bool IsSpecial() const

Returns true if the file system object is a character special file.

 


 

bool IsBlock() const

Returns true if the file system object is a block special file.

 


 

bool IsPipe() const

Returns true if the file system object is a named pipe.

 


 

bool IsSocket() const

Returns true if the file system object is a socket.

 


 

bool IsReadable() const

Returns true if the file system object is readable.

 


 

bool IsWriteable() const

Returns true if the file system object is writable.

 


 

bool IsReadOnly() const

Returns true if the file system object has read-only access.

 


 

bool IsExecutable() const

Returns true if the file system object is executable.

 


 

String ToString() const

Returns the information of file system object as a formattad string. Output of this method is similar to Unix ls command's output.

 


 

FileSystemInfo::FileInfo ToFileInfo() const

Converts the DirEntry object to FileSystemInfo:FileInfo object. See SFtpFileSystemInfo.

 


 

void Serialize(Stream& s)

Serializes the DirEntry object.

 

Constructor detail

 

DirEntry()

Default constructor.

 


 

DirEntry(const String& path)

Creates an empty directory entry representing path.

 


 

DirEntry(const String& path, const SFtpAttrs& attrs)

Cretas a directory entry representing path with attrs.

 

 

SFtp::DirList

 

typedef Vector<DirEntryDirList

SFtp::DirList is a Vector containing SFtp::DirEntry elements.

 

 

SFtpFileSystemInfo

 

class SFtpFileSystemInfo : public FileSystemInfo

This helper class is a FileSystemInfo adapter for the SFtp objects. It allows SFtp objects to be used with FileSystemInfo class that provides uniform access to folder hierarchies in a file-system agnostic ("transparent") way.

 

Public Method List

 

SFtpFileSystemInfo& Mount(SFtp& sftp)

Mounts a valid sftp file system to be enumerated. Returns *this for method chaining.

 


 

Array<FileSystemInfo::FileInfoFind(String mask, int max_count = __INT_MAX__, bool unmounted = falseconst override

Searches for the given mask in the mounted sftp file system. Returns an array of FileInfo objects on success, and an empty array on failure. Mask can be a full or relative path, and may contain wildcards (*, ?). When the mask is null or empty, this method will return the content of the user's default directory. max_count can be used to limit the number of returned FileInfos. The unmounted flag is unused.

 


 

bool CreateFolder(String path, String& errorconst override

Attempts to create a new folder at path. Returns true on success, and the error  buffer will be filled with specific error message on failure.

 


 

int GetStyle() const override

Returns the file system style. This method returns the FileSystemInfo::STYLE_POSIX constant.

 

Constructor detail

 

SFtpFileSystemInfo()

Default constructor.

 


 

SFtpFileSystemInfo(SFtp& sftp)

Constructor. Mounts a valid sftp file system to be enumerated.

 

 

Do you want to contribute?