StreamRecorder/pkg/nas/connector.go
Aaron e2566fdefa
Some checks failed
Build Go Binaries / build (push) Has been cancelled
Claude bugfixes
2026-08-07 03:49:37 +00:00

99 lines
3.7 KiB
Go

package nas
import (
"context"
"fmt"
"os/exec"
"runtime"
)
// Connector attaches a configured Location to the host filesystem so that
// ordinary file operations reach the NAS.
//
// Windows and Linux disagree about what "connecting to a share" means —
// Windows authenticates a UNC path in place, Linux needs the share mounted
// somewhere in its single filesystem tree — so each host gets its own
// implementation and NASService stays platform-neutral. Selection happens at
// runtime rather than through build tags so that every implementation is
// compiled, vetted and unit-testable on every platform; only the final exec
// call is genuinely host-specific.
type Connector interface {
// Name identifies the strategy in logs and check output.
Name() string
// Attach makes loc reachable and reports where to write.
Attach(ctx context.Context, loc Location, cfg NASConfig) (Attachment, error)
// Detach undoes whatever Attach set up. Implementations must undo only
// what they created: a share this process found already mounted belongs
// to the host, and unmounting it could break unrelated software.
Detach(ctx context.Context, att Attachment) error
}
// Attachment is the result of a successful Attach.
type Attachment struct {
// BasePath is the host-native directory that maps to the configured
// destination. Every file operation is resolved against it.
BasePath string
// Notes explain how the host reached the destination, for -check output
// and startup logs.
Notes []string
// Owned records that this process created the connection or mount, and
// so may tear it down on shutdown.
Owned bool
// mountPoint is the directory this process mounted, when Owned is set by
// the Linux connector.
mountPoint string
// share is the network path this process connected, when Owned is set by
// the Windows connector.
share string
}
// commandRunner executes an external command. Implementations keep argument
// construction in a pure function and call through this, so tests can assert
// exactly what would run without running it.
type commandRunner func(ctx context.Context, name string, args ...string) ([]byte, error)
// execRunner is the production runner: it combines stdout and stderr because
// mount and net both report the useful diagnosis on one or the other.
func execRunner(ctx context.Context, name string, args ...string) ([]byte, error) {
return exec.CommandContext(ctx, name, args...).CombinedOutput()
}
// connectorFor picks the strategy for a destination on a given OS. A
// destination that is already an ordinary directory needs no network step on
// any platform, so it short-circuits before the OS switch.
func connectorFor(loc Location, goos string) Connector {
if !loc.IsRemote() {
return &localConnector{goos: goos}
}
switch goos {
case "windows":
return &windowsConnector{run: execRunner}
case "linux":
return &linuxConnector{run: execRunner, mountsPath: procMountsPath}
default:
return &unsupportedConnector{goos: goos}
}
}
// defaultConnector selects the strategy for the running host.
func defaultConnector(loc Location) Connector {
return connectorFor(loc, runtime.GOOS)
}
// unsupportedConnector fails clearly on platforms this project does not target,
// rather than silently writing a share-shaped directory to local disk.
type unsupportedConnector struct {
goos string
}
func (c *unsupportedConnector) Name() string { return "unsupported" }
func (c *unsupportedConnector) Attach(_ context.Context, loc Location, _ NASConfig) (Attachment, error) {
return Attachment{}, fmt.Errorf(
"connecting to network share %s is not supported on %s; mount the share and set NAS_OUTPUT_PATH to the mount point",
loc.SMBSource(), c.goos)
}
func (c *unsupportedConnector) Detach(context.Context, Attachment) error { return nil }