|
| 1 | +// +build go1.16,!js |
| 2 | + |
| 3 | +// Copyright 2016 The Go Authors. All rights reserved. |
| 4 | +// Use of this source code is governed by a BSD-style |
| 5 | +// license that can be found in the LICENSE file. |
| 6 | + |
| 7 | +package os |
| 8 | + |
| 9 | +import ( |
| 10 | + "io/fs" |
| 11 | + "sort" |
| 12 | +) |
| 13 | + |
| 14 | +type readdirMode int |
| 15 | + |
| 16 | +const ( |
| 17 | + readdirName readdirMode = iota |
| 18 | + readdirDirEntry |
| 19 | + readdirFileInfo |
| 20 | +) |
| 21 | + |
| 22 | +// Readdir reads the contents of the directory associated with file and |
| 23 | +// returns a slice of up to n FileInfo values, as would be returned |
| 24 | +// by Lstat, in directory order. Subsequent calls on the same file will yield |
| 25 | +// further FileInfos. |
| 26 | +// |
| 27 | +// If n > 0, Readdir returns at most n FileInfo structures. In this case, if |
| 28 | +// Readdir returns an empty slice, it will return a non-nil error |
| 29 | +// explaining why. At the end of a directory, the error is io.EOF. |
| 30 | +// |
| 31 | +// If n <= 0, Readdir returns all the FileInfo from the directory in |
| 32 | +// a single slice. In this case, if Readdir succeeds (reads all |
| 33 | +// the way to the end of the directory), it returns the slice and a |
| 34 | +// nil error. If it encounters an error before the end of the |
| 35 | +// directory, Readdir returns the FileInfo read until that point |
| 36 | +// and a non-nil error. |
| 37 | +// |
| 38 | +// Most clients are better served by the more efficient ReadDir method. |
| 39 | +func (f *File) Readdir(n int) ([]FileInfo, error) { |
| 40 | + if f == nil { |
| 41 | + return nil, ErrInvalid |
| 42 | + } |
| 43 | + _, _, infos, err := f.readdir(n, readdirFileInfo) |
| 44 | + if infos == nil { |
| 45 | + // Readdir has historically always returned a non-nil empty slice, never nil, |
| 46 | + // even on error (except misuse with nil receiver above). |
| 47 | + // Keep it that way to avoid breaking overly sensitive callers. |
| 48 | + infos = []FileInfo{} |
| 49 | + } |
| 50 | + return infos, err |
| 51 | +} |
| 52 | + |
| 53 | +// Readdirnames reads the contents of the directory associated with file |
| 54 | +// and returns a slice of up to n names of files in the directory, |
| 55 | +// in directory order. Subsequent calls on the same file will yield |
| 56 | +// further names. |
| 57 | +// |
| 58 | +// If n > 0, Readdirnames returns at most n names. In this case, if |
| 59 | +// Readdirnames returns an empty slice, it will return a non-nil error |
| 60 | +// explaining why. At the end of a directory, the error is io.EOF. |
| 61 | +// |
| 62 | +// If n <= 0, Readdirnames returns all the names from the directory in |
| 63 | +// a single slice. In this case, if Readdirnames succeeds (reads all |
| 64 | +// the way to the end of the directory), it returns the slice and a |
| 65 | +// nil error. If it encounters an error before the end of the |
| 66 | +// directory, Readdirnames returns the names read until that point and |
| 67 | +// a non-nil error. |
| 68 | +func (f *File) Readdirnames(n int) (names []string, err error) { |
| 69 | + if f == nil { |
| 70 | + return nil, ErrInvalid |
| 71 | + } |
| 72 | + names, _, _, err = f.readdir(n, readdirName) |
| 73 | + if names == nil { |
| 74 | + // Readdirnames has historically always returned a non-nil empty slice, never nil, |
| 75 | + // even on error (except misuse with nil receiver above). |
| 76 | + // Keep it that way to avoid breaking overly sensitive callers. |
| 77 | + names = []string{} |
| 78 | + } |
| 79 | + return names, err |
| 80 | +} |
| 81 | + |
| 82 | +// A DirEntry is an entry read from a directory |
| 83 | +// (using the ReadDir function or a File's ReadDir method). |
| 84 | +type DirEntry = fs.DirEntry |
| 85 | + |
| 86 | +// ReadDir reads the contents of the directory associated with the file f |
| 87 | +// and returns a slice of DirEntry values in directory order. |
| 88 | +// Subsequent calls on the same file will yield later DirEntry records in the directory. |
| 89 | +// |
| 90 | +// If n > 0, ReadDir returns at most n DirEntry records. |
| 91 | +// In this case, if ReadDir returns an empty slice, it will return an error explaining why. |
| 92 | +// At the end of a directory, the error is io.EOF. |
| 93 | +// |
| 94 | +// If n <= 0, ReadDir returns all the DirEntry records remaining in the directory. |
| 95 | +// When it succeeds, it returns a nil error (not io.EOF). |
| 96 | +func (f *File) ReadDir(n int) ([]DirEntry, error) { |
| 97 | + if f == nil { |
| 98 | + return nil, ErrInvalid |
| 99 | + } |
| 100 | + _, dirents, _, err := f.readdir(n, readdirDirEntry) |
| 101 | + if dirents == nil { |
| 102 | + // Match Readdir and Readdirnames: don't return nil slices. |
| 103 | + dirents = []DirEntry{} |
| 104 | + } |
| 105 | + return dirents, err |
| 106 | +} |
| 107 | + |
| 108 | +// testingForceReadDirLstat forces ReadDir to call Lstat, for testing that code path. |
| 109 | +// This can be difficult to provoke on some Unix systems otherwise. |
| 110 | +var testingForceReadDirLstat bool |
| 111 | + |
| 112 | +// ReadDir reads the named directory, |
| 113 | +// returning all its directory entries sorted by filename. |
| 114 | +// If an error occurs reading the directory, |
| 115 | +// ReadDir returns the entries it was able to read before the error, |
| 116 | +// along with the error. |
| 117 | +func ReadDir(name string) ([]DirEntry, error) { |
| 118 | + f, err := Open(name) |
| 119 | + if err != nil { |
| 120 | + return nil, err |
| 121 | + } |
| 122 | + defer f.Close() |
| 123 | + |
| 124 | + dirs, err := f.ReadDir(-1) |
| 125 | + sort.Slice(dirs, func(i, j int) bool { return dirs[i].Name() < dirs[j].Name() }) |
| 126 | + return dirs, err |
| 127 | +} |
0 commit comments