-
Notifications
You must be signed in to change notification settings - Fork 96
/
pos_node.go
91 lines (72 loc) · 2.51 KB
/
pos_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
package fuse
import (
"context"
"fmt"
"io"
"syscall"
"bazil.org/fuse"
"bazil.org/fuse/fs"
"github.com/superfly/litefs"
)
// PosFileSize is the size, in bytes, of the "-pos" file.
const PosFileSize = 34
var _ fs.Node = (*PosNode)(nil)
var _ fs.NodeOpener = (*PosNode)(nil)
var _ fs.NodeForgetter = (*PosNode)(nil)
var _ fs.NodeListxattrer = (*PosNode)(nil)
var _ fs.NodeGetxattrer = (*PosNode)(nil)
var _ fs.NodeSetxattrer = (*PosNode)(nil)
var _ fs.NodeRemovexattrer = (*PosNode)(nil)
var _ fs.NodePoller = (*PosNode)(nil)
// PosNode represents a file that returns the current position of the database.
type PosNode struct {
fsys *FileSystem
db *litefs.DB
}
func newPosNode(fsys *FileSystem, db *litefs.DB) *PosNode {
return &PosNode{fsys: fsys, db: db}
}
func (n *PosNode) Attr(ctx context.Context, attr *fuse.Attr) error {
attr.Mode = 0666
attr.Size = uint64(PosFileSize)
attr.Uid = uint32(n.fsys.Uid)
attr.Gid = uint32(n.fsys.Gid)
attr.Valid = 0
return nil
}
func (n *PosNode) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) {
return n, nil
}
func (n *PosNode) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
pos := n.db.Pos()
data := fmt.Sprintf("%016x/%016x\n", pos.TXID, pos.PostApplyChecksum)
if req.Offset >= int64(len(data)) {
return io.EOF
}
// Size buffer to offset/size.
data = data[req.Offset:]
if len(data) > req.Size {
data = data[:req.Size]
}
resp.Data = []byte(data)
return nil
}
func (n *PosNode) Forget() { n.fsys.root.ForgetNode(n) }
// ENOSYS is a special return code for xattr requests that will be treated as a permanent failure for any such
// requests in the future without being sent to the filesystem.
// Source: https://github.com/libfuse/libfuse/blob/0b6d97cf5938f6b4885e487c3bd7b02144b1ea56/include/fuse_lowlevel.h#L811
func (n *PosNode) Listxattr(ctx context.Context, req *fuse.ListxattrRequest, resp *fuse.ListxattrResponse) error {
return fuse.ToErrno(syscall.ENOSYS)
}
func (n *PosNode) Getxattr(ctx context.Context, req *fuse.GetxattrRequest, resp *fuse.GetxattrResponse) error {
return fuse.ToErrno(syscall.ENOSYS)
}
func (n *PosNode) Setxattr(ctx context.Context, req *fuse.SetxattrRequest) error {
return fuse.ToErrno(syscall.ENOSYS)
}
func (n *PosNode) Removexattr(ctx context.Context, req *fuse.RemovexattrRequest) error {
return fuse.ToErrno(syscall.ENOSYS)
}
func (n *PosNode) Poll(ctx context.Context, req *fuse.PollRequest, resp *fuse.PollResponse) error {
return fuse.Errno(syscall.ENOSYS)
}