-
Notifications
You must be signed in to change notification settings - Fork 35
/
fshandler.go
417 lines (372 loc) · 12.2 KB
/
fshandler.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// Copyright 2018 The go-hep 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 xrootd // import "go-hep.org/x/hep/xrootd"
import (
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
"path"
"sync"
"go-hep.org/x/hep/xrootd/xrdfs"
"go-hep.org/x/hep/xrootd/xrdproto"
"go-hep.org/x/hep/xrootd/xrdproto/dirlist"
"go-hep.org/x/hep/xrootd/xrdproto/mkdir"
"go-hep.org/x/hep/xrootd/xrdproto/mv"
"go-hep.org/x/hep/xrootd/xrdproto/open"
"go-hep.org/x/hep/xrootd/xrdproto/read"
"go-hep.org/x/hep/xrootd/xrdproto/rm"
"go-hep.org/x/hep/xrootd/xrdproto/rmdir"
"go-hep.org/x/hep/xrootd/xrdproto/stat"
xrdsync "go-hep.org/x/hep/xrootd/xrdproto/sync"
"go-hep.org/x/hep/xrootd/xrdproto/truncate"
"go-hep.org/x/hep/xrootd/xrdproto/write"
"go-hep.org/x/hep/xrootd/xrdproto/xrdclose"
)
// fshandler implements server.Handler API by making request to the backing filesystem at basePath.
type fshandler struct {
Handler
basePath string
// map + RWMutex works a bit faster and with significant lower memory usage under Linux
// than sync.Map for given scenarios (write to map once per session and a lot of reads per session).
mu sync.RWMutex
sessions map[[16]byte]*srvSession
}
type srvSession struct {
mu sync.Mutex
handles map[xrdfs.FileHandle]*os.File
}
// NewFSHandler creates a Handler that passes requests to the backing filesystem at basePath.
func NewFSHandler(basePath string) Handler {
return &fshandler{
Handler: Default(),
basePath: basePath,
sessions: make(map[[16]byte]*srvSession),
}
}
// Dirlist implements server.Handler.Dirlist.
func (h *fshandler) Dirlist(sessionID [16]byte, request *dirlist.Request) (xrdproto.Marshaler, xrdproto.ResponseStatus) {
files, err := ioutil.ReadDir(path.Join(h.basePath, request.Path))
if err != nil {
return xrdproto.ServerError{
Code: xrdproto.IOError,
Message: fmt.Sprintf("An IO error occurred: %v", err),
}, xrdproto.Error
}
resp := &dirlist.Response{
WithStatInfo: request.Options&dirlist.WithStatInfo != 0,
Entries: make([]xrdfs.EntryStat, 0, len(files)),
}
for _, file := range files {
entry := xrdfs.EntryStatFrom(file)
entry.HasStatInfo = resp.WithStatInfo
resp.Entries = append(resp.Entries, entry)
}
return resp, xrdproto.Ok
}
// Open implements server.Handler.Open.
func (h *fshandler) Open(sessionID [16]byte, request *open.Request) (xrdproto.Marshaler, xrdproto.ResponseStatus) {
var flag int
if request.Options&xrdfs.OpenOptionsOpenRead != 0 {
flag |= os.O_RDONLY
}
if request.Options&xrdfs.OpenOptionsOpenUpdate != 0 {
flag |= os.O_RDWR
}
if request.Options&xrdfs.OpenOptionsOpenAppend != 0 {
flag |= os.O_APPEND
}
if request.Options&xrdfs.OpenOptionsNew != 0 || request.Options&xrdfs.OpenOptionsDelete != 0 {
flag |= os.O_CREATE
if request.Options&xrdfs.OpenOptionsDelete == 0 {
flag |= os.O_EXCL
} else {
flag |= os.O_TRUNC
}
}
filePath := path.Join(h.basePath, request.Path)
if request.Options&xrdfs.OpenOptionsMkPath != 0 {
if err := os.MkdirAll(path.Dir(filePath), os.FileMode(request.Mode)); err != nil {
return xrdproto.ServerError{
Code: xrdproto.IOError,
Message: fmt.Sprintf("An IO error occurred: %v", err),
}, xrdproto.Error
}
}
file, err := os.OpenFile(filePath, flag, os.FileMode(request.Mode))
if err != nil {
return xrdproto.ServerError{
Code: xrdproto.IOError,
Message: fmt.Sprintf("An IO error occurred: %v", err),
}, xrdproto.Error
}
h.mu.RLock()
sess, ok := h.sessions[sessionID]
h.mu.RUnlock()
if !ok {
h.mu.Lock()
// Check that there was no change in state during h.mu.RUnlock and h.mu.Lock.
sess, ok = h.sessions[sessionID]
if !ok {
sess = &srvSession{handles: make(map[xrdfs.FileHandle]*os.File)}
h.sessions[sessionID] = sess
}
h.mu.Unlock()
}
sess.mu.Lock()
defer sess.mu.Unlock()
var handle xrdfs.FileHandle
// TODO: make handle obtain more deterministic.
// Right now, we hope that even if 1000000000 of 256*256*256*256 handles are obtained by single user,
// we have appr. 0.7 probability to find a free handle by the random guess.
// Then, probability that no free handle is found by 100 tries is something near pow(0.3,100) = 1e-53.
for i := 0; i < 100; i++ {
// TODO: use crypto/rand under Windows (4 times faster than math/rand) if handle generation is on the hot path.
rand.Read(handle[:])
if _, dup := sess.handles[handle]; !dup {
resp := open.Response{FileHandle: handle}
if request.Options&xrdfs.OpenOptionsReturnStatus != 0 {
st, err := file.Stat()
if err != nil {
return xrdproto.ServerError{
Code: xrdproto.IOError,
Message: fmt.Sprintf("An IO error occurred: %v", err),
}, xrdproto.Error
}
es := xrdfs.EntryStatFrom(st)
resp.Stat = &es
if request.Options&xrdfs.OpenOptionsCompress == 0 {
resp.Compression = &xrdfs.FileCompression{}
}
}
// TODO: return compression info if requested.
sess.handles[handle] = file
return resp, xrdproto.Ok
}
}
return xrdproto.ServerError{
Code: xrdproto.InvalidRequest,
Message: "handle limit exceeded",
}, xrdproto.Error
}
// Close implements server.Handler.Close.
func (h *fshandler) Close(sessionID [16]byte, request *xrdclose.Request) (xrdproto.Marshaler, xrdproto.ResponseStatus) {
h.mu.RLock()
sess, ok := h.sessions[sessionID]
h.mu.RUnlock()
if !ok {
// This situation can appear if user tries to close without opening any file at all.
return xrdproto.ServerError{
Code: xrdproto.InvalidRequest,
Message: fmt.Sprintf("Invalid file handle: %v", request.Handle),
}, xrdproto.Error
}
sess.mu.Lock()
defer sess.mu.Unlock()
file, ok := sess.handles[request.Handle]
if !ok {
return xrdproto.ServerError{
Code: xrdproto.InvalidRequest,
Message: fmt.Sprintf("Invalid file handle: %v", request.Handle),
}, xrdproto.Error
}
delete(sess.handles, request.Handle)
err := file.Close()
if err != nil {
return xrdproto.ServerError{
Code: xrdproto.IOError,
Message: fmt.Sprintf("An IO error occurred: %v", err),
}, xrdproto.Error
}
return nil, xrdproto.Ok
}
// Read implements server.Handler.Read.
func (h *fshandler) Read(sessionID [16]byte, request *read.Request) (xrdproto.Marshaler, xrdproto.ResponseStatus) {
file := h.getFile(sessionID, request.Handle)
if file == nil {
return xrdproto.ServerError{
Code: xrdproto.InvalidRequest,
Message: fmt.Sprintf("Invalid file handle: %v", request.Handle),
}, xrdproto.Error
}
buf := make([]byte, request.Length)
n, err := file.ReadAt(buf, request.Offset)
if err != nil && err != io.EOF {
return xrdproto.ServerError{
Code: xrdproto.IOError,
Message: fmt.Sprintf("An IO error occurred: %v", err),
}, xrdproto.Error
}
return read.Response{Data: buf[:n]}, xrdproto.Ok
}
// Write implements server.Handler.Write.
func (h *fshandler) Write(sessionID [16]byte, request *write.Request) (xrdproto.Marshaler, xrdproto.ResponseStatus) {
file := h.getFile(sessionID, request.Handle)
if file == nil {
return xrdproto.ServerError{
Code: xrdproto.InvalidRequest,
Message: fmt.Sprintf("Invalid file handle: %v", request.Handle),
}, xrdproto.Error
}
_, err := file.WriteAt(request.Data, request.Offset)
if err != nil {
return xrdproto.ServerError{
Code: xrdproto.IOError,
Message: fmt.Sprintf("An IO error occurred: %v", err),
}, xrdproto.Error
}
return nil, xrdproto.Ok
}
func (h *fshandler) getFile(sessionID [16]byte, handle xrdfs.FileHandle) *os.File {
h.mu.RLock()
sess, ok := h.sessions[sessionID]
h.mu.RUnlock()
if !ok {
return nil
}
sess.mu.Lock()
defer sess.mu.Unlock()
file, ok := sess.handles[handle]
if !ok {
return nil
}
return file
}
// Stat implements server.Handler.Stat.
func (h *fshandler) Stat(sessionID [16]byte, request *stat.Request) (xrdproto.Marshaler, xrdproto.ResponseStatus) {
if request.Options&stat.OptionsVFS != 0 {
// TODO: handle virtual stat info.
return xrdproto.ServerError{
Code: xrdproto.InvalidRequest,
Message: "Stat request with OptionsVFS is not implemented",
}, xrdproto.Error
}
var fi os.FileInfo
var err error
if len(request.Path) == 0 {
file := h.getFile(sessionID, request.FileHandle)
if file == nil {
return xrdproto.ServerError{
Code: xrdproto.InvalidRequest,
Message: fmt.Sprintf("Invalid file handle: %v", request.FileHandle),
}, xrdproto.Error
}
fi, err = file.Stat()
} else {
fi, err = os.Stat(path.Join(h.basePath, request.Path))
}
if err != nil {
return xrdproto.ServerError{
Code: xrdproto.IOError,
Message: fmt.Sprintf("An IO error occurred: %v", err),
}, xrdproto.Error
}
return stat.DefaultResponse{EntryStat: xrdfs.EntryStatFrom(fi)}, xrdproto.Ok
}
// Truncate implements server.Handler.Truncate.
func (h *fshandler) Truncate(sessionID [16]byte, request *truncate.Request) (xrdproto.Marshaler, xrdproto.ResponseStatus) {
var err error
if len(request.Path) == 0 {
file := h.getFile(sessionID, request.Handle)
if file == nil {
return xrdproto.ServerError{
Code: xrdproto.InvalidRequest,
Message: fmt.Sprintf("Invalid file handle: %v", request.Handle),
}, xrdproto.Error
}
err = file.Truncate(request.Size)
} else {
err = os.Truncate(path.Join(h.basePath, request.Path), request.Size)
}
if err != nil {
return xrdproto.ServerError{
Code: xrdproto.IOError,
Message: fmt.Sprintf("An IO error occurred: %v", err),
}, xrdproto.Error
}
return nil, xrdproto.Ok
}
// Sync implements server.Handler.Sync.
func (h *fshandler) Sync(sessionID [16]byte, request *xrdsync.Request) (xrdproto.Marshaler, xrdproto.ResponseStatus) {
file := h.getFile(sessionID, request.Handle)
if file == nil {
return xrdproto.ServerError{
Code: xrdproto.InvalidRequest,
Message: fmt.Sprintf("Invalid file handle: %v", request.Handle),
}, xrdproto.Error
}
if err := file.Sync(); err != nil {
return xrdproto.ServerError{
Code: xrdproto.IOError,
Message: fmt.Sprintf("An IO error occurred: %v", err),
}, xrdproto.Error
}
return nil, xrdproto.Ok
}
// Rename implements server.Handler.Rename.
func (h *fshandler) Rename(sessionID [16]byte, request *mv.Request) (xrdproto.Marshaler, xrdproto.ResponseStatus) {
if err := os.Rename(path.Join(h.basePath, request.OldPath), path.Join(h.basePath, request.NewPath)); err != nil {
return xrdproto.ServerError{
Code: xrdproto.IOError,
Message: fmt.Sprintf("An IO error occurred: %v", err),
}, xrdproto.Error
}
return nil, xrdproto.Ok
}
// Mkdir implements server.Handler.Mkdir.
func (h *fshandler) Mkdir(sessionID [16]byte, request *mkdir.Request) (xrdproto.Marshaler, xrdproto.ResponseStatus) {
mkdirFunc := os.Mkdir
if request.Options&mkdir.OptionsMakePath != 0 {
mkdirFunc = os.MkdirAll
}
if err := mkdirFunc(path.Join(h.basePath, request.Path), os.FileMode(request.Mode)); err != nil {
return xrdproto.ServerError{
Code: xrdproto.IOError,
Message: fmt.Sprintf("An IO error occurred: %v", err),
}, xrdproto.Error
}
return nil, xrdproto.Ok
}
// Remove implements server.Handler.Remove.
func (h *fshandler) Remove(sessionID [16]byte, request *rm.Request) (xrdproto.Marshaler, xrdproto.ResponseStatus) {
if err := os.Remove(path.Join(h.basePath, request.Path)); err != nil {
return xrdproto.ServerError{
Code: xrdproto.IOError,
Message: fmt.Sprintf("An IO error occurred: %v", err),
}, xrdproto.Error
}
return nil, xrdproto.Ok
}
// RemoveDir implements server.Handler.RemoveDir.
func (h *fshandler) RemoveDir(sessionID [16]byte, request *rmdir.Request) (xrdproto.Marshaler, xrdproto.ResponseStatus) {
if err := os.Remove(path.Join(h.basePath, request.Path)); err != nil {
return xrdproto.ServerError{
Code: xrdproto.IOError,
Message: fmt.Sprintf("An IO error occurred: %v", err),
}, xrdproto.Error
}
return nil, xrdproto.Ok
}
// CloseSession implements server.Handler.CloseSession.
func (h *fshandler) CloseSession(sessionID [16]byte) error {
h.mu.Lock()
sess, ok := h.sessions[sessionID]
if !ok {
// That means that no files were opened in that session and we have nothing to clear.
h.mu.Unlock()
return nil
}
delete(h.sessions, sessionID)
h.mu.Unlock()
sess.mu.Lock()
defer sess.mu.Unlock()
var err error
for _, f := range sess.handles {
if cerr := f.Close(); cerr != nil && err == nil {
err = cerr
}
}
return err
}