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

Commit 1f73b93

Browse files
authored
Merge pull request #30 from cftorres/FileDiffSize
Adding size info to file diff/analysis
2 parents 19582af + 7c2bc0a commit 1f73b93

File tree

9 files changed

+136
-47
lines changed

9 files changed

+136
-47
lines changed

differs/fileDiff.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package differs
22

33
import (
4-
"sort"
5-
64
"github.com/GoogleCloudPlatform/container-diff/utils"
75
)
86

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

2321
func (a FileAnalyzer) Analyze(image utils.Image) (utils.AnalyzeResult, error) {
24-
var result utils.ListAnalyzeResult
22+
var result utils.FileAnalyzeResult
2523

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

3129
result.Image = image.Source
3230
result.AnalyzeType = "File"
33-
result.Analysis = imgDir.Content
31+
result.Analysis = utils.GetDirectoryEntries(imgDir)
3432
return &result, err
3533
}
3634

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

52-
adds := utils.GetAddedEntries(img1Dir, img2Dir)
53-
sort.Strings(adds)
54-
dels := utils.GetDeletedEntries(img1Dir, img2Dir)
55-
sort.Strings(dels)
56-
57-
diff = utils.DirDiff{
58-
Adds: adds,
59-
Dels: dels,
60-
Mods: []string{},
61-
}
50+
diff, _ = utils.DiffDirectory(img1Dir, img2Dir)
6251
return diff, nil
6352
}

differs/nodeDiff.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,7 @@ func (a NodeAnalyzer) getPackages(image utils.Image) (map[string]map[string]util
5555
var currInfo utils.PackageInfo
5656
currInfo.Version = packageJSON.Version
5757
packagePath := strings.TrimSuffix(currPackage, "package.json")
58-
size, err := utils.GetDirectorySize(packagePath)
59-
if err != nil {
60-
glog.Warningf("Error getting package size at %s: %s\n", currPackage, err)
61-
return packages, err
62-
}
58+
size := utils.GetSize(packagePath)
6359
currInfo.Size = strconv.FormatInt(size, 10)
6460
mapPath := strings.Replace(packagePath, path, "", 1)
6561
// Check if other package version already recorded

differs/pipDiff.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,8 @@ func (a PipAnalyzer) getPackages(image utils.Image) (map[string]map[string]utils
6969
var size string
7070
if i-1 >= 0 && contents[i-1].Name() == packageName {
7171
packagePath := filepath.Join(pythonPath, packageName)
72-
intSize, err := utils.GetDirectorySize(packagePath)
73-
if err != nil {
74-
glog.Errorf("Could not obtain size for package %s", packagePath)
75-
size = ""
76-
} else {
77-
size = strconv.FormatInt(intSize, 10)
78-
}
72+
intSize := utils.GetSize(packagePath)
73+
size = strconv.FormatInt(intSize, 10)
7974
} else if i+1 < len(contents) && contents[i+1].Name() == packageName+".py" {
8075
size = strconv.FormatInt(contents[i+1].Size(), 10)
8176

tests/file_diff_expected.json

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,25 @@
44
"Image2": "gcr.io/gcp-runtimes/diff-modified",
55
"DiffType": "File",
66
"Diff": {
7-
"Adds": [],
7+
"Adds": null,
88
"Dels": [
9-
"/home/test"
9+
{
10+
"Name": "/home/test",
11+
"Size": 0
12+
}
1013
],
11-
"Mods": []
14+
"Mods": [
15+
{
16+
"Name": "/bin/[",
17+
"Size1": 1026712,
18+
"Size2": 1026712
19+
},
20+
{
21+
"Name": "/bin/getconf",
22+
"Size1": 78584,
23+
"Size2": 78584
24+
}
25+
]
1226
}
1327
}
14-
]
28+
]

