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

108 lines
3.4 KiB
Go

package nas
import (
"context"
"fmt"
"path/filepath"
"strings"
)
// windowsConnector reaches a share the way Windows does: authenticate the UNC
// path with `net use`, then use the UNC path directly as a filesystem path.
// Nothing is mounted into a tree, so the resolved base path is the configured
// UNC path itself.
type windowsConnector struct {
run commandRunner
}
func (c *windowsConnector) Name() string { return "net use" }
func (c *windowsConnector) Attach(ctx context.Context, loc Location, cfg NASConfig) (Attachment, error) {
share := loc.ShareUNC()
att := Attachment{
BasePath: loc.UNC(),
share: share,
Notes: []string{"share: " + share, "user: " + usernameNote(cfg.Username)},
}
if cfg.Timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, cfg.Timeout)
defer cancel()
}
out, err := c.run(ctx, "net", netUseArgs(loc, cfg)...)
if err != nil {
// A failure here is not decisive. `net use` also fails when the share
// is already connected — commonly because Explorer or another process
// got there first — in which case the path is perfectly usable. Record
// the reason and let the caller's write probe deliver the verdict,
// rather than refusing to run against a working share.
att.Notes = append(att.Notes,
fmt.Sprintf("net use did not complete (%v); continuing, the share may already be connected", summarize(err, out)))
return att, nil
}
att.Owned = true
att.Notes = append(att.Notes, "connected via net use")
return att, nil
}
func (c *windowsConnector) Detach(ctx context.Context, att Attachment) error {
// Only drop connections this process made. Deleting a mapping the user or
// another application established would break them.
if !att.Owned || att.share == "" {
return nil
}
if out, err := c.run(ctx, "net", "use", att.share, "/delete"); err != nil {
return fmt.Errorf("failed to disconnect %s: %v", att.share, summarize(err, out))
}
return nil
}
// netUseArgs builds the `net use` invocation. Kept pure so the Windows code
// path can be asserted from any host, including the Linux box this is
// developed on, without executing anything.
func netUseArgs(loc Location, cfg NASConfig) []string {
args := []string{"use", loc.ShareUNC()}
if cfg.Username != "" && cfg.Password != "" {
args = append(args, "/user:"+cfg.Username, cfg.Password)
}
return append(args, "/persistent:no")
}
// summarize pairs an exec error with the first line of command output, which is
// where net and mount put the actual diagnosis.
func summarize(err error, out []byte) string {
text := strings.TrimSpace(string(out))
if text == "" {
return err.Error()
}
if idx := strings.IndexByte(text, '\n'); idx >= 0 {
text = text[:idx]
}
return fmt.Sprintf("%v: %s", err, text)
}
func usernameNote(username string) string {
if username == "" {
return "(none, using ambient credentials)"
}
return username
}
// joinBase resolves a relative path against a base that may be a UNC path.
// filepath.Join is correct on Windows and would mangle the leading \\ anywhere
// else, so UNC bases are joined with backslashes explicitly.
func joinBase(base string, elem ...string) string {
if len(elem) == 0 {
return base
}
if strings.HasPrefix(base, `\\`) {
parts := append([]string{strings.TrimRight(base, `\`)}, elem...)
return strings.Join(parts, `\`)
}
return filepath.Join(append([]string{base}, elem...)...)
}