2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-02 09:58:25 +00:00
restic/vendor/github.com/pkg/sftp/request.go

345 lines
7.7 KiB
Go
Raw Normal View History

2017-07-23 12:24:45 +00:00
package sftp
import (
"io"
"os"
"path"
"path/filepath"
"sync"
"syscall"
"github.com/pkg/errors"
)
2017-09-13 12:09:48 +00:00
// MaxFilelist is the max number of files to return in a readdir batch.
var MaxFilelist int64 = 100
2017-07-23 12:24:45 +00:00
// Request contains the data and state for the incoming service request.
type Request struct {
// Get, Put, Setstat, Stat, Rename, Remove
// Rmdir, Mkdir, List, Readlink, Symlink
Method string
Filepath string
Flags uint32
Attrs []byte // convert to sub-struct
Target string // for renames and sym-links
// reader/writer/readdir from handlers
stateLock *sync.RWMutex
state *state
}
type state struct {
2017-09-13 12:09:48 +00:00
writerAt io.WriterAt
readerAt io.ReaderAt
listerAt ListerAt
lsoffset int64
2017-07-23 12:24:45 +00:00
}
type packet_data struct {
2017-09-13 12:09:48 +00:00
_id uint32
2017-07-23 12:24:45 +00:00
data []byte
length uint32
offset int64
}
2017-09-13 12:09:48 +00:00
func (pd packet_data) id() uint32 {
return pd._id
}
2017-07-23 12:24:45 +00:00
// New Request initialized based on packet data
2017-09-13 12:09:48 +00:00
func requestFromPacket(pkt hasPath) *Request {
2017-07-23 12:24:45 +00:00
method := requestMethod(pkt)
request := NewRequest(method, pkt.getPath())
switch p := pkt.(type) {
case *sshFxpSetstatPacket:
request.Flags = p.Flags
request.Attrs = p.Attrs.([]byte)
case *sshFxpRenamePacket:
2017-09-13 12:09:48 +00:00
request.Target = cleanPath(p.Newpath)
2017-07-23 12:24:45 +00:00
case *sshFxpSymlinkPacket:
2017-09-13 12:09:48 +00:00
request.Target = cleanPath(p.Linkpath)
2017-07-23 12:24:45 +00:00
}
return request
}
2017-09-13 12:09:48 +00:00
func newRequest() *Request {
return &Request{state: &state{}, stateLock: &sync.RWMutex{}}
}
2017-07-23 12:24:45 +00:00
// NewRequest creates a new Request object.
2017-09-13 12:09:48 +00:00
func NewRequest(method, path string) *Request {
request := newRequest()
request.Method = method
request.Filepath = cleanPath(path)
2017-07-23 12:24:45 +00:00
return request
}
2017-09-13 12:09:48 +00:00
// Returns current offset for file list
func (r *Request) lsNext() int64 {
2017-07-23 12:24:45 +00:00
r.stateLock.RLock()
defer r.stateLock.RUnlock()
2017-09-13 12:09:48 +00:00
return r.state.lsoffset
2017-07-23 12:24:45 +00:00
}
2017-09-13 12:09:48 +00:00
// Increases next offset
func (r *Request) lsInc(offset int64) {
r.stateLock.Lock()
defer r.stateLock.Unlock()
r.state.lsoffset = r.state.lsoffset + offset
2017-07-23 12:24:45 +00:00
}
// manage file read/write state
2017-09-13 12:09:48 +00:00
func (r *Request) setFileState(s interface{}) {
2017-07-23 12:24:45 +00:00
r.stateLock.Lock()
defer r.stateLock.Unlock()
switch s := s.(type) {
case io.WriterAt:
r.state.writerAt = s
case io.ReaderAt:
r.state.readerAt = s
2017-09-13 12:09:48 +00:00
case ListerAt:
r.state.listerAt = s
case int64:
r.state.lsoffset = s
2017-07-23 12:24:45 +00:00
}
}
2017-09-13 12:09:48 +00:00
func (r *Request) getWriter() io.WriterAt {
2017-07-23 12:24:45 +00:00
r.stateLock.RLock()
defer r.stateLock.RUnlock()
return r.state.writerAt
}
2017-09-13 12:09:48 +00:00
func (r *Request) getReader() io.ReaderAt {
2017-07-23 12:24:45 +00:00
r.stateLock.RLock()
defer r.stateLock.RUnlock()
return r.state.readerAt
}
2017-09-13 12:09:48 +00:00
func (r *Request) getLister() ListerAt {
2017-07-23 12:24:45 +00:00
r.stateLock.RLock()
defer r.stateLock.RUnlock()
2017-09-13 12:09:48 +00:00
return r.state.listerAt
2017-07-23 12:24:45 +00:00
}
// Close reader/writer if possible
2017-09-13 12:09:48 +00:00
func (r *Request) close() {
2017-07-23 12:24:45 +00:00
rd := r.getReader()
if c, ok := rd.(io.Closer); ok {
c.Close()
}
wt := r.getWriter()
if c, ok := wt.(io.Closer); ok {
c.Close()
}
}
// called from worker to handle packet/request
2017-09-13 12:09:48 +00:00
func (r *Request) call(handlers Handlers, pkt requestPacket) responsePacket {
pd := packetData(pkt)
2017-07-23 12:24:45 +00:00
switch r.Method {
case "Get":
2017-09-13 12:09:48 +00:00
return fileget(handlers.FileGet, r, pd)
2017-07-23 12:24:45 +00:00
case "Put": // add "Append" to this to handle append only file writes
2017-09-13 12:09:48 +00:00
return fileput(handlers.FilePut, r, pd)
2017-07-23 12:24:45 +00:00
case "Setstat", "Rename", "Rmdir", "Mkdir", "Symlink", "Remove":
2017-09-13 12:09:48 +00:00
return filecmd(handlers.FileCmd, r, pd)
2017-07-23 12:24:45 +00:00
case "List", "Stat", "Readlink":
2017-09-13 12:09:48 +00:00
return filelist(handlers.FileList, r, pd)
2017-07-23 12:24:45 +00:00
default:
2017-09-13 12:09:48 +00:00
return statusFromError(pkt,
errors.Errorf("unexpected method: %s", r.Method))
2017-07-23 12:24:45 +00:00
}
2017-09-13 12:09:48 +00:00
}
// file data for additional read/write packets
func packetData(p requestPacket) packet_data {
pd := packet_data{_id: p.id()}
switch p := p.(type) {
case *sshFxpReadPacket:
pd.length = p.Len
pd.offset = int64(p.Offset)
case *sshFxpWritePacket:
pd.data = p.Data
pd.length = p.Length
pd.offset = int64(p.Offset)
}
return pd
2017-07-23 12:24:45 +00:00
}
// wrap FileReader handler
2017-09-13 12:09:48 +00:00
func fileget(h FileReader, r *Request, pd packet_data) responsePacket {
2017-07-23 12:24:45 +00:00
var err error
reader := r.getReader()
if reader == nil {
reader, err = h.Fileread(r)
if err != nil {
2017-09-13 12:09:48 +00:00
return statusFromError(pd, err)
2017-07-23 12:24:45 +00:00
}
r.setFileState(reader)
}
data := make([]byte, clamp(pd.length, maxTxPacket))
n, err := reader.ReadAt(data, pd.offset)
2017-09-13 12:09:48 +00:00
// only return EOF erro if no data left to read
2017-07-23 12:24:45 +00:00
if err != nil && (err != io.EOF || n == 0) {
2017-09-13 12:09:48 +00:00
return statusFromError(pd, err)
2017-07-23 12:24:45 +00:00
}
return &sshFxpDataPacket{
2017-09-13 12:09:48 +00:00
ID: pd.id(),
2017-07-23 12:24:45 +00:00
Length: uint32(n),
Data: data[:n],
2017-09-13 12:09:48 +00:00
}
2017-07-23 12:24:45 +00:00
}
// wrap FileWriter handler
2017-09-13 12:09:48 +00:00
func fileput(h FileWriter, r *Request, pd packet_data) responsePacket {
2017-07-23 12:24:45 +00:00
var err error
writer := r.getWriter()
if writer == nil {
writer, err = h.Filewrite(r)
if err != nil {
2017-09-13 12:09:48 +00:00
return statusFromError(pd, err)
2017-07-23 12:24:45 +00:00
}
r.setFileState(writer)
}
_, err = writer.WriteAt(pd.data, pd.offset)
if err != nil {
2017-09-13 12:09:48 +00:00
return statusFromError(pd, err)
2017-07-23 12:24:45 +00:00
}
return &sshFxpStatusPacket{
2017-09-13 12:09:48 +00:00
ID: pd.id(),
2017-07-23 12:24:45 +00:00
StatusError: StatusError{
Code: ssh_FX_OK,
2017-09-13 12:09:48 +00:00
}}
2017-07-23 12:24:45 +00:00
}
// wrap FileCmder handler
2017-09-13 12:09:48 +00:00
func filecmd(h FileCmder, r *Request, pd packet_data) responsePacket {
2017-07-23 12:24:45 +00:00
err := h.Filecmd(r)
if err != nil {
2017-09-13 12:09:48 +00:00
return statusFromError(pd, err)
2017-07-23 12:24:45 +00:00
}
return &sshFxpStatusPacket{
2017-09-13 12:09:48 +00:00
ID: pd.id(),
2017-07-23 12:24:45 +00:00
StatusError: StatusError{
Code: ssh_FX_OK,
2017-09-13 12:09:48 +00:00
}}
2017-07-23 12:24:45 +00:00
}
2017-09-13 12:09:48 +00:00
// wrap FileLister handler
func filelist(h FileLister, r *Request, pd packet_data) responsePacket {
var err error
lister := r.getLister()
if lister == nil {
lister, err = h.Filelist(r)
if err != nil {
return statusFromError(pd, err)
}
r.setFileState(lister)
2017-07-23 12:24:45 +00:00
}
2017-09-13 12:09:48 +00:00
offset := r.lsNext()
finfo := make([]os.FileInfo, MaxFilelist)
n, err := lister.ListAt(finfo, offset)
r.lsInc(int64(n))
// ignore EOF as we only return it when there are no results
finfo = finfo[:n] // avoid need for nil tests below
2017-07-23 12:24:45 +00:00
switch r.Method {
case "List":
2017-09-13 12:09:48 +00:00
if err != nil && err != io.EOF {
return statusFromError(pd, err)
}
if n == 0 {
return statusFromError(pd, io.EOF)
}
dirname := filepath.ToSlash(path.Base(r.Filepath))
ret := &sshFxpNamePacket{ID: pd.id()}
2017-07-23 12:24:45 +00:00
for _, fi := range finfo {
ret.NameAttrs = append(ret.NameAttrs, sshFxpNameAttr{
Name: fi.Name(),
LongName: runLs(dirname, fi),
Attrs: []interface{}{fi},
})
}
2017-09-13 12:09:48 +00:00
return ret
2017-07-23 12:24:45 +00:00
case "Stat":
2017-09-13 12:09:48 +00:00
if err != nil && err != io.EOF {
return statusFromError(pd, err)
}
if n == 0 {
2017-07-23 12:24:45 +00:00
err = &os.PathError{Op: "stat", Path: r.Filepath,
Err: syscall.ENOENT}
2017-09-13 12:09:48 +00:00
return statusFromError(pd, err)
2017-07-23 12:24:45 +00:00
}
return &sshFxpStatResponse{
2017-09-13 12:09:48 +00:00
ID: pd.id(),
2017-07-23 12:24:45 +00:00
info: finfo[0],
2017-09-13 12:09:48 +00:00
}
2017-07-23 12:24:45 +00:00
case "Readlink":
2017-09-13 12:09:48 +00:00
if err != nil && err != io.EOF {
return statusFromError(pd, err)
}
if n == 0 {
2017-07-23 12:24:45 +00:00
err = &os.PathError{Op: "readlink", Path: r.Filepath,
Err: syscall.ENOENT}
2017-09-13 12:09:48 +00:00
return statusFromError(pd, err)
2017-07-23 12:24:45 +00:00
}
filename := finfo[0].Name()
return &sshFxpNamePacket{
2017-09-13 12:09:48 +00:00
ID: pd.id(),
2017-07-23 12:24:45 +00:00
NameAttrs: []sshFxpNameAttr{{
Name: filename,
LongName: filename,
Attrs: emptyFileStat,
}},
2017-09-13 12:09:48 +00:00
}
default:
err = errors.Errorf("unexpected method: %s", r.Method)
return statusFromError(pd, err)
2017-07-23 12:24:45 +00:00
}
}
// file data for additional read/write packets
2017-09-13 12:09:48 +00:00
func (r *Request) updateMethod(p hasHandle) error {
2017-07-23 12:24:45 +00:00
switch p := p.(type) {
case *sshFxpReadPacket:
r.Method = "Get"
case *sshFxpWritePacket:
r.Method = "Put"
case *sshFxpReaddirPacket:
r.Method = "List"
default:
return errors.Errorf("unexpected packet type %T", p)
}
return nil
}
// init attributes of request object from packet data
func requestMethod(p hasPath) (method string) {
switch p.(type) {
case *sshFxpOpenPacket, *sshFxpOpendirPacket:
method = "Open"
case *sshFxpSetstatPacket:
method = "Setstat"
case *sshFxpRenamePacket:
method = "Rename"
case *sshFxpSymlinkPacket:
method = "Symlink"
case *sshFxpRemovePacket:
method = "Remove"
case *sshFxpStatPacket, *sshFxpLstatPacket:
method = "Stat"
case *sshFxpRmdirPacket:
method = "Rmdir"
case *sshFxpReadlinkPacket:
method = "Readlink"
case *sshFxpMkdirPacket:
method = "Mkdir"
}
return method
}