| 1 |
// Copyright 2016 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package os |
| 6 |
|
| 7 |
import ( |
| 8 |
"internal/bytealg" |
| 9 |
"internal/filepathlite" |
| 10 |
"io" |
| 11 |
"io/fs" |
| 12 |
"slices" |
| 13 |
) |
| 14 |
|
| 15 |
type readdirMode int |
| 16 |
|
| 17 |
const ( |
| 18 |
readdirName readdirMode = iota |
| 19 |
readdirDirEntry |
| 20 |
readdirFileInfo |
| 21 |
) |
| 22 |
|
| 23 |
// Readdir reads the contents of the directory associated with file and |
| 24 |
// returns a slice of up to n [FileInfo] values, as would be returned |
| 25 |
// by [Lstat], in directory order. Subsequent calls on the same file will yield |
| 26 |
// further FileInfos. |
| 27 |
// |
| 28 |
// If n > 0, Readdir returns at most n FileInfo structures. In this case, if |
| 29 |
// Readdir returns an empty slice, it will return a non-nil error |
| 30 |
// explaining why. At the end of a directory, the error is [io.EOF]. |
| 31 |
// |
| 32 |
// If n <= 0, Readdir returns all the FileInfo from the directory in |
| 33 |
// a single slice. In this case, if Readdir succeeds (reads all |
| 34 |
// the way to the end of the directory), it returns the slice and a |
| 35 |
// nil error. If it encounters an error before the end of the |
| 36 |
// directory, Readdir returns the FileInfo read until that point |
| 37 |
// and a non-nil error. |
| 38 |
// |
| 39 |
// Most clients are better served by the more efficient ReadDir method. |
| 40 |
func (f *File) Readdir(n int) ([]FileInfo, error) { |
| 41 |
if f == nil { |
| 42 |
return nil, ErrInvalid |
| 43 |
} |
| 44 |
_, _, infos, err := f.readdir(n, readdirFileInfo) |
| 45 |
if infos == nil { |
| 46 |
// Readdir has historically always returned a non-nil empty slice, never nil, |
| 47 |
// even on error (except misuse with nil receiver above). |
| 48 |
// Keep it that way to avoid breaking overly sensitive callers. |
| 49 |
infos = []FileInfo{} |
| 50 |
} |
| 51 |
return infos, err |
| 52 |
} |
| 53 |
|
| 54 |
// Readdirnames reads the contents of the directory associated with file |
| 55 |
// and returns a slice of up to n names of files in the directory, |
| 56 |
// in directory order. Subsequent calls on the same file will yield |
| 57 |
// further names. |
| 58 |
// |
| 59 |
// If n > 0, Readdirnames returns at most n names. In this case, if |
| 60 |
// Readdirnames returns an empty slice, it will return a non-nil error |
| 61 |
// explaining why. At the end of a directory, the error is [io.EOF]. |
| 62 |
// |
| 63 |
// If n <= 0, Readdirnames returns all the names from the directory in |
| 64 |
// a single slice. In this case, if Readdirnames succeeds (reads all |
| 65 |
// the way to the end of the directory), it returns the slice and a |
| 66 |
// nil error. If it encounters an error before the end of the |
| 67 |
// directory, Readdirnames returns the names read until that point and |
| 68 |
// a non-nil error. |
| 69 |
func (f *File) Readdirnames(n int) (names []string, err error) { |
| 70 |
if f == nil { |
| 71 |
return nil, ErrInvalid |
| 72 |
} |
| 73 |
names, _, _, err = f.readdir(n, readdirName) |
| 74 |
if names == nil { |
| 75 |
// Readdirnames has historically always returned a non-nil empty slice, never nil, |
| 76 |
// even on error (except misuse with nil receiver above). |
| 77 |
// Keep it that way to avoid breaking overly sensitive callers. |
| 78 |
names = []string{} |
| 79 |
} |
| 80 |
return names, err |
| 81 |
} |
| 82 |
|
| 83 |
// A DirEntry is an entry read from a directory |
| 84 |
// (using the [ReadDir] function or a [File.ReadDir] method). |
| 85 |
type DirEntry = fs.DirEntry |
| 86 |
|
| 87 |
// ReadDir reads the contents of the directory associated with the file f |
| 88 |
// and returns a slice of [DirEntry] values in directory order. |
| 89 |
// Subsequent calls on the same file will yield later DirEntry records in the directory. |
| 90 |
// |
| 91 |
// If n > 0, ReadDir returns at most n DirEntry records. |
| 92 |
// In this case, if ReadDir returns an empty slice, it will return an error explaining why. |
| 93 |
// At the end of a directory, the error is [io.EOF]. |
| 94 |
// |
| 95 |
// If n <= 0, ReadDir returns all the DirEntry records remaining in the directory. |
| 96 |
// When it succeeds, it returns a nil error (not io.EOF). |
| 97 |
func (f *File) ReadDir(n int) ([]DirEntry, error) { |
| 98 |
if f == nil { |
| 99 |
return nil, ErrInvalid |
| 100 |
} |
| 101 |
_, dirents, _, err := f.readdir(n, readdirDirEntry) |
| 102 |
if dirents == nil { |
| 103 |
// Match Readdir and Readdirnames: don't return nil slices. |
| 104 |
dirents = []DirEntry{} |
| 105 |
} |
| 106 |
return dirents, err |
| 107 |
} |
| 108 |
|
| 109 |
// ReadDir reads the named directory, |
| 110 |
// returning all its directory entries sorted by filename. |
| 111 |
// If an error occurs reading the directory, |
| 112 |
// ReadDir returns the entries it was able to read before the error, |
| 113 |
// along with the error. |
| 114 |
func ReadDir(name string) ([]DirEntry, error) { |
| 115 |
f, err := openDir(name) |
| 116 |
if err != nil { |
| 117 |
return nil, err |
| 118 |
} |
| 119 |
defer f.Close() |
| 120 |
|
| 121 |
dirs, err := f.ReadDir(-1) |
| 122 |
slices.SortFunc(dirs, func(a, b DirEntry) int { |
| 123 |
return bytealg.CompareString(a.Name(), b.Name()) |
| 124 |
}) |
| 125 |
return dirs, err |
| 126 |
} |
| 127 |
|
| 128 |
// CopyFS copies the file system fsys into the directory dir, |
| 129 |
// creating dir if necessary. |
| 130 |
// |
| 131 |
// Files are created with mode 0o666 plus any execute permissions |
| 132 |
// from the source, and directories are created with mode 0o777 |
| 133 |
// (before umask). |
| 134 |
// |
| 135 |
// CopyFS will not overwrite existing files. If a file name in fsys |
| 136 |
// already exists in the destination, CopyFS will return an error |
| 137 |
// such that errors.Is(err, fs.ErrExist) will be true. |
| 138 |
// |
| 139 |
// Symbolic links in dir are followed. |
| 140 |
// |
| 141 |
// New files added to fsys (including if dir is a subdirectory of fsys) |
| 142 |
// while CopyFS is running are not guaranteed to be copied. |
| 143 |
// |
| 144 |
// Copying stops at and returns the first error encountered. |
| 145 |
func CopyFS(dir string, fsys fs.FS) error { |
| 146 |
return fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error { |
| 147 |
if err != nil { |
| 148 |
return err |
| 149 |
} |
| 150 |
|
| 151 |
fpath, err := filepathlite.Localize(path) |
| 152 |
if err != nil { |
| 153 |
return err |
| 154 |
} |
| 155 |
newPath := joinPath(dir, fpath) |
| 156 |
|
| 157 |
switch d.Type() { |
| 158 |
case ModeDir: |
| 159 |
return MkdirAll(newPath, 0777) |
| 160 |
case ModeSymlink: |
| 161 |
target, err := fs.ReadLink(fsys, path) |
| 162 |
if err != nil { |
| 163 |
return err |
| 164 |
} |
| 165 |
return Symlink(target, newPath) |
| 166 |
case 0: |
| 167 |
r, err := fsys.Open(path) |
| 168 |
if err != nil { |
| 169 |
return err |
| 170 |
} |
| 171 |
defer r.Close() |
| 172 |
info, err := r.Stat() |
| 173 |
if err != nil { |
| 174 |
return err |
| 175 |
} |
| 176 |
w, err := OpenFile(newPath, O_CREATE|O_EXCL|O_WRONLY, 0666|info.Mode()&0777) |
| 177 |
if err != nil { |
| 178 |
return err |
| 179 |
} |
| 180 |
|
| 181 |
if _, err := io.Copy(w, r); err != nil { |
| 182 |
w.Close() |
| 183 |
return &PathError{Op: "Copy", Path: newPath, Err: err} |
| 184 |
} |
| 185 |
return w.Close() |
| 186 |
default: |
| 187 |
return &PathError{Op: "CopyFS", Path: path, Err: ErrInvalid} |
| 188 |
} |
| 189 |
}) |
| 190 |
} |