-
Notifications
You must be signed in to change notification settings - Fork 0
/
filetree.go
195 lines (150 loc) · 4.28 KB
/
filetree.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
package resonatefuse
import (
"path/filepath"
"bazil.org/fuse"
"github.com/pkg/errors"
)
// FileTree is the building node of a filesystem
type FileTree struct {
name string
link string
parent *FileTree
children map[string]*FileTree
}
// NewNode constructs a new file tree node
func NewNode(name string, parent *FileTree) *FileTree {
ft := &FileTree{
name: name,
parent: parent,
children: nil,
}
return ft
}
func NewLink(name string, parent *FileTree, linked string) *FileTree {
ft := NewNode(name, parent)
ft.link = linked
return ft
}
// NewDirectory constructs a new file tree with given parent
func NewDirectory(name string, parent *FileTree) *FileTree {
ft := NewNode(name, parent)
ft.children = make(map[string]*FileTree)
return ft
}
func (ft *FileTree) Link() string {
return ft.link
}
// Name gets file's name
func (ft *FileTree) Name() string {
return ft.name
}
// Type returns the type of filetree (folder, file, symlink, etc)
func (ft *FileTree) Type() NodeType {
if ft.link != "" {
return LINK
}
if ft.children == nil {
return FILE
}
return DIR
}
// CreateChild adds a new child to the filetree
func (ft *FileTree) CreateChild(name string) error {
return ft.AddChild(name, NewNode(filepath.Base(name), ft))
}
// CreateDirChild creates a new folder under the current folder
func (ft *FileTree) CreateDirChild(name string) error {
return ft.AddChild(name, NewDirectory(filepath.Base(name), ft))
}
// CreateLinkChild creates a new symlink under the current folder
func (ft *FileTree) CreateLinkChild(name string, link string) error {
return ft.AddChild(name, NewLink(filepath.Base(name), ft, link))
}
// AddChild adds an existing filetree as a child
func (ft *FileTree) AddChild(name string, child *FileTree) error {
if ft.Type() == FILE {
return errors.New("cannot add child to leaf")
}
current := ft.Child(filepath.Dir(name))
if current == nil {
return errors.Errorf("path %v does not exist", filepath.Dir(name))
}
name = filepath.Base(name)
// if _, ok := current.children[name]; ok {
// return errors.Errorf("child %v already exists", name)
// }
current.children[name] = child
child.parent = current
return nil
}
// RemoveChild removes a child from chosen filetree
func (ft *FileTree) RemoveChild(name string) error {
current := ft.Child(filepath.Dir(name))
if current == nil {
return errors.Errorf("path %v does not exist", filepath.Dir(name))
}
name = filepath.Base(name)
if _, ok := current.children[name]; !ok {
return errors.Errorf("child %v does not exist", name)
}
delete(current.children, name)
return nil
}
// Rename changes the name of the current filetree
func (ft *FileTree) Rename(oldname string, newName string, newParent *FileTree) error {
child := ft.Child(oldname)
if child == nil {
return errors.Errorf("could not rename none existant child (%v)", oldname)
}
if err := ft.RemoveChild(oldname); err != nil {
return errors.Wrapf(err, "could not remove child (%v) while renaming", oldname)
}
child.name = newName
if err := newParent.AddChild(newName, child); err != nil {
return errors.Wrapf(err, "could not add child (%v) while renaming", newName)
}
return nil
}
// Children returns all children from chosen filetree
func (ft *FileTree) Children() []*FileTree {
childrenList := make([]*FileTree, 0, len(ft.children))
for _, child := range ft.children {
childrenList = append(childrenList, child)
}
return childrenList
}
// Child returns a specific child from chosen directory
func (ft *FileTree) Child(name string) *FileTree {
if ft.Type() == FILE {
return nil
}
current := ft
for _, child := range splitPath(name) {
current = current.children[child]
if current == nil {
return nil
}
}
return current
}
// Dirents returns all children from chosen filetree
func (ft *FileTree) Dirents() []fuse.Dirent {
childrenList := make([]fuse.Dirent, 0, len(ft.children))
for _, child := range ft.children {
childrenList = append(childrenList, fuse.Dirent{
Name: child.Name(),
Type: child.Type().ToFUSE(),
})
}
return childrenList
}
// Path returns the path of the filetree with respect to the root parent
func (ft *FileTree) Path() string {
if ft.parent == nil {
return "."
}
return filepath.Join(ft.parent.Path(), ft.name)
}
func (ft *FileTree) String() string {
return ft.Path()
}