diff --git a/rustcryptfs-lib/src/content_enc.rs b/rustcryptfs-lib/src/content_enc.rs index ed94ff3..702bcbd 100644 --- a/rustcryptfs-lib/src/content_enc.rs +++ b/rustcryptfs-lib/src/content_enc.rs @@ -69,4 +69,33 @@ impl ContentEnc { return Ok(buf.to_vec()); } + + pub fn get_real_size(encrypted_size: u64) -> u64 { + if encrypted_size == 0 { + 0 + } else { + let x = (encrypted_size - 50) / 4128; + + let y = (encrypted_size - 50) - x * 4128; + x * 4096 + y + } + } +} + +#[cfg(test)] +mod test { + use super::ContentEnc; + + #[test] + fn test_get_real_size() { + assert_eq!(0, ContentEnc::get_real_size(0)); + + for real_size in 1..4096 * 4 + 1 { + let nbr_full_blocks = real_size / 4096; + let encrypted_size = + 18 + nbr_full_blocks * (4096 + 32) + real_size - nbr_full_blocks * 4096 + 32; + + assert_eq!(real_size, ContentEnc::get_real_size(encrypted_size)); + } + } } diff --git a/rustcryptfs-linux/src/encrypted_filesystem.rs b/rustcryptfs-linux/src/encrypted_filesystem.rs index 7b340ce..e858a88 100644 --- a/rustcryptfs-linux/src/encrypted_filesystem.rs +++ b/rustcryptfs-linux/src/encrypted_filesystem.rs @@ -10,7 +10,7 @@ use std::{ }; use fuser::{FileAttr, FileType, Filesystem, FUSE_ROOT_ID}; -use rustcryptfs_lib::GocryptFs; +use rustcryptfs_lib::{content_enc::ContentEnc, GocryptFs}; use crate::{ error::Result, @@ -49,17 +49,6 @@ impl EncryptedFs { fuser::mount2(self, mountpoint, &[]).unwrap(); } - fn get_real_size(size: u64) -> u64 { - if size == 0 { - 0 - } else { - let x = (size - 50) / 4128; - - let y = (size - 50) - x * 4128; - x * 4096 + y - } - } - fn get_file_type(file_type: StdFileType) -> FileType { if file_type.is_file() { FileType::RegularFile @@ -93,7 +82,7 @@ impl EncryptedFs { let file_type = Self::get_file_type(meta.file_type()); let file_size = if meta.is_file() { - EncryptedFs::get_real_size(meta.size()) + ContentEnc::get_real_size(meta.size()) } else { meta.size() };