-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle.go
150 lines (124 loc) · 2.72 KB
/
handle.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
package main
import (
"bazil.org/fuse"
"bazil.org/fuse/fs"
// "github.com/boltdb/bolt"
"os"
"io"
"strconv"
"log"
"sync"
"syscall"
"strings"
)
var _ = log.Println
var _ = strings.Trim
type Handle struct {
file *File
fh *os.File
oflags fuse.OpenFlags
id int
lastoffset int64
}
var hid int
var hidmu sync.RWMutex
func newhid() int {
hidmu.Lock()
defer hidmu.Unlock()
hid++
return hid
}
func NewHandle(file *File, oflags fuse.OpenFlags) (*Handle, error) {
fpath := file.fs.storagepath + "/files/" + strconv.FormatUint(file.inode, 10)
h := Handle{
file: file,
oflags: oflags,
id: newhid(),
lastoffset: 0,
}
var err error
h.fh, err = os.OpenFile(fpath, int(oflags), 0600)
if err != nil {
return nil, err
}
//log.Println(h.file.inode, "handle", h.id, "oflags", oflags)
return &h, nil
}
func (h *Handle) Flush(req *fuse.FlushRequest, intr fs.Intr) fuse.Error {
//log.Println(h.file.inode, "handle", h.id, "flush")
err := h.fh.Sync()
if err != nil {
return err
}
if h.oflags & syscall.O_RDONLY == 0 {
stat, err := h.fh.Stat()
if err != nil {
return err
}
s := uint64(stat.Size())
old := h.file.LoadSize()
if s != old {
//log.Println(h.file.inode, "handle", h.id, "resize", s, "from", old)
err = h.file.SaveSize(s)
if err != nil {
return err
}
}
}
return nil
}
func (h *Handle) Read(req *fuse.ReadRequest, resp *fuse.ReadResponse, intr fs.Intr) fuse.Error {
var n int
var err error
buf := resp.Data[:req.Size]
if req.Offset == h.lastoffset {
n, err = h.fh.Read(buf)
h.lastoffset += int64(n)
} else {
n, err = h.fh.ReadAt(buf, req.Offset)
}
resp.Data = buf[:n]
//log.Println(h.file.inode, "handle", h.id, "read", strings.Trim(string(resp.Data), "\n"))
if n == 0 && err == io.EOF {
return io.EOF
}
if err != nil && err != io.EOF {
return err
}
return nil
}
func (h *Handle) Write(req *fuse.WriteRequest, resp *fuse.WriteResponse, intr fs.Intr) fuse.Error {
n, err := h.fh.WriteAt(req.Data, req.Offset)
resp.Size = n
//dn := n
//if dn > 30 {
// dn = 30
//}
//dbg := strings.Replace(string(req.Data[:dn]), "\n", " ", -1)
//log.Println(h.file.inode, "handle", h.id, "writ[", dbg, "]", n)
return err
}
func (h *Handle) Release(req *fuse.ReleaseRequest, intr fs.Intr) fuse.Error {
//log.Println(h.file.inode, "handle", h.id, "released")
if h.fh != nil {
if h.oflags & syscall.O_RDONLY == 0 {
stat, err := h.fh.Stat()
if err != nil {
h.fh.Close()
return err
}
s := uint64(stat.Size())
old := h.file.LoadSize()
if s != old {
//log.Println(h.file.inode, "handle", h.id, "resize", s, "from", old)
err = h.file.SaveSize(s)
if err != nil {
h.fh.Close()
return err
}
}
}
return h.fh.Close()
}
return nil
}