github.com/artpar/rclone@v1.67.3/cmd/serve/nfs/server.go (about)

     1  //go:build unix
     2  
     3  package nfs
     4  
     5  import (
     6  	"context"
     7  	"net"
     8  
     9  	nfs "github.com/willscott/go-nfs"
    10  
    11  	"github.com/artpar/rclone/fs"
    12  	"github.com/artpar/rclone/vfs"
    13  	"github.com/artpar/rclone/vfs/vfscommon"
    14  )
    15  
    16  // Server contains everything to run the Server
    17  type Server struct {
    18  	opt                 Options
    19  	handler             nfs.Handler
    20  	ctx                 context.Context // for global config
    21  	listener            net.Listener
    22  	UnmountedExternally bool
    23  }
    24  
    25  // NewServer creates a new server
    26  func NewServer(ctx context.Context, vfs *vfs.VFS, opt *Options) (s *Server, err error) {
    27  	if vfs.Opt.CacheMode == vfscommon.CacheModeOff {
    28  		fs.LogPrintf(fs.LogLevelWarning, ctx, "NFS writes don't work without a cache, the filesystem will be served read-only")
    29  	}
    30  	// Our NFS server doesn't have any authentication, we run it on localhost and random port by default
    31  	if opt.ListenAddr == "" {
    32  		opt.ListenAddr = "localhost:"
    33  	}
    34  
    35  	s = &Server{
    36  		ctx: ctx,
    37  		opt: *opt,
    38  	}
    39  	s.handler = newHandler(vfs, opt)
    40  	s.listener, err = net.Listen("tcp", s.opt.ListenAddr)
    41  	if err != nil {
    42  		fs.Errorf(nil, "NFS server failed to listen: %v\n", err)
    43  	}
    44  	return
    45  }
    46  
    47  // Addr returns the listening address of the server
    48  func (s *Server) Addr() net.Addr {
    49  	return s.listener.Addr()
    50  }
    51  
    52  // Shutdown stops the server
    53  func (s *Server) Shutdown() error {
    54  	return s.listener.Close()
    55  }
    56  
    57  // Serve starts the server
    58  func (s *Server) Serve() (err error) {
    59  	fs.Logf(nil, "NFS Server running at %s\n", s.listener.Addr())
    60  	return nfs.Serve(s.listener, s.handler)
    61  }