-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathupgrade.go
128 lines (100 loc) · 3.57 KB
/
upgrade.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
package operate
import (
"fmt"
"path/filepath"
"github.com/itchio/butler/installer/bfs"
itchio "github.com/itchio/go-itchio"
"github.com/itchio/httpkit/progress"
"github.com/itchio/savior/filesource"
"github.com/itchio/wharf/eos/option"
"github.com/itchio/wharf/pools/fspool"
"github.com/itchio/wharf/pwr/bowl"
"github.com/itchio/wharf/pwr/patcher"
"github.com/pkg/errors"
)
func upgrade(oc *OperationContext, meta *MetaSubcontext, isub *InstallSubcontext, receiptIn *bfs.Receipt) error {
consumer := oc.Consumer()
istate := isub.Data
totalPatches := len(istate.UpgradePath.Builds)
donePatches := istate.UpgradePathIndex
remainingPatches := totalPatches - donePatches
consumer.Infof("Applying %d patches (%d already done)", remainingPatches, donePatches)
for i := istate.UpgradePathIndex; i < totalPatches; i++ {
build := istate.UpgradePath.Builds[i]
err := applyPatch(oc, meta, isub, receiptIn, i)
if err != nil {
return errors.WithMessage(err, fmt.Sprintf("while applying patch %d/%d (build %d)", i, totalPatches, build.ID))
}
}
return nil
}
func applyPatch(oc *OperationContext, meta *MetaSubcontext, isub *InstallSubcontext, receiptIn *bfs.Receipt, upgradePathIndex int) error {
rc := oc.rc
consumer := oc.Consumer()
params := meta.Data
istate := isub.Data
build := istate.UpgradePath.Builds[upgradePathIndex]
LogBuild(consumer, params.Upload, build)
client := rc.Client(params.Access.APIKey)
subType := itchio.BuildFileSubTypeDefault
if FindBuildFile(build.Files, itchio.BuildFileTypePatch, itchio.BuildFileSubTypeOptimized) != nil {
subType = itchio.BuildFileSubTypeOptimized
}
patchURL := client.MakeBuildDownloadURL(itchio.MakeBuildDownloadParams{
Credentials: params.Access.Credentials,
BuildID: build.ID,
Type: itchio.BuildFileTypePatch,
SubType: subType,
UUID: istate.DownloadSessionID,
})
patchSource, err := filesource.Open(patchURL, option.WithConsumer(consumer))
if err != nil {
return errors.Wrap(err, "opening remote patch")
}
consumer.Infof("Patch is %s", progress.FormatBytes(patchSource.Size()))
p, err := patcher.New(patchSource, consumer)
if err != nil {
return errors.Wrap(err, "creating patcher")
}
targetPool := fspool.New(p.GetTargetContainer(), params.InstallFolder)
stageFolder := filepath.Join(params.StagingFolder, "patch-overlay")
bowl, err := bowl.NewOverlayBowl(&bowl.OverlayBowlParams{
TargetContainer: p.GetTargetContainer(),
SourceContainer: p.GetSourceContainer(),
OutputFolder: params.InstallFolder,
StageFolder: stageFolder,
})
if err != nil {
return errors.WithMessage(err, "while creating bowl for patch")
}
var checkpoint *patcher.Checkpoint
err = p.Resume(checkpoint, targetPool, bowl)
if err != nil {
return errors.WithMessage(err, "while applying patch")
}
err = bowl.Commit()
if err != nil {
return errors.WithMessage(err, "while committing patch")
}
consumer.Infof("Patching done, getting signature info...")
res := resultForContainer(p.GetSourceContainer())
err = commitInstall(oc, &CommitInstallParams{
InstallFolder: params.InstallFolder,
// if we're applying patches, 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: build,
InstallResult: res,
})
if err != nil {
return errors.WithMessage(err, "while committing install")
}
istate.UpgradePathIndex = upgradePathIndex + 1
err = oc.Save(isub)
if err != nil {
return errors.WithMessage(err, "while saving install subcontext")
}
return nil
}