2016-07-07 02:59:55 +08:00
package sftp
2016-07-09 05:13:03 +08:00
import (
"io"
"os"
)
2016-07-09 03:38:35 +08:00
2020-08-30 16:40:22 +08:00
// WriterAtReaderAt defines the interface to return when a file is to
// be opened for reading and writing
type WriterAtReaderAt interface {
io . WriterAt
io . ReaderAt
}
2016-07-09 05:13:03 +08:00
// Interfaces are differentiated based on required returned values.
// All input arguments are to be pulled from Request (the only arg).
2018-05-26 10:25:08 +08:00
// The Handler interfaces all take the Request object as its only argument.
// All the data you should need to handle the call are in the Request object.
// The request.Method attribute is initially the most important one as it
// determines which Handler gets called.
2018-01-17 06:42:06 +08:00
// FileReader should return an io.ReaderAt for the filepath
// Note in cases of an error, the error text will be sent to the client.
2018-05-26 10:25:08 +08:00
// Called for Methods: Get
2016-07-09 05:13:03 +08:00
type FileReader interface {
2017-08-13 15:22:00 +08:00
Fileread ( * Request ) ( io . ReaderAt , error )
2016-07-09 05:13:03 +08:00
}
2018-01-17 06:42:06 +08:00
// FileWriter should return an io.WriterAt for the filepath.
//
// The request server code will call Close() on the returned io.WriterAt
2023-10-02 14:12:40 +08:00
// object if an io.Closer type assertion succeeds.
2018-01-17 06:42:06 +08:00
// Note in cases of an error, the error text will be sent to the client.
2020-01-03 06:33:58 +08:00
// Note when receiving an Append flag it is important to not open files using
// O_APPEND if you plan to use WriteAt, as they conflict.
2018-05-26 10:25:08 +08:00
// Called for Methods: Put, Open
2016-07-09 05:13:03 +08:00
type FileWriter interface {
2017-08-13 15:22:00 +08:00
Filewrite ( * Request ) ( io . WriterAt , error )
2016-07-09 05:13:03 +08:00
}
2020-09-26 00:16:01 +08:00
// OpenFileWriter is a FileWriter that implements the generic OpenFile method.
2020-08-30 16:40:22 +08:00
// You need to implement this optional interface if you want to be able
// to read and write from/to the same handle.
// Called for Methods: Open
type OpenFileWriter interface {
FileWriter
OpenFile ( * Request ) ( WriterAtReaderAt , error )
}
2018-01-17 06:42:06 +08:00
// FileCmder should return an error
// Note in cases of an error, the error text will be sent to the client.
2019-05-25 03:23:18 +08:00
// Called for Methods: Setstat, Rename, Rmdir, Mkdir, Link, Symlink, Remove
2016-07-09 05:13:03 +08:00
type FileCmder interface {
2017-08-13 15:22:00 +08:00
Filecmd ( * Request ) error
2016-07-09 05:13:03 +08:00
}
2021-02-11 02:13:19 +08:00
// PosixRenameFileCmder is a FileCmder that implements the PosixRename method.
2020-09-26 00:16:01 +08:00
// If this interface is implemented PosixRename requests will call it
// otherwise they will be handled in the same way as Rename
type PosixRenameFileCmder interface {
FileCmder
PosixRename ( * Request ) error
}
2021-02-11 02:13:19 +08:00
// StatVFSFileCmder is a FileCmder that implements the StatVFS method.
// You need to implement this interface if you want to handle statvfs requests.
// Please also be sure that the statvfs@openssh.com extension is enabled
type StatVFSFileCmder interface {
FileCmder
StatVFS ( * Request ) ( * StatVFS , error )
}
2018-01-17 06:42:06 +08:00
// FileLister should return an object that fulfils the ListerAt interface
// Note in cases of an error, the error text will be sent to the client.
2018-05-26 10:25:08 +08:00
// Called for Methods: List, Stat, Readlink
2022-10-14 23:12:56 +08:00
//
2023-03-15 20:21:45 +08:00
// Since Filelist returns an os.FileInfo, this can make it non-ideal for implementing Readlink.
2022-10-14 23:12:56 +08:00
// This is because the Name receiver method defined by that interface defines that it should only return the base name.
// However, Readlink is required to be capable of returning essentially any arbitrary valid path relative or absolute.
// In order to implement this more expressive requirement, implement [ReadlinkFileLister] which will then be used instead.
2017-07-28 09:22:11 +08:00
type FileLister interface {
2017-08-13 15:22:00 +08:00
Filelist ( * Request ) ( ListerAt , error )
2017-07-28 09:22:11 +08:00
}
2020-09-06 14:26:58 +08:00
// LstatFileLister is a FileLister that implements the Lstat method.
// If this interface is implemented Lstat requests will call it
// otherwise they will be handled in the same way as Stat
type LstatFileLister interface {
2020-09-26 00:16:01 +08:00
FileLister
2020-09-06 14:26:58 +08:00
Lstat ( * Request ) ( ListerAt , error )
}
2021-04-28 00:06:39 +08:00
// RealPathFileLister is a FileLister that implements the Realpath method.
2022-07-16 15:55:51 +08:00
// The built-in RealPath implementation does not resolve symbolic links.
// By implementing this interface you can customize the returned path
// and, for example, resolve symbolinc links if needed for your use case.
2021-04-27 17:08:29 +08:00
// You have to return an absolute POSIX path.
2022-03-03 01:44:54 +08:00
//
2022-07-16 15:55:51 +08:00
// Up to v1.13.5 the signature for the RealPath method was:
//
2022-10-14 23:12:56 +08:00
// # RealPath(string) string
2022-07-16 15:55:51 +08:00
//
// we have added a legacyRealPathFileLister that implements the old method
// to ensure that your code does not break.
// You should use the new method signature to avoid future issues
2021-04-28 00:06:39 +08:00
type RealPathFileLister interface {
2022-07-16 15:55:51 +08:00
FileLister
RealPath ( string ) ( string , error )
}
2022-10-14 23:12:56 +08:00
// ReadlinkFileLister is a FileLister that implements the Readlink method.
// By implementing the Readlink method, it is possible to return any arbitrary valid path relative or absolute.
// This allows giving a better response than via the default FileLister (which is limited to os.FileInfo, whose Name method should only return the base name of a file)
type ReadlinkFileLister interface {
FileLister
Readlink ( string ) ( string , error )
}
2022-07-16 15:55:51 +08:00
// This interface is here for backward compatibility only
type legacyRealPathFileLister interface {
2021-04-27 17:08:29 +08:00
FileLister
2021-04-28 00:06:39 +08:00
RealPath ( string ) string
2021-04-27 17:08:29 +08:00
}
2021-08-17 19:40:30 +08:00
// NameLookupFileLister is a FileLister that implmeents the LookupUsername and LookupGroupName methods.
// If this interface is implemented, then longname ls formatting will use these to convert usernames and groupnames.
type NameLookupFileLister interface {
FileLister
LookupUserName ( string ) string
LookupGroupName ( string ) string
}
2023-03-15 20:21:45 +08:00
// ListerAt does for file lists what io.ReaderAt does for files, i.e. a []os.FileInfo buffer is passed to the ListAt function
// and the entries that are populated in the buffer will be passed to the client.
//
// ListAt should return the number of entries copied and an io.EOF error if at end of list.
// This is testable by comparing how many you copied to how many could be copied (eg. n < len(ls) below).
2017-07-28 09:22:11 +08:00
// The copy() builtin is best for the copying.
2023-03-15 20:21:45 +08:00
//
// Uid and gid information will on unix systems be retrieved from [os.FileInfo.Sys]
// if this function returns a [syscall.Stat_t] when called on a populated entry.
// Alternatively, if the entry implements [FileInfoUidGid], it will be used for uid and gid information.
//
// If a populated entry implements [FileInfoExtendedData], extended attributes will also be returned to the client.
//
2024-02-09 17:22:54 +08:00
// The request server code will call Close() on ListerAt if an io.Closer type assertion succeeds.
//
2018-01-17 06:42:06 +08:00
// Note in cases of an error, the error text will be sent to the client.
2017-07-28 09:22:11 +08:00
type ListerAt interface {
ListAt ( [ ] os . FileInfo , int64 ) ( int , error )
2016-07-09 05:13:03 +08:00
}
2019-09-12 14:17:32 +08:00
// TransferError is an optional interface that readerAt and writerAt
// can implement to be notified about the error causing Serve() to exit
// with the request still open
type TransferError interface {
TransferError ( err error )
}