compress-tools 0.16.0

Utility functions for compressed and archive files handling
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
// Copyright (C) 2019-2021 O.S. Systems Sofware LTDA
//
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Generic async support with which you can use you own thread pool by
//! implementing the [`BlockingExecutor`] trait.

use crate::{
    ArchiveContents, ArchiveIteratorBuilder, ArchivePassword, DecodeCallback, Ownership, Result,
    READER_BUFFER_SIZE,
};
use async_trait::async_trait;
use futures_channel::mpsc::{channel, Receiver, Sender};
use futures_core::{FusedStream, Stream};
use futures_executor::block_on;
use futures_io::{AsyncRead, AsyncSeek, AsyncWrite};
use futures_util::{
    io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt},
    join,
    sink::SinkExt,
    stream::StreamExt,
};
use std::{
    future::Future,
    io::{ErrorKind, Read, Seek, SeekFrom, Write},
    path::Path,
    pin::Pin,
    task::{Context, Poll},
};

#[async_trait]
pub trait BlockingExecutor {
    /// Execute the provided function on a thread where blocking is acceptable
    /// (in some kind of thread pool).
    async fn execute_blocking<T, F>(f: F) -> Result<T>
    where
        T: Send + 'static,
        F: FnOnce() -> T + Send + 'static;
}

// ----------------------------------------------------------------------------
// Stream-only reader wrapper (used by `uncompress_data`, which never seeks)
// ----------------------------------------------------------------------------

struct AsyncReadWrapper {
    rx: Receiver<Vec<u8>>,
}

impl Read for AsyncReadWrapper {
    fn read(&mut self, mut buf: &mut [u8]) -> std::io::Result<usize> {
        if self.rx.is_terminated() {
            return Ok(0);
        }
        assert_eq!(buf.len(), READER_BUFFER_SIZE);
        Ok(match block_on(self.rx.next()) {
            Some(data) => {
                buf.write_all(&data)?;
                data.len()
            }
            None => 0,
        })
    }
}

fn make_async_read_wrapper_and_worker<R>(
    mut read: R,
) -> (AsyncReadWrapper, impl Future<Output = Result<()>>)
where
    R: AsyncRead + Unpin,
{
    let (mut tx, rx) = channel(0);
    (AsyncReadWrapper { rx }, async move {
        loop {
            let mut data = vec![0; READER_BUFFER_SIZE];
            let read = read.read(&mut data).await?;
            data.truncate(read);
            if read == 0 || tx.send(data).await.is_err() {
                break;
            }
        }
        Ok(())
    })
}

// ----------------------------------------------------------------------------
// Seekable read bridge (used by list / extract / iterator paths)
// ----------------------------------------------------------------------------
//
// libarchive's seekable formats (ZIP, 7z, …) issue `seek()` calls through the
// synchronous callback it registers with us. When the caller supplies an
// `AsyncRead + AsyncSeek` source, we cannot call `.await` from inside that
// C callback, so we stand up a request/response channel pair: the sync side
// sends a `BridgeReq` describing the desired operation and blocks on the
// matching `BridgeRes`, while an async worker future awaits the operation on
// the underlying source.

enum BridgeReq {
    Read(usize),
    Seek(SeekFrom),
}

enum BridgeRes {
    Read(std::io::Result<Vec<u8>>),
    Seek(std::io::Result<u64>),
}

pub(crate) struct SeekableAsyncReadWrapper {
    req_tx: Sender<BridgeReq>,
    res_rx: Receiver<BridgeRes>,
}

impl Read for SeekableAsyncReadWrapper {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        if block_on(self.req_tx.send(BridgeReq::Read(buf.len()))).is_err() {
            return Ok(0);
        }
        match block_on(self.res_rx.next()) {
            Some(BridgeRes::Read(Ok(data))) => {
                let n = data.len().min(buf.len());
                buf[..n].copy_from_slice(&data[..n]);
                Ok(n)
            }
            Some(BridgeRes::Read(Err(e))) => Err(e),
            Some(BridgeRes::Seek(_)) | None => Ok(0),
        }
    }
}

impl Seek for SeekableAsyncReadWrapper {
    fn seek(&mut self, pos: SeekFrom) -> std::io::Result<u64> {
        if block_on(self.req_tx.send(BridgeReq::Seek(pos))).is_err() {
            return Err(std::io::Error::new(
                ErrorKind::BrokenPipe,
                "async seek bridge closed",
            ));
        }
        match block_on(self.res_rx.next()) {
            Some(BridgeRes::Seek(r)) => r,
            Some(BridgeRes::Read(_)) | None => Err(std::io::Error::new(
                ErrorKind::BrokenPipe,
                "async seek bridge closed",
            )),
        }
    }
}

