/
node.go
197 lines (179 loc) · 5.1 KB
/
node.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
package main
import (
"fmt"
"os"
"path/filepath"
"syscall"
"time"
"bazil.org/fuse"
"bazil.org/fuse/syscallx"
"golang.org/x/net/context"
)
type Node struct {
parent string
name string
path string
fs *ClueFS
}
func NewNode(parent string, name string, fs *ClueFS) *Node {
path := filepath.Join(parent, name)
if len(parent) == 0 {
path = name
}
return &Node{parent: parent, name: name, path: path, fs: fs}
}
func (n *Node) setParentAndName(parent string, name string) {
path := filepath.Join(parent, name)
if len(parent) == 0 {
path = name
}
n.parent, n.name, n.path = parent, name, path
}
func (n Node) String() string {
return fmt.Sprintf("%s %s", n.parent, n.name)
}
func (n *Node) Attr(ctx context.Context, attr *fuse.Attr) error {
var st syscall.Stat_t
if err := syscall.Lstat(n.path, &st); err != nil {
return osErrorToFuseError(err)
}
*attr = statToFuseAttr(st)
return nil
}
func (n *Node) Getattr(ctx context.Context, req *fuse.GetattrRequest, resp *fuse.GetattrResponse) error {
return n.Attr(ctx, &resp.Attr)
}
func (n *Node) Access(ctx context.Context, req *fuse.AccessRequest) error {
isDir, err := isDirectory(n.path)
defer trace(NewAccessOp(req, n.path, isDir))
if err != nil {
return err
}
if access(n.path, req.Mask) {
return nil
}
return fuse.Errno(syscall.EACCES)
}
func (n *Node) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
defer trace(NewSetattrOp(req, n.path))
var err error
if req.Valid.Atime() {
var mtime time.Time
_, mtime, err = statAtimeMtime(n.path)
if err == nil {
err = os.Chtimes(n.path, req.Atime, mtime)
}
} else if req.Valid.Mtime() {
var atime time.Time
atime, _, err = statAtimeMtime(n.path)
if err == nil {
err = os.Chtimes(n.path, atime, req.Mtime)
}
} else if req.Valid.Bkuptime() {
// TODO: set backup time
} else if req.Valid.Chgtime() {
// TODO: set change time
} else if req.Valid.Crtime() {
// TODO: set creation time
} else if req.Valid.Flags() {
// TODO: set flags
} else if req.Valid.Uid() {
var gid uint32
_, gid, err = getUidGid(n.path)
if err == nil {
err = os.Chown(n.path, int(req.Uid), int(gid))
}
} else if req.Valid.Gid() {
var uid uint32
uid, _, err = getUidGid(n.path)
if err == nil {
err = os.Chown(n.path, int(uid), int(req.Gid))
}
} else if req.Valid.Size() {
err = os.Truncate(n.path, int64(req.Size))
} else if req.Valid.Mode() {
err = os.Chmod(n.path, req.Mode.Perm())
}
if err != nil {
return osErrorToFuseError(err)
}
return n.Attr(ctx, &resp.Attr)
}
func (n *Node) Readlink(ctx context.Context, req *fuse.ReadlinkRequest) (string, error) {
defer trace(NewReadlinkOp(req, n.path))
dest, err := os.Readlink(n.path)
if err != nil {
return "", osErrorToFuseError(err)
}
return dest, nil
}
func (n *Node) Getxattr(ctx context.Context, req *fuse.GetxattrRequest, resp *fuse.GetxattrResponse) error {
defer trace(NewGetxattrOp(req, n.path))
size, err := syscallx.Getxattr(n.path, req.Name, []byte{})
if err != nil || size <= 0 {
return fuse.ErrNoXattr
}
buffer := make([]byte, size)
size, err = syscallx.Getxattr(n.path, req.Name, buffer)
if err != nil {
return osErrorToFuseError(err)
}
resp.Xattr = buffer
return nil
}
func (n *Node) Listxattr(ctx context.Context, req *fuse.ListxattrRequest, resp *fuse.ListxattrResponse) error {
defer trace(NewListxattrOp(req, n.path))
size, err := syscallx.Listxattr(n.path, []byte{})
if err != nil || size <= 0 {
return nil
}
buffer := make([]byte, size)
size, err = syscallx.Listxattr(n.path, buffer)
if err != nil {
return osErrorToFuseError(err)
}
resp.Xattr = buffer
return nil
}
func (n *Node) Setxattr(ctx context.Context, req *fuse.SetxattrRequest) error {
defer trace(NewSetxattrOp(req, n.path))
err := syscallx.Setxattr(n.path, req.Name, req.Xattr, int(req.Flags))
return osErrorToFuseError(err)
}
func (n *Node) Removexattr(ctx context.Context, req *fuse.RemovexattrRequest) error {
defer trace(NewRemovexattrOp(req, n.path))
// TODO: this needs to be improved, since the behavior of Removexattr depends
// on the previous existance of the attribute. The return code of the operation
// is governed by the flags. See bazil.org/fuse/syscallx.Removexattr comments.
_, err := syscallx.Getxattr(n.path, req.Name, []byte{})
if err == nil {
// TODO: There is already an attribute with that name. Should return
// the expected error code according to the request's flags
err = syscallx.Removexattr(n.path, req.Name)
return osErrorToFuseError(err)
}
return nil
}
func isDirectory(fullpath string) (bool, error) {
var st syscall.Stat_t
if err := syscall.Stat(fullpath, &st); err != nil {
return false, osErrorToFuseError(err)
}
return st.Mode&syscall.S_IFMT == syscall.S_IFDIR, nil
}
func getFuseDirent(fullpath string, name string) fuse.Dirent {
var st syscall.Stat_t
syscall.Lstat(fullpath, &st)
return fuse.Dirent{
Inode: st.Ino,
Type: fuseTypeFromStatMode(st.Mode),
Name: name,
}
}
func getUidGid(path string) (uint32, uint32, error) {
var st syscall.Stat_t
if err := syscall.Lstat(path, &st); err != nil {
return 0, 0, err
}
return st.Uid, st.Gid, nil
}