github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/libfuse/debug_server_file.go (about)

     1  // Copyright 2017 Keybase Inc. All rights reserved.
     2  // Use of this source code is governed by a BSD
     3  // license that can be found in the LICENSE file.
     4  //
     5  //go:build !windows
     6  // +build !windows
     7  
     8  package libfuse
     9  
    10  import (
    11  	"strconv"
    12  	"strings"
    13  
    14  	"bazil.org/fuse"
    15  	"bazil.org/fuse/fs"
    16  	"github.com/keybase/client/go/kbfs/libkbfs"
    17  	"golang.org/x/net/context"
    18  )
    19  
    20  // DebugServerFile represents a write-only file where any write of at
    21  // least one byte triggers either disabling or enabling the debug
    22  // server. For enabling, the port number to listen on (with localhost)
    23  // must be what is written, e.g.
    24  //
    25  //	echo 8080 > /keybase/.kbfs_enable_debug_server
    26  //
    27  // will spawn the HTTP debug server on port 8080.
    28  type DebugServerFile struct {
    29  	fs     *FS
    30  	enable bool
    31  }
    32  
    33  var _ fs.Node = (*DebugServerFile)(nil)
    34  
    35  // Attr implements the fs.Node interface for DebugServerFile.
    36  func (f *DebugServerFile) Attr(ctx context.Context, a *fuse.Attr) error {
    37  	a.Size = 0
    38  	a.Mode = 0222
    39  	return nil
    40  }
    41  
    42  var _ fs.Handle = (*DebugServerFile)(nil)
    43  
    44  var _ fs.HandleWriter = (*DebugServerFile)(nil)
    45  
    46  // Write implements the fs.HandleWriter interface for DebugServerFile.
    47  func (f *DebugServerFile) Write(ctx context.Context, req *fuse.WriteRequest,
    48  	resp *fuse.WriteResponse) (err error) {
    49  	f.fs.log.CDebugf(ctx, "DebugServerFile (enable: %t) Write", f.enable)
    50  	defer func() { err = f.fs.processError(ctx, libkbfs.WriteMode, err) }()
    51  	if len(req.Data) == 0 {
    52  		return nil
    53  	}
    54  
    55  	if f.enable {
    56  		portStr := strings.TrimSpace(string(req.Data))
    57  		port, err := strconv.ParseUint(portStr, 10, 16)
    58  		if err != nil {
    59  			return err
    60  		}
    61  
    62  		err = f.fs.enableDebugServer(ctx, uint16(port))
    63  		if err != nil {
    64  			return err
    65  		}
    66  	} else {
    67  		err := f.fs.disableDebugServer(ctx)
    68  		if err != nil {
    69  			return err
    70  		}
    71  	}
    72  
    73  	resp.Size = len(req.Data)
    74  	return nil
    75  }