/
client.go
785 lines (712 loc) · 17.4 KB
/
client.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
package client
import (
"context"
"errors"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
"sync/atomic"
"github.com/gogo/status"
"github.com/ipfs/go-cid"
"github.com/ipfs/interface-go-ipfs-core/path"
pb "github.com/textileio/textile/v2/api/bucketsd/pb"
"github.com/textileio/textile/v2/buckets"
"github.com/textileio/textile/v2/util"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
)
const (
// chunkSize for add file requests.
chunkSize = 1024 * 32
)
var (
// ErrPushPathQueueClosed indicates the push path is or was closed.
ErrPushPathQueueClosed = errors.New("push path queue is closed")
)
// Client provides the client api.
type Client struct {
c pb.APIServiceClient
conn *grpc.ClientConn
}
// NewClient starts the client.
func NewClient(target string, opts ...grpc.DialOption) (*Client, error) {
conn, err := grpc.Dial(target, opts...)
if err != nil {
return nil, err
}
return &Client{
c: pb.NewAPIServiceClient(conn),
conn: conn,
}, nil
}
// Close closes the client's grpc connection and cancels any active requests.
func (c *Client) Close() error {
return c.conn.Close()
}
// Create initializes a new bucket.
// The bucket name is only meant to help identify a bucket in a UI and is not unique.
func (c *Client) Create(ctx context.Context, opts ...CreateOption) (*pb.CreateResponse, error) {
args := &createOptions{}
for _, opt := range opts {
opt(args)
}
var strCid string
if args.fromCid.Defined() {
strCid = args.fromCid.String()
}
return c.c.Create(ctx, &pb.CreateRequest{
Name: args.name,
Private: args.private,
BootstrapCid: strCid,
Unfreeze: args.unfreeze,
})
}
// Root returns the bucket root.
func (c *Client) Root(ctx context.Context, key string) (*pb.RootResponse, error) {
return c.c.Root(ctx, &pb.RootRequest{
Key: key,
})
}
// Links returns a list of bucket path URL links.
func (c *Client) Links(ctx context.Context, key, pth string) (*pb.LinksResponse, error) {
return c.c.Links(ctx, &pb.LinksRequest{
Key: key,
Path: filepath.ToSlash(pth),
})
}
// List returns a list of all bucket roots.
func (c *Client) List(ctx context.Context) (*pb.ListResponse, error) {
return c.c.List(ctx, &pb.ListRequest{})
}
// ListIpfsPath returns items at a particular path in a UnixFS path living in the IPFS network.
func (c *Client) ListIpfsPath(ctx context.Context, pth path.Path) (*pb.ListIpfsPathResponse, error) {
return c.c.ListIpfsPath(ctx, &pb.ListIpfsPathRequest{Path: pth.String()})
}
// ListPath returns information about a bucket path.
func (c *Client) ListPath(ctx context.Context, key, pth string) (*pb.ListPathResponse, error) {
return c.c.ListPath(ctx, &pb.ListPathRequest{
Key: key,
Path: filepath.ToSlash(pth),
})
}
// SetPath set a particular path to an existing IPFS UnixFS DAG.
func (c *Client) SetPath(ctx context.Context, key, pth string, remoteCid cid.Cid) (*pb.SetPathResponse, error) {
return c.c.SetPath(ctx, &pb.SetPathRequest{
Key: key,
Path: filepath.ToSlash(pth),
Cid: remoteCid.String(),
})
}
// MovePath moves a particular path to another path in the existing IPFS UnixFS DAG.
func (c *Client) MovePath(ctx context.Context, key, pth string, dest string) error {
_, err := c.c.MovePath(ctx, &pb.MovePathRequest{
Key: key,
FromPath: filepath.ToSlash(pth),
ToPath: filepath.ToSlash(dest),
})
return err
}
type pushPathResult struct {
path path.Resolved
root path.Resolved
err error
}
// PushPath pushes a file to a bucket path.
// This will return the resolved path and the bucket's new root path.
func (c *Client) PushPath(
ctx context.Context,
key, pth string,
reader io.Reader,
opts ...Option,
) (result path.Resolved, root path.Resolved, err error) {
args := &options{}
for _, opt := range opts {
opt(args)
}
if args.progress != nil {
defer close(args.progress)
}
stream, err := c.c.PushPath(ctx)
if err != nil {
return nil, nil, err
}
var xr string
if args.root != nil {
xr = args.root.String()
}
if err = stream.Send(&pb.PushPathRequest{
Payload: &pb.PushPathRequest_Header_{
Header: &pb.PushPathRequest_Header{
Key: key,
Path: filepath.ToSlash(pth),
Root: xr,
},
},
}); err != nil {
return nil, nil, err
}
waitCh := make(chan pushPathResult)
go func() {
defer close(waitCh)
for {
rep, err := stream.Recv()
if err == io.EOF {
return
} else if err != nil {
waitCh <- pushPathResult{err: err}
return
}
if rep.Event.Path != "" {
id, err := cid.Parse(rep.Event.Path)
if err != nil {
waitCh <- pushPathResult{err: err}
return
}
r, err := util.NewResolvedPath(rep.Event.Root.Path)
if err != nil {
waitCh <- pushPathResult{err: err}
return
}
waitCh <- pushPathResult{
path: path.IpfsPath(id),
root: r,
}
} else if args.progress != nil {
args.progress <- rep.Event.Bytes
}
}
}()
end := func(err error) error {
if err := stream.CloseSend(); err != nil {
return err
}
return err
}
buf := make([]byte, chunkSize)
for {
n, err := reader.Read(buf)
if n > 0 {
if err := stream.Send(&pb.PushPathRequest{
Payload: &pb.PushPathRequest_Chunk{
Chunk: buf[:n],
},
}); err == io.EOF {
break
} else if err != nil {
return nil, nil, end(err)
}
} else if err == io.EOF {
break
} else if err != nil {
return nil, nil, end(err)
}
}
if err := end(nil); err != nil {
return nil, nil, err
}
res := <-waitCh
return res.path, res.root, res.err
}
// PushPathsResult contains the result of a Push.
type PushPathsResult struct {
Path string
Cid cid.Cid
Size int64
Pinned int64
Root path.Resolved
err error
}
// PushPathsQueue handles PushPath input and output.
type PushPathsQueue struct {
// Current contains the current push result.
Current PushPathsResult
q []pushPath
len int
startCh chan []string
inCh chan pushPath
inWaitCh chan struct{}
outCh chan PushPathsResult
started bool
stopped bool
closeFunc func() error
closed bool
closeWaitCh chan error
size int64
complete int64
lk sync.Mutex
wg sync.WaitGroup
}
type pushPath struct {
path string
r func() (io.ReadCloser, error)
}
// AddFile adds a file to the queue.
// pth is the location relative to the bucket root at which to insert the file, e.g., "/path/to/mybone.jpg".
// name is the location of the file on the local filesystem, e.g., "/Users/clyde/Downloads/mybone.jpg".
func (c *PushPathsQueue) AddFile(pth, name string) error {
c.lk.Lock()
defer c.lk.Unlock()
if c.closed {
return ErrPushPathQueueClosed
}
if c.started {
return errors.New("cannot call AddFile after Next")
}
f, err := os.Open(name)
if err != nil {
return err
}
info, err := f.Stat()
if err != nil {
f.Close()
return err
}
atomic.AddInt64(&c.size, info.Size())
f.Close()
c.q = append(c.q, pushPath{
path: filepath.ToSlash(pth),
r: func() (io.ReadCloser, error) {
return os.Open(name)
},
})
return nil
}
// AddReader adds a reader to the queue.
// pth is the location relative to the bucket root at which to insert the file, e.g., "/path/to/mybone.jpg".
// r is the reader to read from.
// size is the size of the reader. Use of the WithProgress option is not recommended if the reader size is unknown.
func (c *PushPathsQueue) AddReader(pth string, r io.Reader, size int64) error {
c.lk.Lock()
defer c.lk.Unlock()
if c.closed {
return ErrPushPathQueueClosed
}
if c.started {
return errors.New("cannot call AddReader after Next")
}
atomic.AddInt64(&c.size, size)
c.q = append(c.q, pushPath{
path: filepath.ToSlash(pth),
r: func() (io.ReadCloser, error) { return ioutil.NopCloser(r), nil },
})
return nil
}
// Size returns the queue size in bytes.
func (c *PushPathsQueue) Size() int64 {
return atomic.LoadInt64(&c.size)
}
// Complete returns the portion of the queue size that has been pushed.
func (c *PushPathsQueue) Complete() int64 {
return atomic.LoadInt64(&c.complete)
}
// Next blocks while the queue is open, returning true when a result is ready.
// Use Current to access the result.
func (c *PushPathsQueue) Next() (ok bool) {
c.lk.Lock()
if c.closed {
c.lk.Unlock()
return false
}
if !c.started {
c.started = true
c.len = len(c.q)
c.start()
}
if c.len == 0 {
c.lk.Unlock()
return false
}
c.lk.Unlock()
select {
case r, k := <-c.outCh:
if !k {
return false
}
c.lk.Lock()
c.len--
c.Current = r
c.lk.Unlock()
return true
}
}
func (c *PushPathsQueue) start() {
go func() {
defer close(c.inWaitCh)
var paths []string
c.lk.Lock()
for _, p := range c.q {
paths = append(paths, p.path)
}
c.lk.Unlock()
c.startCh <- paths
for {
c.lk.Lock()
if c.closed {
c.q = nil
c.lk.Unlock()
return
}
if len(c.q) == 0 {
c.lk.Unlock()
return
}
var p pushPath
p, c.q = c.q[0], c.q[1:]
c.lk.Unlock()
c.inCh <- p
}
}()
}
func (c *PushPathsQueue) stop() {
c.lk.Lock()
defer c.lk.Unlock()
c.stopped = true
}
// Err returns the current queue error.
// Call this method before checking the value of Current.
func (c *PushPathsQueue) Err() error {
c.lk.Lock()
defer c.lk.Unlock()
return c.Current.err
}
// Close the queue.
// Failure to close may lead to unpredictable bucket state.
func (c *PushPathsQueue) Close() error {
c.lk.Lock()
if c.closed {
c.lk.Unlock()
return nil
}
c.closed = true
c.lk.Unlock()
<-c.inWaitCh
close(c.inCh)
c.lk.Lock()
wait := !c.stopped
c.lk.Unlock()
if err := c.closeFunc(); err != nil {
return err
}
if wait {
return <-c.closeWaitCh
}
return nil
}
// PushPaths returns a queue that can be used to push multiple files and readers to bucket paths.
// See PushPathQueue.AddFile and PushPathsQueue.AddReader for more.
func (c *Client) PushPaths(ctx context.Context, key string, opts ...Option) (*PushPathsQueue, error) {
args := &options{}
for _, opt := range opts {
opt(args)
}
stream, err := c.c.PushPaths(ctx)
if err != nil {
return nil, err
}
var xr string
if args.root != nil {
xr = args.root.String()
}
q := &PushPathsQueue{
startCh: make(chan []string),
inCh: make(chan pushPath),
inWaitCh: make(chan struct{}),
outCh: make(chan PushPathsResult),
closeFunc: func() error {
return stream.CloseSend()
},
closeWaitCh: make(chan error, 1),
}
go func() {
defer q.stop()
for {
rep, err := stream.Recv()
if err == io.EOF {
q.closeWaitCh <- nil
return
} else if err != nil {
if strings.Contains(err.Error(), "STREAM_CLOSED") {
err = ErrPushPathQueueClosed
}
q.lk.Lock()
closed := q.closed
q.lk.Unlock()
if !closed {
q.outCh <- PushPathsResult{err: err}
}
q.closeWaitCh <- err
return
}
id, err := cid.Parse(rep.Cid)
if err != nil {
q.outCh <- PushPathsResult{err: err}
return
}
root, err := util.NewResolvedPath(rep.Root.Path)
if err != nil {
q.outCh <- PushPathsResult{err: err}
return
}
q.outCh <- PushPathsResult{
Path: rep.Path,
Cid: id,
Size: rep.Size,
Pinned: rep.Pinned,
Root: root,
}
}
}()
sendChunk := func(c *pb.PushPathsRequest_Chunk) bool {
q.lk.Lock()
if q.closed {
q.lk.Unlock()
return false
}
q.lk.Unlock()
if err := stream.Send(&pb.PushPathsRequest{
Payload: &pb.PushPathsRequest_Chunk_{
Chunk: c,
},
}); err == io.EOF {
return false // error is waiting to be received with stream.Recv above
} else if err != nil {
q.outCh <- PushPathsResult{err: err}
return false
}
atomic.AddInt64(&q.complete, int64(len(c.Data)))
q.lk.Lock()
if !q.closed && args.progress != nil {
args.progress <- atomic.LoadInt64(&q.complete)
}
q.lk.Unlock()
return true
}
go func() {
loop:
for {
select {
case paths := <-q.startCh:
if len(paths) > 0 {
if err := stream.Send(&pb.PushPathsRequest{
Payload: &pb.PushPathsRequest_Header_{
Header: &pb.PushPathsRequest_Header{
Key: key,
Root: xr,
Paths: paths,
},
},
}); err != nil {
q.outCh <- PushPathsResult{err: err}
break loop
}
}
case p, ok := <-q.inCh:
if !ok {
break loop
}
r, err := p.r()
if err != nil {
q.outCh <- PushPathsResult{err: err}
break loop
}
buf := make([]byte, chunkSize)
for {
n, err := r.Read(buf)
c := &pb.PushPathsRequest_Chunk{
Path: p.path,
}
if n > 0 {
c.Data = make([]byte, n)
copy(c.Data, buf[:n])
if ok := sendChunk(c); !ok {
break
}
} else if err == io.EOF {
sendChunk(c)
break
} else if err != nil {
q.outCh <- PushPathsResult{err: err}
break
}
}
r.Close()
}
}
}()
return q, nil
}
// PullPath pulls the bucket path, writing it to writer if it's a file.
func (c *Client) PullPath(ctx context.Context, key, pth string, writer io.Writer, opts ...Option) error {
args := &options{}
for _, opt := range opts {
opt(args)
}
pth = filepath.ToSlash(pth)
stream, err := c.c.PullPath(ctx, &pb.PullPathRequest{
Key: key,
Path: pth,
})
if err != nil {
return err
}
var written int64
for {
rep, err := stream.Recv()
if err == io.EOF {
break
} else if err != nil {
return err
}
n, err := writer.Write(rep.Chunk)
if err != nil {
return err
}
written += int64(n)
if args.progress != nil {
args.progress <- written
}
}
return nil
}
// PullIpfsPath pulls the path from a remote UnixFS dag, writing it to writer if it's a file.
func (c *Client) PullIpfsPath(ctx context.Context, pth path.Path, writer io.Writer, opts ...Option) error {
args := &options{}
for _, opt := range opts {
opt(args)
}
stream, err := c.c.PullIpfsPath(ctx, &pb.PullIpfsPathRequest{
Path: pth.String(),
})
if err != nil {
return err
}
var written int64
for {
rep, err := stream.Recv()
if err == io.EOF {
break
} else if err != nil {
return err
}
n, err := writer.Write(rep.Chunk)
if err != nil {
return err
}
written += int64(n)
if args.progress != nil {
args.progress <- written
}
}
return nil
}
// Remove removes an entire bucket.
// Files and directories will be unpinned.
func (c *Client) Remove(ctx context.Context, key string) error {
_, err := c.c.Remove(ctx, &pb.RemoveRequest{
Key: key,
})
return err
}
// RemovePath removes the file or directory at path.
// Files and directories will be unpinned.
func (c *Client) RemovePath(ctx context.Context, key, pth string, opts ...Option) (path.Resolved, error) {
args := &options{}
for _, opt := range opts {
opt(args)
}
var xr string
if args.root != nil {
xr = args.root.String()
}
res, err := c.c.RemovePath(ctx, &pb.RemovePathRequest{
Key: key,
Path: filepath.ToSlash(pth),
Root: xr,
})
if err != nil {
return nil, err
}
return util.NewResolvedPath(res.Root.Path)
}
// PushPathAccessRoles updates path access roles by merging the pushed roles with existing roles.
// roles is a map of string marshaled public keys to path roles. A non-nil error is returned
// if the map keys are not unmarshalable to public keys.
// To delete a role for a public key, set its value to buckets.None.
func (c *Client) PushPathAccessRoles(ctx context.Context, key, pth string, roles map[string]buckets.Role) error {
pbroles, err := buckets.RolesToPb(roles)
if err != nil {
return err
}
_, err = c.c.PushPathAccessRoles(ctx, &pb.PushPathAccessRolesRequest{
Key: key,
Path: filepath.ToSlash(pth),
Roles: pbroles,
})
return err
}
// PullPathAccessRoles returns access roles for a path.
func (c *Client) PullPathAccessRoles(ctx context.Context, key, pth string) (map[string]buckets.Role, error) {
res, err := c.c.PullPathAccessRoles(ctx, &pb.PullPathAccessRolesRequest{
Key: key,
Path: filepath.ToSlash(pth),
})
if err != nil {
return nil, err
}
return buckets.RolesFromPb(res.Roles)
}
// DefaultArchiveConfig gets the default archive config for the specified Bucket.
func (c *Client) DefaultArchiveConfig(ctx context.Context, key string) (*pb.ArchiveConfig, error) {
res, err := c.c.DefaultArchiveConfig(ctx, &pb.DefaultArchiveConfigRequest{Key: key})
if err != nil {
return nil, err
}
return res.ArchiveConfig, nil
}
// SetDefaultArchiveConfig sets the default archive config for the specified Bucket.
func (c *Client) SetDefaultArchiveConfig(ctx context.Context, key string, config *pb.ArchiveConfig) error {
req := &pb.SetDefaultArchiveConfigRequest{
Key: key,
ArchiveConfig: config,
}
_, err := c.c.SetDefaultArchiveConfig(ctx, req)
return err
}
// Archive creates a Filecoin bucket archive via Powergate.
func (c *Client) Archive(ctx context.Context, key string, opts ...ArchiveOption) error {
req := &pb.ArchiveRequest{
Key: key,
}
for _, opt := range opts {
opt(req)
}
_, err := c.c.Archive(ctx, req)
return err
}
// Archives returns information about current and historical archives.
func (c *Client) Archives(ctx context.Context, key string) (*pb.ArchivesResponse, error) {
return c.c.Archives(ctx, &pb.ArchivesRequest{Key: key})
}
// ArchiveWatch watches status events from a Filecoin bucket archive.
func (c *Client) ArchiveWatch(ctx context.Context, key string, ch chan<- string) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
stream, err := c.c.ArchiveWatch(ctx, &pb.ArchiveWatchRequest{Key: key})
if err != nil {
return err
}
for {
reply, err := stream.Recv()
if err == io.EOF || status.Code(err) == codes.Canceled {
break
}
if err != nil {
return err
}
ch <- reply.Msg
}
return nil
}