From 520845131162c8aa3c8b9d1cfb41ff86a6011a19 Mon Sep 17 00:00:00 2001 From: oupson Date: Thu, 6 Oct 2022 11:59:22 +0200 Subject: [PATCH] Add read in fuse fs --- rustcryptfs-linux/src/encrypted_filesystem.rs | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/rustcryptfs-linux/src/encrypted_filesystem.rs b/rustcryptfs-linux/src/encrypted_filesystem.rs index d8bda21..8711351 100644 --- a/rustcryptfs-linux/src/encrypted_filesystem.rs +++ b/rustcryptfs-linux/src/encrypted_filesystem.rs @@ -1,6 +1,8 @@ use std::{ collections::BTreeMap, ffi::OsStr, + fs::{File, FileType as StdFileType}, + io::{Read, Seek}, ops::Add, os::unix::prelude::{FileTypeExt, MetadataExt, OsStrExt, PermissionsExt}, path::{Path, PathBuf}, @@ -258,6 +260,44 @@ impl Filesystem for EncryptedFs { reply.error(libc::ENOENT) } } + + fn read( + &mut self, + _req: &fuser::Request<'_>, + ino: u64, + _fh: u64, + offset: i64, + _size: u32, + _flags: i32, + _lock_owner: Option, + reply: fuser::ReplyData, + ) { + if let Some(file_path) = &self.get_path(ino) { + log::debug!("read {:?}", file_path); + + let mut file = File::open(file_path).unwrap(); + let decoder = self.fs.content_decoder(); + + let mut buf = [0u8; 18]; + let n = file.read(&mut buf).unwrap(); + let id = if n < 18 { None } else { Some(&buf[2..]) }; + + let block_index = offset as u64 / 4096; + + let mut buf = [0u8; 4096 + 32]; + + file.seek(std::io::SeekFrom::Start(18 + block_index * (4096 + 32))) + .unwrap(); + + let n = file.read(&mut buf).unwrap(); + + let res = decoder.decrypt_block(&buf[..n], block_index, id).unwrap(); + + reply.data(&res); + } else { + reply.error(libc::ENOENT) + } + } } fn extract_name(