/
pack.go
169 lines (141 loc) · 4.11 KB
/
pack.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
package icepacker
import (
"os"
"path/filepath"
"regexp"
)
// PackSettings records the settings of the packing
type PackSettings struct {
SourceDir string
TargetFilename string
Includes string
Excludes string
Compression byte
Encryption byte
Cipher CipherSettings
OnProgress chan ProgressState
OnFinish chan FinishResult
}
// Progress push a success ProgressState instance to the OnProgress channel.
func (this *PackSettings) Progress(total, index int, filename string) {
if this.OnProgress != nil {
this.OnProgress <- ProgressState{nil, total, index, filename}
}
}
// ProgressError push an error ProgressState instance to the OnProgress channel.
func (this *PackSettings) ProgressError(err error, filename string) {
if this.OnProgress != nil {
this.OnProgress <- ProgressState{err, 0, 0, filename}
}
}
// Finish returns a success FinishResult instance and put to the OnFinish channel if it's not nil
func (this *PackSettings) Finish(err error, fileCount int64, size int64, dupCount int, dupSize int64) FinishResult {
ret := FinishResult{err, fileCount, size, dupCount, dupSize}
if this.OnFinish != nil {
this.OnFinish <- ret
}
return ret
}
// Finish returns an errored FinishResult instance and put to the OnFinish channel if it's not nil
func (this *PackSettings) FinishError(err error) FinishResult {
ret := FinishResult{Err: err}
if this.OnFinish != nil {
this.OnFinish <- ret
}
return ret
}
// collectFiles walk directories recursively and collect files considering include and exclude filters
func collectFiles(settings PackSettings) []string {
files := []string{}
filepath.Walk(FixPath(settings.SourceDir), func(path string, f os.FileInfo, err error) error {
if err != nil {
if f != nil {
settings.ProgressError(err, f.Name())
} else {
settings.ProgressError(err, path)
}
return err
}
if f != nil {
needAppend := true
// Matching include filter
if settings.Includes != "" && !f.IsDir() {
if matched, _ := regexp.MatchString(settings.Includes, path); matched {
} else {
needAppend = false
}
}
// Unmatching exclude filter
if needAppend && settings.Excludes != "" {
if matched, _ := regexp.MatchString(settings.Excludes, path); !matched {
} else {
needAppend = false
}
}
if needAppend {
if !f.IsDir() {
files = append(files, path)
}
} else {
if f.IsDir() {
return filepath.SkipDir
}
}
}
return nil
})
return files
}
// Pack bundles the files of the source directory to the target package file.
func Pack(settings PackSettings) FinishResult {
// Hash the cipher key
shaKey := HashingKey(settings.Cipher)
// Create a new bundle
bundle, err := CreateBundle(settings.TargetFilename, BundleSettings{Compression: settings.Compression, Encryption: settings.Encryption, CipherKey: shaKey})
if err != nil {
return settings.FinishError(err)
}
defer bundle.Close()
// Get info from source
sourceInfo, err := os.Stat(settings.SourceDir)
if err != nil {
return settings.FinishError(err)
}
files := []string{}
if sourceInfo.IsDir() {
// Collect files from source directory
files = collectFiles(settings)
} else {
// SourceDir is a file, not a directory
files = append(files, settings.SourceDir)
}
fileCount := len(files)
for i, file := range files {
func(i int, file string) {
var relativePath string
if file == settings.SourceDir {
// SourceDir is a file
relativePath = filepath.Base(FixPath(settings.SourceDir))
} else {
relativePath, _ = filepath.Rel(FixPath(settings.SourceDir), file)
}
// Update progress state at every 100th file
if i%100 == 0 {
settings.Progress(fileCount, i, relativePath)
}
_, err := bundle.AddFile(relativePath, file)
if err != nil {
settings.ProgressError(err, relativePath)
return
}
}(i, file)
}
// Update progress state
settings.Progress(fileCount, fileCount, "")
err = bundle.Flush()
if err != nil {
return settings.FinishError(err)
}
// Process finished
return settings.Finish(nil, bundle.FAT.Count, bundle.Footer.PackSize, bundle.DupCount, bundle.DupSize)
}