-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworkerLink.go
136 lines (126 loc) · 2.5 KB
/
workerLink.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
package fsdd
import (
"os"
)
//
// WORKER SECTION
//
func feedLinkStats() {
var result [][]string
switch c.FastHash {
case true:
result = consolidateFastHash()
default:
result = consolidateHash()
}
for _, v := range result {
l := len(v)
var save uint64
if c.Verbose {
out("parent [" + v[0] + "]")
}
for i := 1; i < l; i++ {
if ifs[v[0]] != ifs[v[i]] {
switch {
case c.HardLink:
linkChan <- &file{name: v[0], newlinktarget: v[i], size: sfs[v[0]]}
save += sfs[v[i]]
sinodes++
case c.SymLink:
linkChan <- &file{name: v[0], newlinktarget: v[i], size: sfs[v[0]], newSymLink: true}
save += sfs[v[i]]
case c.Verbose:
out("-> identic [" + v[i] + "]")
save += sfs[v[i]]
sinodes++
default:
save += sfs[v[i]]
sinodes++
}
}
}
stotal += save
}
ctl.Done()
}
func workerLink(worker int) {
for i := 0; i < worker; i++ {
go func() {
for f := range linkChan {
if !linkFast(f) {
failChan <- f
}
}
bg.Done()
}()
}
}
func collectStats() {
for f := range failChan {
failsize += f.size
switch {
case f.newSymLink:
failssym++
case f.syminvalid:
failisym++
default:
failinodes++
}
}
global.Done()
}
func consolidateFastHash() [][]string {
var r [][]string
for _, v := range hFs {
if len(v) > 1 {
r = append(r, v)
}
}
return r
}
func consolidateHash() [][]string {
var r [][]string
for _, v := range hfs {
if len(v) > 1 {
r = append(r, v)
}
}
return r
}
//
// BACKEND
//
const _templink = ".fsdd.temp.link.pls.remove.me"
func linkFast(f *file) bool {
var err error
if c.Verbose {
switch {
case f.newSymLink:
out("-> new SymLink [" + f.name + "] -> [" + f.newlinktarget + "]")
default:
out("-> new HardLink [" + f.name + "] -> [" + f.newlinktarget + "]")
}
}
tlink := f.newlinktarget + _templink
err = os.Rename(f.newlinktarget, tlink)
if err != nil {
errOut("[link] unable to rename [" + f.newlinktarget + "] [" + err.Error() + "]")
return false
}
switch {
case f.newSymLink:
err = os.Symlink(f.name, f.newlinktarget)
default:
err = os.Link(f.name, f.newlinktarget)
}
if err != nil {
errOut("[link] [unable to link] [" + f.name + "] [" + f.newlinktarget + "] [" + err.Error() + "]")
errExit("[link] unrecoverable error, please restore [" + f.newlinktarget + "] via [" + f.name + "] manually, EXIT")
}
err = os.Remove(tlink)
if err != nil {
errOut("[link] unable to rename [" + tlink + "] [" + err.Error() + "]")
return false
}
return true
}