Skip to content

Commit

Permalink
increase read/write buffer size to 1 MB for release and 256 KB for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
radumarias committed Apr 27, 2024
1 parent 961549c commit acd27b3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
33 changes: 19 additions & 14 deletions src/encryptedfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,11 @@ pub struct EncryptedFs {
key: SecretVec<u8>,
}

#[cfg(test)]
const BUF_SIZE: usize = 256 * 1024; // 256 KB buffer, smaller for tests because they all run in parallel
#[cfg(not(test))]
const BUF_SIZE: usize = 1024 * 1024; // 1 MB buffer

impl EncryptedFs {
pub fn new(data_dir: &str, password: SecretString, cipher: Cipher) -> FsResult<Self> {
let path = PathBuf::from(&data_dir);
Expand Down Expand Up @@ -665,7 +670,7 @@ impl EncryptedFs {
if offset > 0 {
let (_, position, decryptor, _) =
self.read_handles.get_mut(&handle).unwrap();
let mut buffer: [u8; 4096] = [0; 4096];
let mut buffer: [u8; BUF_SIZE] = [0; BUF_SIZE]; // 1MB buffer
loop {
let read_len = if *position + buffer.len() as u64 > offset {
(offset - *position) as usize
Expand Down Expand Up @@ -818,7 +823,7 @@ impl EncryptedFs {
let mut decryptor2 = crypto_util::create_decryptor(in_file, &self.cipher, &self.key);
let mut encryptor = crypto_util::create_encryptor(tmp_file, &self.cipher, &self.key);

let mut buffer: [u8; 4096] = [0; 4096];
let mut buffer: [u8; BUF_SIZE] = [0; BUF_SIZE]; // 1MB buffer
let mut pos_read = 0;
let mut position = 0;
if offset > 0 {
Expand Down Expand Up @@ -849,9 +854,9 @@ impl EncryptedFs {

// if offset is after current position (max file size) we fill up with zeros until offset
if offset > *position {
let buffer: [u8; 4096] = [0; 4096];
let buffer: [u8; BUF_SIZE] = [0; BUF_SIZE]; // 1MB buffer
loop {
let len = min(4096, offset - *position) as usize;
let len = min(buffer.len(), (offset - *position) as usize);
encryptor.write_all(&buffer[..len])?;
*position += len as u64;
if *position == offset {
Expand Down Expand Up @@ -879,10 +884,10 @@ impl EncryptedFs {
// create a new decryptor by reading from the beginning of the file
let mut decryptor = crypto_util::create_decryptor(OpenOptions::new().read(true).open(file)?, cipher, key);
// move read position to the desired position
let mut buffer: [u8; 4096] = [0; 4096];
let mut buffer: [u8; BUF_SIZE] = [0; BUF_SIZE]; // 1MB buffer
let mut read_pos = 0u64;
loop {
let len = min(4096, *position - read_pos) as usize;
let len = min(buffer.len(), (*position - read_pos) as usize);
decryptor.read_exact(&mut buffer[..len])?;
read_pos += len as u64;
if read_pos == *position {
Expand All @@ -891,9 +896,9 @@ impl EncryptedFs {
}

// copy the rest of the file
let mut buffer: [u8; 4096] = [0; 4096];
let mut buffer: [u8; BUF_SIZE] = [0; BUF_SIZE]; // 1MB buffer
loop {
let len = min(4096, attr.size - *position) as usize;
let len = min(buffer.len(), (attr.size - *position) as usize);
decryptor.read_exact(&mut buffer[..len])?;
encryptor.write_all(&buffer[..len])?;
*position += len as u64;
Expand Down Expand Up @@ -993,10 +998,10 @@ impl EncryptedFs {
let mut encryptor = crypto_util::create_encryptor(tmp_file, &self.cipher, &self.key);

// copy existing data until new size
let mut buf: [u8; 4096] = [0; 4096];
let mut buf: [u8; BUF_SIZE] = [0; BUF_SIZE]; // 1MB buffer
let mut pos = 0_u64;
loop {
let len = min(4096, size - pos) as usize;
let len = min(buf.len(), (size - pos) as usize);
decryptor.read_exact(&mut buf[..len])?;
encryptor.write_all(&buf[..len])?;
pos += len as u64;
Expand Down Expand Up @@ -1024,10 +1029,10 @@ impl EncryptedFs {
let mut encryptor = crypto_util::create_encryptor(tmp_file, &self.cipher, &self.key);

// copy existing data
let mut buf: [u8; 4096] = [0; 4096];
let mut buf: [u8; BUF_SIZE] = [0; BUF_SIZE]; // 1MB buffer
let mut pos = 0_u64;
loop {
let len = min(4096, attr.size - pos) as usize;
let len = min(buf.len(), (attr.size - pos) as usize);
decryptor.read_exact(&mut buf[..len])?;
encryptor.write_all(&buf[..len])?;
pos += len as u64;
Expand All @@ -1037,9 +1042,9 @@ impl EncryptedFs {
}

// now fill up with zeros until new size
let buf: [u8; 4096] = [0; 4096];
let buf: [u8; BUF_SIZE] = [0; BUF_SIZE]; // 1MB buffer
loop {
let len = min(4096, size - attr.size) as usize;
let len = min(buf.len(), (size - attr.size) as usize);
encryptor.write_all(&buf[..len])?;
attr.size += len as u64;
if attr.size == size {
Expand Down
4 changes: 2 additions & 2 deletions src/encryptedfs_fuse3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -811,9 +811,9 @@ impl Filesystem for EncryptedFsFuse3 {
}
};

let create = flags & libc::O_CREAT as u32 != 0;
let _create = flags & libc::O_CREAT as u32 != 0;
let truncate = flags & libc::O_TRUNC as u32 != 0;
let append = flags & libc::O_APPEND as u32 != 0;
let _append = flags & libc::O_APPEND as u32 != 0;

let attr = self.get_fs().borrow_mut().get_inode(inode).map_err(|err| {
error!(err = %err);
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,9 @@ pub async fn run_fuse(mountpoint: &str, data_dir: &str, password: SecretString,
let mount_options = MountOptions::default()
.uid(uid)
.gid(gid)
.read_only(false).
allow_root(allow_root).
allow_other(allow_other)
.read_only(false)
.allow_root(allow_root)
.allow_other(allow_other)
.clone();
let mount_path = OsStr::new(mountpoint);

Expand Down

0 comments on commit acd27b3

Please sign in to comment.