Mount as a feature

This commit is contained in:
oupson 2022-10-09 12:18:08 +02:00
parent 94a3627a0f
commit 198a75d0ee
Signed by: oupson
GPG Key ID: 3BD88615552EFCB7
7 changed files with 53 additions and 12 deletions

9
Cargo.lock generated
View File

@ -532,8 +532,8 @@ dependencies = [
"env_logger", "env_logger",
"log", "log",
"rpassword", "rpassword",
"rustcryptfs-fuse",
"rustcryptfs-lib", "rustcryptfs-lib",
"rustcryptfs-mount",
"serde", "serde",
"serde_json", "serde_json",
] ]
@ -566,6 +566,13 @@ dependencies = [
"thiserror", "thiserror",
] ]
[[package]]
name = "rustcryptfs-mount"
version = "0.1.0"
dependencies = [
"rustcryptfs-fuse",
]
[[package]] [[package]]
name = "ryu" name = "ryu"
version = "1.0.11" version = "1.0.11"

View File

@ -2,5 +2,6 @@
members = [ members = [
"rustcryptfs", "rustcryptfs",
"rustcryptfs-lib", "rustcryptfs-lib",
"rustcryptfs-fuse" "rustcryptfs-fuse",
"rustcryptfs-mount"
] ]

View File

@ -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" }

View File

@ -0,0 +1,14 @@
use std::path::Path;
use rustcryptfs_fuse::EncryptedFs;
#[cfg(target_os = "linux")]
pub fn mount<P>(path: P, mount_point: P, password: &str) -> rustcryptfs_fuse::error::Result<()>
where
P: AsRef<Path>,
{
let fs = EncryptedFs::new(path, password)?;
fs.mount(mount_point)?;
Ok(())
}

View File

@ -5,6 +5,10 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
default = ["mount"]
mount = ["rustcryptfs-mount"]
[dependencies] [dependencies]
anyhow = "1.0.53" anyhow = "1.0.53"
serde = { version = "1.0.136", features = ["derive"] } serde = { version = "1.0.136", features = ["derive"] }
@ -14,6 +18,4 @@ log = "0.4.17"
rustcryptfs-lib = { path = "../rustcryptfs-lib" } rustcryptfs-lib = { path = "../rustcryptfs-lib" }
env_logger = "0.9.0" env_logger = "0.9.0"
rpassword = "7.0.0" rpassword = "7.0.0"
rustcryptfs-mount = { path = "../rustcryptfs-mount", optional = true }
[target.'cfg(target_os = "linux")'.dependencies]
rustcryptfs-fuse = { path = "../rustcryptfs-fuse" }

View File

@ -1,3 +1,4 @@
#[cfg(feature = "mount")]
use std::path::PathBuf; use std::path::PathBuf;
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
@ -14,9 +15,10 @@ pub(crate) enum Commands {
/// Decrypt a file /// Decrypt a file
Decrypt(DecryptCommand), Decrypt(DecryptCommand),
// List file contained in a directory /// List file contained in a directory
Ls(LsCommand), Ls(LsCommand),
#[cfg(feature = "mount")]
/// Mount an encrypted folder /// Mount an encrypted folder
Mount(MountCommand), Mount(MountCommand),
} }
@ -49,6 +51,7 @@ pub(crate) struct LsCommand {
pub(crate) password : Option<String> pub(crate) password : Option<String>
} }
#[cfg(feature = "mount")]
#[derive(Debug, Parser)] #[derive(Debug, Parser)]
pub(crate) struct MountCommand { pub(crate) struct MountCommand {
/// The directory /// The directory

View File

@ -6,19 +6,22 @@ use std::{
use clap::Parser; use clap::Parser;
use args::{DecryptCommand, LsCommand, MountCommand}; use args::{DecryptCommand, LsCommand};
use rustcryptfs_lib::GocryptFs; use rustcryptfs_lib::GocryptFs;
#[cfg(feature = "mount")]
use args::MountCommand;
mod args; mod args;
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
env_logger::init(); env_logger::init();
let args = args::Args::parse(); let args = args::Args::parse();
log::debug!("{:?}", args);
match &args.command { match &args.command {
args::Commands::Decrypt(c) => decrypt_file(c), args::Commands::Decrypt(c) => decrypt_file(c),
args::Commands::Ls(c) => ls(c), args::Commands::Ls(c) => ls(c),
#[cfg(feature = "mount")]
args::Commands::Mount(c) => mount(c), args::Commands::Mount(c) => mount(c),
} }
} }
@ -116,9 +119,9 @@ fn decrypt_file(c: &DecryptCommand) -> anyhow::Result<()> {
} }
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
#[cfg(feature = "mount")]
fn mount(mount: &MountCommand) -> anyhow::Result<()> { fn mount(mount: &MountCommand) -> anyhow::Result<()> {
use anyhow::Context; use anyhow::Context;
use rustcryptfs_fuse::EncryptedFs;
let password = if let Some(password) = &mount.password { let password = if let Some(password) = &mount.password {
password.clone() password.clone()
@ -126,14 +129,14 @@ fn mount(mount: &MountCommand) -> anyhow::Result<()> {
rpassword::prompt_password("Your password: ")? rpassword::prompt_password("Your password: ")?
}; };
let fs = EncryptedFs::new(&mount.path, &password)?; rustcryptfs_mount::mount(&mount.path, &mount.mountpoint, &password)
fs.mount(&mount.mountpoint)
.context("Failed to run fuse fs")?; .context("Failed to run fuse fs")?;
Ok(()) Ok(())
} }
#[cfg(not(target_os = "linux"))] #[cfg(not(target_os = "linux"))]
#[cfg(feature = "mount")]
fn mount(mount: &MountCommand) -> anyhow::Result<()> { fn mount(mount: &MountCommand) -> anyhow::Result<()> {
unimplemented!() unimplemented!()
} }