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 (
|
2019-07-30 12:40:05 +01:00
|
|
|
"fmt"
|
2019-06-15 12:23:12 +01:00
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"storj.io/storj/lib/uplink"
|
|
|
|
"storj.io/storj/pkg/storj"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Bucket is a scoped uplink.Bucket
|
|
|
|
type Bucket struct {
|
|
|
|
scope
|
|
|
|
*uplink.Bucket
|
|
|
|
}
|
|
|
|
|
|
|
|
//export create_bucket
|
2019-07-30 12:40:05 +01:00
|
|
|
// create_bucket creates a new bucket if authorized.
|
2019-06-15 12:23:12 +01:00
|
|
|
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 {
|
2019-07-30 12:40:05 +01:00
|
|
|
*cerr = C.CString(fmt.Sprintf("%+v", err))
|
2019-06-15 12:23:12 +01:00
|
|
|
return C.BucketInfo{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newBucketInfo(&bucket)
|
|
|
|
}
|
|
|
|
|
|
|
|
//export get_bucket_info
|
2019-07-30 12:40:05 +01:00
|
|
|
// get_bucket_info returns info about the requested bucket if authorized.
|
2019-06-15 12:23:12 +01:00
|
|
|
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 {
|
2019-07-30 12:40:05 +01:00
|
|
|
*cerr = C.CString(fmt.Sprintf("%+v", err))
|
2019-06-15 12:23:12 +01:00
|
|
|
return C.BucketInfo{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newBucketInfo(&bucket)
|
|
|
|
}
|
|
|
|
|
|
|
|
//export open_bucket
|
2019-07-30 12:40:05 +01:00
|
|
|
// open_bucket returns a Bucket handle with the given encryption context information.
|
2019-06-28 06:18:24 +01:00
|
|
|
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{}
|
|
|
|
}
|
|
|
|
|
2019-06-28 06:18:24 +01:00
|
|
|
access, err := uplink.ParseEncryptionAccess(C.GoString(encryptionAccess))
|
2019-06-27 18:36:51 +01:00
|
|
|
if err != nil {
|
2019-07-30 12:40:05 +01:00
|
|
|
*cerr = C.CString(fmt.Sprintf("%+v", err))
|
2019-06-27 18:36:51 +01:00
|
|
|
return C.BucketRef{}
|
2019-06-15 12:23:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
scope := project.scope.child()
|
|
|
|
|
2019-06-28 06:18:24 +01:00
|
|
|
bucket, err := project.OpenBucket(scope.ctx, C.GoString(name), access)
|
2019-06-15 12:23:12 +01:00
|
|
|
if err != nil {
|
2019-07-30 12:40:05 +01:00
|
|
|
*cerr = C.CString(fmt.Sprintf("%+v", err))
|
2019-06-15 12:23:12 +01:00
|
|
|
return C.BucketRef{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return C.BucketRef{universe.Add(&Bucket{scope, bucket})}
|
|
|
|
}
|
|
|
|
|
|
|
|
//export list_buckets
|
2019-07-30 12:40:05 +01:00
|
|
|
// list_buckets will list authorized buckets.
|
2019-06-15 12:23:12 +01:00
|
|
|
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),
|
2019-07-12 13:57:02 +01:00
|
|
|
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 {
|
2019-07-30 12:40:05 +01:00
|
|
|
*cerr = C.CString(fmt.Sprintf("%+v", err))
|
2019-06-15 12:23:12 +01:00
|
|
|
return C.BucketList{}
|
|
|
|
}
|
|
|
|
|
|
|
|
listLen := len(bucketList.Items)
|
|
|
|
infoSize := int(unsafe.Sizeof(C.BucketInfo{}))
|
|
|
|
|
|
|
|
itemsPtr := C.malloc(C.size_t(listLen * infoSize))
|
2019-08-26 14:12:26 +01:00
|
|
|
items := (*[1 << 30 / unsafe.Sizeof(C.BucketInfo{})]C.BucketInfo)(itemsPtr)
|
2019-06-15 12:23:12 +01:00
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-30 12:40:05 +01:00
|
|
|
//export delete_bucket
|
2019-06-15 12:23:12 +01:00
|
|
|
// delete_bucket deletes a bucket if authorized. If the bucket contains any
|
|
|
|
// Objects at the time of deletion, they may be lost permanently.
|
|
|
|
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 {
|
2019-07-30 12:40:05 +01:00
|
|
|
*cerr = C.CString(fmt.Sprintf("%+v", err))
|
2019-06-15 12:23:12 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//export close_bucket
|
2019-07-30 12:40:05 +01:00
|
|
|
// close_bucket closes a Bucket handle.
|
2019-06-15 12:23:12 +01:00
|
|
|
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 {
|
2019-07-30 12:40:05 +01:00
|
|
|
*cerr = C.CString(fmt.Sprintf("%+v", err))
|
2019-06-15 12:23:12 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//export free_bucket_info
|
2019-07-30 12:40:05 +01:00
|
|
|
// free_bucket_info frees bucket info.
|
2019-06-15 12:23:12 +01:00
|
|
|
func free_bucket_info(bucketInfo *C.BucketInfo) {
|
|
|
|
C.free(unsafe.Pointer(bucketInfo.name))
|
|
|
|
bucketInfo.name = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//export free_bucket_list
|
2019-07-30 12:40:05 +01:00
|
|
|
// free_bucket_list will free a list of buckets
|
2019-06-15 12:23:12 +01:00
|
|
|
func free_bucket_list(bucketlist *C.BucketList) {
|
2019-08-26 14:12:26 +01:00
|
|
|
items := (*[1 << 30 / unsafe.Sizeof(C.BucketInfo{})]C.BucketInfo)(unsafe.Pointer(bucketlist.items))
|
2019-06-15 12:23:12 +01:00
|
|
|
for i := 0; i < int(bucketlist.length); i++ {
|
|
|
|
free_bucket_info(&items[i])
|
|
|
|
}
|
|
|
|
C.free(unsafe.Pointer(bucketlist.items))
|
|
|
|
bucketlist.items = nil
|
|
|
|
}
|