/
fs.go
executable file
·87 lines (70 loc) · 1.48 KB
/
fs.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
package http
import (
"bytes"
"io/ioutil"
"net/http"
"path/filepath"
"sort"
)
// EqualFileSystems traverses two http.FileSystem instances and returns true
// if they are equal
func EqualFileSystems(fs, otherFS http.FileSystem, currentPath string) bool {
file, err := fs.Open(currentPath)
if err != nil {
return false
}
otherFile, err := otherFS.Open(currentPath)
if err != nil {
return false
}
stat, err := file.Stat()
if err != nil {
return false
}
otherStat, err := otherFile.Stat()
if err != nil {
return false
}
if stat.IsDir() != otherStat.IsDir() {
return false
}
if !stat.IsDir() {
var fileBytes, otherFileBytes []byte
fileBytes, err = ioutil.ReadAll(file)
if err != nil {
return false
}
otherFileBytes, err = ioutil.ReadAll(otherFile)
if err != nil {
return false
}
return bytes.Equal(fileBytes, otherFileBytes)
}
files, err := file.Readdir(0)
if err != nil {
return false
}
otherFiles, err := otherFile.Readdir(0)
if err != nil {
return false
}
if len(files) != len(otherFiles) {
return false
}
sort.Slice(files, func(i, j int) bool {
return files[i].Name() < files[j].Name()
})
sort.Slice(otherFiles, func(i, j int) bool {
return otherFiles[i].Name() < otherFiles[j].Name()
})
for i := range files {
if files[i].Name() != otherFiles[i].Name() {
return false
}
nextPath := filepath.Join(currentPath, files[i].Name())
if !EqualFileSystems(fs, otherFS, nextPath) {
return false
}
}
return true
}