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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
//! 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!
//!

use libatasmart_sys::*;
use nix::errno::Errno;
use std::{ffi::{CString, CStr}, path::{Path, PathBuf}, mem::MaybeUninit, ptr::null};
pub use libatasmart_sys::SkSmartSelfTest;
pub extern crate nix;

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

    #[test]
    fn test_new_failure() {
        match Disk::new(Path::new("/dev/null")) {
            Ok(_) => panic!("Opening /dev/null succeeded"),
            Err(e) => assert_eq!(Errno::ENODEV, e),
        }
    }
    /*
    #[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,
}

#[derive(Debug)]
pub struct IdentifyParsedData{
    pub serial: String,
    pub firmware: String,
    pub model: String,
}

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, Errno> {
        let device = CString::new(disk_path.to_str().unwrap()).unwrap();
        let mut disk = MaybeUninit::<SkDisk>::uninit().as_mut_ptr();

        unsafe {
            let ret = libatasmart_sys::sk_disk_open(device.as_ptr(), &mut disk);
            if ret < 0 {
                // Do not call sk_disk_free here, sk_disk_open already did that.
                let fail = nix::errno::errno();
                return Err(Errno::from_i32(fail));
            }

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

    /// Refreshes cached SMART attribute values.
    ///
    /// SMART attribute values are read once in Disk::new and cached. Methods such as
    /// `get_temperature` use these cached values and do not access the disk. Call this method to
    /// refresh the cached values.
    ///
    /// Note: calling this method might cause the disk to wake up from sleep. Consider checking if
    /// the disk is asleep using `check_sleep_mode` before calling this method to avoid this.
    pub fn refresh_smart_data(&mut self) -> Result<(), Errno> {
        unsafe {
            let ret = sk_disk_smart_read_data(self.skdisk);
            if ret < 0 {
                let fail = nix::errno::errno();
                return Err(Errno::from_i32(fail));
            }
            Ok(())
        }
    }

    /// Returns a u64 representing the size of the disk in bytes.
    pub fn get_disk_size(&mut self) -> Result<u64, Errno> {
        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));
            }
            return Ok(bytes);
        }
    }

    /// Returns a bool of true if sleep mode is supported, false otherwise.
    pub fn check_sleep_mode(&mut self) -> Result<bool, Errno> {
        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));
            }
            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, Errno> {
        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));
            }
            Ok(power_on_time)
        }
    }

    /// Returns a u64 representing the number of power on cycles
    pub fn get_power_cycle_count(&mut self) -> Result<u64, Errno> {
        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));
            }
            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, Errno> {
        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));
            }
            Ok(bad_sector_count)
        }
    }

    /// Returns a u64 representing the mkelvin of the disk
    pub fn get_temperature(&mut self) -> Result<u64, Errno> {
        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));
            }
            Ok(mkelvin)
        }
    }

    /// Returns true if the disk passed smart, false otherwise.
    pub fn get_smart_status(&mut self) -> Result<bool, Errno> {
        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));
            }
            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<(), Errno> {
        unsafe {
            let ret = sk_disk_dump(self.skdisk);
            if ret < 0 {
                let fail = nix::errno::errno();
                return Err(Errno::from_i32(fail));
            }
            Ok(())
        }
    }

    pub fn identify_is_available(&mut self) -> Result<bool, Errno> {
        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));
            }
            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, Errno> {
        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));
            }
            if available == 0 {
                Ok(false)
            } else {
                Ok(true)
            }
        }
    }

    // This is a lower level function that is used to build new smart functions
    pub fn parse_attributes(&mut self, parser_callback: extern "C" fn(*mut SkDisk, *const SkSmartAttributeParsedData, *mut std::ffi::c_void)) -> Result<(), Errno> 
    {
        unsafe {
            let ret = sk_disk_smart_parse_attributes(self.skdisk, parser_callback, std::ptr::null_mut());
            if ret < 0 {
                let fail = nix::errno::errno();
                return Err(Errno::from_i32(fail));
            }
            Ok(())
        }
    }

    /// Query the device and return whether or not a particular smart test is supported on it
    pub fn smart_test_available(
        &mut self,
        test_attributes: &mut SkSmartParsedData,
        test_type: SkSmartSelfTest,
    ) -> Result<bool, Errno> {
        unsafe {
            let available = sk_smart_self_test_available(test_attributes, test_type);
            if available == 0 {
                Ok(false)
            } else {
                Ok(true)
            }
        }
    }

    pub fn execute_smart_self_test(&mut self, test_type: SkSmartSelfTest) -> Result<(), Errno> {
        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));
            }
            Ok(())
        }
    }

    pub fn smart_get_overall(&mut self) -> Result<SkSmartOverall, Errno> {
        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));
            }
            Ok(overall)
        }
    }

    /// Get the model, firmware, and serial of the disk as a IdentifyParsedDatastruct
    /// If Errno::EINVAL gets returned there is a problem with the C string parser
    pub fn identify_parse(&mut self) -> Result<IdentifyParsedData, Errno> {
        let mut available: SkBool = 0;
        unsafe {
            sk_disk_identify_is_available(self.skdisk, &mut available);
            if available == 1{
                let parsed_data_pointer: *const SkIdentifyParsedData = null();
                let ret = sk_disk_identify_parse(self.skdisk, &parsed_data_pointer);
                if ret < 0 {
                    let fail = nix::errno::errno();
                    return Err(Errno::from_i32(fail));
                }
                let model = CStr::from_ptr((*parsed_data_pointer).model.as_ptr()).to_str().map_err(|_| Errno::EINVAL)?;
                let firmware = CStr::from_ptr((*parsed_data_pointer).firmware.as_ptr()).to_str().map_err(|_| Errno::EINVAL)?;
                let serial = CStr::from_ptr((*parsed_data_pointer).serial.as_ptr()).to_str().map_err(|_| Errno::EINVAL)?;
                
                let parsed_data: IdentifyParsedData = IdentifyParsedData {
                    serial: String::from(serial),
                    firmware: String::from(firmware),
                    model: String::from(model)
                };
                Ok(parsed_data)
            }
            else{
                let fail = nix::errno::errno();
                return Err(Errno::from_i32(fail));
            }
        }
    }
}

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

/// Helper fn. I believe this function returns how many minutes it takes to run a particular type of smart test
/// but it's not entirely clear and the original source code doesn't have a comment
pub fn smart_test_polling_minutes(test_attributes: &SkSmartParsedData, test: SkSmartSelfTest) -> u32 {
    unsafe {
        sk_smart_self_test_polling_minutes(test_attributes, test)
    }
}

/// Helper fn. Transforms a SkSmartSelfTest into a String
pub fn smart_test_to_string(test: SkSmartSelfTest) -> String {
    unsafe {
        let str_ptr = sk_smart_self_test_to_string(test);
        let c_str = CStr::from_ptr(str_ptr);
        c_str.to_string_lossy().into_owned()
    }
}

/// Helper fn. Transforms an SkSmartOfflineDataCollectionStatus into a String
pub fn get_offline_collection_status_as_string(status: SkSmartOfflineDataCollectionStatus) -> String {
    unsafe {
        let str_ptr = sk_smart_offline_data_collection_status_to_string(status);
        let c_str = CStr::from_ptr(str_ptr);
        c_str.to_string_lossy().into_owned()
    }
}

/// Helper fn. Transforms an SkSmartSelfTestExecutionStatus into a String
pub fn get_smart_status_as_string(status: SkSmartSelfTestExecutionStatus) -> String {
    unsafe {
        let str_ptr = sk_smart_self_test_execution_status_to_string(status);
        let c_str = CStr::from_ptr(str_ptr);
        c_str.to_string_lossy().into_owned()
    }
}


/*
pub fn sk_disk_identify_parse(d: *mut *mut SkDisk, data: *const SkIdentifyParsedData) -> ::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;
*/