libarchive_oxide 0.2.0

Pure-Rust archive detection, compression, extraction, and creation
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
// SPDX-FileCopyrightText: 2026 libarchive_oxide contributors
//
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Runtime-neutral asynchronous archive adapters.

use std::future::poll_fn;
use std::pin::Pin;
use std::task::{Context, Poll};

use futures_io::{AsyncRead, AsyncWrite};
use libarchive_oxide_core::filter::FilterId;
use libarchive_oxide_core::{
    ArchiveError, ArchiveMetadata, CpioDialect, EncodeCommand, EncodeStatus, EntryMetadata,
    ErrorKind, FormatId, Limits, TarEncoder,
};

#[cfg(feature = "aes")]
use crate::SecretBytes;
use crate::async_filter::{AsyncFilterReader, AsyncFilterWriter};
use crate::stream::{Pipeline, PipelineEvent, ReaderEvent, RuntimeEncoder, StreamError};
use crate::zip::ZipMethod;

const BUFFER: usize = 64 * 1024;

/// Runtime-neutral asynchronous archive reader.
///
/// Dropping this value cancels reads without performing hidden I/O.
#[derive(Debug)]
pub struct AsyncArchiveReader<R> {
    reader: AsyncReaderInput<R>,
    pipeline: Pipeline,
    read_buffer: Vec<u8>,
    event_data: Vec<u8>,
}

#[derive(Debug)]
enum AsyncReaderInput<R> {
    Plain(R),
    One(Box<AsyncFilterReader<R>>),
    Two(Box<AsyncFilterReader<AsyncFilterReader<R>>>),
    Three(Box<AsyncFilterReader<AsyncFilterReader<AsyncFilterReader<R>>>>),
    Four(Box<AsyncFilterReader<AsyncFilterReader<AsyncFilterReader<AsyncFilterReader<R>>>>>),
}

impl<R: AsyncRead + Unpin> AsyncReaderInput<R> {
    fn new(input: R, limits: Limits) -> Self {
        let depth = limits.filter_depth().unwrap_or(4).min(4);
        if depth == 0 {
            return Self::Plain(input);
        }
        let one = AsyncFilterReader::new(input, limits);
        if depth == 1 {
            return Self::One(Box::new(one));
        }
        let two = AsyncFilterReader::new(one, limits);
        if depth == 2 {
            return Self::Two(Box::new(two));
        }
        let three = AsyncFilterReader::new(two, limits);
        if depth == 3 {
            return Self::Three(Box::new(three));
        }
        Self::Four(Box::new(AsyncFilterReader::new(three, limits)))
    }

    fn poll_read(
        &mut self,
        context: &mut Context<'_>,
        output: &mut [u8],
    ) -> Poll<std::io::Result<usize>> {
        match self {
            Self::Plain(input) => Pin::new(input).poll_read(context, output),
            Self::One(input) => Pin::new(input.as_mut()).poll_read(context, output),
            Self::Two(input) => Pin::new(input.as_mut()).poll_read(context, output),
            Self::Three(input) => Pin::new(input.as_mut()).poll_read(context, output),
            Self::Four(input) => Pin::new(input.as_mut()).poll_read(context, output),
        }
    }

    fn into_inner(self) -> R {
        match self {
            Self::Plain(input) => input,
            Self::One(input) => (*input).into_inner(),
            Self::Two(input) => (*input).into_inner().into_inner(),
            Self::Three(input) => (*input).into_inner().into_inner().into_inner(),
            Self::Four(input) => (*input).into_inner().into_inner().into_inner().into_inner(),
        }
    }
}

impl<R: AsyncRead + Unpin> AsyncArchiveReader<R> {
    /// Creates a reader using safe default resource limits.
    #[must_use]
    pub fn new(reader: R) -> Self {
        Self::with_limits(reader, Limits::default())
    }

    /// Creates a reader with explicit resource limits.
    #[must_use]
    pub fn with_limits(reader: R, limits: Limits) -> Self {
        Self {
            reader: AsyncReaderInput::new(reader, limits),
            pipeline: Pipeline::after_filter_adapters(limits),
            read_buffer: vec![0; BUFFER],
            event_data: Vec::with_capacity(BUFFER),
        }
    }

    /// Produces the next structural event with bounded backpressure.
    pub async fn next_event(&mut self) -> Result<ReaderEvent<'_>, StreamError> {
        #[allow(clippy::large_enum_variant)]
        enum OwnedEvent {
            NeedInput,
            ArchiveMetadata(ArchiveMetadata),
            Entry(EntryMetadata),
            Data,
            EndEntry,
            Done,
        }

