wim 0.1.3

Windows Imaging (WIM) archive reader and writer
Documentation
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
//! Unix capture scanner with explicit, unhashed stream ownership.
use std::{
    collections::{HashMap, HashSet},
    ffi::CString,
    os::unix::{ffi::OsStrExt, fs::MetadataExt},
    path::{Path, PathBuf},
    sync::Arc,
};
use wim_format::{ParseError, metadata_write::OwnedDentry};

use super::{CaptureBinding, CaptureIdentity, CapturePlan, CapturedSource, CapturedStream};
use sha1::{Digest, Sha1};

use super::common::{CaptureConfig, ScanCallback, ScanEvent};

fn timestamp(seconds: i64, nanos: i64) -> u64 {
    ((i128::from(seconds) + 11_644_473_600) * 10_000_000 + i128::from(nanos) / 100)
        .clamp(0, i128::from(u64::MAX)) as u64
}
fn tag(node: &mut OwnedDentry, id: u32, bytes: &[u8]) -> Result<(), ParseError> {
    let size = u32::try_from(bytes.len()).map_err(|_| ParseError::Nomem)?;
    node.tagged_items
        .try_reserve(bytes.len() + 15)
        .map_err(|_| ParseError::Nomem)?;
    node.tagged_items.extend_from_slice(&id.to_le_bytes());
    node.tagged_items.extend_from_slice(&size.to_le_bytes());
    node.tagged_items.extend_from_slice(bytes);
    node.tagged_items
        .resize((node.tagged_items.len() + 7) & !7, 0);
    Ok(())
}
fn name(bytes: &[u8]) -> Result<Vec<u8>, ParseError> {
    Ok(wim_format::platform_text::wtf8_to_utf16(bytes)
        .inspect_err(|_| {
            #[cfg(target_os = "linux")]
            // SAFETY: errno is calling-thread storage.
            unsafe {
                *libc::__errno_location() = libc::EILSEQ;
            }
        })?
        .into_iter()
        .flat_map(u16::to_le_bytes)
        .collect())
}
fn symlink_data(target: &[u8]) -> Result<Vec<u8>, ParseError> {
    let mut units = wim_format::platform_text::wtf8_to_utf16(target)?;
    for unit in &mut units {
        *unit = match *unit {
            47 => 92,
            92 => 47,
            value => value,
        };
    }
    let absolute = units.first().is_none_or(|&u| u == 92);
    let mut substitute = if absolute {
        vec![92, 63, 63, 92, 67, 58]
    } else {
        Vec::new()
    };
    substitute.extend_from_slice(&units);
    let print = if absolute {
        &substitute[4..]
    } else {
        &substitute[..]
    };
    let sub_len = u16::try_from(substitute.len() * 2).map_err(|_| ParseError::Readlink)?;
    let print_len = u16::try_from(print.len() * 2).map_err(|_| ParseError::Readlink)?;
    let mut bytes = Vec::new();
    for value in [0, sub_len, sub_len + 2, print_len] {
        bytes.extend_from_slice(&value.to_le_bytes());
    }
    bytes.extend_from_slice(&(u32::from(!absolute)).to_le_bytes());
    bytes.extend(substitute.iter().flat_map(|u| u.to_le_bytes()));
    bytes.extend_from_slice(&[0, 0]);
    bytes.extend(print.iter().flat_map(|u| u.to_le_bytes()));
    bytes.extend_from_slice(&[0, 0]);
    if bytes.len() + 8 > 16_384 {
        return Err(ParseError::Readlink);
    }
    Ok(bytes)
}
fn relativize(target: &[u8], root: CaptureIdentity) -> &[u8] {
    let mut pos = 0;
    while pos < target.len() {
        while target.get(pos) == Some(&b'/') {
            pos += 1;
        }
        if pos == target.len() {
            break;
        }
        while pos < target.len() && target[pos] != b'/' {
            pos += 1;
        }
        let path = Path::new(std::ffi::OsStr::from_bytes(&target[..pos]));
        let Ok(stat) = std::fs::metadata(path) else {
            break;
        };
        if stat.dev() == root.device && stat.ino() == root.inode {
            return &target[pos..];
        }
    }
    target
}

