hadoop_common/fs/
file_system.rs

1use super::{
2    common_configuration_keys, common_configuration_keys_public, permission::FsPermission, Path,
3};
4use crate::conf::Configuration;
5use anyhow::Error;
6use iref::Iri;
7use std::any::type_name;
8
9pub const FS_DEFAULT_NAME_KEY: &str = common_configuration_keys::FS_DEFAULT_NAME_KEY;
10pub const DEFAULT_FS: &str = common_configuration_keys::FS_DEFAULT_NAME_DEFAULT;
11/// Prefix for trash directory.
12pub const TRASH_PREFIX: &str = ".Trash";
13pub const USER_HOME_PREFIX: &str = "/user";
14
15/// An abstract base class for a fairly generic filesystem.  It
16/// may be implemented as a distributed filesystem, or as a "local"
17/// one that reflects the locally-connected disk.  The local version
18/// exists for small Hadoop instances and for testing.
19pub trait FileSystem {
20    type FileSystemImpl: FileSystem;
21
22    fn new(uri: &Iri, conf: &Configuration) -> anyhow::Result<Self::FileSystemImpl>;
23
24    fn get_resolve_symlinks(conf: &Configuration) -> bool {
25        conf.get_bool(
26            common_configuration_keys_public::FS_CLIENT_RESOLVE_REMOTE_SYMLINKS_KEY,
27            common_configuration_keys_public::FS_CLIENT_RESOLVE_REMOTE_SYMLINKS_DEFAULT,
28        )
29    }
30
31    /// Get the default FileSystem URI from a configuration.
32    fn get_default_uri(conf: &Configuration) -> anyhow::Result<Iri> {
33        Ok(Iri::new(conf.get_trimmed_with_default(
34            common_configuration_keys::FS_DEFAULT_NAME_KEY,
35            common_configuration_keys::FS_DEFAULT_NAME_DEFAULT,
36        ))?)
37    }
38
39    /// Should symbolic links be resolved by `FileSystemLinkResolver`.
40    fn resolve_symlinks(&self) -> bool;
41
42    /// Return the protocol scheme for this FileSystem.
43    fn get_scheme(&self) -> anyhow::Result<&str> {
44        Err(Error::msg(format!(
45            "Not implemented by the {} FileSystem implementation",
46            type_name::<Self>().split("::").last().unwrap_or_default()
47        )))
48    }
49
50    /// Returns a URI which identifies this FileSystem.
51    fn get_uri(&self) -> Iri;
52
53    /// Return the current user's home directory in this FileSystem.
54    /// The default implementation returns `"/user/$USER/"`.
55    fn get_home_directory(&self) -> anyhow::Result<Path>;
56
57    /// Get the current working directory for the given FileSystem
58    fn get_working_directory(&self) -> &Path;
59
60    /// Make the given file and all non-existent parents into
61    /// directories. Has roughly the semantics of Unix `mkdir -p`.
62    /// Existence of the directory hierarchy is not an error.
63    fn mkdirs(&self, f: &Path, permission: Option<&FsPermission>) -> anyhow::Result<bool>;
64
65    /// See [`FileContext#fix_relative_part`]
66    fn fix_relative_part(&self, p: &Path) -> anyhow::Result<Path> {
67        if p.is_uri_path_absolute() {
68            Ok(p.clone())
69        } else {
70            Path::from_parent(self.get_working_directory(), p)
71        }
72    }
73}