zarlib 0.16.0

ZAR archive library with compression, deduplication, and commit-based history
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
use super::common::*;
use crate::{
    ArchivePath, BLOCK_FLAG_ENCRYPTED, BLOCK_FLAG_UNCOMPRESSED, BlockFrame, CommitValidationLimits,
    ContainerHeader, EntryMetadata, Toc, TocChange, ValidatedCommit,
};
use std::collections::{BTreeMap, HashMap, HashSet};
use std::fs::File;
use std::io::{Read, Seek, SeekFrom};

#[derive(Debug, Clone, Default, PartialEq)]
pub struct ArchiveEntryDetails {
    /// Estimated using the arithmetic mean of the file's block compression ratios.
    pub compressed_size: Option<u64>,
    /// Compressed-to-uncompressed ratio, before rounding the estimated byte count.
    pub compression_ratio: Option<f64>,
    pub owner: Option<u32>,
    pub group: Option<u32>,
    pub permissions: Option<u32>,
    pub metadata_platform: Option<String>,
    pub modified_unix_millis: Option<i64>,
    pub created_unix_millis: Option<i64>,
    pub accessed_unix_millis: Option<i64>,
}

pub fn list_archive_with_details(
    path: &str,
    to: Option<&str>,
    password: Option<&[u8]>,
) -> Result<(Toc, BTreeMap<ArchivePath, ArchiveEntryDetails>), Box<dyn std::error::Error>> {
    let passwords = PasswordSet::new(password.map(<[u8]>::to_vec));
    list_archive_with_details_and_passwords(path, to, &passwords)
}

pub fn list_archive_with_details_and_passwords(
    path: &str,
    to: Option<&str>,
    passwords: &PasswordSet,
) -> Result<(Toc, BTreeMap<ArchivePath, ArchiveEntryDetails>), Box<dyn std::error::Error>> {
    list_archive_with_initial_details(path, to, passwords, Toc::default(), BTreeMap::new(), false)
}

/// Lists the current state after applying a separate archive's commits over a base archive.
pub fn list_archive_with_overlay_and_passwords(
    base: &str,
    overlay: &str,
    passwords: &PasswordSet,
) -> Result<(Toc, BTreeMap<ArchivePath, ArchiveEntryDetails>), Box<dyn std::error::Error>> {
    list_archive_with_overlay_through_and_passwords(base, overlay, None, passwords)
}

/// Lists the combined archive state through an optional base or overlay commit.
pub fn list_archive_with_overlay_through_and_passwords(
    base: &str,
    overlay: &str,
    to: Option<&str>,
    passwords: &PasswordSet,
) -> Result<(Toc, BTreeMap<ArchivePath, ArchiveEntryDetails>), Box<dyn std::error::Error>> {
    if let Some(id) = to {
        let requested = parse_commit_id(id)?;
        if super::info::info_archive(base, false)?
            .iter()
            .any(|commit| commit.id == requested)
        {
            return list_archive_with_details_and_passwords(base, Some(id), passwords);
        }
    }
    let (toc, details) = list_archive_with_details_and_passwords(base, None, passwords)?;
    list_archive_with_initial_details(overlay, to, passwords, toc, details, true)
}

