go-hep.org/x/hep@v0.38.1/xrootd/xrdproto/mv/mv.go (about)

     1  // Copyright ©2018 The go-hep Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package mv contains the structures describing mv request.
     6  // See xrootd protocol specification (http://xrootd.org/doc/dev45/XRdv310.pdf, p. 106) for details.
     7  package mv // import "go-hep.org/x/hep/xrootd/xrdproto/mv"
     8  
     9  import (
    10  	"errors"
    11  	"fmt"
    12  	"strings"
    13  
    14  	"go-hep.org/x/hep/xrootd/internal/xrdenc"
    15  	"go-hep.org/x/hep/xrootd/xrdproto"
    16  )
    17  
    18  // RequestID is the id of the request, it is sent as part of message.
    19  // See xrootd protocol specification for details: http://xrootd.org/doc/dev45/XRdv310.pdf, 2.3 Client Request Format.
    20  const RequestID uint16 = 3009
    21  
    22  // Request holds mv request parameters.
    23  type Request struct {
    24  	_       [14]byte
    25  	OldPath string
    26  	NewPath string
    27  }
    28  
    29  // MarshalXrd implements xrdproto.Marshaler.
    30  func (req Request) MarshalXrd(wBuffer *xrdenc.WBuffer) error {
    31  	wBuffer.Next(14)
    32  	wBuffer.WriteU16(uint16(len(req.OldPath)))
    33  	wBuffer.WriteLen(len(req.OldPath) + len(req.NewPath) + 1)
    34  	wBuffer.WriteBytes([]byte(req.OldPath))
    35  	wBuffer.WriteBytes([]byte{' '})
    36  	wBuffer.WriteBytes([]byte(req.NewPath))
    37  	return nil
    38  }
    39  
    40  // UnmarshalXrd implements xrdproto.Unmarshaler.
    41  func (req *Request) UnmarshalXrd(rBuffer *xrdenc.RBuffer) error {
    42  	rBuffer.Skip(14)
    43  	fromLen := int(rBuffer.ReadU16())
    44  	paths := rBuffer.ReadStr()
    45  	if fromLen >= len(paths) {
    46  		return fmt.Errorf("xrootd: wrong mv request. Want fromLen < %d, got %d", len(paths)-1, fromLen)
    47  	}
    48  	if fromLen == 0 {
    49  		fromLen = strings.Index(paths, " ")
    50  		if fromLen == -1 {
    51  			return errors.New("xrootd: wrong mv request. Want paths to be separated by ' ', none found")
    52  		}
    53  	}
    54  	req.OldPath = string(paths[:fromLen])
    55  	req.NewPath = string(paths[fromLen+1:])
    56  	return nil
    57  }
    58  
    59  // ReqID implements xrdproto.Request.ReqID.
    60  func (req *Request) ReqID() uint16 { return RequestID }
    61  
    62  // ShouldSign implements xrdproto.Request.ShouldSign.
    63  func (req *Request) ShouldSign() bool { return false }
    64  
    65  // Opaque implements xrdproto.FilepathRequest.Opaque.
    66  func (req *Request) Opaque() string {
    67  	return xrdproto.Opaque(req.NewPath)
    68  }
    69  
    70  // SetOpaque implements xrdproto.FilepathRequest.SetOpaque.
    71  func (req *Request) SetOpaque(opaque string) {
    72  	xrdproto.SetOpaque(&req.NewPath, opaque)
    73  }