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
use regex::Regex;
use super::data::InitiatorType;
use super::error::*;
struct SizeUnit<'a> {
unit: &'a str,
bytes: u64,
}
const SIZE_CONVS: [SizeUnit<'static>; 6] = [
SizeUnit {
unit: "EiB",
bytes: 1u64 << 60,
},
SizeUnit {
unit: "PiB",
bytes: 1u64 << 50,
},
SizeUnit {
unit: "TiB",
bytes: 1u64 << 40,
},
SizeUnit {
unit: "GiB",
bytes: 1u64 << 30,
},
SizeUnit {
unit: "MiB",
bytes: 1u64 << 20,
},
SizeUnit {
unit: "KiB",
bytes: 1u64 << 10,
},
];
const EXTRA_SIZE_CONVS: [SizeUnit<'static>; 7] = [
SizeUnit {
unit: "B",
bytes: 1u64,
},
SizeUnit {
unit: "KB",
bytes: 1_000u64,
},
SizeUnit {
unit: "MB",
bytes: 1_000_000u64,
},
SizeUnit {
unit: "GB",
bytes: 1_000_000_000u64,
},
SizeUnit {
unit: "TB",
bytes: 1_000_000_000_000u64,
},
SizeUnit {
unit: "PB",
bytes: 1_000_000_000_000_000u64,
},
SizeUnit {
unit: "EB",
bytes: 1_000_000_000_000_000_000u64,
},
];
pub fn size_human_2_size_bytes(s: &str) -> u64 {
let regex_size_human = match Regex::new(
r"(?x)
^
([0-9\.]+) # 1: number
[\ \t]* # might have space between number and unit
([a-zA-Z]*) # 2: units
$
",
) {
Ok(r) => r,
Err(_) => return 0,
};
let cap = match regex_size_human.captures(s) {
Some(c) => c,
None => return 0,
};
let number = match cap.get(1) {
Some(n) => n.as_str().parse::<f64>().unwrap_or(0f64),
None => return 0,
};
let unit = match cap.get(2) {
Some(u) => {
let tmp = u.as_str().to_uppercase();
println!("tmp: {}", tmp);
if !tmp.ends_with('B') {
format!("{}IB", tmp)
} else {
tmp
}
}
None => return 0,
};
for size_conv in &SIZE_CONVS {
if size_conv.unit.to_uppercase() == unit {
return (size_conv.bytes as f64 * number) as u64;
}
}
for size_conv in &EXTRA_SIZE_CONVS {
if size_conv.unit == unit {
return (size_conv.bytes as f64 * number) as u64;
}
}
0
}
pub fn size_bytes_2_size_human(i: u64) -> String {
let mut unit = "B";
let mut num: f64 = 0f64;
for size_conv in &SIZE_CONVS {
if i >= size_conv.bytes {
num = (i as f64) / (size_conv.bytes as f64);
unit = size_conv.unit;
break;
}
}
if num == 0f64 {
num = i as f64;
}
format!("{:.2}{}", num, unit)
}
pub(crate) fn verify_init_id_str(init_id: &str, init_type: InitiatorType) -> Result<()> {
let valid: bool = match init_type {
InitiatorType::Wwpn => {
let regex_wwpn = Regex::new(
r"(?x)
^(?:0x|0X)?(?:[0-9A-Fa-f]{2})
(?:(?:[\.:\-])?[0-9A-Fa-f]{2}){7}$
",
)?;
regex_wwpn.is_match(init_id)
}
InitiatorType::IscsiIqn => {
init_id.starts_with("iqn") || init_id.starts_with("eui") || init_id.starts_with("naa")
}
_ => {
return Err(LsmError::InvalidArgument(format!(
"Invalid init_type {}, should be \
InitiatorType::Wwpn or InitiatorType::IscsiIqn.",
init_type as i32
)))
}
};
if valid {
Ok(())
} else {
Err(LsmError::InvalidArgument(format!(
"Invalid initiator ID string '{}'",
init_id
)))
}
}