2023-10-05 01:28:41 +08:00
|
|
|
//go:build unix
|
|
|
|
|
|
|
|
package nfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-06-24 23:32:09 +08:00
|
|
|
"fmt"
|
2023-10-05 01:28:41 +08:00
|
|
|
"net"
|
|
|
|
|
|
|
|
nfs "github.com/willscott/go-nfs"
|
|
|
|
|
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/vfs"
|
|
|
|
"github.com/rclone/rclone/vfs/vfscommon"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Server contains everything to run the Server
|
|
|
|
type Server struct {
|
2024-02-06 15:53:12 +08:00
|
|
|
opt Options
|
|
|
|
handler nfs.Handler
|
|
|
|
ctx context.Context // for global config
|
|
|
|
listener net.Listener
|
|
|
|
UnmountedExternally bool
|
2023-10-05 01:28:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer creates a new server
|
|
|
|
func NewServer(ctx context.Context, vfs *vfs.VFS, opt *Options) (s *Server, err error) {
|
|
|
|
if vfs.Opt.CacheMode == vfscommon.CacheModeOff {
|
|
|
|
fs.LogPrintf(fs.LogLevelWarning, ctx, "NFS writes don't work without a cache, the filesystem will be served read-only")
|
|
|
|
}
|
|
|
|
// Our NFS server doesn't have any authentication, we run it on localhost and random port by default
|
|
|
|
if opt.ListenAddr == "" {
|
|
|
|
opt.ListenAddr = "localhost:"
|
|
|
|
}
|
|
|
|
|
|
|
|
s = &Server{
|
|
|
|
ctx: ctx,
|
|
|
|
opt: *opt,
|
|
|
|
}
|
2024-08-26 17:35:28 +08:00
|
|
|
s.handler, err = NewHandler(ctx, vfs, opt)
|
2024-06-25 18:18:39 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to make NFS handler: %w", err)
|
|
|
|
}
|
2023-10-05 01:28:41 +08:00
|
|
|
s.listener, err = net.Listen("tcp", s.opt.ListenAddr)
|
|
|
|
if err != nil {
|
2024-06-24 23:32:09 +08:00
|
|
|
return nil, fmt.Errorf("failed to open listening socket: %w", err)
|
2023-10-05 01:28:41 +08:00
|
|
|
}
|
2024-06-24 23:32:09 +08:00
|
|
|
return s, nil
|
2023-10-05 01:28:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Addr returns the listening address of the server
|
|
|
|
func (s *Server) Addr() net.Addr {
|
|
|
|
return s.listener.Addr()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown stops the server
|
|
|
|
func (s *Server) Shutdown() error {
|
|
|
|
return s.listener.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serve starts the server
|
|
|
|
func (s *Server) Serve() (err error) {
|
|
|
|
fs.Logf(nil, "NFS Server running at %s\n", s.listener.Addr())
|
|
|
|
return nfs.Serve(s.listener, s.handler)
|
|
|
|
}
|