1
1
/*
2
- Copyright 2018 Google, Inc. All rights reserved.
2
+ Copyright 2020 Google, Inc. All rights reserved.
3
3
4
4
Licensed under the Apache License, Version 2.0 (the "License");
5
5
you may not use this file except in compliance with the License.
@@ -31,8 +31,7 @@ import (
31
31
//Emerge package database location
32
32
const emergePkgFile string = "/var/db/pkg"
33
33
34
- type EmergeAnalyzer struct {
35
- }
34
+ type EmergeAnalyzer struct {}
36
35
37
36
func (em EmergeAnalyzer ) Name () string {
38
37
return "EmergeAnalyzer"
@@ -51,13 +50,12 @@ func (em EmergeAnalyzer) Analyze(image pkgutil.Image) (util.Result, error) {
51
50
52
51
func (em EmergeAnalyzer ) getPackages (image pkgutil.Image ) (map [string ]util.PackageInfo , error ) {
53
52
var path string
54
- path = image .FSPath
55
- switch path {
56
- case "" :
53
+ if image .FSPath == "" {
57
54
path = emergePkgFile
58
- default :
59
- path = filepath .Join (path , emergePkgFile )
55
+ } else {
56
+ path = filepath .Join (image . FSPath , emergePkgFile )
60
57
}
58
+
61
59
packages := make (map [string ]util.PackageInfo )
62
60
if _ , err := os .Stat (path ); err != nil {
63
61
// invalid image directory path
@@ -71,17 +69,19 @@ func (em EmergeAnalyzer) getPackages(image pkgutil.Image) (map[string]util.Packa
71
69
return packages , err
72
70
}
73
71
74
- for i := 0 ; i < len (contents ); i ++ {
75
- c := contents [i ]
72
+ // for i := 0; i < len(contents); i++ {
73
+ for _ , c := range contents {
74
+ // c := contents[i]
76
75
pkgPrefix := c .Name ()
77
76
pkgContents , err := ioutil .ReadDir (filepath .Join (path , pkgPrefix ))
78
77
if err != nil {
79
78
return packages , err
80
79
}
81
- for j := 0 ; j < len (pkgContents ); j ++ {
82
- c := pkgContents [j ]
80
+ // for j := 0; j < len(pkgContents); j++ {
81
+ for _ , c := range pkgContents {
82
+ // c := pkgContents[j]
83
83
pkgRawName := c .Name ()
84
- // in usual, name of package installed by emerge is formatted as '{pkgName}-{version}' e.g.(pymongo-3.9.0)
84
+ // usually, the name of a package installed by emerge is formatted as '{pkgName}-{version}' e.g.(pymongo-3.9.0)
85
85
s := strings .Split (pkgRawName , "-" )
86
86
if len (s ) != 2 {
87
87
continue
@@ -104,20 +104,22 @@ func (em EmergeAnalyzer) getPackages(image pkgutil.Image) (map[string]util.Packa
104
104
// emerge will count the total size of a package and store it as a SIZE file in pkg metadata directory
105
105
// getPkgSize read this SIZE file of a given package
106
106
func getPkgSize (pkgPath string ) (int64 , error ) {
107
- var sizeFile * os.File
108
- var err error
109
- sizeFile , err = os .Open (pkgPath )
107
+ sizeFile , err := os .Open (pkgPath )
110
108
if err != nil {
111
- logrus .Debugf ("unable to open SIZE file for pkg %s" , pkgPath )
109
+ logrus .Warnf ("unable to open SIZE file for pkg %s" , pkgPath )
112
110
return 0 , err
113
111
}
114
112
defer sizeFile .Close ()
115
113
fileBody , err := ioutil .ReadAll (sizeFile )
116
114
if err != nil {
117
- logrus .Debugf ("unable to read SIZE file for pkg %s" , pkgPath )
115
+ logrus .Warnf ("unable to read SIZE file for pkg %s" , pkgPath )
118
116
return 0 , err
119
117
}
120
118
strFileBody := strings .Replace (string (fileBody ), "\n " , "" , - 1 )
121
- size , _ := strconv .ParseInt (strFileBody , 10 , 64 )
119
+ size , err := strconv .ParseInt (strFileBody , 10 , 64 )
120
+ if err != nil {
121
+ logrus .Warnf ("unable to compute size for pkg %s" , pkgPath )
122
+ return 0 , err
123
+ }
122
124
return size , nil
123
125
}
0 commit comments