-
Notifications
You must be signed in to change notification settings - Fork 402
/
bucket.go
195 lines (165 loc) · 5.56 KB
/
bucket.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package main
// #include "uplink_definitions.h"
import "C"
import (
"fmt"
"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
// create_bucket creates a new bucket if authorized.
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(fmt.Sprintf("%+v", err))
return C.BucketInfo{}
}
return newBucketInfo(&bucket)
}
//export get_bucket_info
// get_bucket_info returns info about the requested bucket if authorized.
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(fmt.Sprintf("%+v", err))
return C.BucketInfo{}
}
return newBucketInfo(&bucket)
}
//export open_bucket
// open_bucket returns a Bucket handle with the given encryption context information.
func open_bucket(projectHandle C.ProjectRef, name *C.char, encryptionAccess *C.char, cerr **C.char) C.BucketRef {
project, ok := universe.Get(projectHandle._handle).(*Project)
if !ok {
*cerr = C.CString("invalid project")
return C.BucketRef{}
}
access, err := uplink.ParseEncryptionAccess(C.GoString(encryptionAccess))
if err != nil {
*cerr = C.CString(fmt.Sprintf("%+v", err))
return C.BucketRef{}
}
scope := project.scope.child()
bucket, err := project.OpenBucket(scope.ctx, C.GoString(name), access)
if err != nil {
*cerr = C.CString(fmt.Sprintf("%+v", err))
return C.BucketRef{}
}
return C.BucketRef{universe.Add(&Bucket{scope, bucket})}
}
//export list_buckets
// list_buckets will list authorized 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,
Limit: int(bucketListOptions.limit),
}
}
bucketList, err := project.ListBuckets(project.scope.ctx, opts)
if err != nil {
*cerr = C.CString(fmt.Sprintf("%+v", err))
return C.BucketList{}
}
listLen := len(bucketList.Items)
infoSize := int(unsafe.Sizeof(C.BucketInfo{}))
itemsPtr := C.malloc(C.size_t(listLen * infoSize))
items := (*[1 << 30 / unsafe.Sizeof(C.BucketInfo{})]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),
}
}
//export delete_bucket
// 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 {
*cerr = C.CString(fmt.Sprintf("%+v", err))
return
}
}
//export close_bucket
// close_bucket closes a Bucket handle.
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(fmt.Sprintf("%+v", err))
return
}
}
//export free_bucket_info
// free_bucket_info frees bucket info.
func free_bucket_info(bucketInfo *C.BucketInfo) {
C.free(unsafe.Pointer(bucketInfo.name))
bucketInfo.name = nil
}
//export free_bucket_list
// free_bucket_list will free a list of buckets
func free_bucket_list(bucketlist *C.BucketList) {
items := (*[1 << 30 / unsafe.Sizeof(C.BucketInfo{})]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
}