浏览代码

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 <tobias.klauser@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Benny Siegert <bsiegert@gmail.com>
Tobias Klauser 6 年之前
父节点
当前提交
e93b963712
共有 1 个文件被更改,包括 6 次插入9 次删除
  1. 6 9
      unix/getdirentries_test.go

+ 6 - 9
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, ":") {