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

     1  package openssh
     2  
     3  import (
     4  	sshfx "github.com/pkg/sftp/internal/encoding/ssh/filexfer"
     5  )
     6  
     7  const extensionFSync = "fsync@openssh.com"
     8  
     9  // RegisterExtensionFSync registers the "fsync@openssh.com" extended packet with the encoding/ssh/filexfer package.
    10  func RegisterExtensionFSync() {
    11  	sshfx.RegisterExtendedPacketType(extensionFSync, func() sshfx.ExtendedData {
    12  		return new(FSyncExtendedPacket)
    13  	})
    14  }
    15  
    16  // ExtensionFSync returns an ExtensionPair suitable to append into an sshfx.InitPacket or sshfx.VersionPacket.
    17  func ExtensionFSync() *sshfx.ExtensionPair {
    18  	return &sshfx.ExtensionPair{
    19  		Name: extensionFSync,
    20  		Data: "1",
    21  	}
    22  }
    23  
    24  // FSyncExtendedPacket defines the fsync@openssh.com extend packet.
    25  type FSyncExtendedPacket struct {
    26  	Handle string
    27  }
    28  
    29  // Type returns the SSH_FXP_EXTENDED packet type.
    30  func (ep *FSyncExtendedPacket) Type() sshfx.PacketType {
    31  	return sshfx.PacketTypeExtended
    32  }
    33  
    34  // MarshalPacket returns ep as a two-part binary encoding of the full extended packet.
    35  func (ep *FSyncExtendedPacket) MarshalPacket(reqid uint32, b []byte) (header, payload []byte, err error) {
    36  	p := &sshfx.ExtendedPacket{
    37  		ExtendedRequest: extensionFSync,
    38  
    39  		Data: ep,
    40  	}
    41  	return p.MarshalPacket(reqid, b)
    42  }
    43  
    44  // MarshalInto encodes ep into the binary encoding of the fsync@openssh.com extended packet-specific data.
    45  func (ep *FSyncExtendedPacket) MarshalInto(buf *sshfx.Buffer) {
    46  	buf.AppendString(ep.Handle)
    47  }
    48  
    49  // MarshalBinary encodes ep into the binary encoding of the fsync@openssh.com extended packet-specific data.
    50  //
    51  // NOTE: This _only_ encodes the packet-specific data, it does not encode the full extended packet.
    52  func (ep *FSyncExtendedPacket) MarshalBinary() ([]byte, error) {
    53  	// string(handle)
    54  	size := 4 + len(ep.Handle)
    55  
    56  	buf := sshfx.NewBuffer(make([]byte, 0, size))
    57  	ep.MarshalInto(buf)
    58  	return buf.Bytes(), nil
    59  }
    60  
    61  // UnmarshalFrom decodes the fsync@openssh.com extended packet-specific data from buf.
    62  func (ep *FSyncExtendedPacket) UnmarshalFrom(buf *sshfx.Buffer) (err error) {
    63  	*ep = FSyncExtendedPacket{
    64  		Handle: buf.ConsumeString(),
    65  	}
    66  
    67  	return buf.Err
    68  }
    69  
    70  // UnmarshalBinary decodes the fsync@openssh.com extended packet-specific data into ep.
    71  func (ep *FSyncExtendedPacket) UnmarshalBinary(data []byte) (err error) {
    72  	return ep.UnmarshalFrom(sshfx.NewBuffer(data))
    73  }