Skip to content

Commit

Permalink
Refactor, add clamd client
Browse files Browse the repository at this point in the history
  • Loading branch information
filariow committed Aug 9, 2022
1 parent d87fcf1 commit d1d5e34
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 3 deletions.
4 changes: 2 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ func main() {
log.Fatal(err)
}

c := execute.NewShellExecutor(f, e.Visited(), 8)
c := execute.NewClamdExecutor(f, e.Visited(), 1)
c.Execute()

b := progressbar.NewOptions64(
int64(ce.VisitedNum()),
progressbar.OptionUseANSICodes(true),
progressbar.OptionSetDescription("Scanned files..."),
progressbar.OptionShowBytes(true),
progressbar.OptionShowCount(),
progressbar.OptionThrottle(50*time.Millisecond),
progressbar.OptionFullWidth(),
progressbar.OptionSetVisibility(true),
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ module github.com/filariow/polo

go 1.18

require github.com/schollz/progressbar/v3 v3.9.0
require (
github.com/baruwa-enterprise/clamd v1.0.1
github.com/schollz/progressbar/v3 v3.9.0
)

require (
github.com/mattn/go-runewidth v0.0.13 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/baruwa-enterprise/clamd v1.0.1 h1:/Urmb7+snmNSTpklsXNh7sbMWAmJjp97g6B6zX9QU8c=
github.com/baruwa-enterprise/clamd v1.0.1/go.mod h1:2fjN7vyA+9voVOtNf5dNGr8wsio6ezvuCPX8udrsDGA=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -13,6 +15,7 @@ github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/schollz/progressbar/v3 v3.9.0 h1:k9SRNQ8KZyibz1UZOaKxnkUE3iGtmGSDt1YY9KlCYQk=
github.com/schollz/progressbar/v3 v3.9.0/go.mod h1:W5IEwbJecncFGBvuEh4A7HT1nZZ6WNIL2i3qbnI0WKY=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand Down
98 changes: 98 additions & 0 deletions pkg/execute/clamd_executor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package execute

import (
"context"
"fmt"
"log"
"os"
"path"
"sync"
"sync/atomic"

"github.com/baruwa-enterprise/clamd"
)

type clamdExecutor struct {
folder string
source <-chan string

started uint64
terminated uint64
done chan struct{}
concurrency int

workers chan struct{}
found chan string
clamdClient *clamd.Client
}

func NewClamdExecutor(folder string, source <-chan string, t int) Executor {
e := &clamdExecutor{
folder: folder,
source: source,
started: 0,
terminated: 0,
done: make(chan struct{}),
concurrency: t,
workers: make(chan struct{}, t),
found: make(chan string),
}
return e
}

func (l *clamdExecutor) Execute() error {
c, err := clamd.NewClient("unix", "/var/run/clamav/clamd.ctl")
if err != nil {
return err
}
l.clamdClient = c

go func() {
var wg sync.WaitGroup

for e := range l.source {
l.workers <- struct{}{}
go func(h string) {
defer wg.Done()
wg.Add(1)

if err := l.execute(h); err != nil {
log.Println(err)
}
<-l.workers
}(e)
}

wg.Wait()
close(l.done)
}()
return nil
}

func (l *clamdExecutor) execute(e string) error {
atomic.AddUint64(&l.started, 1)
defer atomic.AddUint64(&l.terminated, 1)
a := path.Join(l.folder, e)

rr, err := l.clamdClient.Fildes(context.TODO(), a)
if err != nil {
return err
}

for _, r := range rr {
fmt.Fprintf(os.Stderr, "File: %v, Status: %v\n", a, r.Status)
}

return nil
}

func (l *clamdExecutor) Done() <-chan struct{} {
return l.done
}

func (l *clamdExecutor) Started() uint64 {
return l.started
}
func (l *clamdExecutor) Terminated() uint64 {
return l.terminated
}
File renamed without changes.
File renamed without changes.

0 comments on commit d1d5e34

Please sign in to comment.