package sftp
import "git.sr.ht/~shulhan/pakakeh.go/lib/ssh/sftp"
Package sftp implement SSH File Transfer Protocol v3 as defined in draft-ietf-secsh-filexfer-02.txt.
The sftp package extend the golang.org/x/crypto/ssh package by implementing "sftp" subsystem using the ssh.Client connection.
For information, even if scp working normally on server, this package functionalities will not working if the server disable or does not support the "sftp" subsystem. For reference, on openssh, the following configuration
Subsystem sftp /usr/lib/sftp-server
should be un-commented on /etc/ssh/sshd_config if its exist.
Index ¶
- Constants
- Variables
- type Client
- func NewClient(sshc *ssh.Client) (cl *Client, err error)
- func (cl *Client) Close() (err error)
- func (cl *Client) CloseFile(fh *FileHandle) (err error)
- func (cl *Client) Create(remoteFile string, fa *FileAttrs) (*FileHandle, error)
- func (cl *Client) Fsetstat(fh *FileHandle, fa *FileAttrs) (err error)
- func (cl *Client) Fstat(fh *FileHandle) (fa *FileAttrs, err error)
- func (cl *Client) Get(remoteFile, localFile string) (err error)
- func (cl *Client) Lstat(remoteFile string) (fa *FileAttrs, err error)
- func (cl *Client) Mkdir(path string, fa *FileAttrs) (err error)
- func (cl *Client) MkdirAll(dir string, fa *FileAttrs) (err error)
- func (cl *Client) Open(remoteFile string) (fh *FileHandle, err error)
- func (cl *Client) OpenFile(remoteFile string, flags uint32, fa *FileAttrs) (fh *FileHandle, err error)
- func (cl *Client) Opendir(path string) (fh *FileHandle, err error)
- func (cl *Client) Put(localFile, remoteFile string) (err error)
- func (cl *Client) Read(fh *FileHandle, offset uint64) (data []byte, err error)
- func (cl *Client) Readdir(fh *FileHandle) (nodes []fs.DirEntry, err error)
- func (cl *Client) Readlink(linkPath string) (node fs.DirEntry, err error)
- func (cl *Client) Realpath(path string) (node fs.DirEntry, err error)
- func (cl *Client) Remove(remoteFile string) (err error)
- func (cl *Client) Rename(oldPath, newPath string) (err error)
- func (cl *Client) Rmdir(path string) (err error)
- func (cl *Client) Setstat(remoteFile string, fa *FileAttrs) (err error)
- func (cl *Client) Stat(remoteFile string) (fa *FileAttrs, err error)
- func (cl *Client) Symlink(targetPath, linkPath string) (err error)
- func (cl *Client) Write(fh *FileHandle, offset uint64, data []byte) (err error)
- type FileAttrs
- func NewFileAttrs(fi fs.FileInfo) (fa *FileAttrs)
- func (fa *FileAttrs) AccessTime() uint32
- func (fa *FileAttrs) Extensions() map[string]string
- func (fa *FileAttrs) Gid() uint32
- func (fa *FileAttrs) IsDir() bool
- func (fa *FileAttrs) ModTime() time.Time
- func (fa *FileAttrs) Mode() fs.FileMode
- func (fa *FileAttrs) Name() string
- func (fa *FileAttrs) Permissions() uint32
- func (fa *FileAttrs) SetAccessTime(v uint32)
- func (fa *FileAttrs) SetExtension(name, data string)
- func (fa *FileAttrs) SetGid(gid uint32)
- func (fa *FileAttrs) SetModifiedTime(v uint32)
- func (fa *FileAttrs) SetPermissions(v uint32)
- func (fa *FileAttrs) SetSize(v uint64)
- func (fa *FileAttrs) SetUID(uid uint32)
- func (fa *FileAttrs) Size() int64
- func (fa *FileAttrs) Sys() any
- func (fa *FileAttrs) UID() uint32
- type FileHandle
Constants ¶
const ( // OpenFlagRead open remote file for read only. OpenFlagRead uint32 = 0x00000001 // OpenFlagWrite open remote file for write only. OpenFlagWrite uint32 = 0x00000002 // OpenFlagAppend any write to remote file handle opened with this // file will be appended at the end of the file. OpenFlagAppend uint32 = 0x00000004 // OpenFlagCreate create new file on the server if it does not exist. OpenFlagCreate uint32 = 0x00000008 // OpenFlagTruncate truncated the remote file. // OpenFlagCreate MUST also be specified. OpenFlagTruncate uint32 = 0x00000010 // OpenFlagExcl passing this flag along OpenFlagCreate will fail the // remote file already exists. // OpenFlagCreate MUST also be specified. OpenFlagExcl uint32 = 0x00000020 )
List of valid values for OpenFile flag.
Variables ¶
var ( // ErrFailure or SSH_FX_FAILURE(4) is a generic catch-all error // message; it should be returned if an error occurs for which there // is no more specific error code defined. ErrFailure = errors.New("sftp: failure") // ErrBadMessage or SSH_FX_BAD_MESSAGE(5) may be returned if a badly // formatted packet or protocol incompatibility is detected. ErrBadMessage = errors.New("sftp: bad message") // ErrNoConnection or SSH_FX_NO_CONNECTION(6) indicates that the // client has no connection to the server. // This error returned by client not server. ErrNoConnection = errors.New("sftp: no connection") // ErrConnectionLost or SSH_FX_CONNECTION_LOST(7) indicated that the // connection to the server has been lost. // This error returned by client not server. ErrConnectionLost = errors.New("sftp: connection lost") // ErrOpUnsupported or SSH_FX_OP_UNSUPPORTED(8) indicates that an // attempt was made to perform an operation which is not supported for // the server or the server does not implement an operation. ErrOpUnsupported = errors.New("sftp: operation unsupported") // ErrSubsystem indicates that the server does not support or enable // the Subsystem for sftp. // For reference, on openssh, the following configuration // // Subsystem sftp /usr/lib/sftp-server // // maybe commented on /etc/ssh/sshd_config. // ErrSubsystem = errors.New("sftp: unsupported subsystem") // ErrVersion indicates that this client does not support the version // on server. ErrVersion = errors.New("sftp: unsupported version") )
Some response status code from server is mapped to existing errors on standard packages,
- SSH_FX_EOF (1) = io.EOF
- SSH_FX_NO_SUCH_FILE (2) = fs.ErrNotExist
- SSH_FX_PERMISSION_DENIED (3) = fs.ErrPermission
Other errors is defined below,
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client for SFTP.
func NewClient ¶
NewClient create and initialize new client for SSH file transfer protocol.
On failure, it will return ErrSubsystem if the server does not support "sftp" subsystem, ErrVersion if the client does not support the server version, and other errors.
func (*Client) Close ¶
Close the client sftp session and release all resources.
func (*Client) CloseFile ¶
func (cl *Client) CloseFile(fh *FileHandle) (err error)
CloseFile close the remote file handle.
func (*Client) Create ¶
func (cl *Client) Create(remoteFile string, fa *FileAttrs) (*FileHandle, error)
Create creates or truncates the named file. If the remote file does not exist, it will be created. If the remote file already exists, it will be truncated. On success, it will return the remote FileHandle ready for write only.
func (*Client) Fsetstat ¶
func (cl *Client) Fsetstat(fh *FileHandle, fa *FileAttrs) (err error)
Fsetstat set the file attributes based on the opened remote file handle.
func (*Client) Fstat ¶
func (cl *Client) Fstat(fh *FileHandle) (fa *FileAttrs, err error)
Fstat get the file attributes based on the opened remote file handle.
func (*Client) Get ¶
Get copy remote file to local. The local file will be created if its not exist; otherwise it will truncated.
func (*Client) Lstat ¶
Lstat get the file attributes based on the remote file path. Unlike Client.Stat, the Lstat method does not follow symbolic links.
func (*Client) Mkdir ¶
Mkdir create new directory on the server.
func (*Client) MkdirAll ¶
MkdirAll create directory on the server, from left to right. Each directory is separated by '/', where the left part is the parent of the right part. This method is similar to os.MkdirAll.
Note that using `~` as home directory is not working. If you want to create directory under home, use relative path, for example "a/b/c" will create directory "~/a/b/c".
func (*Client) Open ¶
func (cl *Client) Open(remoteFile string) (fh *FileHandle, err error)
Open the remote file for read only.
func (*Client) OpenFile ¶
func (cl *Client) OpenFile(remoteFile string, flags uint32, fa *FileAttrs) (fh *FileHandle, err error)
OpenFile open remote file with custom open flag (OpenFlagRead, OpenFlagWrite, and so on) and with specific file attributes.
func (*Client) Opendir ¶
func (cl *Client) Opendir(path string) (fh *FileHandle, err error)
Opendir open the directory on the server.
func (*Client) Put ¶
Put local file to remote file.
func (*Client) Read ¶
func (cl *Client) Read(fh *FileHandle, offset uint64) (data []byte, err error)
Read the remote file using handle on specific offset. On end-of-file it will return empty data with io.EOF.
func (*Client) Readdir ¶
func (cl *Client) Readdir(fh *FileHandle) (nodes []fs.DirEntry, err error)
Readdir list files and/or directories inside the handle.
func (*Client) Readlink ¶
Readlink read the target of a symbolic link.
func (*Client) Realpath ¶
Realpath canonicalize any given path name to an absolute path. This is useful for converting path names containing ".." components or relative pathnames without a leading slash into absolute paths.
func (*Client) Remove ¶
Remove the remote file.
func (*Client) Rename ¶
Rename the file, or move the file, from old path to new path.
func (*Client) Rmdir ¶
Rmdir remove the directory on the server.
func (*Client) Setstat ¶
Setstat change the file attributes on remote file or directory. These request can be used for operations such as changing the ownership, permissions or access times, as well as for truncating a file.
func (*Client) Stat ¶
Stat get the file attributes based on the remote file path. This method follow symbolic links.
To stat the user's home directory pass empty string or '.' in remoteFile parameter.
func (*Client) Symlink ¶
Symlink create a symbolic link on the server. The `linkpath' specifies the path name of the symlink to be created and `targetpath' specifies the target of the symlink.
func (*Client) Write ¶
func (cl *Client) Write(fh *FileHandle, offset uint64, data []byte) (err error)
Write write the data into remote file at specific offset.
type FileAttrs ¶
type FileAttrs struct {
// contains filtered or unexported fields
}
FileAttrs define the attributes for opening or creating file on the remote.
func NewFileAttrs ¶
NewFileAttrs create and initialize FileAttrs from fs.FileInfo.
func (*FileAttrs) AccessTime ¶
AccessTime return the remote file access time.
func (*FileAttrs) Extensions ¶
Extensions return the remote file attribute extensions as map of type and data.
func (*FileAttrs) Gid ¶
Gid return the group ID attribute of file.
func (*FileAttrs) IsDir ¶
IsDir return true if the file is a directory.
func (*FileAttrs) ModTime ¶
ModTime return the remote file modified time.
func (*FileAttrs) Mode ¶
Mode return the file mode bits as standard fs.FileMode type.
func (*FileAttrs) Name ¶
Name return the name of file.
func (*FileAttrs) Permissions ¶
Permissions return the remote file mode and permissions.
func (*FileAttrs) SetAccessTime ¶
SetAccessTime set the file attribute access time.
func (*FileAttrs) SetExtension ¶
SetExtension set the file attribute extension.
func (*FileAttrs) SetGid ¶
SetGid set the file attribute group ID.
func (*FileAttrs) SetModifiedTime ¶
SetModifiedTime set the file attribute modified time.
func (*FileAttrs) SetPermissions ¶
SetPermissions set the remote file permission.
func (*FileAttrs) SetSize ¶
SetSize set the remote file size.
func (*FileAttrs) SetUID ¶
SetUID set the file attribute user ID.
func (*FileAttrs) Size ¶
Size return the file size information.
func (*FileAttrs) Sys ¶
Sys return the pointer to FileAttrs itself. This method is added to comply with fs.FileInfo interface.
func (*FileAttrs) UID ¶
UID return the user ID of file.
type FileHandle ¶
type FileHandle struct {
// contains filtered or unexported fields
}
FileHandle define the container to store remote file.
Source Files ¶
client.go dir_entry.go extensions.go file_attrs.go file_handle.go packet.go sftp.go
- Version
- v0.60.0 (latest)
- Published
- Feb 1, 2025
- Platform
- linux/amd64
- Imports
- 12 packages
- Last checked
- 1 minute ago –
Tools for package owners.