forked from GoogleContainerTools/container-diff
/
fs_utils.go
238 lines (204 loc) · 5.32 KB
/
fs_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package utils
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
"github.com/golang/glog"
)
// 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
}
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() {
size += info.Size()
}
return err
})
return size, err
}
// GetDirectoryContents converts the directory starting at the provided path into a Directory struct.
func GetDirectory(path string, deep bool) (Directory, error) {
var directory Directory
directory.Root = path
var err error
if deep {
walkFn := func(currPath string, info os.FileInfo, err error) error {
newContent := strings.TrimPrefix(currPath, directory.Root)
if newContent != "" {
directory.Content = append(directory.Content, newContent)
}
return nil
}
err = filepath.Walk(path, walkFn)
} else {
contents, err := ioutil.ReadDir(path)
if err != nil {
return directory, err
}
for _, file := range contents {
fileName := "/" + file.Name()
directory.Content = append(directory.Content, fileName)
}
}
return directory, err
}
// Checks for content differences between files of the same name from different directories
func GetModifiedEntries(d1, d2 Directory) []string {
d1files := d1.Content
d2files := d2.Content
filematches := GetMatches(d1files, d2files)
modified := []string{}
for _, f := range filematches {
f1path := fmt.Sprintf("%s%s", d1.Root, f)
f2path := fmt.Sprintf("%s%s", d2.Root, f)
f1stat, err := os.Stat(f1path)
if err != nil {
glog.Errorf("Error checking directory entry %s: %s\n", f, err)
continue
}
f2stat, err := os.Stat(f2path)
if err != nil {
glog.Errorf("Error checking directory entry %s: %s\n", f, err)
continue
}
// If the directory entry in question is a tar, verify that the two have the same size
if isTar(f1path) {
if f1stat.Size() != f2stat.Size() {
modified = append(modified, f)
}
continue
}
// 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
// Note: We skip over directory entries because to compare directories, we compare their contents
if !f1stat.IsDir() {
same, err := checkSameFile(f1path, f2path)
if err != nil {
glog.Errorf("Error diffing contents of %s and %s: %s\n", f1path, f2path, err)
continue
}
if !same {
modified = append(modified, f)
}
}
}
return modified
}
func GetAddedEntries(d1, d2 Directory) []string {
return GetAdditions(d1.Content, d2.Content)
}
func GetDeletedEntries(d1, d2 Directory) []string {
return GetDeletions(d1.Content, d2.Content)
}
type DirDiff struct {
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 {
same = true
} else {
same = false
}
return DirDiff{addedEntries, deletedEntries, modifiedEntries}, same
}
func checkSameFile(f1name, f2name string) (bool, error) {
// Check first if files differ in size and immediately return
f1stat, err := os.Stat(f1name)
if err != nil {
return false, err
}
f2stat, err := os.Stat(f2name)
if err != nil {
return false, err
}
if f1stat.Size() != f2stat.Size() {
return false, nil
}
// Next, check file contents
f1, err := ioutil.ReadFile(f1name)
if err != nil {
return false, err
}
f2, err := ioutil.ReadFile(f2name)
if err != nil {
return false, err
}
if !bytes.Equal(f1, f2) {
return false, nil
}
return true, nil
}