|
| 1 | +//go:build ceph_preview |
| 2 | + |
| 3 | +package rados |
| 4 | + |
| 5 | +/* |
| 6 | +#cgo LDFLAGS: -lrados |
| 7 | +#include <stdlib.h> |
| 8 | +#include <rados/librados.h> |
| 9 | +*/ |
| 10 | +import "C" |
| 11 | + |
| 12 | +import "unsafe" |
| 13 | + |
| 14 | +// Checksum calculates the checksum of the given object data, using one of the supported checksum algorithms. |
| 15 | +// |
| 16 | +// Implements: |
| 17 | +// |
| 18 | +// int rados_checksum(rados_ioctx_t io, |
| 19 | +// const char *oid, |
| 20 | +// rados_checksum_type_t type, |
| 21 | +// const char *init_value, |
| 22 | +// size_t init_value_len, |
| 23 | +// size_t len, |
| 24 | +// uint64_t off, |
| 25 | +// size_t chunk_size, |
| 26 | +// char *pchecksum, |
| 27 | +// size_t checksum_len); |
| 28 | +func (ioctx *IOContext) Checksum(oid string, checksumType ChecksumType, dst []byte, opts *ChecksumOptions) error { |
| 29 | + // apply defaults |
| 30 | + if opts == nil { |
| 31 | + opts = &ChecksumOptions{} |
| 32 | + } |
| 33 | + if opts.InitValue == nil { |
| 34 | + initLen := 4 |
| 35 | + if checksumType == ChecksumTypeXXHash64 { |
| 36 | + initLen = 8 |
| 37 | + } |
| 38 | + opts.InitValue = make([]byte, initLen) |
| 39 | + } |
| 40 | + |
| 41 | + // call library |
| 42 | + coid := C.CString(oid) |
| 43 | + defer C.free(unsafe.Pointer(coid)) |
| 44 | + |
| 45 | + return getError(C.rados_checksum( |
| 46 | + ioctx.ioctx, |
| 47 | + coid, |
| 48 | + C.rados_checksum_type_t(checksumType), |
| 49 | + (*C.char)(unsafe.Pointer(&opts.InitValue[0])), |
| 50 | + C.size_t(len(opts.InitValue)), |
| 51 | + C.size_t(opts.Len), |
| 52 | + C.uint64_t(opts.Off), |
| 53 | + C.size_t(opts.ChunkSize), |
| 54 | + (*C.char)(unsafe.Pointer(&dst[0])), |
| 55 | + C.size_t(len(dst)), |
| 56 | + )) |
| 57 | +} |
| 58 | + |
| 59 | +// ChecksumType indicates checksum algorithm types supported by the IOContext.Checksum method. |
| 60 | +// Equivalent to the rados_checksum_type_t enum. |
| 61 | +type ChecksumType uint32 |
| 62 | + |
| 63 | +const ( |
| 64 | + // ChecksumTypeXXHash32 produces an encoded le32 checksum of the given object. |
| 65 | + ChecksumTypeXXHash32 = ChecksumType(C.LIBRADOS_CHECKSUM_TYPE_XXHASH32) |
| 66 | + // ChecksumTypeXXHash64 produces an encoded le64 checksum of the given object. |
| 67 | + ChecksumTypeXXHash64 = ChecksumType(C.LIBRADOS_CHECKSUM_TYPE_XXHASH64) |
| 68 | + // ChecksumTypeCRC32C produces an encoded le32 checksum of the given object. |
| 69 | + ChecksumTypeCRC32C = ChecksumType(C.LIBRADOS_CHECKSUM_TYPE_CRC32C) |
| 70 | +) |
| 71 | + |
| 72 | +// ChecksumOptions exposes non-required parameters for the Checksum method. |
| 73 | +type ChecksumOptions struct { |
| 74 | + // Off sets the object offset to start checksumming in the object. |
| 75 | + // By default, the entire object will be checksummed. |
| 76 | + Off uint64 |
| 77 | + // Len sets the the number of bytes to checksum in the object. |
| 78 | + // By default, the entire object will be checksummed. |
| 79 | + Len uint64 |
| 80 | + // ChunkSize sets the length-aligned chunk size for the checksum calculation. |
| 81 | + // By default, the entire object will be checksummed as a single chunk. |
| 82 | + ChunkSize uint64 |
| 83 | + // InitValue sets the initial value for the checksum calculation. |
| 84 | + // By default, the initial value will be a zeroed-out byte slice. |
| 85 | + InitValue []byte |
| 86 | +} |
0 commit comments