diff --git a/src/blkio.rs b/src/blkio.rs index 7e21faf..457dceb 100644 --- a/src/blkio.rs +++ b/src/blkio.rs @@ -8,13 +8,13 @@ //! //! See the Kernel's documentation for more information about this subsystem, found at: //! [Documentation/cgroup-v1/blkio-controller.txt](https://www.kernel.org/doc/Documentation/cgroup-v1/blkio-controller.txt) -use std::fs::File; -use std::io::{Read, Write}; +use std::io::Write; use std::path::PathBuf; use crate::error::ErrorKind::*; use crate::error::*; +use crate::{read_string_from, read_u64_from}; use crate::{ BlkIoResources, ControllIdentifier, ControllerInternal, Controllers, Resources, Subsystem, }; @@ -401,25 +401,6 @@ impl<'a> From<&'a Subsystem> for &'a BlkIoController { } } -fn read_string_from(mut file: File) -> Result { - let mut string = String::new(); - match file.read_to_string(&mut string) { - Ok(_) => Ok(string.trim().to_string()), - Err(e) => Err(Error::with_cause(ReadFailed, e)), - } -} - -fn read_u64_from(mut file: File) -> Result { - let mut string = String::new(); - match file.read_to_string(&mut string) { - Ok(_) => string - .trim() - .parse() - .map_err(|e| Error::with_cause(ParseError, e)), - Err(e) => Err(Error::with_cause(ReadFailed, e)), - } -} - impl BlkIoController { /// Constructs a new `BlkIoController` with `root` serving as the root of the control group. pub fn new(root: PathBuf, v2: bool) -> Self { diff --git a/src/cpu.rs b/src/cpu.rs index bcf9206..6293e3c 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -15,7 +15,7 @@ use std::path::PathBuf; use crate::error::ErrorKind::*; use crate::error::*; -use crate::{parse_max_value, read_i64_from}; +use crate::{parse_max_value, read_i64_from, read_u64_from}; use crate::{ ControllIdentifier, ControllerInternal, Controllers, CpuResources, CustomizedAttribute, @@ -110,17 +110,6 @@ impl<'a> From<&'a Subsystem> for &'a CpuController { } } -fn read_u64_from(mut file: File) -> Result { - let mut string = String::new(); - match file.read_to_string(&mut string) { - Ok(_) => string - .trim() - .parse() - .map_err(|e| Error::with_cause(ParseError, e)), - Err(e) => Err(Error::with_cause(ReadFailed, e)), - } -} - impl CpuController { /// Contructs a new `CpuController` with `root` serving as the root of the control group. pub fn new(root: PathBuf, v2: bool) -> Self { diff --git a/src/cpuacct.rs b/src/cpuacct.rs index 6efde64..57eafbd 100644 --- a/src/cpuacct.rs +++ b/src/cpuacct.rs @@ -7,13 +7,13 @@ //! //! See the Kernel's documentation for more information about this subsystem, found at: //! [Documentation/cgroup-v1/cpuacct.txt](https://www.kernel.org/doc/Documentation/cgroup-v1/cpuacct.txt) -use std::fs::File; -use std::io::{Read, Write}; +use std::io::Write; use std::path::PathBuf; use crate::error::ErrorKind::*; use crate::error::*; +use crate::{read_string_from, read_u64_from}; use crate::{ControllIdentifier, ControllerInternal, Controllers, Resources, Subsystem}; /// A controller that allows controlling the `cpuacct` subsystem of a Cgroup. @@ -97,26 +97,6 @@ impl<'a> From<&'a Subsystem> for &'a CpuAcctController { } } -fn read_u64_from(mut file: File) -> Result { - let mut string = String::new(); - let res = file.read_to_string(&mut string); - match res { - Ok(_) => match string.trim().parse() { - Ok(e) => Ok(e), - Err(e) => Err(Error::with_cause(ParseError, e)), - }, - Err(e) => Err(Error::with_cause(ReadFailed, e)), - } -} - -fn read_string_from(mut file: File) -> Result { - let mut string = String::new(); - match file.read_to_string(&mut string) { - Ok(_) => Ok(string.trim().to_string()), - Err(e) => Err(Error::with_cause(ReadFailed, e)), - } -} - impl CpuAcctController { /// Contructs a new `CpuAcctController` with `root` serving as the root of the control group. pub fn new(root: PathBuf) -> Self { diff --git a/src/cpuset.rs b/src/cpuset.rs index cef4679..957ed7a 100644 --- a/src/cpuset.rs +++ b/src/cpuset.rs @@ -10,13 +10,13 @@ //! [Documentation/cgroup-v1/cpusets.txt](https://www.kernel.org/doc/Documentation/cgroup-v1/cpusets.txt) use log::*; -use std::fs::File; -use std::io::{Read, Write}; +use std::io::Write; use std::path::PathBuf; use crate::error::ErrorKind::*; use crate::error::*; +use crate::{read_string_from, read_u64_from}; use crate::{ ControllIdentifier, ControllerInternal, Controllers, CpuResources, Resources, Subsystem, }; @@ -204,25 +204,6 @@ impl<'a> From<&'a Subsystem> for &'a CpuSetController { } } -fn read_string_from(mut file: File) -> Result { - let mut string = String::new(); - match file.read_to_string(&mut string) { - Ok(_) => Ok(string.trim().to_string()), - Err(e) => Err(Error::with_cause(ReadFailed, e)), - } -} - -fn read_u64_from(mut file: File) -> Result { - let mut string = String::new(); - match file.read_to_string(&mut string) { - Ok(_) => string - .trim() - .parse() - .map_err(|e| Error::with_cause(ParseError, e)), - Err(e) => Err(Error::with_cause(ReadFailed, e)), - } -} - /// Parse a string like "1,2,4-5,8" into a list of (start, end) tuples. fn parse_range(s: String) -> Result> { let mut fin = Vec::new(); diff --git a/src/hugetlb.rs b/src/hugetlb.rs index 686a599..ed8d83d 100644 --- a/src/hugetlb.rs +++ b/src/hugetlb.rs @@ -8,13 +8,12 @@ //! //! See the Kernel's documentation for more information about this subsystem, found at: //! [Documentation/cgroup-v1/hugetlb.txt](https://www.kernel.org/doc/Documentation/cgroup-v1/hugetlb.txt) -use std::fs::File; -use std::io::{Read, Write}; +use std::io::Write; use std::path::PathBuf; use crate::error::ErrorKind::*; use crate::error::*; -use crate::flat_keyed_to_vec; +use crate::{flat_keyed_to_vec, read_u64_from}; use crate::{ ControllIdentifier, ControllerInternal, Controllers, HugePageResources, Resources, Subsystem, @@ -86,17 +85,6 @@ impl<'a> From<&'a Subsystem> for &'a HugeTlbController { } } -fn read_u64_from(mut file: File) -> Result { - let mut string = String::new(); - match file.read_to_string(&mut string) { - Ok(_) => string - .trim() - .parse() - .map_err(|e| Error::with_cause(ParseError, e)), - Err(e) => Err(Error::with_cause(ReadFailed, e)), - } -} - impl HugeTlbController { /// Constructs a new `HugeTlbController` with `root` serving as the root of the control group. pub fn new(root: PathBuf, v2: bool) -> Self { diff --git a/src/lib.rs b/src/lib.rs index e08f2d9..3d3c68d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,6 +10,7 @@ use std::collections::HashMap; use std::fs::{self, File}; use std::io::{BufRead, BufReader, Read, Write}; use std::path::{Path, PathBuf}; +use std::str::FromStr; macro_rules! update_and_test { ($self: ident, $set_func:ident, $value:expr, $get_func:ident) => { @@ -832,14 +833,35 @@ pub fn nested_keyed_to_hashmap(mut file: File) -> Result Result { +fn read_from(mut file: File) -> Result +where + T: FromStr, + ::Err: 'static + Send + Sync + std::error::Error, +{ let mut string = String::new(); match file.read_to_string(&mut string) { Ok(_) => string .trim() - .parse() + .parse::() .map_err(|e| Error::with_cause(ParseError, e)), Err(e) => Err(Error::with_cause(ReadFailed, e)), } } + +fn read_string_from(mut file: File) -> Result { + let mut string = String::new(); + match file.read_to_string(&mut string) { + Ok(_) => Ok(string.trim().to_string()), + Err(e) => Err(Error::with_cause(ReadFailed, e)), + } +} + +/// read and parse an u64 data +fn read_u64_from(file: File) -> Result { + read_from::(file) +} + +/// read and parse an i64 data +fn read_i64_from(file: File) -> Result { + read_from::(file) +} diff --git a/src/memory.rs b/src/memory.rs index 04fd908..8ceb15a 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -9,15 +9,14 @@ //! See the Kernel's documentation for more information about this subsystem, found at: //! [Documentation/cgroup-v1/memory.txt](https://www.kernel.org/doc/Documentation/cgroup-v1/memory.txt) use std::collections::HashMap; -use std::fs::File; -use std::io::{Read, Write}; +use std::io::Write; use std::path::PathBuf; use std::sync::mpsc::Receiver; use crate::error::ErrorKind::*; use crate::error::*; use crate::events; -use crate::read_i64_from; +use crate::{read_i64_from, read_string_from, read_u64_from}; use crate::flat_keyed_to_hashmap; @@ -870,25 +869,6 @@ impl<'a> From<&'a Subsystem> for &'a MemController { } } -fn read_u64_from(mut file: File) -> Result { - let mut string = String::new(); - match file.read_to_string(&mut string) { - Ok(_) => string - .trim() - .parse() - .map_err(|e| Error::with_cause(ParseError, e)), - Err(e) => Err(Error::with_cause(ReadFailed, e)), - } -} - -fn read_string_from(mut file: File) -> Result { - let mut string = String::new(); - match file.read_to_string(&mut string) { - Ok(_) => Ok(string.trim().to_string()), - Err(e) => Err(Error::with_cause(ReadFailed, e)), - } -} - #[cfg(test)] mod tests { use crate::memory::{ diff --git a/src/net_cls.rs b/src/net_cls.rs index 5328eb1..77b3f07 100644 --- a/src/net_cls.rs +++ b/src/net_cls.rs @@ -7,13 +7,13 @@ //! //! See the Kernel's documentation for more information about this subsystem, found at: //! [Documentation/cgroup-v1/net_cls.txt](https://www.kernel.org/doc/Documentation/cgroup-v1/net_cls.txt) -use std::fs::File; -use std::io::{Read, Write}; +use std::io::Write; use std::path::PathBuf; use crate::error::ErrorKind::*; use crate::error::*; +use crate::read_u64_from; use crate::{ ControllIdentifier, ControllerInternal, Controllers, NetworkResources, Resources, Subsystem, }; @@ -74,17 +74,6 @@ impl<'a> From<&'a Subsystem> for &'a NetClsController { } } -fn read_u64_from(mut file: File) -> Result { - let mut string = String::new(); - match file.read_to_string(&mut string) { - Ok(_) => string - .trim() - .parse() - .map_err(|e| Error::with_cause(ParseError, e)), - Err(e) => Err(Error::with_cause(ReadFailed, e)), - } -} - impl NetClsController { /// Constructs a new `NetClsController` with `root` serving as the root of the control group. pub fn new(root: PathBuf) -> Self { diff --git a/src/net_prio.rs b/src/net_prio.rs index d1bbbde..d2b6c68 100644 --- a/src/net_prio.rs +++ b/src/net_prio.rs @@ -8,13 +8,13 @@ //! See the Kernel's documentation for more information about this subsystem, found at: //! [Documentation/cgroup-v1/net_prio.txt](https://www.kernel.org/doc/Documentation/cgroup-v1/net_prio.txt) use std::collections::HashMap; -use std::fs::File; -use std::io::{BufRead, BufReader, Read, Write}; +use std::io::{BufRead, BufReader, Write}; use std::path::PathBuf; use crate::error::ErrorKind::*; use crate::error::*; +use crate::read_u64_from; use crate::{ ControllIdentifier, ControllerInternal, Controllers, NetworkResources, Resources, Subsystem, }; @@ -77,17 +77,6 @@ impl<'a> From<&'a Subsystem> for &'a NetPrioController { } } -fn read_u64_from(mut file: File) -> Result { - let mut string = String::new(); - match file.read_to_string(&mut string) { - Ok(_) => string - .trim() - .parse() - .map_err(|e| Error::with_cause(ParseError, e)), - Err(e) => Err(Error::with_cause(ReadFailed, e)), - } -} - impl NetPrioController { /// Constructs a new `NetPrioController` with `root` serving as the root of the control group. pub fn new(root: PathBuf) -> Self { diff --git a/src/pid.rs b/src/pid.rs index 1955828..fef8421 100644 --- a/src/pid.rs +++ b/src/pid.rs @@ -8,13 +8,13 @@ //! //! See the Kernel's documentation for more information about this subsystem, found at: //! [Documentation/cgroups-v1/pids.txt](https://www.kernel.org/doc/Documentation/cgroup-v1/pids.txt) -use std::fs::File; use std::io::{Read, Write}; use std::path::PathBuf; use crate::error::ErrorKind::*; use crate::error::*; +use crate::read_u64_from; use crate::{ parse_max_value, ControllIdentifier, ControllerInternal, Controllers, MaxValue, PidResources, Resources, Subsystem, @@ -89,17 +89,6 @@ impl<'a> From<&'a Subsystem> for &'a PidController { } } -fn read_u64_from(mut file: File) -> Result { - let mut string = String::new(); - match file.read_to_string(&mut string) { - Ok(_) => string - .trim() - .parse() - .map_err(|e| Error::with_cause(ParseError, e)), - Err(e) => Err(Error::with_cause(ReadFailed, e)), - } -} - impl PidController { /// Constructors a new `PidController` instance, with `root` serving as the controller's root /// directory. diff --git a/src/rdma.rs b/src/rdma.rs index 8066c15..1ee1fc8 100644 --- a/src/rdma.rs +++ b/src/rdma.rs @@ -7,13 +7,13 @@ //! //! See the Kernel's documentation for more information about this subsystem, found at: //! [Documentation/cgroup-v1/rdma.txt](https://www.kernel.org/doc/Documentation/cgroup-v1/rdma.txt) -use std::fs::File; -use std::io::{Read, Write}; +use std::io::Write; use std::path::PathBuf; use crate::error::ErrorKind::*; use crate::error::*; +use crate::read_string_from; use crate::{ControllIdentifier, ControllerInternal, Controllers, Resources, Subsystem}; /// A controller that allows controlling the `rdma` subsystem of a Cgroup. @@ -66,14 +66,6 @@ impl<'a> From<&'a Subsystem> for &'a RdmaController { } } -fn read_string_from(mut file: File) -> Result { - let mut string = String::new(); - match file.read_to_string(&mut string) { - Ok(_) => Ok(string.trim().to_string()), - Err(e) => Err(Error::with_cause(ReadFailed, e)), - } -} - impl RdmaController { /// Constructs a new `RdmaController` with `root` serving as the root of the control group. pub fn new(root: PathBuf) -> Self {