i6_pack/
cli.rs

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
use crate::compression;
use crate::encryption;
use crate::utils;

pub fn run(action: &str, target: &str, password: &str) -> std::io::Result<()> {
  // Validate and sanitize the target path
  let target_path = utils::validate_path(target)
    .or_else(|_| utils::sanitize_output_path(target))
    .expect("Invalid target path");

  let tar_file =
    &format!(".{}_{}.tar", target_path.display(), uuid::Uuid::new_v4());
  let compressed_file =
    &format!(".{}_{}.tar.zst", target_path.display(), uuid::Uuid::new_v4());
  let encrypted_file = &format!("{}.i6p", target_path.display());

  match action {
    "pack" => {
      compression::create_tar_archive(target_path.to_str().unwrap(), tar_file)?;
      compression::compress_tar_file(tar_file, compressed_file)?;
      encryption::encrypt_file(compressed_file, encrypted_file, password)?;
    }
    "unpack" => {
      encryption::decrypt_file(
        target_path.to_str().unwrap(),
        compressed_file,
        password,
      )?;
      compression::decompress_file(compressed_file, tar_file)?;
      compression::extract_tar_archive(
        tar_file,
        &utils::remove_extension(target_path.to_str().unwrap(), ".i6p"),
      )?;
    }
    _ => {
      eprintln!("Invalid action. Use 'pack' or 'unpack'.");
      std::process::exit(1);
    }
  }

  // Clean up temporary files
  std::fs::remove_file(tar_file)?;
  std::fs::remove_file(compressed_file)?;

  Ok(())
}