2017-08-13 21:13:44 +02:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package syscallcompat
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2018-02-04 21:13:14 +01:00
|
|
|
"runtime"
|
2017-08-13 21:13:44 +02:00
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
"testing"
|
|
|
|
|
2018-02-01 23:46:02 +01:00
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
|
2020-05-17 14:18:23 +02:00
|
|
|
"github.com/hanwen/go-fuse/v2/fuse"
|
2017-08-13 21:13:44 +02:00
|
|
|
)
|
|
|
|
|
2018-02-04 21:13:14 +01:00
|
|
|
var emulate = false
|
2017-12-03 17:57:08 +01:00
|
|
|
|
2017-08-13 21:13:44 +02:00
|
|
|
func TestGetdents(t *testing.T) {
|
2017-12-03 17:57:08 +01:00
|
|
|
t.Logf("testing native getdents")
|
|
|
|
testGetdents(t)
|
|
|
|
t.Logf("testing emulateGetdents")
|
2018-02-04 21:13:14 +01:00
|
|
|
emulate = true
|
2017-12-03 17:57:08 +01:00
|
|
|
testGetdents(t)
|
|
|
|
}
|
|
|
|
|
2018-02-04 21:13:14 +01:00
|
|
|
// skipOnGccGo skips the emulateGetdents test when we are
|
|
|
|
// running linux and were compiled with gccgo. The test is known to fail
|
|
|
|
// (https://github.com/rfjakob/gocryptfs/issues/201), but getdents emulation
|
|
|
|
// is not used on linux, so let's skip the test and ignore the failure.
|
|
|
|
func skipOnGccGo(t *testing.T) {
|
|
|
|
if !emulate || runtime.GOOS != "linux" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// runtime.Version() output...
|
|
|
|
// on go: go1.9.2
|
|
|
|
// on gccgo: go1.8.1 gccgo (GCC) 7.2.1 20170915 (Red Hat 7.2.1-2)
|
|
|
|
v := runtime.Version()
|
|
|
|
if strings.Contains(v, "gccgo") {
|
|
|
|
t.Skipf("test is known-broken on gccgo")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-03 17:57:08 +01:00
|
|
|
func testGetdents(t *testing.T) {
|
2018-02-04 21:13:14 +01:00
|
|
|
getdentsUnderTest := getdents
|
|
|
|
if emulate {
|
|
|
|
getdentsUnderTest = emulateGetdents
|
|
|
|
}
|
2017-08-13 21:13:44 +02:00
|
|
|
// Fill a directory with filenames of length 1 ... 255
|
2017-12-03 17:57:08 +01:00
|
|
|
testDir, err := ioutil.TempDir(tmpDir, "TestGetdents")
|
2017-08-13 21:13:44 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-02-01 23:46:02 +01:00
|
|
|
for i := 1; i <= unix.NAME_MAX; i++ {
|
2017-08-13 21:13:44 +02:00
|
|
|
n := strings.Repeat("x", i)
|
|
|
|
err = ioutil.WriteFile(testDir+"/"+n, nil, 0600)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2017-10-22 18:13:08 +02:00
|
|
|
// "/", "/dev" and "/proc/self" are good test cases because they contain
|
|
|
|
// many different file types (block and char devices, symlinks,
|
|
|
|
// mountpoints).
|
|
|
|
dirs := []string{testDir, "/", "/dev", "/proc/self"}
|
2017-08-13 21:13:44 +02:00
|
|
|
for _, dir := range dirs {
|
|
|
|
// Read directory using stdlib Readdir()
|
|
|
|
fd, err := os.Open(dir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-12-03 17:57:08 +01:00
|
|
|
defer fd.Close()
|
2017-08-13 21:13:44 +02:00
|
|
|
readdirEntries, err := fd.Readdir(0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
readdirMap := make(map[string]*syscall.Stat_t)
|
|
|
|
for _, v := range readdirEntries {
|
|
|
|
readdirMap[v.Name()] = fuse.ToStatT(v)
|
|
|
|
}
|
2017-12-03 17:57:08 +01:00
|
|
|
// Read using our Getdents() implementation
|
|
|
|
_, err = fd.Seek(0, 0) // Rewind directory
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-05-23 12:00:09 +02:00
|
|
|
getdentsEntries, special, err := getdentsUnderTest(int(fd.Fd()))
|
2017-08-13 21:13:44 +02:00
|
|
|
if err != nil {
|
2018-02-04 21:13:14 +01:00
|
|
|
t.Log(err)
|
|
|
|
skipOnGccGo(t)
|
|
|
|
t.FailNow()
|
2017-08-13 21:13:44 +02:00
|
|
|
}
|
|
|
|
getdentsMap := make(map[string]fuse.DirEntry)
|
|
|
|
for _, v := range getdentsEntries {
|
|
|
|
getdentsMap[v.Name] = v
|
|
|
|
}
|
|
|
|
// Compare results
|
|
|
|
if len(getdentsEntries) != len(readdirEntries) {
|
|
|
|
t.Fatalf("len(getdentsEntries)=%d, len(readdirEntries)=%d",
|
|
|
|
len(getdentsEntries), len(readdirEntries))
|
|
|
|
}
|
|
|
|
for name := range readdirMap {
|
|
|
|
g := getdentsMap[name]
|
|
|
|
r := readdirMap[name]
|
|
|
|
rTyp := r.Mode & syscall.S_IFMT
|
|
|
|
if g.Mode != rTyp {
|
|
|
|
t.Errorf("%q: g.Mode=%#o, r.Mode=%#o", name, g.Mode, rTyp)
|
|
|
|
}
|
|
|
|
if g.Ino != r.Ino {
|
|
|
|
// The inode number of a directory that is reported by stat
|
|
|
|
// and getdents is different when it is a mountpoint. Only
|
|
|
|
// throw an error when we are NOT looking at a directory.
|
|
|
|
if g.Mode != syscall.S_IFDIR {
|
|
|
|
t.Errorf("%s: g.Ino=%d, r.Ino=%d", name, g.Ino, r.Ino)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-23 12:00:09 +02:00
|
|
|
if len(special) != 2 {
|
|
|
|
t.Error(special)
|
|
|
|
}
|
|
|
|
if !(special[0].Name == "." && special[1].Name == ".." ||
|
|
|
|
special[1].Name == "." && special[0].Name == "..") {
|
|
|
|
t.Error(special)
|
|
|
|
}
|
|
|
|
for _, v := range special {
|
|
|
|
if v.Ino == 0 {
|
|
|
|
t.Error(v)
|
|
|
|
}
|
|
|
|
if v.Mode != syscall.S_IFDIR {
|
|
|
|
t.Error(v)
|
|
|
|
}
|
|
|
|
}
|
2017-08-13 21:13:44 +02:00
|
|
|
}
|
|
|
|
}
|