Skip to content
This repository was archived by the owner on Mar 27, 2024. It is now read-only.

Adding size info to file diff/analysis #30

Merged
merged 4 commits into from
Aug 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 3 additions & 14 deletions differs/fileDiff.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package differs

import (
"sort"

"github.com/GoogleCloudPlatform/container-diff/utils"
)

Expand All @@ -21,7 +19,7 @@ func (a FileAnalyzer) Diff(image1, image2 utils.Image) (utils.DiffResult, error)
}

func (a FileAnalyzer) Analyze(image utils.Image) (utils.AnalyzeResult, error) {
var result utils.ListAnalyzeResult
var result utils.FileAnalyzeResult

imgDir, err := utils.GetDirectory(image.FSPath, true)
if err != nil {
Expand All @@ -30,7 +28,7 @@ func (a FileAnalyzer) Analyze(image utils.Image) (utils.AnalyzeResult, error) {

result.Image = image.Source
result.AnalyzeType = "File"
result.Analysis = imgDir.Content
result.Analysis = utils.GetDirectoryEntries(imgDir)
return &result, err
}

Expand All @@ -49,15 +47,6 @@ func diffImageFiles(image1, image2 utils.Image) (utils.DirDiff, error) {
return diff, err
}

adds := utils.GetAddedEntries(img1Dir, img2Dir)
sort.Strings(adds)
dels := utils.GetDeletedEntries(img1Dir, img2Dir)
sort.Strings(dels)

diff = utils.DirDiff{
Adds: adds,
Dels: dels,
Mods: []string{},
}
diff, _ = utils.DiffDirectory(img1Dir, img2Dir)
return diff, nil
}
6 changes: 1 addition & 5 deletions differs/nodeDiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,7 @@ func (a NodeAnalyzer) getPackages(image utils.Image) (map[string]map[string]util
var currInfo utils.PackageInfo
currInfo.Version = packageJSON.Version
packagePath := strings.TrimSuffix(currPackage, "package.json")
size, err := utils.GetDirectorySize(packagePath)
if err != nil {
glog.Warningf("Error getting package size at %s: %s\n", currPackage, err)
return packages, err
}
size := utils.GetSize(packagePath)
currInfo.Size = strconv.FormatInt(size, 10)
mapPath := strings.Replace(packagePath, path, "", 1)
// Check if other package version already recorded
Expand Down
9 changes: 2 additions & 7 deletions differs/pipDiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,8 @@ func (a PipAnalyzer) getPackages(image utils.Image) (map[string]map[string]utils
var size string
if i-1 >= 0 && contents[i-1].Name() == packageName {
packagePath := filepath.Join(pythonPath, packageName)
intSize, err := utils.GetDirectorySize(packagePath)
if err != nil {
glog.Errorf("Could not obtain size for package %s", packagePath)
size = ""
} else {
size = strconv.FormatInt(intSize, 10)
}
intSize := utils.GetSize(packagePath)
size = strconv.FormatInt(intSize, 10)
} else if i+1 < len(contents) && contents[i+1].Name() == packageName+".py" {
size = strconv.FormatInt(contents[i+1].Size(), 10)

Expand Down
22 changes: 18 additions & 4 deletions tests/file_diff_expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,25 @@
"Image2": "gcr.io/gcp-runtimes/diff-modified",
"DiffType": "File",
"Diff": {
"Adds": [],
"Adds": null,
"Dels": [
"/home/test"
{
"Name": "/home/test",
"Size": 0
}
],
"Mods": []
"Mods": [
{
"Name": "/bin/[",
"Size1": 1026712,
"Size2": 1026712
},
{
"Name": "/bin/getconf",
"Size1": 78584,
"Size2": 78584
}
]
}
}
]
]
14 changes: 14 additions & 0 deletions utils/analyze_output_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,17 @@ func (r SingleVersionPackageAnalyzeResult) GetStruct() AnalyzeResult {
func (r SingleVersionPackageAnalyzeResult) OutputText(diffType string) error {
return TemplateOutput(r)
}

type FileAnalyzeResult struct {
Image string
AnalyzeType string
Analysis []DirectoryEntry
}

func (r FileAnalyzeResult) GetStruct() AnalyzeResult {
return r
}

func (r FileAnalyzeResult) OutputText(analyzeType string) error {
return TemplateOutput(r)
}
1 change: 1 addition & 0 deletions utils/format_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var templates = map[string]string{
"utils.HistDiffResult": HistoryDiffOutput,
"utils.DirDiffResult": FSDiffOutput,
"utils.ListAnalyzeResult": ListAnalysisOutput,
"utils.FileAnalyzeResult": FileAnalysisOutput,
"utils.MultiVersionPackageAnalyzeResult": MultiVersionPackageOutput,
"utils.SingleVersionPackageAnalyzeResult": SingleVersionPackageOutput,
}
Expand Down
88 changes: 83 additions & 5 deletions utils/fs_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,46 @@ import (
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"

"github.com/golang/glog"
)

func GetDirectorySize(path string) (int64, error) {
// Directory stores a representaiton of a file directory.
type Directory struct {
Root string
Content []string
}

type DirectoryEntry struct {
Name string
Size int64
}

type EntryDiff struct {
Name string
Size1 int64
Size2 int64
}

func GetSize(path string) int64 {
stat, err := os.Stat(path)
if err != nil {
glog.Errorf("Could not obtain size for %s: %s", path, err)
return -1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems to me that if we hit an error here, we have another problem and probably want to throw some kind of error, rather than just silently returning -1 (which will just get outputted silently). WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The next thing I was working on was modifying the human-readable output, to check for any sizes that = -1 and outputting "unknown" or "error" or something under the size column instead of -1. The reason I didn't want to error out when the size of a particular file could not be found was to allow the rest of the analysis to be performed for the non-problematic files regardless of potential size retrieval errors.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok that's fine. if we start seeing errors happen there we should probably investigate the root cause, but for now this should be good.

}
if stat.IsDir() {
size, err := getDirectorySize(path)
if err != nil {
glog.Errorf("Could not obtain directory size for %s: %s", path, err)
}
return size
}
return stat.Size()
}

func getDirectorySize(path string) (int64, error) {
var size int64
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
if !info.IsDir() {
Expand Down Expand Up @@ -107,16 +141,60 @@ func GetDeletedEntries(d1, d2 Directory) []string {
}

type DirDiff struct {
Adds []string
Dels []string
Mods []string
Adds []DirectoryEntry
Dels []DirectoryEntry
Mods []EntryDiff
}

func GetDirectoryEntries(d Directory) []DirectoryEntry {
return createDirectoryEntries(d.Root, d.Content)
}

func createDirectoryEntries(root string, entryNames []string) (entries []DirectoryEntry) {
for _, name := range entryNames {
entryPath := filepath.Join(root, name)
size := GetSize(entryPath)

entry := DirectoryEntry{
Name: name,
Size: size,
}
entries = append(entries, entry)
}
return entries
}

func createEntryDiffs(root1, root2 string, entryNames []string) (entries []EntryDiff) {
for _, name := range entryNames {
entryPath1 := filepath.Join(root1, name)
size1 := GetSize(entryPath1)

entryPath2 := filepath.Join(root2, name)
size2 := GetSize(entryPath2)

entry := EntryDiff{
Name: name,
Size1: size1,
Size2: size2,
}
entries = append(entries, entry)
}
return entries
}

// DiffDirectory takes the diff of two directories, assuming both are completely unpacked
func DiffDirectory(d1, d2 Directory) (DirDiff, bool) {
adds := GetAddedEntries(d1, d2)
sort.Strings(adds)
addedEntries := createDirectoryEntries(d2.Root, adds)

dels := GetDeletedEntries(d1, d2)
sort.Strings(dels)
deletedEntries := createDirectoryEntries(d1.Root, dels)

mods := GetModifiedEntries(d1, d2)
sort.Strings(mods)
modifiedEntries := createEntryDiffs(d1.Root, d2.Root, mods)

var same bool
if len(adds) == 0 && len(dels) == 0 && len(mods) == 0 {
Expand All @@ -125,7 +203,7 @@ func DiffDirectory(d1, d2 Directory) (DirDiff, bool) {
same = false
}

return DirDiff{adds, dels, mods}, same
return DirDiff{addedEntries, deletedEntries, modifiedEntries}, same
}

func checkSameFile(f1name, f2name string) (bool, error) {
Expand Down
6 changes: 0 additions & 6 deletions utils/tar_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ import (
"github.com/golang/glog"
)

// Directory stores a representaiton of a file directory.
type Directory struct {
Root string
Content []string
}

func unpackTar(tr *tar.Reader, path string) error {
for {
header, err := tr.Next()
Expand Down
20 changes: 14 additions & 6 deletions utils/template_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ const FSDiffOutput = `
-----{{.DiffType}}-----

These entries have been added to {{.Image1}}:{{if not .Diff.Adds}} None{{else}}
{{range .Diff.Adds}}{{print .}}
{{end}}{{end}}
FILE SIZE{{range .Diff.Adds}}{{"\n"}}{{.Name}} {{.Size}}{{end}}
{{end}}

These entries have been deleted from {{.Image1}}:{{if not .Diff.Dels}} None{{else}}
{{range .Diff.Dels}}{{print .}}
{{end}}{{end}}
FILE SIZE{{range .Diff.Dels}}{{"\n"}}{{.Name}} {{.Size}}{{end}}
{{end}}

These entries have been changed between {{.Image1}} and {{.Image2}}:{{if not .Diff.Mods}} None{{else}}
{{range .Diff.Mods}}{{print .}}
{{end}}{{end}}
FILE SIZE1 SIZE2{{range .Diff.Mods}}{{"\n"}}{{.Name}} {{.Size1}} {{.Size2}}{{end}}
{{end}}
`

const SingleVersionDiffOutput = `
Expand Down Expand Up @@ -56,6 +56,14 @@ const ListAnalysisOutput = `
Analysis for {{.Image}}:{{if not .Analysis}} None{{else}}{{block "list" .Analysis}}{{"\n"}}{{range .}}{{print "-" .}}{{"\n"}}{{end}}{{end}}{{end}}
`

const FileAnalysisOutput = `
-----{{.AnalyzeType}}-----

Analysis for {{.Image}}:{{if not .Analysis}} None{{else}}
FILE SIZE{{range .Analysis}}{{"\n"}}{{.Name}} {{.Size}}{{end}}
{{end}}
`

const MultiVersionPackageOutput = `
-----{{.AnalyzeType}}-----

Expand Down