From 198a75d0ee9563d2df98d5c146b47e0d8ad63a04 Mon Sep 17 00:00:00 2001 From: oupson Date: Sun, 9 Oct 2022 12:18:08 +0200 Subject: [PATCH] Mount as a feature --- Cargo.lock | 9 ++++++++- Cargo.toml | 3 ++- rustcryptfs-mount/Cargo.toml | 11 +++++++++++ rustcryptfs-mount/src/lib.rs | 14 ++++++++++++++ rustcryptfs/Cargo.toml | 8 +++++--- rustcryptfs/src/args.rs | 5 ++++- rustcryptfs/src/main.rs | 15 +++++++++------ 7 files changed, 53 insertions(+), 12 deletions(-) create mode 100644 rustcryptfs-mount/Cargo.toml create mode 100644 rustcryptfs-mount/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 35b1648..ff6880d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -532,8 +532,8 @@ dependencies = [ "env_logger", "log", "rpassword", - "rustcryptfs-fuse", "rustcryptfs-lib", + "rustcryptfs-mount", "serde", "serde_json", ] @@ -566,6 +566,13 @@ dependencies = [ "thiserror", ] +[[package]] +name = "rustcryptfs-mount" +version = "0.1.0" +dependencies = [ + "rustcryptfs-fuse", +] + [[package]] name = "ryu" version = "1.0.11" diff --git a/Cargo.toml b/Cargo.toml index 540ba42..4edd13b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,5 +2,6 @@ members = [ "rustcryptfs", "rustcryptfs-lib", - "rustcryptfs-fuse" + "rustcryptfs-fuse", + "rustcryptfs-mount" ] diff --git a/rustcryptfs-mount/Cargo.toml b/rustcryptfs-mount/Cargo.toml new file mode 100644 index 0000000..fc3e0af --- /dev/null +++ b/rustcryptfs-mount/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "rustcryptfs-mount" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] + +[target.'cfg(target_os = "linux")'.dependencies] +rustcryptfs-fuse = { path = "../rustcryptfs-fuse" } diff --git a/rustcryptfs-mount/src/lib.rs b/rustcryptfs-mount/src/lib.rs new file mode 100644 index 0000000..7a51b40 --- /dev/null +++ b/rustcryptfs-mount/src/lib.rs @@ -0,0 +1,14 @@ +use std::path::Path; + +use rustcryptfs_fuse::EncryptedFs; + +#[cfg(target_os = "linux")] +pub fn mount

(path: P, mount_point: P, password: &str) -> rustcryptfs_fuse::error::Result<()> +where + P: AsRef, +{ + let fs = EncryptedFs::new(path, password)?; + + fs.mount(mount_point)?; + Ok(()) +} diff --git a/rustcryptfs/Cargo.toml b/rustcryptfs/Cargo.toml index a89647b..4db0ef5 100644 --- a/rustcryptfs/Cargo.toml +++ b/rustcryptfs/Cargo.toml @@ -5,6 +5,10 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[features] +default = ["mount"] +mount = ["rustcryptfs-mount"] + [dependencies] anyhow = "1.0.53" serde = { version = "1.0.136", features = ["derive"] } @@ -14,6 +18,4 @@ log = "0.4.17" rustcryptfs-lib = { path = "../rustcryptfs-lib" } env_logger = "0.9.0" rpassword = "7.0.0" - -[target.'cfg(target_os = "linux")'.dependencies] -rustcryptfs-fuse = { path = "../rustcryptfs-fuse" } +rustcryptfs-mount = { path = "../rustcryptfs-mount", optional = true } \ No newline at end of file diff --git a/rustcryptfs/src/args.rs b/rustcryptfs/src/args.rs index 44d66e5..7cc85e8 100644 --- a/rustcryptfs/src/args.rs +++ b/rustcryptfs/src/args.rs @@ -1,3 +1,4 @@ +#[cfg(feature = "mount")] use std::path::PathBuf; use clap::{Parser, Subcommand}; @@ -14,9 +15,10 @@ pub(crate) enum Commands { /// Decrypt a file Decrypt(DecryptCommand), - // List file contained in a directory + /// List file contained in a directory Ls(LsCommand), + #[cfg(feature = "mount")] /// Mount an encrypted folder Mount(MountCommand), } @@ -49,6 +51,7 @@ pub(crate) struct LsCommand { pub(crate) password : Option } +#[cfg(feature = "mount")] #[derive(Debug, Parser)] pub(crate) struct MountCommand { /// The directory diff --git a/rustcryptfs/src/main.rs b/rustcryptfs/src/main.rs index 5a0c2f0..2b99749 100644 --- a/rustcryptfs/src/main.rs +++ b/rustcryptfs/src/main.rs @@ -6,19 +6,22 @@ use std::{ use clap::Parser; -use args::{DecryptCommand, LsCommand, MountCommand}; +use args::{DecryptCommand, LsCommand}; use rustcryptfs_lib::GocryptFs; +#[cfg(feature = "mount")] +use args::MountCommand; + mod args; fn main() -> anyhow::Result<()> { env_logger::init(); let args = args::Args::parse(); - log::debug!("{:?}", args); match &args.command { args::Commands::Decrypt(c) => decrypt_file(c), args::Commands::Ls(c) => ls(c), + #[cfg(feature = "mount")] args::Commands::Mount(c) => mount(c), } } @@ -116,9 +119,9 @@ fn decrypt_file(c: &DecryptCommand) -> anyhow::Result<()> { } #[cfg(target_os = "linux")] +#[cfg(feature = "mount")] fn mount(mount: &MountCommand) -> anyhow::Result<()> { use anyhow::Context; - use rustcryptfs_fuse::EncryptedFs; let password = if let Some(password) = &mount.password { password.clone() @@ -126,14 +129,14 @@ fn mount(mount: &MountCommand) -> anyhow::Result<()> { rpassword::prompt_password("Your password: ")? }; - let fs = EncryptedFs::new(&mount.path, &password)?; - - fs.mount(&mount.mountpoint) + rustcryptfs_mount::mount(&mount.path, &mount.mountpoint, &password) .context("Failed to run fuse fs")?; + Ok(()) } #[cfg(not(target_os = "linux"))] +#[cfg(feature = "mount")] fn mount(mount: &MountCommand) -> anyhow::Result<()> { unimplemented!() }