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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
//! A library to interface with libatasmart-sys.  For more information about libatasmart-sys see
//! [libatasmart-sys](https://github.com/cholcombe973/libatasmart-sys)
//! This library is useful for gathering ata smart information from your hard drives concerning
//! their remaining lifetime.  The underlying libatasmart doesn't expose every possible metric like
//! smartmontools but it does expose a few important ones like bad sector count and overall status.
//! This also has the advantage of avoiding CLI calls and scraping the text output which makes it
//! more reliable and also a lot more performant!
//!

extern crate libatasmart_sys;
extern crate nix;

use libatasmart_sys::*;
use nix::errno::Errno;
use std::ffi::CString;
use std::path::{Path, PathBuf};

/*
#[cfg(test)]
mod tests{
    use std::path::Path;
    use super::*;

    #[test]
    fn test_smart(){
        let mut disk = Disk::new(Path::new("/dev/sda")).unwrap();
        let ret = disk.get_smart_status();
        println!("Smart status: {:?}", ret);
        println!("Dumping disk stats");
        let ret = disk.dump();
    }
}
*/

/// Our ata smart disk
pub struct Disk{
    /// The path in the filesystem to the hard drive
    pub disk: PathBuf,
    skdisk: *mut SkDisk,
}

impl Disk{
    /// This will initialize a new Disk by asking libatasmart to open it.
    /// Note that this requires root permissions usually to succeed.
    pub fn new(disk_path: &Path) -> Result<Disk, String>{
        let device = CString::new(disk_path.to_str().unwrap()).unwrap();
        let mut disk: *mut SkDisk = unsafe { std::mem::uninitialized() };

        unsafe{
            let ret = libatasmart_sys::sk_disk_open(device.as_ptr(), &mut disk);
            if ret < 0{
                let fail = nix::errno::errno();
                return Err(Errno::from_i32(fail).desc().to_string());
            }

            let ret = libatasmart_sys::sk_disk_smart_read_data(disk);
            if ret < 0{
                let fail = nix::errno::errno();
                return Err(Errno::from_i32(fail).desc().to_string());
            }
            Ok(Disk{
                disk: disk_path.to_path_buf(),
                skdisk: disk,
            })
        }
    }


    /// Returns a u64 representing the size of the disk in bytes.
    pub fn get_disk_size(&mut self)->Result<u64, String>{
        unsafe{
            let mut bytes: u64 = 0;
            let ret = sk_disk_get_size(self.skdisk, &mut bytes);
            if ret < 0{
                let fail = nix::errno::errno();
                return Err(Errno::from_i32(fail).desc().to_string());
            }
            return Ok(bytes);
        }
    }

    /// Returns a bool of true if sleep mode is supported, false otherwise.
    pub fn check_sleep_mode(&mut self) -> Result<bool,String> {
        unsafe{
            let mut mode: SkBool = 0;
            let ret = sk_disk_check_sleep_mode(self.skdisk, &mut mode);
            if ret < 0{
                let fail = nix::errno::errno();
                return Err(Errno::from_i32(fail).desc().to_string());
            }
            if mode == 0{
                Ok(false)
            }else{
                Ok(true)
            }
        }
    }

    /// Returns a u64 representing the power on time in milliseconds
    pub fn get_power_on(&mut self) -> Result<u64, String>{
        unsafe{
            let mut power_on_time:u64 = 0;
            let ret = sk_disk_smart_get_power_on(self.skdisk, &mut power_on_time);
            if ret < 0{
                let fail = nix::errno::errno();
                return Err(Errno::from_i32(fail).desc().to_string());
            }
            Ok(power_on_time)
        }
    }

    /// Returns a u64 representing the number of power on cycles
    pub fn get_power_cycle_count(&mut self) -> Result<u64,String> {
        unsafe{
            let mut power_cycle_count:u64 = 0;
            let ret = sk_disk_smart_get_power_cycle(self.skdisk, &mut power_cycle_count);
            if ret < 0{
                let fail = nix::errno::errno();
                return Err(Errno::from_i32(fail).desc().to_string());
            }
            Ok(power_cycle_count)
        }
    }

    /// Returns a u64 representing the number of bad sections on the disk
    pub fn get_bad_sectors(&mut self) -> Result<u64,String> {
        unsafe{
            let mut bad_sector_count: u64 = 0;
            let ret = sk_disk_smart_get_bad(self.skdisk, &mut bad_sector_count);
            if ret < 0{
                let fail = nix::errno::errno();
                return Err(Errno::from_i32(fail).desc().to_string());
            }
            Ok(bad_sector_count)
        }
    }

