-
Notifications
You must be signed in to change notification settings - Fork 1
/
ossfs.go
126 lines (112 loc) · 3.26 KB
/
ossfs.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
package main
//Now only implements Oss Read file operation
import (
"flag"
"fmt"
"github.com/hanwen/go-fuse/fuse"
"github.com/hanwen/go-fuse/fuse/nodefs"
"github.com/hanwen/go-fuse/fuse/pathfs"
"log"
// "os"
"oss"
"sync"
// "strings"
// "sys"
// "time"
)
type OssFs struct {
pathfs.FileSystem
ossClient *oss.Client
bucket string
}
func check(e error) {
if e != nil {
fmt.Println(e)
panic(e)
}
}
func (me *OssFs) GetAttr(name string, context *fuse.Context) (*fuse.Attr, fuse.Status) {
log.Printf("name:%s", name)
if name == "/" || name == "\\" || name == "" {
return &fuse.Attr{
Mode: fuse.S_IFDIR | 0755,
}, fuse.OK
}
if len(name) > 0 {
log.Printf("Get attr name %s \n", name)
objectInfo, err := me.ossClient.GetObjectInfo(me.bucket, name)
if err != nil {
log.Printf("%s \n", err)
return &fuse.Attr{
Mode: fuse.S_IFDIR | 0777,
Size: uint64(0),
}, fuse.OK
}
log.Printf("Get attr bucket %s, name %s \n", me.bucket, name)
log.Printf("%s's size is %d", name, objectInfo.Size)
fmt.Printf("This file's size is:%d \n", objectInfo.Size)
if objectInfo.Size == 0 {
return &fuse.Attr{
Mode: fuse.S_IFDIR | 0777,
Size: uint64(0),
}, fuse.OK
} else {
return &fuse.Attr{
Mode: fuse.S_IFREG | 0777,
Size: uint64(objectInfo.Size),
}, fuse.OK
}
}
log.Printf("Get Attr return nil!%s \n", name)
return nil, fuse.ENOENT
}
func (me *OssFs) OpenDir(name string, context *fuse.Context) (c []fuse.DirEntry, code fuse.Status) {
dirEntrys := listDir(name, me.ossClient, me.bucket)
if dirEntrys != nil {
log.Printf("dirEntrys length:%d name:%s direntrys:%s\n", len(dirEntrys), name, dirEntrys)
return dirEntrys, fuse.OK
}
return nil, fuse.EIO
}
var fileMap = map[uint32]nodefs.File{}
func (me *OssFs) Open(name string, flags uint32, context *fuse.Context) (file nodefs.File, code fuse.Status) {
// log.Println("Start to call Open method!name:", name, "flags:", flags)
if x, ok := fileMap[flags]; !ok {
objectInfo, err := me.ossClient.GetObjectInfo(me.bucket, name)
log.Printf("%s's size is %d", name, objectInfo.Size)
if err != nil {
return nil, fuse.ENOENT
}
fmt.Printf("This file's size is:%d \n", objectInfo.Size)
log.Printf("Start to call Open method.name:%s,flags:%s \n", name, flags, objectInfo.Size)
file = NewOssFile(me.bucket, name, me.ossClient, new(sync.Mutex), objectInfo.Size, nil)
return file, fuse.OK
} else {
return x, fuse.OK
}
}
func main() {
help := flag.String("h", "", "help document")
bucket := flag.String("b", "frio-tegong", "set oss bucket to use")
mountPath := flag.String("p", "", "mount path")
endPoint := flag.String("e", "oss-cn-hangzhou.aliyuncs.com", "oss endpoint to use")
key := flag.String("k", "", "oss key to use")
secret := flag.String("s", "", "oss secret to use")
flag.Parse()
if len(*help) > 0 {
fmt.Println(flag.Args())
return
}
if len(*mountPath) == 0 {
fmt.Errorf("mount path must not be null", "")
return
}
cfg := &oss.Config{Endpoint: *endPoint, Key: *key, Secret: *secret}
ossClient, err := oss.NewClient(cfg)
check(err)
nfs := pathfs.NewPathNodeFs(&OssFs{FileSystem: pathfs.NewDefaultFileSystem(),
bucket: *bucket, ossClient: ossClient}, nil)
server, _, err := nodefs.MountRoot(*mountPath, nfs.Root(), nil)
check(err)
server.Serve()
}