-
Notifications
You must be signed in to change notification settings - Fork 1
/
mount.go
195 lines (171 loc) · 3.72 KB
/
mount.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
package main
import (
"context"
"io"
"net/http"
"os"
"strconv"
"syscall"
"time"
"bazil.org/fuse"
"bazil.org/fuse/fs"
)
func mount(cfg *Config, mountpoint string) error {
clt, err := NewClient(cfg)
if err != nil {
return err
}
mountOptions := []fuse.MountOption{
fuse.AllowOther(),
fuse.NoAppleDouble(),
fuse.NoAppleXattr(),
fuse.ReadOnly(),
}
c, err := fuse.Mount(mountpoint, mountOptions...)
if err != nil {
return err
}
defer c.Close()
filesys := &FS{client: clt}
if err := fs.Serve(c, filesys); err != nil {
return err
}
<-c.Ready
return c.MountError
}
type FS struct {
client *Client
}
var _ fs.FS = (*FS)(nil)
func (f *FS) Root() (fs.Node, error) {
return &Root{client: f.client}, nil
}
type Root struct {
client *Client
}
var _ fs.Node = (*Root)(nil)
func (r *Root) Attr(ctx context.Context, a *fuse.Attr) error {
a.Mode = os.ModeDir | 0111
return nil
}
var _ fs.NodeRequestLookuper = (*Root)(nil)
func (r *Root) Lookup(ctx context.Context, req *fuse.LookupRequest, resp *fuse.LookupResponse) (fs.Node, error) {
key := req.Name
remotePath, err := r.client.getPath(key)
if cerr, ok := err.(*ClientError); ok && cerr.Code == http.StatusNotFound {
return nil, fuse.ENOENT
}
if err != nil {
return nil, err
}
hreq, err := http.NewRequestWithContext(ctx, http.MethodHead, remotePath.Path, nil)
if err != nil {
return nil, err
}
headResp, err := r.client.httpClient.Do(hreq)
if err != nil {
return nil, err
}
defer headResp.Body.Close()
if headResp.StatusCode == http.StatusNotFound {
return nil, fuse.ENOENT
}
err = checkResponseError(headResp)
if err != nil {
return nil, err
}
t, _ := time.Parse(time.RFC3339, remotePath.CreatedAt)
f := &File{
client: r.client,
path: remotePath.Path,
createdAt: t,
size: uint64(r.client.getContentLength(headResp)),
}
return f, nil
}
type File struct {
client *Client
path string
createdAt time.Time
size uint64
}
var _ fs.Node = (*File)(nil)
func (f *File) Attr(ctx context.Context, a *fuse.Attr) error {
a.Size = f.size
a.Mode = 0444
a.Mtime = f.createdAt
a.Ctime = f.createdAt
a.Crtime = f.createdAt
return nil
}
var _ fs.NodeOpener = (*File)(nil)
func (f *File) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) {
if !req.Flags.IsReadOnly() {
return nil, fuse.Errno(syscall.EROFS)
}
return &FileHandle{
client: f.client,
path: f.path,
size: f.size,
}, nil
}
type FileHandle struct {
client *Client
path string
size uint64
r io.ReadCloser
offset int64
}
var _ fs.Handle = (*FileHandle)(nil)
func (h *FileHandle) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
if req.Offset != h.offset {
err := h.close()
if err != nil {
return err
}
}
if h.r == nil {
if err := h.open(ctx, req.Offset); err != nil {
return err
}
}
buf := make([]byte, req.Size)
n, err := io.ReadFull(h.r, buf)
if err == io.ErrUnexpectedEOF || err == io.EOF {
err = nil
}
resp.Data = buf[:n]
h.offset += int64(n)
return err
}
func (h *FileHandle) open(ctx context.Context, offset int64) error {
req, err := http.NewRequest(http.MethodGet, h.path, nil)
if err != nil {
return err
}
req = req.WithContext(ctx)
req.Header.Set("bytes", strconv.FormatInt(h.offset, 10)+"-")
r, err := h.client.httpClient.Do(req)
if err != nil {
return err
}
err = checkResponseError(r)
if err != nil {
return err
}
h.r = r.Body
h.offset = offset
return nil
}
var _ fs.HandleReleaser = (*FileHandle)(nil)
func (h *FileHandle) Release(ctx context.Context, req *fuse.ReleaseRequest) error {
return h.close()
}
func (h *FileHandle) close() error {
if h.r == nil {
return nil
}
err := h.r.Close()
h.r = nil
return err
}