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
//! Transactional in-memory deletion and renaming of image paths.

use crate::engine::collections::FallibleCollections as _;
use crate::engine::collections::FallibleSet as _;
use crate::engine::{
    TChar,
    handles::{
        HandleImage, OwnedImage, PendingMetadata, WimHandle, image_identity, image_metadata_bytes,
    },
};
use std::ffi::c_int;
use wim_format::{ParseError, metadata::Metadata, metadata_write::OwnedMetadata};

unsafe fn path(pointer: *const TChar) -> Result<Vec<Vec<u16>>, ParseError> {
    if pointer.is_null() {
        return Ok(Vec::new());
    }
    #[cfg(not(windows))]
    // SAFETY: Caller supplies terminated readable platform text.
    let units = wim_format::platform_text::wtf8_to_utf16(
        unsafe { std::ffi::CStr::from_ptr(pointer) }.to_bytes(),
    )?;
    #[cfg(windows)]
    let units = {
        let mut len = 0;
        // SAFETY: Caller supplies terminated readable platform text.
        unsafe {
            while *pointer.add(len) != 0 {
                len += 1;
            }
            std::slice::from_raw_parts(pointer, len).to_vec()
        }
    };
    Ok(units
        .split(|&u| u == 47 || u == 92)
        .filter(|c| !c.is_empty())
        .map(<[u16]>::to_vec)
        .collect())
}
fn errno(value: c_int) {
    #[cfg(target_os = "linux")]
    // SAFETY: libc exposes writable errno storage for this thread.
    unsafe {
        *libc::__errno_location() = value;
    }
    #[cfg(not(target_os = "linux"))]
    let _ = value;
}
fn find(tree: &OwnedMetadata, parts: &[Vec<u16>]) -> Result<usize, ParseError> {
    if tree.nodes.is_empty() {
        errno(libc::ENOENT);
        return Err(ParseError::PathDoesNotExist);
    }
    let mut node = 0;
    for component in parts {
        if tree.nodes[node].attributes & 0x10 == 0 {
            errno(libc::ENOTDIR);
            return Err(ParseError::Notdir);
        }
        let matches = |index: usize, folded: bool| {
            let fold = |u| {
                if folded {
                    wim_format::ntfs_upcase::uppercase(u)
                } else {
                    u
                }
            };
            tree.nodes[index]
                .name
                .chunks_exact(2)
                .map(|b| fold(u16::from_le_bytes([b[0], b[1]])))
                .eq(component.iter().copied().map(fold))
        };
        node = tree.nodes[node]
            .children
            .iter()
            .copied()
            .find(|&i| matches(i, false))
            .or_else(|| {
                crate::engine::runtime::ignore_case()
                    .then(|| {
                        tree.nodes[node]
                            .children
                            .iter()
                            .copied()
                            .find(|&i| matches(i, true))
                    })
                    .flatten()
            })
            .ok_or_else(|| {
                errno(libc::ENOENT);
                ParseError::PathDoesNotExist
            })?;
    }
    Ok(node)
}
fn parent(tree: &OwnedMetadata, node: usize) -> Option<usize> {
    tree.nodes.iter().position(|n| n.children.contains(&node))
}
fn diagnostic_path(parts: &[Vec<u16>]) -> Result<Vec<u8>, ParseError> {
    let mut units = vec![47];
    for (index, part) in parts.iter().enumerate() {
        if index != 0 {
            units.push(47);
        }
        units.extend_from_slice(part);
    }
    wim_format::platform_text::utf16_to_wtf8(&units)
}
fn compact(tree: &mut OwnedMetadata) -> Result<Vec<usize>, ParseError> {
    let mut order = Vec::new();
    let mut pending = vec![0];
    let mut mapping = vec![usize::MAX; tree.nodes.len()];
    while let Some(index) = pending.pop() {
        if index >= mapping.len() || mapping[index] != usize::MAX {
            return Err(ParseError::InvalidMetadataResource);
        }
        mapping[index] = order.len();
        order.push(index);
        pending.extend(tree.nodes[index].children.iter().rev().copied());
    }
    let mut nodes = Vec::new();
    nodes
        .try_reserve_exact(order.len())
        .map_err(|_| ParseError::Nomem)?;
    for index in order {
        let mut node = tree.nodes[index].clone();
        for child in &mut node.children {
            *child = mapping[*child];
        }
        nodes.push(node);
    }
    tree.nodes = nodes;
    Ok(mapping)
}
pub(crate) fn select_for_update(handle: &WimHandle, image: c_int) -> Result<(), ParseError> {
    if image < 1 || image as u32 > handle.header.image_count {
        return Err(ParseError::InvalidImage);
    }
    let index = image as usize - 1;
    let bytes = image_metadata_bytes(handle, index)?;
    Metadata::parse(&bytes)?;
    if crate::engine::handles::image_owner_count(handle, index) > 1 {
        return Err(ParseError::ImageHasMultipleReferences);
    }
    Ok(())
}
fn mutate(
    handle: &mut WimHandle,
    image: c_int,
    source: &[Vec<u16>],
    target: Option<&[Vec<u16>]>,
    flags: c_int,
) -> Result<(), ParseError> {
    if image < 1 || image as u32 > handle.header.image_count {
        return Err(ParseError::InvalidImage);
    }
    let index = image as usize - 1;
    let bytes = image_metadata_bytes(handle, index)?;
    let parsed = Metadata::parse(&bytes)?;
    if crate::engine::handles::image_owner_count(handle, index) > 1 {
        return Err(ParseError::ImageHasMultipleReferences);
    }
    if flags & !3 != 0 {
        return Err(ParseError::InvalidParam);
    }
    let mut capture = crate::engine::handles::image_capture_plan(&handle.images[index])?;
    let mut tree = match &capture {
        Some(plan) => plan.tree.clone(),
        None => OwnedMetadata::from_metadata(&parsed)?,
    };
    let original = tree.clone();
    let src = match find(&tree, source) {
        Ok(src) => src,
        Err(e) if target.is_none() => {
            if flags & 1 != 0 {
                return Ok(());
            }
            let _ = e;
            let mut message = b"Path \"".to_vec();
            message.extend_from_slice(&diagnostic_path(source)?);
            message.extend_from_slice(format!("\" does not exist in WIM image {image}").as_bytes());
            crate::engine::diagnostics::message(false, &message, false);
            return Err(ParseError::PathDoesNotExist);
        }
        Err(e) => return Err(e),
    };
    let mut root_deleted = false;
    if let Some(target) = target {
        let destination = find(&tree, target).ok();
        if destination == Some(src) {
            return Ok(());
        }
        let destination_parent = if let Some(dst) = destination {
            let src_dir = tree.nodes[src].attributes & 0x10 != 0;
            let dst_dir = tree.nodes[dst].attributes & 0x10 != 0;
            if !src_dir && dst_dir {
                errno(libc::EISDIR);
                return Err(ParseError::IsDirectory);
            }
            if src_dir && !dst_dir {
                errno(libc::ENOTDIR);
                return Err(ParseError::Notdir);
            }
            if src_dir && !tree.nodes[dst].children.is_empty() {
                errno(libc::ENOTEMPTY);
                return Err(ParseError::Notempty);
            }
            parent(&tree, dst).unwrap_or(dst)
        } else {
            let p = find(&tree, &target[..target.len().saturating_sub(1)])?;
            if tree.nodes[p].attributes & 0x10 == 0 {
                errno(libc::ENOTDIR);
                return Err(ParseError::Notdir);
            }
            p
        };
        let mut ancestor = Some(destination_parent);
        while let Some(node) = ancestor {
            if node == src {
                errno(libc::EBUSY);
                return Err(ParseError::Notempty);
            }
            ancestor = parent(&tree, node);
        }
        if let Some(dst) = destination {
            tree.nodes[destination_parent]
                .children
                .retain(|&c| c != dst);
        }
        if let Some(p) = parent(&tree, src) {
            tree.nodes[p].children.retain(|&c| c != src);
        }
        tree.nodes[src].name = target
            .last()
            .map(|n| n.iter().flat_map(|u| u.to_le_bytes()).collect())
            .unwrap_or_default();
        tree.nodes[destination_parent].children.push(src);
    } else {
        if tree.nodes[src].attributes & 0x10 != 0 && flags & 2 == 0 {
            let mut message = b"Path \"".to_vec();
            message.extend_from_slice(&diagnostic_path(source)?);
            message.extend_from_slice(format!("\" in WIM image {image} is a directory but a recursive delete was not requested").as_bytes());
            crate::engine::diagnostics::message(false, &message, false);
            return Err(ParseError::IsDirectory);
        }
        if let Some(p) = parent(&tree, src) {
            tree.nodes[p].children.retain(|&c| c != src);
        } else {
            root_deleted = true;
        }
    }
    let output = if root_deleted {
        tree.nodes.clear();
        if let Some(plan) = &mut capture {
            plan.identities.clear();
            plan.bindings.clear();
        }
        let mut output = parsed.security.raw.to_vec();
        output.extend_from_slice(&0u64.to_le_bytes());
        output
    } else {
        let mapping = compact(&mut tree)?;
        if let Some(plan) = &mut capture {
            let mut identities = vec![None; tree.nodes.len()];
            for (old, &new) in mapping.iter().enumerate() {
                if new != usize::MAX {
                    identities[new] = plan.identities.get(old).copied().flatten();
                }
            }
            plan.identities = identities;
            plan.bindings.retain_mut(|binding| {
                let next = mapping.get(binding.node).copied().unwrap_or(usize::MAX);
                if next == usize::MAX {
                    false
                } else {
                    binding.node = next;
                    true
                }
            });
        }
        tree.encode()?
    };
    drop(parsed);
    drop(bytes);
    if let Some(plan) = &mut capture {
        plan.tree = tree.clone();
    }
    let identity = image_identity(handle, &handle.images[index]);
    handle
        .dirty_images
        .try_reserve(1)
        .map_err(|_| ParseError::Nomem)?;
    let staged_index =
        crate::engine::lookup::prepare_capture_index(handle, index, capture.as_ref())?;
    let pending = crate::engine::handles::new_pending_metadata(PendingMetadata {
        capture,
        metadata: output,
        hash: [0; 20],
    })?;
    let counts = |tree: &OwnedMetadata| {
        let mut result = std::collections::HashMap::<[u8; 20], u32>::new();
        for node in &tree.nodes {
            for hash in
                std::iter::once(node.main_hash).chain(node.extra_streams.iter().map(|s| s.hash))
            {
                if hash != [0; 20] {
                    *result.entry(hash).or_default() += 1;
                }
            }
        }
        result
    };
    let next = counts(&tree);
    let mut dead = Vec::new();
    let old_counts = counts(&original);
    for (hash, blob) in handle.owned_blobs.iter() {
        let decrement = old_counts
            .get(hash)
            .copied()
            .unwrap_or(0)
            .saturating_sub(next.get(hash).copied().unwrap_or(0));
        if (blob.bytes.is_memory() || blob.captured.is_some())
            && blob.descriptor.blob.reference_count <= decrement
        {
            staged_index.unlink(hash);
            dead.try_push(*hash).map_err(|_| ParseError::Nomem)?;
        }
    }
    for (hash, count) in old_counts {
        let decrement = count.saturating_sub(next.get(&hash).copied().unwrap_or(0));
        if let Some(blob) = handle.owned_blobs.get_mut(&hash) {
            blob.descriptor.blob.reference_count = blob
                .descriptor
                .blob
                .reference_count
                .saturating_sub(decrement);
        } else if let Some(lookup) = &mut handle.lookup
            && let Some(blob) = lookup.blobs.iter_mut().find(|b| b.hash == hash)
        {
            blob.reference_count = blob.reference_count.saturating_sub(decrement);
        }
    }
    handle.blob_index = staged_index;
    for hash in &dead {
        handle.owned_blobs.remove(hash);
    }
    handle.images[index] = HandleImage::Owned(OwnedImage {
        metadata: Vec::new(),
        hash: [0; 20],
        pending: Some(pending),
        descriptor: None,
        identity,
    });
    handle
        .dirty_images
        .try_insert(identity)
        .map_err(|_| ParseError::Nomem)?;
    Ok(())
}
/// Delete a path in one image, optionally forcing absence or recursively deleting directories.
/// # Safety
/// The handle must be exclusively live; a nonnull path must be readable terminated text.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn wimlib_delete_path(
    handle: *mut WimHandle,
    image: c_int,
    source: *const TChar,
    flags: c_int,
) -> c_int {
    // SAFETY: The C caller guarantees handle and platform string lifetimes.
    unsafe { delete_path(handle, image, source, flags) }
        .err()
        .map_or(0, |error| error as c_int)
}

