mirror of
https://github.com/rwinkhart/sys.git
synced 2026-09-02 23:27:31 -04:00
unix: in TestDirent, make as many ReadDirent calls as are needed
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 <bcmills@google.com> Run-TryBot: M Zhuo <mzh@golangcn.org> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
+26
-14
@@ -23,7 +23,7 @@ import (
|
|||||||
|
|
||||||
func TestDirent(t *testing.T) {
|
func TestDirent(t *testing.T) {
|
||||||
const (
|
const (
|
||||||
direntBufSize = 2048
|
direntBufSize = 2048 // arbitrary? See https://go.dev/issue/37323.
|
||||||
filenameMinSize = 11
|
filenameMinSize = 11
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -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)
|
fd, err := unix.Open(d, unix.O_RDONLY, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Open: %v", err)
|
t.Fatalf("Open: %v", err)
|
||||||
}
|
}
|
||||||
defer unix.Close(fd)
|
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)
|
buf := bytes.Repeat([]byte{0xCD}, direntBufSize)
|
||||||
for len(buf) > 0 {
|
for {
|
||||||
var bc int
|
n, err := unix.ReadDirent(fd, buf)
|
||||||
bc, _, names = unix.ParseDirent(buf, -1, names)
|
if err == unix.EINVAL {
|
||||||
if bc == 0 && len(buf) > 0 {
|
// On linux, 'man getdents64' says that EINVAL indicates result buffer is too small.
|
||||||
t.Fatal("no progress")
|
// 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)
|
sort.Strings(names)
|
||||||
|
|||||||
Reference in New Issue
Block a user