1use std::io::Write;
11use std::path::PathBuf;
12
13use crate::fs::error::ErrorKind::*;
14use crate::fs::error::*;
15
16use crate::fs::read_string_from;
17use crate::fs::{ControllIdentifier, ControllerInternal, Controllers, Resources, Subsystem};
18
19#[derive(Debug, Clone)]
24pub struct RdmaController {
25 base: PathBuf,
26 path: PathBuf,
27}
28
29impl ControllerInternal for RdmaController {
30 fn control_type(&self) -> Controllers {
31 Controllers::Rdma
32 }
33 fn get_path(&self) -> &PathBuf {
34 &self.path
35 }
36 fn get_path_mut(&mut self) -> &mut PathBuf {
37 &mut self.path
38 }
39 fn get_base(&self) -> &PathBuf {
40 &self.base
41 }
42
43 fn apply(&self, _res: &Resources) -> Result<()> {
44 Ok(())
45 }
46}
47
48impl ControllIdentifier for RdmaController {
49 fn controller_type() -> Controllers {
50 Controllers::Rdma
51 }
52}
53
54impl<'a> From<&'a Subsystem> for &'a RdmaController {
55 fn from(sub: &'a Subsystem) -> &'a RdmaController {
56 unsafe {
57 match sub {
58 Subsystem::Rdma(c) => c,
59 _ => {
60 assert_eq!(1, 0);
61 let v = std::mem::MaybeUninit::uninit();
62 v.assume_init()
63 }
64 }
65 }
66 }
67}
68
69impl RdmaController {
70 pub fn new(point: PathBuf, root: PathBuf) -> Self {
72 Self {
73 base: root,
74 path: point,
75 }
76 }
77
78 pub fn current(&self) -> Result<String> {
80 self.open_path("rdma.current", false)
81 .and_then(read_string_from)
82 }
83
84 pub fn max(&self) -> Result<String> {
86 self.open_path("rdma.max", false).and_then(read_string_from)
87 }
88
89 pub fn set_max(&self, max: &str) -> Result<()> {
91 self.open_path("rdma.max", true).and_then(|mut file| {
92 file.write_all(max.as_ref()).map_err(|e| {
93 Error::with_cause(WriteFailed("rdma.max".to_string(), max.to_string()), e)
94 })
95 })
96 }
97}