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
pub trait PathExt
{
#[cfg(unix)]
#[inline(always)]
fn to_c_string(&self) -> CString;
#[cfg(unix)]
#[inline(always)]
fn make_file_read_write_all(&self) -> io::Result<()>;
#[cfg(unix)]
#[inline(always)]
fn make_folder_searchable_to_all(&self) -> io::Result<()>;
#[inline(always)]
fn read_hexadecimal_value_with_prefix<P: Fn(&str) -> Result<T, ParseIntError>, T>(&self, size: usize, parser: P) -> io::Result<T>;
#[inline(always)]
fn read_hexadecimal_value_with_prefix_u16(&self) -> io::Result<u16>
{
self.read_hexadecimal_value_with_prefix(4, |raw_string| u16::from_str_radix(raw_string, 16))
}
#[inline(always)]
fn read_raw_string(&self) -> io::Result<String>;
#[inline(always)]
fn read_string_without_line_feed(&self) -> io::Result<String>;
#[inline(always)]
fn read_value<F>(&self) -> io::Result<F> where F: FromStr, <F as FromStr>::Err: 'static + Send + Sync + Error;
#[inline(always)]
fn write_value<D: Display>(&self, value: D) -> io::Result<()>;
#[inline(always)]
fn read_linux_core_or_numa_list<Mapper: Fn(u16) -> R, R: Ord>(&self, mapper: Mapper) -> Result<BTreeSet<R>, ListParseError>;
#[inline(always)]
fn parse_linux_core_or_numa_mask(&self) -> Result<u32, io::Error>;
#[inline(always)]
fn parse_virtual_memory_statistics_file(&self) -> io::Result<HashMap<VirtualMemoryStatisticName, u64>>;
#[inline(always)]
fn parse_memory_information_file(&self, memory_information_name_prefix: &str) -> Result<MemoryInformation, MemoryInformationParseError>;
}
impl PathExt for Path
{
#[cfg(unix)]
#[inline(always)]
fn to_c_string(&self) -> CString
{
CString::new(self.as_os_str().as_bytes()).expect("Paths should not contain interior ASCII NULs")
}
#[cfg(unix)]
#[inline(always)]
fn make_file_read_write_all(&self) -> io::Result<()>
{
#[inline(always)]
fn add_read_write_permissions(permissions: Permissions) -> Permissions
{
Permissions::from_mode(permissions.mode() | 0o666)
}
let metadata = metadata(self)?;
set_permissions(self, add_read_write_permissions(metadata.permissions()))
}
#[cfg(unix)]
#[inline(always)]
fn make_folder_searchable_to_all(&self) -> io::Result<()>
{
#[inline(always)]
fn add_read_and_execute_permissions(permissions: Permissions) -> Permissions
{
Permissions::from_mode(permissions.mode() | 0o555)
}
let metadata = metadata(self)?;
set_permissions(self, add_read_and_execute_permissions(metadata.permissions()))
}
#[inline(always)]
fn read_hexadecimal_value_with_prefix<P: Fn(&str) -> Result<T, ParseIntError>, T>(&self, size: usize, parser: P) -> io::Result<T>
{
use self::ErrorKind::InvalidData;
let raw_string = self.read_string_without_line_feed()?;
let size_wih_0x_prefix = 2 + size;
if raw_string.len() != size_wih_0x_prefix
{
return Err(io::Error::new(InvalidData, format!("{} bytes not read", size_wih_0x_prefix)));
}
match &raw_string[..2]
{
"0x" => (),
_ => return Err(io::Error::new(InvalidData, "value does not start '0x'")),
}
match parser(&raw_string[2..])
{
Err(error) => Err(io::Error::new(InvalidData, error)),
Ok(value) => Ok(value),
}
}
#[inline(always)]
fn read_raw_string(&self) -> io::Result<String>
{
let raw_string = read_to_string(self)?;
if raw_string.is_empty()
{
Err(io::Error::new(ErrorKind::InvalidData, "Empty file"))
}
else
{
Ok(raw_string)
}
}
#[inline(always)]
fn read_string_without_line_feed(&self) -> io::Result<String>
{
let mut raw_string = self.read_raw_string()?;
let length = raw_string.len();
let should_be_line_feed = raw_string.remove(length - 1);
if should_be_line_feed != '\n'
{
return Err(io::Error::new(ErrorKind::InvalidData, "File lacks terminating line feed"));
}
Ok(raw_string)
}
#[inline(always)]
fn read_value<F>(&self) -> io::Result<F> where F: FromStr, <F as FromStr>::Err: 'static + Send + Sync + Error
{
let string = self.read_string_without_line_feed()?;
match string.parse::<F>()
{
Err(error) => Err(io::Error::new(ErrorKind::InvalidData, error)),
Ok(value) => Ok(value),
}
}
#[inline(always)]
fn write_value<D: Display>(&self, value: D) -> io::Result<()>
{
let value = format!("{}\n", value).into_bytes();
let mut file = OpenOptions::new().write(true).open(self)?;
file.write_all(value.as_slice())
}
#[inline(always)]
fn read_linux_core_or_numa_list<Mapper: Fn(u16) -> R, R: Ord>(&self, mapper: Mapper) -> Result<BTreeSet<R>, ListParseError>
{
let without_line_feed = self.read_string_without_line_feed()?;
ListParseError::parse_linux_list_string::<Mapper, R>(&without_line_feed, mapper)
}
#[inline(always)]
fn parse_linux_core_or_numa_mask(&self) -> Result<u32, io::Error>
{
let without_line_feed = self.read_string_without_line_feed()?;
if without_line_feed.len() != 8
{
return Err(io::Error::new(ErrorKind::InvalidData, "Linux core or numa mask string should be 8 characters long"))
}
u32::from_str_radix(&without_line_feed, 16).map_err(|error| io::Error::new(ErrorKind::InvalidData, error))
}
#[inline(always)]
fn parse_virtual_memory_statistics_file(&self) -> io::Result<HashMap<VirtualMemoryStatisticName, u64>>
{
let file = File::open(self)?;
let mut reader = BufReader::with_capacity(4096, file);
let mut statistics = HashMap::with_capacity(6);
let mut zero_based_line_number = 0;
let mut line = String::with_capacity(64);
while reader.read_line(&mut line)? > 0
{
{
use self::ErrorKind::InvalidData;
let mut split = line.splitn(2, ' ');
let statistic_name = VirtualMemoryStatisticName::parse(split.next().unwrap());
let statistic_value = match split.next()
{
None => return Err(io::Error::new(InvalidData, format!("Zero based line '{}' does not have a value second column", zero_based_line_number))),
Some(value) =>
{
match value.parse::<u64>()
{
Err(parse_error) => return Err(io::Error::new(InvalidData, parse_error)),
Ok(value) => value,
}
}
};
if let Some(previous) = statistics.insert(statistic_name, statistic_value)
{
return Err(io::Error::new(InvalidData, format!("Zero based line '{}' has a duplicate statistic (was '{}')", zero_based_line_number, previous)))
}
}
line.clear();
zero_based_line_number += 1;
}
Ok(statistics)
}
fn parse_memory_information_file(&self, memory_information_name_prefix: &str) -> Result<MemoryInformation, MemoryInformationParseError>
{
let mut reader = BufReader::with_capacity(4096, File::open(self)?);
let mut map = HashMap::new();
let mut line_number = 0;
let mut line = String::with_capacity(512);
while reader.read_line(&mut line)? > 0
{
{
let mut split = line.splitn(2, ':');
let memory_information_name = MemoryInformationName::parse(split.next().unwrap(), memory_information_name_prefix);
let memory_information_value = match split.next()
{
None => return Err(MemoryInformationParseError::CouldNotParseMemoryInformationValue(line_number, memory_information_name)),
Some(raw_value) =>
{
let trimmed_raw_value = raw_value.trim();
let ends_with = memory_information_name.unit().ends_with();
if !trimmed_raw_value.ends_with(ends_with)
{
return Err(MemoryInformationParseError::CouldNotParseMemoryInformationValue(line_number, memory_information_name));
}
let trimmed = &raw_value[0..raw_value.len() - ends_with.len()];
match trimmed.parse::<u64>()
{
Ok(value) => value,
Err(int_parse_error) => return Err(MemoryInformationParseError::CouldNotParseMemoryInformationValueAsU64(line_number, memory_information_name, raw_value.to_owned(), int_parse_error))
}
}
};
if map.contains_key(&memory_information_name)
{
return Err(MemoryInformationParseError::DuplicateMemoryInformation(line_number, memory_information_name, memory_information_value));
}
map.insert(memory_information_name, memory_information_value);
}
line.clear();
line_number += 1;
}
Ok(MemoryInformation(map))
}
}