Skip to content

Commit

Permalink
unix: in TestDirent, make as many ReadDirent calls as are needed
Browse files Browse the repository at this point in the history
This CL just port CL 376334 from main repo with minor modification.

Fixes golang/go#65015

Change-Id: I327d33bde39a2fcb818e28bcb7ff524ca19c4a38
Reviewed-on: https://go-review.googlesource.com/c/sys/+/554875
Reviewed-by: Bryan Mills <[email protected]>
Run-TryBot: M Zhuo <[email protected]>
Reviewed-by: Dmitri Shuralyov <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
  • Loading branch information
mengzhuo committed Jan 10, 2024
1 parent 0d9df52 commit f69d32a
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions unix/dirent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

func TestDirent(t *testing.T) {
const (
direntBufSize = 2048
direntBufSize = 2048 // arbitrary? See https://go.dev/issue/37323.
filenameMinSize = 11
)

Expand All @@ -38,26 +38,38 @@ func TestDirent(t *testing.T) {
}
}

buf := bytes.Repeat([]byte("DEADBEAF"), direntBufSize/8)
names := make([]string, 0, 10)

fd, err := unix.Open(d, unix.O_RDONLY, 0)
if err != nil {
t.Fatalf("Open: %v", err)
}
defer unix.Close(fd)
n, err := unix.ReadDirent(fd, buf)
if err != nil {
t.Fatalf("ReadDirent: %v", err)
}
buf = buf[:n]

names := make([]string, 0, 10)
for len(buf) > 0 {
var bc int
bc, _, names = unix.ParseDirent(buf, -1, names)
if bc == 0 && len(buf) > 0 {
t.Fatal("no progress")
buf := bytes.Repeat([]byte{0xCD}, direntBufSize)
for {
n, err := unix.ReadDirent(fd, buf)
if err == unix.EINVAL {
// On linux, 'man getdents64' says that EINVAL indicates result buffer is too small.
// Try a bigger buffer.
t.Logf("ReadDirent: %v; retrying with larger buffer", err)
buf = bytes.Repeat([]byte{0xCD}, len(buf)*2)
continue
}
if err != nil {
t.Fatalf("ReadDirent: %v", err)
}
t.Logf("ReadDirent: read %d bytes", n)
if n == 0 {
break
}

var consumed, count int
consumed, count, names = unix.ParseDirent(buf[:n], -1, names)
t.Logf("ParseDirent: %d new name(s)", count)
if consumed != n {
t.Fatalf("ParseDirent: consumed %d bytes; expected %d", consumed, n)
}
buf = buf[bc:]
}

sort.Strings(names)
Expand Down

0 comments on commit f69d32a

Please sign in to comment.