Source file
src/syscall/dirent_test.go
1
2
3
4
5
6
7 package syscall_test
8
9 import (
10 "bytes"
11 "fmt"
12 "os"
13 "path/filepath"
14 "runtime"
15 "sort"
16 "strconv"
17 "strings"
18 "syscall"
19 "testing"
20 "unsafe"
21 )
22
23 func TestDirent(t *testing.T) {
24 const (
25 direntBufSize = 2048
26 filenameMinSize = 11
27 )
28
29 d := t.TempDir()
30 t.Logf("tmpdir: %s", d)
31
32 for i, c := range []byte("0123456789") {
33 name := string(bytes.Repeat([]byte{c}, filenameMinSize+i))
34 err := os.WriteFile(filepath.Join(d, name), nil, 0644)
35 if err != nil {
36 t.Fatalf("writefile: %v", err)
37 }
38 }
39
40 names := make([]string, 0, 10)
41
42 fd, err := syscall.Open(d, syscall.O_RDONLY, 0)
43 if err != nil {
44 t.Fatalf("syscall.open: %v", err)
45 }
46 defer syscall.Close(fd)
47
48 buf := bytes.Repeat([]byte{0xCD}, direntBufSize)
49 for {
50 n, err := syscall.ReadDirent(fd, buf)
51 if err == syscall.EINVAL {
52
53
54 t.Logf("ReadDirent: %v; retrying with larger buffer", err)
55 buf = bytes.Repeat([]byte{0xCD}, len(buf)*2)
56 continue
57 }
58 if err != nil {
59 t.Fatalf("syscall.readdir: %v", err)
60 }
61 t.Logf("ReadDirent: read %d bytes", n)
62 if n == 0 {
63 break
64 }
65
66 var consumed, count int
67 consumed, count, names = syscall.ParseDirent(buf[:n], -1, names)
68 t.Logf("ParseDirent: %d new name(s)", count)
69 if consumed != n {
70 t.Fatalf("ParseDirent: consumed %d bytes; expected %d", consumed, n)
71 }
72 }
73
74 sort.Strings(names)
75 t.Logf("names: %q", names)
76
77 if len(names) != 10 {
78 t.Errorf("got %d names; expected 10", len(names))
79 }
80 for i, name := range names {
81 ord, err := strconv.Atoi(name[:1])
82 if err != nil {
83 t.Fatalf("names[%d] is non-integer %q: %v", i, names[i], err)
84 }
85 if expected := string(strings.Repeat(name[:1], filenameMinSize+ord)); name != expected {
86 t.Errorf("names[%d] is %q (len %d); expected %q (len %d)", i, name, len(name), expected, len(expected))
87 }
88 }
89 }
90
91 func TestDirentRepeat(t *testing.T) {
92 const N = 100
93
94
95 size := N * unsafe.Offsetof(syscall.Dirent{}.Name) / 4
96 if runtime.GOOS == "freebsd" || runtime.GOOS == "netbsd" {
97 if size < 1024 {
98 size = 1024
99 }
100 if runtime.GOOS == "freebsd" {
101 t.Skip("need to fix issue 31416 first")
102 }
103 }
104
105
106 d := t.TempDir()
107
108 var files []string
109 for i := 0; i < N; i++ {
110 files = append(files, fmt.Sprintf("file%d", i))
111 }
112 for _, file := range files {
113 err := os.WriteFile(filepath.Join(d, file), []byte("contents"), 0644)
114 if err != nil {
115 t.Fatalf("writefile: %v", err)
116 }
117 }
118
119
120 fd, err := syscall.Open(d, syscall.O_RDONLY, 0)
121 if err != nil {
122 t.Fatalf("syscall.open: %v", err)
123 }
124 defer syscall.Close(fd)
125 var files2 []string
126 for {
127 buf := make([]byte, size)
128 n, err := syscall.ReadDirent(fd, buf)
129 if err != nil {
130 t.Fatalf("syscall.readdir: %v", err)
131 }
132 if n == 0 {
133 break
134 }
135 buf = buf[:n]
136 for len(buf) > 0 {
137 var consumed int
138 consumed, _, files2 = syscall.ParseDirent(buf, -1, files2)
139 buf = buf[consumed:]
140 }
141 }
142
143
144 sort.Strings(files)
145 sort.Strings(files2)
146 if strings.Join(files, "|") != strings.Join(files2, "|") {
147 t.Errorf("bad file list: want\n%q\ngot\n%q", files, files2)
148 }
149 }
150
View as plain text