From 4f6ed94cb3277dcd874b31b8d373689ed30d1354 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Harabie=C5=84?= Date: Sat, 9 Jun 2018 19:32:15 +0200 Subject: [PATCH] Limit file size to 4GB when writting --- src/file.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/file.rs b/src/file.rs index 95f0dde..b4b86c8 100644 --- a/src/file.rs +++ b/src/file.rs @@ -1,4 +1,5 @@ use core::cmp; +use core; use io::prelude::*; use io::{SeekFrom, ErrorKind}; use io; @@ -6,6 +7,8 @@ use io; use fs::FileSystemRef; use dir_entry::{DirEntryEditor, DateTime, Date}; +const MAX_FILE_SIZE: u32 = core::u32::MAX; + /// FAT file used for reading and writing. #[derive(Clone)] pub struct File<'a, 'b: 'a> { @@ -193,7 +196,9 @@ impl<'a, 'b> Write for File<'a, 'b> { let cluster_size = self.fs.cluster_size(); let offset_in_cluster = self.offset % cluster_size; let bytes_left_in_cluster = (cluster_size - offset_in_cluster) as usize; + let bytes_left_until_max_file_size = (MAX_FILE_SIZE - self.offset) as usize; let write_size = cmp::min(buf.len(), bytes_left_in_cluster); + let write_size = cmp::min(write_size, bytes_left_until_max_file_size); // Exit early if we are going to write no data if write_size == 0 { return Ok(0);