utils/analyze_output_utils.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,17 @@ func (r SingleVersionPackageAnalyzeResult) GetStruct() AnalyzeResult {
4646
func (r SingleVersionPackageAnalyzeResult) OutputText(diffType string) error {
4747
return TemplateOutput(r)
4848
}
49+
50+
type FileAnalyzeResult struct {
51+
Image string
52+
AnalyzeType string
53+
Analysis []DirectoryEntry
54+
}
55+
56+
func (r FileAnalyzeResult) GetStruct() AnalyzeResult {
57+
return r
58+
}
59+
60+
func (r FileAnalyzeResult) OutputText(analyzeType string) error {
61+
return TemplateOutput(r)
62+
}

utils/format_utils.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var templates = map[string]string{
1919
"utils.HistDiffResult": HistoryDiffOutput,
2020
"utils.DirDiffResult": FSDiffOutput,
2121
"utils.ListAnalyzeResult": ListAnalysisOutput,
22+
"utils.FileAnalyzeResult": FileAnalysisOutput,
2223
"utils.MultiVersionPackageAnalyzeResult": MultiVersionPackageOutput,
2324
"utils.SingleVersionPackageAnalyzeResult": SingleVersionPackageOutput,
2425
}

utils/fs_utils.go

Lines changed: 83 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,46 @@ import (
66
"io/ioutil"
77
"os"
88
"path/filepath"
9+
"sort"
910
"strings"
1011

1112
"github.com/golang/glog"
1213
)
1314

14-
func GetDirectorySize(path string) (int64, error) {
15+
// Directory stores a representaiton of a file directory.
16+
type Directory struct {
17+
Root string
18+
Content []string
19+
}
20+
21+
type DirectoryEntry struct {
22+
Name string
23+
Size int64
24+
}
25+
26+
type EntryDiff struct {
27+
Name string
28+
Size1 int64
29+
Size2 int64
30+
}
31+
32+
func GetSize(path string) int64 {
33+
stat, err := os.Stat(path)
34+
if err != nil {
35+
glog.Errorf("Could not obtain size for %s: %s", path, err)
36+
return -1
37+
}
38+
if stat.IsDir() {
39+
size, err := getDirectorySize(path)
40+
if err != nil {
41+
glog.Errorf("Could not obtain directory size for %s: %s", path, err)
42+
}
43+
return size
44+
}
45+
return stat.Size()
46+
}
47+
48+
func getDirectorySize(path string) (int64, error) {
1549
var size int64
1650
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
1751
if !info.IsDir() {
@@ -107,16 +141,60 @@ func GetDeletedEntries(d1, d2 Directory) []string {
107141
}
108142

109143
type DirDiff struct {
110-
Adds []string
111-
Dels []string
112-
Mods []string
144+
Adds []DirectoryEntry
145+
Dels []DirectoryEntry
146+
Mods []EntryDiff
147+
}
148+
149+
func GetDirectoryEntries(d Directory) []DirectoryEntry {
150+
return createDirectoryEntries(d.Root, d.Content)
151+
}
152+
153+
func createDirectoryEntries(root string, entryNames []string) (entries []DirectoryEntry) {
154+
for _, name := range entryNames {
155+
entryPath := filepath.Join(root, name)
156+
size := GetSize(entryPath)
157+
158+
entry := DirectoryEntry{
159+
Name: name,
160+
Size: size,
161+
}
162+
entries = append(entries, entry)
163+
}
164+
return entries
165+
}
166+
167+
func createEntryDiffs(root1, root2 string, entryNames []string) (entries []EntryDiff) {
168+
for _, name := range entryNames {
169+
entryPath1 := filepath.Join(root1, name)
170+
size1 := GetSize(entryPath1)
171+
172+
entryPath2 := filepath.Join(root2, name)
173+
size2 := GetSize(entryPath2)
174+
175+
entry := EntryDiff{
176+
Name: name,
177+
Size1: size1,
178+
Size2: size2,
179+
}
180+
entries = append(entries, entry)
181+
}
182+
return entries
113183
}
114184

115185
// DiffDirectory takes the diff of two directories, assuming both are completely unpacked
116186
func DiffDirectory(d1, d2 Directory) (DirDiff, bool) {
117187
adds := GetAddedEntries(d1, d2)
188+
sort.Strings(adds)
189+
addedEntries := createDirectoryEntries(d2.Root, adds)
190+
118191
dels := GetDeletedEntries(d1, d2)
192+
sort.Strings(dels)
193+
deletedEntries := createDirectoryEntries(d1.Root, dels)
194+
119195
mods := GetModifiedEntries(d1, d2)
196+
sort.Strings(mods)
197+
modifiedEntries := createEntryDiffs(d1.Root, d2.Root, mods)
120198

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

128-
return DirDiff{adds, dels, mods}, same
206+
return DirDiff{addedEntries, deletedEntries, modifiedEntries}, same
129207
}
130208

131209
func checkSameFile(f1name, f2name string) (bool, error) {

utils/tar_utils.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@ import (
1010
"github.com/golang/glog"
1111
)
1212

13-
// Directory stores a representaiton of a file directory.
14-
type Directory struct {
15-
Root string
16-
Content []string
17-
}
18-
1913
func unpackTar(tr *tar.Reader, path string) error {
2014
for {
2115
header, err := tr.Next()

utils/template_utils.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ const FSDiffOutput = `
44
-----{{.DiffType}}-----
55
66
These entries have been added to {{.Image1}}:{{if not .Diff.Adds}} None{{else}}
7-
{{range .Diff.Adds}}{{print .}}
8-
{{end}}{{end}}
7+
FILE SIZE{{range .Diff.Adds}}{{"\n"}}{{.Name}} {{.Size}}{{end}}
8+
{{end}}
99
1010
These entries have been deleted from {{.Image1}}:{{if not .Diff.Dels}} None{{else}}
11-
{{range .Diff.Dels}}{{print .}}
12-
{{end}}{{end}}
11+
FILE SIZE{{range .Diff.Dels}}{{"\n"}}{{.Name}} {{.Size}}{{end}}
12+
{{end}}
1313
1414
These entries have been changed between {{.Image1}} and {{.Image2}}:{{if not .Diff.Mods}} None{{else}}
15-
{{range .Diff.Mods}}{{print .}}
16-
{{end}}{{end}}
15+
FILE SIZE1 SIZE2{{range .Diff.Mods}}{{"\n"}}{{.Name}} {{.Size1}} {{.Size2}}{{end}}
16+
{{end}}
1717
`
1818

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

59+
const FileAnalysisOutput = `
60+
-----{{.AnalyzeType}}-----
61+
62+
Analysis for {{.Image}}:{{if not .Analysis}} None{{else}}
63+
FILE SIZE{{range .Analysis}}{{"\n"}}{{.Name}} {{.Size}}{{end}}
64+
{{end}}
65+
`
66+
5967
const MultiVersionPackageOutput = `
6068
-----{{.AnalyzeType}}-----
6169

0 commit comments

Comments
 (0)