-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathblock.go
161 lines (130 loc) · 3.42 KB
/
block.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
package coreapi
import (
"bytes"
"context"
"errors"
"io"
"github.com/ipfs/boxo/path"
pin "github.com/ipfs/boxo/pinning/pinner"
blocks "github.com/ipfs/go-block-format"
cid "github.com/ipfs/go-cid"
coreiface "github.com/ipfs/kubo/core/coreiface"
caopts "github.com/ipfs/kubo/core/coreiface/options"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
util "github.com/ipfs/kubo/blocks/blockstoreutil"
"github.com/ipfs/kubo/tracing"
)
type BlockAPI CoreAPI
type BlockStat struct {
path path.ImmutablePath
size int
}
func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.BlockPutOption) (coreiface.BlockStat, error) {
ctx, span := tracing.Span(ctx, "CoreAPI.BlockAPI", "Put")
defer span.End()
settings, err := caopts.BlockPutOptions(opts...)
if err != nil {
return nil, err
}
data, err := io.ReadAll(src)
if err != nil {
return nil, err
}
bcid, err := settings.CidPrefix.Sum(data)
if err != nil {
return nil, err
}
b, err := blocks.NewBlockWithCid(data, bcid)
if err != nil {
return nil, err
}
if settings.Pin {
defer api.blockstore.PinLock(ctx).Unlock(ctx)
}
err = api.blocks.AddBlock(ctx, b)
if err != nil {
return nil, err
}
if settings.Pin {
if err = api.pinning.PinWithMode(ctx, b.Cid(), pin.Recursive, ""); err != nil {
return nil, err
}
if err := api.pinning.Flush(ctx); err != nil {
return nil, err
}
}
return &BlockStat{path: path.FromCid(b.Cid()), size: len(data)}, nil
}
func (api *BlockAPI) Get(ctx context.Context, p path.Path) (io.Reader, error) {
ctx, span := tracing.Span(ctx, "CoreAPI.BlockAPI", "Get", trace.WithAttributes(attribute.String("path", p.String())))
defer span.End()
rp, _, err := api.core().ResolvePath(ctx, p)
if err != nil {
return nil, err
}
b, err := api.blocks.GetBlock(ctx, rp.RootCid())
if err != nil {
return nil, err
}
return bytes.NewReader(b.RawData()), nil
}
func (api *BlockAPI) Rm(ctx context.Context, p path.Path, opts ...caopts.BlockRmOption) error {
ctx, span := tracing.Span(ctx, "CoreAPI.BlockAPI", "Rm", trace.WithAttributes(attribute.String("path", p.String())))
defer span.End()
rp, _, err := api.core().ResolvePath(ctx, p)
if err != nil {
return err
}
settings, err := caopts.BlockRmOptions(opts...)
if err != nil {
return err
}
cids := []cid.Cid{rp.RootCid()}
o := util.RmBlocksOpts{Force: settings.Force}
out, err := util.RmBlocks(ctx, api.blockstore, api.pinning, cids, o)
if err != nil {
return err
}
select {
case res, ok := <-out:
if !ok {
return nil
}
remBlock, ok := res.(*util.RemovedBlock)
if !ok {
return errors.New("got unexpected output from util.RmBlocks")
}
if remBlock.Error != nil {
return remBlock.Error
}
return nil
case <-ctx.Done():
return ctx.Err()
}
}
func (api *BlockAPI) Stat(ctx context.Context, p path.Path) (coreiface.BlockStat, error) {
ctx, span := tracing.Span(ctx, "CoreAPI.BlockAPI", "Stat", trace.WithAttributes(attribute.String("path", p.String())))
defer span.End()
rp, _, err := api.core().ResolvePath(ctx, p)
if err != nil {
return nil, err
}
b, err := api.blocks.GetBlock(ctx, rp.RootCid())
if err != nil {
return nil, err
}
return &BlockStat{
path: path.FromCid(b.Cid()),
size: len(b.RawData()),
}, nil
}
func (bs *BlockStat) Size() int {
return bs.size
}
func (bs *BlockStat) Path() path.ImmutablePath {
return bs.path
}
func (api *BlockAPI) core() coreiface.CoreAPI {
return (*CoreAPI)(api)
}