From e6fd43728172853661fae8146febc1a2e366b6aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1ndly=20Gerg=C5=91?= Date: Wed, 20 Nov 2024 12:58:51 +0200 Subject: [PATCH 1/2] Allow changing chunk size and replace chan bool with chan struct --- clamd.go | 9 +++++---- clamdBuilder.go | 30 ++++++++++++++++++++++++++++++ go.mod | 3 +++ 3 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 clamdBuilder.go create mode 100644 go.mod diff --git a/clamd.go b/clamd.go index 5199f63..6845d5a 100644 --- a/clamd.go +++ b/clamd.go @@ -41,7 +41,8 @@ const ( ) type Clamd struct { - address string + address string + chunkSize int } type Stats struct { @@ -258,7 +259,7 @@ the actual chunk. Streaming is terminated by sending a zero-length chunk. Note: do not exceed StreamMaxLength as defined in clamd.conf, otherwise clamd will reply with INSTREAM size limit exceeded and close the connection */ -func (c *Clamd) ScanStream(r io.Reader, abort chan bool) (chan *ScanResult, error) { +func (c *Clamd) ScanStream(r io.Reader, abort chan struct{}) (chan *ScanResult, error) { conn, err := c.newConnection() if err != nil { return nil, err @@ -277,7 +278,7 @@ func (c *Clamd) ScanStream(r io.Reader, abort chan bool) (chan *ScanResult, erro conn.sendCommand("INSTREAM") for { - buf := make([]byte, CHUNK_SIZE) + buf := make([]byte, c.chunkSize) nr, err := r.Read(buf) if nr > 0 { @@ -306,6 +307,6 @@ func (c *Clamd) ScanStream(r io.Reader, abort chan bool) (chan *ScanResult, erro } func NewClamd(address string) *Clamd { - clamd := &Clamd{address: address} + clamd := &Clamd{address: address, chunkSize: CHUNK_SIZE} return clamd } diff --git a/clamdBuilder.go b/clamdBuilder.go new file mode 100644 index 0000000..a066b40 --- /dev/null +++ b/clamdBuilder.go @@ -0,0 +1,30 @@ +package clamd + +type Builder struct { + address string + chunkSize int +} + +func NewBuilder() *Builder { + return &Builder{ + address: "", + chunkSize: CHUNK_SIZE, + } +} + +func (b *Builder) WithAddress(address string) *Builder { + b.address = address + return b +} + +func (b *Builder) WithChunkSize(chunkSize int) *Builder { + b.chunkSize = chunkSize + return b +} + +func (b *Builder) Build() *Clamd { + return &Clamd{ + address: b.address, + chunkSize: b.chunkSize, + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..2344300 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/dutchcoders/go-clamd + +go 1.22.4 From e57115382a02784e830234d0643bd42564cd96e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1ndly=20Gerg=C5=91?= Date: Wed, 20 Nov 2024 13:06:13 +0200 Subject: [PATCH 2/2] Change abort to receive only channel --- clamd.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clamd.go b/clamd.go index 6845d5a..0477d8d 100644 --- a/clamd.go +++ b/clamd.go @@ -259,7 +259,7 @@ the actual chunk. Streaming is terminated by sending a zero-length chunk. Note: do not exceed StreamMaxLength as defined in clamd.conf, otherwise clamd will reply with INSTREAM size limit exceeded and close the connection */ -func (c *Clamd) ScanStream(r io.Reader, abort chan struct{}) (chan *ScanResult, error) { +func (c *Clamd) ScanStream(r io.Reader, abort <-chan struct{}) (chan *ScanResult, error) { conn, err := c.newConnection() if err != nil { return nil, err