Skip to content

Commit

Permalink
Interim commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaddeusTreloar committed Nov 4, 2022
1 parent 6f59201 commit 542e930
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 32 deletions.
6 changes: 4 additions & 2 deletions src/bin/zap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{

use clap::{Parser, Subcommand, ValueEnum};
use zapf::{pack_files, unpack_files};
use zap::compression::algorithms::{lz4_encoder, lz4_decoder};
use zap::{compression::algorithms::{lz4_encoder, lz4_decoder}, internal::build_writer, encryption::algorithm::{encryption_passthrough, aes256}, signing::signers::signer_passthrough};
#[derive(Debug, Parser)]
#[command(
author,
Expand Down Expand Up @@ -66,7 +66,9 @@ impl Command {
zap::compress_directory(
&input,
"/tmp/stuff",
lz4_encoder
lz4_encoder,
None,
None
).await?;

let out_file = File::create(&output).expect("Could not create file");
Expand Down
41 changes: 41 additions & 0 deletions src/encryption/algorithm.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::compression::Cleanup;

//Internal
use super::Encryptor;

Expand Down Expand Up @@ -27,4 +29,43 @@ pub fn aes256<T>(
writer: writer?
}
)
}

pub fn encryption_passthrough<T>(input: Result<T, Error>) -> Result<EncryptionPassthrough<T>, Error>
where T: Write+Cleanup<T>
{
match input {
Err(e) => Err(e),
Ok(input) => Ok(
EncryptionPassthrough{
inner: input
}
)
}
}

pub struct EncryptionPassthrough<T>
where T: Write+Cleanup<T>
{
inner: T
}

impl<T> Write for EncryptionPassthrough<T>
where T: Write+Cleanup<T>
{
fn flush(&mut self) -> std::io::Result<()> {
self.inner.flush()
}

fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.inner.write(buf)
}
}

impl<T> Cleanup<T> for EncryptionPassthrough<T>
where T: Cleanup<T>+Write
{
fn cleanup(self) -> Result<T, Error> {
self.inner.cleanup()
}
}
40 changes: 23 additions & 17 deletions src/internal.rs → src/internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ use std::io::{
ErrorKind
};

use openssl::encrypt;

pub fn build_writer<T, U, V, W>(
compressor: fn(Result<T, Error>) -> Result<U, Error>,
encryptor: fn(Result<U, Error>) -> Result<V, Error>,
signer: fn(Result<V, Error>) -> Result<W, Error>,
) -> impl Fn(Result<T, Error>) -> Result<W, Error>
{
move | x | signer(encryptor(compressor(x)))
}

pub fn return_if_equal<T>(a: T, b: T) -> Result<T, Error>
where T: Eq
{
Expand All @@ -27,19 +38,16 @@ pub fn process_unit<T, U>(
func(input)
}

fn process_bind<T, U, V>(
input: Result<T, Error>,
pub fn bind<T, U, V>(
f1: fn(Result<T, Error>) -> Result<U, Error>,
f2: fn(Result<U, Error>) -> Result<V, Error>
) -> Result<V, Error> where
T: Write,
U: Write,
V: Write
f2: fn(Result<U, Error>) -> Result<V, Error>,
) -> impl Fn(Result<T, Error>) -> Result<V, Error>
{

f2(f1(input))
move |x| f2(f1(x))
}


fn process_sign<T, U>(
input: Result<T, Error>,
func: fn(Result<T, Error>) -> Result<(T, U), Error>
Expand All @@ -51,16 +59,14 @@ T: Write
}

/*
Experimenting with some sort of functor to simplify optional processing
pub fn exp_process_bind<T: Write, U: Write>(
input: Result<impl Write, Error>,
Experimental infinite bind
pub fn experimental_bind<T: Write, U: Write>(
x: Result<T, Error>,
mut f: Vec<fn(Result<T, Error>) -> Result<U, Error>>,
) -> Result<impl Write, Error> where
) -> impl Fn(Result<T, Error>) -> Result<U, Error>
{
match f.pop() {
Some(func) => exp_process_bind(func(input), f),
None => input
Some(func) => move | x | func(experimental_bind(f, x)),
None => x
}
}
*/
} */
30 changes: 17 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod compression;
pub mod encryption;
pub mod internal;
pub mod signing;

use std::{
fs::{self, File},
Expand All @@ -9,16 +10,27 @@ use std::{
};

use compression::{Cleanup, compress, decompress};
use internal::{process_unit};
use encryption::algorithm::encryption_passthrough;
use internal::{process_unit, bind, build_writer};
use signing::signers::signer_passthrough;
use walkdir::WalkDir;

pub async fn compress_directory<T: 'static>(
pub async fn compress_directory<T: 'static+Cleanup<T>>(
input_folder_path: &str,
output_folder_path: &str,
compression_algorithm: fn(Result<fs::File, io::Error>) -> Result<T, io::Error>
writer_algorithm: fn(Result<fs::File, io::Error>) -> Result<T, io::Error>,
enc: fn(Result<fs::File, io::Error>) -> Result<T, io::Error>,
sig: fn(Result<fs::File, io::Error>) -> Result<T, io::Error>
) -> io::Result<()>
where T: io::Write+Cleanup<fs::File>
{

let writer = build_writer(
writer_algorithm,
encryption_passthrough,
signer_passthrough
);

let mut task_list = Vec::with_capacity(800);

for entry in WalkDir::new(input_folder_path) {
Expand Down Expand Up @@ -51,20 +63,12 @@ where T: io::Write+Cleanup<fs::File>
std::fs::create_dir_all(current_dir)
.expect("Failed to create all the required directories/subdirectories");

let func = compression_algorithm.clone();
let func = writer_algorithm.clone();
// Rewrite to return errors
let compress_task = tokio::spawn(async move {
/*internal::exp_process_bind(
fs::File::create(output_path),
vec!(func)
);*/

compress(
fs::File::open(entry_path).expect("Failed to open input file"),
internal::process_unit(
fs::File::create(output_path),
func
)
writer(fs::File::create(output_path))
);
});

Expand Down
48 changes: 48 additions & 0 deletions src/signing/signers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// External
use std::{
io::{
Write,
Error
}
};

use crate::compression::Cleanup;

pub fn signer_passthrough<T>(input: Result<T, Error>) -> Result<SignerPassthrough<T>, Error>
where T: Write+Cleanup<T>
{
match input {
Err(e) => Err(e),
Ok(input) => Ok(
SignerPassthrough{
inner: input
}
)
}
}

pub struct SignerPassthrough<T>
where T: Write+Cleanup<T>
{
inner: T
}

impl<T> Write for SignerPassthrough<T>
where T: Write+Cleanup<T>
{
fn flush(&mut self) -> std::io::Result<()> {
self.inner.flush()
}

fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.inner.write(buf)
}
}

impl<T> Cleanup<T> for SignerPassthrough<T>
where T: Write+Cleanup<T>
{
fn cleanup(self) -> Result<T, Error> {
self.inner.cleanup()
}
}

0 comments on commit 542e930

Please sign in to comment.