Removed the start_cluster field from DirEntry.

This commit is contained in:
Bas Wiel, van de 2024-04-29 10:11:12 +02:00
parent 997946b969
commit 8999be1945
2 changed files with 29 additions and 68 deletions

View File

@ -176,7 +176,7 @@ impl Fat {
"ROOTDIR",
DirEntryType::RootDirectory,
None,
Vec::new(),
&Vec::new(),
0,
None,
)?,
@ -267,7 +267,7 @@ impl Fat {
"ROOTDIR",
DirEntryType::RootDirectory,
None,
Vec::new(),
&Vec::new(),
0,
None,
)?,
@ -353,7 +353,7 @@ impl Fat {
filename,
DirEntryType::File,
None,
self.allocate_clusters(data)?,
&self.allocate_clusters(data)?,
data.len() as u32,
Some(data),
)?;
@ -364,30 +364,6 @@ impl Fat {
None => self.root_directory.add_child(file_entry),
}
// 4. Write the file's bytes to the clusters
for i in 0..clusters.len() {
let mut data: Vec<u8> = Vec::with_capacity(bytes_per_cluster);
let cluster_number = clusters[i];
let bytes_to_write = std::cmp::min(bytes_per_cluster, remaining_data.len());
// Copy data to write into the buffer
data.extend_from_slice(&remaining_data[..bytes_to_write]);
remaining_data = &remaining_data[bytes_to_write..];
// Write data to cluster
self.write_cluster(&mut data, cluster_number as u16, partition, disk)?;
// Update cluster allocation table
if let Some(value) = clusters.get(i + 1).cloned() {
self.allocation_table
.allocate_cluster(cluster_number, value)?;
} else {
// No next cluster, mark end of chain.
self.allocation_table.mark_end_of_chain(cluster_number)?;
break;
}
}
Ok(())
}
@ -400,6 +376,7 @@ impl Fat {
// Set the parent to the root dir if none is given
let new_start_cluster = self.allocate_cluster()?;
self.allocation_table.mark_end_of_chain(new_start_cluster)?;
let allocated_clusters: Vec<u32> = vec![new_start_cluster];
let parent_entry = parent.unwrap_or(&mut self.root_directory);
@ -408,7 +385,7 @@ impl Fat {
dir_name,
DirEntryType::Directory,
None,
new_start_cluster,
&allocated_clusters,
0,
None,
)?;
@ -418,7 +395,7 @@ impl Fat {
".",
DirEntryType::Directory,
None,
new_entry.get_start_cluster(),
&allocated_clusters,
0,
None,
)?);
@ -428,7 +405,7 @@ impl Fat {
"..",
DirEntryType::Directory,
None,
parent_entry.get_start_cluster(),
&allocated_clusters,
0,
None,
)?);
@ -585,7 +562,7 @@ impl Fat {
partition: &Partition,
disk: &mut Disk,
) -> Result<(), std::io::Error> {
let cluster_numbers: Vec<u32> = self.allocate_clusters(file)?;
let allocated_clusters: Vec<u32> = self.allocate_clusters(file)?;
let bytes_per_cluster = self.sectors_per_cluster as usize * 512;
let mut remaining_data = file;
@ -595,7 +572,7 @@ impl Fat {
filename,
DirEntryType::File,
None,
cluster_numbers[0],
&allocated_clusters,
file.len() as u32,
Some(file),
)?,
@ -603,7 +580,7 @@ impl Fat {
filename,
entry_type,
None,
cluster_numbers[0],
&allocated_clusters,
file.len() as u32,
Some(file),
)?,
@ -612,31 +589,6 @@ impl Fat {
if directory.is_none() {
self.root_directory.add_child(dir_entry);
}
// Write the individual clusters to the file sytem
for i in 0..cluster_numbers.len() {
let mut data: Vec<u8> = Vec::with_capacity(bytes_per_cluster);
let cluster_number = cluster_numbers[i];
let bytes_to_write = std::cmp::min(bytes_per_cluster, remaining_data.len());
// Copy data to write into the buffer
data.extend_from_slice(&remaining_data[..bytes_to_write]);
remaining_data = &remaining_data[bytes_to_write..];
// Write data to cluster
self.write_cluster(&mut data, cluster_number as u16, partition, disk)?;
// Update cluster allocation table
if let Some(value) = cluster_numbers.get(i + 1).cloned() {
self.allocation_table
.allocate_cluster(cluster_number, value)?;
} else {
// No next cluster, mark end of chain.
self.allocation_table.mark_end_of_chain(cluster_number)?;
break;
}
}
Ok(())
}

View File

@ -54,7 +54,7 @@ impl DirEntry {
filename: &str,
entry_type: DirEntryType,
create_timedate: Option<TimeDate>,
allocated_clusters: Vec<u32>,
allocated_clusters: &Vec<u32>,
file_size: u32,
data: Option<&[u8]>,
) -> Result<Self, std::io::Error> {
@ -82,7 +82,7 @@ impl DirEntry {
filename: &str,
create_timedate: Option<TimeDate>,
file_size: u32,
allocated_clusters: Vec<u32>,
allocated_clusters: &Vec<u32>,
) -> Result<Self, std::io::Error> {
// Set of attributes appropriate for a system file
let attributes = Attributes::new(true, true, true, true, false, false, false);
@ -94,7 +94,7 @@ impl DirEntry {
file_size,
entry_type: DirEntryType::SysFile,
children: Vec::new(),
allocated_clusters: Vec::new(),
allocated_clusters: allocated_clusters.to_vec(),
data: Vec::new(),
}),
_ => Ok(DirEntry {
@ -104,7 +104,7 @@ impl DirEntry {
file_size,
entry_type: DirEntryType::SysFile,
children: Vec::new(),
allocated_clusters: Vec::new(),
allocated_clusters: allocated_clusters.to_vec(),
data: Vec::new(),
}),
}
@ -115,7 +115,7 @@ impl DirEntry {
fn create_file(
filename: &str,
create_timedate: Option<TimeDate>,
allocated_clusters: Vec<u32>,
allocated_clusters: &Vec<u32>,
file_size: u32,
data: Vec<u8>,
) -> Result<Self, std::io::Error> {
@ -129,7 +129,7 @@ impl DirEntry {
file_size,
entry_type: DirEntryType::File,
children: Vec::new(),
allocated_clusters,
allocated_clusters: allocated_clusters.to_vec(),
data,
}),
_ => Ok(DirEntry {
@ -139,7 +139,7 @@ impl DirEntry {
file_size,
entry_type: DirEntryType::File,
children: Vec::new(),
allocated_clusters,
allocated_clusters: allocated_clusters.to_vec(),
data,
}),
}
@ -161,7 +161,7 @@ impl DirEntry {
fn create_directory(
filename: &str,
create_timedate: Option<TimeDate>,
allocated_clusters: Vec<u32>,
allocated_clusters: &Vec<u32>,
) -> Result<Self, std::io::Error> {
// Set of attributes appropriate to a directory
let attributes = Attributes::new(false, false, false, false, false, true, false);
@ -174,7 +174,7 @@ impl DirEntry {
file_size: 0,
entry_type: DirEntryType::Directory,
children: Vec::new(),
allocated_clusters,
allocated_clusters: allocated_clusters.to_vec(),
data: Vec::new(),
},
_ => DirEntry {
@ -184,7 +184,7 @@ impl DirEntry {
file_size: 0,
entry_type: DirEntryType::Directory,
children: Vec::new(),
allocated_clusters,
allocated_clusters: allocated_clusters.to_vec(),
data: Vec::new(),
},
};
@ -331,6 +331,15 @@ impl DirEntry {
self.file_size.to_le_bytes()
}
/// The start cluster is either 0, or the first entry in the allocated clusters vec.
pub fn get_start_cluster(&self) -> u32 {
if self.allocated_clusters.len() == 0 {
return 0;
} else {
self.allocated_clusters[0]
}
}
pub fn get_children(&mut self) -> &Vec<DirEntry> {
&mut self.children
}