-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilewalker.go
181 lines (163 loc) · 4.47 KB
/
filewalker.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
package artifact_diff
import (
"fmt"
"io/fs"
"log"
"os"
"path/filepath"
"strings"
)
type unarchiver func(string, string) error
var unarchiverByExt = map[string]unarchiver{
".zip": Unzip,
".jar": Unzip,
".tar": Untar,
".gz": Gunzip,
}
func CollectFileInfos(path string) (*ArtifactInfo, error) {
log.Println("scanning", path)
stat, err := os.Stat(path)
if err != nil {
return nil, err
}
var result *ArtifactInfo
if stat.IsDir() {
result, err = WalkTree(
path,
os.DirFS(path),
)
if err != nil {
return nil, err
}
normalizedFiles := getNormalizedPaths(path, "", result)
result = normalizedFiles
} else if strings.HasSuffix(path, ".zip") || strings.HasSuffix(path, ".jar") || strings.HasSuffix(path, ".gz") {
extension := filepath.Ext(path)
result, err = WalkArchive(
".",
path,
unarchiverByExt[extension],
)
if err != nil {
return nil, err
}
normalizedFiles := getNormalizedPaths(path, extension[1:], result)
result = normalizedFiles
}
result.Path = path
return result, nil
}
func getNormalizedPaths(prefix string, replacement string, result *ArtifactInfo) *ArtifactInfo {
normalizedFiles := &ArtifactInfo{
Count: result.Count,
FileInfos: FileInfos{},
}
for _, v := range result.FileInfos {
// make the f.Path look like 'path/to/content.txt'
// so that we can compare relative paths regardless of the prefix
//v.Path = fmt.Sprintf("%s%s", replacement, filepath.ToSlash(strings.TrimPrefix(v.Path, prefix)))
v.Path = fmt.Sprintf("%s%s", replacement, filepath.ToSlash(strings.TrimPrefix(filepath.ToSlash(v.Path), filepath.ToSlash(prefix))))
normalizedFiles.AddFileInfo(v.Path, v)
}
return normalizedFiles
}
func WalkTree(root string, rootFS fs.FS) (*ArtifactInfo, error) {
diff := ArtifactInfo{
FileInfos: FileInfos{},
}
err := fs.WalkDir(rootFS, ".", func(p string, d fs.DirEntry, err error) error {
if d.IsDir() {
return nil
}
diff.Count++
//log.Println(fmt.Sprintf("file[%v]: %s", diff.Count, p))
path := filepath.Clean(fmt.Sprintf("%s/%s", root, p))
info := getFileInfo(path)
diff.AddFileInfo(path, info)
if filepath.Ext(p) == ".zip" || filepath.Ext(p) == ".jar" || filepath.Ext(p) == ".gz" {
// TODO zip in zip won't work?
//archiveFs, err := zip.OpenReader(p)
//if err != nil {
// log.Println(fmt.Sprintf("error reading archive %s at tree %s: %v", p, root, err))
// return err
//}
// //var AppFs = afero.NewMemMapFs()
// //var AppFs = afero.NewOsFs()
archiveDiff, err := WalkArchive(root, p, unarchiverByExt[filepath.Ext(p)])
if err != nil {
log.Println(fmt.Sprintf("error walking archive: %v", err))
return err
}
diff.Count = diff.Count + archiveDiff.Count
for _, f := range archiveDiff.FileInfos {
diff.AddFileInfo(f.Path, f)
}
}
return nil
})
return &diff, err
}
func WalkArchive(root string, zip string, unarchiver unarchiver) (*ArtifactInfo, error) {
temp, err := os.MkdirTemp(
"",
fmt.Sprintf("artifact-diff_unzipped_%s",
strings.ReplaceAll(
strings.ReplaceAll(filepath.ToSlash(zip), "/", "_"),
":", "_")))
if err != nil {
log.Println(fmt.Sprintf("error creating temporary directory for unzipping %s: %v", zip, err))
return nil, nil
}
log.Println(fmt.Sprintf("tmp dir: %s", temp))
absSrc := zip
if !filepath.IsAbs(zip) {
absSrc, _ = filepath.Abs(fmt.Sprintf("%s/%s", root, zip))
}
err = unarchiver(absSrc, temp)
if err != nil {
log.Println(fmt.Sprintf("unzip err %v", err))
return nil, nil
}
defer func() {
err = os.RemoveAll(temp)
if err != nil {
log.Println(fmt.Sprintf("error removing temporary files at %s: %v", temp, err))
}
}()
tmpDirFS := os.DirFS(temp)
files, err := WalkTree(temp, tmpDirFS)
if err != nil {
return nil, err
}
normalizedFiles := &ArtifactInfo{
Count: files.Count,
FileInfos: FileInfos{},
}
for _, v := range files.FileInfos {
// make the f.Path look like 'file.zip!/path/to/content.txt
// so that we remove the system specific temp directory prefix
v.Path = fmt.Sprintf("%s!%s", filepath.ToSlash(zip), filepath.ToSlash(strings.TrimPrefix(v.Path, temp)))
normalizedFiles.AddFileInfo(v.Path, v)
}
files = nil
return normalizedFiles, nil
}
func getFileInfo(path string) *FileInfo {
info := &FileInfo{
Path: path,
}
stat, err := os.Stat(path)
if err != nil {
//return err
} else {
info.Filesize = stat.Size()
}
hash, err := checksum(path)
if err != nil {
//return err
} else {
info.Checksum = hash
}
//log.Println(fmt.Sprintf("info: %s", info))
return info
}