@@ -2,11 +2,11 @@ package utils
22
33import (
44 "bytes"
5- "encoding/json"
65 "fmt"
76 "io/ioutil"
87 "os"
98 "path/filepath"
9+ "strings"
1010
1111 "github.com/golang/glog"
1212)
@@ -22,18 +22,33 @@ func GetDirectorySize(path string) (int64, error) {
2222 return size , err
2323}
2424
25- func GetDirectory (dirpath string ) (Directory , error ) {
26- dirfile , err := ioutil .ReadFile (dirpath )
27- if err != nil {
28- return Directory {}, err
29- }
25+ // GetDirectoryContents converts the directory starting at the provided path into a Directory struct.
26+ func GetDirectory (path string , deep bool ) (Directory , error ) {
27+ var directory Directory
28+ directory .Root = path
29+ var err error
30+ if deep {
31+ walkFn := func (currPath string , info os.FileInfo , err error ) error {
32+ newContent := strings .TrimPrefix (currPath , directory .Root )
33+ if newContent != "" {
34+ directory .Content = append (directory .Content , newContent )
35+ }
36+ return nil
37+ }
3038
31- var dir Directory
32- err = json .Unmarshal (dirfile , & dir )
33- if err != nil {
34- return Directory {}, err
39+ err = filepath .Walk (path , walkFn )
40+ } else {
41+ contents , err := ioutil .ReadDir (path )
42+ if err != nil {
43+ return directory , err
44+ }
45+
46+ for _ , file := range contents {
47+ fileName := "/" + file .Name ()
48+ directory .Content = append (directory .Content , fileName )
49+ }
3550 }
36- return dir , nil
51+ return directory , err
3752}
3853
3954// Checks for content differences between files of the same name from different directories
@@ -53,6 +68,22 @@ func GetModifiedEntries(d1, d2 Directory) []string {
5368 glog .Errorf ("Error checking directory entry %s: %s\n " , f , err )
5469 continue
5570 }
71+ f2stat , err := os .Stat (f2path )
72+ if err != nil {
73+ glog .Errorf ("Error checking directory entry %s: %s\n " , f , err )
74+ continue
75+ }
76+
77+ // If the directory entry in question is a tar, verify that the two have the same size
78+ if isTar (f1path ) {
79+ if f1stat .Size () != f2stat .Size () {
80+ modified = append (modified , f )
81+ }
82+ continue
83+ }
84+
85+ // If the directory entry is not a tar and not a directory, then it's a file so make sure the file contents are the same
86+ // Note: We skip over directory entries because to compare directories, we compare their contents
5687 if ! f1stat .IsDir () {
5788 same , err := checkSameFile (f1path , f2path )
5889 if err != nil {
@@ -83,12 +114,20 @@ type DirDiff struct {
83114 Mods []string
84115}
85116
86- func compareDirEntries (d1 , d2 Directory ) DirDiff {
117+ // DiffDirectory takes the diff of two directories, assuming both are completely unpacked
118+ func DiffDirectory (d1 , d2 Directory ) (DirDiff , bool ) {
87119 adds := GetAddedEntries (d1 , d2 )
88120 dels := GetDeletedEntries (d1 , d2 )
89121 mods := GetModifiedEntries (d1 , d2 )
90122
91- return DirDiff {d1 .Root , d2 .Root , adds , dels , mods }
123+ var same bool
124+ if len (adds ) == 0 && len (dels ) == 0 && len (mods ) == 0 {
125+ same = true
126+ } else {
127+ same = false
128+ }
129+
130+ return DirDiff {d1 .Root , d2 .Root , adds , dels , mods }, same
92131}
93132
94133func checkSameFile (f1name , f2name string ) (bool , error ) {
@@ -121,7 +160,3 @@ func checkSameFile(f1name, f2name string) (bool, error) {
121160 }
122161 return true , nil
123162}
124-
125- func DiffDirectory (d1 , d2 Directory ) DirDiff {
126- return compareDirEntries (d1 , d2 )
127- }
0 commit comments