Explorar el Código

refactor: Move ALL_MOUNTS to fs::mounts

Robert Minsk hace 2 años
padre
commit
6a8ad952e5
Se han modificado 4 ficheros con 36 adiciones y 82 borrados
  1. 1 1
      src/fs/file.rs
  2. 35 3
      src/fs/mounts/mod.rs
  3. 0 46
      src/lib.rs
  4. 0 32
      src/main.rs

+ 1 - 1
src/fs/file.rs

@@ -11,12 +11,12 @@ use chrono::prelude::*;
 
 use log::*;
 
-use crate::ALL_MOUNTS;
 use crate::fs::dir::Dir;
 use crate::fs::feature::xattr;
 use crate::fs::feature::xattr::{FileAttributes, Attribute};
 use crate::fs::fields as f;
 
+use super::mounts::ALL_MOUNTS;
 use super::mounts::MountedFs;
 
 

+ 35 - 3
src/fs/mounts/mod.rs

@@ -1,17 +1,22 @@
+use lazy_static::lazy_static;
+
+use std::collections::HashMap;
+use std::path::PathBuf;
+
 #[cfg(target_os = "linux")]
 mod linux;
 #[cfg(target_os = "macos")]
 mod macos;
 
 #[cfg(target_os = "linux")]
-pub use linux::mounts;
+use linux::mounts;
 #[cfg(target_os = "macos")]
-pub use macos::mounts;
+use macos::mounts;
 
 /// Details of a mounted filesystem.
 #[derive(Clone)]
 pub struct MountedFs {
-    pub dest: std::path::PathBuf,
+    pub dest: PathBuf,
     pub fstype: String,
     pub source: String,
 }
@@ -40,3 +45,30 @@ impl std::fmt::Display for Error {
         }
     }
 }
+
+// A lazily initialised static map of all mounted file systems.
+//
+// The map contains a mapping from the mounted directory path to the
+// corresponding mount information. If there's an error retrieving the mount
+// list or if we're not running on Linux or Mac, the map will be empty.
+//
+// Initialise this at application start so we don't have to look the details
+// up for every directory. Ideally this would only be done if the --mounts
+// option is specified which will be significantly easier once the move
+// to `clap` is complete.
+lazy_static! {
+    pub(crate) static ref ALL_MOUNTS: HashMap<PathBuf, MountedFs> = {
+        // Allow unused_mut for windows build
+        #[allow(unused_mut)]
+        let mut mount_map: HashMap<PathBuf, MountedFs> = HashMap::new();
+
+        #[cfg(any(target_os = "linux", target_os = "macos"))]
+        if let Ok(mounts)  = mounts() {
+            for mount in mounts {
+                mount_map.insert(mount.dest.clone(), mount);
+            }
+        }
+
+        mount_map
+    };
+}

+ 0 - 46
src/lib.rs

@@ -1,49 +1,3 @@
-#[macro_use]
-extern crate lazy_static;
-
-use crate::fs::mounts::MountedFs;
-
-#[cfg(target_os = "linux")]
-use proc_mounts::MountList;
-use std::collections::HashMap;
-use std::path::PathBuf;
-
-// A lazily initialised static map of all mounted file systems.
-//
-// The map contains a mapping from the mounted directory path to the
-// corresponding mount information. On Linux systems, this map is populated
-// using the `proc-mounts` crate. If there's an error retrieving the mount
-// list or if we're not running on Linux, the map will be empty.
-//
-// Initialise this at application start so we don't have to look the details
-// up for every directory. Ideally this would only be done if the --mounts
-// option is specified which will be significantly easier once the move
-// to `clap` is complete.
-lazy_static! {
-    static ref ALL_MOUNTS: HashMap<PathBuf, MountedFs> = {
-        #[cfg(target_os = "linux")]
-        match MountList::new() {
-            Ok(mount_list) => {
-                let mut m = HashMap::new();
-                mount_list.0.iter().for_each(|mount| {
-                    m.insert(
-                        mount.dest.clone(),
-                        MountedFs {
-                            dest: mount.dest.to_string_lossy().into_owned(),
-                            fstype: mount.fstype.clone(),
-                            source: mount.source.to_string_lossy().into(),
-                        },
-                    );
-                });
-                m
-            }
-            Err(_) => HashMap::new(),
-        }
-        #[cfg(not(target_os = "linux"))]
-        HashMap::new()
-    };
-}
-
 #[allow(unused)]
 pub mod fs;
 #[allow(unused)]

+ 0 - 32
src/main.rs

@@ -22,7 +22,6 @@
 #![allow(clippy::upper_case_acronyms)]
 #![allow(clippy::wildcard_imports)]
 
-use std::collections::HashMap;
 use std::env;
 use std::ffi::{OsStr, OsString};
 use std::io::{self, Write, ErrorKind};
@@ -33,10 +32,6 @@ use ansiterm::{ANSIStrings, Style};
 
 use log::*;
 
-#[macro_use]
-extern crate lazy_static;
-
-use crate::fs::mounts;
 use crate::fs::{Dir, File};
 use crate::fs::feature::git::GitCache;
 use crate::fs::filter::GitIgnore;
@@ -51,33 +46,6 @@ mod options;
 mod output;
 mod theme;
 
-// A lazily initialised static map of all mounted file systems.
-//
-// The map contains a mapping from the mounted directory path to the
-// corresponding mount information. If there's an error retrieving the mount
-// list or if we're not running on Linux or Mac, the map will be empty.
-//
-// Initialise this at application start so we don't have to look the details
-// up for every directory. Ideally this would only be done if the --mounts
-// option is specified which will be significantly easier once the move
-// to `clap` is complete.
-lazy_static! {
-    static ref ALL_MOUNTS: HashMap<PathBuf, mounts::MountedFs> = {
-        // Allow unused_mut for windows build
-        #[allow(unused_mut)]
-        let mut mount_map: HashMap<PathBuf, mounts::MountedFs> = HashMap::new();
-
-        #[cfg(any(target_os = "linux", target_os = "macos"))]
-        if let Ok(mounts)  = mounts::mounts() {
-            for mount in mounts {
-                mount_map.insert(mount.dest.clone(), mount);
-            }
-        }
-
-        mount_map
-    };
-}
-
 fn main() {
     #[cfg(unix)]
     unsafe {