-
Notifications
You must be signed in to change notification settings - Fork 17
/
multipart_iterators.go
372 lines (312 loc) · 9.37 KB
/
multipart_iterators.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// Copyright (C) 2021 Storj Labs, Inc.
// See LICENSE for copying information.
package uplink
import (
"context"
"sort"
"strings"
"github.com/zeebo/errs"
"storj.io/common/base58"
"storj.io/common/encryption"
"storj.io/common/storj"
"storj.io/uplink/private/metaclient"
)
// ListUploadsOptions options for listing uncommitted uploads.
type ListUploadsOptions struct {
// Prefix allows to filter uncommitted uploads by a key prefix. If not empty, it must end with slash.
Prefix string
// Cursor sets the starting position of the iterator.
// The first item listed will be the one after the cursor.
// Cursor is relative to Prefix.
Cursor string
// Recursive iterates the objects without collapsing prefixes.
Recursive bool
// System includes SystemMetadata in the results.
System bool
// Custom includes CustomMetadata in the results.
Custom bool
}
// UploadIterator is an iterator over a collection of uncommitted uploads.
type UploadIterator struct {
ctx context.Context
project *Project
bucket string
options metaclient.ListOptions
uploadOptions ListUploadsOptions
list *metaclient.ObjectList
position int
completed bool
err error
listObjects func(tx context.Context, db *metaclient.DB, bucket string, options metaclient.ListOptions) (metaclient.ObjectList, error)
}
func listObjects(ctx context.Context, db *metaclient.DB, bucket string, options metaclient.ListOptions) (metaclient.ObjectList, error) {
return db.ListObjects(ctx, bucket, options)
}
func listPendingObjectStreams(ctx context.Context, db *metaclient.DB, bucket string, options metaclient.ListOptions) (metaclient.ObjectList, error) {
return db.ListPendingObjectStreams(ctx, bucket, options)
}
// Next prepares next entry for reading.
// It returns false if the end of the iteration is reached and there are no more uploads, or if there is an error.
func (uploads *UploadIterator) Next() bool {
if uploads.err != nil {
uploads.completed = true
return false
}
if uploads.list == nil {
more := uploads.loadNext()
uploads.completed = !more
return more
}
if uploads.position >= len(uploads.list.Items)-1 {
if !uploads.list.More {
uploads.completed = true
return false
}
more := uploads.loadNext()
uploads.completed = !more
return more
}
uploads.position++
return true
}
func (uploads *UploadIterator) loadNext() bool {
ok, err := uploads.tryLoadNext()
if err != nil {
uploads.err = err
return false
}
return ok
}
func (uploads *UploadIterator) tryLoadNext() (ok bool, err error) {
db, err := dialMetainfoDB(uploads.ctx, uploads.project)
if err != nil {
return false, convertKnownErrors(err, uploads.bucket, "")
}
defer func() { err = errs.Combine(err, db.Close()) }()
list, err := uploads.listObjects(uploads.ctx, db, uploads.bucket, uploads.options)
if err != nil {
return false, convertKnownErrors(err, uploads.bucket, "")
}
uploads.list = &list
if list.More {
uploads.options = uploads.options.NextPage(list)
}
uploads.position = 0
return len(list.Items) > 0, nil
}
// Err returns error, if one happened during iteration.
func (uploads *UploadIterator) Err() error {
return packageError.Wrap(uploads.err)
}
// Item returns the current entry in the iterator.
func (uploads *UploadIterator) Item() *UploadInfo {
item := uploads.item()
if item == nil {
return nil
}
key := item.Path
if len(uploads.options.Prefix) > 0 && strings.HasSuffix(uploads.options.Prefix, "/") {
key = uploads.options.Prefix + item.Path
}
obj := UploadInfo{
Key: key,
IsPrefix: item.IsPrefix,
UploadID: base58.CheckEncode(item.Stream.ID, 1),
}
// TODO: Make this filtering on the satellite
if uploads.uploadOptions.System {
obj.System = SystemMetadata{
Created: item.Created,
Expires: item.Expires,
ContentLength: item.Size,
}
}
// TODO: Make this filtering on the satellite
if uploads.uploadOptions.Custom {
obj.Custom = item.Metadata
}
return &obj
}
func (uploads *UploadIterator) item() *metaclient.Object {
if uploads.completed {
return nil
}
if uploads.err != nil {
return nil
}
if uploads.list == nil {
return nil
}
if len(uploads.list.Items) == 0 {
return nil
}
return &uploads.list.Items[uploads.position]
}
// ListUploadPartsOptions options for listing upload parts.
type ListUploadPartsOptions struct {
// Cursor sets the starting position of the iterator.
// The first item listed will be the one after the cursor.
Cursor uint32
}
// PartIterator is an iterator over a collection of parts of an upload.
type PartIterator struct {
ctx context.Context
project *Project
bucket string
key string
uploadID string
options metaclient.ListSegmentsParams
items []*Part
more bool
position int
lastPart *Part
completed bool
err error
}
// Next prepares next entry for reading.
func (parts *PartIterator) Next() bool {
if parts.err != nil {
parts.completed = true
return false
}
if len(parts.items) == 0 {
more := parts.loadNext()
parts.completed = !more
return more
}
if parts.position >= len(parts.items)-1 {
if !parts.more {
parts.completed = true
return false
}
more := parts.loadNext()
parts.completed = !more
return more
}
parts.position++
return true
}
func (parts *PartIterator) loadNext() bool {
ok, err := parts.tryLoadNext()
if err != nil {
parts.err = err
return false
}
return ok
}
func (parts *PartIterator) tryLoadNext() (ok bool, err error) {
metainfoClient, err := parts.project.dialMetainfoClient(parts.ctx)
if err != nil {
return false, convertKnownErrors(err, parts.bucket, parts.key)
}
defer func() { err = errs.Combine(err, metainfoClient.Close()) }()
partsMap := make(map[uint32]*Part)
// put into map last part from previous listing
if parts.lastPart != nil {
partsMap[parts.lastPart.PartNumber] = parts.lastPart
}
parts.position = 0
parts.items = parts.items[:0]
for {
list, err := metainfoClient.ListSegments(parts.ctx, parts.options)
if err != nil {
return false, convertKnownErrors(err, parts.bucket, parts.key)
}
for _, item := range list.Items {
var etag []byte
if item.EncryptedETag != nil {
// ETag will be only with last segment in a part
etag, err = decryptETag(parts.project, parts.bucket, parts.key, list.EncryptionParameters, item)
if err != nil {
return false, convertKnownErrors(err, parts.bucket, parts.key)
}
}
partNumber := uint32(item.Position.PartNumber)
_, exists := partsMap[partNumber]
if !exists {
partsMap[partNumber] = &Part{
PartNumber: partNumber,
Size: item.PlainSize,
Modified: item.CreatedAt,
ETag: etag,
}
} else {
partsMap[partNumber].Size += item.PlainSize
if item.CreatedAt.After(partsMap[partNumber].Modified) {
partsMap[partNumber].Modified = item.CreatedAt
}
// The satellite returns the segments ordered by position. So it is
// OK to just overwrite the ETag with the one from the next segment.
// Eventually, the map will contain the ETag of the last segment,
// which is the part's ETag.
partsMap[partNumber].ETag = etag
}
}
if list.More && len(list.Items) > 0 {
item := list.Items[len(list.Items)-1]
parts.options.Cursor = item.Position
}
parts.more = list.More
// stop this loop when there is no next page or we have more than single
// result in map. Single result means that we still cannot present result
// as more segment from this part can be still not listed.
if !parts.more || len(partsMap) == 0 || len(partsMap) > 1 {
break
}
}
for _, part := range partsMap {
parts.items = append(parts.items, part)
}
sort.Slice(parts.items, func(i, k int) bool {
return parts.items[i].PartNumber < parts.items[k].PartNumber
})
if parts.more && len(parts.items) > 0 {
// remove last part from results as it may be incomplete and rest
// of segments will be in next chunk of results, removed part
// will be added to next chunk and updated if needed
parts.lastPart = parts.items[len(parts.items)-1]
parts.items = parts.items[:len(parts.items)-1]
}
return len(parts.items) > 0, nil
}
// Item returns the current entry in the iterator.
func (parts *PartIterator) Item() *Part {
if parts.completed {
return nil
}
if parts.err != nil {
return nil
}
if len(parts.items) == 0 {
return nil
}
return parts.items[parts.position]
}
// Err returns error, if one happened during iteration.
func (parts *PartIterator) Err() error {
return packageError.Wrap(parts.err)
}
func decryptETag(project *Project, bucket, key string, encryptionParameters storj.EncryptionParameters, segment metaclient.SegmentListItem) ([]byte, error) {
if segment.EncryptedETag == nil {
return nil, nil
}
derivedKey, err := deriveContentKey(project, bucket, key)
if err != nil {
return nil, err
}
contentKey, err := encryption.DecryptKey(segment.EncryptedKey, encryptionParameters.CipherSuite, derivedKey, &segment.EncryptedKeyNonce)
if err != nil {
return nil, err
}
// Derive another key from the randomly generated content key to decrypt
// the segment's ETag.
etagKey, err := deriveETagKey(contentKey)
if err != nil {
return nil, err
}
return encryption.Decrypt(segment.EncryptedETag, encryptionParameters.CipherSuite, etagKey, &storj.Nonce{})
}
// TODO move it to be accesible here and from streams/store.go.
func deriveETagKey(key *storj.Key) (*storj.Key, error) {
return encryption.DeriveKey(key, "storj-etag-v1")
}