-
Notifications
You must be signed in to change notification settings - Fork 43
/
directory.go
151 lines (123 loc) · 3.35 KB
/
directory.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
//+build !windows
package libfuse
import (
"context"
"fmt"
"log"
"strconv"
"strings"
"syscall"
"github.com/FleekHQ/space-daemon/core/spacefs"
"bazil.org/fuse"
"bazil.org/fuse/fs"
)
var _ fs.Node = (*VFSDir)(nil)
var _ = fs.NodeRequestLookuper(&VFSDir{})
var _ = fs.HandleReadDirAller(&VFSDir{})
var _ = fs.NodeCreater(&VFSDir{})
// VFSDir represents a directory in the Virtual file system
type VFSDir struct {
vfs *VFS // pointer to the parent file system
dirOps spacefs.DirOps
}
func NewVFSDir(vfs *VFS, dirOps spacefs.DirOps) *VFSDir {
return &VFSDir{
vfs: vfs,
dirOps: dirOps,
}
}
// Attr returns fuse.Attr for the directory
func (dir *VFSDir) Attr(ctx context.Context, attr *fuse.Attr) error {
dirAttribute, err := dir.dirOps.Attribute()
if err != nil {
return err
}
attr.Mode = dirAttribute.Mode()
if uid, err := strconv.Atoi(dirAttribute.Uid()); err == nil {
attr.Uid = uint32(uid)
}
if gid, err := strconv.Atoi(dirAttribute.Gid()); err == nil {
attr.Gid = uint32(gid)
}
return nil
}
// ReadDirAll reads all the content of a directory
// In this mirror drive case, we just return items in the mirror path
func (dir *VFSDir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
dirList, err := dir.dirOps.ReadDir(ctx)
if err != nil {
return nil, err
}
var res []fuse.Dirent
for _, dirEntry := range dirList {
entryAttribute, err := dirEntry.Attribute()
if err != nil {
return nil, err
}
entry := fuse.Dirent{
Name: entryAttribute.Name(),
}
if entryAttribute.IsDir() {
entry.Type = fuse.DT_Dir
} else {
entry.Type = fuse.DT_File
}
res = append(res, entry)
}
return res, nil
}
// Lookup finds entry Node within a directory
// Seems to be called when not enough information is gotten from the ReadDirAll
func (dir *VFSDir) Lookup(ctx context.Context, req *fuse.LookupRequest, resp *fuse.LookupResponse) (fs.Node, error) {
path := dir.dirOps.Path() + req.Name
entry, err := dir.vfs.fsOps.LookupPath(ctx, path)
if err != nil {
return nil, err
}
entryAttribute, err := entry.Attribute()
if err != nil {
return nil, err
}
if entryAttribute.IsDir() {
dirOps, ok := entry.(spacefs.DirOps)
if !ok {
// TODO: Return a better syscall error
return nil, syscall.ENOENT
}
return NewVFSDir(dir.vfs, dirOps), nil
}
fileOps, ok := entry.(spacefs.FileOps)
if !ok {
// TODO: Return a better syscall error
return nil, syscall.ENOENT
}
return &VFSFile{
vfs: dir.vfs,
fileOps: fileOps,
}, nil
}
// Create is invoked when a new directory is to be created
// It implements the fs.NodeCreator interface
func (dir *VFSDir) Create(ctx context.Context, req *fuse.CreateRequest, resp *fuse.CreateResponse) (fs.Node, fs.Handle, error) {
path := dir.dirOps.Path()
log.Printf("Creating a file/directory: %+v in path: %s", *req, path)
dirEntry, err := dir.vfs.fsOps.CreateEntry(ctx, spacefs.CreateDirEntry{
Path: fmt.Sprintf("%s%c%s", strings.TrimSuffix(path, "/"), '/', req.Name),
Mode: req.Mode,
})
if err != nil {
return nil, nil, err
}
if dirOps, ok := dirEntry.(spacefs.DirOps); ok {
return NewVFSDir(dir.vfs, dirOps), nil, nil
}
if fileOps, ok := dirEntry.(spacefs.FileOps); ok {
vfsFile := NewVFSFile(dir.vfs, fileOps)
handler, err := NewVFSFileHandler(ctx, vfsFile)
if err != nil {
return nil, nil, err
}
return vfsFile, handler, nil
}
return nil, nil, syscall.EACCES
}