summaryrefslogtreecommitdiffstats
path: root/src/filesystems/mount_list.rs
blob: 03e044e4c278605a68d9291badd1a78dbf3aa192 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use {
    crate::{
        errors::ProgramError,
    },
    lfs_core::{
        DeviceId,
        Mount,
        read_mounts,
        ReadOptions,
    },
};

pub struct MountList {
    mounts: Option<Vec<Mount>>,
}

impl MountList {
    pub const fn new() -> Self {
        Self { mounts: None }
    }
    pub fn clear_cache(&mut self) {
        self.mounts = None;
    }
    /// try to load the mounts if they aren't loaded.
    pub fn load(&mut self) -> Result<&Vec<Mount>, ProgramError> {
        if self.mounts.is_none() {
            let mut options = ReadOptions::default();
            options.remote_stats(false);
            match read_mounts(&options) {
                Ok(mut vec) => {
                    debug!("{} mounts loaded", vec.len());
                    vec.sort_by_key(|m| {
                        let size = m.stats().map_or(0, |s| s.size());
                        u64::MAX - size
                    });
                    self.mounts = Some(vec);
                }
                Err(e) => {
                    warn!("Failed to load mounts: {:?}", e);
                    return Err(ProgramError::Lfs {
                        details: e.to_string(),
                    });
                }
            }
        }
        Ok(
            // this unwrap will be fixed as soon as there's option.insert in stable
            self.mounts.as_ref().unwrap()
        )
    }
    pub fn get_by_device_id(&self, dev: DeviceId) -> Option<&Mount> {
        self.mounts.as_ref()
            .and_then(|mounts| mounts.iter().find(|m| m.info.dev == dev))
    }
}