-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ro_node.go
171 lines (144 loc) · 3.89 KB
/
ro_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
// 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"
)
// Reader is the data interface for a read only file.
type Reader interface {
io.ReaderAt
Size() (int64, error)
}
// RO is a read only file node.
type RO struct {
mu sync.Mutex
name string
attr
fs *FileSystem
openFlags fuse.OpenResponseFlags
dev Reader
}
var (
_ Node = (*RO)(nil)
_ fs.Node = (*RO)(nil)
_ fs.Handle = (*RO)(nil)
_ fs.NodeOpener = (*RO)(nil)
_ fs.HandleReleaser = (*RO)(nil)
_ fs.HandleReader = (*RO)(nil)
)
// NewRO returns a new RO file with the given name and file mode.
func NewRO(name string, mode os.FileMode, dev Reader) (*RO, error) {
return NewROFlags(name, mode, 0, dev)
}
// NewRO returns a new RO file with the given name and file mode.
// The provided flags are used when opening the RO node.
func NewROFlags(name string, mode os.FileMode, flags fuse.OpenResponseFlags, dev Reader) (*RO, error) {
if strings.Contains(name, string(filepath.Separator)) {
return nil, ErrBadName
}
return &RO{
name: name,
attr: attr{
mode: mode &^ (os.ModeDir | 0222),
},
dev: dev,
openFlags: flags,
}, nil
}
// MustNewRO returns a new RO with the given name and file mode. It
// will panic if name contains a filepath separator.
func MustNewRO(name string, mode os.FileMode, dev Reader) *RO {
return MustNewROFlags(name, mode, 0, dev)
}
// MustNewRO returns a new RO with the given name and file mode. It
// will panic if name contains a filepath separator.
// The provided flags are used when opening the RO node.
func MustNewROFlags(name string, mode os.FileMode, flags fuse.OpenResponseFlags, dev Reader) *RO {
ro, err := NewROFlags(name, mode, flags, dev)
if err != nil {
panic(err)
}
return ro
}
// Own sets the uid and gid of the file.
func (f *RO) Own(uid, gid uint32) *RO {
f.uid = uid
f.gid = gid
return f
}
// Name returns the name of the file.
func (f *RO) Name() string { return f.name }
// SetSys sets the file's containing file system.
func (f *RO) 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 *RO) Sys() *FileSystem {
f.mu.Lock()
defer f.mu.Unlock()
return f.fs
}
// Invalidate invalidates the kernel cache of the file.
func (f *RO) 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 *RO) 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 *RO) 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 RO Reader device is an io.Closer, its Close method is called.
func (f *RO) 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
}
// Read satisfies the bazil.org/fuse/fs.HandleReader interface.
func (f *RO) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
f.mu.Lock()
defer f.mu.Unlock()
f.atime = f.fs.now()
n, err := f.dev.ReadAt(resp.Data[:req.Size], int64(req.Offset))
resp.Data = resp.Data[:n]
if err == io.EOF {
return nil
}
return err
}