forked from M-Labs/rust-fatfs
Improve code structure
This commit is contained in:
parent
ccb205c906
commit
632a371b0d
43
src/dir.rs
43
src/dir.rs
@ -120,7 +120,7 @@ impl <'a, 'b> Dir<'a, 'b> {
|
||||
let e = self.find_entry(name, None)?;
|
||||
match rest_opt {
|
||||
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)?;
|
||||
match rest_opt {
|
||||
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.
|
||||
pub fn create_file(&mut self, path: &str) -> io::Result<File<'a, 'b>> {
|
||||
// traverse path
|
||||
let (name, rest_opt) = split_path(path);
|
||||
match rest_opt {
|
||||
// path contains more than 1 component
|
||||
Some(rest) => self.find_entry(name, None)?.to_dir().create_file(rest),
|
||||
None => {
|
||||
if let Some(rest) = rest_opt {
|
||||
return self.find_entry(name, None)?.to_dir().create_file(rest);
|
||||
}
|
||||
// this is final filename in the path
|
||||
let mut short_name_gen = ShortNameGenerator::new(name);
|
||||
let r = self.find_entry(name, Some(&mut short_name_gen));
|
||||
match r {
|
||||
// file does not exist - create it
|
||||
Err(ref err) if err.kind() == ErrorKind::NotFound => {
|
||||
let short_name = short_name_gen.generate()?;
|
||||
let sfn_entry = self.create_sfn_entry(short_name, FileAttributes::from_bits_truncate(0), None);
|
||||
Ok(self.write_entry(name, sfn_entry)?.to_file())
|
||||
},
|
||||
// other error
|
||||
Err(err) => Err(err),
|
||||
// file already exists - return it
|
||||
Ok(e) => Ok(e.to_file()),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates new directory or opens existing.
|
||||
pub fn create_dir(&mut self, path: &str) -> io::Result<Self> {
|
||||
// traverse path
|
||||
let (name, rest_opt) = split_path(path);
|
||||
match rest_opt {
|
||||
// path contains more than 1 component
|
||||
Some(rest) => self.find_entry(name, None)?.to_dir().create_dir(rest),
|
||||
None => {
|
||||
if let Some(rest) = rest_opt {
|
||||
return self.find_entry(name, None)?.to_dir().create_dir(rest);
|
||||
}
|
||||
// this is final filename in the path
|
||||
let mut short_name_gen = ShortNameGenerator::new(name);
|
||||
let r = self.find_entry(name, Some(&mut short_name_gen));
|
||||
match r {
|
||||
// directory does not exist - create it
|
||||
Err(ref err) if err.kind() == ErrorKind::NotFound => {
|
||||
// alloc cluster for directory data
|
||||
let cluster = self.fs.alloc_cluster(None)?;
|
||||
@ -185,12 +187,12 @@ impl <'a, 'b> Dir<'a, 'b> {
|
||||
dir.write_entry("..", sfn_entry)?;
|
||||
Ok(dir)
|
||||
},
|
||||
// other error
|
||||
Err(err) => Err(err),
|
||||
// directory already exists - return it
|
||||
Ok(e) => Ok(e.to_dir()),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn is_empty(&mut self) -> io::Result<bool> {
|
||||
// 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
|
||||
/// can happen.
|
||||
pub fn remove(&mut self, path: &str) -> io::Result<()> {
|
||||
// traverse path
|
||||
let (name, rest_opt) = split_path(path);
|
||||
let e = self.find_entry(name, None)?;
|
||||
match rest_opt {
|
||||
Some(rest) => e.to_dir().remove(rest),
|
||||
None => {
|
||||
if let Some(rest) = rest_opt {
|
||||
return e.to_dir().remove(rest);
|
||||
}
|
||||
trace!("removing {}", path);
|
||||
// in case of directory check if it is empty
|
||||
if e.is_dir() && !e.to_dir().is_empty()? {
|
||||
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() {
|
||||
self.fs.free_cluster_chain(n)?;
|
||||
}
|
||||
@ -237,8 +240,6 @@ impl <'a, 'b> Dir<'a, 'b> {
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 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<()> {
|
||||
// check if length is valid
|
||||
if name.len() == 0 {
|
||||
return Err(io::Error::new(ErrorKind::InvalidInput, "filename cannot be empty"));
|
||||
}
|
||||
if name.len() > 255 {
|
||||
return Err(io::Error::new(ErrorKind::InvalidInput, "filename is too long"));
|
||||
}
|
||||
// check if there are only valid characters
|
||||
for c in name.chars() {
|
||||
match c {
|
||||
'a'...'z' | 'A'...'Z' | '0'...'9' | '\u{80}'...'\u{FFFF}' |
|
||||
|
Loading…
Reference in New Issue
Block a user