fn make_seekable_read_wrapper_and_worker<R>(
    mut read: R,
) -> (SeekableAsyncReadWrapper, impl Future<Output = Result<()>>)
where
    R: AsyncRead + AsyncSeek + Unpin,
{
    let (req_tx, mut req_rx) = channel::<BridgeReq>(0);
    let (mut res_tx, res_rx) = channel::<BridgeRes>(0);
    let worker = async move {
        while let Some(req) = req_rx.next().await {
            let res = match req {
                BridgeReq::Read(n) => {
                    let mut buf = vec![0u8; n];
                    match read.read(&mut buf).await {
                        Ok(size) => {
                            buf.truncate(size);
                            BridgeRes::Read(Ok(buf))
                        }
                        Err(e) => BridgeRes::Read(Err(e)),
                    }
                }
                BridgeReq::Seek(pos) => BridgeRes::Seek(read.seek(pos).await),
            };
            if res_tx.send(res).await.is_err() {
                break;
            }
        }
        Ok(())
    };
    (SeekableAsyncReadWrapper { req_tx, res_rx }, worker)
}

// ----------------------------------------------------------------------------
// Write bridge (unchanged)
// ----------------------------------------------------------------------------

pub(crate) struct AsyncWriteWrapper {
    tx: Sender<Vec<u8>>,
}

impl Write for AsyncWriteWrapper {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        match block_on(self.tx.send(buf.to_owned())) {
            Ok(()) => Ok(buf.len()),
            Err(err) => Err(std::io::Error::new(ErrorKind::Other, err)),
        }
    }

    fn flush(&mut self) -> std::io::Result<()> {
        block_on(self.tx.send(vec![])).map_err(|err| std::io::Error::new(ErrorKind::Other, err))
    }
}

fn make_async_write_wrapper_and_worker<W>(
    mut write: W,
) -> (AsyncWriteWrapper, impl Future<Output = Result<()>>)
where
    W: AsyncWrite + Unpin,
{
    let (tx, mut rx) = channel(0);
    (AsyncWriteWrapper { tx }, async move {
        while let Some(v) = rx.next().await {
            if v.is_empty() {
                write.flush().await?;
            } else {
                write.write_all(&v).await?;
            }
        }
        Ok(())
    })
}

// ----------------------------------------------------------------------------
// High-level wrappers
// ----------------------------------------------------------------------------

async fn wrap_async_read_and_write<B, R, W, F, T>(_: B, read: R, write: W, f: F) -> Result<T>
where
    B: BlockingExecutor,
    R: AsyncRead + Unpin,
    W: AsyncWrite + Unpin,
    F: FnOnce(AsyncReadWrapper, AsyncWriteWrapper) -> T + Send + 'static,
    T: Send + 'static,
{
    let (async_read_wrapper, async_read_wrapper_worker) = make_async_read_wrapper_and_worker(read);
    let (async_write_wrapper, async_write_wrapper_worker) =
        make_async_write_wrapper_and_worker(write);
    let g = B::execute_blocking(move || f(async_read_wrapper, async_write_wrapper));
    let join = join!(async_read_wrapper_worker, async_write_wrapper_worker, g);
    join.0?;
    join.1?;
    join.2
}

async fn wrap_async_seek_read<B, R, F, T>(_: B, read: R, f: F) -> Result<T>
where
    B: BlockingExecutor,
    R: AsyncRead + AsyncSeek + Unpin,
    F: FnOnce(SeekableAsyncReadWrapper) -> T + Send + 'static,
    T: Send + 'static,
{
    let (seekable_wrapper, seekable_worker) = make_seekable_read_wrapper_and_worker(read);
    let g = B::execute_blocking(move || f(seekable_wrapper));
    let join = join!(seekable_worker, g);
    join.0?;
    join.1
}

async fn wrap_async_seek_read_and_write<B, R, W, F, T>(_: B, read: R, write: W, f: F) -> Result<T>
where
    B: BlockingExecutor,
    R: AsyncRead + AsyncSeek + Unpin,
    W: AsyncWrite + Unpin,
    F: FnOnce(SeekableAsyncReadWrapper, AsyncWriteWrapper) -> T + Send + 'static,
    T: Send + 'static,
{
    let (seekable_wrapper, seekable_worker) = make_seekable_read_wrapper_and_worker(read);
    let (async_write_wrapper, async_write_wrapper_worker) =
        make_async_write_wrapper_and_worker(write);
    let g = B::execute_blocking(move || f(seekable_wrapper, async_write_wrapper));
    let join = join!(seekable_worker, async_write_wrapper_worker, g);
    join.0?;
    join.1?;
    join.2
}

