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

Cleanups.

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