Commit fe0c36a5 authored by Han-Wen Nienhuys's avatar Han-Wen Nienhuys

Cleanups.

parent c8a1ba96
......@@ -135,25 +135,6 @@ func TestTouch(t *testing.T) {
}
}
func TestReadLarge(t *testing.T) {
ts := NewTestCase(t)
defer ts.Cleanup()
// Add a bit more to test the splicing at the end.
content := make([]byte, 1024*1024+43)
for i := range content {
content[i] = byte(i)
}
err := ioutil.WriteFile(ts.origFile, []byte(content), 0644)
CheckSuccess(err)
back, err := ioutil.ReadFile(ts.mountFile)
CheckSuccess(err)
if bytes.Compare(content, back) != 0 {
t.Errorf("content comparison failed")
}
}
func TestReadThrough(t *testing.T) {
ts := NewTestCase(t)
defer ts.Cleanup()
......@@ -545,63 +526,22 @@ func TestFSync(t *testing.T) {
f.Close()
}
func TestLargeRead(t *testing.T) {
tc := NewTestCase(t)
defer tc.Cleanup()
t.Log("Testing large read.")
name := filepath.Join(tc.orig, "large")
f, err := os.OpenFile(name, os.O_WRONLY|os.O_CREATE, 0777)
CheckSuccess(err)
b := bytes.NewBuffer(nil)
for i := 0; i < 20*1024; i++ {
b.WriteString("bla")
}
b.WriteString("something extra to not be round")
slice := b.Bytes()
n, err := f.Write(slice)
CheckSuccess(err)
err = f.Close()
CheckSuccess(err)
func TestReadLarge(t *testing.T) {
ts := NewTestCase(t)
defer ts.Cleanup()
// Read in one go.
g, err := os.Open(filepath.Join(tc.mnt, "large"))
CheckSuccess(err)
readSlice := make([]byte, len(slice))
m, err := g.Read(readSlice)
if m != n {
t.Errorf("read mismatch %v %v", m, n)
}
for i, v := range readSlice {
if slice[i] != v {
t.Errorf("char mismatch %v %v %v", i, slice[i], v)
break
}
// Add a bit more to test the splicing at the end.
content := make([]byte, 1024*1024+43)
for i := range content {
content[i] = byte(i)
}
err := ioutil.WriteFile(ts.origFile, []byte(content), 0644)
CheckSuccess(err)
g.Close()
// Read in chunks
g, err = os.Open(filepath.Join(tc.mnt, "large"))
back, err := ioutil.ReadFile(ts.mountFile)
CheckSuccess(err)
defer g.Close()
readSlice = make([]byte, 4096)
total := 0
for {
m, err := g.Read(readSlice)
if m == 0 && err == io.EOF {
break
}
CheckSuccess(err)
total += m
}
if total != len(slice) {
t.Errorf("slice error %d", total)
if bytes.Compare(content, back) != 0 {
t.Errorf("content comparison failed")
}
}
......
......@@ -342,7 +342,7 @@ func (ms *MountState) write(req *request) Status {
return OK
}
header := req.serializeHeader()
header := req.serializeHeader(req.flatData.Size())
if ms.Debug {
log.Println(req.OutputDebug())
}
......@@ -361,13 +361,13 @@ func (ms *MountState) write(req *request) Status {
}
if req.flatData.FdSize > 0 {
if err := ms.TrySplice(header, req); err == nil {
if err := ms.TrySplice(header, req, req.flatData.Fd, req.flatData.FdSize, req.flatData.FdOff); err == nil {
return OK
} else {
log.Println("Splice error", err)
buf := ms.AllocOut(req, uint32(req.flatData.FdSize))
req.flatData.Read(buf)
header = req.serializeHeader()
header = req.serializeHeader(req.flatData.Size())
}
}
......@@ -375,14 +375,15 @@ func (ms *MountState) write(req *request) Status {
return ToStatus(err)
}
func (ms *MountState) TrySplice(header []byte, req *request) error {
func (ms *MountState) TrySplice(header []byte, req *request,
fd uintptr, size int, off int64) error {
finalSplice, err := splice.Get()
if err != nil {
return err
}
defer splice.Done(finalSplice)
total := len(header) + req.flatData.FdSize
total := len(header) + size
if !finalSplice.Grow(total) {
return fmt.Errorf("splice.Grow failed.")
}
......@@ -393,12 +394,12 @@ func (ms *MountState) TrySplice(header []byte, req *request) error {
}
var n int
if req.flatData.FdOff < 0 {
n, err = finalSplice.LoadFrom(req.flatData.Fd, req.flatData.FdSize)
if off < 0 {
n, err = finalSplice.LoadFrom(fd, size)
} else {
n, err = finalSplice.LoadFromAt(req.flatData.Fd, req.flatData.FdSize, req.flatData.FdOff)
n, err = finalSplice.LoadFromAt(fd, size, off)
}
if err == io.EOF || (err == nil && n < req.flatData.FdSize && n > 0) {
if err == io.EOF || (err == nil && n < size && n > 0) {
discard := make([]byte, len(header))
_, err = finalSplice.Read(discard)
if err != nil {
......@@ -406,11 +407,8 @@ func (ms *MountState) TrySplice(header []byte, req *request) error {
}
// TODO - fix debug output.
req.flatData.FdSize = n
req.flatData.Fd = finalSplice.ReadFd()
req.flatData.FdOff = -1
header = req.serializeHeader()
return ms.TrySplice(header, req)
header = req.serializeHeader(n)
return ms.TrySplice(header, req, fd, n, -1)
}
if err != nil {
......@@ -418,7 +416,7 @@ func (ms *MountState) TrySplice(header []byte, req *request) error {
return err
}
if n != req.flatData.FdSize {
if n != size {
return fmt.Errorf("splice: wrote %d, want %d", n, req.flatData.FdSize)
}
......
......@@ -179,7 +179,7 @@ func (r *request) parse() {
r.outData = unsafe.Pointer(&r.outBuf[sizeOfOutHeader])
}
func (r *request) serializeHeader() (header []byte) {
func (r *request) serializeHeader(dataSize int) (header []byte) {
dataLength := r.handler.OutputSize
if r.outData == nil || r.status > OK {
dataLength = 0
......@@ -191,7 +191,7 @@ func (r *request) serializeHeader() (header []byte) {
o.Unique = r.inHeader.Unique
o.Status = int32(-r.status)
o.Length = uint32(
int(sizeOfOutHeader) + int(dataLength) + r.flatData.Size())
int(sizeOfOutHeader) + int(dataLength) + dataSize)
var asSlice []byte
toSlice(&asSlice, r.outData, dataLength)
......
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