/
bucket.go
319 lines (294 loc) · 7.97 KB
/
bucket.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
package local
import (
"context"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/ipfs/go-cid"
"github.com/ipfs/interface-go-ipfs-core/options"
"github.com/textileio/go-buckets"
"github.com/textileio/go-buckets/api/cast"
"github.com/textileio/go-buckets/api/client"
"github.com/textileio/go-buckets/cmd"
"github.com/textileio/go-buckets/collection"
"github.com/textileio/go-buckets/dag"
"github.com/textileio/go-threads/core/did"
"github.com/textileio/go-threads/core/thread"
)
var (
// ErrUpToDate indicates there are no locally staged changes.
ErrUpToDate = errors.New("everything up-to-date")
// ErrAborted indicates the caller aborted the operation via a confirm function.
ErrAborted = errors.New("operation aborted by caller")
)
// Event describes a path event that occurred.
// These events can be used to display live info during path uploads/downloads.
type Event struct {
// Type of event.
Type EventType
// Path relative to the bucket's cwd.
Path string
// Cid of associated Path.
Cid cid.Cid
// Size of the total operation or completed file.
Size int64
// Complete is the amount of Size that is complete (useful for upload/download progress).
Complete int64
}
// EventType is the type of path event.
type EventType int
const (
// EventProgress indicates a file has made some progress uploading/downloading.
EventProgress EventType = iota
// EventFileComplete indicates a file has completed uploading/downloading.
EventFileComplete
// EventFileRemoved indicates a file has been removed.
EventFileRemoved
)
// Bucket is a local-first object storage and synchronization model built
// on ThreadDB, IPFS, and Filecoin.
// A bucket represents a dynamic Unixfs directory with auto-updating
// IPNS resolution, website rendering, and Filecoin archivng.
//
// Private buckets are fully encrypted using AES-CTR + AES-512 HMAC (see https://github.com/textileio/dcrypto for more).
// Both Unixfs node metadata (size, links, etc.) and node data (files) are obfuscated by encryption.
// The AES and HMAC keys used for bucket encryption are stored in the ThreadDB collection instance.
// This setup allows for bucket access to inherit from thread ACL rules.
//
// Additionally, files can be further protected by password-based encryption before they are added to the bucket.
// See EncryptLocalPath and DecryptLocalPath for more.
type Bucket struct {
c *client.Client
cwd string
conf *cmd.Config
repo *Repo
links *buckets.Links
pushBlock chan struct{}
sync.Mutex
}
// Thread returns the bucket's thread ID.
func (b *Bucket) Thread() (id thread.ID, err error) {
ids := b.conf.Viper.GetString("thread")
if ids == "" {
return thread.Undef, nil
}
return thread.Decode(ids)
}
// Key returns the bucket's unique key identifier, which is also an IPNS public key.
func (b *Bucket) Key() string {
return b.conf.Viper.GetString("key")
}
// Identity returns the identity associated with the bucket.
func (b *Bucket) Identity() (thread.Identity, error) {
identity := &thread.Libp2pIdentity{}
err := identity.UnmarshalString(b.conf.Viper.GetString("identity"))
return identity, err
}
// Path returns the bucket's top-level local filesystem path.
func (b *Bucket) Path() (string, error) {
conf := b.conf.Viper.ConfigFileUsed()
if conf == "" {
return b.cwd, nil
}
return filepath.Dir(filepath.Dir(conf)), nil
}
// LocalSize returns the cumalative size of the bucket's local files.
func (b *Bucket) LocalSize() (int64, error) {
if b.repo == nil {
return 0, nil
}
bp, err := b.Path()
if err != nil {
return 0, err
}
var size int64
err = filepath.Walk(bp, func(n string, info os.FileInfo, err error) error {
if err != nil {
return fmt.Errorf("getting fileinfo of %s: %s", n, err)
}
if !info.IsDir() {
f := strings.TrimPrefix(n, bp+string(os.PathSeparator))
if Ignore(n) || (strings.HasPrefix(f, b.conf.Dir) && f != collection.SeedName) {
return nil
}
size += info.Size()
}
return err
})
return size, err
}
// Get returns info about a bucket from the remote.
func (b *Bucket) Get(ctx context.Context) (bucket buckets.Bucket, err error) {
ctx, err = b.authCtx(ctx, time.Hour)
if err != nil {
return
}
id, err := b.Thread()
if err != nil {
return
}
rep, err := b.c.Get(ctx, id, b.Key())
if err != nil {
return
}
return cast.BucketFromPb(rep.Bucket)
}
// Roots wraps local and remote root cids.
// If the bucket is not private (encrypted), these will be the same.
type Roots struct {
Local cid.Cid `json:"local"`
Remote cid.Cid `json:"remote"`
}
// Roots returns the bucket's current local and remote root cids.
func (b *Bucket) Roots(ctx context.Context) (roots Roots, err error) {
var lc, rc cid.Cid
if b.repo != nil {
lc, rc, err = b.repo.Root()
if err != nil {
return
}
}
if !rc.Defined() {
rc, err = b.getRemoteRoot(ctx)
if err != nil {
return
}
}
return Roots{Local: lc, Remote: rc}, nil
}
// RemoteLinks returns the remote links for the bucket.
func (b *Bucket) RemoteLinks(ctx context.Context, pth string) (links buckets.Links, err error) {
if b.links != nil {
return *b.links, nil
}
ctx, err = b.authCtx(ctx, time.Hour)
if err != nil {
return
}
id, err := b.Thread()
if err != nil {
return
}
res, err := b.c.GetLinks(ctx, id, b.Key(), pth)
if err != nil {
return
}
links = cast.LinksFromPb(res.Links)
b.links = &links
return links, err
}
// CatRemotePath writes the content of the remote path to writer.
func (b *Bucket) CatRemotePath(ctx context.Context, pth string, w io.Writer) error {
ctx, err := b.authCtx(ctx, time.Hour)
if err != nil {
return err
}
id, err := b.Thread()
if err != nil {
return err
}
return b.c.PullPath(ctx, id, b.Key(), pth, w)
}
// Destroy completely deletes the local and remote bucket.
func (b *Bucket) Destroy(ctx context.Context) (err error) {
b.Lock()
defer b.Unlock()
ctx, err = b.authCtx(ctx, time.Hour)
if err != nil {
return
}
bp, err := b.Path()
if err != nil {
return
}
id, err := b.Thread()
if err != nil {
return
}
if err = b.c.Remove(ctx, id, b.Key()); err != nil {
return
}
_ = os.RemoveAll(filepath.Join(bp, collection.SeedName))
_ = os.RemoveAll(filepath.Join(bp, b.conf.Dir))
return nil
}
// GetIdentityToken returns a did.Token for the bucket identity valid for the given duration.
func (b *Bucket) GetIdentityToken(dur time.Duration) (did.Token, error) {
ctx, err := b.authCtx(context.TODO(), dur)
if err != nil {
return "", err
}
token, _ := did.TokenFromContext(ctx)
return token, nil
}
func (b *Bucket) loadLocalRepo(ctx context.Context, pth, name string, setCidVersion bool) error {
r, err := NewRepo(pth, name, options.BalancedLayout)
if err != nil {
return err
}
b.repo = r
if setCidVersion {
if err = b.setRepoCidVersion(ctx); err != nil {
return err
}
}
return nil
}
func (b *Bucket) setRepoCidVersion(ctx context.Context) error {
if b.repo == nil {
return nil
}
r, err := b.Roots(ctx)
if err != nil {
return err
}
b.repo.SetCidVersion(int(r.Remote.Version()))
return nil
}
func (b *Bucket) containsPath(pth string) (c bool, err error) {
bp, err := b.Path()
if err != nil {
return
}
ar, err := filepath.Abs(bp)
if err != nil {
return
}
ap, err := filepath.Abs(pth)
if err != nil {
return
}
return strings.HasPrefix(ap, ar), nil
}
func (b *Bucket) getRemoteRoot(ctx context.Context) (cid.Cid, error) {
ctx, err := b.authCtx(ctx, time.Hour)
if err != nil {
return cid.Undef, err
}
id, err := b.Thread()
if err != nil {
return cid.Undef, err
}
rr, err := b.c.Get(ctx, id, b.Key())
if err != nil {
return cid.Undef, err
}
rp, err := dag.NewResolvedPath(rr.Bucket.Path)
if err != nil {
return cid.Undef, err
}
return rp.Cid(), nil
}
// authCtx returns an identity token context for authentication and authorization.
func (b *Bucket) authCtx(ctx context.Context, dur time.Duration) (context.Context, error) {
identity := &thread.Libp2pIdentity{}
if err := identity.UnmarshalString(b.conf.Viper.GetString("identity")); err != nil {
return nil, err
}
return authCtx(ctx, b.c, identity, dur)
}