-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.go
157 lines (139 loc) · 3.37 KB
/
content.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
package main
import (
"context"
"fmt"
"io"
"os"
"path"
"syscall"
"time"
"bazil.org/fuse"
"bazil.org/fuse/fs"
"github.com/jinzhu/gorm"
)
type content struct {
id uint64
itype itemType
}
type virtualFile struct {
handle *os.File
}
var contentCache *fileCache = newCache()
func nameByID(db *gorm.DB, itemID uint64) (string, error) {
if cached, ok := contentCache.getByID(id(itemID)); ok {
if cached.missing {
return "", syscall.ENOENT
}
return cached.Name, nil
}
var result item
if db.Model(&item{}).Select("name").First(&result, "id = ?", itemID).RecordNotFound() {
contentCache.putMissingID(id(itemID))
return "", syscall.ENOENT
}
contentCache.putID(id(itemID), &result)
return result.Name, nil
}
func filePath(id uint64) (string, error) {
return filePathWithTx(db, id)
}
func filePathWithTx(tx *gorm.DB, id uint64) (string, error) {
name, err := nameByID(tx, id)
if err != nil {
return "", err
}
return filePathWithNameTx(id, name)
}
func filePathWithNameTx(id uint64, name string) (string, error) {
first := fmt.Sprintf("%06d", id/10000)
second := fmt.Sprintf("%02d", (id/100)%100)
dir := path.Join(storagePath, first, second)
os.MkdirAll(dir, 0755)
return path.Join(dir, fmt.Sprintf("%010d_%s", id, name)), nil
}
func (c content) filePathWithTx(tx *gorm.DB) (string, error) {
return filePathWithTx(tx, c.id)
}
func (c content) filePath() (string, error) {
return filePath(c.id)
}
func (c content) Attr(ctx context.Context, attr *fuse.Attr) error {
attr.Inode = c.id
if c.itype == file {
path, err := c.filePath()
if err != nil {
return err
}
fi, err := os.Stat(path)
if err != nil {
return syscall.ENOENT
}
attr.Mode = fi.Mode()
attr.Size = uint64(fi.Size())
attr.Atime = time.Now()
attr.Ctime = fi.ModTime()
attr.Mtime = fi.ModTime()
} else {
attr.Mode = os.ModeDir | 0755
attr.Size = 4096
}
attr.Uid = uid
attr.Gid = gid
return nil
}
func (c content) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) {
if req.Dir {
return nil, syscall.EINVAL
}
var path string
path, err := c.filePath()
if err != nil {
return nil, err
}
f, err := os.OpenFile(path, int(req.Flags), os.ModePerm)
if err != nil {
return nil, err
}
return virtualFile{handle: f}, nil
}
func (c content) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
if req.Valid.Size() {
path, err := c.filePath()
if err != nil {
return err
}
os.Truncate(path, int64(req.Size))
resp.Attr.Size = req.Size
}
c.Attr(ctx, &resp.Attr)
return nil
}
func (c content) Fsync(ctx context.Context, req *fuse.FsyncRequest) error {
// just for vim and such to work
return nil
}
func (v virtualFile) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
n, err := v.handle.ReadAt(resp.Data[:req.Size], req.Offset)
if err != nil && err != io.EOF {
return err
}
resp.Data = resp.Data[:n]
return nil
}
func (v virtualFile) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse) error {
var n int
var err error
if int(req.FileFlags)&os.O_APPEND != 0 {
n, err = v.handle.Write(req.Data)
} else {
n, err = v.handle.WriteAt(req.Data, req.Offset)
}
if err != nil {
return err
}
resp.Size = n
return nil
}
func (v virtualFile) Release(ctx context.Context, req *fuse.ReleaseRequest) error {
return v.handle.Close()
}