Support removal of managed diagnostic views

This commit is contained in:
2026-11-30 13:54:57 +00:00
parent 2ca09e2a00
commit 5196c67570
3 changed files with 132 additions and 1 deletions

View File

@@ -184,6 +184,66 @@ func ManagedAttemptTargets(sessionRoot, runID string, names []string) ([]string,
return targets, nil
}
// InspectManagedAttemptViews reports whether any recognized managed view exists.
// Dangling links count as present; their targets are never followed.
func InspectManagedAttemptViews(sessionRoot string, names []string) (bool, error) {
return visitManagedAttemptViews(sessionRoot, names, false)
}
// RemoveManagedAttemptViews removes recognized managed links without following them.
func RemoveManagedAttemptViews(sessionRoot string, names []string) error {
_, err := visitManagedAttemptViews(sessionRoot, names, true)
return err
}
func visitManagedAttemptViews(sessionRoot string, names []string, remove bool) (bool, error) {
for _, name := range names {
if err := pathsafe.ValidateOpaqueSegment(name); err != nil {
return false, fmt.Errorf("invalid managed view %q: %w", name, err)
}
}
root, err := os.OpenRoot(sessionRoot)
if err != nil {
return false, err
}
defer root.Close()
found := false
for _, category := range []string{"logs", "debug"} {
info, err := root.Lstat(category)
if errors.Is(err, os.ErrNotExist) {
continue
}
if err != nil {
return false, err
}
if !info.IsDir() || info.Mode()&os.ModeSymlink != 0 {
return false, fmt.Errorf("managed %s directory is unsafe", category)
}
parent, err := root.OpenRoot(category)
if err != nil {
return false, err
}
for _, name := range names {
target, err := existingManagedView(parent, name, category)
if err == nil && target != nil {
found = true
if remove {
err = parent.Remove(name)
if errors.Is(err, os.ErrNotExist) {
err = nil
}
}
}
if err != nil {
_ = parent.Close()
return false, err
}
}
_ = parent.Close()
}
return found, nil
}
func replaceManagedView(parent *os.Root, name, category, target string) error {
if _, err := existingManagedView(parent, name, category); err != nil {
return err