Skip to content

Commit

Permalink
canonicalize path when opening MmapDirectory (#1231)
Browse files Browse the repository at this point in the history
* canonicalize path when opening `MmapDirectory`
fixes #1229
  • Loading branch information
saroh authored Dec 9, 2021
1 parent 098eea8 commit c980b19
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
10 changes: 10 additions & 0 deletions src/directory/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ pub enum OpenDirectoryError {
},
}

impl OpenDirectoryError {
/// Wraps an io error.
pub fn wrap_io_error(io_error: io::Error, directory_path: PathBuf) -> Self {
Self::IoError {
io_error,
directory_path,
}
}
}

/// Error that may occur when starting to write in a file
#[derive(Debug, Error)]
pub enum OpenWriteError {
Expand Down
17 changes: 10 additions & 7 deletions src/directory/mmap_directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,19 @@ impl MmapDirectory {
pub fn open<P: AsRef<Path>>(directory_path: P) -> Result<MmapDirectory, OpenDirectoryError> {
let directory_path: &Path = directory_path.as_ref();
if !directory_path.exists() {
Err(OpenDirectoryError::DoesNotExist(PathBuf::from(
return Err(OpenDirectoryError::DoesNotExist(PathBuf::from(
directory_path,
)))
} else if !directory_path.is_dir() {
Err(OpenDirectoryError::NotADirectory(PathBuf::from(
)));
}
let canonical_path: PathBuf = directory_path.canonicalize().map_err(|io_err| {
OpenDirectoryError::wrap_io_error(io_err, PathBuf::from(directory_path))
})?;
if !canonical_path.is_dir() {
return Err(OpenDirectoryError::NotADirectory(PathBuf::from(
directory_path,
)))
} else {
Ok(MmapDirectory::new(PathBuf::from(directory_path), None))
)));
}
Ok(MmapDirectory::new(canonical_path, None))
}

/// Joins a relative_path to the directory `root_path`
Expand Down

0 comments on commit c980b19

Please sign in to comment.