-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathget_installer_info.go
198 lines (167 loc) Β· 5.44 KB
/
get_installer_info.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
package installer
import (
"io"
"path/filepath"
"time"
"github.com/itchio/boar"
"github.com/itchio/dash"
"github.com/itchio/pelican"
"github.com/itchio/savior"
"github.com/itchio/wharf/eos"
"github.com/itchio/wharf/state"
"github.com/pkg/errors"
)
func GetInstallerInfo(consumer *state.Consumer, file eos.File) (*InstallerInfo, error) {
stat, err := file.Stat()
if err != nil {
return nil, errors.WithStack(err)
}
target := stat.Name()
ext := filepath.Ext(target)
name := filepath.Base(target)
consumer.Infof("β For source (%s)", name)
if typ, ok := installerForExt[ext]; ok {
if typ == InstallerTypeArchive {
// let code flow, probe it as archive
} else {
consumer.Infof("β Using file extension registry (%s)", typ)
return &InstallerInfo{
Type: typ,
}, nil
}
}
// configurator is what we do first because it's generally fast:
// it shouldn't read *much* of the remote file, and with htfs
// caching, it's even faster. whereas 7-zip might read a *bunch*
// of an .exe file before it gives up
consumer.Infof(" Probing with dash...")
beforeConfiguratorProbe := time.Now()
candidate, err := dash.Sniff(file, target, stat.Size())
if err != nil {
return nil, errors.WithStack(err)
}
consumer.Debugf(" (took %s)", time.Since(beforeConfiguratorProbe))
var typePerConfigurator = InstallerTypeUnknown
if candidate != nil {
consumer.Infof(" Candidate: %s", candidate.String())
typePerConfigurator, err = getInstallerTypeForCandidate(consumer, candidate, file)
if err != nil {
return nil, errors.WithStack(err)
}
} else {
consumer.Infof(" No results from configurator")
}
if typePerConfigurator == InstallerTypeUnknown || typePerConfigurator == InstallerTypeNaked || typePerConfigurator == InstallerTypeArchive {
// some archive types are better sniffed by 7-zip and/or butler's own
// decompression engines, so if configurator returns naked, we try
// to open as an archive.
beforeArchiveProbe := time.Now()
consumer.Infof(" Probing as archive...")
// seek to start first because configurator may have seeked itself
_, err = file.Seek(0, io.SeekStart)
if err != nil {
return nil, errors.WithStack(err)
}
var entries []*savior.Entry
archiveInfo, err := boar.Probe(&boar.ProbeParams{
File: file,
Consumer: consumer,
Candidate: candidate,
OnEntries: func(es []*savior.Entry) {
entries = es
},
})
consumer.Debugf(" (took %s)", time.Since(beforeArchiveProbe))
if err != nil {
return nil, errors.WithStack(err)
}
if archiveInfo == nil {
consumer.Infof("β Source is not a supported archive format")
} else {
consumer.Infof("β Source is a supported archive format (%s)", archiveInfo.Format)
return &InstallerInfo{
Type: InstallerTypeArchive,
ArchiveInfo: archiveInfo,
Entries: entries,
}, nil
}
_, err = file.Seek(0, io.SeekStart)
if err != nil {
return nil, errors.WithStack(err)
}
}
consumer.Infof("β Using configurator results")
return &InstallerInfo{
Type: typePerConfigurator,
}, nil
}
func getInstallerTypeForCandidate(consumer *state.Consumer, candidate *dash.Candidate, file eos.File) (InstallerType, error) {
switch candidate.Flavor {
case dash.FlavorNativeWindows:
if candidate.WindowsInfo != nil && candidate.WindowsInfo.InstallerType != "" {
typ := (InstallerType)(candidate.WindowsInfo.InstallerType)
consumer.Infof(" β Windows installer of type %s", typ)
return typ, nil
}
_, err := file.Seek(0, io.SeekStart)
if err != nil {
return InstallerTypeUnknown, errors.WithStack(err)
}
peInfo, err := pelican.Probe(file, &pelican.ProbeParams{
Consumer: consumer,
})
if err != nil {
return InstallerTypeUnknown, errors.WithStack(err)
}
if peInfo.AssemblyInfo != nil {
switch peInfo.AssemblyInfo.RequestedExecutionLevel {
case "highestAvailable", "requireAdministrator":
consumer.Infof(" β Unsupported Windows installer (requested execution level %s)", peInfo.AssemblyInfo.RequestedExecutionLevel)
return InstallerTypeUnsupported, nil
}
if peInfo.AssemblyInfo.Description == "IExpress extraction tool" {
consumer.Infof(" β Self-extracting CAB created with IExpress")
return InstallerTypeIExpress, nil
}
} else {
stats, err := file.Stat()
if err != nil {
return InstallerTypeUnknown, errors.WithStack(err)
}
if HasSuspiciouslySetupLikeName(stats.Name()) {
consumer.Infof(" β Unsupported Windows installer (no manifest, has name '%s')", stats.Name())
return InstallerTypeUnsupported, nil
}
}
consumer.Infof(" β Native windows executable, but not an installer")
return InstallerTypeNaked, nil
case dash.FlavorNativeMacos:
consumer.Infof(" β Native macOS executable")
return InstallerTypeNaked, nil
case dash.FlavorNativeLinux:
consumer.Infof(" β Native linux executable")
return InstallerTypeNaked, nil
case dash.FlavorScript:
consumer.Infof(" β Script")
if candidate.ScriptInfo != nil && candidate.ScriptInfo.Interpreter != "" {
consumer.Infof(" with interpreter %s", candidate.ScriptInfo.Interpreter)
}
return InstallerTypeNaked, nil
case dash.FlavorScriptWindows:
consumer.Infof(" β Windows script")
return InstallerTypeNaked, nil
}
return InstallerTypeUnknown, nil
}
func IsWindowsInstaller(typ InstallerType) bool {
switch typ {
case InstallerTypeMSI:
return true
case InstallerTypeNsis:
return true
case InstallerTypeInno:
return true
default:
return false
}
}