diff --git a/clamd.go b/clamd.go index 5199f63..fa5a9e3 100644 --- a/clamd.go +++ b/clamd.go @@ -38,6 +38,7 @@ const ( RES_FOUND = "FOUND" RES_ERROR = "ERROR" RES_PARSE_ERROR = "PARSE ERROR" + CHUNK_SIZE = 1024 ) type Clamd struct { @@ -104,6 +105,11 @@ func (c *Clamd) simpleCommand(command string) (chan *ScanResult, error) { return ch, err } +func (c *Clamd) simpleCommandWithPath(commandName string, path string) (chan *ScanResult, error) { + command := fmt.Sprintf("%s %s", commandName, path) + return c.simpleCommand(command) +} + /* Check the daemon's state (should reply with PONG). */ @@ -122,8 +128,6 @@ func (c *Clamd) Ping() error { return errors.New(fmt.Sprintf("Invalid response, got %s.", s)) } } - - return nil } /* @@ -185,17 +189,14 @@ func (c *Clamd) Reload() error { return errors.New(fmt.Sprintf("Invalid response, got %s.", s)) } } - - return nil } -func (c *Clamd) Shutdown() error { - _, err := c.simpleCommand("SHUTDOWN") - if err != nil { - return err - } - - return err +/* +Sends the SHUTDOWN command to the ClamdAV instance +*/ +func (c *Clamd) Shutdown() (err error) { + _, err = c.simpleCommand("SHUTDOWN") + return } /* @@ -203,9 +204,7 @@ Scan file or directory (recursively) with archive support enabled (a full path i required). */ func (c *Clamd) ScanFile(path string) (chan *ScanResult, error) { - command := fmt.Sprintf("SCAN %s", path) - ch, err := c.simpleCommand(command) - return ch, err + return c.simpleCommandWithPath("Scan", path) } /* @@ -213,9 +212,7 @@ Scan file or directory (recursively) with archive and special file support disab (a full path is required). */ func (c *Clamd) RawScanFile(path string) (chan *ScanResult, error) { - command := fmt.Sprintf("RAWSCAN %s", path) - ch, err := c.simpleCommand(command) - return ch, err + return c.simpleCommandWithPath("RAWSCAN", path) } /* @@ -223,9 +220,7 @@ Scan file in a standard way or scan directory (recursively) using multiple threa (to make the scanning faster on SMP machines). */ func (c *Clamd) MultiScanFile(path string) (chan *ScanResult, error) { - command := fmt.Sprintf("MULTISCAN %s", path) - ch, err := c.simpleCommand(command) - return ch, err + return c.simpleCommandWithPath("MULTISCAN", path) } /* @@ -233,9 +228,7 @@ Scan file or directory (recursively) with archive support enabled and don’t st the scanning when a virus is found. */ func (c *Clamd) ContScanFile(path string) (chan *ScanResult, error) { - command := fmt.Sprintf("CONTSCAN %s", path) - ch, err := c.simpleCommand(command) - return ch, err + return c.simpleCommandWithPath("CONTSCAN", path) } /* @@ -243,9 +236,7 @@ Scan file or directory (recursively) with archive support enabled and don’t st the scanning when a virus is found. */ func (c *Clamd) AllMatchScanFile(path string) (chan *ScanResult, error) { - command := fmt.Sprintf("ALLMATCHSCAN %s", path) - ch, err := c.simpleCommand(command) - return ch, err + return c.simpleCommandWithPath("ALLMATCHSCAN", path) } /* @@ -306,6 +297,5 @@ func (c *Clamd) ScanStream(r io.Reader, abort chan bool) (chan *ScanResult, erro } func NewClamd(address string) *Clamd { - clamd := &Clamd{address: address} - return clamd + return &Clamd{address: address} } diff --git a/conn.go b/conn.go index 5c9f7f9..1646e68 100644 --- a/conn.go +++ b/conn.go @@ -37,7 +37,6 @@ import ( "time" ) -const CHUNK_SIZE = 1024 const TCP_TIMEOUT = time.Second * 2 var resultRegex = regexp.MustCompile( @@ -81,11 +80,11 @@ func (conn *CLAMDConn) sendChunk(data []byte) error { return err } -func (c *CLAMDConn) readResponse() (chan *ScanResult, *sync.WaitGroup, error) { +func (conn *CLAMDConn) readResponse() (chan *ScanResult, *sync.WaitGroup, error) { var wg sync.WaitGroup wg.Add(1) - reader := bufio.NewReader(c) + reader := bufio.NewReader(conn) ch := make(chan *ScanResult) go func() {