136 lines
3.8 KiB
Go
136 lines
3.8 KiB
Go
package nas
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// procMountsPath is the kernel's view of what is mounted in this process's
|
|
// mount namespace, which is the right question to ask inside a container too.
|
|
const procMountsPath = "/proc/mounts"
|
|
|
|
// mountEntry is one line of the mount table.
|
|
type mountEntry struct {
|
|
Source string
|
|
Point string
|
|
FSType string
|
|
}
|
|
|
|
// networkFilesystems are the mount types that can plausibly be serving a NAS
|
|
// share. Restricting the search avoids matching, say, a local directory that
|
|
// merely happens to be named like a server.
|
|
var networkFilesystems = map[string]bool{
|
|
"cifs": true,
|
|
"smb3": true,
|
|
"smbfs": true,
|
|
"nfs": true,
|
|
"nfs4": true,
|
|
"fuse.smbnetfs": true,
|
|
}
|
|
|
|
// readMountTable parses /proc/mounts (or any file in its format). Fields are
|
|
// whitespace-separated: source, mount point, type, options, dump, pass.
|
|
func readMountTable(path string) ([]mountEntry, error) {
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to read mount table %s: %w", path, err)
|
|
}
|
|
defer f.Close()
|
|
|
|
var entries []mountEntry
|
|
scanner := bufio.NewScanner(f)
|
|
for scanner.Scan() {
|
|
fields := strings.Fields(scanner.Text())
|
|
if len(fields) < 3 {
|
|
continue
|
|
}
|
|
entries = append(entries, mountEntry{
|
|
Source: unescapeMountField(fields[0]),
|
|
Point: unescapeMountField(fields[1]),
|
|
FSType: fields[2],
|
|
})
|
|
}
|
|
if err := scanner.Err(); err != nil {
|
|
return nil, fmt.Errorf("failed to scan mount table %s: %w", path, err)
|
|
}
|
|
|
|
return entries, nil
|
|
}
|
|
|
|
// unescapeMountField decodes the octal escapes the kernel uses for characters
|
|
// that would otherwise break the whitespace-separated format — most often \040
|
|
// for a space in a mount point.
|
|
func unescapeMountField(field string) string {
|
|
if !strings.Contains(field, `\`) {
|
|
return field
|
|
}
|
|
|
|
var b strings.Builder
|
|
for i := 0; i < len(field); i++ {
|
|
if field[i] == '\\' && i+3 < len(field) {
|
|
if v, err := strconv.ParseUint(field[i+1:i+4], 8, 8); err == nil {
|
|
b.WriteByte(byte(v))
|
|
i += 3
|
|
continue
|
|
}
|
|
}
|
|
b.WriteByte(field[i])
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
// findMount locates the mount serving loc and returns its mount point plus the
|
|
// path components still to be appended below it.
|
|
//
|
|
// The search runs deepest-first because a share may be mounted at any level:
|
|
// //nas/dci might be mounted at /mnt/dci, or //nas/dci/streams mounted directly
|
|
// at /mnt/streams. Preferring the longest matching source keeps both working
|
|
// and avoids pointing at a parent that the mount does not actually cover.
|
|
func findMount(entries []mountEntry, loc Location) (point string, rest []string, ok bool) {
|
|
if !loc.IsRemote() {
|
|
return "", nil, false
|
|
}
|
|
|
|
for depth := len(loc.Sub); depth >= 0; depth-- {
|
|
for _, candidate := range mountSourceForms(loc, depth) {
|
|
for _, entry := range entries {
|
|
if !networkFilesystems[entry.FSType] {
|
|
continue
|
|
}
|
|
if normalizeMountSource(entry.Source) == candidate {
|
|
return entry.Point, loc.Sub[depth:], true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return "", nil, false
|
|
}
|
|
|
|
// mountSourceForms renders the share, plus its first `depth` subdirectories, in
|
|
// the spellings a mount table might use for it.
|
|
func mountSourceForms(loc Location, depth int) []string {
|
|
segments := append([]string{loc.Share}, loc.Sub[:depth]...)
|
|
tail := strings.Join(segments, "/")
|
|
|
|
return []string{
|
|
normalizeMountSource("//" + loc.Server + "/" + tail), // cifs/smb3
|
|
normalizeMountSource(loc.Server + ":/" + tail), // nfs
|
|
}
|
|
}
|
|
|
|
// normalizeMountSource makes sources comparable: SMB servers and shares are
|
|
// case-insensitive, separators may be either slash, and a trailing slash is not
|
|
// meaningful.
|
|
func normalizeMountSource(source string) string {
|
|
s := strings.ReplaceAll(source, `\`, "/")
|
|
s = strings.ToLower(strings.TrimSpace(s))
|
|
if len(s) > 1 {
|
|
s = strings.TrimRight(s, "/")
|
|
}
|
|
return s
|
|
}
|