-
Notifications
You must be signed in to change notification settings - Fork 7
/
objects.go
134 lines (113 loc) · 3.36 KB
/
objects.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
// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package main
// #include "uplink_definitions.h"
import "C"
import (
"unsafe"
"storj.io/uplink"
)
// ObjectIterator is an iterator over objects.
type ObjectIterator struct {
scope
iterator *uplink.ObjectIterator
initialError error
}
// uplink_list_objects lists objects.
//
//export uplink_list_objects
func uplink_list_objects(project *C.UplinkProject, bucket_name *C.uplink_const_char, options *C.UplinkListObjectsOptions) *C.UplinkObjectIterator { //nolint:golint
if project == nil {
return (*C.UplinkObjectIterator)(mallocHandle(universe.Add(&ObjectIterator{
initialError: ErrNull.New("project"),
})))
}
if bucket_name == nil {
return (*C.UplinkObjectIterator)(mallocHandle(universe.Add(&ObjectIterator{
initialError: ErrNull.New("bucket_name"),
})))
}
proj, ok := universe.Get(project._handle).(*Project)
if !ok {
return (*C.UplinkObjectIterator)(mallocHandle(universe.Add(&ObjectIterator{
initialError: ErrInvalidHandle.New("project"),
})))
}
opts := &uplink.ListObjectsOptions{}
if options != nil {
opts.Prefix = C.GoString(options.prefix)
opts.Cursor = C.GoString(options.cursor)
opts.Recursive = bool(options.recursive)
opts.System = bool(options.system)
opts.Custom = bool(options.custom)
}
scope := proj.scope.child()
iterator := proj.ListObjects(scope.ctx, C.GoString(bucket_name), opts)
return (*C.UplinkObjectIterator)(mallocHandle(universe.Add(&ObjectIterator{
scope: scope,
iterator: iterator,
})))
}
// uplink_object_iterator_next prepares next Object for reading.
//
// It returns false if the end of the iteration is reached and there are no more objects, or if there is an error.
//
//export uplink_object_iterator_next
func uplink_object_iterator_next(iterator *C.UplinkObjectIterator) C.bool {
if iterator == nil {
return C.bool(false)
}
iter, ok := universe.Get(iterator._handle).(*ObjectIterator)
if !ok {
return C.bool(false)
}
if iter.initialError != nil {
return C.bool(false)
}
return C.bool(iter.iterator.Next())
}
// uplink_object_iterator_err returns error, if one happened during iteration.
//
//export uplink_object_iterator_err
func uplink_object_iterator_err(iterator *C.UplinkObjectIterator) *C.UplinkError {
if iterator == nil {
return mallocError(ErrNull.New("iterator"))
}
iter, ok := universe.Get(iterator._handle).(*ObjectIterator)
if !ok {
return mallocError(ErrInvalidHandle.New("iterator"))
}
if iter.initialError != nil {
return mallocError(iter.initialError)
}
return mallocError(iter.iterator.Err())
}
// uplink_object_iterator_item returns the current object in the iterator.
//
//export uplink_object_iterator_item
func uplink_object_iterator_item(iterator *C.UplinkObjectIterator) *C.UplinkObject {
if iterator == nil {
return nil
}
iter, ok := universe.Get(iterator._handle).(*ObjectIterator)
if !ok {
return nil
}
return mallocObject(iter.iterator.Item())
}
// uplink_free_object_iterator frees memory associated with the ObjectIterator.
//
//export uplink_free_object_iterator
func uplink_free_object_iterator(iterator *C.UplinkObjectIterator) {
if iterator == nil {
return
}
defer C.free(unsafe.Pointer(iterator))
defer universe.Del(iterator._handle)
iter, ok := universe.Get(iterator._handle).(*ObjectIterator)
if ok {
if iter.scope.cancel != nil {
iter.scope.cancel()
}
}
}