Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

General fixups #8

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 18 additions & 28 deletions clamd.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const (
RES_FOUND = "FOUND"
RES_ERROR = "ERROR"
RES_PARSE_ERROR = "PARSE ERROR"
CHUNK_SIZE = 1024
)

type Clamd struct {
Expand Down Expand Up @@ -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).
*/
Expand All @@ -122,8 +128,6 @@ func (c *Clamd) Ping() error {
return errors.New(fmt.Sprintf("Invalid response, got %s.", s))
}
}

return nil
}

/*
Expand Down Expand Up @@ -185,67 +189,54 @@ 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
}

/*
Scan file or directory (recursively) with archive support enabled (a full path is
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)
}

/*
Scan file or directory (recursively) with archive and special file support disabled
(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)
}

/*
Scan file in a standard way or scan directory (recursively) using multiple threads
(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)
}

/*
Scan file or directory (recursively) with archive support enabled and don’t stop
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)
}

/*
Scan file or directory (recursively) with archive support enabled and don’t stop
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)
}

/*
Expand Down Expand Up @@ -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}
}
5 changes: 2 additions & 3 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
"time"
)

const CHUNK_SIZE = 1024
const TCP_TIMEOUT = time.Second * 2

var resultRegex = regexp.MustCompile(
Expand Down Expand Up @@ -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() {
Expand Down