summaryrefslogtreecommitdiffstats
path: root/src/filestore/staging.rs
blob: c3215c8f23ad9ba2b3a672a0ed6cc89af587a9be (plain)
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
use std::path::Path;
use std::path::PathBuf;
use anyhow::Result;
use anyhow::Error;
use indicatif::ProgressBar;
use futures::stream::Stream;
use resiter::Map;
use tar;

use crate::filestore::util::FileStoreImpl;

// The implementation of this type must be available in the merged filestore.
pub struct StagingStore(pub (in crate::filestore) FileStoreImpl);

impl StagingStore {
    pub fn load(root: &Path, progress: ProgressBar) -> Result<Self> {
        FileStoreImpl::load(root, progress).map(StagingStore)
    }

    /// Write the passed tar stream to the file store
    ///
    /// # Returns
    ///
    /// Returns a list of Artifacts that were written from the stream
    pub async fn write_files_from_tar_stream<S>(&mut self, stream: S) -> Result<Vec<PathBuf>>
        where S: Stream<Item = Result<Vec<u8>>>
    {
        use futures::stream::TryStreamExt;
        use std::io::Read;

        let dest = &self.0.root;
        stream.try_concat()
            .await
            .and_then(|bytes| {
                let mut archive = tar::Archive::new(&bytes[..]);

                let outputs = archive.entries()?
                    .map(|ent| {
                        let p = ent?.path()?.into_owned();
                        Ok(p)
                    })
                    .map_ok(|path| dest.join(path))
                    .collect::<Result<Vec<_>>>()?;

                tar::Archive::new(&bytes[..])
                    .unpack(dest)
                    .map_err(Error::from)
                    .map(|_| outputs)
            })?
            .into_iter()
            .map(|path| {
                self.0.load_from_path(&path).map(|art| art.path().clone())
            })
            .collect()
    }
}