        loop {
            let event = match self.pipeline.poll_event().map_err(StreamError::archive)? {
                PipelineEvent::NeedInput => OwnedEvent::NeedInput,
                PipelineEvent::ArchiveMetadata(meta) => OwnedEvent::ArchiveMetadata(meta),
                PipelineEvent::Entry(meta) => OwnedEvent::Entry(meta),
                PipelineEvent::Data(data) => {
                    self.event_data.clear();
                    self.event_data.extend_from_slice(data);
                    OwnedEvent::Data
                },
                PipelineEvent::EndEntry => OwnedEvent::EndEntry,
                PipelineEvent::Done => OwnedEvent::Done,
            };

            match event {
                OwnedEvent::NeedInput => {
                    let capacity = self.pipeline.feed_capacity().min(self.read_buffer.len());
                    if capacity == 0 {
                        return Err(StreamError::archive(
                            ArchiveError::new(ErrorKind::Protocol)
                                .with_context("pipeline requested input without capacity"),
                        ));
                    }
                    let buffer = &mut self.read_buffer[..capacity];
                    let read = poll_fn(|cx| self.reader.poll_read(cx, buffer))
                        .await
                        .map_err(StreamError::io)?;
                    if read == 0 {
                        self.pipeline.finish_input().map_err(StreamError::archive)?;
                    } else {
                        let accepted = self
                            .pipeline
                            .feed(&self.read_buffer[..read])
                            .map_err(StreamError::archive)?;
                        if accepted != read {
                            return Err(StreamError::archive(
                                ArchiveError::new(ErrorKind::Protocol)
                                    .with_context("pipeline accepted a partial async read"),
                            ));
                        }
                    }
                },
                OwnedEvent::ArchiveMetadata(meta) => {
                    return Ok(ReaderEvent::ArchiveMetadata(meta));
                },
                OwnedEvent::Entry(meta) => return Ok(ReaderEvent::Entry(meta)),
                OwnedEvent::Data => return Ok(ReaderEvent::Data(&self.event_data)),
                OwnedEvent::EndEntry => return Ok(ReaderEvent::EndEntry),
                OwnedEvent::Done => return Ok(ReaderEvent::Done),
            }
        }
    }

    /// Returns the wrapped asynchronous input.
    #[must_use]
    pub fn into_inner(self) -> R {
        self.reader.into_inner()
    }

    /// Detected format, once the first archive event is available.
    #[must_use]
    pub const fn format(&self) -> Option<FormatId> {
        self.pipeline.format()
    }
}

/// Runtime-neutral asynchronous archive writer.
///
/// Drop and cancellation never synthesize a tar trailer. Call [`Self::finish`]
/// explicitly or use [`Self::abort`] to recover an intentionally incomplete
/// destination.
#[derive(Debug)]
pub struct AsyncArchiveWriter<W> {
    output: AsyncFilterWriter<W>,
    encoder: RuntimeEncoder,
    format: FormatId,
    buffer: Vec<u8>,
    failed: bool,
}

impl<W: AsyncWrite + Unpin> AsyncArchiveWriter<W> {
    /// Creates a sequential asynchronous tar writer.
    #[must_use]
    pub fn new(output: W) -> Self {
        Self {
            output: AsyncFilterWriter::Plain(output),
            encoder: RuntimeEncoder::Tar(TarEncoder::new(Limits::default())),
            format: FormatId::Tar,
            buffer: vec![0; BUFFER],
            failed: false,
        }
    }

    /// Creates a sequential asynchronous writer for an explicit format.
    pub fn with_format(output: W, format: FormatId) -> Result<Self, ArchiveError> {
        Self::with_format_and_limits(output, format, Limits::default())
    }

    /// Creates an asynchronous writer with explicit format and limits.
    pub fn with_format_and_limits(
        output: W,
        format: FormatId,
        limits: Limits,
    ) -> Result<Self, ArchiveError> {
        Ok(Self {
            output: AsyncFilterWriter::Plain(output),
            encoder: RuntimeEncoder::sequential(format, limits)?,
            format,
            buffer: vec![0; BUFFER],
            failed: false,
        })
    }

    /// Creates a sequential asynchronous ZIP writer with an explicit method.
    pub fn with_zip_method(output: W, method: ZipMethod, limits: Limits) -> Self {
        Self {
            output: AsyncFilterWriter::Plain(output),
            encoder: RuntimeEncoder::zip(limits, method),
            format: FormatId::Zip,
            buffer: vec![0; BUFFER],
            failed: false,
        }
    }

    /// Creates a sequential asynchronous cpio writer for an explicit dialect.
    pub fn with_cpio_dialect(output: W, dialect: CpioDialect, limits: Limits) -> Self {
        Self {
            output: AsyncFilterWriter::Plain(output),
            encoder: RuntimeEncoder::cpio(limits, dialect),
            format: FormatId::Cpio,
            buffer: vec![0; BUFFER],
            failed: false,
        }
    }

    /// Creates a streaming asynchronous `WinZip` AES-256 AE-2 writer.
    #[cfg(feature = "aes")]
    pub fn with_zip_password(
        output: W,
        method: ZipMethod,
        password: SecretBytes,
        limits: Limits,
    ) -> Self {
        Self {
            output: AsyncFilterWriter::Plain(output),
            encoder: RuntimeEncoder::encrypted_zip(limits, method, password),
            format: FormatId::Zip,
            buffer: vec![0; BUFFER],
            failed: false,
        }
    }

