-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwo_node.go
206 lines (174 loc) · 4.67 KB
/
wo_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
198
199
200
201
202
203
204
205
206
// Copyright ©2016 The ev3go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package sisyphus
import (
"context"
"io"
"os"
"path/filepath"
"strings"
"sync"
"syscall"
"time"
"bazil.org/fuse"
"bazil.org/fuse/fs"
)
// Writer is the data interface for a write only file.
type Writer interface {
io.WriterAt
Truncate(int64) error
Size() (int64, error)
}
// WO is a write only file node.
type WO struct {
mu sync.Mutex
name string
attr
fs *FileSystem
openFlags fuse.OpenResponseFlags
dev Writer
}
var (
_ Node = (*WO)(nil)
_ fs.Node = (*WO)(nil)
_ fs.Handle = (*WO)(nil)
_ fs.NodeOpener = (*WO)(nil)
_ fs.HandleReleaser = (*WO)(nil)
_ fs.HandleWriter = (*WO)(nil)
_ fs.HandleFlusher = (*WO)(nil)
_ fs.NodeSetattrer = (*WO)(nil)
)
// NewWO returns a new WO file with the given name and file mode.
func NewWO(name string, mode os.FileMode, dev Writer) (*WO, error) {
return NewWOFlags(name, mode, 0, dev)
}
// NewWOFlags returns a new WO file with the given name and file mode.
// The provided flags are used when opening the WO node.
func NewWOFlags(name string, mode os.FileMode, flags fuse.OpenResponseFlags, dev Writer) (*WO, error) {
if strings.Contains(name, string(filepath.Separator)) {
return nil, ErrBadName
}
return &WO{
name: name,
attr: attr{
mode: mode &^ (os.ModeDir | 0444),
},
openFlags: flags,
dev: dev,
}, nil
}
// MustNewWO returns a new WO with the given name and file mode. It
// will panic if name contains a filepath separator.
func MustNewWO(name string, mode os.FileMode, dev Writer) *WO {
return MustNewWOFlags(name, mode, 0, dev)
}
// MustNewWOFlags returns a new WO with the given name and file mode. It
// will panic if name contains a filepath separator.
// The provided flags are used when opening the WO node.
func MustNewWOFlags(name string, mode os.FileMode, flags fuse.OpenResponseFlags, dev Writer) *WO {
wo, err := NewWOFlags(name, mode, flags, dev)
if err != nil {
panic(err)
}
return wo
}
// Own sets the uid and gid of the file.
func (f *WO) Own(uid, gid uint32) *WO {
f.uid = uid
f.gid = gid
return f
}
// Name returns the name of the file.
func (f *WO) Name() string { return f.name }
// SetSys sets the file's containing file system.
func (f *WO) SetSys(filesys *FileSystem) {
f.mu.Lock()
f.fs = filesys
var now time.Time
if filesys != nil {
now = filesys.now()
}
f.ctime = now
f.atime = now
f.mtime = now
f.mu.Unlock()
}
// Sys returns the file's containing filesystem.
func (f *WO) Sys() *FileSystem {
f.mu.Lock()
defer f.mu.Unlock()
return f.fs
}
// Invalidate invalidates the kernel cache of the file.
func (f *WO) Invalidate() error {
f.mu.Lock()
defer f.mu.Unlock()
return f.fs.Invalidate(f)
}
// Attr satisfies the bazil.org/fuse/fs.Node interface.
func (f *WO) Attr(ctx context.Context, a *fuse.Attr) error {
f.mu.Lock()
defer f.mu.Unlock()
copyAttr(a, f.attr)
size, err := f.dev.Size()
if err != nil {
return errno{error: err, errno: fuse.Errno(syscall.EBADFD)}
}
a.Size = uint64(size)
return nil
}
// Open satisfies the bazil.org/fuse/fs.NodeOpener interface.
func (f *WO) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) {
resp.Flags |= fuse.OpenDirectIO
return f, nil
}
// Release satisfies the bazil.org/fuse/fs.HandleReleaser interface.
// If the WO Writer device is an io.Closer, its Close method is called.
func (f *WO) Release(ctx context.Context, req *fuse.ReleaseRequest) error {
f.mu.Lock()
defer f.mu.Unlock()
if c, ok := f.dev.(io.Closer); ok {
return c.Close()
}
return nil
}
// Write satisfies the bazil.org/fuse/fs.HandleWriter interface.
func (f *WO) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse) error {
f.mu.Lock()
defer f.mu.Unlock()
f.mtime = f.fs.now()
var err error
resp.Size, err = f.dev.WriteAt(req.Data, req.Offset)
return err
}
// Flush satisfies the bazil.org/fuse/fs.HandleFlusher interface.
func (f *WO) Flush(ctx context.Context, req *fuse.FlushRequest) error {
f.mu.Lock()
defer f.mu.Unlock()
type syncer interface {
Sync() error
}
if s, ok := f.dev.(syncer); ok {
return s.Sync()
}
return nil
}
// Setattr satisfies the bazil.org/fuse/fs.NodeSetattrer interface.
func (f *WO) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
f.mu.Lock()
defer f.mu.Unlock()
if req.Valid&fuse.SetattrSize != 0 {
err := f.dev.Truncate(int64(req.Size))
if err != nil {
return err
}
size, err := f.dev.Size()
if err != nil {
return err
}
resp.Attr.Size = uint64(size)
}
setAttr(&f.attr, resp, req)
return nil
}