Make errors more consistent with std::fs

This commit is contained in:
Rafał Harabień 2018-06-17 23:11:53 +02:00
parent 8995a43bc4
commit a6b66f9434
4 changed files with 21 additions and 26 deletions

View File

@ -126,7 +126,7 @@ impl <'a, T: ReadWriteSeek + 'a> Dir<'a, T> {
gen.add_existing(e.raw_short_name()); gen.add_existing(e.raw_short_name());
} }
} }
Err(io::Error::new(ErrorKind::NotFound, "file not found")) Err(io::Error::new(ErrorKind::NotFound, "No such file or directory"))
} }
/// Opens existing subdirectory. /// Opens existing subdirectory.
@ -250,7 +250,7 @@ impl <'a, T: ReadWriteSeek + 'a> Dir<'a, T> {
// in case of directory check if it is empty // in case of directory check if it is empty
let e = self.find_entry(name, None, None)?; let e = self.find_entry(name, None, None)?;
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::Other, "Directory not empty"));
} }
// free data // free data
if let Some(n) = e.first_cluster() { if let Some(n) = e.first_cluster() {
@ -306,7 +306,7 @@ impl <'a, T: ReadWriteSeek + 'a> Dir<'a, T> {
if e.is_same_entry(&r.unwrap()) { if e.is_same_entry(&r.unwrap()) {
return Ok(()); return Ok(());
} }
return Err(io::Error::new(ErrorKind::AlreadyExists, "destination file already exists")) return Err(io::Error::new(ErrorKind::AlreadyExists, "Destination file already exists"))
} }
// free long and short name entries // free long and short name entries
let mut stream = self.stream.clone(); let mut stream = self.stream.clone();
@ -522,10 +522,10 @@ impl <'a, T: ReadWriteSeek> Iterator for DirIter<'a, T> {
fn validate_long_name(name: &str) -> io::Result<()> { fn validate_long_name(name: &str) -> io::Result<()> {
// check if length is valid // 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::Other, "File name is 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::Other, "File name too long"));
} }
// check if there are only valid characters // check if there are only valid characters
for c in name.chars() { for c in name.chars() {
@ -533,7 +533,7 @@ fn validate_long_name(name: &str) -> io::Result<()> {
'a'...'z' | 'A'...'Z' | '0'...'9' | '\u{80}'...'\u{FFFF}' | 'a'...'z' | 'A'...'Z' | '0'...'9' | '\u{80}'...'\u{FFFF}' |
'$' | '%' | '\'' | '-' | '_' | '@' | '~' | '`' | '!' | '(' | ')' | '{' | '}' | '$' | '%' | '\'' | '-' | '_' | '@' | '~' | '`' | '!' | '(' | ')' | '{' | '}' |
'.' | ' ' | '+' | ',' | ';' | '=' | '[' | ']' => {}, '.' | ' ' | '+' | ',' | ';' | '=' | '[' | ']' => {},
_ => return Err(io::Error::new(ErrorKind::InvalidInput, "invalid character in filename")), _ => return Err(io::Error::new(ErrorKind::Other, "File name contains unsupported characters")),
} }
} }
Ok(()) Ok(())

View File

