Commit 1989e683 authored by Han-Wen Nienhuys's avatar Han-Wen Nienhuys

Fix bug with dirent padding.

Add test case with random filename lengths.
parent a9d18e32
......@@ -542,7 +542,6 @@ func (de *DEntryList) AddString(name string, inode uint64, mode uint32) {
func (de *DEntryList) Add(name []byte, inode uint64, mode uint32) {
de.offset++
dirent := new(Dirent)
dirent.Off = de.offset
dirent.Ino = inode
......@@ -553,11 +552,11 @@ func (de *DEntryList) Add(name []byte, inode uint64, mode uint32) {
if err != nil {
panic("Serialization of Dirent failed")
}
de.buf.Write([]byte(name))
de.buf.WriteByte(0)
n := (len(name) + 1) % 8 // padding
if n != 0 {
de.buf.Write(make([]byte, 8-n))
de.buf.Write(name)
padding := 8 - len(name)&7
if padding < 8 {
de.buf.Write(make([]byte, padding))
}
}
......
......@@ -9,6 +9,7 @@ import (
"strings"
"testing"
"syscall"
"rand"
)
var _ = strings.Join
......@@ -505,18 +506,37 @@ func (self *testCase) testLargeRead() {
os.Remove(name)
}
func randomLengthString(length int) string {
r := rand.Intn(length)
j := 0
b := make([]byte, r)
for i := 0; i < r; i++ {
j = (j + 1) % 10
b[i] = byte(j) + byte('0')
}
return string(b)
}
func (self *testCase) testLargeDirRead() {
count := 100
created := 100
names := make([]string, count)
names := make([]string, created)
subdir := path.Join(self.origDir, "readdirSubdir")
os.Mkdir(subdir, 0700)
longname := "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
for i := 0; i < count; i++ {
nameSet := make(map[string]bool)
for i := 0; i < created; i++ {
// Should vary file name length.
name := path.Join(subdir, fmt.Sprintf("file%d", i))
base := fmt.Sprintf("file%d%s", i,
randomLengthString(len(longname)))
name := path.Join(subdir, base)
nameSet[base] = true
f, err := os.Open(name, os.O_WRONLY | os.O_CREATE, 0777)
if err != nil {
self.tester.Errorf("open write err %v", err)
......@@ -534,6 +554,7 @@ func (self *testCase) testLargeDirRead() {
}
// Chunked read.
total := 0
readSet := make(map[string] bool)
for {
namesRead, err := dir.Readdirnames(20)
if err != nil {
......@@ -543,13 +564,24 @@ func (self *testCase) testLargeDirRead() {
if len(namesRead) == 0 {
break
}
for _, v := range(namesRead) {
readSet[v] = true
}
total += len(namesRead)
}
if total != count {
self.tester.Errorf("readdir mismatch %v %v", total, count)
if total != created {
self.tester.Errorf("readdir mismatch got %v wanted %v", total, created)
}
for k, _ := range(nameSet) {
_, ok := readSet[k]
if !ok {
self.tester.Errorf("Name %v not found", k)
}
}
dir.Close()
os.RemoveAll(subdir)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment