rclone/vendor/github.com/pkg/sftp/packet-typing.go

137 lines
4.3 KiB
Go
Raw Normal View History

package sftp
import (
"encoding"
"github.com/pkg/errors"
)
// all incoming packets
type requestPacket interface {
encoding.BinaryUnmarshaler
id() uint32
}
type responsePacket interface {
encoding.BinaryMarshaler
id() uint32
}
// interfaces to group types
type hasPath interface {
requestPacket
getPath() string
}
type hasHandle interface {
requestPacket
getHandle() string
}
type notReadOnly interface {
notReadOnly()
}
//// define types by adding methods
// hasPath
func (p sshFxpLstatPacket) getPath() string { return p.Path }
func (p sshFxpStatPacket) getPath() string { return p.Path }
func (p sshFxpRmdirPacket) getPath() string { return p.Path }
func (p sshFxpReadlinkPacket) getPath() string { return p.Path }
func (p sshFxpRealpathPacket) getPath() string { return p.Path }
func (p sshFxpMkdirPacket) getPath() string { return p.Path }
func (p sshFxpSetstatPacket) getPath() string { return p.Path }
func (p sshFxpStatvfsPacket) getPath() string { return p.Path }
func (p sshFxpRemovePacket) getPath() string { return p.Filename }
func (p sshFxpRenamePacket) getPath() string { return p.Oldpath }
func (p sshFxpSymlinkPacket) getPath() string { return p.Targetpath }
func (p sshFxpOpendirPacket) getPath() string { return p.Path }
func (p sshFxpOpenPacket) getPath() string { return p.Path }
func (p sshFxpExtendedPacketPosixRename) getPath() string { return p.Oldpath }
2020-02-25 14:20:57 +00:00
func (p sshFxpExtendedPacketHardlink) getPath() string { return p.Oldpath }
2020-02-25 14:20:57 +00:00
// getHandle
func (p sshFxpFstatPacket) getHandle() string { return p.Handle }
func (p sshFxpFsetstatPacket) getHandle() string { return p.Handle }
func (p sshFxpReadPacket) getHandle() string { return p.Handle }
func (p sshFxpWritePacket) getHandle() string { return p.Handle }
func (p sshFxpReaddirPacket) getHandle() string { return p.Handle }
2018-03-19 15:51:38 +00:00
func (p sshFxpClosePacket) getHandle() string { return p.Handle }
// notReadOnly
func (p sshFxpWritePacket) notReadOnly() {}
func (p sshFxpSetstatPacket) notReadOnly() {}
func (p sshFxpFsetstatPacket) notReadOnly() {}
func (p sshFxpRemovePacket) notReadOnly() {}
func (p sshFxpMkdirPacket) notReadOnly() {}
func (p sshFxpRmdirPacket) notReadOnly() {}
func (p sshFxpRenamePacket) notReadOnly() {}
func (p sshFxpSymlinkPacket) notReadOnly() {}
func (p sshFxpExtendedPacketPosixRename) notReadOnly() {}
2020-02-25 14:20:57 +00:00
func (p sshFxpExtendedPacketHardlink) notReadOnly() {}
// some packets with ID are missing id()
func (p sshFxpDataPacket) id() uint32 { return p.ID }
func (p sshFxpStatusPacket) id() uint32 { return p.ID }
func (p sshFxpStatResponse) id() uint32 { return p.ID }
func (p sshFxpNamePacket) id() uint32 { return p.ID }
func (p sshFxpHandlePacket) id() uint32 { return p.ID }
func (p StatVFS) id() uint32 { return p.ID }
func (p sshFxVersionPacket) id() uint32 { return 0 }
// take raw incoming packet data and build packet objects
func makePacket(p rxPacket) (requestPacket, error) {
var pkt requestPacket
switch p.pktType {
2020-02-25 14:20:57 +00:00
case sshFxpInit:
pkt = &sshFxInitPacket{}
2020-02-25 14:20:57 +00:00
case sshFxpLstat:
pkt = &sshFxpLstatPacket{}
2020-02-25 14:20:57 +00:00
case sshFxpOpen:
pkt = &sshFxpOpenPacket{}
2020-02-25 14:20:57 +00:00
case sshFxpClose:
pkt = &sshFxpClosePacket{}
2020-02-25 14:20:57 +00:00
case sshFxpRead:
pkt = &sshFxpReadPacket{}
2020-02-25 14:20:57 +00:00
case sshFxpWrite:
pkt = &sshFxpWritePacket{}
2020-02-25 14:20:57 +00:00
case sshFxpFstat:
pkt = &sshFxpFstatPacket{}
2020-02-25 14:20:57 +00:00
case sshFxpSetstat:
pkt = &sshFxpSetstatPacket{}
2020-02-25 14:20:57 +00:00
case sshFxpFsetstat:
pkt = &sshFxpFsetstatPacket{}
2020-02-25 14:20:57 +00:00
case sshFxpOpendir:
pkt = &sshFxpOpendirPacket{}
2020-02-25 14:20:57 +00:00
case sshFxpReaddir:
pkt = &sshFxpReaddirPacket{}
2020-02-25 14:20:57 +00:00
case sshFxpRemove:
pkt = &sshFxpRemovePacket{}
2020-02-25 14:20:57 +00:00
case sshFxpMkdir:
pkt = &sshFxpMkdirPacket{}
2020-02-25 14:20:57 +00:00
case sshFxpRmdir:
pkt = &sshFxpRmdirPacket{}
2020-02-25 14:20:57 +00:00
case sshFxpRealpath:
pkt = &sshFxpRealpathPacket{}
2020-02-25 14:20:57 +00:00
case sshFxpStat:
pkt = &sshFxpStatPacket{}
2020-02-25 14:20:57 +00:00
case sshFxpRename:
pkt = &sshFxpRenamePacket{}
2020-02-25 14:20:57 +00:00
case sshFxpReadlink:
pkt = &sshFxpReadlinkPacket{}
2020-02-25 14:20:57 +00:00
case sshFxpSymlink:
pkt = &sshFxpSymlinkPacket{}
2020-02-25 14:20:57 +00:00
case sshFxpExtended:
pkt = &sshFxpExtendedPacket{}
default:
return nil, errors.Errorf("unhandled packet type: %s", p.pktType)
}
if err := pkt.UnmarshalBinary(p.pktBytes); err != nil {
// Return partially unpacked packet to allow callers to return
// error messages appropriately with necessary id() method.
return pkt, err
}
return pkt, nil
}