go-hep.org/x/hep@v0.38.1/xrootd/xrdproto/open/open.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 open contains the structures describing request and response for open request. 6 // See xrootd protocol specification (http://xrootd.org/doc/dev45/XRdv310.pdf, p. 63) for details. 7 package open // import "go-hep.org/x/hep/xrootd/xrdproto/open" 8 9 import ( 10 "go-hep.org/x/hep/xrootd/internal/xrdenc" 11 "go-hep.org/x/hep/xrootd/xrdfs" 12 "go-hep.org/x/hep/xrootd/xrdproto" 13 ) 14 15 // RequestID is the id of the request, it is sent as part of message. 16 // See xrootd protocol specification for details: http://xrootd.org/doc/dev45/XRdv310.pdf, 2.3 Client Request Format. 17 const RequestID uint16 = 3010 18 19 // Response is a response for the open request, 20 // which contains the file handle, the compression page size, 21 // the compression type and the stat information. 22 type Response struct { 23 FileHandle xrdfs.FileHandle 24 Compression *xrdfs.FileCompression 25 Stat *xrdfs.EntryStat 26 } 27 28 // MarshalXrd implements xrdproto.Marshaler. 29 func (o Response) MarshalXrd(wBuffer *xrdenc.WBuffer) error { 30 wBuffer.WriteBytes(o.FileHandle[:]) 31 if o.Compression == nil { 32 return nil 33 } 34 if err := o.Compression.MarshalXrd(wBuffer); err != nil { 35 return err 36 } 37 38 if o.Stat == nil { 39 return nil 40 } 41 if err := o.Stat.MarshalXrd(wBuffer); err != nil { 42 return err 43 } 44 return nil 45 } 46 47 // UnmarshalXrd implements xrdproto.Unmarshaler. 48 func (o *Response) UnmarshalXrd(rBuffer *xrdenc.RBuffer) error { 49 rBuffer.ReadBytes(o.FileHandle[:]) 50 if rBuffer.Len() == 0 { 51 return nil 52 } 53 o.Compression = &xrdfs.FileCompression{} 54 if err := o.Compression.UnmarshalXrd(rBuffer); err != nil { 55 return err 56 } 57 if rBuffer.Len() == 0 { 58 return nil 59 } 60 o.Stat = &xrdfs.EntryStat{} 61 if err := o.Stat.UnmarshalXrd(rBuffer); err != nil { 62 return err 63 } 64 return nil 65 } 66 67 // RespID implements xrdproto.Response.RespID. 68 func (resp *Response) RespID() uint16 { return RequestID } 69 70 // Request holds open request parameters. 71 type Request struct { 72 Mode xrdfs.OpenMode 73 Options xrdfs.OpenOptions 74 _ [12]byte 75 Path string 76 } 77 78 // Opaque implements xrdproto.FilepathRequest.Opaque. 79 func (req *Request) Opaque() string { 80 return xrdproto.Opaque(req.Path) 81 } 82 83 // SetOpaque implements xrdproto.FilepathRequest.SetOpaque. 84 func (req *Request) SetOpaque(opaque string) { 85 xrdproto.SetOpaque(&req.Path, opaque) 86 } 87 88 // NewRequest forms a Request according to provided path, mode, and options. 89 func NewRequest(path string, mode xrdfs.OpenMode, options xrdfs.OpenOptions) *Request { 90 return &Request{Mode: mode, Options: options, Path: path} 91 } 92 93 // MarshalXrd implements xrdproto.Marshaler. 94 func (o Request) MarshalXrd(wBuffer *xrdenc.WBuffer) error { 95 wBuffer.WriteU16(uint16(o.Mode)) 96 wBuffer.WriteU16(uint16(o.Options)) 97 wBuffer.Next(12) 98 wBuffer.WriteStr(o.Path) 99 return nil 100 } 101 102 // UnmarshalXrd implements xrdproto.Unmarshaler. 103 func (o *Request) UnmarshalXrd(rBuffer *xrdenc.RBuffer) error { 104 o.Mode = xrdfs.OpenMode(rBuffer.ReadU16()) 105 o.Options = xrdfs.OpenOptions(rBuffer.ReadU16()) 106 rBuffer.Skip(12) 107 o.Path = rBuffer.ReadStr() 108 return nil 109 } 110 111 // ReqID implements xrdproto.Request.ReqID. 112 func (req *Request) ReqID() uint16 { return RequestID } 113 114 // ShouldSign implements xrdproto.Request.ShouldSign. 115 func (req *Request) ShouldSign() bool { 116 // According to specification, the open request needs to be signed 117 // if any of the following options has been specified. 118 return req.Options&xrdfs.OpenOptionsDelete != 0 || 119 req.Options&xrdfs.OpenOptionsNew != 0 || 120 req.Options&xrdfs.OpenOptionsOpenUpdate != 0 || 121 req.Options&xrdfs.OpenOptionsMkPath != 0 || 122 req.Options&xrdfs.OpenOptionsOpenAppend != 0 123 } 124 125 var ( 126 _ xrdproto.FilepathRequest = (*Request)(nil) 127 )