forked from keybase/kbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug_server_file.go
72 lines (60 loc) · 1.72 KB
/
debug_server_file.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
// Copyright 2017 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package libfuse
import (
"strconv"
"strings"
"bazil.org/fuse"
"bazil.org/fuse/fs"
"github.com/keybase/kbfs/libkbfs"
"golang.org/x/net/context"
)
// DebugServerFile represents a write-only file where any write of at
// least one byte triggers either disabling or enabling the debug
// server. For enabling, the port number to listen on (with localhost)
// must be what is written, e.g.
//
// echo 8080 > /keybase/.kbfs_enable_debug_server
//
// will spawn the HTTP debug server on port 8080.
type DebugServerFile struct {
fs *FS
enable bool
}
var _ fs.Node = (*DebugServerFile)(nil)
// Attr implements the fs.Node interface for DebugServerFile.
func (f *DebugServerFile) Attr(ctx context.Context, a *fuse.Attr) error {
a.Size = 0
a.Mode = 0222
return nil
}
var _ fs.Handle = (*DebugServerFile)(nil)
var _ fs.HandleWriter = (*DebugServerFile)(nil)
// Write implements the fs.HandleWriter interface for DebugServerFile.
func (f *DebugServerFile) Write(ctx context.Context, req *fuse.WriteRequest,
resp *fuse.WriteResponse) (err error) {
f.fs.log.CDebugf(ctx, "DebugServerFile (enable: %t) Write", f.enable)
defer func() { err = f.fs.processError(ctx, libkbfs.WriteMode, err) }()
if len(req.Data) == 0 {
return nil
}
if f.enable {
portStr := strings.TrimSpace(string(req.Data))
port, err := strconv.ParseUint(portStr, 10, 16)
if err != nil {
return err
}
err = f.fs.enableDebugServer(ctx, uint16(port))
if err != nil {
return err
}
} else {
err := f.fs.disableDebugServer(ctx)
if err != nil {
return err
}
}
resp.Size = len(req.Data)
return nil
}