github.com/cyverse/go-irodsclient@v0.13.2/irods/message/lock_data_object_request.go (about) 1 package message 2 3 import ( 4 "encoding/xml" 5 6 "github.com/cyverse/go-irodsclient/irods/common" 7 "github.com/cyverse/go-irodsclient/irods/types" 8 "golang.org/x/xerrors" 9 ) 10 11 // IRODSMessageLockDataObjectRequest stores data object lock request 12 type IRODSMessageLockDataObjectRequest IRODSMessageDataObjectRequest 13 14 // NewIRODSMessageLockDataObjectRequest creates a IRODSMessageLockDataObjectRequest message 15 func NewIRODSMessageLockDataObjectRequest(path string, lockType types.DataObjectLockType, lockCommand types.DataObjectLockCommand) *IRODSMessageLockDataObjectRequest { 16 request := &IRODSMessageLockDataObjectRequest{ 17 Path: path, 18 CreateMode: 0, 19 OpenFlags: 0, 20 Offset: 0, 21 Size: -1, 22 Threads: 0, 23 OperationType: 0, 24 KeyVals: IRODSMessageSSKeyVal{ 25 Length: 0, 26 }, 27 } 28 29 // lockType must be either types.DataObjectLockTypeRead or types.DataObjectLockTypeWrite 30 31 fileOpenMode := lockType.GetFileOpenMode() 32 request.OpenFlags = fileOpenMode.GetFlag() 33 34 request.KeyVals.Add(string(common.LOCK_TYPE_KW), string(lockType)) 35 request.KeyVals.Add(string(common.LOCK_CMD_KW), string(lockCommand)) 36 37 return request 38 } 39 40 // NewIRODSMessageReadLockDataObjectRequest creates a IRODSMessageLockDataObjectRequest message 41 func NewIRODSMessageReadLockDataObjectRequest(path string, lockCommand types.DataObjectLockCommand) *IRODSMessageLockDataObjectRequest { 42 request := &IRODSMessageLockDataObjectRequest{ 43 Path: path, 44 CreateMode: 0, 45 OpenFlags: int(types.O_RDONLY), 46 Offset: 0, 47 Size: -1, 48 Threads: 0, 49 OperationType: 0, 50 KeyVals: IRODSMessageSSKeyVal{ 51 Length: 0, 52 }, 53 } 54 55 request.KeyVals.Add(string(common.LOCK_TYPE_KW), string(types.DataObjectLockTypeRead)) 56 request.KeyVals.Add(string(common.LOCK_CMD_KW), string(lockCommand)) 57 58 return request 59 } 60 61 // NewIRODSMessageWriteLockDataObjectRequest creates a IRODSMessageLockDataObjectRequest message 62 func NewIRODSMessageWriteLockDataObjectRequest(path string, lockCommand types.DataObjectLockCommand) *IRODSMessageLockDataObjectRequest { 63 request := &IRODSMessageLockDataObjectRequest{ 64 Path: path, 65 CreateMode: 0, 66 OpenFlags: int(types.O_WRONLY), 67 Offset: 0, 68 Size: -1, 69 Threads: 0, 70 OperationType: 0, 71 KeyVals: IRODSMessageSSKeyVal{ 72 Length: 0, 73 }, 74 } 75 76 request.KeyVals.Add(string(common.LOCK_TYPE_KW), string(types.DataObjectLockTypeWrite)) 77 request.KeyVals.Add(string(common.LOCK_CMD_KW), string(lockCommand)) 78 79 return request 80 } 81 82 // AddKeyVal adds a key-value pair 83 func (msg *IRODSMessageLockDataObjectRequest) AddKeyVal(key common.KeyWord, val string) { 84 msg.KeyVals.Add(string(key), val) 85 } 86 87 // GetBytes returns byte array 88 func (msg *IRODSMessageLockDataObjectRequest) GetBytes() ([]byte, error) { 89 xmlBytes, err := xml.Marshal(msg) 90 if err != nil { 91 return nil, xerrors.Errorf("failed to marshal irods message to xml: %w", err) 92 } 93 return xmlBytes, nil 94 } 95 96 // FromBytes returns struct from bytes 97 func (msg *IRODSMessageLockDataObjectRequest) FromBytes(bytes []byte) error { 98 err := xml.Unmarshal(bytes, msg) 99 if err != nil { 100 return xerrors.Errorf("failed to unmarshal xml to irods message: %w", err) 101 } 102 return nil 103 } 104 105 // GetMessage builds a message 106 func (msg *IRODSMessageLockDataObjectRequest) GetMessage() (*IRODSMessage, error) { 107 bytes, err := msg.GetBytes() 108 if err != nil { 109 return nil, xerrors.Errorf("failed to get bytes from irods message: %w", err) 110 } 111 112 msgBody := IRODSMessageBody{ 113 Type: RODS_MESSAGE_API_REQ_TYPE, 114 Message: bytes, 115 Error: nil, 116 Bs: nil, 117 IntInfo: int32(common.DATA_OBJ_LOCK_AN), 118 } 119 120 msgHeader, err := msgBody.BuildHeader() 121 if err != nil { 122 return nil, xerrors.Errorf("failed to build header from irods message: %w", err) 123 } 124 125 return &IRODSMessage{ 126 Header: msgHeader, 127 Body: &msgBody, 128 }, nil 129 }