fn list_archive_with_initial_details(
    path: &str,
    to: Option<&str>,
    passwords: &PasswordSet,
    mut toc: Toc,
    mut details: BTreeMap<ArchivePath, ArchiveEntryDetails>,
    overlay: bool,
) -> Result<(Toc, BTreeMap<ArchivePath, ArchiveEntryDetails>), Box<dyn std::error::Error>> {
    let mut file = File::open(path)?;
    let container = ContainerHeader::read_from(&mut file)?;
    let limits = CommitValidationLimits::default();
    let mut header_offset = container.first_header_offset;
    let requested_id = to.map(parse_commit_id).transpose()?;
    let mut matching_commits = 0_u32;
    let mut stopped_at_to = false;
    let mut commit_ids = HashSet::new();

    while header_offset != 0 {
        let (commit_header, data_start, next_header_offset) =
            read_commit_at_offset(&mut file, header_offset, limits)?;
        if !commit_ids.insert(commit_header.commit_id) {
            return Err(crate::TocError::DuplicateCommitId(commit_header.commit_id).into());
        }
        if requested_id == Some(commit_header.commit_id) {
            matching_commits += 1;
            if matching_commits > 1 {
                return Err("--to commit ID is ambiguous".into());
            }
        }
        if stopped_at_to {
            header_offset = next_header_offset;
            continue;
        }
        file.seek(SeekFrom::Start(header_offset))?;
        let (validated, key) = read_header_with_passwords(&mut file, limits, passwords)?;
        let header = validated.logical_header;
        if header.is_none() && !validated.encryption.is_header_encrypted() {
            return Err("commit header is missing its logical header".into());
        }

        file.seek(SeekFrom::Start(data_start))?;
        let mut block_sizes = HashMap::new();
        let mut remaining = validated.commit_header.data_section_size;
        let mut blocks_seen = 0_u64;
        while remaining > 0 {
            if blocks_seen >= limits.max_blocks {
                return Err(crate::FramingError::InvalidSize.into());
            }
            let before = file.stream_position()?;
            let metadata =
                BlockFrame::read_metadata_from(&mut file, limits.max_frame_payload_size)?;
            let consumed = file
                .stream_position()?
                .checked_sub(before)
                .ok_or("block offset underflow")?;
            if consumed == 0 || consumed > remaining || consumed != metadata.encoded_size {
                return Err(crate::FramingError::InvalidSize.into());
            }
            let uncompressed_size = if metadata.flags & BLOCK_FLAG_ENCRYPTED != 0 {
                if let Some(key) = &key {
                    let algorithm = encryption_algorithm(&validated.encryption)
                        .ok_or("missing encryption algorithm")?;
                    file.seek(SeekFrom::Start(before))?;
                    let frame = BlockFrame::read_from(&mut file, limits.max_frame_payload_size)?;
                    Some(
                        frame
                            .uncompressed_encrypted(
                                &validated.commit_header,
                                key,
                                algorithm,
                                limits.max_uncompressed_block_size,
                            )?
                            .len() as u64,
                    )
                } else {
                    None
                }
            } else if metadata.flags & BLOCK_FLAG_UNCOMPRESSED != 0 {
                if metadata.payload_size as usize > limits.max_uncompressed_block_size {
                    return Err(crate::FramingError::InvalidSize.into());
                }
                Some(u64::from(metadata.payload_size))
            } else {
                let end = file.stream_position()?;
                file.seek(SeekFrom::Start(end - u64::from(metadata.payload_size)))?;
                let mut prefix = [0_u8; 18];
                let length = usize::try_from(metadata.payload_size)?.min(prefix.len());
                file.read_exact(&mut prefix[..length])?;
                let size = match zstd::zstd_safe::get_frame_content_size(&prefix[..length]) {
                    Ok(Some(size)) => size,
                    _ => {
                        file.seek(SeekFrom::Start(before))?;
                        let frame =
                            BlockFrame::read_from(&mut file, limits.max_frame_payload_size)?;
                        frame
                            .uncompressed_at_most(limits.max_uncompressed_block_size)?
                            .len() as u64
                    }
                };
                file.seek(SeekFrom::Start(end))?;
                if size > limits.max_uncompressed_block_size as u64 {
                    return Err(crate::FramingError::InvalidSize.into());
                }
                Some(size)
            };
            if block_sizes
                .insert(
                    metadata.block_index,
                    (u64::from(metadata.payload_size), uncompressed_size),
                )
                .is_some()
            {
                return Err(crate::FramingError::InvalidSize.into());
            }
            remaining -= consumed;
            blocks_seen += 1;
        }

        let block_indices = block_sizes.keys().copied().collect::<HashSet<_>>();
        if let Some(header) = header {
            Toc::check_available_blocks(&header.changes, &block_indices)?;
            if overlay {
                toc.apply_overlay_commit(validated.commit_header.commit_id, &header.changes)?;
            } else {
                toc.apply_archive_commit(validated.commit_header.commit_id, &header.changes)?;
            }
            apply_entry_details(
                &mut details,
                &header.changes,
                &header.entry_metadata,
                &block_sizes,
            )?;
        }
        if requested_id == Some(validated.commit_header.commit_id) {
            stopped_at_to = true;
        }
        header_offset = next_header_offset;
    }

    if requested_id.is_some() && matching_commits == 0 {
        return Err("--to commit ID was not found".into());
    }

    Ok((toc, details))
}

