github.com/Andyfoo/golang/x/sys@v0.0.0-20190901054642-57c1bf301704/unix/dirent_test.go (about)

     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 aix darwin dragonfly freebsd linux netbsd openbsd solaris
     6  
     7  package unix_test
     8  
     9  import (
    10  	"bytes"
    11  	"fmt"
    12  	"io/ioutil"
    13  	"os"
    14  	"path/filepath"
    15  	"runtime"
    16  	"sort"
    17  	"strconv"
    18  	"strings"
    19  	"testing"
    20  	"unsafe"
    21  
    22  	"github.com/Andyfoo/golang/x/sys/unix"
    23  )
    24  
    25  func TestDirent(t *testing.T) {
    26  	const (
    27  		direntBufSize   = 2048
    28  		filenameMinSize = 11
    29  	)
    30  
    31  	d, err := ioutil.TempDir("", "dirent-test")
    32  	if err != nil {
    33  		t.Fatalf("tempdir: %v", err)
    34  	}
    35  	defer os.RemoveAll(d)
    36  	t.Logf("tmpdir: %s", d)
    37  
    38  	for i, c := range []byte("0123456789") {
    39  		name := string(bytes.Repeat([]byte{c}, filenameMinSize+i))
    40  		err = ioutil.WriteFile(filepath.Join(d, name), nil, 0644)
    41  		if err != nil {
    42  			t.Fatalf("writefile: %v", err)
    43  		}
    44  	}
    45  
    46  	buf := bytes.Repeat([]byte("DEADBEAF"), direntBufSize/8)
    47  	fd, err := unix.Open(d, unix.O_RDONLY, 0)
    48  	if err != nil {
    49  		t.Fatalf("Open: %v", err)
    50  	}
    51  	defer unix.Close(fd)
    52  	n, err := unix.ReadDirent(fd, buf)
    53  	if err != nil {
    54  		t.Fatalf("ReadDirent: %v", err)
    55  	}
    56  	buf = buf[:n]
    57  
    58  	names := make([]string, 0, 10)
    59  	for len(buf) > 0 {
    60  		var bc int
    61  		bc, _, names = unix.ParseDirent(buf, -1, names)
    62  		if bc == 0 && len(buf) > 0 {
    63  			t.Fatal("no progress")
    64  		}
    65  		buf = buf[bc:]
    66  	}
    67  
    68  	sort.Strings(names)
    69  	t.Logf("names: %q", names)
    70  
    71  	if len(names) != 10 {
    72  		t.Errorf("got %d names; expected 10", len(names))
    73  	}
    74  	for i, name := range names {
    75  		ord, err := strconv.Atoi(name[:1])
    76  		if err != nil {
    77  			t.Fatalf("names[%d] is non-integer %q: %v", i, names[i], err)
    78  		}
    79  		if expected := string(strings.Repeat(name[:1], filenameMinSize+ord)); name != expected {
    80  			t.Errorf("names[%d] is %q (len %d); expected %q (len %d)", i, name, len(name), expected, len(expected))
    81  		}
    82  	}
    83  }
    84  
    85  func TestDirentRepeat(t *testing.T) {
    86  	const N = 100
    87  	// Note: the size of the buffer is small enough that the loop
    88  	// below will need to execute multiple times. See issue #31368.
    89  	size := N * unsafe.Offsetof(unix.Dirent{}.Name) / 4
    90  	if runtime.GOOS == "freebsd" || runtime.GOOS == "netbsd" {
    91  		if size < 1024 {
    92  			size = 1024 // DIRBLKSIZ, see issue 31403.
    93  		}
    94  		if runtime.GOOS == "freebsd" {
    95  			t.Skip("need to fix issue 31416 first")
    96  		}
    97  	}
    98  
    99  	// Make a directory containing N files
   100  	d, err := ioutil.TempDir("", "direntRepeat-test")
   101  	if err != nil {
   102  		t.Fatalf("tempdir: %v", err)
   103  	}
   104  	defer os.RemoveAll(d)
   105  
   106  	var files []string
   107  	for i := 0; i < N; i++ {
   108  		files = append(files, fmt.Sprintf("file%d", i))
   109  	}
   110  	for _, file := range files {
   111  		err = ioutil.WriteFile(filepath.Join(d, file), []byte("contents"), 0644)
   112  		if err != nil {
   113  			t.Fatalf("writefile: %v", err)
   114  		}
   115  	}
   116  
   117  	// Read the directory entries using ReadDirent.
   118  	fd, err := unix.Open(d, unix.O_RDONLY, 0)
   119  	if err != nil {
   120  		t.Fatalf("Open: %v", err)
   121  	}
   122  	defer unix.Close(fd)
   123  	var files2 []string
   124  	for {
   125  		buf := make([]byte, size)
   126  		n, err := unix.ReadDirent(fd, buf)
   127  		if err != nil {
   128  			t.Fatalf("ReadDirent: %v", err)
   129  		}
   130  		if n == 0 {
   131  			break
   132  		}
   133  		buf = buf[:n]
   134  		for len(buf) > 0 {
   135  			var consumed int
   136  			consumed, _, files2 = unix.ParseDirent(buf, -1, files2)
   137  			if consumed == 0 && len(buf) > 0 {
   138  				t.Fatal("no progress")
   139  			}
   140  			buf = buf[consumed:]
   141  		}
   142  	}
   143  
   144  	// Check results
   145  	sort.Strings(files)
   146  	sort.Strings(files2)
   147  	if strings.Join(files, "|") != strings.Join(files2, "|") {
   148  		t.Errorf("bad file list: want\n%q\ngot\n%q", files, files2)
   149  	}
   150  }