11/*
2- Copyright 2018 Google, Inc. All rights reserved.
2+ Copyright 2020 Google, Inc. All rights reserved.
33
44Licensed under the Apache License, Version 2.0 (the "License");
55you may not use this file except in compliance with the License.
@@ -31,8 +31,7 @@ import (
3131//Emerge package database location
3232const emergePkgFile string = "/var/db/pkg"
3333
34- type EmergeAnalyzer struct {
35- }
34+ type EmergeAnalyzer struct {}
3635
3736func (em EmergeAnalyzer ) Name () string {
3837 return "EmergeAnalyzer"
@@ -51,13 +50,12 @@ func (em EmergeAnalyzer) Analyze(image pkgutil.Image) (util.Result, error) {
5150
5251func (em EmergeAnalyzer ) getPackages (image pkgutil.Image ) (map [string ]util.PackageInfo , error ) {
5352 var path string
54- path = image .FSPath
55- switch path {
56- case "" :
53+ if image .FSPath == "" {
5754 path = emergePkgFile
58- default :
59- path = filepath .Join (path , emergePkgFile )
55+ } else {
56+ path = filepath .Join (image . FSPath , emergePkgFile )
6057 }
58+
6159 packages := make (map [string ]util.PackageInfo )
6260 if _ , err := os .Stat (path ); err != nil {
6361 // invalid image directory path
@@ -71,17 +69,19 @@ func (em EmergeAnalyzer) getPackages(image pkgutil.Image) (map[string]util.Packa
7169 return packages , err
7270 }
7371
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]
7675 pkgPrefix := c .Name ()
7776 pkgContents , err := ioutil .ReadDir (filepath .Join (path , pkgPrefix ))
7877 if err != nil {
7978 return packages , err
8079 }
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]
8383 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)
8585 s := strings .Split (pkgRawName , "-" )
8686 if len (s ) != 2 {
8787 continue
@@ -104,20 +104,22 @@ func (em EmergeAnalyzer) getPackages(image pkgutil.Image) (map[string]util.Packa
104104// emerge will count the total size of a package and store it as a SIZE file in pkg metadata directory
105105// getPkgSize read this SIZE file of a given package
106106func 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 )
110108 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 )
112110 return 0 , err
113111 }
114112 defer sizeFile .Close ()
115113 fileBody , err := ioutil .ReadAll (sizeFile )
116114 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 )
118116 return 0 , err
119117 }
120118 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+ }
122124 return size , nil
123125}
0 commit comments