-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathheal.go
169 lines (134 loc) · 4.13 KB
/
heal.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 operate
import (
"fmt"
"time"
"github.com/itchio/butler/butlerd"
"github.com/itchio/butler/butlerd/messages"
"github.com/itchio/butler/installer"
"github.com/itchio/butler/installer/bfs"
"github.com/itchio/httpkit/progress"
"github.com/itchio/savior/seeksource"
"github.com/itchio/wharf/eos"
"github.com/itchio/wharf/eos/option"
"github.com/itchio/wharf/tlc"
"github.com/itchio/wharf/pwr"
"github.com/pkg/errors"
)
func heal(oc *OperationContext, meta *MetaSubcontext, isub *InstallSubcontext, receiptIn *bfs.Receipt) error {
consumer := oc.Consumer()
istate := isub.Data
params := meta.Data
if params.Build == nil {
return errors.New("heal: missing build")
}
messages.TaskStarted.Notify(oc.rc, butlerd.TaskStartedNotification{
Reason: butlerd.TaskReasonInstall,
Type: butlerd.TaskTypeHeal,
Game: params.Game,
Upload: params.Upload,
Build: params.Build,
})
client := oc.rc.Client(params.Access.APIKey)
signatureURL := MakeSourceURL(client, consumer, istate.DownloadSessionID, params, "signature")
archiveURL := MakeSourceURL(client, consumer, istate.DownloadSessionID, params, "archive")
healSpec := fmt.Sprintf("archive,%s", archiveURL)
vc := &pwr.ValidatorContext{
Consumer: consumer,
NumWorkers: 1,
HealPath: healSpec,
}
signatureFile, err := eos.Open(signatureURL, option.WithConsumer(consumer))
if err != nil {
return errors.WithStack(err)
}
defer signatureFile.Close()
stat, err := signatureFile.Stat()
if err != nil {
return errors.WithStack(err)
}
consumer.Infof("Fetching + parsing %s signature...",
progress.FormatBytes(stat.Size()),
)
signatureSource := seeksource.FromFile(signatureFile)
timeBeforeSig := time.Now()
_, err = signatureSource.Resume(nil)
if err != nil {
return errors.WithStack(err)
}
sigInfo, err := pwr.ReadSignature(oc.ctx, signatureSource)
if err != nil {
return errors.WithStack(err)
}
consumer.Infof("✓ Fetched signature in %s, dealing with %s container",
time.Since(timeBeforeSig),
progress.FormatBytes(sigInfo.Container.Size),
)
consumer.Infof("Healing container...")
timeBeforeHeal := time.Now()
oc.rc.StartProgress()
err = vc.Validate(oc.ctx, params.InstallFolder, sigInfo)
oc.rc.EndProgress()
if err != nil {
return errors.WithStack(err)
}
healDuration := time.Since(timeBeforeHeal)
containerSize := sigInfo.Container.Size
if vc.WoundsConsumer.HasWounds() {
if healer, ok := vc.WoundsConsumer.(pwr.Healer); ok {
totalHealed := healer.TotalHealed()
perSec := progress.FormatBPS(totalHealed, healDuration)
consumer.Infof("✓ %s corrupted data found (of %s total), %s healed @ %s/s, %s total",
progress.FormatBytes(vc.WoundsConsumer.TotalCorrupted()),
progress.FormatBytes(sigInfo.Container.Size),
progress.FormatBytes(totalHealed),
perSec,
progress.FormatDuration(healDuration),
)
} else {
consumer.Warnf("%s corrupted data found (of %s total)",
progress.FormatBytes(vc.WoundsConsumer.TotalCorrupted()),
progress.FormatBytes(sigInfo.Container.Size),
)
}
} else {
perSec := progress.FormatBPS(containerSize, healDuration)
consumer.Infof("✓ All %s were healthy (checked @ %s/s, %s total)",
progress.FormatBytes(containerSize),
perSec,
healDuration,
)
}
res := resultForContainer(sigInfo.Container)
consumer.Infof("Busting ghosts...")
err = bfs.BustGhosts(&bfs.BustGhostsParams{
Folder: params.InstallFolder,
NewFiles: res.Files,
Receipt: receiptIn,
Consumer: consumer,
})
if err != nil {
return errors.WithStack(err)
}
return commitInstall(oc, &CommitInstallParams{
InstallFolder: params.InstallFolder,
// if we're healing, it's a wharf-enabled upload,
// and if it's a wharf-enabled upload, our installer is "archive".
InstallerName: "archive",
Game: params.Game,
Upload: params.Upload,
Build: params.Build,
InstallResult: res,
})
}
func resultForContainer(c *tlc.Container) *installer.InstallResult {
res := &installer.InstallResult{
Files: nil,
}
for _, f := range c.Files {
res.Files = append(res.Files, f.Path)
}
for _, f := range c.Symlinks {
res.Files = append(res.Files, f.Path)
}
return res
}