Improve code structure

This commit is contained in:
Rafał Harabień 2018-06-07 23:07:52 +02:00
parent ccb205c906
commit 632a371b0d

View File

@ -120,7 +120,7 @@ impl <'a, 'b> Dir<'a, 'b> {
let e = self.find_entry(name, None)?; let e = self.find_entry(name, None)?;
match rest_opt { match rest_opt {
Some(rest) => e.to_dir().open_dir(rest), Some(rest) => e.to_dir().open_dir(rest),
None => Ok(e.to_dir()) None => Ok(e.to_dir()),
} }
} }
@ -130,44 +130,46 @@ impl <'a, 'b> Dir<'a, 'b> {
let e = self.find_entry(name, None)?; let e = self.find_entry(name, None)?;
match rest_opt { match rest_opt {
Some(rest) => e.to_dir().open_file(rest), Some(rest) => e.to_dir().open_file(rest),
None => Ok(e.to_file()) None => Ok(e.to_file()),
} }
} }
/// Creates new file or opens existing without truncating. /// Creates new file or opens existing without truncating.
pub fn create_file(&mut self, path: &str) -> io::Result<File<'a, 'b>> { pub fn create_file(&mut self, path: &str) -> io::Result<File<'a, 'b>> {
// traverse path
let (name, rest_opt) = split_path(path); let (name, rest_opt) = split_path(path);
match rest_opt { if let Some(rest) = rest_opt {
// path contains more than 1 component return self.find_entry(name, None)?.to_dir().create_file(rest);
Some(rest) => self.find_entry(name, None)?.to_dir().create_file(rest), }
None => {
// this is final filename in the path // this is final filename in the path
let mut short_name_gen = ShortNameGenerator::new(name); let mut short_name_gen = ShortNameGenerator::new(name);
let r = self.find_entry(name, Some(&mut short_name_gen)); let r = self.find_entry(name, Some(&mut short_name_gen));
match r { match r {
// file does not exist - create it
Err(ref err) if err.kind() == ErrorKind::NotFound => { Err(ref err) if err.kind() == ErrorKind::NotFound => {
let short_name = short_name_gen.generate()?; let short_name = short_name_gen.generate()?;
let sfn_entry = self.create_sfn_entry(short_name, FileAttributes::from_bits_truncate(0), None); let sfn_entry = self.create_sfn_entry(short_name, FileAttributes::from_bits_truncate(0), None);
Ok(self.write_entry(name, sfn_entry)?.to_file()) Ok(self.write_entry(name, sfn_entry)?.to_file())
}, },
// other error
Err(err) => Err(err), Err(err) => Err(err),
// file already exists - return it
Ok(e) => Ok(e.to_file()), Ok(e) => Ok(e.to_file()),
} }
} }
}
}
/// Creates new directory or opens existing. /// Creates new directory or opens existing.
pub fn create_dir(&mut self, path: &str) -> io::Result<Self> { pub fn create_dir(&mut self, path: &str) -> io::Result<Self> {
// traverse path
let (name, rest_opt) = split_path(path); let (name, rest_opt) = split_path(path);
match rest_opt { if let Some(rest) = rest_opt {
// path contains more than 1 component return self.find_entry(name, None)?.to_dir().create_dir(rest);
Some(rest) => self.find_entry(name, None)?.to_dir().create_dir(rest), }
None => {
// this is final filename in the path // this is final filename in the path
let mut short_name_gen = ShortNameGenerator::new(name); let mut short_name_gen = ShortNameGenerator::new(name);
let r = self.find_entry(name, Some(&mut short_name_gen)); let r = self.find_entry(name, Some(&mut short_name_gen));
match r { match r {
// directory does not exist - create it
Err(ref err) if err.kind() == ErrorKind::NotFound => { Err(ref err) if err.kind() == ErrorKind::NotFound => {
// alloc cluster for directory data // alloc cluster for directory data
let cluster = self.fs.alloc_cluster(None)?; let cluster = self.fs.alloc_cluster(None)?;
@ -185,12 +187,12 @@ impl <'a, 'b> Dir<'a, 'b> {
dir.write_entry("..", sfn_entry)?; dir.write_entry("..", sfn_entry)?;
Ok(dir) Ok(dir)
}, },
// other error
Err(err) => Err(err), Err(err) => Err(err),
// directory already exists - return it
Ok(e) => Ok(e.to_dir()), Ok(e) => Ok(e.to_dir()),
} }
} }
}
}
fn is_empty(&mut self) -> io::Result<bool> { fn is_empty(&mut self) -> io::Result<bool> {
// check if directory contains no files // check if directory contains no files
@ -210,17 +212,18 @@ impl <'a, 'b> Dir<'a, 'b> {
/// Make sure there is no reference to this file (no File instance) or filesystem corruption /// Make sure there is no reference to this file (no File instance) or filesystem corruption
/// can happen. /// can happen.
pub fn remove(&mut self, path: &str) -> io::Result<()> { pub fn remove(&mut self, path: &str) -> io::Result<()> {
// traverse path
let (name, rest_opt) = split_path(path); let (name, rest_opt) = split_path(path);
let e = self.find_entry(name, None)?; let e = self.find_entry(name, None)?;
match rest_opt { if let Some(rest) = rest_opt {
Some(rest) => e.to_dir().remove(rest), return e.to_dir().remove(rest);
None => { }
trace!("removing {}", path); trace!("removing {}", path);
// in case of directory check if it is empty // in case of directory check if it is empty
if e.is_dir() && !e.to_dir().is_empty()? { if e.is_dir() && !e.to_dir().is_empty()? {
return Err(io::Error::new(ErrorKind::NotFound, "removing non-empty directory is denied")); return Err(io::Error::new(ErrorKind::NotFound, "removing non-empty directory is denied"));
} }
// free directory data // free data
if let Some(n) = e.first_cluster() { if let Some(n) = e.first_cluster() {
self.fs.free_cluster_chain(n)?; self.fs.free_cluster_chain(n)?;
} }
@ -237,8 +240,6 @@ impl <'a, 'b> Dir<'a, 'b> {
} }
Ok(()) Ok(())
} }
}
}
/// Renames or moves existing file or directory. /// Renames or moves existing file or directory.
/// ///
@ -464,12 +465,14 @@ impl <'a, 'b> Iterator for DirIter<'a, 'b> {
} }
fn validate_long_name(name: &str) -> io::Result<()> { fn validate_long_name(name: &str) -> io::Result<()> {
// check if length is valid
if name.len() == 0 { if name.len() == 0 {
return Err(io::Error::new(ErrorKind::InvalidInput, "filename cannot be empty")); return Err(io::Error::new(ErrorKind::InvalidInput, "filename cannot be empty"));
} }
if name.len() > 255 { if name.len() > 255 {
return Err(io::Error::new(ErrorKind::InvalidInput, "filename is too long")); return Err(io::Error::new(ErrorKind::InvalidInput, "filename is too long"));
} }
// check if there are only valid characters
for c in name.chars() { for c in name.chars() {
match c { match c {
'a'...'z' | 'A'...'Z' | '0'...'9' | '\u{80}'...'\u{FFFF}' | 'a'...'z' | 'A'...'Z' | '0'...'9' | '\u{80}'...'\u{FFFF}' |