-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmove.go
179 lines (164 loc) · 4.69 KB
/
move.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
package buckets
import (
"context"
"fmt"
gopath "path"
"strings"
"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"
)
// MovePath moves a path to a different location.
// The destination path does not need to exist.
// Currently, moving the root path is not possible.
func (b *Buckets) MovePath(
ctx context.Context,
thread core.ID,
key string,
identity did.Token,
root path.Resolved,
fpth, tpth string,
) (int64, *Bucket, error) {
txn, err := b.NewTxn(thread, key, identity)
if err != nil {
return 0, nil, err
}
defer txn.Close()
return txn.MovePath(ctx, root, fpth, tpth)
}
// MovePath is Txn based MovePath.
func (t *Txn) MovePath(
ctx context.Context,
root path.Resolved,
fpth, tpth string,
) (int64, *Bucket, error) {
fpth, err := parsePath(fpth)
if err != nil {
return 0, nil, err
}
if fpth == "" {
// @todo: Enable move of root directory
return 0, nil, fmt.Errorf("root cannot be moved")
}
tpth, err = parsePath(tpth)
if err != nil {
return 0, nil, err
}
// Paths are the same, nothing to do
if fpth == tpth {
return 0, nil, fmt.Errorf("path is destination")
}
instance, pth, err := t.b.getBucketAndPath(ctx, t.thread, t.key, t.identity, fpth)
if err != nil {
return 0, nil, fmt.Errorf("getting path: %v", err)
}
if root != nil && root.String() != instance.Path {
return 0, nil, ErrNonFastForward
}
instance.UpdatedAt = time.Now().UnixNano()
instance.SetMetadataAtPath(tpth, collection.Metadata{
UpdatedAt: instance.UpdatedAt,
})
instance.UnsetMetadataWithPrefix(fpth + "/")
if err := t.b.c.Verify(ctx, t.thread, instance, collection.WithIdentity(t.identity)); err != nil {
return 0, nil, fmt.Errorf("verifying bucket update: %v", err)
}
fbpth, err := getBucketPath(instance, fpth)
if err != nil {
return 0, nil, err
}
fitem, err := t.b.pathToItem(ctx, instance, fbpth, false)
if err != nil {
return 0, nil, err
}
tbpth, err := getBucketPath(instance, tpth)
if err != nil {
return 0, nil, err
}
titem, err := t.b.pathToItem(ctx, instance, tbpth, false)
if err == nil {
if fitem.IsDir && !titem.IsDir {
return 0, nil, fmt.Errorf("destination is not a directory")
}
if titem.IsDir {
// from => to becomes new dir:
// - "c" => "b" becomes "b/c"
// - "a.jpg" => "b" becomes "b/a.jpg"
tpth = gopath.Join(tpth, fitem.Name)
}
}
pnode, err := dag.GetNodeAtPath(ctx, t.b.ipfs, pth, instance.GetLinkEncryptionKey())
if err != nil {
return 0, nil, fmt.Errorf("getting node: %v", err)
}
var dirPath path.Resolved
if instance.IsPrivate() {
ctx, dirPath, err = dag.CopyDag(ctx, t.b.ipfs, instance, pnode, fpth, tpth)
if err != nil {
return 0, nil, fmt.Errorf("copying node: %v", err)
}
} else {
ctx, dirPath, err = t.b.setPathFromExistingCid(
ctx,
instance,
path.New(instance.Path),
tpth,
pnode.Cid(),
nil,
nil,
)
if err != nil {
return 0, nil, fmt.Errorf("copying path: %v", err)
}
}
instance.Path = dirPath.String()
// If "a/b" => "a/", cleanup only needed for priv
if strings.HasPrefix(fpth, tpth) {
if instance.IsPrivate() {
ctx, dirPath, err = t.b.removePath(ctx, instance, fpth)
if err != nil {
return 0, nil, fmt.Errorf("removing path: %v", err)
}
instance.Path = dirPath.String()
}
if err := t.b.saveAndPublish(ctx, t.thread, t.identity, instance); err != nil {
return 0, nil, err
}
log.Debugf("moved %s to %s", fpth, tpth)
return dag.GetPinnedBytes(ctx), instanceToBucket(t.thread, instance), nil
}
if strings.HasPrefix(tpth, fpth) {
// If "a/" => "a/b" cleanup each leaf in "a" that isn't "b" (skipping .textileseed)
ppth := path.Join(path.New(instance.Path), fpth)
item, err := t.b.listPath(ctx, instance, ppth)
if err != nil {
return 0, nil, fmt.Errorf("listing path: %v", err)
}
for _, chld := range item.Items {
sp := trimSlash(movePathRegexp.ReplaceAllString(chld.Path, ""))
if strings.Compare(chld.Name, collection.SeedName) == 0 || sp == tpth {
continue
}
ctx, dirPath, err = t.b.removePath(ctx, instance, trimSlash(sp))
if err != nil {
return 0, nil, fmt.Errorf("removing path: %v", err)
}
instance.Path = dirPath.String()
}
} else {
// if a/ => b/ remove a
ctx, dirPath, err = t.b.removePath(ctx, instance, fpth)
if err != nil {
return 0, nil, fmt.Errorf("removing path: %v", err)
}
instance.Path = dirPath.String()
}
if err := t.b.saveAndPublish(ctx, t.thread, t.identity, instance); err != nil {
return 0, nil, err
}
log.Debugf("moved %s to %s", fpth, tpth)
return dag.GetPinnedBytes(ctx), instanceToBucket(t.thread, instance), nil
}