This repository has been archived by the owner on Jun 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
ops.go
260 lines (222 loc) · 5.66 KB
/
ops.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
package mfs
import (
"context"
"fmt"
"os"
gopath "path"
"strings"
path "github.com/ipfs/go-path"
cid "github.com/ipfs/go-cid"
ipld "github.com/ipfs/go-ipld-format"
)
// TODO: Evaluate moving all this operations to as `Root`
// methods, since all of them use it as its first argument
// and there is no clear documentation that explains this
// separation.
// Mv moves the file or directory at 'src' to 'dst'
// TODO: Document what the strings 'src' and 'dst' represent.
//
// Deprecated: use github.com/ipfs/boxo/mfs.Mv
func Mv(r *Root, src, dst string) error {
srcDirName, srcFname := gopath.Split(src)
var dstDirName string
var dstFname string
if dst[len(dst)-1] == '/' {
dstDirName = dst
dstFname = srcFname
} else {
dstDirName, dstFname = gopath.Split(dst)
}
// get parent directories of both src and dest first
dstDir, err := lookupDir(r, dstDirName)
if err != nil {
return err
}
srcDir, err := lookupDir(r, srcDirName)
if err != nil {
return err
}
srcObj, err := srcDir.Child(srcFname)
if err != nil {
return err
}
nd, err := srcObj.GetNode()
if err != nil {
return err
}
fsn, err := dstDir.Child(dstFname)
if err == nil {
switch n := fsn.(type) {
case *File:
_ = dstDir.Unlink(dstFname)
case *Directory:
dstDir = n
dstFname = srcFname
default:
return fmt.Errorf("unexpected type at path: %s", dst)
}
} else if err != os.ErrNotExist {
return err
}
err = dstDir.AddChild(dstFname, nd)
if err != nil {
return err
}
if srcDir.name == dstDir.name && srcFname == dstFname {
return nil
}
return srcDir.Unlink(srcFname)
}
func lookupDir(r *Root, path string) (*Directory, error) {
di, err := Lookup(r, path)
if err != nil {
return nil, err
}
d, ok := di.(*Directory)
if !ok {
return nil, fmt.Errorf("%s is not a directory", path)
}
return d, nil
}
// PutNode inserts 'nd' at 'path' in the given mfs
// TODO: Rename or clearly document that this is not about nodes but actually
// MFS files/directories (that in the underlying representation can be
// considered as just nodes).
// TODO: Document why are we handling IPLD nodes in the first place when we
// are actually referring to files/directories (that is, it can't be any
// node, it has to have a specific format).
// TODO: Can this function add directories or just files? What would be the
// difference between adding a directory with this method and creating it
// with `Mkdir`.
//
// Deprecated: use github.com/ipfs/boxo/mfs.PutNode
func PutNode(r *Root, path string, nd ipld.Node) error {
dirp, filename := gopath.Split(path)
if filename == "" {
return fmt.Errorf("cannot create file with empty name")
}
pdir, err := lookupDir(r, dirp)
if err != nil {
return err
}
return pdir.AddChild(filename, nd)
}
// MkdirOpts is used by Mkdir
//
// Deprecated: use github.com/ipfs/boxo/mfs.MkdirOpts
type MkdirOpts struct {
Mkparents bool
Flush bool
CidBuilder cid.Builder
}
// Mkdir creates a directory at 'path' under the directory 'd', creating
// intermediary directories as needed if 'mkparents' is set to true
//
// Deprecated: use github.com/ipfs/boxo/mfs.Mkdir
func Mkdir(r *Root, pth string, opts MkdirOpts) error {
if pth == "" {
return fmt.Errorf("no path given to Mkdir")
}
parts := path.SplitList(pth)
if parts[0] == "" {
parts = parts[1:]
}
// allow 'mkdir /a/b/c/' to create c
if parts[len(parts)-1] == "" {
parts = parts[:len(parts)-1]
}
if len(parts) == 0 {
// this will only happen on 'mkdir /'
if opts.Mkparents {
return nil
}
return fmt.Errorf("cannot create directory '/': Already exists")
}
cur := r.GetDirectory()
for i, d := range parts[:len(parts)-1] {
fsn, err := cur.Child(d)
if err == os.ErrNotExist && opts.Mkparents {
mkd, err := cur.Mkdir(d)
if err != nil {
return err
}
if opts.CidBuilder != nil {
mkd.SetCidBuilder(opts.CidBuilder)
}
fsn = mkd
} else if err != nil {
return err
}
next, ok := fsn.(*Directory)
if !ok {
return fmt.Errorf("%s was not a directory", path.Join(parts[:i]))
}
cur = next
}
final, err := cur.Mkdir(parts[len(parts)-1])
if err != nil {
if !opts.Mkparents || err != os.ErrExist || final == nil {
return err
}
}
if opts.CidBuilder != nil {
final.SetCidBuilder(opts.CidBuilder)
}
if opts.Flush {
err := final.Flush()
if err != nil {
return err
}
}
return nil
}
// Lookup extracts the root directory and performs a lookup under it.
// TODO: Now that the root is always a directory, can this function
// be collapsed with `DirLookup`? Or at least be made a method of `Root`?
//
// Deprecated: use github.com/ipfs/boxo/mfs.Lookup
func Lookup(r *Root, path string) (FSNode, error) {
dir := r.GetDirectory()
return DirLookup(dir, path)
}
// DirLookup will look up a file or directory at the given path
// under the directory 'd'
//
// Deprecated: use github.com/ipfs/boxo/mfs.DirLookup
func DirLookup(d *Directory, pth string) (FSNode, error) {
pth = strings.Trim(pth, "/")
parts := path.SplitList(pth)
if len(parts) == 1 && parts[0] == "" {
return d, nil
}
var cur FSNode
cur = d
for i, p := range parts {
chdir, ok := cur.(*Directory)
if !ok {
return nil, fmt.Errorf("cannot access %s: Not a directory", path.Join(parts[:i+1]))
}
child, err := chdir.Child(p)
if err != nil {
return nil, err
}
cur = child
}
return cur, nil
}
// TODO: Document this function and link its functionality
// with the republisher.
//
// Deprecated: use github.com/ipfs/boxo/mfs.FlushPath
func FlushPath(ctx context.Context, rt *Root, pth string) (ipld.Node, error) {
nd, err := Lookup(rt, pth)
if err != nil {
return nil, err
}
err = nd.Flush()
if err != nil {
return nil, err
}
rt.repub.WaitPub(ctx)
return nd.GetNode()
}