    /// Creates an asynchronous writer with an optional outer filter.
    pub fn with_filter(
        output: W,
        format: FormatId,
        filter: Option<FilterId>,
        limits: Limits,
    ) -> Result<Self, ArchiveError> {
        Ok(Self {
            output: AsyncFilterWriter::new(output, filter)?,
            encoder: RuntimeEncoder::sequential(format, limits)?,
            format,
            buffer: vec![0; BUFFER],
            failed: false,
        })
    }

    fn ensure_live(&self) -> Result<(), StreamError> {
        if self.failed {
            return Err(StreamError::archive(
                ArchiveError::new(ErrorKind::Protocol)
                    .with_context("archive writer is poisoned by an earlier I/O error"),
            ));
        }
        Ok(())
    }

    /// Sets archive-level metadata before the first entry.
    pub fn set_archive_metadata(&mut self, metadata: &ArchiveMetadata) -> Result<(), StreamError> {
        self.ensure_live()?;
        self.encoder
            .set_archive_metadata(metadata)
            .map_err(StreamError::archive)
    }

    async fn write_output(&mut self, bytes: &[u8]) -> Result<(), StreamError> {
        let mut produced = bytes.len();
        let mut offset = 0;
        while produced != 0 {
            let output = &mut self.output;
            let chunk = &bytes[offset..offset + produced];
            let written = match poll_fn(|cx| Pin::new(&mut *output).poll_write(cx, chunk)).await {
                Ok(0) => {
                    self.failed = true;
                    return Err(StreamError::io(std::io::Error::new(
                        std::io::ErrorKind::WriteZero,
                        "asynchronous archive destination made no progress",
                    )));
                },
                Ok(written) => written,
                Err(error) => {
                    self.failed = true;
                    return Err(StreamError::io(error));
                },
            };
            offset += written;
            produced -= written;
        }
        Ok(())
    }

    async fn emit(&mut self, produced: usize) -> Result<(), StreamError> {
        if produced == 0 {
            return Ok(());
        }
        let bytes = self.buffer[..produced].to_vec();
        self.write_output(&bytes).await
    }

    async fn finish_filter(&mut self) -> Result<(), StreamError> {
        poll_fn(|cx| self.output.poll_finish(cx))
            .await
            .map_err(StreamError::io)
    }

    /// Begins an entry. Tar requires a declared size.
    pub async fn start_entry(&mut self, metadata: &EntryMetadata) -> Result<(), StreamError> {
        self.ensure_live()?;
        loop {
            let step = self
                .encoder
                .step(EncodeCommand::BeginEntry(metadata), &mut self.buffer)
                .map_err(StreamError::archive)?;
            self.emit(step.produced).await?;
            if step.consumed == 1 {
                return Ok(());
            }
            if step.produced == 0 && !matches!(step.status, EncodeStatus::NeedOutput) {
                return Err(StreamError::archive(
                    ArchiveError::new(ErrorKind::Protocol)
                        .with_context("tar encoder did not accept begin-entry"),
                ));
            }
        }
    }

    /// Writes entry body bytes with bounded backpressure.
    pub async fn write_data(&mut self, mut data: &[u8]) -> Result<(), StreamError> {
        self.ensure_live()?;
        while !data.is_empty() {
            let step = self
                .encoder
                .step(EncodeCommand::Data(data), &mut self.buffer)
                .map_err(StreamError::archive)?;
            self.emit(step.produced).await?;
            data = &data[step.consumed..];
            if step.consumed == 0 && step.produced == 0 {
                return Err(StreamError::archive(
                    ArchiveError::new(ErrorKind::Protocol)
                        .with_context("tar encoder made no async write progress"),
                ));
            }
        }
        Ok(())
    }

    /// Ends the current entry and verifies its declared size.
    pub async fn end_entry(&mut self) -> Result<(), StreamError> {
        self.ensure_live()?;
        loop {
            let step = self
                .encoder
                .step(EncodeCommand::EndEntry, &mut self.buffer)
                .map_err(StreamError::archive)?;
            self.emit(step.produced).await?;
            if step.consumed == 1 {
                return Ok(());
            }
            if step.produced == 0 {
                return Err(StreamError::archive(
                    ArchiveError::new(ErrorKind::Protocol)
                        .with_context("tar encoder did not accept end-entry"),
                ));
            }
        }
    }

    /// Finishes the archive and returns its destination.
    pub async fn finish(mut self) -> Result<W, StreamError> {
        self.ensure_live()?;
        loop {
            let step = self
                .encoder
                .step(EncodeCommand::Finish, &mut self.buffer)
                .map_err(StreamError::archive)?;
            self.emit(step.produced).await?;
            if matches!(step.status, EncodeStatus::Done) {
                self.finish_filter().await?;
                return Ok(self.output.into_inner());
            }
            if step.consumed == 0 && step.produced == 0 {
                return Err(StreamError::archive(
                    ArchiveError::new(ErrorKind::Protocol)
                        .with_context("tar encoder made no async finish progress"),
                ));
            }
        }
    }

    /// Recovers the destination without completing the archive.
    #[must_use]
    pub fn abort(self) -> W {
        self.output.into_inner()
    }

    /// Output archive format.
    #[must_use]
    pub const fn format(&self) -> FormatId {
        self.format
    }
}