Skip to content

Commit

Permalink
guarantee io::ErrorKind::InvalidInput for non-table files
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasf committed Jun 17, 2024
1 parent fc019de commit 4a2719a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ pub trait Filesystem: Sync + Send {
///
/// # Errors
///
/// See [`std::fs::metadata()`]. Additionally errors if `path` does not
/// See [`std::fs::metadata()`]. Additionally errors with
/// [`std::io::ErrorKind::InvalidInput`] if `path` does not
/// ultimately point to a regular file.
fn regular_file_size(&self, path: &Path) -> io::Result<u64>;

Expand Down Expand Up @@ -206,7 +207,7 @@ fn regular_file_size_impl(path: &Path) -> io::Result<u64> {
let meta = path.metadata()?;
if !meta.is_file() {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
io::ErrorKind::InvalidInput,
"not a regular file",
));
}
Expand Down
11 changes: 8 additions & 3 deletions src/tablebase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,20 @@ impl<S: Position + Clone + Syzygy> Tablebase<S> {
///
/// Returns an error result when:
///
/// * The `path` does not exist.
/// * `path` is not pointing to a directory.
/// * Listing the directory fails (no permission, ...).
/// * Querying meta data for a table in `path` failed. Some tables
/// maybe have already been added.
/// * There is a table in `path` where the file size indicates that
/// it must be corrupted. Some tables may have already been added.
pub fn add_directory<P: AsRef<Path>>(&mut self, path: P) -> io::Result<usize> {
let mut num = 0;

for entry in self.filesystem.read_dir(path.as_ref())? {
if self.add_file_impl(&entry).is_ok() {
num += 1;
match self.add_file_impl(&entry) {
Ok(()) => num += 1,
Err(err) if err.kind() == io::ErrorKind::InvalidInput => (), // Not a table. Ignore
Err(err) => return Err(err),
}
}

Expand Down

0 comments on commit 4a2719a

Please sign in to comment.