serve nfs: rename types and methods which should be internal

This commit is contained in:
Nick Craig-Wood 2024-06-24 16:32:09 +01:00
parent 50d42babd8
commit 574378e871
3 changed files with 41 additions and 40 deletions

View File

@ -15,9 +15,9 @@ import (
nfshelper "github.com/willscott/go-nfs/helpers" nfshelper "github.com/willscott/go-nfs/helpers"
) )
// NewBackendAuthHandler creates a handler for the provided filesystem // NewHandler creates a handler for the provided filesystem
func NewBackendAuthHandler(vfs *vfs.VFS, opt *Options) nfs.Handler { func NewHandler(vfs *vfs.VFS, opt *Options) nfs.Handler {
handler := &BackendAuthHandler{ handler := &Handler{
vfs: vfs, vfs: vfs,
opt: opt, opt: opt,
} }
@ -26,15 +26,15 @@ func NewBackendAuthHandler(vfs *vfs.VFS, opt *Options) nfs.Handler {
return handler return handler
} }
// BackendAuthHandler returns a NFS backing that exposes a given file system in response to all mount requests. // Handler returns a NFS backing that exposes a given file system in response to all mount requests.
type BackendAuthHandler struct { type Handler struct {
vfs *vfs.VFS vfs *vfs.VFS
opt *Options opt *Options
cache nfs.Handler cache nfs.Handler
} }
// Mount backs Mount RPC Requests, allowing for access control policies. // Mount backs Mount RPC Requests, allowing for access control policies.
func (h *BackendAuthHandler) Mount(ctx context.Context, conn net.Conn, req nfs.MountRequest) (status nfs.MountStatus, hndl billy.Filesystem, auths []nfs.AuthFlavor) { func (h *Handler) Mount(ctx context.Context, conn net.Conn, req nfs.MountRequest) (status nfs.MountStatus, hndl billy.Filesystem, auths []nfs.AuthFlavor) {
status = nfs.MountStatusOk status = nfs.MountStatusOk
hndl = &FS{vfs: h.vfs} hndl = &FS{vfs: h.vfs}
auths = []nfs.AuthFlavor{nfs.AuthFlavorNull} auths = []nfs.AuthFlavor{nfs.AuthFlavorNull}
@ -42,7 +42,7 @@ func (h *BackendAuthHandler) Mount(ctx context.Context, conn net.Conn, req nfs.M
} }
// Change provides an interface for updating file attributes. // Change provides an interface for updating file attributes.
func (h *BackendAuthHandler) Change(fs billy.Filesystem) billy.Change { func (h *Handler) Change(fs billy.Filesystem) billy.Change {
if c, ok := fs.(billy.Change); ok { if c, ok := fs.(billy.Change); ok {
return c return c
} }
@ -50,7 +50,7 @@ func (h *BackendAuthHandler) Change(fs billy.Filesystem) billy.Change {
} }
// FSStat provides information about a filesystem. // FSStat provides information about a filesystem.
func (h *BackendAuthHandler) FSStat(ctx context.Context, f billy.Filesystem, s *nfs.FSStat) error { func (h *Handler) FSStat(ctx context.Context, f billy.Filesystem, s *nfs.FSStat) error {
total, _, free := h.vfs.Statfs() total, _, free := h.vfs.Statfs()
s.TotalSize = uint64(total) s.TotalSize = uint64(total)
s.FreeSize = uint64(free) s.FreeSize = uint64(free)
@ -59,28 +59,28 @@ func (h *BackendAuthHandler) FSStat(ctx context.Context, f billy.Filesystem, s *
} }
// ToHandle handled by CachingHandler // ToHandle handled by CachingHandler
func (h *BackendAuthHandler) ToHandle(f billy.Filesystem, s []string) []byte { func (h *Handler) ToHandle(f billy.Filesystem, s []string) []byte {
return h.cache.ToHandle(f, s) return h.cache.ToHandle(f, s)
} }
// FromHandle handled by CachingHandler // FromHandle handled by CachingHandler
func (h *BackendAuthHandler) FromHandle(b []byte) (billy.Filesystem, []string, error) { func (h *Handler) FromHandle(b []byte) (billy.Filesystem, []string, error) {
return h.cache.FromHandle(b) return h.cache.FromHandle(b)
} }
// HandleLimit handled by cachingHandler // HandleLimit handled by cachingHandler
func (h *BackendAuthHandler) HandleLimit() int { func (h *Handler) HandleLimit() int {
return h.opt.HandleLimit return h.opt.HandleLimit
} }
// InvalidateHandle is called on removes or renames // InvalidateHandle is called on removes or renames
func (h *BackendAuthHandler) InvalidateHandle(billy.Filesystem, []byte) error { func (h *Handler) InvalidateHandle(billy.Filesystem, []byte) error {
return nil return nil
} }
func newHandler(vfs *vfs.VFS, opt *Options) nfs.Handler { func newHandler(vfs *vfs.VFS, opt *Options) nfs.Handler {
handler := NewBackendAuthHandler(vfs, opt) handler := NewHandler(vfs, opt)
nfs.SetLogger(&LogIntercepter{Level: nfs.DebugLevel}) nfs.SetLogger(&logIntercepter{Level: nfs.DebugLevel})
return handler return handler
} }
@ -110,20 +110,20 @@ func onUnmount() {
} }
} }
// LogIntercepter intercepts noisy go-nfs logs and reroutes them to DEBUG // logIntercepter intercepts noisy go-nfs logs and reroutes them to DEBUG
type LogIntercepter struct { type logIntercepter struct {
Level nfs.LogLevel Level nfs.LogLevel
} }
// Intercept intercepts go-nfs logs and calls fs.Debugf instead // Intercept intercepts go-nfs logs and calls fs.Debugf instead
func (l *LogIntercepter) Intercept(args ...interface{}) { func (l *logIntercepter) Intercept(args ...interface{}) {
args = append([]interface{}{"[NFS DEBUG] "}, args...) args = append([]interface{}{"[NFS DEBUG] "}, args...)
argsS := fmt.Sprint(args...) argsS := fmt.Sprint(args...)
fs.Debugf(nil, "%v", argsS) fs.Debugf(nil, "%v", argsS)
} }
// Interceptf intercepts go-nfs logs and calls fs.Debugf instead // Interceptf intercepts go-nfs logs and calls fs.Debugf instead
func (l *LogIntercepter) Interceptf(format string, args ...interface{}) { func (l *logIntercepter) Interceptf(format string, args ...interface{}) {
argsS := fmt.Sprint(args...) argsS := fmt.Sprint(args...)
// bit of a workaround... the real fix is probably https://github.com/willscott/go-nfs/pull/28 // bit of a workaround... the real fix is probably https://github.com/willscott/go-nfs/pull/28
if strings.Contains(argsS, "mount.Umnt") { if strings.Contains(argsS, "mount.Umnt") {
@ -134,96 +134,96 @@ func (l *LogIntercepter) Interceptf(format string, args ...interface{}) {
} }
// Debug reroutes go-nfs Debug messages to Intercept // Debug reroutes go-nfs Debug messages to Intercept
func (l *LogIntercepter) Debug(args ...interface{}) { func (l *logIntercepter) Debug(args ...interface{}) {
l.Intercept(args...) l.Intercept(args...)
} }
// Debugf reroutes go-nfs Debugf messages to Interceptf // Debugf reroutes go-nfs Debugf messages to Interceptf
func (l *LogIntercepter) Debugf(format string, args ...interface{}) { func (l *logIntercepter) Debugf(format string, args ...interface{}) {
l.Interceptf(format, args...) l.Interceptf(format, args...)
} }
// Error reroutes go-nfs Error messages to Intercept // Error reroutes go-nfs Error messages to Intercept
func (l *LogIntercepter) Error(args ...interface{}) { func (l *logIntercepter) Error(args ...interface{}) {
l.Intercept(args...) l.Intercept(args...)
} }
// Errorf reroutes go-nfs Errorf messages to Interceptf // Errorf reroutes go-nfs Errorf messages to Interceptf
func (l *LogIntercepter) Errorf(format string, args ...interface{}) { func (l *logIntercepter) Errorf(format string, args ...interface{}) {
l.Interceptf(format, args...) l.Interceptf(format, args...)
} }
// Fatal reroutes go-nfs Fatal messages to Intercept // Fatal reroutes go-nfs Fatal messages to Intercept
func (l *LogIntercepter) Fatal(args ...interface{}) { func (l *logIntercepter) Fatal(args ...interface{}) {
l.Intercept(args...) l.Intercept(args...)
} }
// Fatalf reroutes go-nfs Fatalf messages to Interceptf // Fatalf reroutes go-nfs Fatalf messages to Interceptf
func (l *LogIntercepter) Fatalf(format string, args ...interface{}) { func (l *logIntercepter) Fatalf(format string, args ...interface{}) {
l.Interceptf(format, args...) l.Interceptf(format, args...)
} }
// GetLevel returns the nfs.LogLevel // GetLevel returns the nfs.LogLevel
func (l *LogIntercepter) GetLevel() nfs.LogLevel { func (l *logIntercepter) GetLevel() nfs.LogLevel {
return l.Level return l.Level
} }
// Info reroutes go-nfs Info messages to Intercept // Info reroutes go-nfs Info messages to Intercept
func (l *LogIntercepter) Info(args ...interface{}) { func (l *logIntercepter) Info(args ...interface{}) {
l.Intercept(args...) l.Intercept(args...)
} }
// Infof reroutes go-nfs Infof messages to Interceptf // Infof reroutes go-nfs Infof messages to Interceptf
func (l *LogIntercepter) Infof(format string, args ...interface{}) { func (l *logIntercepter) Infof(format string, args ...interface{}) {
l.Interceptf(format, args...) l.Interceptf(format, args...)
} }
// Panic reroutes go-nfs Panic messages to Intercept // Panic reroutes go-nfs Panic messages to Intercept
func (l *LogIntercepter) Panic(args ...interface{}) { func (l *logIntercepter) Panic(args ...interface{}) {
l.Intercept(args...) l.Intercept(args...)
} }
// Panicf reroutes go-nfs Panicf messages to Interceptf // Panicf reroutes go-nfs Panicf messages to Interceptf
func (l *LogIntercepter) Panicf(format string, args ...interface{}) { func (l *logIntercepter) Panicf(format string, args ...interface{}) {
l.Interceptf(format, args...) l.Interceptf(format, args...)
} }
// ParseLevel parses the nfs.LogLevel // ParseLevel parses the nfs.LogLevel
func (l *LogIntercepter) ParseLevel(level string) (nfs.LogLevel, error) { func (l *logIntercepter) ParseLevel(level string) (nfs.LogLevel, error) {
return nfs.Log.ParseLevel(level) return nfs.Log.ParseLevel(level)
} }
// Print reroutes go-nfs Print messages to Intercept // Print reroutes go-nfs Print messages to Intercept
func (l *LogIntercepter) Print(args ...interface{}) { func (l *logIntercepter) Print(args ...interface{}) {
l.Intercept(args...) l.Intercept(args...)
} }
// Printf reroutes go-nfs Printf messages to Intercept // Printf reroutes go-nfs Printf messages to Intercept
func (l *LogIntercepter) Printf(format string, args ...interface{}) { func (l *logIntercepter) Printf(format string, args ...interface{}) {
l.Interceptf(format, args...) l.Interceptf(format, args...)
} }
// SetLevel sets the nfs.LogLevel // SetLevel sets the nfs.LogLevel
func (l *LogIntercepter) SetLevel(level nfs.LogLevel) { func (l *logIntercepter) SetLevel(level nfs.LogLevel) {
l.Level = level l.Level = level
} }
// Trace reroutes go-nfs Trace messages to Intercept // Trace reroutes go-nfs Trace messages to Intercept
func (l *LogIntercepter) Trace(args ...interface{}) { func (l *logIntercepter) Trace(args ...interface{}) {
l.Intercept(args...) l.Intercept(args...)
} }
// Tracef reroutes go-nfs Tracef messages to Interceptf // Tracef reroutes go-nfs Tracef messages to Interceptf
func (l *LogIntercepter) Tracef(format string, args ...interface{}) { func (l *logIntercepter) Tracef(format string, args ...interface{}) {
l.Interceptf(format, args...) l.Interceptf(format, args...)
} }
// Warn reroutes go-nfs Warn messages to Intercept // Warn reroutes go-nfs Warn messages to Intercept
func (l *LogIntercepter) Warn(args ...interface{}) { func (l *logIntercepter) Warn(args ...interface{}) {
l.Intercept(args...) l.Intercept(args...)
} }
// Warnf reroutes go-nfs Warnf messages to Interceptf // Warnf reroutes go-nfs Warnf messages to Interceptf
func (l *LogIntercepter) Warnf(format string, args ...interface{}) { func (l *logIntercepter) Warnf(format string, args ...interface{}) {
l.Interceptf(format, args...) l.Interceptf(format, args...)
} }

View File

@ -98,7 +98,7 @@ We specify a specific port that we can use in the mount command:
To mount the server under Linux/macOS, use the following command: To mount the server under Linux/macOS, use the following command:
mount -oport=$PORT,mountport=$PORT $HOSTNAME: path/to/mountpoint mount -t nfs -o port=$PORT,mountport=$PORT,tcp $HOSTNAME:/ path/to/mountpoint
Where ` + "`$PORT`" + ` is the same port number we used in the serve nfs command. Where ` + "`$PORT`" + ` is the same port number we used in the serve nfs command.

View File

@ -4,6 +4,7 @@ package nfs
import ( import (
"context" "context"
"fmt"
"net" "net"
nfs "github.com/willscott/go-nfs" nfs "github.com/willscott/go-nfs"
@ -39,9 +40,9 @@ func NewServer(ctx context.Context, vfs *vfs.VFS, opt *Options) (s *Server, err
s.handler = newHandler(vfs, opt) s.handler = newHandler(vfs, opt)
s.listener, err = net.Listen("tcp", s.opt.ListenAddr) s.listener, err = net.Listen("tcp", s.opt.ListenAddr)
if err != nil { if err != nil {
fs.Errorf(nil, "NFS server failed to listen: %v\n", err) return nil, fmt.Errorf("failed to open listening socket: %w", err)
} }
return return s, nil
} }
// Addr returns the listening address of the server // Addr returns the listening address of the server