storj/lib/uplinkc/bucket.go

195 lines
5.4 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 (
"unsafe"
"storj.io/storj/lib/uplink"
"storj.io/storj/pkg/storj"
)
// Bucket is a scoped uplink.Bucket
type Bucket struct {
scope
*uplink.Bucket
}
// create_bucket creates a new bucket if authorized.
//export create_bucket
func create_bucket(projectHandle C.ProjectRef, name *C.char, bucketConfig *C.BucketConfig, cerr **C.char) C.BucketInfo {
project, ok := universe.Get(projectHandle._handle).(*Project)
if !ok {
*cerr = C.CString("invalid project")
return C.BucketInfo{}
}
var config *uplink.BucketConfig
if bucketConfig != nil {
config = &uplink.BucketConfig{
PathCipher: storj.CipherSuite(bucketConfig.path_cipher),
EncryptionParameters: storj.EncryptionParameters{
CipherSuite: storj.CipherSuite(bucketConfig.encryption_parameters.cipher_suite),
BlockSize: int32(bucketConfig.encryption_parameters.block_size),
},
}
config.Volatile.RedundancyScheme = storj.RedundancyScheme{
Algorithm: storj.RedundancyAlgorithm(bucketConfig.redundancy_scheme.algorithm),
ShareSize: int32(bucketConfig.redundancy_scheme.share_size),
RequiredShares: int16(bucketConfig.redundancy_scheme.required_shares),
RepairShares: int16(bucketConfig.redundancy_scheme.repair_shares),
OptimalShares: int16(bucketConfig.redundancy_scheme.optimal_shares),
TotalShares: int16(bucketConfig.redundancy_scheme.total_shares),
}
}
bucket, err := project.CreateBucket(project.scope.ctx, C.GoString(name), config)
if err != nil {
*cerr = C.CString(err.Error())
return C.BucketInfo{}
}
return newBucketInfo(&bucket)
}
// get_bucket_info returns info about the requested bucket if authorized.
//export get_bucket_info
func get_bucket_info(projectHandle C.ProjectRef, bucketName *C.char, cerr **C.char) C.BucketInfo {
project, ok := universe.Get(projectHandle._handle).(*Project)
if !ok {
*cerr = C.CString("invalid project")
return C.BucketInfo{}
}
bucket, _, err := project.GetBucketInfo(project.scope.ctx, C.GoString(bucketName))
if err != nil {
*cerr = C.CString(err.Error())
return C.BucketInfo{}
}
return newBucketInfo(&bucket)
}
lib/uplink: encryption context (#2349) * lib/uplink: encryption context Change-Id: I5c23dca3286a46b713b30c4997e9ae6e630b2280 * lib/uplink: bucket operation examples Change-Id: Ia0f6e69f365dcff0cf11c731f51b30842bce053b * lib/uplink: encryption key sharing test cases Change-Id: I3a172d565f33f4e591402cdcb9460664a7cc7fbe * fix encrypted path prefix restriction issue Change-Id: I8f3921f9d52aaf4b84039de608b8cbbc88769554 * implement panics in libuplink encryption code todo on cipher suite selection as well as an api concern Change-Id: Ifa39eb3cc4b3443f7d96f9304df9b2ac4ec4085d * implement GetProjectInfo api call to get salt Change-Id: Ic5f6b3be9ea35df48c1aa214ab5d355fb328e2cf * some fixes and accessors for encryption store Change-Id: I3bb61f6712a037900e2a96e72ad4029ec1d3f718 * general fixes to builds/tests/etc Change-Id: I9930fa96acb3b221d9a001f8e274af5729cc8a47 * java bindings changes Change-Id: Ia2bd4c9c69739c8d3154d79616cff1f36fb403b6 * get libuplink examples passing Change-Id: I828f09a144160e0a5dd932324f78491ae2ec8a07 * fix proto.lock file Change-Id: I2fbbf4d0976a7d0473c2645e6dcb21aaa3be7651 * fix proto.lock again Change-Id: I92702cf49e1a340eef6379c2be4f7c4a268112a9 * fix golint issues Change-Id: I631ff9f43307a58e3b25a58cbb4a4cc2495f5eb6 * more linting fixes Change-Id: I51f8f30b367b5bca14c94b15417b9a4c9e7aa0ce * bug fixed by structs bump Change-Id: Ibb03c691fce7606c35c08721b3ef0781ab48a38a * retrigger Change-Id: Ieee0470b6a2d07168a1578552e8e7f271ae93a13 * retrigger Change-Id: I753d63853171e6a436c104ce176048892eb974c5 * semantic merge conflict Change-Id: I9419448496de90340569047a6a16a1b858a7978a * update total to match prod defaults Change-Id: I693d55c1ebb28b5803ee1d26e9e198decf82308b * retrigger Change-Id: I28b74d5d6202f61aa3866fe407d423f6a0a14b9e * retrigger Change-Id: I6fd054885c715f602e2cef623fd464c42e88742c * retrigger Change-Id: I6a01bae88c72406d4ed5a8f13bf8a2b3c650bd2d
2019-06-27 18:36:51 +01:00
// open_bucket returns a Bucket handle with the given encryption context information.
2019-06-15 12:23:12 +01:00
//export open_bucket
func open_bucket(projectHandle C.ProjectRef, name *C.char, encryptionAccess *C.char, cerr **C.char) C.BucketRef {
2019-06-15 12:23:12 +01:00
project, ok := universe.Get(projectHandle._handle).(*Project)
if !ok {
*cerr = C.CString("invalid project")
return C.BucketRef{}
}
access, err := uplink.ParseEncryptionAccess(C.GoString(encryptionAccess))
lib/uplink: encryption context (#2349) * lib/uplink: encryption context Change-Id: I5c23dca3286a46b713b30c4997e9ae6e630b2280 * lib/uplink: bucket operation examples Change-Id: Ia0f6e69f365dcff0cf11c731f51b30842bce053b * lib/uplink: encryption key sharing test cases Change-Id: I3a172d565f33f4e591402cdcb9460664a7cc7fbe * fix encrypted path prefix restriction issue Change-Id: I8f3921f9d52aaf4b84039de608b8cbbc88769554 * implement panics in libuplink encryption code todo on cipher suite selection as well as an api concern Change-Id: Ifa39eb3cc4b3443f7d96f9304df9b2ac4ec4085d * implement GetProjectInfo api call to get salt Change-Id: Ic5f6b3be9ea35df48c1aa214ab5d355fb328e2cf * some fixes and accessors for encryption store Change-Id: I3bb61f6712a037900e2a96e72ad4029ec1d3f718 * general fixes to builds/tests/etc Change-Id: I9930fa96acb3b221d9a001f8e274af5729cc8a47 * java bindings changes Change-Id: Ia2bd4c9c69739c8d3154d79616cff1f36fb403b6 * get libuplink examples passing Change-Id: I828f09a144160e0a5dd932324f78491ae2ec8a07 * fix proto.lock file Change-Id: I2fbbf4d0976a7d0473c2645e6dcb21aaa3be7651 * fix proto.lock again Change-Id: I92702cf49e1a340eef6379c2be4f7c4a268112a9 * fix golint issues Change-Id: I631ff9f43307a58e3b25a58cbb4a4cc2495f5eb6 * more linting fixes Change-Id: I51f8f30b367b5bca14c94b15417b9a4c9e7aa0ce * bug fixed by structs bump Change-Id: Ibb03c691fce7606c35c08721b3ef0781ab48a38a * retrigger Change-Id: Ieee0470b6a2d07168a1578552e8e7f271ae93a13 * retrigger Change-Id: I753d63853171e6a436c104ce176048892eb974c5 * semantic merge conflict Change-Id: I9419448496de90340569047a6a16a1b858a7978a * update total to match prod defaults Change-Id: I693d55c1ebb28b5803ee1d26e9e198decf82308b * retrigger Change-Id: I28b74d5d6202f61aa3866fe407d423f6a0a14b9e * retrigger Change-Id: I6fd054885c715f602e2cef623fd464c42e88742c * retrigger Change-Id: I6a01bae88c72406d4ed5a8f13bf8a2b3c650bd2d
2019-06-27 18:36:51 +01:00
if err != nil {
*cerr = C.CString(err.Error())
return C.BucketRef{}
2019-06-15 12:23:12 +01:00
}
scope := project.scope.child()
bucket, err := project.OpenBucket(scope.ctx, C.GoString(name), access)
2019-06-15 12:23:12 +01:00
if err != nil {
*cerr = C.CString(err.Error())
return C.BucketRef{}
}
return C.BucketRef{universe.Add(&Bucket{scope, bucket})}
}
// list_buckets will list authorized buckets.
//export list_buckets
func list_buckets(projectHandle C.ProjectRef, bucketListOptions *C.BucketListOptions, cerr **C.char) C.BucketList {
project, ok := universe.Get(projectHandle._handle).(*Project)
if !ok {
*cerr = C.CString("invalid project")
return C.BucketList{}
}
var opts *uplink.BucketListOptions
if bucketListOptions != nil {
opts = &uplink.BucketListOptions{
Cursor: C.GoString(bucketListOptions.cursor),
Direction: storj.Forward,
2019-06-15 12:23:12 +01:00
Limit: int(bucketListOptions.limit),
}
}
bucketList, err := project.ListBuckets(project.scope.ctx, opts)
if err != nil {
*cerr = C.CString(err.Error())
return C.BucketList{}
}
listLen := len(bucketList.Items)
infoSize := int(unsafe.Sizeof(C.BucketInfo{}))
itemsPtr := C.malloc(C.size_t(listLen * infoSize))
items := (*[1 << 30]C.BucketInfo)(itemsPtr)
for i, bucket := range bucketList.Items {
bucket := bucket
items[i] = newBucketInfo(&bucket)
}
return C.BucketList{
more: C.bool(bucketList.More),
items: &items[0],
length: C.int32_t(listLen),
}
}
// delete_bucket deletes a bucket if authorized. If the bucket contains any
// Objects at the time of deletion, they may be lost permanently.
//export delete_bucket
func delete_bucket(projectHandle C.ProjectRef, bucketName *C.char, cerr **C.char) {
project, ok := universe.Get(projectHandle._handle).(*Project)
if !ok {
*cerr = C.CString("invalid project")
return
}
if err := project.DeleteBucket(project.scope.ctx, C.GoString(bucketName)); err != nil {
*cerr = C.CString(err.Error())
return
}
}
// close_bucket closes a Bucket handle.
//export close_bucket
func close_bucket(bucketHandle C.BucketRef, cerr **C.char) {
bucket, ok := universe.Get(bucketHandle._handle).(*Bucket)
if !ok {
*cerr = C.CString("invalid bucket")
return
}
universe.Del(bucketHandle._handle)
defer bucket.cancel()
if err := bucket.Close(); err != nil {
*cerr = C.CString(err.Error())
return
}
}
// free_bucket_info frees bucket info.
//export free_bucket_info
func free_bucket_info(bucketInfo *C.BucketInfo) {
C.free(unsafe.Pointer(bucketInfo.name))
bucketInfo.name = nil
}
// free_bucket_list will free a list of buckets
//export free_bucket_list
func free_bucket_list(bucketlist *C.BucketList) {
items := (*[1 << 30]C.BucketInfo)(unsafe.Pointer(bucketlist.items))
for i := 0; i < int(bucketlist.length); i++ {
free_bucket_info(&items[i])
}
C.free(unsafe.Pointer(bucketlist.items))
bucketlist.items = nil
}