    /// Returns a u64 representing the mkelvin of the disk
    pub fn get_temperature(&mut self) -> Result<u64,String> {
        unsafe{
            let mut mkelvin: u64 = 0;
            let ret = sk_disk_smart_get_temperature(self.skdisk, &mut mkelvin);
            if ret < 0{
                let fail = nix::errno::errno();
                return Err(Errno::from_i32(fail).desc().to_string());
            }
            Ok(mkelvin)
        }
    }

    /// Returns true if the disk passed smart, false otherwise.
    pub fn get_smart_status(&mut self) -> Result<bool,String> {
        unsafe{
            let mut good: SkBool = 0;
            let ret = sk_disk_smart_status(self.skdisk, &mut good);
            if ret < 0{
                let fail = nix::errno::errno();
                return Err(Errno::from_i32(fail).desc().to_string());
            }
            if good == 0{
                Ok(false)
            }else{
                Ok(true)
            }
        }
    }

    /// This will dump all available information to stdout about the drive
    pub fn dump(&mut self)->Result<(), String>{
        unsafe{
            let ret = sk_disk_dump(self.skdisk);
            if ret < 0{
                let fail = nix::errno::errno();
                return Err(Errno::from_i32(fail).desc().to_string());
            }
            Ok(())
        }
    }

    pub fn identify_is_available(&mut self)->Result<bool, String>{
        unsafe{
            let mut available: SkBool = 0;
            let ret = sk_disk_identify_is_available(self.skdisk, &mut available);
            if ret < 0{
                let fail = nix::errno::errno();
                return Err(Errno::from_i32(fail).desc().to_string());
            }
            if available == 0{
                Ok(false)
            }else{
                Ok(true)
            }
        }
    }

    /// Query the device and return whether or not smart is supported on it
    pub fn smart_is_available(&mut self)->Result<bool, String>{
        unsafe{
            let mut available: SkBool = 0;
            let ret = sk_disk_smart_is_available(self.skdisk, &mut available);
            if ret < 0{
                let fail = nix::errno::errno();
                return Err(Errno::from_i32(fail).desc().to_string());
            }
            if available == 0{
                Ok(false)
            }else{
                Ok(true)
            }
        }
    }

    pub fn execute_smart_self_test(&mut self, test_type: SkSmartSelfTest)->Result<(), String>{
        unsafe{
            let ret = sk_disk_smart_self_test(self.skdisk, test_type);
            if ret < 0{
                let fail = nix::errno::errno();
                return Err(Errno::from_i32(fail).desc().to_string());
            }
            Ok(())
        }
    }

    pub fn smart_get_overall(&mut self)->Result<SkSmartOverall, String>{
        unsafe{
            let mut overall: SkSmartOverall = SkSmartOverall::SK_SMART_OVERALL_GOOD;
            let ret = sk_disk_smart_get_overall(self.skdisk, &mut overall);
            if ret < 0{
                let fail = nix::errno::errno();
                return Err(Errno::from_i32(fail).desc().to_string());
            }
            Ok(overall)
        }
    }
}

impl Drop for Disk {
	fn drop(&mut self){
        unsafe{
            sk_disk_free(self.skdisk);
        }
    }
}

/*
pub fn sk_smart_self_test_execution_status_to_string(status: SkSmartSelfTestExecutionStatus) -> *const ::libc::c_char;
pub fn sk_smart_offline_data_collection_status_to_string(status: SkSmartOfflineDataCollectionStatus) -> *const ::libc::c_char;
pub fn sk_smart_self_test_to_string(test: SkSmartSelfTest) -> *const ::libc::c_char;
pub fn sk_smart_self_test_polling_minutes(d: *const SkSmartParsedData, test: SkSmartSelfTest) -> uint32_t;

pub fn sk_smart_self_test_available(d: *const SkSmartParsedData, test: SkSmartSelfTest) -> SkBool;
pub fn sk_disk_identify_parse(d: *mut *mut SkDisk, data: *const SkIdentifyParsedData) -> ::libc::c_int;
pub fn sk_disk_smart_read_data(d: *mut SkDisk) -> ::libc::c_int;
pub fn sk_disk_get_blob(d: *mut *mut SkDisk, blob: *const ::libc::c_void, size: *mut size_t) -> ::libc::c_int;
pub fn sk_disk_set_blob(d: *mut SkDisk, blob: *const ::libc::c_void, size: size_t) -> ::libc::c_int;
pub fn sk_disk_smart_parse(d: *mut *mut SkDisk, data: *const SkSmartParsedData) -> ::libc::c_int;
*/