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

Commit 2103dc7

Browse files
committed
Adding size to FileDiff
1 parent e6d04ae commit 2103dc7

File tree

4 files changed

+48
-7
lines changed

4 files changed

+48
-7
lines changed

differs/fileDiff.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ func diffImageFiles(image1, image2 utils.Image) (utils.DirDiff, error) {
3434
sort.Strings(adds)
3535
dels := utils.GetDeletedEntries(img1Dir, img2Dir)
3636
sort.Strings(dels)
37+
mods := utils.GetModifiedEntries(img1Dir, img2Dir)
38+
sort.Strings(mods)
3739

3840
diff = utils.DirDiff{
3941
Image1: image1.Source,
4042
Image2: image2.Source,
4143
Adds: adds,
4244
Dels: dels,
43-
Mods: []string{},
45+
Mods: mods,
4446
}
4547
return diff, nil
4648
}

differs/nodeDiff.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (d NodeDiffer) getPackages(image utils.Image) (map[string]map[string]utils.
5656
var currInfo utils.PackageInfo
5757
currInfo.Version = packageJSON.Version
5858
packagePath := strings.TrimSuffix(currPackage, "package.json")
59-
size, err := utils.GetDirectorySize(packagePath)
59+
size, err := utils.GetSize(packagePath)
6060
if err != nil {
6161
glog.Warningf("Error getting package size at %s: %s\n", currPackage, err)
6262
return packages, err

differs/pipDiff.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (d PipDiffer) getPackages(image utils.Image) (map[string]map[string]utils.P
9494
var size string
9595
if i-1 >= 0 && contents[i-1].Name() == packageName {
9696
packagePath := filepath.Join(pythonPath, packageName)
97-
intSize, err := utils.GetDirectorySize(packagePath)
97+
intSize, err := utils.GetSize(packagePath)
9898
if err != nil {
9999
glog.Errorf("Could not obtain size for package %s", packagePath)
100100
size = ""

utils/fs_utils.go

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,32 @@ import (
99
"strings"
1010

1111
"github.com/golang/glog"
12+
"code.cloudfoundry.org/bytefmt"
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 string
24+
}
25+
26+
func GetSize(path string) (int64, error) {
27+
stat, err := os.Stat(path)
28+
if err != nil {
29+
return -1, errors.Errorf("Could not get size for %s: %s", path, err)
30+
}
31+
if stat.IsDir() {
32+
return getDirectorySize(path)
33+
}
34+
return stat.Size(), nil
35+
}
36+
37+
func getDirectorySize(path string) (int64, error) {
1538
var size int64
1639
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
1740
if !info.IsDir() {
@@ -109,9 +132,25 @@ func GetDeletedEntries(d1, d2 Directory) []string {
109132
type DirDiff struct {
110133
Image1 string
111134
Image2 string
112-
Adds []string
113-
Dels []string
114-
Mods []string
135+
Adds []DirectoryEntry
136+
Dels []DirectoryEntry
137+
Mods []DirectoryEntry
138+
}
139+
140+
func createDirectoryEntries(entryNames []string) (entries []DirectoryEntry) {
141+
for _, name := range entryNames {
142+
strSize := ""
143+
size, err := GetSize(name)
144+
if err == nil {
145+
strSize = bytefmt.ByteSize(size)
146+
}
147+
entry := DirectoryEntry{
148+
Name: name,
149+
Size: strSize,
150+
}
151+
entries = append(entries, entry)
152+
}
153+
return entries
115154
}
116155

117156
// DiffDirectory takes the diff of two directories, assuming both are completely unpacked

0 commit comments

Comments
 (0)