// The filepath package uses either forward slashes or backslashes,
// depending on the operating system. To process paths such as URLs
// that always use forward slashes regardless of the operating
-// system, see the path package.
+// system, see the [path] package.
package filepath
import (
"errors"
"io/fs"
"os"
- "runtime"
+ "slices"
"sort"
"strings"
)
b.w++
}
+func (b *lazybuf) prepend(prefix ...byte) {
+ b.buf = slices.Insert(b.buf, 0, prefix...)
+ b.w += len(prefix)
+}
+
func (b *lazybuf) string() string {
if b.buf == nil {
return b.volAndPath[:b.volLen+b.w]
if rooted && out.w != 1 || !rooted && out.w != 0 {
out.append(Separator)
}
- // If a ':' appears in the path element at the start of a Windows path,
- // insert a .\ at the beginning to avoid converting relative paths
- // like a/../c: into c:.
- if runtime.GOOS == "windows" && out.w == 0 && out.volLen == 0 && r != 0 {
- for i := r; i < n && !os.IsPathSeparator(path[i]); i++ {
- if path[i] == ':' {
- out.append('.')
- out.append(Separator)
- break
- }
- }
- }
// copy element
for ; r < n && !os.IsPathSeparator(path[r]); r++ {
out.append(path[r])
out.append('.')
}
+ postClean(&out) // avoid creating absolute paths on Windows
return FromSlash(out.string())
}
return err
}
- dirs, err := readDir(path)
+ dirs, err := os.ReadDir(path)
if err != nil {
// Second call, to report ReadDir error.
err = walkDirFn(path, d, err)
if err != nil {
err = fn(root, nil, err)
} else {
- err = walkDir(root, &statDirEntry{info}, fn)
+ err = walkDir(root, fs.FileInfoToDirEntry(info), fn)
}
if err == SkipDir || err == SkipAll {
return nil
return err
}
-type statDirEntry struct {
- info fs.FileInfo
-}
-
-func (d *statDirEntry) Name() string { return d.info.Name() }
-func (d *statDirEntry) IsDir() bool { return d.info.IsDir() }
-func (d *statDirEntry) Type() fs.FileMode { return d.info.Mode().Type() }
-func (d *statDirEntry) Info() (fs.FileInfo, error) { return d.info, nil }
-
// Walk walks the file tree rooted at root, calling fn for each file or
// directory in the tree, including root.
//
return err
}
-// readDir reads the directory named by dirname and returns
-// a sorted list of directory entries.
-func readDir(dirname string) ([]fs.DirEntry, error) {
- f, err := os.Open(dirname)
- if err != nil {
- return nil, err
- }
- dirs, err := f.ReadDir(-1)
- f.Close()
- if err != nil {
- return nil, err
- }
- sort.Slice(dirs, func(i, j int) bool { return dirs[i].Name() < dirs[j].Name() })
- return dirs, nil
-}
-
// readDirNames reads the directory named by dirname and returns
// a sorted list of directory entry names.
func readDirNames(dirname string) ([]string, error) {