#[cfg(target_os = "linux")]
fn xattrs(path: &Path, node: &mut OwnedDentry) -> Result<(), ParseError> {
    let path = CString::new(path.as_os_str().as_bytes()).map_err(|_| ParseError::InvalidParam)?;
    // SAFETY: The terminated path and caller-owned buffers remain live.
    let length = unsafe { libc::llistxattr(path.as_ptr(), std::ptr::null_mut(), 0) };
    if length < 0 {
        let code = std::io::Error::last_os_error().raw_os_error();
        return if matches!(code, Some(libc::ENOTSUP | libc::ENOSYS)) {
            Ok(())
        } else {
            Err(ParseError::Stat)
        };
    }
    if length == 0 {
        return Ok(());
    }
    let mut names = vec![0u8; length as usize];
    // SAFETY: names spans the size queried above; races produce an error.
    let length = unsafe { libc::llistxattr(path.as_ptr(), names.as_mut_ptr().cast(), names.len()) };
    if length < 0 {
        return Err(ParseError::Stat);
    }
    names.truncate(length as usize);
    let mut entries = Vec::new();
    for bytes in names.split(|&b| b == 0).filter(|b| !b.is_empty()) {
        let attr = CString::new(bytes).map_err(|_| ParseError::Stat)?;
        // SAFETY: Both strings are terminated and readable.
        let length =
            unsafe { libc::lgetxattr(path.as_ptr(), attr.as_ptr(), std::ptr::null_mut(), 0) };
        if length < 0 {
            return Err(ParseError::Stat);
        }
        if length > 65535 || bytes.len() > 255 {
            continue;
        }
        let mut value = vec![0u8; length as usize];
        // SAFETY: value spans the queried size; concurrent changes fail safely.
        let length = unsafe {
            libc::lgetxattr(
                path.as_ptr(),
                attr.as_ptr(),
                value.as_mut_ptr().cast(),
                value.len(),
            )
        };
        if length < 0 {
            return Err(ParseError::Stat);
        }
        value.truncate(length as usize);
        entries.extend_from_slice(&(length as u16).to_le_bytes());
        entries.extend_from_slice(&[bytes.len() as u8, 0]);
        entries.extend_from_slice(bytes);
        entries.push(0);
        entries.extend(value);
    }
    tag(node, 2, &entries)
}
#[cfg(not(target_os = "linux"))]
fn xattrs(_path: &Path, _node: &mut OwnedDentry) -> Result<(), ParseError> {
    Ok(())
}

