github.com/cyverse/go-irodsclient@v0.13.2/irods/fs/checksum.go (about)

     1  package fs
     2  
     3  import (
     4  	"github.com/cyverse/go-irodsclient/irods/common"
     5  	"github.com/cyverse/go-irodsclient/irods/connection"
     6  	"github.com/cyverse/go-irodsclient/irods/message"
     7  	"github.com/cyverse/go-irodsclient/irods/types"
     8  	"golang.org/x/xerrors"
     9  )
    10  
    11  // GetDataObjectChecksum returns a data object checksum for the path
    12  func GetDataObjectChecksum(conn *connection.IRODSConnection, path string, resource string) (*types.IRODSChecksum, error) {
    13  	if conn == nil || !conn.IsConnected() {
    14  		return nil, xerrors.Errorf("connection is nil or disconnected")
    15  	}
    16  
    17  	metrics := conn.GetMetrics()
    18  	if metrics != nil {
    19  		metrics.IncreaseCounterForStat(1)
    20  	}
    21  
    22  	// lock the connection
    23  	conn.Lock()
    24  	defer conn.Unlock()
    25  
    26  	// use default resource when resource param is empty
    27  	if len(resource) == 0 {
    28  		account := conn.GetAccount()
    29  		resource = account.DefaultResource
    30  	}
    31  
    32  	request := message.NewIRODSMessageChecksumRequest(path, resource)
    33  	response := message.IRODSMessageChecksumResponse{}
    34  	err := conn.RequestAndCheck(request, &response, nil)
    35  	if err != nil {
    36  		if types.GetIRODSErrorCode(err) == common.CAT_NO_ROWS_FOUND {
    37  			return nil, xerrors.Errorf("failed to find the data object for path %s: %w", path, types.NewFileNotFoundError(path))
    38  		}
    39  		return nil, xerrors.Errorf("failed to get data object checksum: %w", err)
    40  	}
    41  
    42  	checksum, err := types.CreateIRODSChecksum(response.Checksum)
    43  	if err != nil {
    44  		return nil, xerrors.Errorf("failed to create iRODS checksum: %w", err)
    45  	}
    46  
    47  	return checksum, nil
    48  }