Skip to content

Commit

Permalink
Merge pull request #94 from NicsTr/master
Browse files Browse the repository at this point in the history
Addressing issue #15
  • Loading branch information
commial authored May 31, 2021
2 parents 950fac0 + 609f947 commit 3e36618
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 7 deletions.
48 changes: 42 additions & 6 deletions mlar/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use mla::{
use rand::SeedableRng;
use rand_chacha::ChaChaRng;
use std::collections::{HashMap, HashSet};
use std::fs::{self, File};
use std::io;
use std::fs::{self, read_dir, File};
use std::io::{self, BufRead};
use std::io::{Read, Seek, SeekFrom, Write};
use std::path::{Component, Path, PathBuf};
use tar::{Builder, Header};
Expand Down Expand Up @@ -396,17 +396,53 @@ impl Write for FileWriter {
}
}

/// Add whatever is specified by `path`
fn add_file_or_dir(mla: &mut ArchiveWriter<OutputTypes>, path: &Path) -> Result<(), Error> {
if path.is_dir() {
add_dir(mla, path)?;
} else {
// This can lead to some non-obvious DuplicateFilename error (files
// that appear with different file names in the filesystem
// but mlar raising DuplicateFilename)
let filename = path.to_string_lossy();
let file = File::open(path)?;
let length = file.metadata()?.len();
eprintln!("{}", filename);
mla.add_file(&filename, length, file)?;
}
Ok(())
}

/// Recursively explore a dir to add all the files
/// Ignore empty directory
fn add_dir(mut mla: &mut ArchiveWriter<OutputTypes>, dir: &Path) -> Result<(), Error> {
for file in read_dir(dir)? {
let new_path = file?.path();
add_file_or_dir(&mut mla, &new_path)?;
}
Ok(())
}

fn add_from_stdin(mut mla: &mut ArchiveWriter<OutputTypes>) -> Result<(), Error> {
for line in io::stdin().lock().lines() {
add_file_or_dir(&mut mla, Path::new(&line?))?;
}
Ok(())
}

// ----- Commands ------

fn create(matches: &ArgMatches) -> Result<(), Error> {
let mut mla = writer_from_matches(matches)?;

if let Some(files) = matches.values_of("files") {
for filename in files {
eprintln!("{}", filename);
let file = File::open(&Path::new(&filename))?;
let length = file.metadata()?.len();
mla.add_file(filename, length, file)?;
if filename == "-" {
add_from_stdin(&mut mla)?;
} else {
let path = Path::new(&filename);
add_file_or_dir(&mut mla, path)?;
}
}
};

Expand Down
104 changes: 103 additions & 1 deletion mlar/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rand::distributions::{Alphanumeric, Distribution, Standard};
use rand::rngs::StdRng;
use rand::SeedableRng;
use std::collections::{HashMap, HashSet};
use std::fs::{metadata, File};
use std::fs::{metadata, read_dir, File};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use tar::Archive;
Expand Down Expand Up @@ -129,6 +129,108 @@ fn ensure_directory_content(directory: &Path, files: &[NamedTempFile]) {
assert_eq!(fname2content.len(), 0);
}

fn file_list_append_from_dir(dir: &Path, file_list: &mut Vec<String>) {
for entry in read_dir(dir).unwrap() {
let new_path = entry.unwrap().path();
if new_path.is_dir() {
file_list_append_from_dir(&new_path, file_list);
} else {
file_list.push(new_path.to_string_lossy().to_string());
}
}
}

#[test]
fn test_create_from_dir() {
let mlar_file = NamedTempFile::new("output.mla").unwrap();
let ecc_public = Path::new("../samples/test_x25519_pub.pem");
let ecc_private = Path::new("../samples/test_x25519.pem");

// Temporary directory to test recursive file addition
let tmp_dir = TempDir::new().unwrap();
let mut subfile1_path = tmp_dir.path().join("subfile1");
let subdir_path = tmp_dir.path().join("subdir");
let mut subfile2_path = subdir_path.join("subfile2");

std::fs::write(&mut subfile1_path, "Test1").unwrap();
std::fs::create_dir(subdir_path).unwrap();
std::fs::write(&mut subfile2_path, "Test2").unwrap();

// `mlar create -o output.mla -p samples/test_x25519_pub.pem <tmp_dir>`
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("create")
.arg("-o")
.arg(mlar_file.path())
.arg("-p")
.arg(ecc_public);

cmd.arg(tmp_dir.path());

let mut file_list: Vec<String> = Vec::new();
// The exact order of the files in the archive depends on the order of the
// result of `read_dir` which is plateform and filesystem dependent.
file_list_append_from_dir(tmp_dir.path(), &mut file_list);

println!("{:?}", cmd);
let assert = cmd.assert();
assert.success().stderr(file_list.join("\n") + "\n");

// `mlar list -i output.mla -k samples/test_x25519.pem`
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("list")
.arg("-i")
.arg(mlar_file.path())
.arg("-k")
.arg(ecc_private);

println!("{:?}", cmd);
let assert = cmd.assert();
file_list.sort();
assert.success().stdout(file_list.join("\n") + "\n");
}

#[test]
fn test_create_filelist_stdin() {
let mlar_file = NamedTempFile::new("output.mla").unwrap();
let ecc_public = Path::new("../samples/test_x25519_pub.pem");
let ecc_private = Path::new("../samples/test_x25519.pem");

// Create files
let testfs = setup();

// `mlar create -o output.mla -p samples/test_x25519_pub.pem -`
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("create")
.arg("-o")
.arg(mlar_file.path())
.arg("-p")
.arg(ecc_public);

cmd.arg("-");
println!("{:?}", cmd);

let mut file_list = String::new();
for file in &testfs.files {
file_list.push_str(format!("{}\n", file.path().to_string_lossy()).as_str());
}
cmd.write_stdin(String::from(&file_list));
println!("{:?}", file_list);
let assert = cmd.assert();
assert.success().stderr(String::from(&file_list));

// `mlar list -i output.mla -k samples/test_x25519.pem`
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("list")
.arg("-i")
.arg(mlar_file.path())
.arg("-k")
.arg(ecc_private);

println!("{:?}", cmd);
let assert = cmd.assert();
assert.success().stdout(file_list);
}

#[test]
fn test_create_list_tar() {
let mlar_file = NamedTempFile::new("output.mla").unwrap();
Expand Down

0 comments on commit 3e36618

Please sign in to comment.