aruna_file/
transformer.rs

1use crate::notifications::{Message, Response};
2use anyhow::Result;
3use async_channel::{Receiver, Sender};
4
5#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
6pub enum TransformerType {
7    Unspecified,
8    ReadWriter,
9    All,
10    AsyncSenderSink,
11    ZstdCompressor,
12    GzipCompressor,
13    ZstdDecompressor,
14    ChaCha20Encrypt,
15    ChaCha20Decrypt,
16    Filter,
17    FooterGenerator,
18    HyperSink,
19    SizeProbe,
20    TarEncoder,
21    TarDecoder,
22    WriterSink,
23    Hashing,
24    ZipEncoder,
25}
26
27#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Default)]
28pub struct FileContext {
29    // FileName
30    pub file_name: String,
31    // Input size
32    pub input_size: u64,
33    // Filesize
34    pub file_size: u64,
35    // FileSubpath without filename
36    pub file_path: Option<String>,
37    // UserId
38    pub uid: Option<u64>,
39    // GroupId
40    pub gid: Option<u64>,
41    // Octal like mode
42    pub mode: Option<u32>,
43    // Created at
44    pub mtime: Option<u64>,
45    // Should this file be skipped by decompressors
46    pub skip_decompression: bool,
47    // Should this file be skipped by decryptors
48    pub skip_decryption: bool,
49    // Encryption key
50    pub encryption_key: Option<Vec<u8>>,
51    // Is this file a directory
52    pub is_dir: bool,
53    // Is this file a symlink
54    pub is_symlink: bool,
55}
56
57impl FileContext {
58    #[tracing::instrument(level = "trace", skip(self))]
59    pub fn get_path(&self) -> String {
60        match &self.file_path {
61            Some(p) => p.clone() + "/" + &self.file_name,
62            None => self.file_name.clone(),
63        }
64    }
65}
66
67// Marker trait to signal that this Transformer can be a "final" destination for data
68pub trait Sink: Transformer {}
69
70#[async_trait::async_trait]
71pub trait ReadWriter {
72    async fn process(&mut self) -> Result<()>;
73    async fn announce_all(&mut self, message: Message) -> Result<()>;
74    async fn add_file_context_receiver(&mut self, rx: Receiver<(FileContext, bool)>) -> Result<()>;
75}
76
77#[async_trait::async_trait]
78pub trait Transformer {
79    async fn process_bytes(
80        &mut self,
81        buf: &mut bytes::BytesMut,
82        finished: bool,
83        should_flush: bool,
84    ) -> Result<bool>;
85    #[allow(unused_variables)]
86    async fn notify(&mut self, message: &Message) -> Result<Response> {
87        Ok(Response::Ok)
88    }
89    #[allow(unused_variables)]
90    fn add_sender(&mut self, s: Sender<Message>) {}
91    #[allow(unused_variables)]
92    fn get_type(&self) -> TransformerType {
93        TransformerType::Unspecified
94    }
95}