-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
luotianqi
committed
Apr 2, 2022
1 parent
f3ebcb8
commit 5df0afb
Showing
8 changed files
with
174 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package rust | ||
|
||
import ( | ||
"opensca/internal/enum/language" | ||
"opensca/internal/filter" | ||
"opensca/internal/srt" | ||
) | ||
|
||
type Analyzer struct{} | ||
|
||
func New() Analyzer { | ||
return Analyzer{} | ||
} | ||
|
||
/** | ||
* @description: Get language of Analyzer | ||
* @return {language.Type} language type | ||
*/ | ||
func (a Analyzer) GetLanguage() language.Type { | ||
return language.Rust | ||
} | ||
|
||
/** | ||
* @description: Check if it is a parsable file | ||
* @param {string} filename file name | ||
* @return {bool} is a parseable file returns true | ||
*/ | ||
func (a Analyzer) CheckFile(filename string) bool { | ||
return filter.RustCargoLock(filename) | ||
} | ||
|
||
/** | ||
* @description: filters the files that the current parser needs to parse | ||
* @param {*srt.DirTree} dirRoot directory tree node | ||
* @param {*srt.DepTree} depRoot Dependency tree node | ||
* @return {[]*srt.FileData} List of files to parse | ||
*/ | ||
func (a Analyzer) FilterFile(dirRoot *srt.DirTree, depRoot *srt.DepTree) []*srt.FileData { | ||
files := []*srt.FileData{} | ||
for _, f := range dirRoot.Files { | ||
if a.CheckFile(f.Name) { | ||
files = append(files, f) | ||
} | ||
} | ||
return files | ||
} | ||
|
||
/** | ||
* @description: Parse the file | ||
* @param {*srt.DirTree} dirRoot directory tree node | ||
* @param {*srt.DepTree} depRoot Dependency tree node | ||
* @param {*srt.FileData} file data to parse | ||
* @return {[]*srt.DepTree} parsed dependency list | ||
*/ | ||
func (a Analyzer) ParseFile(dirRoot *srt.DirTree, depRoot *srt.DepTree, file *srt.FileData) []*srt.DepTree { | ||
if filter.RustCargoLock(file.Name) { | ||
return parseCargoLock(dirRoot, depRoot, file) | ||
} | ||
return []*srt.DepTree{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package rust | ||
|
||
import ( | ||
"opensca/internal/logs" | ||
"opensca/internal/srt" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/BurntSushi/toml" | ||
) | ||
|
||
type cargoPkg struct { | ||
Name string `toml:"name"` | ||
Version string `toml:"version"` | ||
DepStr []string `toml:"dependencies"` | ||
Dependencies []struct { | ||
Name string | ||
Version string | ||
} `toml:"-"` | ||
} | ||
|
||
func parseCargoLock(dirRoot *srt.DirTree, depRoot *srt.DepTree, file *srt.FileData) []*srt.DepTree { | ||
cargo := struct { | ||
Pkgs []*cargoPkg `toml:"package"` | ||
}{} | ||
cdepMap := map[string]*cargoPkg{} | ||
depMap := map[string]*srt.DepTree{} | ||
directMap := map[string]*srt.DepTree{} | ||
if err := toml.Unmarshal(file.Data, &cargo); err != nil { | ||
logs.Warn(err) | ||
} | ||
for _, pkg := range cargo.Pkgs { | ||
dep := srt.NewDepTree(nil) | ||
dep.Name = pkg.Name | ||
dep.Version = srt.NewVersion(pkg.Version) | ||
pkg.Dependencies = make([]struct { | ||
Name string | ||
Version string | ||
}, len(pkg.DepStr)) | ||
for i, str := range pkg.DepStr { | ||
name, version := str, "" | ||
index := strings.Index(str, " ") | ||
if index > -1 { | ||
name, version = str[:index], str[index+1:] | ||
} | ||
pkg.Dependencies[i] = struct { | ||
Name string | ||
Version string | ||
}{Name: name, Version: version} | ||
} | ||
depMap[dep.Name] = dep | ||
directMap[dep.Name] = dep | ||
cdepMap[dep.Name] = pkg | ||
} | ||
// 找出未被依赖的作为直接依赖 | ||
for _, pkg := range cargo.Pkgs { | ||
for _, d := range pkg.Dependencies { | ||
delete(directMap, d.Name) | ||
} | ||
} | ||
directDeps := []*srt.DepTree{} | ||
for _, v := range directMap { | ||
directDeps = append(directDeps, v) | ||
} | ||
sort.Slice(directDeps, func(i, j int) bool { | ||
return directDeps[i].Name < directDeps[j].Name | ||
}) | ||
for _, d := range directDeps { | ||
d.Parent = depRoot | ||
depRoot.Children = append(depRoot.Children, d) | ||
} | ||
// 从顶层开始构建 | ||
q := make([]*srt.DepTree, len(directDeps)) | ||
copy(q, directDeps) | ||
exist := map[string]struct{}{} | ||
for len(q) > 0 { | ||
n := q[0] | ||
exist[n.Name] = struct{}{} | ||
if cdep, ok := cdepMap[n.Name]; ok { | ||
for _, d := range cdep.Dependencies { | ||
if _, ok := exist[d.Name]; !ok { | ||
exist[d.Name] = struct{}{} | ||
if sub, ok := depMap[d.Name]; ok { | ||
sub.Parent = n | ||
n.Children = append(n.Children, sub) | ||
} | ||
} | ||
} | ||
} | ||
q = append(q[1:], n.Children...) | ||
} | ||
return directDeps | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters