Source file
src/io/fs/readdir.go
1
2
3
4
5 package fs
6
7 import (
8 "errors"
9 "sort"
10 )
11
12
13
14 type ReadDirFS interface {
15 FS
16
17
18
19 ReadDir(name string) ([]DirEntry, error)
20 }
21
22
23
24
25
26
27
28 func ReadDir(fsys FS, name string) ([]DirEntry, error) {
29 if fsys, ok := fsys.(ReadDirFS); ok {
30 return fsys.ReadDir(name)
31 }
32
33 file, err := fsys.Open(name)
34 if err != nil {
35 return nil, err
36 }
37 defer file.Close()
38
39 dir, ok := file.(ReadDirFile)
40 if !ok {
41 return nil, &PathError{Op: "readdir", Path: name, Err: errors.New("not implemented")}
42 }
43
44 list, err := dir.ReadDir(-1)
45 sort.Slice(list, func(i, j int) bool { return list[i].Name() < list[j].Name() })
46 return list, err
47 }
48
49
50 type dirInfo struct {
51 fileInfo FileInfo
52 }
53
54 func (di dirInfo) IsDir() bool {
55 return di.fileInfo.IsDir()
56 }
57
58 func (di dirInfo) Type() FileMode {
59 return di.fileInfo.Mode().Type()
60 }
61
62 func (di dirInfo) Info() (FileInfo, error) {
63 return di.fileInfo, nil
64 }
65
66 func (di dirInfo) Name() string {
67 return di.fileInfo.Name()
68 }
69
70
71
72 func FileInfoToDirEntry(info FileInfo) DirEntry {
73 if info == nil {
74 return nil
75 }
76 return dirInfo{fileInfo: info}
77 }
78
View as plain text