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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
use crate::{download::PayloadContents, Ctx, Error, Path, PathBuf};
use anyhow::Context as _;
#[derive(serde::Serialize, serde::Deserialize)]
pub(crate) struct UnpackMeta {
#[serde(serialize_with = "crate::util::serialize_sha256")]
pub(crate) sha256: crate::util::Sha256,
pub(crate) compressed: u64,
pub(crate) decompressed: u64,
pub(crate) num_files: u32,
}
#[derive(Debug)]
pub(crate) struct FileTree {
pub(crate) files: Vec<(PathBuf, u64)>,
pub(crate) dirs: Vec<(PathBuf, FileTree)>,
}
impl FileTree {
fn new() -> Self {
Self {
files: Vec::new(),
dirs: Vec::new(),
}
}
fn push(&mut self, path: &Path, size: u64) {
let fname = path.file_name().unwrap();
let mut tree = self;
for comp in path.iter() {
if comp != fname {
#[allow(clippy::single_match_else)]
match tree.dirs.iter().position(|(dir, _tree)| dir == comp) {
Some(t) => tree = &mut tree.dirs[t].1,
None => {
tree.dirs.push((comp.into(), FileTree::new()));
tree = &mut tree.dirs.last_mut().unwrap().1;
}
}
} else {
tree.files.push((fname.into(), size));
}
}
}
pub(crate) fn stats(&self) -> (u32, u64) {
self.dirs.iter().fold(
(
self.files.len() as u32,
self.files.iter().map(|(_, size)| *size).sum(),
),
|(num_files, size), tree| {
let stats = tree.1.stats();
(num_files + stats.0, size + stats.1)
},
)
}
pub(crate) fn subtree(&self, path: &Path) -> Option<&FileTree> {
let mut tree = self;
for comp in path.iter() {
match tree.dirs.iter().find(|dir| dir.0 == comp) {
Some(t) => tree = &t.1,
None => return None,
}
}
Some(tree)
}
}
fn read_unpack_dir(root: PathBuf) -> Result<FileTree, Error> {
let mut root_tree = FileTree::new();
fn read(src: PathBuf, tree: &mut FileTree) -> Result<(), Error> {
for entry in std::fs::read_dir(&src).with_context(|| format!("unable to read {src}"))? {
let entry = entry.with_context(|| format!("unable to read entry from {src}"))?;
let src_name = PathBuf::from_path_buf(entry.file_name().into()).map_err(|_pb| {
anyhow::anyhow!(
"src path {} is not a valid utf-8 path",
entry.path().display()
)
})?;
if src_name == ".unpack" {
continue;
}
let metadata = entry.metadata().with_context(|| {
format!("unable to get metadata for {}", entry.path().display())
})?;
let ft = metadata.file_type();
if ft.is_dir() {
let mut dir_tree = FileTree::new();
read(src.join(&src_name), &mut dir_tree)?;
tree.dirs.push((src_name, dir_tree));
} else if ft.is_file() {
tree.files.push((src_name, metadata.len()));
} else if ft.is_symlink() {
anyhow::bail!(
"detected symlink {} in source directory which should be impossible",
entry.path().display()
);
}
}
Ok(())
}
read(root, &mut root_tree)?;
Ok(root_tree)
}
pub(crate) fn unpack(
ctx: std::sync::Arc<Ctx>,
item: &crate::WorkItem,
contents: PayloadContents,
) -> Result<FileTree, Error> {
item.progress.reset();
item.progress.set_message("📂 unpacking...");
let output_dir = match ctx.prep_unpack(&item.payload)? {
crate::ctx::Unpack::Present { output_dir, .. } => {
return read_unpack_dir(output_dir);
}
crate::ctx::Unpack::Needed(od) => od,
};
let pkg = &item.payload.filename;
let (tree, compressed) = match contents {
PayloadContents::Vsix(vsix) => {
let mut tree = FileTree::new();
let mut zip = zip::ZipArchive::new(std::io::Cursor::new(vsix))
.with_context(|| format!("invalid zip {pkg}"))?;
// VSIX files are just a "specially" formatted zip file, all
// of the actual files we want are under "Contents"
let mut to_extract = Vec::new();
let mut total_uncompressed = 0;
for findex in 0..zip.len() {
let file = zip.by_index_raw(findex)?;
let fname = file.name();
if fname.starts_with("Contents/")
&& (fname.contains("lib") || fname.contains("include"))
{
to_extract.push(findex);
total_uncompressed += file.size();
}
}
item.progress.set_length(total_uncompressed);
let mut total_compressed = 0;
for findex in to_extract {
let mut file = zip.by_index(findex).unwrap();
let zip_path = Path::new(file.name());
let mut fs_path = output_dir.clone();
for comp in zip_path
.components()
.skip_while(|comp| comp.as_str() != "lib" && comp.as_str() != "include")
{
fs_path.push(comp);
}
if let Some(parent) = fs_path.parent() {
if !parent.exists() {
std::fs::create_dir_all(parent)
.with_context(|| format!("unable to create unpack dir '{parent}'"))?;
}
}
let mut dest = std::fs::File::create(&fs_path).with_context(|| {
format!(
"unable to create {fs_path} to decompress {} from {pkg}",
file.name(),
)
})?;
let decompressed = std::io::copy(&mut file, &mut dest).with_context(|| {
format!(
"unable to decompress {} from {pkg} to {fs_path}",
file.name(),
)
})?;
item.progress.inc(decompressed);
let tree_path = fs_path.strip_prefix(&output_dir).unwrap();
tree.push(tree_path, decompressed);
total_compressed += file.compressed_size();
}
(tree, total_compressed)
}
PayloadContents::Msi { msi, cabs } => {
let mut msi = msi::Package::open(std::io::Cursor::new(msi))
.with_context(|| format!("unable to read MSI from {pkg}"))?;
// Open source ftw https://gitlab.gnome.org/GNOME/msitools/-/blob/master/tools/msiextract.vala
// For some reason many filenames in the table(s) have a weird
// checksum(?) filename with an extension separated from the
// _actual_ filename with a `|` so we need to detect that and
// strip off just the real name we want
#[inline]
fn fix_name(name: &msi::Value) -> Result<&str, Error> {
let name = name.as_str().context("filename is not a string")?;
Ok(match name.find('|') {
Some(ind) => &name[ind + 1..],
None => name,
})
}
let components = {
#[derive(Debug)]
struct Dir {
id: String,
parent: Option<String>,
path: PathBuf,
}
// Collect the directories that can be referenced by a component
// that are reference by files. Ugh.
let mut directories: Vec<_> = msi
.select_rows(msi::Select::table("Directory"))
.with_context(|| format!("MSI {} has no 'Directory' table", pkg))?
.map(|row| -> Result<_, _> {
// Columns:
// 0 - Directory (name)
// 1 - Directory_Parent (name of parent)
// 2 - DefaultDir (location of directory on disk)
// ...
anyhow::ensure!(row.len() >= 3, "invalid row in 'Directory'");
Ok(Dir {
id: row[0]
.as_str()
.context("directory name is not a string")?
.to_owned(),
// This can be `null`
parent: row[1].as_str().map(String::from),
path: fix_name(&row[2])?.into(),
})
})
.collect::<Result<_, _>>()
.with_context(|| format!("unable to read directories for {pkg}"))?;
directories.sort_by(|a, b| a.id.cmp(&b.id));
let components: std::collections::BTreeMap<_, _> = msi
.select_rows(msi::Select::table("Component"))
.with_context(|| format!("MSI {pkg} has no 'Directory' table"))?
.map(|row| -> Result<_, _> {
// Columns:
// 0 - Component (name, really, id)
// 1 - ComponentId
// 2 - Directory_ (directory id)
anyhow::ensure!(row.len() >= 3, "invalid row in 'Component'");
// The recursion depth for directory lookup is quite shallow
// typically, the full path to a file would be something like
// `Program Files/Windows Kits/10/Lib/10.0.19041.0/um/x64`
// but this a terrible path, so we massage it to instead be
// `lib/um/x64`
fn build_dir(dirs: &[Dir], id: &str, dir: &mut PathBuf) {
#[allow(clippy::single_match_else)]
let cur_dir = match dirs.binary_search_by(|d| d.id.as_str().cmp(id)) {
Ok(i) => &dirs[i],
Err(_) => {
tracing::warn!("unable to find directory {id}");
return;
}
};
match cur_dir.path.file_name() {
Some("Lib") => {
dir.push("lib");
}
Some("Include") => {
dir.push("include");
}
other => {
if let Some(parent) = &cur_dir.parent {
build_dir(dirs, parent, dir);
}
if let Some(other) = other {
// Ignore the SDK version directory between
// Lib/Include and the actual subdirs we care about
if !other.starts_with(|c: char| c.is_ascii_digit()) {
dir.push(other);
}
}
}
}
}
let component_id = row[0]
.as_str()
.context("component id is not a string")?
.to_owned();
let mut dir = PathBuf::new();
build_dir(
&directories,
row[2]
.as_str()
.context("component directory is not a string")?,
&mut dir,
);
Ok((component_id, dir))
})
.collect::<Result<_, _>>()
.with_context(|| format!("unable to read components for {pkg}"))?;
components
};
struct Cab {
/// The max sequence number, each `File` in an MSI has a
/// sequence number that maps to exactly one CAB file
sequence: u32,
path: PathBuf,
cab: bytes::Bytes,
}
let cabs = {
let mut cab_contents = Vec::with_capacity(cabs.len());
for cab in cabs {
// Validate the cab file
cab::Cabinet::new(std::io::Cursor::new(cab.content.clone()))
.with_context(|| format!("CAB {} is invalid", cab.path))?;
cab_contents.push(Cab {
sequence: cab.sequence,
path: cab.path,
cab: cab.content,
});
}
// They are usually always sorted correctly, but you never know
cab_contents.sort_by(|a, b| a.sequence.cmp(&b.sequence));
cab_contents
};
anyhow::ensure!(!cabs.is_empty(), "no cab files were referenced by the MSI");
struct CabFile {
id: String,
name: PathBuf,
size: u64,
sequence: u32,
}
let (files, uncompressed) = {
let mut uncompressed = 0u64;
let mut files: Vec<_> = msi
.select_rows(msi::Select::table("File"))
.with_context(|| format!("MSI {} has no 'File' table", pkg))?
.filter_map(|row| -> Option<Result<_, Error>> {
// Columns:
// 0 - File Id (lookup in CAB)
// 1 - Component_ (target directory)
// 2 - FileName
// 3 - FileSize
// 4 - Version
// 5 - Language
// 6 - Attributes
// 7 - Sequence (determines which CAB file)
if row.len() < 8 {
return Some(Err(anyhow::anyhow!("invalid row in 'File'")));
}
let (dir, fname, id, seq, size) = match || -> Result<_, Error> {
let fname = fix_name(&row[2])?;
let dir = components
.get(row[1].as_str().context("component id was not a string")?)
.with_context(|| {
format!("file {} referenced an unknown component", row[2])
})?;
let size = row[3].as_int().context("size is not an integer")? as u64;
let id = row[0].as_str().context("File (id) is not a string")?;
let seq = row[7].as_int().context("sequence is not an integer")? as u32;
Ok((dir, fname, id, seq, size))
}() {
Ok(items) => items,
Err(e) => return Err(e).transpose(),
};
if let Some(camino::Utf8Component::Normal(first)) = dir
.strip_prefix(&output_dir)
.ok()
.and_then(|rel| rel.components().next())
{
match first {
"Catalogs" | "bin" | "Source" | "SourceDir" => {
//tracing::debug!("ignoring {}/{}", dir, fname);
return None;
}
_ => {}
}
}
uncompressed += size;
let cf = CabFile {
id: id.to_owned(),
name: dir.join(fname),
sequence: seq,
size,
};
Some(Ok(cf))
})
.collect::<Result<Vec<_>, Error>>()
.with_context(|| format!("unable to read 'File' metadata for {pkg}"))?;
files.sort_by(|a, b| a.sequence.cmp(&b.sequence));
(files, uncompressed)
};
item.progress.set_length(uncompressed);
// Some MSIs have a lot of cabs and take an _extremely_ long time to
// decompress, so we just split the files into roughly equal sized
// chunks and decompress in parallel to reduce wall time
let mut chunks = Vec::new();
struct Chunk {
cab: bytes::Bytes,
cab_index: usize,
files: Vec<CabFile>,
chunk_size: u64,
}
chunks.push(Chunk {
cab: cabs[0].cab.clone(),
cab_index: 0,
files: Vec::new(),
chunk_size: 0,
});
let mut cur_chunk = 0;
let mut cur_cab = 0;
const CHUNK_SIZE: u64 = 1024 * 1024;
for file in files {
let chunk = &mut chunks[cur_chunk];
if chunk.chunk_size + file.size < CHUNK_SIZE
&& file.sequence <= cabs[cur_cab].sequence
{
chunk.chunk_size += file.size;
chunk.files.push(file);
} else {
let cab = if file.sequence <= cabs[cur_cab].sequence {
chunk.cab.clone()
} else {
match cabs[cur_cab + 1..]
.iter()
.position(|cab| file.sequence <= cab.sequence)
{
Some(i) => cur_cab += i + 1,
None => anyhow::bail!(
"unable to find cab file containing {} {}",
file.name,
file.sequence
),
}
cabs[cur_cab].cab.clone()
};
cur_chunk += 1;
chunks.push(Chunk {
cab,
cab_index: cur_cab,
chunk_size: file.size,
files: vec![file],
});
}
}
let mut results = Vec::new();
use rayon::prelude::*;
let tree = parking_lot::Mutex::new(FileTree::new());
chunks
.into_par_iter()
.map(|chunk| -> Result<(), Error> {
let mut cab = cab::Cabinet::new(std::io::Cursor::new(chunk.cab)).unwrap();
let cab_path = &cabs[chunk.cab_index].path;
for file in chunk.files {
let mut cab_file = match cab.read_file(file.id.as_str()) {
Ok(cf) => cf,
Err(e) => Err(e).with_context(|| {
format!("unable to read '{}' from {cab_path}", file.name)
})?,
};
let unpack_path = output_dir.join(&file.name);
if let Some(parent) = unpack_path.parent() {
if !parent.exists() {
std::fs::create_dir_all(parent)?;
}
}
let unpacked_file = std::fs::File::create(&unpack_path)?;
struct Wrapper<'pb> {
pb: &'pb indicatif::ProgressBar,
uf: std::fs::File,
}
impl<'pb> std::io::Write for Wrapper<'pb> {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.pb.inc(buf.len() as u64);
self.uf.write(buf)
}
fn flush(&mut self) -> std::io::Result<()> {
self.uf.flush()
}
}
let size = std::io::copy(
&mut cab_file,
&mut Wrapper {
pb: &item.progress,
uf: unpacked_file,
},
)?;
tree.lock().push(&file.name, size);
}
Ok(())
})
.collect_into_vec(&mut results);
(tree.into_inner(), uncompressed)
}
};
let tree_path = format!("{output_dir}/tree.txt");
std::fs::write(&tree_path, format!("{tree:#?}").as_bytes())
.with_context(|| format!("failed to write {tree_path}"))?;
item.progress.finish_with_message("unpacked");
let (num_files, decompressed) = tree.stats();
ctx.finish_unpack(
output_dir,
UnpackMeta {
sha256: item.payload.sha256.clone(),
compressed,
decompressed,
num_files,
},
)?;
Ok(tree)
}