forked from keybase/client
/
profilelist.go
117 lines (94 loc) · 2.77 KB
/
profilelist.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
// Copyright 2016 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
//
// +build !windows
package libfuse
import (
"io/ioutil"
"os"
"strings"
"time"
"bazil.org/fuse"
"bazil.org/fuse/fs"
"github.com/keybase/client/go/kbfs/libfs"
"github.com/keybase/client/go/kbfs/libkbfs"
"golang.org/x/net/context"
)
// timedProfileFile represents a file whose contents are determined by
// taking a profile for some duration.
type timedProfileFile struct {
pfs libfs.ProfileFS
name string
}
var _ fs.Node = timedProfileFile{}
func (tpf timedProfileFile) Attr(ctx context.Context, a *fuse.Attr) error {
// Have a low non-zero value for Valid to avoid being swamped
// with requests.
a.Valid = 1 * time.Second
now := time.Now()
a.Size = 0
a.Mtime = now
a.Ctime = now
a.Mode = 0444
return nil
}
var _ fs.NodeOpener = timedProfileFile{}
func (tpf timedProfileFile) Open(ctx context.Context,
req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) {
f, err := tpf.pfs.OpenWithContext(ctx, tpf.name)
if err != nil {
return nil, err
}
buf, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
resp.Flags |= fuse.OpenDirectIO
return fs.DataHandle(buf), nil
}
// ProfileList is a node that can list all of the available profiles.
type ProfileList struct {
config libkbfs.Config
}
var _ fs.Node = ProfileList{}
// Attr implements the fs.Node interface.
func (ProfileList) Attr(_ context.Context, a *fuse.Attr) error {
a.Mode = os.ModeDir | 0755
return nil
}
var _ fs.NodeRequestLookuper = ProfileList{}
// Lookup implements the fs.NodeRequestLookuper interface.
func (pl ProfileList) Lookup(_ context.Context, req *fuse.LookupRequest, resp *fuse.LookupResponse) (node fs.Node, err error) {
pfs := libfs.NewProfileFS(pl.config)
// Handle timed profiles first.
if strings.HasPrefix(req.Name, libfs.CPUProfilePrefix) ||
strings.HasPrefix(req.Name, libfs.TraceProfilePrefix) {
return timedProfileFile{pfs, req.Name}, nil
}
f := libfs.ProfileGet(req.Name)
if f == nil {
return nil, fuse.ENOENT
}
resp.EntryValid = 0
return &SpecialReadFile{read: f}, nil
}
var _ fs.Handle = ProfileList{}
var _ fs.HandleReadDirAller = ProfileList{}
// ReadDirAll implements the ReadDirAll interface.
func (pl ProfileList) ReadDirAll(_ context.Context) (res []fuse.Dirent, err error) {
profiles := libfs.ListProfileNames()
res = make([]fuse.Dirent, 0, len(profiles))
for _, p := range profiles {
res = append(res, fuse.Dirent{
Type: fuse.DT_File,
Name: p,
})
}
return res, nil
}
var _ fs.NodeRemover = (*FolderList)(nil)
// Remove implements the fs.NodeRemover interface for ProfileList.
func (ProfileList) Remove(_ context.Context, req *fuse.RemoveRequest) (err error) {
return fuse.EPERM
}