Skip to main content

file_storage/system/local/file/
exists.rs

1use crate::system::LocalPath;
2use crate::Error;
3use crate::Operation::Exists;
4use std::io::ErrorKind::NotFound;
5
6impl<'a> LocalPath<'a> {
7    //! Exists
8
9    /// See `FilePath::exists`.
10    pub fn exists(&self) -> Result<bool, Error> {
11        match std::fs::metadata(self.path) {
12            Ok(metadata) => {
13                if metadata.is_file() {
14                    Ok(true)
15                } else {
16                    let message: &str = if metadata.is_dir() {
17                        "the file path is a folder on the local file system"
18                    } else if metadata.is_symlink() {
19                        "the file path is a symlink on the local file system"
20                    } else {
21                        "the file path is an unknown entity on the local file system"
22                    };
23                    Err(Error::from_message(self.path.clone(), Exists, message))
24                }
25            }
26            Err(error) => {
27                if error.kind() == NotFound {
28                    Ok(false)
29                } else {
30                    Err(Error::from_source(self.path.clone(), Exists, error))
31                }
32            }
33        }
34    }
35}