-
Notifications
You must be signed in to change notification settings - Fork 5
/
remove.go
65 lines (56 loc) · 1.66 KB
/
remove.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
package buckets
import (
"context"
"time"
"github.com/ipfs/interface-go-ipfs-core/path"
"github.com/textileio/go-buckets/collection"
"github.com/textileio/go-buckets/dag"
"github.com/textileio/go-threads/core/did"
core "github.com/textileio/go-threads/core/thread"
)
// RemovePath removed a path from a bucket.
// All children under the path will be removed and unpinned.
func (b *Buckets) RemovePath(
ctx context.Context,
thread core.ID,
key string,
identity did.Token,
root path.Resolved,
pth string,
) (int64, *Bucket, error) {
txn, err := b.NewTxn(thread, key, identity)
if err != nil {
return 0, nil, err
}
defer txn.Close()
return txn.RemovePath(ctx, root, pth)
}
// RemovePath is Txn based RemovePath.
func (t *Txn) RemovePath(ctx context.Context, root path.Resolved, pth string) (int64, *Bucket, error) {
pth, err := parsePath(pth)
if err != nil {
return 0, nil, err
}
instance, err := t.b.c.GetSafe(ctx, t.thread, t.key, collection.WithIdentity(t.identity))
if err != nil {
return 0, nil, err
}
if root != nil && root.String() != instance.Path {
return 0, nil, ErrNonFastForward
}
instance.UpdatedAt = time.Now().UnixNano()
instance.UnsetMetadataWithPrefix(pth)
if err := t.b.c.Verify(ctx, t.thread, instance, collection.WithIdentity(t.identity)); err != nil {
return 0, nil, err
}
ctx, dirPath, err := t.b.removePath(ctx, instance, pth)
if err != nil {
return 0, nil, err
}
instance.Path = dirPath.String()
if err := t.b.saveAndPublish(ctx, t.thread, t.identity, instance); err != nil {
return 0, nil, err
}
log.Debugf("removed %s from %s", pth, t.key)
return dag.GetPinnedBytes(ctx), instanceToBucket(t.thread, instance), nil
}