arcthis 0.5.0

An agent-native CLI for accessing and manipulating compressed files
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
mod backend;
mod codec;
mod detect;
mod locator;

use std::collections::HashMap;
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::Mutex;

use backend::{
    ArchiveBackend, ArchiveSource, RarBackend, SevenZipBackend, StreamBackend, TarBackend,
    ZipBackend,
};

use crate::error::{ArcthisError, Result};
use crate::extract::{ExtractOptions, ExtractPlan, ExtractResult, ExtractionStats, PlannedEntry};
use crate::model::{
    ArchiveCapabilities, ArchiveEntry, ArchiveFormat, ArchiveInspection, ArchiveWarning,
    EntryCopyResult, EntryKind, EntryPathEncoding, VerificationResult,
};
use crate::security::{ExtractionLimits, validate_entry_path};

pub use locator::ArchiveLocator;

#[derive(Clone)]
pub struct ArchivePassword(Arc<SecretBytes>);

struct SecretBytes(Vec<u8>);

impl Drop for SecretBytes {
    fn drop(&mut self) {
        self.0.fill(0);
    }
}

impl std::fmt::Debug for ArchivePassword {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.write_str("ArchivePassword([REDACTED])")
    }
}

impl ArchivePassword {
    pub fn new(value: impl Into<Vec<u8>>) -> Self {
        Self(Arc::new(SecretBytes(value.into())))
    }

    pub fn expose(&self) -> &[u8] {
        &self.0.0
    }

    fn as_utf8(&self) -> Result<&str> {
        std::str::from_utf8(self.expose()).map_err(|_| ArcthisError::UnsupportedOperation {
            message: "this archive backend requires a UTF-8 password".to_owned(),
        })
    }
}

#[derive(Clone, Default)]
pub struct ArchiveOpenOptions {
    pub password: Option<ArchivePassword>,
    /// Remaining byte-stream volumes, in exact order after the primary path.
    pub volumes: Vec<PathBuf>,
    /// Override the platform cache root used by persistent archive indexes.
    pub index_directory: Option<PathBuf>,
}

impl std::fmt::Debug for ArchiveOpenOptions {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("ArchiveOpenOptions")
            .field("password", &self.password.as_ref().map(|_| "[REDACTED]"))
            .field("volumes", &self.volumes)
            .field("index_directory", &self.index_directory)
            .finish()
    }
}

/// Format-independent access to one archive source.
pub struct Archive {
    locator: ArchiveLocator,
    source: ArchiveSource,
    backend: Box<dyn ArchiveBackend>,
    entry_index: Mutex<Option<Vec<ArchiveEntry>>>,
    index_directory: Option<PathBuf>,
}

impl Archive {
    pub fn open(locator: impl Into<ArchiveLocator>) -> Result<Self> {
        Self::open_with_options(locator, &ArchiveOpenOptions::default())
    }

    pub fn open_with_options(
        locator: impl Into<ArchiveLocator>,
        options: &ArchiveOpenOptions,
    ) -> Result<Self> {
        let locator = locator.into();
        let source = if options.volumes.is_empty() {
            ArchiveSource::file(locator.path().to_path_buf())
        } else {
            let mut paths = Vec::with_capacity(options.volumes.len() + 1);
            paths.push(locator.path().to_path_buf());
            paths.extend(options.volumes.iter().cloned());
            let mut unique = std::collections::HashSet::with_capacity(paths.len());
            if paths.iter().any(|path| !unique.insert(path.clone())) {
                return Err(ArcthisError::Collision {
                    message: "multipart volume paths must be unique".to_owned(),
                });
            }
            ArchiveSource::multipart(paths)?
        };
        Self::open_source(locator, source, options)
    }

    pub fn open_within(
        locator: impl Into<ArchiveLocator>,
        entries: &[String],
        max_nested_entry_size: u64,
    ) -> Result<Self> {
        Self::open_within_options(
            locator,
            entries,
            max_nested_entry_size,
            &ArchiveOpenOptions::default(),
        )
    }

