github.com/pkg/sftp@v1.13.6/internal/encoding/ssh/filexfer/open_packets.go (about) 1 package sshfx 2 3 // SSH_FXF_* flags. 4 const ( 5 FlagRead = 1 << iota // SSH_FXF_READ 6 FlagWrite // SSH_FXF_WRITE 7 FlagAppend // SSH_FXF_APPEND 8 FlagCreate // SSH_FXF_CREAT 9 FlagTruncate // SSH_FXF_TRUNC 10 FlagExclusive // SSH_FXF_EXCL 11 ) 12 13 // OpenPacket defines the SSH_FXP_OPEN packet. 14 type OpenPacket struct { 15 Filename string 16 PFlags uint32 17 Attrs Attributes 18 } 19 20 // Type returns the SSH_FXP_xy value associated with this packet type. 21 func (p *OpenPacket) Type() PacketType { 22 return PacketTypeOpen 23 } 24 25 // MarshalPacket returns p as a two-part binary encoding of p. 26 func (p *OpenPacket) MarshalPacket(reqid uint32, b []byte) (header, payload []byte, err error) { 27 buf := NewBuffer(b) 28 if buf.Cap() < 9 { 29 // string(filename) + uint32(pflags) + ATTRS(attrs) 30 size := 4 + len(p.Filename) + 4 + p.Attrs.Len() 31 buf = NewMarshalBuffer(size) 32 } 33 34 buf.StartPacket(PacketTypeOpen, reqid) 35 buf.AppendString(p.Filename) 36 buf.AppendUint32(p.PFlags) 37 38 p.Attrs.MarshalInto(buf) 39 40 return buf.Packet(payload) 41 } 42 43 // UnmarshalPacketBody unmarshals the packet body from the given Buffer. 44 // It is assumed that the uint32(request-id) has already been consumed. 45 func (p *OpenPacket) UnmarshalPacketBody(buf *Buffer) (err error) { 46 *p = OpenPacket{ 47 Filename: buf.ConsumeString(), 48 PFlags: buf.ConsumeUint32(), 49 } 50 51 return p.Attrs.UnmarshalFrom(buf) 52 } 53 54 // OpenDirPacket defines the SSH_FXP_OPENDIR packet. 55 type OpenDirPacket struct { 56 Path string 57 } 58 59 // Type returns the SSH_FXP_xy value associated with this packet type. 60 func (p *OpenDirPacket) Type() PacketType { 61 return PacketTypeOpenDir 62 } 63 64 // MarshalPacket returns p as a two-part binary encoding of p. 65 func (p *OpenDirPacket) MarshalPacket(reqid uint32, b []byte) (header, payload []byte, err error) { 66 buf := NewBuffer(b) 67 if buf.Cap() < 9 { 68 size := 4 + len(p.Path) // string(path) 69 buf = NewMarshalBuffer(size) 70 } 71 72 buf.StartPacket(PacketTypeOpenDir, reqid) 73 buf.AppendString(p.Path) 74 75 return buf.Packet(payload) 76 } 77 78 // UnmarshalPacketBody unmarshals the packet body from the given Buffer. 79 // It is assumed that the uint32(request-id) has already been consumed. 80 func (p *OpenDirPacket) UnmarshalPacketBody(buf *Buffer) (err error) { 81 *p = OpenDirPacket{ 82 Path: buf.ConsumeString(), 83 } 84 85 return buf.Err 86 }