Removing the start cluster field for DirEntry, not done yet. BROKEN!

This commit is contained in:
Bas Wiel, van de 2024-04-28 21:24:46 +02:00
parent 63891c8f28
commit 997946b969
3 changed files with 25 additions and 42 deletions

View File

@ -76,11 +76,10 @@ impl DiskBuilder {
&root_partition,
&mut disk,
)?;
filesystem.write_file(
msdossys.as_slice(),
operating_system.get_msdossys().as_str(),
filesystem.mkfile(
None,
Some(DirEntryType::SysFile),
operating_system.get_msdossys().as_str(),
msdossys.as_slice(),
&root_partition,
&mut disk,
)?;

View File

@ -176,7 +176,7 @@ impl Fat {
"ROOTDIR",
DirEntryType::RootDirectory,
None,
0,
Vec::new(),
0,
None,
)?,
@ -267,7 +267,7 @@ impl Fat {
"ROOTDIR",
DirEntryType::RootDirectory,
None,
0,
Vec::new(),
0,
None,
)?,
@ -330,6 +330,11 @@ impl Fat {
bootsector
}
/// Create a new system file, give the data as a byte slice.
pub fn mksysfile(&mut self, filename: &str, data: &[u8]) -> Result<(), std::io::Error> {
Ok(())
}
/// Create a new file, give the data as a byte slice.
pub fn mkfile(
&mut self,
@ -340,16 +345,15 @@ impl Fat {
disk: &mut Disk,
) -> Result<(), std::io::Error> {
// 1. Allocate the required number of clusters on the allocation table
let clusters = self.allocate_clusters(data)?;
let bytes_per_cluster = self.sectors_per_cluster as usize * 512;
let mut remaining_data = data;
// 2. Create a directory entry for the new file.
let file_entry = DirEntry::new(
let mut file_entry = DirEntry::new(
filename,
DirEntryType::File,
None,
clusters[0],
self.allocate_clusters(data)?,
data.len() as u32,
Some(data),
)?;

View File

@ -6,7 +6,6 @@ pub struct DirEntry {
filename: FileName,
attributes: Attributes,
create_timedate: TimeDate,
pub start_cluster: u32,
pub file_size: u32,
pub entry_type: DirEntryType,
pub children: Vec<DirEntry>,
@ -40,11 +39,9 @@ impl fmt::Display for DirEntry {
write!(
f,
"File name : {}\n\
Start cluster : {}\n\
Entry type : {}\n\
",
self.filename.base_as_string(),
self.start_cluster,
self.entry_type
)
}
@ -57,7 +54,7 @@ impl DirEntry {
filename: &str,
entry_type: DirEntryType,
create_timedate: Option<TimeDate>,
start_cluster: u32,
allocated_clusters: Vec<u32>,
file_size: u32,
data: Option<&[u8]>,
) -> Result<Self, std::io::Error> {
@ -65,15 +62,15 @@ impl DirEntry {
DirEntryType::File => Self::create_file(
filename,
create_timedate,
start_cluster,
allocated_clusters,
file_size,
data.unwrap().to_vec(),
),
DirEntryType::SysFile => {
Self::create_sysfile(filename, create_timedate, start_cluster, file_size)
Self::create_sysfile(filename, create_timedate, file_size, allocated_clusters)
}
DirEntryType::Directory => {
Self::create_directory(filename, create_timedate, start_cluster)
Self::create_directory(filename, create_timedate, allocated_clusters)
}
DirEntryType::RootDirectory => Self::create_rootdir(),
}
@ -84,8 +81,8 @@ impl DirEntry {
fn create_sysfile(
filename: &str,
create_timedate: Option<TimeDate>,
start_cluster: u32,
file_size: 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 +91,6 @@ impl DirEntry {
filename: FileName::new(filename)?,
attributes,
create_timedate: TimeDate::now(),
start_cluster,
file_size,
entry_type: DirEntryType::SysFile,
children: Vec::new(),
@ -105,7 +101,6 @@ impl DirEntry {
filename: FileName::new(filename)?,
attributes,
create_timedate: create_timedate.unwrap(),
start_cluster,
file_size,
entry_type: DirEntryType::SysFile,
children: Vec::new(),
@ -120,7 +115,7 @@ impl DirEntry {
fn create_file(
filename: &str,
create_timedate: Option<TimeDate>,
start_cluster: u32,
allocated_clusters: Vec<u32>,
file_size: u32,
data: Vec<u8>,
) -> Result<Self, std::io::Error> {
@ -131,22 +126,20 @@ impl DirEntry {
filename: FileName::new(filename)?,
attributes,
create_timedate: TimeDate::now(),
start_cluster,
file_size,
entry_type: DirEntryType::File,
children: Vec::new(),
allocated_clusters: Vec::new(),
allocated_clusters,
data,
}),
_ => Ok(DirEntry {
filename: FileName::new(filename)?,
attributes,
create_timedate: create_timedate.unwrap(),
start_cluster,
file_size,
entry_type: DirEntryType::File,
children: Vec::new(),
allocated_clusters: Vec::new(),
allocated_clusters,
data,
}),
}
@ -157,7 +150,6 @@ impl DirEntry {
filename: FileName::new("ROOTDIR")?,
attributes: Attributes::new(false, false, false, false, false, true, false),
create_timedate: TimeDate::now(),
start_cluster: 0,
file_size: 0,
entry_type: DirEntryType::RootDirectory,
children: Vec::new(),
@ -169,7 +161,7 @@ impl DirEntry {
fn create_directory(
filename: &str,
create_timedate: Option<TimeDate>,
start_cluster: 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);
@ -179,22 +171,20 @@ impl DirEntry {
filename: FileName::new(filename)?,
attributes: attributes.clone(),
create_timedate: TimeDate::now(),
start_cluster,
file_size: 0,
entry_type: DirEntryType::Directory,
children: Vec::new(),
allocated_clusters: Vec::new(),
allocated_clusters,
data: Vec::new(),
},
_ => DirEntry {
filename: FileName::new(filename)?,
attributes: attributes.clone(),
create_timedate: create_timedate.unwrap(),
start_cluster,
file_size: 0,
entry_type: DirEntryType::Directory,
children: Vec::new(),
allocated_clusters: Vec::new(),
allocated_clusters,
data: Vec::new(),
},
};
@ -221,7 +211,7 @@ impl DirEntry {
result[11] = self.attributes.as_byte();
result[22..24].copy_from_slice(&self.create_timedate.creation_time_bytes()?);
result[24..26].copy_from_slice(&self.create_timedate.creation_date_bytes()?);
result[26..28].copy_from_slice(&(self.start_cluster as u16).to_le_bytes());
result[26..28].copy_from_slice(&(self.allocated_clusters[0]).to_le_bytes());
result[28..32].copy_from_slice(&self.file_size.to_le_bytes());
Ok(result)
}
@ -238,7 +228,6 @@ impl DirEntry {
filename,
attributes: Attributes::from_byte(bytes[11]),
create_timedate: TimeDate::from_bytes(&datetime_bytes)?,
start_cluster: 2,
file_size: 0,
entry_type: DirEntryType::File, // This is just as wrong as the rest of this function.
children: Vec::new(),
@ -316,7 +305,7 @@ impl DirEntry {
/// assert_eq!(bytes, [0x32, 0x04]); // Assuming the start cluster number is 1234
/// ```
pub fn start_cluster_as_bytes(&self) -> [u8; 2] {
let cluster: u16 = min(self.start_cluster, 65535) as u16;
let cluster: u16 = min(self.allocated_clusters[0], 65535) as u16;
cluster.to_le_bytes()
}
@ -354,10 +343,6 @@ impl DirEntry {
self.children.len()
}
pub fn get_start_cluster(&self) -> u32 {
self.start_cluster
}
pub fn directory_name(&self) -> String {
self.filename.base_as_string()
}
@ -365,10 +350,6 @@ impl DirEntry {
pub fn get_type(&self) -> DirEntryType {
self.entry_type
}
pub fn set_start_cluster(&mut self, cluster: u32) {
self.start_cluster = cluster;
}
}
#[cfg(test)]
@ -389,7 +370,6 @@ mod tests {
filename: FileName::new("ibmbio.com").unwrap(),
attributes: Attributes::default(),
create_timedate: creation_time,
start_cluster: 2,
file_size: 4608,
entry_type: DirEntryType::File, // Dummy placeholder
children: Vec::new(),