-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
134 lines (114 loc) · 2.99 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"fmt"
"html/template"
"os"
"os/exec"
"sync"
"time"
"github.com/coreos/go-systemd/journal"
"github.com/fsnotify/fsnotify"
)
func main() {
paths := os.Args[1:]
if len(paths) == 0 {
fmt.Fprintln(os.Stderr, "expecting path(s) to scan")
os.Exit(2)
}
if err := run(paths); err != nil {
fmt.Fprintln(os.Stderr, "failed to start up: ", err)
os.Exit(1)
}
}
const (
// ScanDelay is how long we wait for file access to be quiescent before
// initiating a scan.
ScanDelay = 2 * time.Second
)
// timers map filenames to callbacks that actually perform a scan. The idea is
// to wait for 2s of quiescence before initiating the scan, in case the file is
// in the process of being modified.
var timers struct {
lock sync.Mutex
t map[string]*time.Timer
}
// run is the primary runtime function
func run(paths []string) error {
timers.t = make(map[string]*time.Timer)
var err error
if successTpl, err = template.New("success").Parse(successTplT); err != nil {
return err
}
if errorTpl, err = template.New("error").Parse(errorTplT); err != nil {
return err
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
for _, path := range paths {
if err = watcher.Add(path); err != nil {
return err
}
}
go func() {
if err := <-watcher.Errors; err != nil {
fmt.Fprintln(os.Stderr, "fsnotify error: ", err)
os.Exit(1)
}
}()
for ev := range watcher.Events {
localName := ev.Name // don't close over the loop var
timers.lock.Lock()
if tmr, already := timers.t[localName]; already {
// cancel existing timer
tmr.Stop()
}
// start new timer waiting for file access to be quiescent
timers.t[localName] = time.AfterFunc(ScanDelay, func() {
scan(localName)
timers.lock.Lock()
delete(timers.t, localName)
timers.lock.Unlock()
})
timers.lock.Unlock()
}
return nil
}
// scan a file and report the result via dialog.
func scan(name string) {
jvars := map[string]string{
"CLAMSCAN_FNAME": name,
}
// fsnotify will trigger on removed files, symlinks, directories etc.
// so filter out those cases
st, err := os.Lstat(name)
switch {
case os.IsNotExist(err):
journal.Send(fmt.Sprintf("not executing clamscan on %q "+
"as it was removed", name), journal.PriDebug, jvars)
return
case !st.Mode().IsRegular():
journal.Send(fmt.Sprintf("not executing clamscan on %q "+
"as it is mode %v", name, st.Mode()),
journal.PriDebug, jvars)
return
}
jvars["CLAMSCAN_FILESIZE"] = fmt.Sprintf("%d", st.Size())
journal.Send(fmt.Sprintf("executing clamscan on %q "+
"(%d bytes)", name, st.Size()),
journal.PriDebug, jvars)
clam := exec.Command("clamdscan", name)
clamOP, err := clam.CombinedOutput()
status := "success"
priority := journal.PriNotice
if err != nil {
status = err.Error()
priority = journal.PriAlert
}
jvars["CLAMSCAN_STATUS"] = status
jvars["CLAMSCAN_OUTPUT"] = string(clamOP)
journal.Send(fmt.Sprintf("clamscan executed on %q with status: %s",
name, status), priority, jvars)
dialog(clamOP, err)
}