    pub fn open_within_options(
        locator: impl Into<ArchiveLocator>,
        entries: &[String],
        max_nested_entry_size: u64,
        options: &ArchiveOpenOptions,
    ) -> Result<Self> {
        const MAX_NESTED_DEPTH: usize = 8;
        if entries.len() > MAX_NESTED_DEPTH {
            return Err(ArcthisError::ResourceLimit {
                message: format!("nested archive depth exceeds {MAX_NESTED_DEPTH}"),
            });
        }
        let mut archive = Self::open_with_options(locator, options)?;
        for entry_path in entries {
            let entry = archive.entry(entry_path)?;
            if entry.size > max_nested_entry_size {
                return Err(ArcthisError::ResourceLimit {
                    message: format!(
                        "nested archive entry `{entry_path}` declares {} bytes, above limit {max_nested_entry_size}",
                        entry.size
                    ),
                });
            }
            let capacity =
                usize::try_from(entry.size).map_err(|_| ArcthisError::ResourceLimit {
                    message: format!("nested archive entry `{entry_path}` cannot fit in memory"),
                })?;
            let mut writer = BoundedMemoryWriter::new(capacity, max_nested_entry_size)?;
            let copy_result = archive.copy_entry_to(entry_path, &mut writer);
            if writer.exceeded {
                return Err(ArcthisError::ResourceLimit {
                    message: format!(
                        "nested archive entry `{entry_path}` exceeded {max_nested_entry_size} bytes while decoding"
                    ),
                });
            }
            copy_result?;
            let display = PathBuf::from(format!(
                "{}::{}",
                archive.path().to_string_lossy(),
                entry_path
            ));
            let locator = ArchiveLocator::file(display.clone());
            let source = ArchiveSource::memory(writer.bytes, display);
            archive = Self::open_source(locator, source, options)?;
        }
        Ok(archive)
    }

    fn open_source(
        locator: ArchiveLocator,
        source: ArchiveSource,
        options: &ArchiveOpenOptions,
    ) -> Result<Self> {
        let format = detect::detect(&source)?;
        let backend: Box<dyn ArchiveBackend> = match format {
            ArchiveFormat::Zip => {
                Box::new(ZipBackend::new(source.clone(), options.password.clone()))
            }
            ArchiveFormat::SevenZip => Box::new(SevenZipBackend::new(
                source.clone(),
                options.password.clone(),
            )?),
            ArchiveFormat::Rar => {
                Box::new(RarBackend::new(source.clone(), options.password.clone()))
            }
            ArchiveFormat::Tar
            | ArchiveFormat::TarGzip
            | ArchiveFormat::TarBzip2
            | ArchiveFormat::TarXz
            | ArchiveFormat::TarZstd => Box::new(TarBackend::new(source.clone(), format)),
            ArchiveFormat::Gzip
            | ArchiveFormat::Bzip2
            | ArchiveFormat::Xz
            | ArchiveFormat::Zstd => Box::new(StreamBackend::new(source.clone(), format)?),
        };
        backend.validate()?;
        Ok(Self {
            locator,
            source,
            backend,
            entry_index: Mutex::new(None),
            index_directory: options.index_directory.clone(),
        })
    }

    pub fn path(&self) -> &Path {
        self.locator.path()
    }

    pub fn format(&self) -> ArchiveFormat {
        self.backend.format()
    }

    pub fn capabilities(&self) -> ArchiveCapabilities {
        self.backend.capabilities()
    }

    pub fn entries(&self) -> Result<Vec<ArchiveEntry>> {
        if let Some(entries) = self
            .entry_index
            .lock()
            .map_err(|_| ArcthisError::UnsupportedOperation {
                message: "archive entry index lock was poisoned".to_owned(),
            })?
            .as_ref()
        {
            return Ok(entries.clone());
        }
        if let Some(path) = self.source.file_path()
            && let Some(entries) = crate::index::load_cached_entries(
                path,
                self.format(),
                self.index_directory.as_deref(),
            )?
        {
            *self
                .entry_index
                .lock()
                .map_err(|_| ArcthisError::UnsupportedOperation {
                    message: "archive entry index lock was poisoned".to_owned(),
                })? = Some(entries.clone());
            return Ok(entries);
        }
        self.refresh_entries()
    }

    pub fn refresh_entries(&self) -> Result<Vec<ArchiveEntry>> {
        let mut entries = self.backend.entries()?;
        for entry in &mut entries {
            entry.mime_guess = mime_guess::from_path(&entry.path)
                .first_raw()
                .map(str::to_owned);
        }
        *self
            .entry_index
            .lock()
            .map_err(|_| ArcthisError::UnsupportedOperation {
                message: "archive entry index lock was poisoned".to_owned(),
            })? = Some(entries.clone());
        Ok(entries)
    }

