-
Notifications
You must be signed in to change notification settings - Fork 8
/
dir.go
251 lines (229 loc) · 6.87 KB
/
dir.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
239
240
241
242
243
244
245
246
247
248
249
250
251
//--------------------------------------------------------------------------------------------------
// This file is a part of Gorsync Backup project (backup RSYNC frontend).
// Copyright (c) 2017-2020 Denis Dyakov <denis.dyakov@gmail.com>
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//--------------------------------------------------------------------------------------------------
package core
import (
"fmt"
"io/ioutil"
"os"
)
// DirMetrics keeps metrics defined in 1st pass of folders tree.
// Metrics used lately in heuristic algorithm to find optimal folder tree traverse.
type DirMetrics struct {
// Define depth from root folder. Root folder has Depth = 0.
Depth int
// Total count of all child folders.
ChildrenCount int
// "Size" metric defines summary size of all local files,
// do not include any child folders.
Size *FolderSize
// "Full size" metric, which include all files and
// child folders with their content.
FullSize *FolderSize
// Flag which means, that folder contain special file
// which serves as signal to skip backup this folder.
IgnoreToBackup bool
// Flag which means, that this folder already marked
// as "measured" in traverse path search.
Measured bool
// Type of backup for current folder defined
// as a result of traverse path search.
BackupType FolderBackupType
}
// Dir is a "tree data structure" to describe folder's tree
// received from the source in 1st pass of backup process to measure
// counts/sizes and to predict time necessary for backup process (ETA).
// https://en.wikipedia.org/wiki/Tree_%28data_structure%29
type Dir struct {
Paths SrcDstPath
Name string
Parent *Dir
Childs []*Dir
Metrics DirMetrics
}
// BuildDirTree scans and creates Dir object which reflects
// real recursive directory structure defined by file system path
// in paths argument.
func BuildDirTree(paths SrcDstPath, ignoreBackupFileSigName string) (*Dir, error) {
info, err := os.Stat(paths.DestPath)
if err != nil {
return nil, err
}
if !info.IsDir() {
// does not translate this message, since it is very unlikely
return nil, fmt.Errorf("path %q should be a folder", paths.DestPath)
}
root := &Dir{Name: info.Name(), Paths: paths, Metrics: DirMetrics{Depth: 0}}
_, err = createOffsprings(root, paths, ignoreBackupFileSigName, 1)
if err != nil {
return nil, err
}
return root, nil
}
// GetTotalSize calculates total size of data
// to backup, including all subfolders.
func (v *Dir) GetTotalSize() FolderSize {
// use nested call to make recursive calculations
return getTotalSize(v)
}
// GetIgnoreSize calculates total size of data
// which marked as "skip to backup" including all subfolders.
func (v *Dir) GetIgnoreSize() FolderSize {
// use nested call to make recursive calculations
return getIgnoreSize(v)
}
// GetFullBackupSize calculates total size of data
// which marked to backup "full content".
func (v *Dir) GetFullBackupSize() FolderSize {
// use nested call to make recursive calculations
return getFullBackupSize(v)
}
// GetContentBackupSize calculates total size of data
// which marked to backup "local folder content".
func (v *Dir) GetContentBackupSize() FolderSize {
// use nested call to make recursive calculations
return getContentBackupSize(v)
}
// GetFoldersCount return total folder count
// in this directory tree.
func (v *Dir) GetFoldersCount() int {
// use nested call to make recursive calculations
return getFoldersCount(v)
}
// GetFoldersIgnoreCount return total folder count
// to "skip to backup" in this directory tree.
func (v *Dir) GetFoldersIgnoreCount() int {
// use nested call to make recursive calculations
return getFoldersIgnoreCount(v)
}
/*
func containsMeasuredDir(dir *Dir) bool {
if dir.Metrics.Measured {
return true
}
for _, item := range dir.Childs {
if containsMeasuredDir(item) {
return true
}
}
return false
}
func containsNonMeasuredDir(dir *Dir) bool {
if !dir.Metrics.Measured {
return true
}
for _, item := range dir.Childs {
if containsNonMeasuredDir(item) {
return true
}
}
return false
}
*/
func getTotalSize(dir *Dir) FolderSize {
var size FolderSize
if dir.Metrics.BackupType == FBT_CONTENT {
size = *dir.Metrics.Size
} else if dir.Metrics.BackupType == FBT_RECURSIVE {
size = *dir.Metrics.FullSize
} else if dir.Metrics.BackupType == FBT_SKIP {
size = *dir.Metrics.FullSize
}
for _, item := range dir.Childs {
size += getTotalSize(item)
}
return size
}
func getFullBackupSize(dir *Dir) FolderSize {
var size FolderSize
if dir.Metrics.BackupType == FBT_RECURSIVE {
size = *dir.Metrics.FullSize
}
for _, item := range dir.Childs {
size += getFullBackupSize(item)
}
return size
}
func getContentBackupSize(dir *Dir) FolderSize {
var size FolderSize
if dir.Metrics.BackupType == FBT_CONTENT {
size = *dir.Metrics.Size
}
for _, item := range dir.Childs {
size += getContentBackupSize(item)
}
return size
}
func getIgnoreSize(dir *Dir) FolderSize {
var size FolderSize
if dir.Metrics.BackupType == FBT_SKIP {
size = *dir.Metrics.FullSize
}
for _, item := range dir.Childs {
size += getIgnoreSize(item)
}
return size
}
func getFoldersIgnoreCount(dir *Dir) int {
count := 0
if dir.Metrics.BackupType == FBT_SKIP {
count++
}
for _, item := range dir.Childs {
count += getFoldersIgnoreCount(item)
}
return count
}
func getFoldersCount(dir *Dir) int {
count := len(dir.Childs)
for _, item := range dir.Childs {
count += getFoldersCount(item)
}
return count
}
func createOffsprings(parent *Dir, paths SrcDstPath,
sigFileIgnoreBackup string, depth int) (int, error) {
// lg.Debug(f("Iterate path: %q", path))
items, err := ioutil.ReadDir(paths.DestPath)
if err != nil {
return 0, err
}
if sigFileIgnoreBackupFound(items, sigFileIgnoreBackup) {
parent.Metrics.IgnoreToBackup = true
parent.Metrics.ChildrenCount = 1
return 1, nil
}
totalCount := 1
for _, item := range items {
if item.IsDir() {
name := item.Name()
paths2 := paths.Join(name)
dir := &Dir{Parent: parent, Name: name, Paths: paths2,
Metrics: DirMetrics{Depth: depth}}
count, err := createOffsprings(dir, paths2,
sigFileIgnoreBackup, depth+1)
if err != nil {
return 0, err
}
parent.Childs = append(parent.Childs, dir)
totalCount += count
}
}
parent.Metrics.ChildrenCount = totalCount
return totalCount, nil
}
func sigFileIgnoreBackupFound(items []os.FileInfo, sigFileIgnoreBackup string) bool {
for _, item := range items {
if !item.IsDir() && item.Name() == sigFileIgnoreBackup {
return true
}
}
return false
}