struct PriorInode {
    node: OwnedDentry,
    streams: Vec<(usize, Arc<CapturedStream>)>,
}
struct Scanner<'a> {
    session: u64,
    prior: HashMap<(u64, u64), PriorInode>,
    source: &'a Path,
    flags: i32,
    config: &'a CaptureConfig,
    progress: &'a mut ScanCallback<'a>,
    plan: CapturePlan,
    inodes: HashMap<(u64, u64), usize>,
    canonical: HashSet<usize>,
    root_identity: Option<CaptureIdentity>,
    counts: (u64, u64, u64),
    last_path: Option<PathBuf>,
    last_status: i32,
}
impl Scanner<'_> {
    fn event(
        &mut self,
        message: i32,
        path: Option<&Path>,
        status: i32,
        target: Option<&[u8]>,
    ) -> Result<bool, ParseError> {
        let mut event = ScanEvent {
            message,
            source: self.source,
            current_path: path,
            status,
            symlink_target: target,
            directories: self.counts.0,
            nondirectories: self.counts.1,
            bytes: self.counts.2,
            exclude: false,
            error: None,
        };
        (self.progress)(&mut event)?;
        Ok(event.exclude)
    }
    fn report(
        &mut self,
        path: &Path,
        status: i32,
        index: Option<usize>,
        target: Option<&[u8]>,
    ) -> Result<(), ParseError> {
        if (status == 0 && self.flags & 4 == 0) || (status != 0 && self.flags & 0x80 == 0) {
            return Ok(());
        }
        if let Some(index) = index {
            if self.plan.tree.nodes[index].attributes & (0x10 | 0x400) == 0x10 {
                self.counts.0 += 1;
            } else {
                self.counts.1 += 1;
            }
            if self.canonical.contains(&index) {
                self.counts.2 += self
                    .plan
                    .bindings
                    .iter()
                    .filter(|b| b.node == index)
                    .map(|b| b.stream.size)
                    .sum::<u64>();
            }
        }
        self.last_path = Some(path.to_owned());
        self.last_status = status;
        self.event(10, Some(path), status, target).map(|_| ())
    }
    fn visit(
        &mut self,
        path: &Path,
        root: bool,
        image_root: bool,
    ) -> Result<Option<usize>, ParseError> {
        let before = self.plan.tree.nodes.len();
        let result = self.visit_inner(path, root, image_root);
        match result {
            Err(error)
                if !matches!(
                    error,
                    ParseError::AbortedByProgress | ParseError::UnknownProgressStatus
                ) =>
            {
                self.plan.tree.nodes.truncate(before);
                self.plan.identities.truncate(before);
                self.plan.bindings.retain(|binding| binding.node < before);
                self.inodes.retain(|_, node| *node < before);
                self.canonical.retain(|&node| node < before);
                let mut event = ScanEvent {
                    message: 31,
                    source: self.source,
                    current_path: Some(path),
                    status: self.last_status,
                    symlink_target: None,
                    directories: self.counts.0,
                    nondirectories: self.counts.1,
                    bytes: self.counts.2,
                    exclude: false,
                    error: Some(error),
                };
                (self.progress)(&mut event)?;
                if event.exclude { Ok(None) } else { Err(error) }
            }
            other => other,
        }
    }
    fn visit_inner(
        &mut self,
        path: &Path,
        root: bool,
        image_root: bool,
    ) -> Result<Option<usize>, ParseError> {
        if self.config.excluded(
            path.strip_prefix(self.source)
                .unwrap_or(path)
                .as_os_str()
                .as_bytes(),
        ) || (self.flags & 0x4000 != 0 && self.event(30, Some(path), 0, None)?)
        {
            self.report(path, 1, None, None)?;
            return Ok(None);
        }
        let metadata = if self.flags & 2 != 0 || (root && image_root) {
            std::fs::metadata(path)
        } else {
            std::fs::symlink_metadata(path)
        }
        .map_err(|error| {
            #[cfg(target_os = "linux")]
            // SAFETY: Preserve the actual failed filesystem operation's errno.
            unsafe {
                *libc::__errno_location() = error.raw_os_error().unwrap_or(libc::EIO);
            }
            ParseError::Stat
        })?;
        let mode = metadata.mode() & libc::S_IFMT;
        if self.flags & 0x10 == 0 && !matches!(mode, libc::S_IFREG | libc::S_IFDIR | libc::S_IFLNK)
        {
            if self.flags & 0x400 != 0 {
                return Err(ParseError::UnsupportedFile);
            }
            self.report(path, 2, None, None)?;
            return Ok(None);
        }
        let identity = CaptureIdentity {
            session: self.session,
            device: metadata.dev(),
            inode: metadata.ino(),
        };
        if root && image_root {
            self.root_identity = Some(identity);
        }
        let node_name = if root {
            Vec::new()
        } else {
            name(path.file_name().ok_or(ParseError::InvalidParam)?.as_bytes())?
        };
        let index = self.plan.tree.nodes.len();
        if mode != libc::S_IFDIR
            && let Some(&first) = self.inodes.get(&(identity.device, identity.inode))
        {
            let mut node = self.plan.tree.nodes[first].clone();
            node.name = node_name;
            if node.attributes & 0x400 == 0 {
                let id = first as u64 + 1;
                node.inode_union = id;
                self.plan.tree.nodes[first].inode_union = id;
            }
            self.plan.tree.nodes.push(node);
            self.plan.identities.push(Some(identity));
            let aliases: Vec<_> = self
                .plan
                .bindings
                .iter()
                .filter(|b| b.node == first)
                .map(|b| CaptureBinding {
                    node: index,
                    slot: b.slot,
                    stream: b.stream.clone(),
                })
                .collect();
            self.plan.bindings.extend(aliases);
            self.report(path, 0, Some(index), None)?;
            return Ok(Some(index));
        }
        if mode != libc::S_IFDIR
            && let Some(previous) = self.prior.get(&(identity.device, identity.inode))
        {
            let mut node = previous.node.clone();
            node.name = node_name;
            self.plan.tree.nodes.push(node);
            self.plan.identities.push(Some(identity));
            self.plan
                .bindings
                .extend(
                    previous
                        .streams
                        .iter()
                        .map(|(slot, stream)| CaptureBinding {
                            node: index,
                            slot: *slot,
                            stream: stream.clone(),
                        }),
                );
            self.inodes.insert((identity.device, identity.inode), index);
            self.report(path, 0, Some(index), None)?;
            return Ok(Some(index));
        }
        let mut node = OwnedDentry::new(
            node_name,
            match mode {
                libc::S_IFDIR => 0x10,
                libc::S_IFREG if metadata.blocks() < metadata.size().div_ceil(512) => 0x200,
                libc::S_IFREG => 0x80,
                _ => 0,
            },
        );
        node.creation_time = timestamp(metadata.mtime(), metadata.mtime_nsec());
        node.last_write_time = node.creation_time;
        node.last_access_time = timestamp(metadata.atime(), metadata.atime_nsec());
        if self.flags & 0x10 != 0 {
            let data: Vec<_> = [
                metadata.uid(),
                metadata.gid(),
                metadata.mode(),
                metadata.rdev() as u32,
            ]
            .into_iter()
            .flat_map(u32::to_le_bytes)
            .collect();
            tag(&mut node, 0x337dd873, &data)?;
            xattrs(path, &mut node)?;
        }
        self.plan.tree.nodes.push(node);
        self.plan.identities.push(Some(identity));
        self.inodes.insert((identity.device, identity.inode), index);
        self.canonical.insert(index);
        if mode == libc::S_IFREG && metadata.size() != 0 {
            self.plan.bindings.push(CaptureBinding {
                node: index,
                slot: 0,
                stream: Arc::new(CapturedStream {
                    size: metadata.size(),
                    identity,
                    source: CapturedSource::File(path.to_owned()),
                }),
            });
        } else if mode == libc::S_IFDIR {
            let directory = std::fs::read_dir(path).map_err(|_| ParseError::Opendir)?;
            for entry in directory {
                let entry = entry.map_err(|_| ParseError::Read)?;
                if let Some(child) = self.visit(&entry.path(), false, image_root)? {
                    self.plan.tree.nodes[index].children.push(child);
                }
            }
        } else if mode == libc::S_IFLNK {
            let target = std::fs::read_link(path).map_err(|_| ParseError::Readlink)?;
            let original = target.as_os_str().as_bytes();
            let mut rewritten = original;
            let mut rp_flags = 1u64;
            if original.starts_with(b"/") && self.flags & 0x100 != 0 {
                if let Some(root) = self.root_identity {
                    rewritten = relativize(original, root);
                }
                let fixed = rewritten.as_ptr() != original.as_ptr();
                if fixed {
                    rp_flags = 0;
                }
                self.report(path, if fixed { 3 } else { 4 }, None, Some(original))?;
            }
            let data = symlink_data(rewritten)?;
            let node = &mut self.plan.tree.nodes[index];
            node.main_hash = Sha1::digest(&data).into();
            node.attributes = 0x400
                | if std::fs::metadata(path).is_ok_and(|m| m.is_dir()) {
                    0x10
                } else {
                    0
                };
            node.inode_union = 0xa000000c | (rp_flags << 48);
            self.plan.bindings.push(CaptureBinding {
                node: index,
                slot: 0,
                stream: Arc::new(CapturedStream {
                    size: data.len() as u64,
                    identity,
                    source: CapturedSource::Inline(data),
                }),
            });
        }
        self.report(path, 0, Some(index), None)?;
        Ok(Some(index))
    }
}
/// Scan an already validated source command. File content remains deferred.
/// Flags must include facade-normalized verbosity and reparse fixup defaults.
pub fn scan_source<'a>(
    source: &'a Path,
    image_root: bool,
    flags: i32,
    config: &'a CaptureConfig,
    callback: &'a mut ScanCallback<'a>,
) -> Result<CapturePlan, ParseError> {
    scan_source_seeded(
        source,
        image_root,
        flags,
        config,
        &CapturePlan::default(),
        crate::engine::handles::new_identity(),
        callback,
    )
}
/// Scan another command in the same update transaction, reusing inode ownership
/// and unique-byte counts from its earlier scanned branches.
pub fn scan_source_seeded<'a>(
    source: &'a Path,
    image_root: bool,
    flags: i32,
    config: &'a CaptureConfig,
    existing: &CapturePlan,
    session: u64,
    callback: &'a mut ScanCallback<'a>,
) -> Result<CapturePlan, ParseError> {
    let mut prior = HashMap::new();
    for (index, identity) in existing.identities.iter().enumerate() {
        if let Some(identity) = identity
            && identity.session == session
        {
            let node = existing
                .tree
                .nodes
                .get(index)
                .ok_or(ParseError::InvalidMetadataResource)?;
            if node.attributes & (0x10 | 0x400) == 0x10 {
                continue;
            }
            let streams = existing
                .bindings
                .iter()
                .filter(|b| b.node == index)
                .map(|b| (b.slot, b.stream.clone()))
                .collect();
            prior
                .entry((identity.device, identity.inode))
                .or_insert(PriorInode {
                    node: node.clone(),
                    streams,
                });
        }
    }
    let mut scanner = Scanner {
        session,
        prior,
        source,
        flags,
        config,
        progress: callback,
        plan: CapturePlan::default(),
        inodes: HashMap::new(),
        canonical: HashSet::new(),
        root_identity: None,
        counts: (0, 0, 0),
        last_path: None,
        last_status: 0,
    };
    scanner.event(9, None, 0, None)?;
    scanner.visit(source, true, image_root)?;
    let last_path = scanner.last_path.clone();
    scanner.event(11, last_path.as_deref(), scanner.last_status, None)?;
    if image_root
        && scanner
            .plan
            .tree
            .nodes
            .first()
            .is_some_and(|n| n.attributes & 0x10 == 0)
    {
        return Err(ParseError::Notdir);
    }
    Ok(scanner.plan)
}