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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//! Provides easy access to data from the `/proc/swaps` and `/proc/mounts` files.
//!
//! ```rust,no_run
//! extern crate proc_mounts;
//! 
//! use proc_mounts::{MountIter, SwapIter};
//! use std::io;
//! 
//! fn main() -> io::Result<()> {
//!     println!("# Active Mounts");
//!     for mount in MountIter::new()? {
//!         println!("{:#?}", mount);
//!     }
//! 
//!     println!("# Active Swaps");
//!     for swap in SwapIter::new()? {
//!         println!("{:#?}", swap);
//!     }
//! 
//!     Ok(())
//! }
//! ```

#[macro_use]
extern crate lazy_static;
extern crate partition_identity;

mod mounts;
mod swaps;

use std::collections::hash_map::DefaultHasher;

use std::fs::File;
use std::hash::{Hash, Hasher};
use std::io::{self, Read};
use std::path::Path;
use std::sync::{Arc, RwLock};
use std::thread;
use std::time::Duration;

pub use self::mounts::*;
pub use self::swaps::*;

lazy_static! {
    /// Static list of mounts that is dynamically updated in the background.
    pub static ref MOUNTS: Arc<RwLock<MountList>> = {
        let mounts = Arc::new(RwLock::new(MountList::new().unwrap()));
        watch_and_set(mounts.clone(), "/proc/mounts", || MountList::new().ok());
        mounts
    };
}

lazy_static! {
    /// Static list of swap points that is dynamically updated in the background.
    pub static ref SWAPS: Arc<RwLock<SwapList>> = {
        let swaps = Arc::new(RwLock::new(SwapList::new().unwrap()));
        watch_and_set(swaps.clone(), "/proc/swaps", || SwapList::new().ok());
        swaps
    };
}

fn watch_and_set<T: 'static + Send + Sync>(
    swaps: Arc<RwLock<T>>,
    file: &'static str,
    create_new: fn() -> Option<T>
) {
    thread::spawn(move || {
        let buffer: &mut [u8] = &mut [0u8; 8 * 1024];
        let modified = &mut get_file_hash(file, buffer).expect("hash could not be obtained");

        loop {
            thread::sleep(Duration::from_secs(1));
            modify_if_changed(&swaps, modified, buffer, file, create_new);
        }
    });
}

fn modify_if_changed<T: 'static + Send + Sync>(
    swaps: &Arc<RwLock<T>>,
    modified: &mut u64,
    buffer: &mut [u8],
    file: &'static str,
    create_new: fn() -> Option<T>
) {
    if let Ok(new_modified) = get_file_hash(file, buffer) {
        if new_modified != *modified {
            *modified = new_modified;
            if let Ok(ref mut swaps) = swaps.write() {
                if let Some(new_swaps) = create_new() {
                    **swaps = new_swaps;
                }
            }
        }
    }
}

fn get_file_hash<P: AsRef<Path>>(path: P, buffer: &mut [u8]) -> io::Result<u64> {
    let mut file = open(path)?;
    let hasher = &mut DefaultHasher::new();
    while let Ok(read) = file.read(buffer) {
        if read == 0 {
            break;
        }
        buffer[..read].hash(hasher);
    }
    Ok(hasher.finish())
}

fn open<P: AsRef<Path>>(path: P) -> io::Result<File> {
    File::open(&path).map_err(|why| io::Error::new(
        io::ErrorKind::Other,
        format!("unable to open file at {:?}: {}", path.as_ref(), why)
    ))
}