github.com/pkg/sftp@v1.13.6/internal/encoding/ssh/filexfer/path_packets.go (about)

     1  package sshfx
     2  
     3  // LStatPacket defines the SSH_FXP_LSTAT packet.
     4  type LStatPacket struct {
     5  	Path string
     6  }
     7  
     8  // Type returns the SSH_FXP_xy value associated with this packet type.
     9  func (p *LStatPacket) Type() PacketType {
    10  	return PacketTypeLStat
    11  }
    12  
    13  // MarshalPacket returns p as a two-part binary encoding of p.
    14  func (p *LStatPacket) MarshalPacket(reqid uint32, b []byte) (header, payload []byte, err error) {
    15  	buf := NewBuffer(b)
    16  	if buf.Cap() < 9 {
    17  		size := 4 + len(p.Path) // string(path)
    18  		buf = NewMarshalBuffer(size)
    19  	}
    20  
    21  	buf.StartPacket(PacketTypeLStat, reqid)
    22  	buf.AppendString(p.Path)
    23  
    24  	return buf.Packet(payload)
    25  }
    26  
    27  // UnmarshalPacketBody unmarshals the packet body from the given Buffer.
    28  // It is assumed that the uint32(request-id) has already been consumed.
    29  func (p *LStatPacket) UnmarshalPacketBody(buf *Buffer) (err error) {
    30  	*p = LStatPacket{
    31  		Path: buf.ConsumeString(),
    32  	}
    33  
    34  	return buf.Err
    35  }
    36  
    37  // SetstatPacket defines the SSH_FXP_SETSTAT packet.
    38  type SetstatPacket struct {
    39  	Path  string
    40  	Attrs Attributes
    41  }
    42  
    43  // Type returns the SSH_FXP_xy value associated with this packet type.
    44  func (p *SetstatPacket) Type() PacketType {
    45  	return PacketTypeSetstat
    46  }
    47  
    48  // MarshalPacket returns p as a two-part binary encoding of p.
    49  func (p *SetstatPacket) MarshalPacket(reqid uint32, b []byte) (header, payload []byte, err error) {
    50  	buf := NewBuffer(b)
    51  	if buf.Cap() < 9 {
    52  		size := 4 + len(p.Path) + p.Attrs.Len() // string(path) + ATTRS(attrs)
    53  		buf = NewMarshalBuffer(size)
    54  	}
    55  
    56  	buf.StartPacket(PacketTypeSetstat, reqid)
    57  	buf.AppendString(p.Path)
    58  
    59  	p.Attrs.MarshalInto(buf)
    60  
    61  	return buf.Packet(payload)
    62  }
    63  
    64  // UnmarshalPacketBody unmarshals the packet body from the given Buffer.
    65  // It is assumed that the uint32(request-id) has already been consumed.
    66  func (p *SetstatPacket) UnmarshalPacketBody(buf *Buffer) (err error) {
    67  	*p = SetstatPacket{
    68  		Path: buf.ConsumeString(),
    69  	}
    70  
    71  	return p.Attrs.UnmarshalFrom(buf)
    72  }
    73  
    74  // RemovePacket defines the SSH_FXP_REMOVE packet.
    75  type RemovePacket struct {
    76  	Path string
    77  }
    78  
    79  // Type returns the SSH_FXP_xy value associated with this packet type.
    80  func (p *RemovePacket) Type() PacketType {
    81  	return PacketTypeRemove
    82  }
    83  
    84  // MarshalPacket returns p as a two-part binary encoding of p.
    85  func (p *RemovePacket) MarshalPacket(reqid uint32, b []byte) (header, payload []byte, err error) {
    86  	buf := NewBuffer(b)
    87  	if buf.Cap() < 9 {
    88  		size := 4 + len(p.Path) // string(path)
    89  		buf = NewMarshalBuffer(size)
    90  	}
    91  
    92  	buf.StartPacket(PacketTypeRemove, reqid)
    93  	buf.AppendString(p.Path)
    94  
    95  	return buf.Packet(payload)
    96  }
    97  
    98  // UnmarshalPacketBody unmarshals the packet body from the given Buffer.
    99  // It is assumed that the uint32(request-id) has already been consumed.
   100  func (p *RemovePacket) UnmarshalPacketBody(buf *Buffer) (err error) {
   101  	*p = RemovePacket{
   102  		Path: buf.ConsumeString(),
   103  	}
   104  
   105  	return buf.Err
   106  }
   107  
   108  // MkdirPacket defines the SSH_FXP_MKDIR packet.
   109  type MkdirPacket struct {
   110  	Path  string
   111  	Attrs Attributes
   112  }
   113  
   114  // Type returns the SSH_FXP_xy value associated with this packet type.
   115  func (p *MkdirPacket) Type() PacketType {
   116  	return PacketTypeMkdir
   117  }
   118  
   119  // MarshalPacket returns p as a two-part binary encoding of p.
   120  func (p *MkdirPacket) MarshalPacket(reqid uint32, b []byte) (header, payload []byte, err error) {
   121  	buf := NewBuffer(b)
   122  	if buf.Cap() < 9 {
   123  		size := 4 + len(p.Path) + p.Attrs.Len() // string(path) + ATTRS(attrs)
   124  		buf = NewMarshalBuffer(size)
   125  	}
   126  
   127  	buf.StartPacket(PacketTypeMkdir, reqid)
   128  	buf.AppendString(p.Path)
   129  
   130  	p.Attrs.MarshalInto(buf)
   131  
   132  	return buf.Packet(payload)
   133  }
   134  
   135  // UnmarshalPacketBody unmarshals the packet body from the given Buffer.
   136  // It is assumed that the uint32(request-id) has already been consumed.
   137  func (p *MkdirPacket) UnmarshalPacketBody(buf *Buffer) (err error) {
   138  	*p = MkdirPacket{
   139  		Path: buf.ConsumeString(),
   140  	}
   141  
   142  	return p.Attrs.UnmarshalFrom(buf)
   143  }
   144  
   145  // RmdirPacket defines the SSH_FXP_RMDIR packet.
   146  type RmdirPacket struct {
   147  	Path string
   148  }
   149  
   150  // Type returns the SSH_FXP_xy value associated with this packet type.
   151  func (p *RmdirPacket) Type() PacketType {
   152  	return PacketTypeRmdir
   153  }
   154  
   155  // MarshalPacket returns p as a two-part binary encoding of p.
   156  func (p *RmdirPacket) MarshalPacket(reqid uint32, b []byte) (header, payload []byte, err error) {
   157  	buf := NewBuffer(b)
   158  	if buf.Cap() < 9 {
   159  		size := 4 + len(p.Path) // string(path)
   160  		buf = NewMarshalBuffer(size)
   161  	}
   162  
   163  	buf.StartPacket(PacketTypeRmdir, reqid)
   164  	buf.AppendString(p.Path)
   165  
   166  	return buf.Packet(payload)
   167  }
   168  
   169  // UnmarshalPacketBody unmarshals the packet body from the given Buffer.
   170  // It is assumed that the uint32(request-id) has already been consumed.
   171  func (p *RmdirPacket) UnmarshalPacketBody(buf *Buffer) (err error) {
   172  	*p = RmdirPacket{
   173  		Path: buf.ConsumeString(),
   174  	}
   175  
   176  	return buf.Err
   177  }
   178  
   179  // RealPathPacket defines the SSH_FXP_REALPATH packet.
   180  type RealPathPacket struct {
   181  	Path string
   182  }
   183  
   184  // Type returns the SSH_FXP_xy value associated with this packet type.
   185  func (p *RealPathPacket) Type() PacketType {
   186  	return PacketTypeRealPath
   187  }
   188  
   189  // MarshalPacket returns p as a two-part binary encoding of p.
   190  func (p *RealPathPacket) MarshalPacket(reqid uint32, b []byte) (header, payload []byte, err error) {
   191  	buf := NewBuffer(b)
   192  	if buf.Cap() < 9 {
   193  		size := 4 + len(p.Path) // string(path)
   194  		buf = NewMarshalBuffer(size)
   195  	}
   196  
   197  	buf.StartPacket(PacketTypeRealPath, reqid)
   198  	buf.AppendString(p.Path)
   199  
   200  	return buf.Packet(payload)
   201  }
   202  
   203  // UnmarshalPacketBody unmarshals the packet body from the given Buffer.
   204  // It is assumed that the uint32(request-id) has already been consumed.
   205  func (p *RealPathPacket) UnmarshalPacketBody(buf *Buffer) (err error) {
   206  	*p = RealPathPacket{
   207  		Path: buf.ConsumeString(),
   208  	}
   209  
   210  	return buf.Err
   211  }
   212  
   213  // StatPacket defines the SSH_FXP_STAT packet.
   214  type StatPacket struct {
   215  	Path string
   216  }
   217  
   218  // Type returns the SSH_FXP_xy value associated with this packet type.
   219  func (p *StatPacket) Type() PacketType {
   220  	return PacketTypeStat
   221  }
   222  
   223  // MarshalPacket returns p as a two-part binary encoding of p.
   224  func (p *StatPacket) MarshalPacket(reqid uint32, b []byte) (header, payload []byte, err error) {
   225  	buf := NewBuffer(b)
   226  	if buf.Cap() < 9 {
   227  		size := 4 + len(p.Path) // string(path)
   228  		buf = NewMarshalBuffer(size)
   229  	}
   230  
   231  	buf.StartPacket(PacketTypeStat, reqid)
   232  	buf.AppendString(p.Path)
   233  
   234  	return buf.Packet(payload)
   235  }
   236  
   237  // UnmarshalPacketBody unmarshals the packet body from the given Buffer.
   238  // It is assumed that the uint32(request-id) has already been consumed.
   239  func (p *StatPacket) UnmarshalPacketBody(buf *Buffer) (err error) {
   240  	*p = StatPacket{
   241  		Path: buf.ConsumeString(),
   242  	}
   243  
   244  	return buf.Err
   245  }
   246  
   247  // RenamePacket defines the SSH_FXP_RENAME packet.
   248  type RenamePacket struct {
   249  	OldPath string
   250  	NewPath string
   251  }
   252  
   253  // Type returns the SSH_FXP_xy value associated with this packet type.
   254  func (p *RenamePacket) Type() PacketType {
   255  	return PacketTypeRename
   256  }
   257  
   258  // MarshalPacket returns p as a two-part binary encoding of p.
   259  func (p *RenamePacket) MarshalPacket(reqid uint32, b []byte) (header, payload []byte, err error) {
   260  	buf := NewBuffer(b)
   261  	if buf.Cap() < 9 {
   262  		// string(oldpath) + string(newpath)
   263  		size := 4 + len(p.OldPath) + 4 + len(p.NewPath)
   264  		buf = NewMarshalBuffer(size)
   265  	}
   266  
   267  	buf.StartPacket(PacketTypeRename, reqid)
   268  	buf.AppendString(p.OldPath)
   269  	buf.AppendString(p.NewPath)
   270  
   271  	return buf.Packet(payload)
   272  }
   273  
   274  // UnmarshalPacketBody unmarshals the packet body from the given Buffer.
   275  // It is assumed that the uint32(request-id) has already been consumed.
   276  func (p *RenamePacket) UnmarshalPacketBody(buf *Buffer) (err error) {
   277  	*p = RenamePacket{
   278  		OldPath: buf.ConsumeString(),
   279  		NewPath: buf.ConsumeString(),
   280  	}
   281  
   282  	return buf.Err
   283  }
   284  
   285  // ReadLinkPacket defines the SSH_FXP_READLINK packet.
   286  type ReadLinkPacket struct {
   287  	Path string
   288  }
   289  
   290  // Type returns the SSH_FXP_xy value associated with this packet type.
   291  func (p *ReadLinkPacket) Type() PacketType {
   292  	return PacketTypeReadLink
   293  }
   294  
   295  // MarshalPacket returns p as a two-part binary encoding of p.
   296  func (p *ReadLinkPacket) MarshalPacket(reqid uint32, b []byte) (header, payload []byte, err error) {
   297  	buf := NewBuffer(b)
   298  	if buf.Cap() < 9 {
   299  		size := 4 + len(p.Path) // string(path)
   300  		buf = NewMarshalBuffer(size)
   301  	}
   302  
   303  	buf.StartPacket(PacketTypeReadLink, reqid)
   304  	buf.AppendString(p.Path)
   305  
   306  	return buf.Packet(payload)
   307  }
   308  
   309  // UnmarshalPacketBody unmarshals the packet body from the given Buffer.
   310  // It is assumed that the uint32(request-id) has already been consumed.
   311  func (p *ReadLinkPacket) UnmarshalPacketBody(buf *Buffer) (err error) {
   312  	*p = ReadLinkPacket{
   313  		Path: buf.ConsumeString(),
   314  	}
   315  
   316  	return buf.Err
   317  }
   318  
   319  // SymlinkPacket defines the SSH_FXP_SYMLINK packet.
   320  //
   321  // The order of the arguments to the SSH_FXP_SYMLINK method was inadvertently reversed.
   322  // Unfortunately, the reversal was not noticed until the server was widely deployed.
   323  // Covered in Section 4.1 of https://github.com/openssh/openssh-portable/blob/master/PROTOCOL
   324  type SymlinkPacket struct {
   325  	LinkPath   string
   326  	TargetPath string
   327  }
   328  
   329  // Type returns the SSH_FXP_xy value associated with this packet type.
   330  func (p *SymlinkPacket) Type() PacketType {
   331  	return PacketTypeSymlink
   332  }
   333  
   334  // MarshalPacket returns p as a two-part binary encoding of p.
   335  func (p *SymlinkPacket) MarshalPacket(reqid uint32, b []byte) (header, payload []byte, err error) {
   336  	buf := NewBuffer(b)
   337  	if buf.Cap() < 9 {
   338  		// string(targetpath) + string(linkpath)
   339  		size := 4 + len(p.TargetPath) + 4 + len(p.LinkPath)
   340  		buf = NewMarshalBuffer(size)
   341  	}
   342  
   343  	buf.StartPacket(PacketTypeSymlink, reqid)
   344  
   345  	// Arguments were inadvertently reversed.
   346  	buf.AppendString(p.TargetPath)
   347  	buf.AppendString(p.LinkPath)
   348  
   349  	return buf.Packet(payload)
   350  }
   351  
   352  // UnmarshalPacketBody unmarshals the packet body from the given Buffer.
   353  // It is assumed that the uint32(request-id) has already been consumed.
   354  func (p *SymlinkPacket) UnmarshalPacketBody(buf *Buffer) (err error) {
   355  	*p = SymlinkPacket{
   356  		// Arguments were inadvertently reversed.
   357  		TargetPath: buf.ConsumeString(),
   358  		LinkPath:   buf.ConsumeString(),
   359  	}
   360  
   361  	return buf.Err
   362  }