4
4
"context"
5
5
"crypto/sha256"
6
6
"encoding/json"
7
- "io/ioutil"
8
7
"os"
9
8
"path/filepath"
10
9
"strings"
@@ -29,9 +28,9 @@ const (
29
28
)
30
29
31
30
type Artifact struct {
32
- dir string
31
+ rootPath string
33
32
cache cache.ArtifactCache
34
- walker walker.Dir
33
+ walker walker.FS
35
34
analyzer analyzer.AnalyzerGroup
36
35
hookManager hook.Manager
37
36
scanner scanner.Scanner
@@ -40,21 +39,21 @@ type Artifact struct {
40
39
configScannerOption config.ScannerOption
41
40
}
42
41
43
- func NewArtifact (dir string , c cache.ArtifactCache , artifactOpt artifact.Option , scannerOpt config.ScannerOption ) (artifact.Artifact , error ) {
42
+ func NewArtifact (rootPath string , c cache.ArtifactCache , artifactOpt artifact.Option , scannerOpt config.ScannerOption ) (artifact.Artifact , error ) {
44
43
// Register config analyzers
45
44
if err := config .RegisterConfigAnalyzers (scannerOpt .FilePatterns ); err != nil {
46
45
return nil , xerrors .Errorf ("config analyzer error: %w" , err )
47
46
}
48
47
49
- s , err := scanner .New (dir , scannerOpt .Namespaces , scannerOpt .PolicyPaths , scannerOpt .DataPaths , scannerOpt .Trace )
48
+ s , err := scanner .New (rootPath , scannerOpt .Namespaces , scannerOpt .PolicyPaths , scannerOpt .DataPaths , scannerOpt .Trace )
50
49
if err != nil {
51
50
return nil , xerrors .Errorf ("scanner error: %w" , err )
52
51
}
53
52
54
53
return Artifact {
55
- dir : dir ,
54
+ rootPath : filepath . Clean ( rootPath ) ,
56
55
cache : c ,
57
- walker : walker .NewDir (buildAbsPaths (dir , artifactOpt .SkipFiles ), buildAbsPaths (dir , artifactOpt .SkipDirs )),
56
+ walker : walker .NewFS (buildAbsPaths (rootPath , artifactOpt .SkipFiles ), buildAbsPaths (rootPath , artifactOpt .SkipDirs )),
58
57
analyzer : analyzer .NewAnalyzerGroup (artifactOpt .AnalyzerGroup , artifactOpt .DisabledAnalyzers ),
59
58
hookManager : hook .NewManager (artifactOpt .DisabledHooks ),
60
59
scanner : s ,
@@ -81,21 +80,29 @@ func (a Artifact) Inspect(ctx context.Context) (types.ArtifactReference, error)
81
80
result := new (analyzer.AnalysisResult )
82
81
limit := semaphore .NewWeighted (parallel )
83
82
84
- err := a .walker .Walk (a .dir , func (filePath string , info os.FileInfo , opener analyzer.Opener ) error {
83
+ err := a .walker .Walk (a .rootPath , func (filePath string , info os.FileInfo , opener analyzer.Opener ) error {
84
+ directory := a .rootPath
85
+
86
+ // When the directory is the same as the filePath, a file was given
87
+ // instead of a directory, rewrite the directory in this case.
88
+ if a .rootPath == filePath {
89
+ directory = filepath .Dir (a .rootPath )
90
+ }
91
+
85
92
// For exported rootfs (e.g. images/alpine/etc/alpine-release)
86
- filePath , err := filepath .Rel (a . dir , filePath )
93
+ filePath , err := filepath .Rel (directory , filePath )
87
94
if err != nil {
88
95
return xerrors .Errorf ("filepath rel (%s): %w" , filePath , err )
89
96
}
90
97
91
98
opts := analyzer.AnalysisOptions {Offline : a .artifactOption .Offline }
92
- if err = a .analyzer .AnalyzeFile (ctx , & wg , limit , result , a . dir , filePath , info , opener , opts ); err != nil {
99
+ if err = a .analyzer .AnalyzeFile (ctx , & wg , limit , result , directory , filePath , info , opener , opts ); err != nil {
93
100
return xerrors .Errorf ("analyze file (%s): %w" , filePath , err )
94
101
}
95
102
return nil
96
103
})
97
104
if err != nil {
98
- return types.ArtifactReference {}, xerrors .Errorf ("walk dir : %w" , err )
105
+ return types.ArtifactReference {}, xerrors .Errorf ("walk filesystem : %w" , err )
99
106
}
100
107
101
108
// Wait for all the goroutine to finish.
@@ -144,11 +151,11 @@ func (a Artifact) Inspect(ctx context.Context) (types.ArtifactReference, error)
144
151
145
152
// get hostname
146
153
var hostName string
147
- b , err := ioutil .ReadFile (filepath .Join (a .dir , "etc" , "hostname" ))
154
+ b , err := os .ReadFile (filepath .Join (a .rootPath , "etc" , "hostname" ))
148
155
if err == nil && string (b ) != "" {
149
156
hostName = strings .TrimSpace (string (b ))
150
157
} else {
151
- hostName = a .dir
158
+ hostName = a .rootPath
152
159
}
153
160
154
161
return types.ArtifactReference {
0 commit comments