package nas import ( "fmt" "path/filepath" "regexp" "strings" ) // Location is a configured NAS destination parsed into a host-neutral form. // // The same destination is spelled differently depending on where you're // standing: Windows wants \\HomeLabNAS\dci\streams, a Linux host reaches the // same bytes through a mount point like /mnt/dci/streams, and either host may // simply be handed a plain local directory. Parsing happens once, up front, // and platform-specific rendering is left to the Connector implementations. type Location struct { // Raw is the destination exactly as configured, for error messages. Raw string // Server is the host serving the share; empty for a plain local path. Server string // Share is the top-level export name; empty for a plain local path. Share string // Sub holds the path components below the share, if any. Sub []string } // driveLetter matches a Windows drive-qualified path such as C:\data. var driveLetter = regexp.MustCompile(`^[A-Za-z]:[\\/]`) // ParseLocation accepts every spelling of a destination this project supports: // Windows UNC (\\server\share\dir), the SMB source form (//server/share/dir), // a smb:// URL, or a plain local/mounted directory. It does no I/O and makes no // assumptions about the host OS, so the result is safe to compute anywhere. func ParseLocation(raw string) (Location, error) { trimmed := strings.TrimSpace(raw) if trimmed == "" { return Location{}, fmt.Errorf("NAS path is empty") } loc := Location{Raw: trimmed} remainder, isRemote := stripRemotePrefix(trimmed) if !isRemote { return loc, nil } parts := splitPathComponents(remainder) if len(parts) < 2 { return Location{}, fmt.Errorf( "%q names a server but no share; expected a form like \\\\server\\share\\folder", trimmed) } loc.Server = parts[0] loc.Share = parts[1] loc.Sub = parts[2:] return loc, nil } // stripRemotePrefix reports whether raw names a network share and returns the // server/share/subpath portion when it does. A leading // is treated as a share // rather than a local path: POSIX leaves paths beginning with exactly two // slashes implementation-defined, and nobody configures a NAS destination that // way by accident. func stripRemotePrefix(raw string) (string, bool) { lower := strings.ToLower(raw) switch { case strings.HasPrefix(lower, "smb://"): return raw[len("smb://"):], true case strings.HasPrefix(lower, "cifs://"): return raw[len("cifs://"):], true case strings.HasPrefix(raw, `\\`): return raw[2:], true case strings.HasPrefix(raw, "//"): return raw[2:], true default: return raw, false } } // splitPathComponents splits on either separator and drops empty components, so // that \\server\share\\folder\ and //server/share/folder agree. func splitPathComponents(path string) []string { fields := strings.FieldsFunc(path, func(r rune) bool { return r == '/' || r == '\\' }) out := make([]string, 0, len(fields)) for _, f := range fields { if f != "" { out = append(out, f) } } return out } // IsRemote reports whether reaching this destination requires attaching a // network share first, as opposed to writing to a directory that is already // part of the local filesystem. func (l Location) IsRemote() bool { return l.Server != "" && l.Share != "" } // UNC renders the destination in Windows form: \\server\share\sub. func (l Location) UNC() string { if !l.IsRemote() { return l.Raw } return `\\` + strings.Join(append([]string{l.Server, l.Share}, l.Sub...), `\`) } // ShareUNC renders just the share in Windows form: \\server\share. This is the // unit `net use` authenticates against; the subpath below it is an ordinary // directory on the connected share. func (l Location) ShareUNC() string { if !l.IsRemote() { return "" } return `\\` + l.Server + `\` + l.Share } // SMBSource renders the share in the //server/share form used by mount tables // and by mount.cifs. It deliberately omits Sub: that part is a path *within* // the mounted share, not part of the share's identity. func (l Location) SMBSource() string { if !l.IsRemote() { return "" } return "//" + l.Server + "/" + l.Share } // SubPath renders the components below the share using the host's separator, or // "" when the destination is the share root. func (l Location) SubPath() string { if len(l.Sub) == 0 { return "" } return filepath.Join(l.Sub...) } // String describes the destination for logs and check output. func (l Location) String() string { if l.IsRemote() { return l.UNC() } return l.Raw }