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
extern crate libatasmart_sys;
extern crate nix;
use libatasmart_sys::*;
use nix::errno::Errno;
use std::ffi::CString;
use std::path::{Path, PathBuf};
pub struct Disk{
pub disk: PathBuf,
skdisk: *mut SkDisk,
}
impl Disk{
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();
sk_disk_free(disk);
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();
sk_disk_free(disk);
return Err(Errno::from_i32(fail).desc().to_string());
}
Ok(Disk{
disk: disk_path.to_path_buf(),
skdisk: disk,
})
}
}
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);
}
}
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)
}
}
}
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)
}
}
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)
}
}
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)
}
}
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)
}
}
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)
}
}
}
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)
}
}
}
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);
}
}
}