Skip to content

Commit

Permalink
Add overwrite of a small file with a large one
Browse files Browse the repository at this point in the history
  • Loading branch information
alloncm committed Sep 30, 2023
1 parent 9af4161 commit 463ba8c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
2 changes: 1 addition & 1 deletion rpi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ magenboy_common = {path = "../common"}
log = "0.4"
cfg-if = "1"
bitfield-struct = "0.5"
arrayvec = {version = "0.7", default-features = false}
arrayvec = {version = "0.7", default-features = false} # Also contains Stack String which is nice
libc = {version = "0.2", optional = true}
nix = {version = "0.24", optional = true}
crossbeam-channel = {version = "0.5", optional = true}
Expand Down
25 changes: 13 additions & 12 deletions rpi/src/drivers/fat32/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,10 @@ struct FatLongDirEntry{
name3:[u16;2]
}

pub(self) const SECTOR_SIZE:usize = Disk::get_block_size() as usize;
pub(self) const SECTOR_SIZE:usize = Disk::get_block_size() as usize;
const DISK_PARTITION_INDEX:u8 = 0;
const FAT_ENTRY_EOF_INDEX:u32 = 0x0FFF_FFFF;
const FAT_ENTRY_FREE_INDEX:u32 = 0;
const DELETED_DIR_ENTRY_PREFIX:u8 = 0xE5;
const DIR_EOF_PREFIX:u8 = 0;

Expand Down Expand Up @@ -381,7 +382,6 @@ impl Fat32Fs{
cluster_counter += clusters_sequence;
clusters_sequence = 1;
}
// TODO: verify all the file has been read
}

/// Write a file to the root dir
Expand All @@ -392,24 +392,25 @@ impl Fat32Fs{
let (name, extension) = self.create_filename(filename).unwrap_or_else(|_|core::panic!("File name format is bad: {}", filename));
// check if file exists, if exists try to overwrite it, if cant mark it as deleted
if let Some(existing_entry) = self.root_dir_cache.as_mut_slice().into_iter().find(|d|d.file_name == name && d.file_extension == extension){
let segment = self.fat_table_cache.as_slice().into_iter().find(|f|f.start_index == existing_entry.get_first_cluster_index()).unwrap().clone();
let segment_len = match segment.state {
FatSegmentState::Allocated => segment.len + 1,
FatSegmentState::AllocatedEof => 1,
_ => core::panic!("FAT32 FS Error: received not allocated segment"),
};
let mut fat_buffer:FatBuffer = FatBuffer::new(self.fat_info, existing_entry.get_first_cluster_index() as usize, Some(segment_len as usize), &mut self.disk);
if (existing_entry.size as usize) < content.len(){
existing_entry.file_name[0] = DELETED_DIR_ENTRY_PREFIX;
// TODO: mark the fat entries as free
// Mark the fat entries as free
while fat_buffer.write(FAT_ENTRY_FREE_INDEX).is_ok() {}
fat_buffer.flush(&mut self.disk);
}
else{
let existing_entry = existing_entry.clone(); // Shadow the original in order to satisfy the borrow checker
let mut segment = self.fat_table_cache.as_slice().into_iter().find(|f|f.start_index == existing_entry.get_first_cluster_index()).unwrap().clone();
match segment.state {
FatSegmentState::Allocated => segment.len += 1,
FatSegmentState::AllocatedEof => {},
_ => core::panic!("Error received not allocated segment"),
}
let mut fat_buffer:FatBuffer = FatBuffer::new(self.fat_info, existing_entry.get_first_cluster_index() as usize, Some(segment.len as usize), &mut self.disk);
let mut current_cluster = existing_entry.get_first_cluster_index();
let mut cluster_count = 0;
while cluster_count < segment.len{
while cluster_count < segment_len{
self.write_to_data_section(content, current_cluster);

current_cluster = fat_buffer.read().ok().unwrap();
cluster_count += 1;
}
Expand Down

0 comments on commit 463ba8c

Please sign in to comment.