-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix null values in static analysis parsing results (#863)
* move 'created' field to top level in static analysis schema Signed-off-by: Max Fisher <maxfisher@google.com> * remove trailing comma Signed-off-by: Max Fisher <maxfisher@google.com> * fix null values in results where parsing failed, remove unnecessary pointer indirection Signed-off-by: Max Fisher <maxfisher@google.com> * move basic data analysis to separate package, don't fail completely if 'file' command doesn't work (#865) Signed-off-by: Max Fisher <maxfisher@google.com> --------- Signed-off-by: Max Fisher <maxfisher@google.com>
- Loading branch information
1 parent
74979cf
commit e88f265
Showing
9 changed files
with
251 additions
and
212 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 was deleted.
Oops, something went wrong.
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,116 @@ | ||
package basicdata | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/ossf/package-analysis/internal/log" | ||
"github.com/ossf/package-analysis/internal/staticanalysis/linelengths" | ||
"github.com/ossf/package-analysis/internal/utils" | ||
"github.com/ossf/package-analysis/internal/utils/valuecounts" | ||
) | ||
|
||
// PackageData records basic information about files in a package, | ||
// mapping file path within the archive to FileData about that file. | ||
type PackageData struct { | ||
Files []FileData `json:"files"` | ||
} | ||
|
||
// FileData records various information about a file that can be determined | ||
// without parsing it using a programming language parser. | ||
type FileData struct { | ||
// Filename records the path to the file within the package archive | ||
Filename string `json:"filename"` | ||
|
||
// Description records the output of the `file` command run on that file. | ||
Description string `json:"description"` | ||
|
||
// Size records the size of the file (as reported by the filesystem). | ||
Size int64 `json:"size"` | ||
|
||
// SHA256 records the SHA256 hashsum of the file. | ||
SHA256 string `json:"sha256"` | ||
|
||
// LineLengths records the counts of line lengths in the file, | ||
// where a line is defined as all characters up to a newline. | ||
LineLengths valuecounts.ValueCounts `json:"line_lengths"` | ||
} | ||
|
||
func (bd FileData) String() string { | ||
parts := []string{ | ||
fmt.Sprintf("filename: %v\n", bd.Filename), | ||
fmt.Sprintf("description: %v\n", bd.Description), | ||
fmt.Sprintf("size: %v\n", bd.Size), | ||
fmt.Sprintf("sha256: %v\n", bd.SHA256), | ||
fmt.Sprintf("line lengths: %v\n", bd.LineLengths), | ||
} | ||
return strings.Join(parts, "\n") | ||
} | ||
|
||
/* | ||
Analyze collects basic file information for the specified files. Errors are logged | ||
rather than returned where possible, to maximise the amount of data collected. | ||
pathInArchive should return the relative path in the package archive, given an absolute | ||
path to a file in the package. The relative path is used for the result data. | ||
*/ | ||
func Analyze(paths []string, pathInArchive func(absolutePath string) string) (*PackageData, error) { | ||
if len(paths) == 0 { | ||
return &PackageData{Files: []FileData{}}, nil | ||
} | ||
|
||
descriptions, err := describeFiles(paths) | ||
haveDescriptions := true | ||
if err != nil { | ||
log.Error("failed to get file descriptions", "error", err) | ||
haveDescriptions = false | ||
} | ||
if len(descriptions) != len(paths) { | ||
log.Error(fmt.Sprintf("describeFiles() returned %d results, expecting %d", len(descriptions), len(paths))) | ||
haveDescriptions = false | ||
} | ||
|
||
result := PackageData{ | ||
Files: []FileData{}, | ||
} | ||
|
||
for index, filePath := range paths { | ||
archivePath := pathInArchive(filePath) | ||
description := "" | ||
if haveDescriptions { | ||
description = descriptions[index] | ||
} | ||
|
||
var fileSize int64 | ||
if fileInfo, err := os.Stat(filePath); err != nil { | ||
fileSize = -1 // error value | ||
log.Error("Error during stat file", "path", archivePath, "error", err) | ||
} else { | ||
fileSize = fileInfo.Size() | ||
} | ||
|
||
var sha265Sum string | ||
if hash, err := utils.SHA256Hash(filePath); err != nil { | ||
log.Error("Error hashing file", "path", archivePath, "error", err) | ||
} else { | ||
sha265Sum = hash | ||
} | ||
|
||
var lineLengths valuecounts.ValueCounts | ||
if ll, err := linelengths.GetLineLengths(filePath, ""); err != nil { | ||
log.Error("Error counting line lengths", "path", archivePath, "error", err) | ||
} else { | ||
lineLengths = valuecounts.Count(ll) | ||
} | ||
|
||
result.Files = append(result.Files, FileData{ | ||
Filename: archivePath, | ||
Description: description, | ||
Size: fileSize, | ||
SHA256: sha265Sum, | ||
LineLengths: lineLengths, | ||
}) | ||
} | ||
|
||
return &result, nil | ||
} |
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
Oops, something went wrong.