/
retriever.go
233 lines (193 loc) · 5.58 KB
/
retriever.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
package app
import (
"context"
"fmt"
"io"
"os"
"path"
"github.com/filecoin-project/lassie/pkg/lassie"
"github.com/filecoin-project/lassie/pkg/storage"
"github.com/filecoin-project/lassie/pkg/types"
"github.com/ipfs/go-cid"
"github.com/ipld/go-car/v2"
carstorage "github.com/ipld/go-car/v2/storage"
"github.com/ipld/go-car/v2/storage/deferred"
trustlessutils "github.com/ipld/go-trustless-utils"
)
type retriever interface {
retrieveStdout(context.Context, cid.Cid, int64) error
retrieveFile(context.Context, cid.Cid, string, int64) error
}
// Retriever is responsible for retrieving file from the network.
type Retriever struct {
store retriever
timeout int64
}
// NewRetriever creates a new Retriever.
func NewRetriever(provider VaultsProvider, timeout int64) *Retriever {
return &Retriever{
store: &coldStore{
retriever: &cacheStore{
provider: provider,
},
},
timeout: timeout,
}
}
// Retrieve retrieves file from the network.
func (r *Retriever) Retrieve(ctx context.Context, c cid.Cid, output string) error {
if output == "-" || output == "" {
return r.store.retrieveStdout(ctx, c, r.timeout)
}
return r.store.retrieveFile(ctx, c, output, r.timeout)
}
type cacheStore struct {
provider VaultsProvider
}
func (cs *cacheStore) retrieveStdout(ctx context.Context, cid cid.Cid, timeout int64) error {
if _, err := cs.provider.RetrieveEvent(ctx, RetrieveEventParams{
Timeout: timeout,
CID: cid,
}, os.Stdout); err != nil {
return fmt.Errorf("failed to retrieve to file: %s", err)
}
return nil
}
func (cs *cacheStore) retrieveFile(ctx context.Context, cid cid.Cid, output string, timeout int64) error {
f, err := os.OpenFile(output, os.O_RDWR|os.O_CREATE, 0o666)
if err != nil {
return fmt.Errorf("failed to open tmp file: %s", err)
}
defer func() {
_ = f.Close()
}()
_, _ = f.Seek(0, io.SeekStart)
_, err = cs.provider.RetrieveEvent(ctx, RetrieveEventParams{
Timeout: timeout,
CID: cid,
}, f)
if err != nil {
return fmt.Errorf("failed to retrieve to file: %s", err)
}
return nil
}
type coldStore struct {
retriever retriever
}
func (cs *coldStore) retrieveFile(ctx context.Context, c cid.Cid, output string, timeout int64) error {
// try cache first. no matter the error try cold store
err := cs.retriever.retrieveFile(ctx, c, output, timeout)
if err == nil {
return nil
}
lassie, err := lassie.NewLassie(ctx)
if err != nil {
return fmt.Errorf("failed to create lassie instance: %s", err)
}
carOpts := []car.Option{
car.WriteAsCarV1(true),
car.StoreIdentityCIDs(false),
car.UseWholeCIDs(false),
}
carPath := path.Join(".", fmt.Sprintf("%s.car", c.String()))
carWriter := deferred.NewDeferredCarWriterForPath(carPath, []cid.Cid{c}, carOpts...)
carStore := storage.NewCachingTempStore(
carWriter.BlockWriteOpener(), storage.NewDeferredStorageCar(os.TempDir(), c),
)
defer func() {
_ = carStore.Close()
}()
request, err := types.NewRequestForPath(carStore, c, "", trustlessutils.DagScopeAll, nil)
if err != nil {
return fmt.Errorf("failed to create request: %s", err)
}
if _, err := lassie.Fetch(ctx, request, []types.FetchOption{}...); err != nil {
return fmt.Errorf("failed to fetch: %s", err)
}
carFile, err := os.Open(carPath)
if err != nil {
return fmt.Errorf("opening car file: %s", err)
}
defer func() {
_ = os.Remove(carFile.Name())
_ = carFile.Close()
}()
rc, err := extract(carFile)
if err != nil {
return fmt.Errorf("extract: %s", err)
}
f, err := os.OpenFile(output, os.O_RDWR|os.O_CREATE, 0o666)
if err != nil {
return fmt.Errorf("failed to open tmp file: %s", err)
}
defer func() {
_ = f.Close()
}()
if _, err := io.Copy(f, rc); err != nil {
return fmt.Errorf("failed to write to stdout: %s", err)
}
return nil
}
func (cs *coldStore) retrieveStdout(ctx context.Context, c cid.Cid, timeout int64) error {
// try cache first. no matter the error try cold store
err := cs.retriever.retrieveStdout(ctx, c, timeout)
if err == nil {
return nil
}
lassie, err := lassie.NewLassie(ctx)
if err != nil {
return fmt.Errorf("failed to create lassie instance: %s", err)
}
carOpts := []car.Option{
car.WriteAsCarV1(true),
car.StoreIdentityCIDs(false),
car.UseWholeCIDs(false),
}
// Create a temporary file only for writing to stdout case
tmpFile, err := os.CreateTemp("", fmt.Sprintf("%s.car", c.String()))
if err != nil {
return fmt.Errorf("failed to create temporary file: %s", err)
}
defer func() {
_ = os.Remove(tmpFile.Name())
}()
carWriter := deferred.NewDeferredCarWriterForPath(tmpFile.Name(), []cid.Cid{c}, carOpts...)
carStore := storage.NewCachingTempStore(
carWriter.BlockWriteOpener(), storage.NewDeferredStorageCar(os.TempDir(), c),
)
defer func() {
_ = carStore.Close()
}()
request, err := types.NewRequestForPath(carStore, c, "", trustlessutils.DagScopeAll, nil)
if err != nil {
return fmt.Errorf("failed to create request: %s", err)
}
if _, err := lassie.Fetch(ctx, request, []types.FetchOption{}...); err != nil {
return fmt.Errorf("failed to fetch: %s", err)
}
_, _ = tmpFile.Seek(0, io.SeekStart)
rc, err := extract(tmpFile)
if err != nil {
return fmt.Errorf("extract: %s", err)
}
_, err = io.Copy(os.Stdout, rc)
if err != nil {
return fmt.Errorf("failed to write to stdout: %s", err)
}
return nil
}
func extract(f *os.File) (io.ReadCloser, error) {
store, err := carstorage.OpenReadable(f)
if err != nil {
return nil, err
}
blkCid, err := cid.Parse(store.Roots()[0].String())
if err != nil {
return nil, err
}
rc, err := store.GetStream(context.Background(), blkCid.KeyString())
if err != nil {
return nil, err
}
return rc, nil
}