simplify: delegate osroot.MkdirAll to native os.Root.MkdirAll · Entire
simplify: delegate osroot.MkdirAll to native os.Root.MkdirAll
0e4b26f→main·
Soph·1mo ago·3 files·+10 added/-44 removed
Go 1.26's os.Root has a native MkdirAll, so the hand-rolled per-segment Mkdir loop (with its ToSlash/Trim/path.Join handling and extra imports) collapses to a one-line delegation — battle-tested and stats before creating. Also:
- drop the dead
dir != ""guard in the rewind restore loop (path.Dir never returns "") - drop TestMkdirAll_EmptyOrDotIsNoop, which exercised the removed Trim/no-op logic; the call site never passes ""/"."
- correct the now-stale osroot package doc that listed MkdirAll as unsupported
Pure cleanup from a /simplify review; no behavior change.
Co-Authored-By: Claude Opus 4.8 (1M context) noreply@anthropic.com
Sessions
Changes
3
cmd/entire/cli
osroot
Mosroot.go+9/-31
Mosroot_test.go-12
strategy
- Mmanual_commit_rewind.go+1/-1
// (Go 1.24+). These helpers ensure that file operations cannot escape a scoped
// directory, preventing symlink attacks and TOCTOU races at the kernel level.
//
// os.Root supports: Open, OpenFile, Create, Stat, Lstat, Mkdir, Remove, OpenRoot.
// os.Root does NOT support: MkdirAll, WriteFile, ReadFile, Rename, RemoveAll.
// For unsupported operations, callers should use standard os functions with
// lexical validation.
// These wrappers predate Go 1.25, which added native ReadFile/WriteFile/MkdirAll
// (etc.) on *os.Root; they remain as the codebase's stable, consistent helper
// surface and delegate to the native methods where those now exist.
//
// Errors from these functions are returned unwrapped so that callers can use
// os.IsNotExist() and errors.Is() directly without losing the original sentinel.
package osroot
import (
"errors"
"io"
"io/fs"
"os"
"path"
"path/filepath"
"strings"
)
// ReadFile reads the named file relative to root using os.Root for
// MkdirAll creates the directory named by name, along with any necessary
// parents, relative to root. Each level is created with os.Root.Mkdir so the
// kernel enforces containment: unlike os.MkdirAll, it cannot create directories
// outside root. A name that escapes root (absolute, or containing ".." segments
// that climb above root) is rejected by os.Root and returns an error. Already-
// existing directories are tolerated. name may use either OS-native or forward
// slashes; an empty or "." name is a no-op.
func MkdirAll(root *os.Root, name string, perm os.FileMode) error {
name = strings.Trim(filepath.ToSlash(name), "/")
if name == "" || name == "." {
return nil
}
cur := ""
for _, part := range strings.Split(name, "/") {
if part == "" {
continue
}
cur = path.Join(cur, part)
if err := root.Mkdir(cur, perm); err != nil && !errors.Is(err, fs.ErrExist) {
return err //nolint:wrapcheck // preserve original error (incl. traversal rejection) for callers
}
}
return nil
}
// Remove removes the named file relative to root using os.Root for