Read more than 4096 bytes in read function

This commit is contained in:
oupson 2022-10-06 15:08:02 +02:00
parent e86139986d
commit b5fbdb6fb0
Signed by: oupson
GPG Key ID: 3BD88615552EFCB7
1 changed files with 38 additions and 7 deletions

View File

@ -2,7 +2,7 @@ use std::{
collections::BTreeMap, collections::BTreeMap,
ffi::OsStr, ffi::OsStr,
fs::{File, FileType as StdFileType}, fs::{File, FileType as StdFileType},
io::{Read, Seek}, io::{Read, Seek, SeekFrom},
ops::Add, ops::Add,
os::unix::prelude::{FileTypeExt, MetadataExt, OsStrExt, PermissionsExt}, os::unix::prelude::{FileTypeExt, MetadataExt, OsStrExt, PermissionsExt},
path::{Path, PathBuf}, path::{Path, PathBuf},
@ -264,7 +264,7 @@ impl Filesystem for EncryptedFs {
ino: u64, ino: u64,
_fh: u64, _fh: u64,
offset: i64, offset: i64,
_size: u32, size: u32,
_flags: i32, _flags: i32,
_lock_owner: Option<u64>, _lock_owner: Option<u64>,
reply: fuser::ReplyData, reply: fuser::ReplyData,
@ -277,18 +277,49 @@ impl Filesystem for EncryptedFs {
let n = file.read(&mut buf).unwrap(); let n = file.read(&mut buf).unwrap();
let id = if n < 18 { None } else { Some(&buf[2..]) }; let id = if n < 18 { None } else { Some(&buf[2..]) };
let block_index = offset as u64 / 4096; let mut block_index = offset as u64 / 4096;
let mut buffer = Vec::with_capacity(size as usize);
let mut rem = size as usize;
let mut buf = [0u8; 4096 + 32]; let mut buf = [0u8; 4096 + 32];
file.seek(std::io::SeekFrom::Start(18 + block_index * (4096 + 32))) file.seek(SeekFrom::Start(18 + block_index * (4096 + 32)))
.unwrap(); .unwrap();
let n = file.read(&mut buf).unwrap(); {
let n = file.read(&mut buf).unwrap();
let res = decoder.decrypt_block(&buf[..n], block_index, id).unwrap(); let res = decoder.decrypt_block(&buf[..n], block_index, id).unwrap();
reply.data(&res); let seek = (offset as u64 - block_index * 4096) as usize;
buffer.extend_from_slice(&res[seek..]);
block_index += 1;
rem -= res.len() - seek;
}
while rem > 0 {
let n = file.read(&mut buf).unwrap();
if n == 0 {
break;
}
let res = decoder.decrypt_block(&buf[..n], block_index, id).unwrap();
let size = res.len().min(rem);
buffer.extend_from_slice(&res[..size]);
block_index += 1;
rem -= size;
}
reply.data(&buffer);
} else { } else {
reply.error(libc::ENOENT) reply.error(libc::ENOENT)
} }