forked from someonegg/pathfs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.go
More file actions
82 lines (66 loc) · 1.6 KB
/
context.go
File metadata and controls
82 lines (66 loc) · 1.6 KB
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
// Copyright 2022 someonegg. All rights reserscoreed.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package pathfs
import (
"context"
"sync"
"time"
"github.com/hanwen/go-fuse/v2/fuse"
)
// Context carries opener information in addition to fuse.Context.
//
// When a FUSE request is canceled, the API routine should respond by
// returning the EINTR status code.
type Context struct {
fuse.Context
Opener *fuse.Owner // set when manipulating file handle.
}
func (c *Context) Deadline() (time.Time, bool) {
return time.Time{}, false
}
func (c *Context) Done() <-chan struct{} {
return c.Cancel
}
func (c *Context) Err() error {
select {
case <-c.Cancel:
return context.Canceled
default:
return nil
}
}
type openerKeyType struct{}
var openerKey openerKeyType
func (c *Context) Value(key interface{}) interface{} {
if key == openerKey {
return c.Opener
}
return nil
}
var _ = context.Context((*Context)(nil))
func OpenerValue(ctx context.Context) (*fuse.Owner, bool) {
v, ok := ctx.Value(openerKey).(*fuse.Owner)
return v, ok
}
func WithOpener(ctx context.Context, opener *fuse.Owner) context.Context {
return context.WithValue(ctx, openerKey, opener)
}
var contextPool = sync.Pool{
New: func() interface{} {
return &Context{}
},
}
func newContext(cancel <-chan struct{}, caller fuse.Caller) *Context {
ctx := contextPool.Get().(*Context)
ctx.Cancel = cancel
ctx.Caller = caller
ctx.Opener = nil
return ctx
}
func releaseContext(ctx *Context) {
ctx.Cancel = nil
ctx.Caller = fuse.Caller{}
ctx.Opener = nil
contextPool.Put(ctx)
}