storj/lib/uplinkc/convert.go

70 lines
2.5 KiB
Go
Raw Normal View History

2019-06-15 12:23:12 +01:00
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package main
// #include "uplink_definitions.h"
import "C"
import (
"storj.io/common/storj"
2019-06-15 12:23:12 +01:00
)
// newBucketInfo returns a C bucket struct converted from a go bucket struct.
func newBucketInfo(bucket *storj.Bucket) C.BucketInfo {
return C.BucketInfo{
name: C.CString(bucket.Name),
2019-06-19 15:51:06 +01:00
created: C.int64_t(bucket.Created.Unix()),
path_cipher: toCCipherSuite(bucket.PathCipher),
segment_size: C.uint64_t(bucket.DefaultSegmentsSize),
2019-06-15 12:23:12 +01:00
encryption_parameters: convertEncryptionParameters(&bucket.DefaultEncryptionParameters),
redundancy_scheme: convertRedundancyScheme(&bucket.DefaultRedundancyScheme),
2019-06-15 12:23:12 +01:00
}
}
2019-06-21 19:44:00 +01:00
// newObjectInfo returns a C object struct converted from a go object struct.
func newObjectInfo(object *storj.Object) C.ObjectInfo {
return C.ObjectInfo{
version: C.uint32_t(object.Version),
bucket: newBucketInfo(&object.Bucket),
path: C.CString(object.Path),
is_prefix: C.bool(object.IsPrefix),
size: C.int64_t(object.Size),
2019-06-21 19:44:00 +01:00
content_type: C.CString(object.ContentType),
created: C.int64_t(object.Created.Unix()),
modified: C.int64_t(object.Modified.Unix()),
expires: C.int64_t(object.Expires.Unix()),
}
}
2019-06-15 12:23:12 +01:00
// convertEncryptionParameters converts Go EncryptionParameters to C.
func convertEncryptionParameters(goParams *storj.EncryptionParameters) C.EncryptionParameters {
return C.EncryptionParameters{
2019-06-21 19:44:00 +01:00
cipher_suite: toCCipherSuite(goParams.CipherSuite),
2019-06-15 12:23:12 +01:00
block_size: C.int32_t(goParams.BlockSize),
}
}
// convertRedundancyScheme converts Go RedundancyScheme to C.
func convertRedundancyScheme(scheme *storj.RedundancyScheme) C.RedundancyScheme {
return C.RedundancyScheme{
2019-06-21 19:44:00 +01:00
algorithm: C.RedundancyAlgorithm(scheme.Algorithm),
2019-06-15 12:23:12 +01:00
share_size: C.int32_t(scheme.ShareSize),
required_shares: C.int16_t(scheme.RequiredShares),
repair_shares: C.int16_t(scheme.RepairShares),
optimal_shares: C.int16_t(scheme.OptimalShares),
total_shares: C.int16_t(scheme.TotalShares),
}
}
2019-06-21 19:44:00 +01:00
// toCCipherSuite converts a go cipher to its respective C cipher suite.
2019-06-21 19:44:00 +01:00
func toCCipherSuite(cipherSuite storj.CipherSuite) C.CipherSuite {
return C.CipherSuite(cipherSuite)
}
// safeConvertToInt converts the C.size_t to an int, and returns a boolean
// indicating if the conversion was lossless and semantically equivalent.
func safeConvertToInt(n C.size_t) (int, bool) {
return int(n), C.size_t(int(n)) == n && int(n) >= 0
}