-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sisyphus.go
204 lines (178 loc) · 4.51 KB
/
sisyphus.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
// 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 provides tools to build a simple user FUSE-based sysfs-like interface.
package sisyphus
import (
"errors"
"io"
"os"
"sync"
"syscall"
"time"
"bazil.org/fuse"
"bazil.org/fuse/fs"
)
// ErrBadName is returned when a new Node is created with a base name
// that contains a filepath separator.
var ErrBadName = errors.New("sisyphus: base contains filepath separator")
// server is a FUSE server for a FileSystem.
type server struct {
mnt string
fuse *fs.Server
conn *fuse.Conn
mu sync.Mutex
err error
}
// Serve starts a server for filesys mounted at the specified mount point.
// It is the responsibility of the caller to close the returned io.Closer
// when the server is no longer required.
func Serve(mnt string, filesys *FileSystem, config *fs.Config, mntopts ...fuse.MountOption) (io.Closer, error) {
c, err := fuse.Mount(mnt, mntopts...)
if err != nil {
return nil, err
}
s := &server{mnt: mnt, fuse: fs.New(c, config), conn: c}
filesys.server = s
go func() {
err = s.fuse.Serve(filesys)
if err != nil {
s.mu.Lock()
s.err = err
s.mu.Unlock()
}
}()
<-s.conn.Ready
if s.conn.MountError != nil {
return nil, s.conn.MountError
}
return s, nil
}
// Close closes the server.
func (s *server) Close() error {
defer s.conn.Close()
s.mu.Lock()
defer s.mu.Unlock()
if s.err != nil {
return s.err
}
return fuse.Unmount(s.mnt)
}
// Bytes is a ReadWriter backed by a byte slice.
type Bytes []byte
// NewBytes returns a new Bytes backed by the provided data.
func NewBytes(data []byte) *Bytes {
b := Bytes(data)
return &b
}
// ReadAt satisfies the io.ReaderAt interface.
func (f *Bytes) ReadAt(b []byte, offset int64) (int, error) {
if len(b) == 0 {
return 0, nil
}
if offset >= int64(len(*f)) {
return 0, io.EOF
}
n := copy(b, (*f)[offset:])
if n <= len(b) {
return n, io.EOF
}
return n, nil
}
// Truncate truncates the Bytes at n bytes from the beginning of the slice.
func (f *Bytes) Truncate(n int64) error {
if n < 0 || n > int64(len(*f)) {
return syscall.EINVAL
}
tail := (*f)[n:cap(*f)]
for i := range tail {
tail[i] = 0
}
*f = (*f)[:n]
return nil
}
// WriteAt satisfies the io.WriterAt interface.
func (f *Bytes) WriteAt(b []byte, off int64) (int, error) {
if off >= int64(cap(*f)) {
t := make([]byte, off+int64(len(b)))
copy(t, *f)
*f = t
}
*f = (*f)[:off]
*f = append(*f, b...)
return len(b), nil
}
// Size returns the length of the backing data and a nil error.
func (f *Bytes) Size() (int64, error) { return int64(len(*f)), nil }
// Func is a Writer backed by a user defined function.
type Func func([]byte, int64) (int, error)
// WriteAt satisfies the io.WriterAt interface.
func (f Func) WriteAt(b []byte, off int64) (int, error) {
if f == nil {
return 0, syscall.EBADFD
}
return f(b, off)
}
// Truncate is a no-op.
func (f Func) Truncate(_ int64) error { return nil }
// Size returns zero and a nil error.
func (f Func) Size() (int64, error) { return 0, nil }
// String is a Reader backed by a string.
type String string
// ReadAt satisfies the io.ReaderAt interface.
func (s String) ReadAt(b []byte, off int64) (int, error) {
if off < 0 {
return 0, syscall.EINVAL
}
if off >= int64(len(s)) {
return 0, io.EOF
}
n := copy(b, s[off:])
if n <= len(b) {
return n, io.EOF
}
return n, nil
}
// Size returns the length of the backing string and a nil error.
func (s String) Size() (int64, error) { return int64(len(s)), nil }
// attr is the set of node attributes/
type attr struct {
mode os.FileMode
uid uint32
gid uint32
atime time.Time
mtime time.Time
ctime time.Time
}
// copyAttr copies node attributes to a fuse.Attr.
func copyAttr(dst *fuse.Attr, src attr) {
dst.Mode = src.mode
dst.Uid = src.uid
dst.Gid = src.gid
dst.Atime = src.atime
dst.Mtime = src.mtime
dst.Ctime = src.ctime
}
// setAttr copies node attributes from a *fuse.SetattrRequest.
func setAttr(dst *attr, resp *fuse.SetattrResponse, src *fuse.SetattrRequest) {
if src.Valid&fuse.SetattrMode != 0 {
resp.Attr.Mode = src.Mode
dst.mode = src.Mode
}
if src.Valid&fuse.SetattrUid != 0 {
resp.Attr.Uid = src.Uid
dst.uid = src.Uid
}
if src.Valid&fuse.SetattrGid != 0 {
resp.Attr.Gid = src.Gid
dst.gid = src.Gid
}
if src.Valid&fuse.SetattrAtime != 0 {
resp.Attr.Atime = src.Atime
dst.atime = src.Atime
}
if src.Valid&fuse.SetattrMtime != 0 {
resp.Attr.Mtime = src.Mtime
dst.mtime = src.Mtime
}
}