]> Cypherpunks.ru repositories - gostls13.git/blobdiff - src/path/filepath/path.go
path/filepath: fix various issues in parsing Windows paths
[gostls13.git] / src / path / filepath / path.go
index 41fa733af95e07d1e667397f3920d4f21c698621..3d693f840a9ef5fe569283360283115db2dc14f9 100644 (file)
@@ -15,7 +15,6 @@ import (
        "errors"
        "io/fs"
        "os"
-       "runtime"
        "slices"
        "sort"
        "strings"
@@ -168,21 +167,7 @@ func Clean(path string) string {
                out.append('.')
        }
 
-       if runtime.GOOS == "windows" && out.volLen == 0 && out.buf != nil {
-               // 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:.
-               for _, c := range out.buf {
-                       if os.IsPathSeparator(c) {
-                               break
-                       }
-                       if c == ':' {
-                               out.prepend('.', Separator)
-                               break
-                       }
-               }
-       }
-
+       postClean(&out) // avoid creating absolute paths on Windows
        return FromSlash(out.string())
 }
 
@@ -463,7 +448,7 @@ func walkDir(path string, d fs.DirEntry, walkDirFn fs.WalkDirFunc) error {
                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)
@@ -545,7 +530,7 @@ func WalkDir(root string, fn fs.WalkDirFunc) error {
        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
@@ -553,19 +538,6 @@ func WalkDir(root string, fn fs.WalkDirFunc) error {
        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 }
-
-func (d *statDirEntry) String() string {
-       return fs.FormatDirEntry(d)
-}
-
 // Walk walks the file tree rooted at root, calling fn for each file or
 // directory in the tree, including root.
 //
@@ -593,22 +565,6 @@ func Walk(root string, fn WalkFunc) error {
        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) {