...
Source file src/pkg/os/dir_unix.go
1
2
3
4
5
6
7 package os
8
9 import (
10 "io"
11 "runtime"
12 "syscall"
13 )
14
15
16 type dirInfo struct {
17 buf []byte
18 nbuf int
19 bufp int
20 }
21
22 const (
23
24 blockSize = 8192
25 )
26
27 func (d *dirInfo) close() {}
28
29 func (f *File) readdirnames(n int) (names []string, err error) {
30
31 if f.dirinfo == nil {
32 f.dirinfo = new(dirInfo)
33
34 f.dirinfo.buf = make([]byte, blockSize)
35 }
36 d := f.dirinfo
37
38 size := n
39 if size <= 0 {
40 size = 100
41 n = -1
42 }
43
44 names = make([]string, 0, size)
45 for n != 0 {
46
47 if d.bufp >= d.nbuf {
48 d.bufp = 0
49 var errno error
50 d.nbuf, errno = f.pfd.ReadDirent(d.buf)
51 runtime.KeepAlive(f)
52 if errno != nil {
53 return names, wrapSyscallError("readdirent", errno)
54 }
55 if d.nbuf <= 0 {
56 break
57 }
58 }
59
60
61 var nb, nc int
62 nb, nc, names = syscall.ParseDirent(d.buf[d.bufp:d.nbuf], n, names)
63 d.bufp += nb
64 n -= nc
65 }
66 if n >= 0 && len(names) == 0 {
67 return names, io.EOF
68 }
69 return names, nil
70 }
71
View as plain text