github.com/cyverse/go-irodsclient@v0.13.2/irods/message/checksum_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  // IRODSMessageChecksumRequest stores data object checksum request
    12  type IRODSMessageChecksumRequest IRODSMessageDataObjectRequest
    13  
    14  // NewIRODSMessageChecksumRequest creates a IRODSMessageChecksumRequest message
    15  func NewIRODSMessageChecksumRequest(path string, resource string) *IRODSMessageChecksumRequest {
    16  	fileOpenMode := types.FileOpenMode("r")
    17  	flag := fileOpenMode.GetFlag()
    18  
    19  	request := &IRODSMessageChecksumRequest{
    20  		Path:          path,
    21  		CreateMode:    0,
    22  		OpenFlags:     flag,
    23  		Offset:        0,
    24  		Size:          -1,
    25  		Threads:       0,
    26  		OperationType: 0,
    27  		KeyVals: IRODSMessageSSKeyVal{
    28  			Length: 0,
    29  		},
    30  	}
    31  
    32  	if len(resource) > 0 {
    33  		request.KeyVals.Add(string(common.DEST_RESC_NAME_KW), resource)
    34  	}
    35  
    36  	return request
    37  }
    38  
    39  // AddKeyVal adds a key-value pair
    40  func (msg *IRODSMessageChecksumRequest) AddKeyVal(key common.KeyWord, val string) {
    41  	msg.KeyVals.Add(string(key), val)
    42  }
    43  
    44  // GetBytes returns byte array
    45  func (msg *IRODSMessageChecksumRequest) GetBytes() ([]byte, error) {
    46  	xmlBytes, err := xml.Marshal(msg)
    47  	if err != nil {
    48  		return nil, xerrors.Errorf("failed to marshal irods message to xml: %w", err)
    49  	}
    50  	return xmlBytes, nil
    51  }
    52  
    53  // FromBytes returns struct from bytes
    54  func (msg *IRODSMessageChecksumRequest) FromBytes(bytes []byte) error {
    55  	err := xml.Unmarshal(bytes, msg)
    56  	if err != nil {
    57  		return xerrors.Errorf("failed to unmarshal xml to irods message: %w", err)
    58  	}
    59  	return nil
    60  }
    61  
    62  // GetMessage builds a message
    63  func (msg *IRODSMessageChecksumRequest) GetMessage() (*IRODSMessage, error) {
    64  	bytes, err := msg.GetBytes()
    65  	if err != nil {
    66  		return nil, xerrors.Errorf("failed to get bytes from irods message: %w", err)
    67  	}
    68  
    69  	msgBody := IRODSMessageBody{
    70  		Type:    RODS_MESSAGE_API_REQ_TYPE,
    71  		Message: bytes,
    72  		Error:   nil,
    73  		Bs:      nil,
    74  		IntInfo: int32(common.DATA_OBJ_CHKSUM_AN),
    75  	}
    76  
    77  	msgHeader, err := msgBody.BuildHeader()
    78  	if err != nil {
    79  		return nil, xerrors.Errorf("failed to build header from irods message: %w", err)
    80  	}
    81  
    82  	return &IRODSMessage{
    83  		Header: msgHeader,
    84  		Body:   &msgBody,
    85  	}, nil
    86  }