Source file
src/os/dir_plan9.go
1
2
3
4
5 package os
6
7 import (
8 "io"
9 "io/fs"
10 "syscall"
11 )
12
13 func (file *File) readdir(n int, mode readdirMode) (names []string, dirents []DirEntry, infos []FileInfo, err error) {
14 var d *dirInfo
15 for {
16 d = file.dirinfo.Load()
17 if d != nil {
18 break
19 }
20 newD := new(dirInfo)
21 if file.dirinfo.CompareAndSwap(nil, newD) {
22 d = newD
23 break
24 }
25 }
26
27 d.mu.Lock()
28 defer d.mu.Unlock()
29
30 size := n
31 if size <= 0 {
32 size = 100
33 n = -1
34 }
35 for n != 0 {
36
37 if d.bufp >= d.nbuf {
38 nb, err := file.Read(d.buf[:])
39
40
41 d.bufp, d.nbuf = 0, nb
42
43 if err != nil {
44 if err == io.EOF {
45 break
46 }
47 return names, dirents, infos, &PathError{Op: "readdir", Path: file.name, Err: err}
48 }
49 if nb < syscall.STATFIXLEN {
50 return names, dirents, infos, &PathError{Op: "readdir", Path: file.name, Err: syscall.ErrShortStat}
51 }
52 }
53
54
55 b := d.buf[d.bufp:]
56 m := int(uint16(b[0])|uint16(b[1])<<8) + 2
57 if m < syscall.STATFIXLEN {
58 return names, dirents, infos, &PathError{Op: "readdir", Path: file.name, Err: syscall.ErrShortStat}
59 }
60
61 dir, err := syscall.UnmarshalDir(b[:m])
62 if err != nil {
63 return names, dirents, infos, &PathError{Op: "readdir", Path: file.name, Err: err}
64 }
65
66 if mode == readdirName {
67 names = append(names, dir.Name)
68 } else {
69 f := fileInfoFromStat(dir)
70 if mode == readdirDirEntry {
71 dirents = append(dirents, dirEntry{f})
72 } else {
73 infos = append(infos, f)
74 }
75 }
76 d.bufp += m
77 n--
78 }
79
80 if n > 0 && len(names)+len(dirents)+len(infos) == 0 {
81 return nil, nil, nil, io.EOF
82 }
83 return names, dirents, infos, nil
84 }
85
86 type dirEntry struct {
87 fs *fileStat
88 }
89
90 func (de dirEntry) Name() string { return de.fs.Name() }
91 func (de dirEntry) IsDir() bool { return de.fs.IsDir() }
92 func (de dirEntry) Type() FileMode { return de.fs.Mode().Type() }
93 func (de dirEntry) Info() (FileInfo, error) { return de.fs, nil }
94
95 func (de dirEntry) String() string {
96 return fs.FormatDirEntry(de)
97 }
98
View as plain text