fn apply_entry_details(
    details: &mut BTreeMap<ArchivePath, ArchiveEntryDetails>,
    changes: &[TocChange],
    metadata: &[EntryMetadata],
    block_sizes: &HashMap<u32, (u64, Option<u64>)>,
) -> Result<(), Box<dyn std::error::Error>> {
    // Later records win, matching the previous reverse linear search.
    let metadata: HashMap<&ArchivePath, &EntryMetadata> =
        metadata.iter().map(|entry| (&entry.path, entry)).collect();
    for change in changes {
        match change {
            TocChange::PutFile { path, entry } => {
                let mut ratios = Vec::with_capacity(entry.block_indices.len());
                for index in &entry.block_indices {
                    if entry.source_commit.is_some() {
                        break;
                    }
                    let (compressed, uncompressed) =
                        block_sizes.get(index).ok_or("missing block frame size")?;
                    match uncompressed {
                        Some(0) => return Err("file references an empty block".into()),
                        Some(size) => ratios.push(*compressed as f64 / *size as f64),
                        None => break,
                    }
                }
                let ratio = if entry.size == 0 {
                    None
                } else if ratios.len() == entry.block_indices.len() && !ratios.is_empty() {
                    Some(ratios.iter().sum::<f64>() / ratios.len() as f64)
                } else {
                    None
                };
                let compressed_size = if entry.size == 0 {
                    Some(0)
                } else if let Some(ratio) = ratio {
                    let estimate = (ratio * entry.size as f64).round();
                    if !estimate.is_finite() || estimate >= u64::MAX as f64 {
                        return Err("file compressed size overflow".into());
                    }
                    Some(estimate as u64)
                } else {
                    None
                };
                details.insert(
                    path.clone(),
                    entry_details(path, &metadata, compressed_size, ratio),
                );
            }
            TocChange::PutDirectory { path }
            | TocChange::PutSymlink { path, .. }
            | TocChange::PutNamedPipe { path }
            | TocChange::PutDevice { path, .. } => {
                details.insert(path.clone(), entry_details(path, &metadata, None, None));
            }
            TocChange::RemoveFile { path } => {
                details.remove(path);
            }
            TocChange::RemoveDirectory { path } => {
                details.retain(|entry_path, _| {
                    entry_path != path
                        && !entry_path
                            .as_str()
                            .strip_prefix(path.as_str())
                            .is_some_and(|suffix| suffix.starts_with('/'))
                });
            }
        }
    }
    Ok(())
}

fn entry_details(
    path: &ArchivePath,
    metadata: &HashMap<&ArchivePath, &EntryMetadata>,
    compressed_size: Option<u64>,
    compression_ratio: Option<f64>,
) -> ArchiveEntryDetails {
    let entry_metadata = metadata.get(path).copied();
    ArchiveEntryDetails {
        compressed_size,
        compression_ratio,
        owner: entry_metadata.and_then(|entry| entry.owner),
        group: entry_metadata.and_then(|entry| entry.group),
        permissions: entry_metadata.and_then(|entry| entry.permissions),
        metadata_platform: entry_metadata.map(|entry| entry.platform.clone()),
        modified_unix_millis: entry_metadata.and_then(|entry| entry.modified_unix_millis),
        created_unix_millis: entry_metadata.and_then(|entry| entry.created_unix_millis),
        accessed_unix_millis: entry_metadata.and_then(|entry| entry.accessed_unix_millis),
    }
}

pub fn load_toc(file: &mut File) -> Result<Toc, Box<dyn std::error::Error>> {
    let container = ContainerHeader::read_from(file)?;
    let limits = CommitValidationLimits::default();
    let mut toc = Toc::default();
    let mut header_offset = container.first_header_offset;
    while header_offset != 0 {
        let (commit_header, _data_start, next_header_offset) =
            read_commit_at_offset(file, header_offset, limits)?;
        let validated = ValidatedCommit::read_body(file, commit_header, limits)?;
        if let Some(header) = validated.logical_header {
            Toc::check_available_blocks(&header.changes, &validated.block_indices)?;
            toc.apply_archive_commit(validated.commit_header.commit_id, &header.changes)?;
        } else {
            return Err("cannot build archive TOC from a header-encrypted commit".into());
        }
        header_offset = next_header_offset;
    }
    Ok(toc)
}

pub fn list_archive(
    path: &str,
    to: Option<&str>,
    password: Option<&[u8]>,
) -> Result<Toc, Box<dyn std::error::Error>> {
    let passwords = PasswordSet::new(password.map(<[u8]>::to_vec));
    list_archive_with_passwords(path, to, &passwords)
}

