agent: detection's remaining stats make no os.FileInfo on darwin and linux
After statKind (e3ab7f3bf), os.Stat was still a fifth of what forty /api/drift asks allocated with real accounts: Agent.Detected's look at the agent's file, bundleInfo's and folderSubs' size-and-time stamps for every desktop app, and qoderApp's status files. Each os.Stat makes an os.FileInfo on the heap. They now go through the system call into a Stat_t on the stack: - Detected: statKind (exists, followed through links, as os.Stat). - qoderApp: statFile, a regular file as Mode().IsRegular() says. - bundleInfo, folderSubs: statStamp, mtime and size as ModTime/Size say (Mtimespec on darwin, Mtim on linux; os.Stat elsewhere). Windows and other systems keep os.Stat. Run (at this tree): - mote: go test -count=1 ./internal/agent ok (518s, load high after a reboot); go vet darwin; linux and windows vet clean - wsl-run (Linux): TestStatStamp|TestStatKind|TestQoder ok - win-run (Windows): same set; TestQoderSettingsFromAnIDEPluginIsNotTheCLI red, and red the same on origin/main783cd0f84(#1827: the fixture is an extensionless #!/bin/sh on PATH); the rest ok - TestStatStampAndFileSayWhatStatSays compares with os.Stat for a file, a folder, a link, a gone path and a path under a file; with darwin's statStamp put back on os.Stat by hand it is red (3 allocations for two looks), restored - Real accounts (sandbox HOME copy), 40 /api/drift calls, paired runs: os.Stat 2.0/3.1 MB before, 1.5/0 MB after (left: pencilApp, a glob, provider.stampOf); totals 20.2/19.1 MB before, 18.5/17.5 MB after.
This commit is contained in:
@@ -424,7 +424,7 @@ func (a *Agent) Detected() bool {
|
||||
if a.Dir != "" && Taken(a.Dir) {
|
||||
return false
|
||||
}
|
||||
if _, err := os.Stat(a.Path); err == nil && (a.pathOwn == nil || a.pathOwn()) {
|
||||
if _, ok := statKind(a.Path); ok && (a.pathOwn == nil || a.pathOwn()) {
|
||||
return true
|
||||
}
|
||||
if a.Dir != "" && !a.dirShared && agentDir(a.Dir) {
|
||||
|
||||
@@ -256,11 +256,10 @@ type plistInfo struct {
|
||||
// to tell two writes apart is read again (racyDir).
|
||||
func bundleInfo(bundle string) (plistInfo, bool) {
|
||||
p := filepath.Join(bundle, "Contents", "Info.plist")
|
||||
st, err := os.Stat(p)
|
||||
if err != nil {
|
||||
mod, size, ok := statStamp(p)
|
||||
if !ok {
|
||||
return plistInfo{}, false
|
||||
}
|
||||
mod, size := st.ModTime(), st.Size()
|
||||
plists.Lock()
|
||||
k, ok := plists.m[p]
|
||||
plists.Unlock()
|
||||
@@ -341,11 +340,10 @@ func inAppFolders(dirs []string, names ...string) []string {
|
||||
// it. A time too recent to tell two changes apart is not trusted, as git
|
||||
// treats a racily clean index.
|
||||
func folderSubs(dir string) []string {
|
||||
st, err := os.Stat(dir)
|
||||
if err != nil {
|
||||
mod, _, ok := statStamp(dir)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
mod := st.ModTime()
|
||||
appSubs.Lock()
|
||||
k, ok := appSubs.m[dir]
|
||||
appSubs.Unlock()
|
||||
|
||||
@@ -340,7 +340,7 @@ var noticeQoder = newNotice("{agent} reads its settings as a session starts —
|
||||
// runs Qoder's CLI as its agent with that folder's settings.
|
||||
func qoderApp(dir string) bool {
|
||||
for _, f := range []string{".qoder-app-status.json", ".qoder-app-canary-status.json", ".qoder-app-dev-status.json"} {
|
||||
if fi, err := os.Stat(filepath.Join(dir, f)); err == nil && fi.Mode().IsRegular() {
|
||||
if statFile(filepath.Join(dir, f)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,3 +12,9 @@ func statKind(p string) (dir, ok bool) {
|
||||
}
|
||||
return st.IsDir(), true
|
||||
}
|
||||
|
||||
// statFile is whether p, links followed, is a regular file.
|
||||
func statFile(p string) bool {
|
||||
st, err := os.Stat(p)
|
||||
return err == nil && st.Mode().IsRegular()
|
||||
}
|
||||
|
||||
@@ -10,14 +10,27 @@ import "syscall"
|
||||
// FileInfos were a fifth of what forty asks allocated with real accounts.
|
||||
func statKind(p string) (dir, ok bool) {
|
||||
var st syscall.Stat_t
|
||||
if !statInto(p, &st) {
|
||||
return false, false
|
||||
}
|
||||
return uint32(st.Mode)&syscall.S_IFMT == syscall.S_IFDIR, true
|
||||
}
|
||||
|
||||
// statFile is whether p, links followed, is a regular file, as os.Stat's
|
||||
// Mode().IsRegular() would say.
|
||||
func statFile(p string) bool {
|
||||
var st syscall.Stat_t
|
||||
return statInto(p, &st) && uint32(st.Mode)&syscall.S_IFMT == syscall.S_IFREG
|
||||
}
|
||||
|
||||
// statInto is syscall.Stat of p into st, tried again when a signal cuts
|
||||
// it short, as os.Stat is.
|
||||
func statInto(p string, st *syscall.Stat_t) bool {
|
||||
for {
|
||||
err := syscall.Stat(p, &st)
|
||||
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
|
||||
return err == nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
// statStamp is p's modification time and size, links followed, as
|
||||
// os.Stat's ModTime and Size would say, without the os.FileInfo it makes
|
||||
// on the heap: bundleInfo and folderSubs ask it of every desktop app on
|
||||
// each drift ask.
|
||||
func statStamp(p string) (mod time.Time, size int64, ok bool) {
|
||||
var st syscall.Stat_t
|
||||
if !statInto(p, &st) {
|
||||
return time.Time{}, 0, false
|
||||
}
|
||||
return time.Unix(st.Mtimespec.Unix()), st.Size, true
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
// statStamp is p's modification time and size, links followed, as
|
||||
// os.Stat's ModTime and Size would say (statstamp_darwin.go).
|
||||
func statStamp(p string) (mod time.Time, size int64, ok bool) {
|
||||
var st syscall.Stat_t
|
||||
if !statInto(p, &st) {
|
||||
return time.Time{}, 0, false
|
||||
}
|
||||
return time.Unix(st.Mtim.Unix()), st.Size, true
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
//go:build !darwin && !linux
|
||||
|
||||
package agent
|
||||
|
||||
import (
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
// statStamp is p's modification time and size, links followed
|
||||
// (statstamp_darwin.go).
|
||||
func statStamp(p string) (mod time.Time, size int64, ok bool) {
|
||||
st, err := os.Stat(p)
|
||||
if err != nil {
|
||||
return time.Time{}, 0, false
|
||||
}
|
||||
return st.ModTime(), st.Size(), true
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// statStamp and statFile say what os.Stat would, for a file, a folder, a
|
||||
// link and a gone path, and make no os.FileInfo for it on unix.
|
||||
func TestStatStampAndFileSayWhatStatSays(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
file := filepath.Join(dir, "Info.plist")
|
||||
if err := os.WriteFile(file, []byte("<plist/>"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
at := time.Date(2026, 10, 1, 12, 0, 0, 123456789, time.UTC)
|
||||
os.Chtimes(file, at, at)
|
||||
link := filepath.Join(dir, "link")
|
||||
os.Symlink(file, link)
|
||||
for _, p := range []string{file, dir, link, filepath.Join(dir, "gone"), filepath.Join(file, "under")} {
|
||||
mod, size, ok := statStamp(p)
|
||||
st, err := os.Stat(p)
|
||||
if ok != (err == nil) {
|
||||
t.Fatalf("%s: ok %v, os.Stat err %v", p, ok, err)
|
||||
}
|
||||
if err == nil && (!mod.Equal(st.ModTime()) || size != st.Size()) {
|
||||
t.Fatalf("%s: %v %d, os.Stat %v %d", p, mod, size, st.ModTime(), st.Size())
|
||||
}
|
||||
if want := err == nil && st.Mode().IsRegular(); statFile(p) != want {
|
||||
t.Fatalf("%s: statFile %v, want %v", p, !want, want)
|
||||
}
|
||||
}
|
||||
if runtime.GOOS != "darwin" && runtime.GOOS != "linux" {
|
||||
return
|
||||
}
|
||||
// the path's bytes for the system call are all a look allocates
|
||||
if n := testing.AllocsPerRun(20, func() { statStamp(file); statFile(file) }); n > 2 {
|
||||
t.Fatalf("%v allocations for two looks", n)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user