@ -291,7 +291,7 @@ impl<'a, T: ReadWriteSeek> Seek for File<'a, T> {
SeekFrom::End(x) => self.entry.iter().next().map_or(None, |e| e.inner().size()).expect("cannot seek from end if size is unknown") as i64 + x, SeekFrom::End(x) => self.entry.iter().next().map_or(None, |e| e.inner().size()).expect("cannot seek from end if size is unknown") as i64 + x,
}; };
if new_pos < 0 { if new_pos < 0 {
return Err(io::Error::new(ErrorKind::InvalidInput, "invalid seek")); return Err(io::Error::new(ErrorKind::InvalidInput, "Seek to a negative offset"));
} }
new_pos = match self.entry { new_pos = match self.entry {
Some(ref e) => { Some(ref e) => {

View File

@ -385,13 +385,13 @@ impl <T: ReadWriteSeek> FileSystem<T> {
let bpb = { let bpb = {
let boot = BootRecord::deserialize(&mut disk)?; let boot = BootRecord::deserialize(&mut disk)?;
if boot.boot_sig != [0x55, 0xAA] { if boot.boot_sig != [0x55, 0xAA] {
return Err(Error::new(ErrorKind::Other, "invalid signature")); return Err(Error::new(ErrorKind::Other, "Invalid boot sector signature"));
} }
boot.bpb boot.bpb
}; };
if bpb.fs_version != 0 { if bpb.fs_version != 0 {
return Err(Error::new(ErrorKind::Other, "unknown FS version")); return Err(Error::new(ErrorKind::Other, "Unknown FS version"));
} }
let total_sectors = let total_sectors =
@ -676,7 +676,7 @@ impl <'a, T: ReadWriteSeek> Seek for DiskSlice<'a, T> {
SeekFrom::End(x) => self.size as i64 + x, SeekFrom::End(x) => self.size as i64 + x,
}; };
if new_offset < 0 || new_offset as u64 > self.size { if new_offset < 0 || new_offset as u64 > self.size {
Err(io::Error::new(ErrorKind::InvalidInput, "invalid seek")) Err(io::Error::new(ErrorKind::InvalidInput, "Seek to a negative offset"))
} else { } else {
self.offset = new_offset as u64; self.offset = new_offset as u64;
Ok(self.offset) Ok(self.offset)

View File

@ -168,7 +168,7 @@ impl FatTrait for Fat12 {
} }
cluster += 1; cluster += 1;
if cluster == end_cluster { if cluster == end_cluster {
return Err(io::Error::new(io::ErrorKind::Other, "end of FAT reached")); return Err(io::Error::new(io::ErrorKind::Other, "No space left on device"));
} }
packed_val = match cluster & 1 { packed_val = match cluster & 1 {
0 => fat.read_u16::<LittleEndian>()?, 0 => fat.read_u16::<LittleEndian>()?,
@ -191,7 +191,6 @@ impl FatTrait for Fat12 {
_ => fat.read_u8().map(|n| n as u16), _ => fat.read_u8().map(|n| n as u16),
}; };
let packed_val = match res { let packed_val = match res {
Err(ref err) if err.kind() == io::ErrorKind::UnexpectedEof => break,
Err(err) => return Err(err), Err(err) => return Err(err),
Ok(n) => n, Ok(n) => n,
}; };
@ -247,7 +246,7 @@ impl FatTrait for Fat16 {
} }
cluster += 1; cluster += 1;
} }
Err(io::Error::new(io::ErrorKind::Other, "end of FAT reached")) Err(io::Error::new(io::ErrorKind::Other, "No space left on device"))
} }
fn count_free<T: ReadSeek>(fat: &mut T, end_cluster: u32) -> io::Result<u32> { fn count_free<T: ReadSeek>(fat: &mut T, end_cluster: u32) -> io::Result<u32> {
@ -255,11 +254,9 @@ impl FatTrait for Fat16 {
let mut cluster = RESERVED_FAT_ENTRIES; let mut cluster = RESERVED_FAT_ENTRIES;
fat.seek(io::SeekFrom::Start((cluster*2) as u64))?; fat.seek(io::SeekFrom::Start((cluster*2) as u64))?;
while cluster < end_cluster { while cluster < end_cluster {
match fat.read_u16::<LittleEndian>() { let val = fat.read_u16::<LittleEndian>()?;
Err(ref err) if err.kind() == io::ErrorKind::UnexpectedEof => break, if val == 0 {
Err(err) => return Err(err), count += 1;
Ok(0) => count += 1,
_ => {},
} }
cluster += 1; cluster += 1;
} }
@ -305,7 +302,7 @@ impl FatTrait for Fat32 {
} }
cluster += 1; cluster += 1;
} }
Err(io::Error::new(io::ErrorKind::Other, "end of FAT reached")) Err(io::Error::new(io::ErrorKind::Other, "No space left on device"))
} }
fn count_free<T: ReadSeek>(fat: &mut T, end_cluster: u32) -> io::Result<u32> { fn count_free<T: ReadSeek>(fat: &mut T, end_cluster: u32) -> io::Result<u32> {
@ -313,11 +310,9 @@ impl FatTrait for Fat32 {
let mut cluster = RESERVED_FAT_ENTRIES; let mut cluster = RESERVED_FAT_ENTRIES;
fat.seek(io::SeekFrom::Start((cluster*4) as u64))?; fat.seek(io::SeekFrom::Start((cluster*4) as u64))?;
while cluster < end_cluster { while cluster < end_cluster {
match fat.read_u32::<LittleEndian>() { let val = fat.read_u32::<LittleEndian>()? & 0x0FFFFFFF;
Err(ref err) if err.kind() == io::ErrorKind::UnexpectedEof => break, if val == 0 {
Err(err) => return Err(err), count += 1;
Ok(0) => count += 1,
_ => {},
} }
cluster += 1; cluster += 1;
} }
@ -410,7 +405,7 @@ mod tests {
assert_eq!(find_free_cluster(&mut cur, fat_type, 0x13, 0x20).unwrap(), 0x1B); assert_eq!(find_free_cluster(&mut cur, fat_type, 0x13, 0x20).unwrap(), 0x1B);
assert!(find_free_cluster(&mut cur, fat_type, 0x13, 0x14).is_err()); assert!(find_free_cluster(&mut cur, fat_type, 0x13, 0x14).is_err());
assert_eq!(count_free_clusters(&mut cur, fat_type, 0x20).unwrap(), 5); assert_eq!(count_free_clusters(&mut cur, fat_type, 0x1E).unwrap(), 5);
// test allocation // test allocation
assert_eq!(alloc_cluster(&mut cur, fat_type, None, Some(0x13), 0x1E).unwrap(), 0x1B); assert_eq!(alloc_cluster(&mut cur, fat_type, None, Some(0x13), 0x1E).unwrap(), 0x1B);
@ -418,7 +413,7 @@ mod tests {
assert_eq!(alloc_cluster(&mut cur, fat_type, Some(0x1B), None, 0x1E).unwrap(), 0x12); assert_eq!(alloc_cluster(&mut cur, fat_type, Some(0x1B), None, 0x1E).unwrap(), 0x12);
assert_eq!(read_fat(&mut cur, fat_type, 0x1B).unwrap(), FatValue::Data(0x12)); assert_eq!(read_fat(&mut cur, fat_type, 0x1B).unwrap(), FatValue::Data(0x12));
assert_eq!(read_fat(&mut cur, fat_type, 0x12).unwrap(), FatValue::EndOfChain); assert_eq!(read_fat(&mut cur, fat_type, 0x12).unwrap(), FatValue::EndOfChain);
assert_eq!(count_free_clusters(&mut cur, fat_type, 0x20).unwrap(), 3); assert_eq!(count_free_clusters(&mut cur, fat_type, 0x1E).unwrap(), 3);
// test reading from iterator // test reading from iterator
{ {
let iter = ClusterIterator::new(&mut cur, fat_type, 0x9); let iter = ClusterIterator::new(&mut cur, fat_type, 0x9);