forked from xoriors/rencfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencryptedfs.rs
66 lines (56 loc) · 1.79 KB
/
encryptedfs.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use core::str::FromStr;
use std::fs;
use std::path::Path;
use anyhow::Result;
use shush_rs::SecretString;
use rencfs::crypto::Cipher;
use rencfs::encryptedfs::write_all_string_to_fs;
use rencfs::encryptedfs::{CreateFileAttr, EncryptedFs, FileType, PasswordProvider};
const ROOT_INODE: u64 = 1;
struct PasswordProviderImpl {}
impl PasswordProvider for PasswordProviderImpl {
fn get_password(&self) -> Option<SecretString> {
// dummy password, use some secure way to get the password like with [keyring](https://crates.io/crates/keyring) crate
Some(SecretString::from_str("pass42").unwrap())
}
}
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt().init();
let data_dir = Path::new("/tmp/rencfs_data_test").to_path_buf();
let _ = fs::remove_dir_all(data_dir.to_str().unwrap());
let cipher = Cipher::ChaCha20Poly1305;
let fs = EncryptedFs::new(
data_dir.clone(),
Box::new(PasswordProviderImpl {}),
cipher,
false,
)
.await?;
let file1 = SecretString::from_str("file1").unwrap();
let (fh, attr) = fs
.create(ROOT_INODE, &file1, file_attr(), false, true)
.await?;
let data = "Hello, world!";
write_all_string_to_fs(&fs, attr.ino, 0, data, fh).await?;
fs.flush(fh).await?;
fs.release(fh).await?;
let fh = fs.open(attr.ino, true, false).await?;
let mut buf = vec![0; data.len()];
fs.read(attr.ino, 0, &mut buf, fh).await?;
fs.release(fh).await?;
assert_eq!(data, String::from_utf8(buf)?);
fs::remove_dir_all(data_dir)?;
println!("All good, bye!");
Ok(())
}
const fn file_attr() -> CreateFileAttr {
CreateFileAttr {
kind: FileType::RegularFile,
perm: 0o644,
uid: 0,
gid: 0,
rdev: 0,
flags: 0,
}
}