Skip to content

Commit

Permalink
fix CopyTo for files and sockets
Browse files Browse the repository at this point in the history
was ignoring buffering
  • Loading branch information
apmckinlay committed Nov 20, 2024
1 parent 0376aef commit 4226aec
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 22 deletions.
20 changes: 1 addition & 19 deletions builtin/copyto.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
package builtin

import (
"fmt"
"io"
"sync"

. "github.com/apmckinlay/gsuneido/core"
)
Expand All @@ -16,12 +14,6 @@ type writer interface {
writer() io.Writer
}

var bufs = sync.Pool{New: func() any {
return new([rwsize]byte)
}}

const rwsize = 32 * 1024 // same as Go io.Copy

// CopyTo copies from src to to, up to nbytes or until src eof.
// Called by CopyTo in file, socket, and runpiped.
func CopyTo(th *Thread, src io.Reader, to, nbytes Value) Value {
Expand All @@ -31,13 +23,6 @@ func CopyTo(th *Thread, src io.Reader, to, nbytes Value) Value {
}
dst := tow.writer()

if _, ok = src.(io.WriterTo); !ok {
fmt.Println("ERROR: CopyTo: src should have WriteTo")
}
if _, ok = dst.(io.ReaderFrom); !ok {
fmt.Println("ERROR: CopyTo: dst should have ReadFrom")
}

var n int64
if nbytes != False {
n = ToInt64(nbytes)
Expand All @@ -46,10 +31,7 @@ func CopyTo(th *Thread, src io.Reader, to, nbytes Value) Value {
}
src = io.LimitReader(src, int64(n))
}

array := bufs.Get().(*[rwsize]byte)
defer bufs.Put(array)
nw, err := io.CopyBuffer(dst, src, array[:]) // the actual copy
nw, err := dst.(io.ReaderFrom).ReadFrom(src)
if err != nil {
panic("ERROR: CopyTo: " + err.Error())
}
Expand Down
4 changes: 2 additions & 2 deletions builtin/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,11 @@ func file_Read(this, arg Value) Value {
var _ = method(file_CopyTo, "(dest, nbytes = false)")

func file_CopyTo(th *Thread, this Value, args []Value) Value {
return CopyTo(th, sfOpenRead(this).f, args[0], args[1])
return CopyTo(th, sfOpenRead(this).r, args[0], args[1])
}

func (sf *suFile) writer() io.Writer {
return sfOpenWrite(sf).f
return sfOpenWrite(sf).w
}

var _ = method(file_Readline, "()")
Expand Down
2 changes: 1 addition & 1 deletion builtin/socketclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func sock_Writeline(this, arg Value) Value {
var _ = method(sock_CopyTo, "(dest, nbytes = false)")

func sock_CopyTo(th *Thread, this Value, args []Value) Value {
return CopyTo(th, scOpen(this).conn, args[0], args[1])
return CopyTo(th, scOpen(this).rdr, args[0], args[1])
}

func (sc *suSocketClient) writer() io.Writer {
Expand Down

0 comments on commit 4226aec

Please sign in to comment.