Black Lives Matter. Support the Equal Justice Initiative.

Source file src/syscall/getdirentries_test.go

Documentation: syscall

     1  // Copyright 2019 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build darwin freebsd netbsd openbsd
     6  
     7  package syscall_test
     8  
     9  import (
    10  	"fmt"
    11  	"io/ioutil"
    12  	"os"
    13  	"path/filepath"
    14  	"sort"
    15  	"strings"
    16  	"syscall"
    17  	"testing"
    18  	"unsafe"
    19  )
    20  
    21  func TestGetdirentries(t *testing.T) {
    22  	for _, count := range []int{10, 1000} {
    23  		t.Run(fmt.Sprintf("n=%d", count), func(t *testing.T) {
    24  			testGetdirentries(t, count)
    25  		})
    26  	}
    27  }
    28  func testGetdirentries(t *testing.T, count int) {
    29  	if count > 100 && testing.Short() && os.Getenv("GO_BUILDER_NAME") == "" {
    30  		t.Skip("skipping in -short mode")
    31  	}
    32  	d, err := ioutil.TempDir("", "getdirentries-test")
    33  	if err != nil {
    34  		t.Fatalf("Tempdir: %v", err)
    35  	}
    36  	defer os.RemoveAll(d)
    37  	var names []string
    38  	for i := 0; i < count; i++ {
    39  		names = append(names, fmt.Sprintf("file%03d", i))
    40  	}
    41  
    42  	// Make files in the temp directory
    43  	for _, name := range names {
    44  		err := ioutil.WriteFile(filepath.Join(d, name), []byte("data"), 0)
    45  		if err != nil {
    46  			t.Fatalf("WriteFile: %v", err)
    47  		}
    48  	}
    49  
    50  	// Read files using Getdirentries
    51  	var names2 []string
    52  	fd, err := syscall.Open(d, syscall.O_RDONLY, 0)
    53  	if err != nil {
    54  		t.Fatalf("Open: %v", err)
    55  	}
    56  	defer syscall.Close(fd)
    57  	var base uintptr
    58  	var buf [2048]byte
    59  	for {
    60  		n, err := syscall.Getdirentries(fd, buf[:], &base)
    61  		if err != nil {
    62  			t.Fatalf("Getdirentries: %v", err)
    63  		}
    64  		if n == 0 {
    65  			break
    66  		}
    67  		data := buf[:n]
    68  		for len(data) > 0 {
    69  			// If multiple Dirents are written into buf, sometimes when we reach the final one,
    70  			// we have cap(buf) < Sizeof(Dirent). So use an appropriate slice to copy from data.
    71  			var dirent syscall.Dirent
    72  			copy((*[unsafe.Sizeof(dirent)]byte)(unsafe.Pointer(&dirent))[:], data)
    73  
    74  			data = data[dirent.Reclen:]
    75  			name := make([]byte, dirent.Namlen)
    76  			for i := 0; i < int(dirent.Namlen); i++ {
    77  				name[i] = byte(dirent.Name[i])
    78  			}
    79  			names2 = append(names2, string(name))
    80  		}
    81  	}
    82  
    83  	names = append(names, ".", "..") // Getdirentries returns these also
    84  	sort.Strings(names)
    85  	sort.Strings(names2)
    86  	if strings.Join(names, ":") != strings.Join(names2, ":") {
    87  		t.Errorf("names don't match\n names: %q\nnames2: %q", names, names2)
    88  	}
    89  }
    90  

View as plain text