-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbucket.go
202 lines (175 loc) · 5.07 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
196
197
198
199
200
201
202
// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package main
// #include "uplink_definitions.h"
import "C"
import (
"unsafe"
"storj.io/uplink"
)
// uplink_stat_bucket returns information about a bucket.
//
//export uplink_stat_bucket
func uplink_stat_bucket(project *C.UplinkProject, bucket_name *C.uplink_const_char) C.UplinkBucketResult { //nolint:golint
if project == nil {
return C.UplinkBucketResult{
error: mallocError(ErrNull.New("project")),
}
}
if bucket_name == nil {
return C.UplinkBucketResult{
error: mallocError(ErrNull.New("bucket_name")),
}
}
proj, ok := universe.Get(project._handle).(*Project)
if !ok {
return C.UplinkBucketResult{
error: mallocError(ErrInvalidHandle.New("project")),
}
}
bucket, err := proj.StatBucket(proj.scope.ctx, C.GoString(bucket_name))
return C.UplinkBucketResult{
error: mallocError(err),
bucket: mallocBucket(bucket),
}
}
// uplink_create_bucket creates a new bucket.
//
// When bucket already exists it returns a valid Bucket and ErrBucketExists.
//
//export uplink_create_bucket
func uplink_create_bucket(project *C.UplinkProject, bucket_name *C.uplink_const_char) C.UplinkBucketResult { //nolint:golint
if project == nil {
return C.UplinkBucketResult{
error: mallocError(ErrNull.New("project")),
}
}
if bucket_name == nil {
return C.UplinkBucketResult{
error: mallocError(ErrNull.New("bucket_name")),
}
}
proj, ok := universe.Get(project._handle).(*Project)
if !ok {
return C.UplinkBucketResult{
error: mallocError(ErrInvalidHandle.New("project")),
}
}
bucket, err := proj.CreateBucket(proj.scope.ctx, C.GoString(bucket_name))
return C.UplinkBucketResult{
error: mallocError(err),
bucket: mallocBucket(bucket),
}
}
// uplink_ensure_bucket creates a new bucket and ignores the error when it already exists.
//
// When bucket already exists it returns a valid Bucket and ErrBucketExists.
//
//export uplink_ensure_bucket
func uplink_ensure_bucket(project *C.UplinkProject, bucket_name *C.uplink_const_char) C.UplinkBucketResult { //nolint:golint
if project == nil {
return C.UplinkBucketResult{
error: mallocError(ErrNull.New("project")),
}
}
if bucket_name == nil {
return C.UplinkBucketResult{
error: mallocError(ErrNull.New("bucket_name")),
}
}
proj, ok := universe.Get(project._handle).(*Project)
if !ok {
return C.UplinkBucketResult{
error: mallocError(ErrInvalidHandle.New("project")),
}
}
bucket, err := proj.EnsureBucket(proj.scope.ctx, C.GoString(bucket_name))
return C.UplinkBucketResult{
error: mallocError(err),
bucket: mallocBucket(bucket),
}
}
// uplink_delete_bucket deletes a bucket.
//
// When bucket is not empty it returns ErrBucketNotEmpty.
//
//export uplink_delete_bucket
func uplink_delete_bucket(project *C.UplinkProject, bucket_name *C.uplink_const_char) C.UplinkBucketResult { //nolint:golint
if project == nil {
return C.UplinkBucketResult{
error: mallocError(ErrNull.New("project")),
}
}
if bucket_name == nil {
return C.UplinkBucketResult{
error: mallocError(ErrNull.New("bucket_name")),
}
}
proj, ok := universe.Get(project._handle).(*Project)
if !ok {
return C.UplinkBucketResult{
error: mallocError(ErrInvalidHandle.New("project")),
}
}
deleted, err := proj.DeleteBucket(proj.scope.ctx, C.GoString(bucket_name))
return C.UplinkBucketResult{
error: mallocError(err),
bucket: mallocBucket(deleted),
}
}
// uplink_delete_bucket_with_objects deletes a bucket and all objects within that bucket.
//
// When there are concurrent writes to the bucket it returns ErrBucketNotEmpty.
//
//export uplink_delete_bucket_with_objects
func uplink_delete_bucket_with_objects(project *C.UplinkProject, bucket_name *C.uplink_const_char) C.UplinkBucketResult { //nolint:golint
if project == nil {
return C.UplinkBucketResult{
error: mallocError(ErrNull.New("project")),
}
}
if bucket_name == nil {
return C.UplinkBucketResult{
error: mallocError(ErrNull.New("bucket_name")),
}
}
proj, ok := universe.Get(project._handle).(*Project)
if !ok {
return C.UplinkBucketResult{
error: mallocError(ErrInvalidHandle.New("project")),
}
}
deleted, err := proj.DeleteBucketWithObjects(proj.scope.ctx, C.GoString(bucket_name))
return C.UplinkBucketResult{
error: mallocError(err),
bucket: mallocBucket(deleted),
}
}
func mallocBucket(bucket *uplink.Bucket) *C.UplinkBucket {
if bucket == nil {
return nil
}
cbucket := (*C.UplinkBucket)(calloc(1, C.sizeof_UplinkBucket))
cbucket.name = C.CString(bucket.Name)
cbucket.created = timeToUnix(bucket.Created)
return cbucket
}
// uplink_free_bucket_result frees memory associated with the BucketResult.
//
//export uplink_free_bucket_result
func uplink_free_bucket_result(result C.UplinkBucketResult) {
uplink_free_error(result.error)
uplink_free_bucket(result.bucket)
}
// uplink_free_bucket frees memory associated with the bucket.
//
//export uplink_free_bucket
func uplink_free_bucket(bucket *C.UplinkBucket) {
if bucket == nil {
return
}
defer C.free(unsafe.Pointer(bucket))
if bucket.name != nil {
C.free(unsafe.Pointer(bucket.name))
}
}