Source file
src/syscall/getdirentries_test.go
1
2
3
4
5
6
7 package syscall_test
8
9 import (
10 "fmt"
11 "os"
12 "path/filepath"
13 "slices"
14 "strings"
15 "syscall"
16 "testing"
17 "unsafe"
18 )
19
20 func TestGetdirentries(t *testing.T) {
21 for _, count := range []int{10, 1000} {
22 t.Run(fmt.Sprintf("n=%d", count), func(t *testing.T) {
23 testGetdirentries(t, count)
24 })
25 }
26 }
27 func testGetdirentries(t *testing.T, count int) {
28 if count > 100 && testing.Short() && os.Getenv("GO_BUILDER_NAME") == "" {
29 t.Skip("skipping in -short mode")
30 }
31 d := t.TempDir()
32 var names []string
33 for i := 0; i < count; i++ {
34 names = append(names, fmt.Sprintf("file%03d", i))
35 }
36
37
38 for _, name := range names {
39 err := os.WriteFile(filepath.Join(d, name), []byte("data"), 0)
40 if err != nil {
41 t.Fatalf("WriteFile: %v", err)
42 }
43 }
44
45
46 var names2 []string
47 fd, err := syscall.Open(d, syscall.O_RDONLY, 0)
48 if err != nil {
49 t.Fatalf("Open: %v", err)
50 }
51 defer syscall.Close(fd)
52 var base uintptr
53 var buf [2048]byte
54 for {
55 n, err := syscall.Getdirentries(fd, buf[:], &base)
56 if err != nil {
57 t.Fatalf("Getdirentries: %v", err)
58 }
59 if n == 0 {
60 break
61 }
62 data := buf[:n]
63 for len(data) > 0 {
64
65
66 var dirent syscall.Dirent
67 copy((*[unsafe.Sizeof(dirent)]byte)(unsafe.Pointer(&dirent))[:], data)
68
69 data = data[dirent.Reclen:]
70 name := make([]byte, dirent.Namlen)
71 for i := 0; i < int(dirent.Namlen); i++ {
72 name[i] = byte(dirent.Name[i])
73 }
74 names2 = append(names2, string(name))
75 }
76 }
77
78 names = append(names, ".", "..")
79 slices.Sort(names)
80 slices.Sort(names2)
81 if strings.Join(names, ":") != strings.Join(names2, ":") {
82 t.Errorf("names don't match\n names: %q\nnames2: %q", names, names2)
83 }
84 }
85
View as plain text