2021-03-19 22:28:55 +08:00
|
|
|
package filexfer
|
|
|
|
|
|
|
|
// LstatPacket defines the SSH_FXP_LSTAT packet.
|
|
|
|
type LstatPacket struct {
|
2021-03-21 22:11:23 +08:00
|
|
|
Path string
|
2021-03-19 22:28:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalPacket returns p as a two-part binary encoding of p.
|
2021-03-21 22:11:23 +08:00
|
|
|
func (p *LstatPacket) MarshalPacket(reqid uint32) (header, payload []byte, err error) {
|
2021-03-20 05:19:45 +08:00
|
|
|
size := 4 + len(p.Path) // string(path)
|
2021-03-19 22:28:55 +08:00
|
|
|
|
2021-03-21 22:11:23 +08:00
|
|
|
b := NewMarshalBuffer(PacketTypeLstat, reqid, size)
|
2021-03-19 22:37:06 +08:00
|
|
|
|
2021-03-19 22:28:55 +08:00
|
|
|
b.AppendString(p.Path)
|
|
|
|
|
2021-03-20 05:19:45 +08:00
|
|
|
return b.Packet(payload)
|
2021-03-19 22:28:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalPacketBody unmarshals the packet body from the given Buffer.
|
|
|
|
// It is assumed that the uint32(request-id) has already been consumed.
|
|
|
|
func (p *LstatPacket) UnmarshalPacketBody(buf *Buffer) (err error) {
|
|
|
|
if p.Path, err = buf.ConsumeString(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetstatPacket defines the SSH_FXP_SETSTAT packet.
|
|
|
|
type SetstatPacket struct {
|
2021-03-21 22:11:23 +08:00
|
|
|
Path string
|
|
|
|
Attrs Attributes
|
2021-03-19 22:28:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalPacket returns p as a two-part binary encoding of p.
|
2021-03-21 22:11:23 +08:00
|
|
|
func (p *SetstatPacket) MarshalPacket(reqid uint32) (header, payload []byte, err error) {
|
2021-03-20 05:19:45 +08:00
|
|
|
size := 4 + len(p.Path) + p.Attrs.Len() // string(path) + ATTRS(attrs)
|
2021-03-19 22:28:55 +08:00
|
|
|
|
2021-03-21 22:11:23 +08:00
|
|
|
b := NewMarshalBuffer(PacketTypeSetstat, reqid, size)
|
2021-03-19 22:37:06 +08:00
|
|
|
|
2021-03-19 22:28:55 +08:00
|
|
|
b.AppendString(p.Path)
|
|
|
|
|
|
|
|
p.Attrs.MarshalInto(b)
|
|
|
|
|
2021-03-20 05:19:45 +08:00
|
|
|
return b.Packet(payload)
|
2021-03-19 22:28:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalPacketBody unmarshals the packet body from the given Buffer.
|
|
|
|
// It is assumed that the uint32(request-id) has already been consumed.
|
|
|
|
func (p *SetstatPacket) UnmarshalPacketBody(buf *Buffer) (err error) {
|
|
|
|
if p.Path, err = buf.ConsumeString(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return p.Attrs.UnmarshalFrom(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemovePacket defines the SSH_FXP_REMOVE packet.
|
|
|
|
type RemovePacket struct {
|
2021-03-21 22:11:23 +08:00
|
|
|
Path string
|
2021-03-19 22:28:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalPacket returns p as a two-part binary encoding of p.
|
2021-03-21 22:11:23 +08:00
|
|
|
func (p *RemovePacket) MarshalPacket(reqid uint32) (header, payload []byte, err error) {
|
2021-03-20 05:19:45 +08:00
|
|
|
size := 4 + len(p.Path) // string(path)
|
2021-03-19 22:28:55 +08:00
|
|
|
|
2021-03-21 22:11:23 +08:00
|
|
|
b := NewMarshalBuffer(PacketTypeRemove, reqid, size)
|
2021-03-19 22:37:06 +08:00
|
|
|
|
2021-03-19 22:28:55 +08:00
|
|
|
b.AppendString(p.Path)
|
|
|
|
|
2021-03-20 05:19:45 +08:00
|
|
|
return b.Packet(payload)
|
2021-03-19 22:28:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalPacketBody unmarshals the packet body from the given Buffer.
|
|
|
|
// It is assumed that the uint32(request-id) has already been consumed.
|
|
|
|
func (p *RemovePacket) UnmarshalPacketBody(buf *Buffer) (err error) {
|
|
|
|
if p.Path, err = buf.ConsumeString(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MkdirPacket defines the SSH_FXP_MKDIR packet.
|
|
|
|
type MkdirPacket struct {
|
2021-03-21 22:11:23 +08:00
|
|
|
Path string
|
|
|
|
Attrs Attributes
|
2021-03-19 22:28:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalPacket returns p as a two-part binary encoding of p.
|
2021-03-21 22:11:23 +08:00
|
|
|
func (p *MkdirPacket) MarshalPacket(reqid uint32) (header, payload []byte, err error) {
|
2021-03-20 05:19:45 +08:00
|
|
|
size := 4 + len(p.Path) + p.Attrs.Len() // string(path) + ATTRS(attrs)
|
2021-03-19 22:28:55 +08:00
|
|
|
|
2021-03-21 22:11:23 +08:00
|
|
|
b := NewMarshalBuffer(PacketTypeMkdir, reqid, size)
|
2021-03-19 22:37:06 +08:00
|
|
|
|
2021-03-19 22:28:55 +08:00
|
|
|
b.AppendString(p.Path)
|
|
|
|
|
|
|
|
p.Attrs.MarshalInto(b)
|
|
|
|
|
2021-03-20 05:19:45 +08:00
|
|
|
return b.Packet(payload)
|
2021-03-19 22:28:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalPacketBody unmarshals the packet body from the given Buffer.
|
|
|
|
// It is assumed that the uint32(request-id) has already been consumed.
|
|
|
|
func (p *MkdirPacket) UnmarshalPacketBody(buf *Buffer) (err error) {
|
|
|
|
if p.Path, err = buf.ConsumeString(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return p.Attrs.UnmarshalFrom(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RmdirPacket defines the SSH_FXP_RMDIR packet.
|
|
|
|
type RmdirPacket struct {
|
2021-03-21 22:11:23 +08:00
|
|
|
Path string
|
2021-03-19 22:28:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalPacket returns p as a two-part binary encoding of p.
|
2021-03-21 22:11:23 +08:00
|
|
|
func (p *RmdirPacket) MarshalPacket(reqid uint32) (header, payload []byte, err error) {
|
2021-03-20 05:19:45 +08:00
|
|
|
size := 4 + len(p.Path) // string(path)
|
2021-03-19 22:28:55 +08:00
|
|
|
|
2021-03-21 22:11:23 +08:00
|
|
|
b := NewMarshalBuffer(PacketTypeRmdir, reqid, size)
|
2021-03-19 22:37:06 +08:00
|
|
|
|
2021-03-19 22:28:55 +08:00
|
|
|
b.AppendString(p.Path)
|
|
|
|
|
2021-03-20 05:19:45 +08:00
|
|
|
return b.Packet(payload)
|
2021-03-19 22:28:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalPacketBody unmarshals the packet body from the given Buffer.
|
|
|
|
// It is assumed that the uint32(request-id) has already been consumed.
|
|
|
|
func (p *RmdirPacket) UnmarshalPacketBody(buf *Buffer) (err error) {
|
|
|
|
if p.Path, err = buf.ConsumeString(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RealpathPacket defines the SSH_FXP_REALPATH packet.
|
|
|
|
type RealpathPacket struct {
|
2021-03-21 22:11:23 +08:00
|
|
|
Path string
|
2021-03-19 22:28:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalPacket returns p as a two-part binary encoding of p.
|
2021-03-21 22:11:23 +08:00
|
|
|
func (p *RealpathPacket) MarshalPacket(reqid uint32) (header, payload []byte, err error) {
|
2021-03-20 05:19:45 +08:00
|
|
|
size := 4 + len(p.Path) // string(path)
|
2021-03-19 22:28:55 +08:00
|
|
|
|
2021-03-21 22:11:23 +08:00
|
|
|
b := NewMarshalBuffer(PacketTypeRealpath, reqid, size)
|
2021-03-19 22:37:06 +08:00
|
|
|
|
2021-03-19 22:28:55 +08:00
|
|
|
b.AppendString(p.Path)
|
|
|
|
|
2021-03-20 05:19:45 +08:00
|
|
|
return b.Packet(payload)
|
2021-03-19 22:28:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalPacketBody unmarshals the packet body from the given Buffer.
|
|
|
|
// It is assumed that the uint32(request-id) has already been consumed.
|
|
|
|
func (p *RealpathPacket) UnmarshalPacketBody(buf *Buffer) (err error) {
|
|
|
|
if p.Path, err = buf.ConsumeString(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// StatPacket defines the SSH_FXP_STAT packet.
|
|
|
|
type StatPacket struct {
|
2021-03-21 22:11:23 +08:00
|
|
|
Path string
|
2021-03-19 22:28:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalPacket returns p as a two-part binary encoding of p.
|
2021-03-21 22:11:23 +08:00
|
|
|
func (p *StatPacket) MarshalPacket(reqid uint32) (header, payload []byte, err error) {
|
2021-03-20 05:19:45 +08:00
|
|
|
size := 4 + len(p.Path) // string(path)
|
2021-03-19 22:28:55 +08:00
|
|
|
|
2021-03-21 22:11:23 +08:00
|
|
|
b := NewMarshalBuffer(PacketTypeStat, reqid, size)
|
2021-03-19 22:37:06 +08:00
|
|
|
|
2021-03-19 22:28:55 +08:00
|
|
|
b.AppendString(p.Path)
|
|
|
|
|
2021-03-20 05:19:45 +08:00
|
|
|
return b.Packet(payload)
|
2021-03-19 22:28:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalPacketBody unmarshals the packet body from the given Buffer.
|
|
|
|
// It is assumed that the uint32(request-id) has already been consumed.
|
|
|
|
func (p *StatPacket) UnmarshalPacketBody(buf *Buffer) (err error) {
|
|
|
|
if p.Path, err = buf.ConsumeString(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RenamePacket defines the SSH_FXP_RENAME packet.
|
|
|
|
type RenamePacket struct {
|
2021-03-21 22:11:23 +08:00
|
|
|
OldPath string
|
|
|
|
NewPath string
|
2021-03-19 22:28:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalPacket returns p as a two-part binary encoding of p.
|
2021-03-21 22:11:23 +08:00
|
|
|
func (p *RenamePacket) MarshalPacket(reqid uint32) (header, payload []byte, err error) {
|
2021-03-20 05:19:45 +08:00
|
|
|
// string(oldpath) + string(newpath)
|
|
|
|
size := 4 + len(p.OldPath) + 4 + len(p.NewPath)
|
2021-03-19 22:28:55 +08:00
|
|
|
|
2021-03-21 22:11:23 +08:00
|
|
|
b := NewMarshalBuffer(PacketTypeRename, reqid, size)
|
2021-03-19 22:37:06 +08:00
|
|
|
|
2021-03-19 22:28:55 +08:00
|
|
|
b.AppendString(p.OldPath)
|
|
|
|
b.AppendString(p.NewPath)
|
|
|
|
|
2021-03-20 05:19:45 +08:00
|
|
|
return b.Packet(payload)
|
2021-03-19 22:28:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalPacketBody unmarshals the packet body from the given Buffer.
|
|
|
|
// It is assumed that the uint32(request-id) has already been consumed.
|
|
|
|
func (p *RenamePacket) UnmarshalPacketBody(buf *Buffer) (err error) {
|
|
|
|
if p.OldPath, err = buf.ConsumeString(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.NewPath, err = buf.ConsumeString(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadlinkPacket defines the SSH_FXP_READLINK packet.
|
|
|
|
type ReadlinkPacket struct {
|
2021-03-21 22:11:23 +08:00
|
|
|
Path string
|
2021-03-19 22:28:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalPacket returns p as a two-part binary encoding of p.
|
2021-03-21 22:11:23 +08:00
|
|
|
func (p *ReadlinkPacket) MarshalPacket(reqid uint32) (header, payload []byte, err error) {
|
2021-03-20 05:19:45 +08:00
|
|
|
size := 4 + len(p.Path) // string(path)
|
2021-03-19 22:28:55 +08:00
|
|
|
|
2021-03-21 22:11:23 +08:00
|
|
|
b := NewMarshalBuffer(PacketTypeReadlink, reqid, size)
|
2021-03-19 22:37:06 +08:00
|
|
|
|
2021-03-19 22:28:55 +08:00
|
|
|
b.AppendString(p.Path)
|
|
|
|
|
2021-03-20 05:19:45 +08:00
|
|
|
return b.Packet(payload)
|
2021-03-19 22:28:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalPacketBody unmarshals the packet body from the given Buffer.
|
|
|
|
// It is assumed that the uint32(request-id) has already been consumed.
|
|
|
|
func (p *ReadlinkPacket) UnmarshalPacketBody(buf *Buffer) (err error) {
|
|
|
|
if p.Path, err = buf.ConsumeString(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SymlinkPacket defines the SSH_FXP_SYMLINK packet.
|
|
|
|
//
|
|
|
|
// The order of the arguments to the SSH_FXP_SYMLINK method was inadvertently reversed.
|
|
|
|
// Unfortunately, the reversal was not noticed until the server was widely deployed.
|
|
|
|
// Covered in Section 3.1 of https://github.com/openssh/openssh-portable/blob/master/PROTOCOL
|
|
|
|
type SymlinkPacket struct {
|
|
|
|
LinkPath string
|
|
|
|
TargetPath string
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalPacket returns p as a two-part binary encoding of p.
|
2021-03-21 22:11:23 +08:00
|
|
|
func (p *SymlinkPacket) MarshalPacket(reqid uint32) (header, payload []byte, err error) {
|
2021-03-20 05:19:45 +08:00
|
|
|
// string(targetpath) + string(linkpath)
|
|
|
|
size := 4 + len(p.TargetPath) + 4 + len(p.LinkPath)
|
2021-03-19 22:28:55 +08:00
|
|
|
|
2021-03-21 22:11:23 +08:00
|
|
|
b := NewMarshalBuffer(PacketTypeSymlink, reqid, size)
|
2021-03-19 22:37:06 +08:00
|
|
|
|
2021-03-20 05:19:45 +08:00
|
|
|
// Arguments were inadvertently reversed.
|
|
|
|
b.AppendString(p.TargetPath)
|
2021-03-19 22:28:55 +08:00
|
|
|
b.AppendString(p.LinkPath)
|
|
|
|
|
2021-03-20 05:19:45 +08:00
|
|
|
return b.Packet(payload)
|
2021-03-19 22:28:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalPacketBody unmarshals the packet body from the given Buffer.
|
|
|
|
// It is assumed that the uint32(request-id) has already been consumed.
|
|
|
|
func (p *SymlinkPacket) UnmarshalPacketBody(buf *Buffer) (err error) {
|
|
|
|
// Arguments were inadvertently reversed.
|
|
|
|
if p.TargetPath, err = buf.ConsumeString(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.LinkPath, err = buf.ConsumeString(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|