pub fn list_archive_with_passwords(
    path: &str,
    to: Option<&str>,
    passwords: &PasswordSet,
) -> Result<Toc, Box<dyn std::error::Error>> {
    load_toc_through_with_passwords(path, to, passwords)
}

pub fn load_toc_through(
    archive: &str,
    to: Option<&str>,
    password: Option<&[u8]>,
) -> Result<Toc, Box<dyn std::error::Error>> {
    let passwords = PasswordSet::new(password.map(<[u8]>::to_vec));
    load_toc_through_with_passwords(archive, to, &passwords)
}

/// Builds the visible archive state. Commits with encrypted headers are skipped
/// when their passwords are unavailable; later removals of entries from those
/// commits are treated as removals of unknown paths.
pub fn load_toc_through_with_passwords(
    archive: &str,
    to: Option<&str>,
    passwords: &PasswordSet,
) -> Result<Toc, Box<dyn std::error::Error>> {
    load_toc_through_with_passwords_and_progress(archive, to, passwords, None)
}

pub fn load_toc_through_with_progress(
    archive: &str,
    to: Option<&str>,
    password: Option<&[u8]>,
    progress: Option<&ProgressReporter<'_>>,
) -> Result<Toc, Box<dyn std::error::Error>> {
    let passwords = PasswordSet::new(password.map(<[u8]>::to_vec));
    load_toc_through_with_passwords_and_progress(archive, to, &passwords, progress)
}

pub fn load_toc_through_with_passwords_and_progress(
    archive: &str,
    to: Option<&str>,
    passwords: &PasswordSet,
    progress: Option<&ProgressReporter<'_>>,
) -> Result<Toc, Box<dyn std::error::Error>> {
    load_toc_with_initial_state(archive, to, passwords, progress, Toc::default(), false)
}

pub(crate) fn load_overlay_toc(
    base: &str,
    overlay: &str,
    passwords: &PasswordSet,
) -> Result<Toc, Box<dyn std::error::Error>> {
    let toc = load_toc_with_initial_state(base, None, passwords, None, Toc::default(), true)?;
    load_toc_with_initial_state(overlay, None, passwords, None, toc, true)
}

fn load_toc_with_initial_state(
    archive: &str,
    to: Option<&str>,
    passwords: &PasswordSet,
    progress: Option<&ProgressReporter<'_>>,
    mut toc: Toc,
    require_headers: bool,
) -> Result<Toc, Box<dyn std::error::Error>> {
    let mut file = File::open(archive)?;
    let container = ContainerHeader::read_from(&mut file)?;
    let requested_id = to.map(parse_commit_id).transpose()?;
    let mut matches = 0_u32;
    let mut stopped_at_to = false;
    let limits = CommitValidationLimits::default();

    let mut header_offset = container.first_header_offset;
    while header_offset != 0 {
        if let Some(progress) = progress {
            progress.check_cancelled()?;
        }
        let (commit_header, _data_start, next_header_offset) =
            read_commit_at_offset(&mut file, header_offset, limits)?;
        if requested_id == Some(commit_header.commit_id) {
            matches += 1;
            if matches > 1 {
                return Err("--to commit ID is ambiguous".into());
            }
        }
        if stopped_at_to {
            header_offset = next_header_offset;
            continue;
        }
        let body_start = file.stream_position()?;
        file.seek(SeekFrom::Start(header_offset))?;
        let (validated, _) = read_header_with_passwords(&mut file, limits, passwords)?;
        if let Some(progress) = progress {
            let header_end = file.stream_position()?;
            progress.inc(header_end.saturating_sub(body_start));
            progress.set_message(format!(
                "Reading header for commit {:08x}",
                u32::from_le_bytes(validated.commit_header.commit_id)
            ));
        }
        if let Some(header) = validated.logical_header.as_ref() {
            toc.apply_archive_commit(validated.commit_header.commit_id, &header.changes)?;
        } else if require_headers && validated.encryption.is_header_encrypted() {
            return Err(format!(
                "password required for commit {:08x}",
                u32::from_le_bytes(validated.commit_header.commit_id)
            )
            .into());
        } else if !validated.encryption.is_header_encrypted() {
            return Err("commit header is missing its logical header".into());
        }
        if requested_id == Some(validated.commit_header.commit_id) {
            stopped_at_to = true;
        }
        header_offset = next_header_offset;
    }

    if requested_id.is_some() && matches == 0 {
        return Err("--to commit ID was not found".into());
    }
    Ok(toc)
}