From e93b9637120cb6fcf73855427b2c270ec97447d6 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Mon, 24 Jun 2019 10:12:38 +0200 Subject: [PATCH] unix: use ParseDirent in testGetdirentries Use ParseDirent to get the directory entries instead of manually parsing them. This fixes the test on dragonfly where type Dirent doesn't have a Reclen member. Manually tested on darwin, dragonfly and freebsd. Change-Id: I234c6aff78243fec8bba8784c1d4948fbbb4d027 Reviewed-on: https://go-review.googlesource.com/c/sys/+/183226 Run-TryBot: Tobias Klauser TryBot-Result: Gobot Gobot Reviewed-by: Benny Siegert --- unix/getdirentries_test.go | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/unix/getdirentries_test.go b/unix/getdirentries_test.go index 87819e70..7e9600b2 100644 --- a/unix/getdirentries_test.go +++ b/unix/getdirentries_test.go @@ -14,7 +14,6 @@ import ( "sort" "strings" "testing" - "unsafe" "golang.org/x/sys/unix" ) @@ -49,7 +48,6 @@ func testGetdirentries(t *testing.T, count int) { } // Read files using Getdirentries - var names2 []string fd, err := unix.Open(d, unix.O_RDONLY, 0) if err != nil { t.Fatalf("Open: %v", err) @@ -57,6 +55,7 @@ func testGetdirentries(t *testing.T, count int) { defer unix.Close(fd) var base uintptr var buf [2048]byte + names2 := make([]string, 0, count) for { n, err := unix.Getdirentries(fd, buf[:], &base) if err != nil { @@ -67,17 +66,15 @@ func testGetdirentries(t *testing.T, count int) { } data := buf[:n] for len(data) > 0 { - dirent := (*unix.Dirent)(unsafe.Pointer(&data[0])) - data = data[dirent.Reclen:] - name := make([]byte, dirent.Namlen) - for i := 0; i < int(dirent.Namlen); i++ { - name[i] = byte(dirent.Name[i]) + var bc int + bc, _, names2 = unix.ParseDirent(data, -1, names2) + if bc == 0 && len(data) > 0 { + t.Fatal("no progress") } - names2 = append(names2, string(name)) + data = data[bc:] } } - names = append(names, ".", "..") // Getdirentries returns these also sort.Strings(names) sort.Strings(names2) if strings.Join(names, ":") != strings.Join(names2, ":") {