diff --git a/internal/agent/agent.go b/internal/agent/agent.go index 054021eb3..2d40b618f 100644 --- a/internal/agent/agent.go +++ b/internal/agent/agent.go @@ -494,8 +494,8 @@ var leftover = map[string]bool{"logs": true, "log": true, "debug": true, "diagno // p, or one it is in, would be: nothing can be written under it. func Taken(p string) bool { for d := filepath.Clean(p); ; { - if st, err := os.Stat(d); err == nil { - return !st.IsDir() + if dir, ok := statKind(d); ok { + return !dir } up := filepath.Dir(d) if up == d { diff --git a/internal/agent/cliupdate.go b/internal/agent/cliupdate.go index 80caaaad5..33d9ea964 100644 --- a/internal/agent/cliupdate.go +++ b/internal/agent/cliupdate.go @@ -518,8 +518,8 @@ func exe(name string) string { return name } -func isFile(p string) bool { st, err := os.Stat(p); return err == nil && !st.IsDir() } -func isDir(p string) bool { st, err := os.Stat(p); return err == nil && st.IsDir() } +func isFile(p string) bool { dir, ok := statKind(p); return ok && !dir } +func isDir(p string) bool { dir, ok := statKind(p); return ok && dir } // ---- versions -------------------------------------------------------------- diff --git a/internal/agent/statkind_other.go b/internal/agent/statkind_other.go new file mode 100644 index 000000000..684347801 --- /dev/null +++ b/internal/agent/statkind_other.go @@ -0,0 +1,14 @@ +//go:build !unix + +package agent + +import "os" + +// statKind is what is at p, following links (statkind_unix.go). +func statKind(p string) (dir, ok bool) { + st, err := os.Stat(p) + if err != nil { + return false, false + } + return st.IsDir(), true +} diff --git a/internal/agent/statkind_test.go b/internal/agent/statkind_test.go new file mode 100644 index 000000000..29d72cf36 --- /dev/null +++ b/internal/agent/statkind_test.go @@ -0,0 +1,46 @@ +package agent + +import ( + "os" + "path/filepath" + "runtime" + "testing" +) + +// isDir, isFile and Taken answer as os.Stat would, and on unix make no +// os.FileInfo: detection asks them of some thousand paths a drift ask. +func TestStatKindAnswersWithoutAFileInfo(t *testing.T) { + d := t.TempDir() + f := filepath.Join(d, "f") + if err := os.WriteFile(f, nil, 0o644); err != nil { + t.Fatal(err) + } + link := filepath.Join(d, "to-dir") + linked := os.Symlink(d, link) == nil + gone := filepath.Join(d, "gone") + for _, c := range []struct { + p string + dir, file bool + taken bool + }{ + {d, true, false, false}, + {f, false, true, true}, + {gone, false, false, false}, + {filepath.Join(f, "under"), false, false, true}, + {"", false, false, false}, + } { + if isDir(c.p) != c.dir || isFile(c.p) != c.file || Taken(c.p) != c.taken { + t.Errorf("%q: isDir %v isFile %v Taken %v", c.p, isDir(c.p), isFile(c.p), Taken(c.p)) + } + } + if linked && !isDir(link) { + t.Error("a link to a folder is a folder, as os.Stat follows it") + } + if runtime.GOOS == "windows" { + return + } + // the path made a C string is all there is to it + if n := testing.AllocsPerRun(50, func() { isDir(d); isFile(f) }); n > 2 { + t.Fatalf("%v allocations for two looks", n) + } +} diff --git a/internal/agent/statkind_unix.go b/internal/agent/statkind_unix.go new file mode 100644 index 000000000..108707f33 --- /dev/null +++ b/internal/agent/statkind_unix.go @@ -0,0 +1,23 @@ +//go:build unix + +package agent + +import "syscall" + +// statKind is what is at p, following links, as os.Stat's IsDir would say, +// without the os.FileInfo os.Stat makes on the heap: detection asks it of +// some thousand paths on each of the window's 15s drift asks, and the +// FileInfos were a fifth of what forty asks allocated with real accounts. +func statKind(p string) (dir, ok bool) { + var st syscall.Stat_t + for { + err := syscall.Stat(p, &st) + if err == syscall.EINTR { + continue + } + if err != nil { + return false, false + } + return uint32(st.Mode)&syscall.S_IFMT == syscall.S_IFDIR, true + } +}