// ----------------------------------------------------------------------------
// Public async entry points
// ----------------------------------------------------------------------------

/// Async version of
/// [`list_archive_files_with_encoding`](crate::
/// list_archive_files_with_encoding).
pub async fn list_archive_files_with_encoding<B, R>(
    blocking_executor: B,
    source: R,
    decode: DecodeCallback,
) -> Result<Vec<String>>
where
    B: BlockingExecutor,
    R: AsyncRead + AsyncSeek + Unpin,
{
    wrap_async_seek_read(blocking_executor, source, move |source| {
        crate::list_archive_files_with_encoding(source, decode)
    })
    .await?
}

/// Async version of [`list_archive_files`](crate::list_archive_files).
pub async fn list_archive_files<B, R>(blocking_executor: B, source: R) -> Result<Vec<String>>
where
    B: BlockingExecutor,
    R: AsyncRead + AsyncSeek + Unpin,
{
    wrap_async_seek_read(blocking_executor, source, crate::list_archive_files).await?
}

/// Async version of
/// [`list_archive_entries_with_encoding`](crate::
/// list_archive_entries_with_encoding).
pub async fn list_archive_entries_with_encoding<B, R>(
    blocking_executor: B,
    source: R,
    decode: DecodeCallback,
) -> Result<Vec<crate::ArchiveEntryInfo>>
where
    B: BlockingExecutor,
    R: AsyncRead + AsyncSeek + Unpin,
{
    wrap_async_seek_read(blocking_executor, source, move |source| {
        crate::list_archive_entries_with_encoding(source, decode)
    })
    .await?
}

/// Async version of [`list_archive_entries`](crate::list_archive_entries).
pub async fn list_archive_entries<B, R>(
    blocking_executor: B,
    source: R,
) -> Result<Vec<crate::ArchiveEntryInfo>>
where
    B: BlockingExecutor,
    R: AsyncRead + AsyncSeek + Unpin,
{
    wrap_async_seek_read(blocking_executor, source, crate::list_archive_entries).await?
}

/// Async version of [`uncompress_data`](crate::uncompress_data).
pub async fn uncompress_data<B, R, W>(blocking_executor: B, source: R, target: W) -> Result<usize>
where
    B: BlockingExecutor,
    R: AsyncRead + Unpin,
    W: AsyncWrite + Unpin,
{
    wrap_async_read_and_write(blocking_executor, source, target, |source, target| {
        crate::uncompress_data(source, target)
    })
    .await?
}

/// Async version of
/// [`uncompress_archive_with_encoding`](crate::
/// uncompress_archive_with_encoding).
pub async fn uncompress_archive_with_encoding<B, R>(
    blocking_executor: B,
    source: R,
    dest: &Path,
    ownership: Ownership,
    decode: DecodeCallback,
) -> Result<()>
where
    B: BlockingExecutor,
    R: AsyncRead + AsyncSeek + Unpin,
{
    let dest = dest.to_owned();
    wrap_async_seek_read(blocking_executor, source, move |source| {
        crate::uncompress_archive_with_encoding(source, &dest, ownership, decode)
    })
    .await?
}

/// Async version of [`uncompress_archive`](crate::uncompress_archive).
pub async fn uncompress_archive<B, R>(
    blocking_executor: B,
    source: R,
    dest: &Path,
    ownership: Ownership,
) -> Result<()>
where
    B: BlockingExecutor,
    R: AsyncRead + AsyncSeek + Unpin,
{
    let dest = dest.to_owned();
    wrap_async_seek_read(blocking_executor, source, move |source| {
        crate::uncompress_archive(source, &dest, ownership)
    })
    .await?
}

/// Async version of
/// [`uncompress_archive_file_with_encoding`](crate::
/// uncompress_archive_file_with_encoding).
pub async fn uncompress_archive_file_with_encoding<B, R, W>(
    blocking_executor: B,
    source: R,
    target: W,
    path: &str,
    decode: DecodeCallback,
) -> Result<usize>
where
    B: BlockingExecutor,
    R: AsyncRead + AsyncSeek + Unpin,
    W: AsyncWrite + Unpin,
{
    let path = path.to_owned();
    wrap_async_seek_read_and_write(blocking_executor, source, target, move |source, target| {
        crate::uncompress_archive_file_with_encoding(source, target, &path, decode)
    })
    .await?
}

