forked from ankane/pdscan
/
local_file_adapter.go
53 lines (42 loc) · 961 Bytes
/
local_file_adapter.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
package internal
import (
"os"
"path/filepath"
)
type LocalFileAdapter struct {
url string
}
func (a *LocalFileAdapter) ObjectName() string {
return "file"
}
func (a *LocalFileAdapter) Scan(scanOpts ScanOpts) ([]ruleMatch, error) {
return scanFiles(a, scanOpts)
}
func (a *LocalFileAdapter) Init(url string) error {
a.url = url
return nil
}
func (a LocalFileAdapter) FetchFiles() ([]string, error) {
urlStr := a.url
var files []string
root := urlStr[7:]
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err == nil && !info.IsDir() {
files = append(files, path)
}
return nil
})
if err != nil {
return files, err
}
return files, nil
}
// TODO read metadata for certain file types
func (a LocalFileAdapter) FindFileMatches(filename string, matchFinder *MatchFinder) error {
f, err := os.Open(filename)
if err != nil {
return err
}
defer f.Close()
return processFile(f, matchFinder)
}