/// Apply the mutation using checked engine-owned Rust operations.
/// # Safety
/// Handle and platform text pointers must be live for the operation.
pub(crate) unsafe fn delete_path(
    handle: *mut WimHandle,
    image: c_int,
    source: *const TChar,
    flags: c_int,
) -> Result<(), ParseError> {
    // SAFETY: Handle and text validity are caller contracts.
    unsafe { handle.as_mut() }
        .ok_or(ParseError::InvalidParam)
        .and_then(|h| {
            select_for_update(h, image)?;
            if flags & !3 != 0 {
                return Err(ParseError::InvalidParam);
            }
            // SAFETY: The caller supplies readable platform text.
            unsafe { path(source) }.and_then(|p| mutate(h, image, &p, None, flags))
        })
}
/// Rename a path in one image, replacing compatible existing destinations transactionally.
/// # Safety
/// The handle must be exclusively live; nonnull paths must be readable terminated text.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn wimlib_rename_path(
    handle: *mut WimHandle,
    image: c_int,
    source: *const TChar,
    target: *const TChar,
) -> c_int {
    // SAFETY: The C caller guarantees handle and platform string lifetimes.
    unsafe { rename_path(handle, image, source, target) }
        .err()
        .map_or(0, |error| error as c_int)
}

/// Apply the mutation using checked engine-owned Rust operations.
/// # Safety
/// Handle and platform text pointers must be live for the operation.
pub(crate) unsafe fn rename_path(
    handle: *mut WimHandle,
    image: c_int,
    source: *const TChar,
    target: *const TChar,
) -> Result<(), ParseError> {
    // SAFETY: Handle and text validity are caller contracts.
    unsafe { handle.as_mut() }
        .ok_or(ParseError::InvalidParam)
        .and_then(|h| {
            select_for_update(h, image)?;
            unsafe { path(source) }.and_then(|p| {
                unsafe { path(target) }.and_then(|t| {
                    let result = mutate(h, image, &p, Some(&t), 0);
                    if result.is_err() {
                        let mut message = b"Can't rename \"".to_vec();
                        message.extend_from_slice(&diagnostic_path(&p)?);
                        message.extend_from_slice(b"\" to \"");
                        message.extend_from_slice(&diagnostic_path(&t)?);
                        message.push(b'"');
                        crate::engine::diagnostics::message(false, &message, true);
                    }
                    result
                })
            })
        })
}