    pub fn entry(&self, path: &str) -> Result<ArchiveEntry> {
        let mut matches = self
            .entries()?
            .into_iter()
            .filter(|entry| entry.path == path);
        let entry = matches.next().ok_or_else(|| ArcthisError::EntryNotFound {
            entry: path.to_owned(),
        })?;
        if matches.next().is_some() {
            return Err(ArcthisError::Collision {
                message: format!("entry path `{path}` occurs more than once"),
            });
        }
        Ok(entry)
    }

    pub fn copy_entry_to(&self, path: &str, writer: &mut dyn Write) -> Result<EntryCopyResult> {
        let entry = self.entry(path)?;
        if entry.kind != EntryKind::File {
            return Err(ArcthisError::UnsupportedOperation {
                message: format!("entry `{path}` is not a regular file"),
            });
        }
        self.backend.copy_entry_to(path, writer)
    }

    #[allow(clippy::too_many_lines)] // One pass keeps warning derivation consistent with the returned metadata.
    pub fn inspect(&self) -> Result<ArchiveInspection> {
        let entries = self.entries()?;
        let compressed_size = self.source.len()?;
        let mut uncompressed_size = 0_u64;
        let mut size_overflow = false;
        for entry in &entries {
            if let Some(size) = uncompressed_size.checked_add(entry.size) {
                uncompressed_size = size;
            } else {
                uncompressed_size = u64::MAX;
                size_overflow = true;
            }
        }

        let capabilities = self.capabilities();
        let encrypted = entries.iter().any(|entry| entry.encrypted);
        let mut warnings = Vec::new();
        if !capabilities.random_access {
            warnings.push(ArchiveWarning {
                code: "sequential_access".to_owned(),
                message: "selected entry access may require a sequential archive scan".to_owned(),
            });
        }
        if encrypted {
            warnings.push(ArchiveWarning {
                code: "encrypted_entries".to_owned(),
                message:
                    "archive content is encrypted; content operations require the correct password"
                        .to_owned(),
            });
        }
        if entries.iter().any(|entry| {
            matches!(
                entry.kind,
                EntryKind::Symlink | EntryKind::Hardlink | EntryKind::Other
            )
        }) {
            warnings.push(ArchiveWarning {
                code: "non_regular_entries".to_owned(),
                message: "archive contains links or special entry kinds that extraction rejects"
                    .to_owned(),
            });
        }
        let mut counts = HashMap::new();
        for entry in &entries {
            *counts.entry(entry.path.as_str()).or_insert(0_u64) += 1;
        }
        if counts.values().any(|count| *count > 1) {
            warnings.push(ArchiveWarning {
                code: "duplicate_entry_paths".to_owned(),
                message: "archive contains duplicate entry paths; named access is ambiguous"
                    .to_owned(),
            });
        }
        if size_overflow {
            warnings.push(ArchiveWarning {
                code: "size_overflow".to_owned(),
                message: "declared uncompressed sizes exceed the representable total".to_owned(),
            });
        }
        let default_limits = ExtractionLimits::default();
        if entries.iter().any(|entry| {
            entry.path_encoding != EntryPathEncoding::Utf8
                || validate_entry_path(&entry.path, entry.kind, &default_limits).is_err()
        }) {
            warnings.push(ArchiveWarning {
                code: "unsafe_entry_paths".to_owned(),
                message: "one or more entry paths would be rejected by safe extraction".to_owned(),
            });
        }
        let exceeds_default_limits = entry_count_exceeds_default(&entries, &default_limits);
        if exceeds_default_limits {
            warnings.push(ArchiveWarning {
                code: "default_extraction_limits_exceeded".to_owned(),
                message: "declared archive metadata exceeds default extraction limits".to_owned(),
            });
        }
        if entries.iter().any(|entry| {
            entry.size > 0
                && entry.compressed_size.is_some_and(|compressed| {
                    compressed == 0 || entry.size > compressed.saturating_mul(1_000)
                })
        }) {
            warnings.push(ArchiveWarning {
                code: "high_compression_ratio".to_owned(),
                message: "one or more entries declare an expansion ratio above 1000:1".to_owned(),
            });
        }
        if matches!(
            self.format(),
            ArchiveFormat::Gzip | ArchiveFormat::Bzip2 | ArchiveFormat::Xz | ArchiveFormat::Zstd
        ) {
            warnings.push(ArchiveWarning {
                code: "single_stream_metadata_scan".to_owned(),
                message: "this format has no entry table; determining payload size requires a sequential decode"
                    .to_owned(),
            });
        }
        let volume_count = self.source.volume_count();
        if volume_count > 1 {
            warnings.push(ArchiveWarning {
                code: "multipart_byte_stream".to_owned(),
                message: format!(
                    "archive source combines {volume_count} explicitly ordered byte-stream volumes"
                ),
            });
        }
        if self.format() == ArchiveFormat::Rar {
            warnings.push(ArchiveWarning {
                code: "rar_metadata_limited".to_owned(),
                message: "RAR solid, encryption, and compressed-size metadata may be unavailable from the current backend"
                    .to_owned(),
            });
        }

        let compression_ratio =
            calculate_compression_ratio(compressed_size, uncompressed_size, size_overflow);
        let entry_count = u64::try_from(entries.len()).unwrap_or(u64::MAX);
        Ok(ArchiveInspection {
            compression: match self.format() {
                ArchiveFormat::Zip | ArchiveFormat::SevenZip | ArchiveFormat::Rar => {
                    "mixed".to_owned()
                }
                ArchiveFormat::Tar => "none".to_owned(),
                ArchiveFormat::TarGzip | ArchiveFormat::Gzip => "gzip".to_owned(),
                ArchiveFormat::TarBzip2 | ArchiveFormat::Bzip2 => "bzip2".to_owned(),
                ArchiveFormat::TarXz | ArchiveFormat::Xz => "xz".to_owned(),
                ArchiveFormat::TarZstd | ArchiveFormat::Zstd => "zstd".to_owned(),
            },
            encrypted,
            solid: capabilities.solid,
            random_access: capabilities.random_access,
            multipart: volume_count > 1,
            volume_count,
            entry_count,
            compressed_size,
            uncompressed_size,
            compression_ratio,
            warnings,
            capabilities,
        })
    }

