sysctl/linux/
funcs.rs

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
// linux/funcs.rs

use ctl_error::*;
use ctl_value::*;

use std::io::{Read, Write};

pub fn path_to_name(name: &str) -> String {
    name.to_owned()
        .replace("/proc/sys/", "")
        .replace("..", ".")
        .replace("/", ".")
}

pub fn value(name: &str) -> Result<CtlValue, SysctlError> {
    let file_res = std::fs::OpenOptions::new()
        .read(true)
        .write(false)
        .open(&name);

    file_res
        .map(|mut file| {
            let mut v = String::new();
            file.read_to_string(&mut v)?;
            Ok(CtlValue::String(v.trim().to_owned()))
        })
        .map_err(|e| {
            if e.kind() == std::io::ErrorKind::NotFound {
                SysctlError::NotFound(name.into())
            } else {
                e.into()
            }
        })?
}

pub fn set_value(name: &str, v: CtlValue) -> Result<CtlValue, SysctlError> {
    let file_res = std::fs::OpenOptions::new()
        .read(false)
        .write(true)
        .open(&name);

    file_res
        .map(|mut file| match v {
            CtlValue::String(v) => {
                file.write_all(&v.as_bytes())?;
                value(&name)
            }
            _ => Err(std::io::Error::from(std::io::ErrorKind::InvalidData).into()),
        })
        .map_err(|e| {
            if e.kind() == std::io::ErrorKind::NotFound {
                SysctlError::NotFound(name.into())
            } else {
                e.into()
            }
        })?
}