-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
189 lines (160 loc) · 3.81 KB
/
main.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
// Hellofs implements a simple "hello world" file system.
package main
import (
"context"
"flag"
"fmt"
"github.com/liucxer/ceph-fuse/modules/ceph_mgr"
"github.com/liucxer/ceph-fuse/modules/fuse"
"github.com/liucxer/ceph-fuse/modules/fuse/fs"
"github.com/sirupsen/logrus"
"os"
"time"
)
// Dir implements both Node and Handle for the root directory.
type Dir struct {
Path string
}
func (Dir) Attr(ctx context.Context, a *fuse.Attr) error {
a.Inode = 1
a.Mode = os.ModeDir | 0o555
return nil
}
func (dir Dir) Lookup(ctx context.Context, name string) (fs.Node, error) {
return File{
Path: dir.Path,
Name: name,
}, nil
}
var dirDirs = []fuse.Dirent{
{Inode: 2, Name: "hello", Type: fuse.DT_File},
{Inode: 3, Name: "hello_dir", Type: fuse.DT_Dir},
}
func (dir Dir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
dirs, err := GlobalCephMgr.ListDir(dir.Path)
if err != nil {
return nil, err
}
var res []fuse.Dirent
for _, dir := range dirs {
res = append(res, fuse.Dirent{
Inode: uint64(dir.Inode()),
Type: fuse.DirentType(dir.DType()),
Name: dir.Name(),
})
}
return res, nil
}
// File implements both Node and Handle for the hello file.
type File struct {
Path string
Name string
}
const greeting = "hello, world\n"
func (file File) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
dirs, err := GlobalCephMgr.ListDir(file.Path + "/" + file.Name)
if err != nil {
return nil, err
}
var res []fuse.Dirent
for _, dir := range dirs {
res = append(res, fuse.Dirent{
Inode: uint64(dir.Inode()),
Type: fuse.DirentType(dir.DType()),
Name: dir.Name(),
})
}
return res, nil
}
func (file File) Attr(ctx context.Context, a *fuse.Attr) error {
cephAttr, err := GlobalCephMgr.GetFileAttr(file.Path, file.Name)
if err != nil {
return err
}
a.Inode = uint64(cephAttr.Inode)
a.Size = cephAttr.Size
a.Blocks = cephAttr.Blocks
a.Atime = time.Unix(cephAttr.Atime.Sec, 0)
a.Mtime = time.Unix(cephAttr.Mtime.Sec, 0)
a.Ctime = time.Unix(cephAttr.Ctime.Sec, 0)
a.Mode = os.FileMode(uint32(cephAttr.Mode) & (1<<10 - 1))
logrus.Debugf("cephAttr.Mode: %b", cephAttr.Mode)
logrus.Debugf("a.Mode: %b", a.Mode)
logrus.Debugf("mode: %d", cephAttr.Mode>>14)
if cephAttr.Mode>>14 == 1 {
a.Mode = os.ModeDir | a.Mode
}
a.Nlink = cephAttr.Nlink
a.Uid = cephAttr.Uid
a.Gid = cephAttr.Gid
a.Rdev = uint32(cephAttr.Rdev)
a.BlockSize = cephAttr.Blksize
/*
none Valid time.Duration // how long Attr can be cached
none Flags AttrFlags
*/
return nil
}
func (file File) Lookup(ctx context.Context, name string) (fs.Node, error) {
return File{
Path: file.Path + file.Name + "/",
Name: name,
}, nil
}
func (file File) ReadAll(ctx context.Context) ([]byte, error) {
return GlobalCephMgr.ReadFile(file.Path + file.Name)
}
// FS implements the hello world file system.
type FS struct{}
func (FS) Root() (fs.Node, error) {
return Dir{
Path: "/",
}, nil
}
func usage() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s MOUNTPOINT\n", os.Args[0])
flag.PrintDefaults()
}
var (
GlobalCephMgr *ceph_mgr.CephMgr
)
func main() {
flag.Usage = usage
flag.Parse()
if flag.NArg() != 1 {
usage()
os.Exit(2)
}
mountpoint := flag.Arg(0)
err := fuse.Unmount(mountpoint)
if err != nil {
logrus.Errorf("fuse.Unmount err:%v", err)
return
}
GlobalCephMgr, err = ceph_mgr.NewCephMgr()
if err != nil {
logrus.Errorf("ceph_mgr.NewCephMgr err:%v", err)
return
}
fuseConn, err := fuse.Mount(
mountpoint,
fuse.FSName("helloworld"),
fuse.Subtype("hellofs"),
)
if err != nil {
logrus.Errorf("fuse.Mount err:%v", err)
return
}
defer func() {
err = fuseConn.Close()
if err != nil {
logrus.Errorf("fuseConn.Close err:%v", err)
}
}()
err = fs.Serve(fuseConn, FS{})
if err != nil {
logrus.Errorf("fs.Serve err:%v", err)
return
}
}