    pub fn extract(
        &self,
        selected_entry: Option<&str>,
        options: &ExtractOptions,
    ) -> Result<ExtractResult> {
        crate::extract::extract_archive(self, selected_entry, options)
    }

    pub fn plan_extract(
        &self,
        selected_entry: Option<&str>,
        options: &ExtractOptions,
    ) -> Result<ExtractPlan> {
        crate::extract::plan_extract_archive(self, selected_entry, options)
    }

    pub(crate) fn extract_plan(
        &self,
        plan: &[PlannedEntry],
        staging_root: &Path,
        limits: &crate::ExtractionLimits,
    ) -> Result<ExtractionStats> {
        self.backend.extract_plan(plan, staging_root, limits)
    }

    pub fn verify(&self) -> Result<VerificationResult> {
        self.backend.verify()
    }
}

struct BoundedMemoryWriter {
    bytes: Vec<u8>,
    limit: u64,
    exceeded: bool,
}

impl BoundedMemoryWriter {
    fn new(capacity: usize, limit: u64) -> Result<Self> {
        let mut bytes = Vec::new();
        bytes
            .try_reserve_exact(capacity)
            .map_err(|error| ArcthisError::ResourceLimit {
                message: format!("cannot reserve nested archive buffer: {error}"),
            })?;
        Ok(Self {
            bytes,
            limit,
            exceeded: false,
        })
    }
}

impl Write for BoundedMemoryWriter {
    fn write(&mut self, buffer: &[u8]) -> io::Result<usize> {
        let next = u64::try_from(self.bytes.len())
            .unwrap_or(u64::MAX)
            .saturating_add(u64::try_from(buffer.len()).unwrap_or(u64::MAX));
        if next > self.limit {
            self.exceeded = true;
            return Err(io::Error::other("nested archive memory limit exceeded"));
        }
        self.bytes.extend_from_slice(buffer);
        Ok(buffer.len())
    }

    fn flush(&mut self) -> io::Result<()> {
        Ok(())
    }
}

fn entry_count_exceeds_default(entries: &[ArchiveEntry], limits: &ExtractionLimits) -> bool {
    let count = u64::try_from(entries.len()).unwrap_or(u64::MAX);
    if count > limits.max_entries {
        return true;
    }
    let mut total = 0_u64;
    for entry in entries.iter().filter(|entry| entry.kind == EntryKind::File) {
        if entry.size > limits.max_entry_size {
            return true;
        }
        let Some(next) = total.checked_add(entry.size) else {
            return true;
        };
        total = next;
        if total > limits.max_total_size {
            return true;
        }
    }
    false
}

#[allow(clippy::cast_precision_loss)] // JSON ratios are approximate by definition.
fn calculate_compression_ratio(
    compressed_size: u64,
    uncompressed_size: u64,
    size_overflow: bool,
) -> Option<f64> {
    if uncompressed_size == 0 || size_overflow {
        None
    } else {
        Some(compressed_size as f64 / uncompressed_size as f64)
    }
}