2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-11-06 11:40:06 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package kvmetainfo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/encryption"
|
2019-11-24 18:26:18 +00:00
|
|
|
"storj.io/storj/pkg/paths"
|
2018-11-06 11:40:06 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2019-10-28 16:23:20 +00:00
|
|
|
"storj.io/storj/uplink/metainfo"
|
2018-11-06 11:40:06 +00:00
|
|
|
)
|
|
|
|
|
2019-12-18 13:35:28 +00:00
|
|
|
var _ ReadOnlyStream = (*readonlyStream)(nil)
|
2018-11-06 11:40:06 +00:00
|
|
|
|
|
|
|
type readonlyStream struct {
|
2018-11-14 09:26:18 +00:00
|
|
|
db *DB
|
2018-11-06 11:40:06 +00:00
|
|
|
|
2019-11-24 18:26:18 +00:00
|
|
|
info storj.Object
|
2018-11-06 11:40:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (stream *readonlyStream) Info() storj.Object { return stream.info }
|
|
|
|
|
|
|
|
func (stream *readonlyStream) SegmentsAt(ctx context.Context, byteOffset int64, limit int64) (infos []storj.Segment, more bool, err error) {
|
2018-11-16 13:59:27 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2018-11-06 11:40:06 +00:00
|
|
|
if stream.info.FixedSegmentSize <= 0 {
|
|
|
|
return nil, false, errors.New("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
index := byteOffset / stream.info.FixedSegmentSize
|
|
|
|
return stream.Segments(ctx, index, limit)
|
|
|
|
}
|
|
|
|
|
2018-11-19 11:08:28 +00:00
|
|
|
func (stream *readonlyStream) segment(ctx context.Context, index int64) (segment storj.Segment, err error) {
|
2018-11-16 13:59:27 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2018-11-19 11:08:28 +00:00
|
|
|
segment = storj.Segment{
|
2018-11-06 11:40:06 +00:00
|
|
|
Index: index,
|
|
|
|
}
|
|
|
|
|
|
|
|
isLastSegment := segment.Index+1 == stream.info.SegmentCount
|
2019-11-24 18:26:18 +00:00
|
|
|
if isLastSegment {
|
|
|
|
index = -1
|
2018-11-06 11:40:06 +00:00
|
|
|
}
|
2019-11-24 18:26:18 +00:00
|
|
|
info, limits, err := stream.db.metainfo.DownloadSegment(ctx, metainfo.DownloadSegmentParams{
|
|
|
|
StreamID: stream.Info().ID,
|
|
|
|
Position: storj.SegmentPosition{
|
|
|
|
Index: int32(index),
|
|
|
|
},
|
|
|
|
})
|
2018-11-06 11:40:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return segment, err
|
|
|
|
}
|
|
|
|
|
2019-11-24 18:26:18 +00:00
|
|
|
segment.Size = stream.info.Size
|
|
|
|
segment.EncryptedKeyNonce = info.SegmentEncryption.EncryptedKeyNonce
|
|
|
|
segment.EncryptedKey = info.SegmentEncryption.EncryptedKey
|
|
|
|
|
|
|
|
streamKey, err := encryption.DeriveContentKey(stream.info.Bucket.Name, paths.NewUnencrypted(stream.info.Path), stream.db.encStore)
|
2018-11-19 11:08:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return segment, err
|
|
|
|
}
|
|
|
|
|
2019-11-24 18:26:18 +00:00
|
|
|
contentKey, err := encryption.DecryptKey(segment.EncryptedKey, stream.info.EncryptionParameters.CipherSuite, streamKey, &segment.EncryptedKeyNonce)
|
|
|
|
if err != nil {
|
|
|
|
return segment, err
|
2019-03-22 09:01:49 +00:00
|
|
|
}
|
|
|
|
|
2019-11-24 18:26:18 +00:00
|
|
|
nonce := new(storj.Nonce)
|
|
|
|
_, err = encryption.Increment(nonce, segment.Index+1)
|
2018-11-19 11:08:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return segment, err
|
|
|
|
}
|
|
|
|
|
2019-11-24 18:26:18 +00:00
|
|
|
if len(info.EncryptedInlineData) != 0 || len(limits) == 0 {
|
|
|
|
inline, err := encryption.Decrypt(info.EncryptedInlineData, stream.info.EncryptionParameters.CipherSuite, contentKey, nonce)
|
|
|
|
if err != nil {
|
|
|
|
return segment, err
|
2018-11-19 11:08:28 +00:00
|
|
|
}
|
2019-11-24 18:26:18 +00:00
|
|
|
segment.Inline = inline
|
2018-11-19 11:08:28 +00:00
|
|
|
}
|
|
|
|
|
2018-11-06 11:40:06 +00:00
|
|
|
return segment, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (stream *readonlyStream) Segments(ctx context.Context, index int64, limit int64) (infos []storj.Segment, more bool, err error) {
|
2018-11-16 13:59:27 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2018-11-06 11:40:06 +00:00
|
|
|
if index < 0 {
|
|
|
|
return nil, false, errors.New("invalid argument")
|
|
|
|
}
|
|
|
|
if limit <= 0 {
|
|
|
|
limit = defaultSegmentLimit
|
|
|
|
}
|
|
|
|
if index >= stream.info.SegmentCount {
|
|
|
|
return nil, false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
infos = make([]storj.Segment, 0, limit)
|
|
|
|
for ; index < stream.info.SegmentCount && limit > 0; index++ {
|
|
|
|
limit--
|
|
|
|
segment, err := stream.segment(ctx, index)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
infos = append(infos, segment)
|
|
|
|
}
|
|
|
|
|
2018-11-15 15:31:33 +00:00
|
|
|
more = index < stream.info.SegmentCount
|
2018-11-06 11:40:06 +00:00
|
|
|
return infos, more, nil
|
|
|
|
}
|
2018-11-30 13:50:52 +00:00
|
|
|
|
|
|
|
type mutableStream struct {
|
|
|
|
db *DB
|
|
|
|
info storj.Object
|
|
|
|
}
|
|
|
|
|
|
|
|
func (stream *mutableStream) Info() storj.Object { return stream.info }
|
|
|
|
|
2019-06-04 12:36:27 +01:00
|
|
|
func (stream *mutableStream) AddSegments(ctx context.Context, segments ...storj.Segment) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-11-30 13:50:52 +00:00
|
|
|
return errors.New("not implemented")
|
|
|
|
}
|
|
|
|
|
2019-06-04 12:36:27 +01:00
|
|
|
func (stream *mutableStream) UpdateSegments(ctx context.Context, segments ...storj.Segment) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-11-30 13:50:52 +00:00
|
|
|
return errors.New("not implemented")
|
|
|
|
}
|