/// Async version of
/// [`uncompress_archive_file`](crate::uncompress_archive_file).
pub async fn uncompress_archive_file<B, R, W>(
    blocking_executor: B,
    source: R,
    target: W,
    path: &str,
) -> Result<usize>
where
    B: BlockingExecutor,
    R: AsyncRead + AsyncSeek + Unpin,
    W: AsyncWrite + Unpin,
{
    let path = path.to_owned();
    wrap_async_seek_read_and_write(blocking_executor, source, target, move |source, target| {
        crate::uncompress_archive_file(source, target, &path)
    })
    .await?
}

// ----------------------------------------------------------------------------
// Async archive iterator
// ----------------------------------------------------------------------------

/// A filter callback for the async archive iterator.
///
/// Differs from the synchronous [`crate::EntryFilterCallbackFn`] only in that
/// it must be `Send + Sync` so that the filter can cross into the blocking
/// worker driving the sync iterator.
pub type AsyncEntryFilterCallbackFn = dyn Fn(&str, &crate::stat) -> bool + Send + Sync;

/// Asynchronous streaming iterator over the contents of an archive.
///
/// Yields [`ArchiveContents`] items in the same order and shape as the
/// synchronous [`ArchiveIterator`]. The sync iterator and its libarchive
/// state live on a dedicated blocking worker; entries are forwarded through
/// a bounded channel and surfaced through this [`Stream`] impl.
///
/// Polling this stream also drives the bridge worker future that services
/// the sync side's `read`/`seek` requests and the blocking pump's
/// `JoinHandle` — so progress only happens while the consumer polls.
/// Dropping the iterator closes the entry channel; the pump notices on its
/// next send and exits.
pub struct AsyncArchiveIterator {
    rx: Receiver<ArchiveContents>,
    worker: Option<Pin<Box<dyn Future<Output = Result<()>> + Send>>>,
    pump: Option<Pin<Box<dyn Future<Output = Result<()>> + Send>>>,
}

impl Stream for AsyncArchiveIterator {
    type Item = ArchiveContents;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        let this = &mut *self;
        if let Some(worker) = this.worker.as_mut() {
            if let Poll::Ready(res) = worker.as_mut().poll(cx) {
                this.worker = None;
                if let Err(e) = res {
                    return Poll::Ready(Some(ArchiveContents::Err(e)));
                }
            }
        }
        if let Some(pump) = this.pump.as_mut() {
            if let Poll::Ready(res) = pump.as_mut().poll(cx) {
                this.pump = None;
                if let Err(e) = res {
                    return Poll::Ready(Some(ArchiveContents::Err(e)));
                }
            }
        }
        Pin::new(&mut this.rx).poll_next(cx)
    }
}

pub(crate) fn new_async_archive_iterator<B, R>(
    source: R,
    decode: DecodeCallback,
    filter: Option<Box<AsyncEntryFilterCallbackFn>>,
    password: Option<ArchivePassword>,
) -> AsyncArchiveIterator
where
    B: BlockingExecutor + 'static,
    R: AsyncRead + AsyncSeek + Unpin + Send + 'static,
{
    let (mut entry_tx, entry_rx) = channel::<ArchiveContents>(1);
    let (seekable_wrapper, seekable_worker) = make_seekable_read_wrapper_and_worker(source);

    let pump_fut = async move {
        let r: Result<()> = B::execute_blocking(move || -> Result<()> {
            let mut builder = ArchiveIteratorBuilder::new(seekable_wrapper).decoder(decode);
            if let Some(filter) = filter {
                builder = builder.filter(move |name, stat| filter(name, stat));
            }
            if let Some(password) = password {
                builder = builder.with_password(password);
            }
            let mut iter = builder.build()?;
            for content in iter.by_ref() {
                if block_on(entry_tx.send(content)).is_err() {
                    // Consumer dropped the receiver; stop forwarding and
                    // close the iterator so libarchive state is released
                    // promptly.
                    break;
                }
            }
            iter.close()
        })
        .await?;
        r
    };

    AsyncArchiveIterator {
        rx: entry_rx,
        worker: Some(Box::pin(seekable_worker)),
        pump: Some(Box::pin(pump_fut)),
    }
}