-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathdmgextract.go
248 lines (209 loc) · 6.13 KB
/
dmgextract.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
package dmgextract
import (
"io/ioutil"
"os"
"strings"
"github.com/itchio/butler/filtering"
"github.com/itchio/damage"
"github.com/itchio/damage/hdiutil"
"github.com/itchio/wharf/pools/fspool"
"github.com/itchio/wharf/pwr"
"github.com/itchio/wharf/state"
"github.com/itchio/wharf/tlc"
"github.com/pkg/errors"
)
// An Extractor can list the contents of a DMG and extract it to a folder
type Extractor interface {
List() (*ExtractorResult, error)
ExtractTo(dest string) (*ExtractorResult, error)
}
type extractor struct {
opts extractorOpts
}
type extractorOpts struct {
dmgpath string
mountFolder string
consumer *state.Consumer
extractSLA bool
startProgressWithTotalBytes func(totalBytes int64)
endProgress func()
}
// An ExtractorOpt changes the behavior of a dmg extractor.
type ExtractorOpt func(opts *extractorOpts)
// ExtractorResult contains information aobut
type ExtractorResult struct {
Container *tlc.Container
SLA *damage.SLA
}
// WithConsumer hooks up logging and progress to a custom consumer. Otherwise,
// a silent consumer will be used.
func WithConsumer(consumer *state.Consumer) ExtractorOpt {
return func(opts *extractorOpts) {
opts.consumer = consumer
}
}
// WithMountFolder specifies a mount folder for the disk image.
// If none is specified, a temporary path will be generated.
func WithMountFolder(mountFolder string) ExtractorOpt {
return func(opts *extractorOpts) {
opts.mountFolder = mountFolder
}
}
func WithProgressTotalBytes(startProgressWithTotalBytes func(totalBytes int64), endProgress func()) ExtractorOpt {
return func(opts *extractorOpts) {
opts.startProgressWithTotalBytes = startProgressWithTotalBytes
opts.endProgress = endProgress
}
}
// ExtractSLA instructs the dmg extractor to parse any service
// level agreement stored in the DMG file.
var ExtractSLA ExtractorOpt = func(opts *extractorOpts) {
opts.extractSLA = true
}
// New creates a new extractor with the specified options.
// It doesn't do anything until one of its methods are called.
func New(dmgpath string, o ...ExtractorOpt) Extractor {
opts := extractorOpts{
dmgpath: dmgpath,
}
for _, applyOpt := range o {
applyOpt(&opts)
}
return &extractor{opts: opts}
}
func (e *extractor) List() (*ExtractorResult, error) {
res, err := e.do("")
if err != nil {
return nil, errors.WithStack(err)
}
return res, nil
}
func (e *extractor) ExtractTo(dest string) (*ExtractorResult, error) {
return e.do(dest)
}
type unmountMode int
const (
unmountModeSilent unmountMode = 0
unmountModeNoisy unmountMode = 1
)
func (e *extractor) do(dest string) (*ExtractorResult, error) {
res := &ExtractorResult{}
opts := e.opts
consumer := opts.consumer
if consumer == nil {
consumer = &state.Consumer{}
}
host := hdiutil.NewHost(consumer)
consumer.Opf("Analyzing DMG file...")
info, err := damage.GetDiskInfo(host, opts.dmgpath)
if err != nil {
return nil, errors.WithStack(err)
}
consumer.Statf("%s", info)
if info.Properties.SoftwareLicenseAgreement {
consumer.Infof("Found SLA (Software License Agreement)")
if opts.extractSLA {
consumer.Opf("Attempting to retrieve SLA text...")
fetchSLA := func() error {
rez, err := damage.GetUDIFResources(host, opts.dmgpath)
if err != nil {
return errors.WithStack(err)
}
sla, err := damage.GetDefaultSLA(rez)
if err != nil {
return errors.WithStack(err)
}
if sla != nil {
text := sla.Text
consumer.Statf("Fetched SLA text (%d bytes)", len(text))
res.SLA = sla
} else {
consumer.Infof("No SLA text found")
}
return nil
}
err = fetchSLA()
if err != nil {
consumer.Warnf("While fetching SLA: %+v", err)
consumer.Infof("Continuing anyway...")
}
}
}
mountFolder := opts.mountFolder
if mountFolder == "" {
mountFolder, err = ioutil.TempDir("", "dmg-mountpoint")
if err != nil {
return nil, errors.WithStack(err)
}
}
consumer.Opf("Mounting...")
unmount := func(mode unmountMode) {
// if this gets called we're in cleanup mode
err := damage.Unmount(host, mountFolder)
if err != nil && mode == unmountModeNoisy {
consumer.Warnf("While unmounting: %+v", err)
}
}
// Q: why defer unmount before we mount?
// well, damage.Mount is a multi-operation call. First it actually
// calls hdiutil mount, then it parses its result. Maybe it mounted
// successfully but failed to parse the result. In any case, we don't
// want to be leaving a mounted volume behind us.
defer unmount(unmountModeSilent)
_, err = damage.Mount(host, opts.dmgpath, mountFolder)
if err != nil {
return nil, errors.WithStack(err)
}
consumer.Statf("Mounted!")
// should return "false" if file should be excluded from walk
dmgFilterPaths := func(fileInfo os.FileInfo) bool {
if !filtering.FilterPaths(fileInfo) {
return false
}
name := fileInfo.Name()
if strings.Contains(name, "Applications") && fileInfo.Mode()&os.ModeSymlink > 0 {
// DMG files tend to have a symlink to /Applications in there, we don't
// want to install that, ohh, no we don't.
return false
}
return true
}
consumer.Opf("Walking mounted volume...")
container, err := tlc.WalkDir(mountFolder, &tlc.WalkOpts{
Dereference: false,
Filter: dmgFilterPaths,
})
if err != nil {
return nil, errors.WithStack(err)
}
consumer.Statf("Found %s", container.Stats())
if dest == "" {
consumer.Infof("Done! (should not extract)")
} else {
consumer.Opf("Preparing dirs and symlinks in (%s)...", dest)
err = container.Prepare(dest)
if err != nil {
return nil, errors.WithStack(err)
}
consumer.Infof("Copying to (%s)", dest)
copy := func() error {
inPool := fspool.New(container, mountFolder)
defer inPool.Close()
outPool := fspool.New(container, dest)
return pwr.CopyContainer(container, outPool, inPool, consumer)
}
if opts.startProgressWithTotalBytes != nil {
opts.startProgressWithTotalBytes(container.Size)
}
err = copy()
if opts.endProgress != nil {
opts.endProgress()
}
if err != nil {
return nil, errors.WithStack(err)
}
}
unmount(unmountModeNoisy)
res.Container = container
return res, nil
}