1*e5a8055aSGnoCiYeH use crate::error::parse_error::ParseError; 2*e5a8055aSGnoCiYeH use crate::error::parse_error::ParseErrorType; 3*e5a8055aSGnoCiYeH use crate::error::runtime_error::RuntimeError; 4*e5a8055aSGnoCiYeH use crate::error::runtime_error::RuntimeErrorType; 5f206f17aSGnoCiYeH use crate::parse::parse_util::UnitParseUtil; 64e851715SGnoCiYeH use crate::parse::Segment; 7f206f17aSGnoCiYeH 8f206f17aSGnoCiYeH #[cfg(target_os = "dragonos")] 9f206f17aSGnoCiYeH use drstd as std; 10*e5a8055aSGnoCiYeH use hashbrown::HashMap; 11f206f17aSGnoCiYeH 124e851715SGnoCiYeH use std::any::Any; 13f206f17aSGnoCiYeH use std::default::Default; 14*e5a8055aSGnoCiYeH use std::fmt::Debug; 15f206f17aSGnoCiYeH use std::result::Result; 16f206f17aSGnoCiYeH use std::result::Result::Err; 17f206f17aSGnoCiYeH use std::result::Result::Ok; 1821fc724cSGnoCiYeH use std::string::String; 19*e5a8055aSGnoCiYeH use std::sync::atomic::AtomicUsize; 20*e5a8055aSGnoCiYeH use std::sync::atomic::Ordering; 21*e5a8055aSGnoCiYeH use std::sync::Arc; 2221fc724cSGnoCiYeH use std::vec::Vec; 2321fc724cSGnoCiYeH 244e851715SGnoCiYeH pub mod service; 254e851715SGnoCiYeH pub mod target; 2621fc724cSGnoCiYeH 2721fc724cSGnoCiYeH use self::target::TargetUnit; 28*e5a8055aSGnoCiYeH use lazy_static::lazy_static; 29*e5a8055aSGnoCiYeH 30*e5a8055aSGnoCiYeH pub fn generate_unit_id() -> usize { 31*e5a8055aSGnoCiYeH static UNIT_ID: AtomicUsize = AtomicUsize::new(1); 32*e5a8055aSGnoCiYeH return UNIT_ID.fetch_add(1, Ordering::SeqCst); 33*e5a8055aSGnoCiYeH } 3421fc724cSGnoCiYeH 3521fc724cSGnoCiYeH //所有可解析的Unit都应该实现该trait 36*e5a8055aSGnoCiYeH pub trait Unit: Sync + Send + Debug { 374e851715SGnoCiYeH /// @brief 从文件获取到Unit,该函数是解析Unit文件的入口函数 384e851715SGnoCiYeH /// 394e851715SGnoCiYeH /// 从path解析Unit属性 404e851715SGnoCiYeH /// 414e851715SGnoCiYeH /// @param path 需解析的文件 424e851715SGnoCiYeH /// 43*e5a8055aSGnoCiYeH /// @return 解析成功则返回对应Unit的Arc指针,否则返回Err 44*e5a8055aSGnoCiYeH fn from_path(path: &str) -> Result<Arc<Self>, ParseError> 454e851715SGnoCiYeH where 464e851715SGnoCiYeH Self: Sized; 474e851715SGnoCiYeH 484e851715SGnoCiYeH fn as_any(&self) -> &dyn Any; 494e851715SGnoCiYeH 504e851715SGnoCiYeH /// @brief 设置Unit属性 514e851715SGnoCiYeH /// 524e851715SGnoCiYeH /// 设置对应Unit属性 534e851715SGnoCiYeH /// 544e851715SGnoCiYeH /// @param segment 属性段类型 554e851715SGnoCiYeH /// 564e851715SGnoCiYeH /// @param attr 属性名 574e851715SGnoCiYeH /// 584e851715SGnoCiYeH /// @param val 属性值 594e851715SGnoCiYeH /// 604e851715SGnoCiYeH /// @return 设置成功则返回Ok(()),否则返回Err 614e851715SGnoCiYeH fn set_attr(&mut self, segment: Segment, attr: &str, val: &str) -> Result<(), ParseError>; 62f206f17aSGnoCiYeH 63f206f17aSGnoCiYeH /// # 设置每个Unit都应该有的属性 64f206f17aSGnoCiYeH /// 65f206f17aSGnoCiYeH /// 设置BaseUnit 66f206f17aSGnoCiYeH /// 67f206f17aSGnoCiYeH /// ## param unit_base 设置值 68f206f17aSGnoCiYeH fn set_unit_base(&mut self, unit_base: BaseUnit); 69f206f17aSGnoCiYeH 70f206f17aSGnoCiYeH /// # 获取UnitType 71f206f17aSGnoCiYeH /// 72f206f17aSGnoCiYeH /// ## return UnitType 73f206f17aSGnoCiYeH fn unit_type(&self) -> UnitType; 74*e5a8055aSGnoCiYeH 75*e5a8055aSGnoCiYeH fn unit_base(&self) -> &BaseUnit; 76*e5a8055aSGnoCiYeH 77*e5a8055aSGnoCiYeH fn mut_unit_base(&mut self) -> &mut BaseUnit; 78*e5a8055aSGnoCiYeH 79*e5a8055aSGnoCiYeH fn unit_id(&self) -> usize; 80*e5a8055aSGnoCiYeH 81*e5a8055aSGnoCiYeH /// ## Unit的工作逻辑 82*e5a8055aSGnoCiYeH /// 83*e5a8055aSGnoCiYeH /// ### return OK(())/Err 84*e5a8055aSGnoCiYeH fn run(&self) -> Result<(), RuntimeError>; 85*e5a8055aSGnoCiYeH 86*e5a8055aSGnoCiYeH /// ## 设置unit_id 87*e5a8055aSGnoCiYeH /// 88*e5a8055aSGnoCiYeH /// ### return OK(())/Err 89*e5a8055aSGnoCiYeH fn set_unit_id(&mut self) { 90*e5a8055aSGnoCiYeH self.mut_unit_base().set_id(generate_unit_id()); 91*e5a8055aSGnoCiYeH } 92*e5a8055aSGnoCiYeH } 93*e5a8055aSGnoCiYeH 94*e5a8055aSGnoCiYeH pub struct Downcast; 95*e5a8055aSGnoCiYeH impl Downcast { 96*e5a8055aSGnoCiYeH fn downcast<T: Unit + Clone + 'static>(unit: Arc<dyn Unit>) -> Result<Arc<T>,RuntimeError> { 97*e5a8055aSGnoCiYeH let any = unit.as_any(); 98*e5a8055aSGnoCiYeH let unit = match any.downcast_ref::<T>(){ 99*e5a8055aSGnoCiYeH Some(v) => v, 100*e5a8055aSGnoCiYeH None => { 101*e5a8055aSGnoCiYeH return Err(RuntimeError::new(RuntimeErrorType::DatabaseError)); 102*e5a8055aSGnoCiYeH } 103*e5a8055aSGnoCiYeH }; 104*e5a8055aSGnoCiYeH 105*e5a8055aSGnoCiYeH return Ok(Arc::new(unit.clone())); 106*e5a8055aSGnoCiYeH } 10721fc724cSGnoCiYeH } 10821fc724cSGnoCiYeH 1094e851715SGnoCiYeH //Unit状态 110*e5a8055aSGnoCiYeH #[derive(Clone, Copy, Debug,PartialEq)] 111*e5a8055aSGnoCiYeH pub enum UnitState { 11221fc724cSGnoCiYeH Enabled, 11321fc724cSGnoCiYeH Disabled, 11421fc724cSGnoCiYeH Static, 1154e851715SGnoCiYeH Masked, 11621fc724cSGnoCiYeH } 11721fc724cSGnoCiYeH 1184e851715SGnoCiYeH //Unit类型 1194e851715SGnoCiYeH #[derive(Clone, Copy, PartialEq, Debug)] 1204e851715SGnoCiYeH pub enum UnitType { 12121fc724cSGnoCiYeH Automount, 12221fc724cSGnoCiYeH Device, 12321fc724cSGnoCiYeH Mount, 12421fc724cSGnoCiYeH Path, 12521fc724cSGnoCiYeH Scope, 12621fc724cSGnoCiYeH Service, 12721fc724cSGnoCiYeH Slice, 12821fc724cSGnoCiYeH Snapshot, 12921fc724cSGnoCiYeH Socket, 13021fc724cSGnoCiYeH Swap, 13121fc724cSGnoCiYeH Target, 13221fc724cSGnoCiYeH Timer, 1334e851715SGnoCiYeH Unknown, 13421fc724cSGnoCiYeH } 13521fc724cSGnoCiYeH 1364e851715SGnoCiYeH //记录unit文件基本信息,这个结构体里面的信息是所有Unit文件都可以有的属性 137*e5a8055aSGnoCiYeH #[derive(Debug, Clone)] 1384e851715SGnoCiYeH pub struct BaseUnit { 139f206f17aSGnoCiYeH unit_part: UnitPart, 140f206f17aSGnoCiYeH install_part: InstallPart, 141f206f17aSGnoCiYeH state: UnitState, 142f206f17aSGnoCiYeH unit_type: UnitType, 143*e5a8055aSGnoCiYeH unit_id: usize, 14421fc724cSGnoCiYeH } 14521fc724cSGnoCiYeH 1464e851715SGnoCiYeH impl Default for BaseUnit { 1474e851715SGnoCiYeH fn default() -> Self { 1484e851715SGnoCiYeH BaseUnit { 1494e851715SGnoCiYeH unit_part: UnitPart::default(), 1504e851715SGnoCiYeH install_part: InstallPart::default(), 1514e851715SGnoCiYeH state: UnitState::Disabled, 1524e851715SGnoCiYeH unit_type: UnitType::Unknown, 153*e5a8055aSGnoCiYeH unit_id: 0, 15421fc724cSGnoCiYeH } 15521fc724cSGnoCiYeH } 15621fc724cSGnoCiYeH } 15721fc724cSGnoCiYeH 1584e851715SGnoCiYeH impl BaseUnit { 159f206f17aSGnoCiYeH pub fn set_state(&mut self, state: UnitState) { 160f206f17aSGnoCiYeH self.state = state; 161f206f17aSGnoCiYeH } 162f206f17aSGnoCiYeH 163f206f17aSGnoCiYeH pub fn set_unit_type(&mut self, utype: UnitType) { 164f206f17aSGnoCiYeH self.unit_type = utype; 165f206f17aSGnoCiYeH } 166f206f17aSGnoCiYeH 167f206f17aSGnoCiYeH pub fn set_unit_part_attr( 168f206f17aSGnoCiYeH &mut self, 169f206f17aSGnoCiYeH attr_type: &BaseUnitAttr, 170f206f17aSGnoCiYeH val: &str, 171f206f17aSGnoCiYeH ) -> Result<(), ParseError> { 172f206f17aSGnoCiYeH return self.unit_part.set_attr(attr_type, val); 173f206f17aSGnoCiYeH } 174f206f17aSGnoCiYeH 175f206f17aSGnoCiYeH pub fn set_install_part_attr( 176f206f17aSGnoCiYeH &mut self, 177f206f17aSGnoCiYeH attr_type: &InstallUnitAttr, 178f206f17aSGnoCiYeH val: &str, 179f206f17aSGnoCiYeH ) -> Result<(), ParseError> { 180f206f17aSGnoCiYeH return self.install_part.set_attr(attr_type, val); 181f206f17aSGnoCiYeH } 182f206f17aSGnoCiYeH 1834e851715SGnoCiYeH pub fn parse_and_set_attribute(&self) -> Result<(), ParseError> { 1844e851715SGnoCiYeH return Ok(()); 1854e851715SGnoCiYeH } 1864e851715SGnoCiYeH 187f206f17aSGnoCiYeH pub fn unit_part(&self) -> &UnitPart { 188f206f17aSGnoCiYeH &self.unit_part 189f206f17aSGnoCiYeH } 190f206f17aSGnoCiYeH 191f206f17aSGnoCiYeH pub fn install_part(&self) -> &InstallPart { 192f206f17aSGnoCiYeH &self.install_part 193f206f17aSGnoCiYeH } 194f206f17aSGnoCiYeH 195f206f17aSGnoCiYeH pub fn state(&self) -> &UnitState { 196f206f17aSGnoCiYeH &self.state 197f206f17aSGnoCiYeH } 198f206f17aSGnoCiYeH 199f206f17aSGnoCiYeH pub fn unit_type(&self) -> &UnitType { 200f206f17aSGnoCiYeH &self.unit_type 201f206f17aSGnoCiYeH } 202*e5a8055aSGnoCiYeH 203*e5a8055aSGnoCiYeH pub fn set_id(&mut self, id: usize) { 204*e5a8055aSGnoCiYeH self.unit_id = id; 205*e5a8055aSGnoCiYeH } 206f206f17aSGnoCiYeH } 207f206f17aSGnoCiYeH 208*e5a8055aSGnoCiYeH #[derive(Default, Debug, Clone)] 2094e851715SGnoCiYeH pub struct Url { 2104e851715SGnoCiYeH pub url_string: String, // pub protocol: String, 2114e851715SGnoCiYeH // pub host: String, 2124e851715SGnoCiYeH // pub port: Option<u16>, 2134e851715SGnoCiYeH // pub path: String, 2144e851715SGnoCiYeH // pub query: Option<String>, 2154e851715SGnoCiYeH // pub fragment: Option<String>, 2164e851715SGnoCiYeH } 2174e851715SGnoCiYeH 2184e851715SGnoCiYeH //对应Unit文件的Unit段 219*e5a8055aSGnoCiYeH #[derive(Debug, Clone)] 2204e851715SGnoCiYeH pub struct UnitPart { 221f206f17aSGnoCiYeH description: String, 222f206f17aSGnoCiYeH documentation: Vec<Url>, 223*e5a8055aSGnoCiYeH requires: Vec<Arc<dyn Unit>>, 224*e5a8055aSGnoCiYeH wants: Vec<Arc<dyn Unit>>, 225*e5a8055aSGnoCiYeH after: Vec<Arc<dyn Unit>>, 226*e5a8055aSGnoCiYeH before: Vec<Arc<dyn Unit>>, 227*e5a8055aSGnoCiYeH binds_to: Vec<Arc<dyn Unit>>, 228*e5a8055aSGnoCiYeH part_of: Vec<Arc<dyn Unit>>, 229*e5a8055aSGnoCiYeH on_failure: Vec<Arc<dyn Unit>>, 230*e5a8055aSGnoCiYeH conflicts: Vec<Arc<dyn Unit>>, 23121fc724cSGnoCiYeH } 23221fc724cSGnoCiYeH 23321fc724cSGnoCiYeH impl Default for UnitPart { 23421fc724cSGnoCiYeH fn default() -> Self { 23521fc724cSGnoCiYeH UnitPart { 23621fc724cSGnoCiYeH description: String::new(), 23721fc724cSGnoCiYeH documentation: Vec::new(), 23821fc724cSGnoCiYeH requires: Vec::new(), 23921fc724cSGnoCiYeH wants: Vec::new(), 24021fc724cSGnoCiYeH after: Vec::new(), 24121fc724cSGnoCiYeH before: Vec::new(), 24221fc724cSGnoCiYeH binds_to: Vec::new(), 24321fc724cSGnoCiYeH part_of: Vec::new(), 24421fc724cSGnoCiYeH on_failure: Vec::new(), 2454e851715SGnoCiYeH conflicts: Vec::new(), 24621fc724cSGnoCiYeH } 24721fc724cSGnoCiYeH } 24821fc724cSGnoCiYeH } 24921fc724cSGnoCiYeH 250f206f17aSGnoCiYeH impl UnitPart { 251f206f17aSGnoCiYeH pub fn set_attr(&mut self, attr: &BaseUnitAttr, val: &str) -> Result<(), ParseError> { 252f206f17aSGnoCiYeH match attr { 253f206f17aSGnoCiYeH BaseUnitAttr::None => { 254*e5a8055aSGnoCiYeH return Err(ParseError::new( 255*e5a8055aSGnoCiYeH ParseErrorType::ESyntaxError, 256*e5a8055aSGnoCiYeH String::new(), 257*e5a8055aSGnoCiYeH 0, 258*e5a8055aSGnoCiYeH )); 259f206f17aSGnoCiYeH } 260f206f17aSGnoCiYeH BaseUnitAttr::Description => self.description = String::from(val), 261f206f17aSGnoCiYeH BaseUnitAttr::Documentation => { 262f206f17aSGnoCiYeH self.documentation.extend(UnitParseUtil::parse_url(val)?) 263f206f17aSGnoCiYeH } 264f206f17aSGnoCiYeH BaseUnitAttr::Requires => { 265f206f17aSGnoCiYeH let units = val.split_whitespace().collect::<Vec<&str>>(); 266f206f17aSGnoCiYeH //TODO:目前先加入requires列表,可能会出现循环依赖问题,后续应解决循环依赖问题 267f206f17aSGnoCiYeH for unit_path in units { 268f206f17aSGnoCiYeH self.requires 269f206f17aSGnoCiYeH .push(UnitParseUtil::parse_unit_no_type(unit_path)?); 270f206f17aSGnoCiYeH } 271f206f17aSGnoCiYeH } 272f206f17aSGnoCiYeH BaseUnitAttr::Wants => { 273f206f17aSGnoCiYeH let units = val.split_whitespace().collect::<Vec<&str>>(); 274f206f17aSGnoCiYeH //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题 275f206f17aSGnoCiYeH for unit_path in units { 276f206f17aSGnoCiYeH self.wants 277f206f17aSGnoCiYeH .push(UnitParseUtil::parse_unit_no_type(unit_path)?); 278f206f17aSGnoCiYeH } 279f206f17aSGnoCiYeH } 280f206f17aSGnoCiYeH BaseUnitAttr::After => { 281f206f17aSGnoCiYeH let units = val.split_whitespace().collect::<Vec<&str>>(); 282f206f17aSGnoCiYeH //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题 283f206f17aSGnoCiYeH for unit_path in units { 284f206f17aSGnoCiYeH self.after 285f206f17aSGnoCiYeH .push(UnitParseUtil::parse_unit_no_type(unit_path)?); 286f206f17aSGnoCiYeH } 287f206f17aSGnoCiYeH } 288f206f17aSGnoCiYeH BaseUnitAttr::Before => { 289f206f17aSGnoCiYeH let units = val.split_whitespace().collect::<Vec<&str>>(); 290f206f17aSGnoCiYeH //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题 291f206f17aSGnoCiYeH for unit_path in units { 292f206f17aSGnoCiYeH self.before 293f206f17aSGnoCiYeH .push(UnitParseUtil::parse_unit_no_type(unit_path)?); 294f206f17aSGnoCiYeH } 295f206f17aSGnoCiYeH } 296f206f17aSGnoCiYeH BaseUnitAttr::BindsTo => { 297f206f17aSGnoCiYeH let units = val.split_whitespace().collect::<Vec<&str>>(); 298f206f17aSGnoCiYeH //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题 299f206f17aSGnoCiYeH for unit_path in units { 300f206f17aSGnoCiYeH self.binds_to 301f206f17aSGnoCiYeH .push(UnitParseUtil::parse_unit_no_type(unit_path)?); 302f206f17aSGnoCiYeH } 303f206f17aSGnoCiYeH } 304f206f17aSGnoCiYeH BaseUnitAttr::PartOf => { 305f206f17aSGnoCiYeH let units = val.split_whitespace().collect::<Vec<&str>>(); 306f206f17aSGnoCiYeH //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题 307f206f17aSGnoCiYeH for unit_path in units { 308f206f17aSGnoCiYeH self.part_of 309f206f17aSGnoCiYeH .push(UnitParseUtil::parse_unit_no_type(unit_path)?); 310f206f17aSGnoCiYeH } 311f206f17aSGnoCiYeH } 312f206f17aSGnoCiYeH BaseUnitAttr::OnFailure => { 313f206f17aSGnoCiYeH let units = val.split_whitespace().collect::<Vec<&str>>(); 314f206f17aSGnoCiYeH //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题 315f206f17aSGnoCiYeH for unit_path in units { 316f206f17aSGnoCiYeH self.on_failure 317f206f17aSGnoCiYeH .push(UnitParseUtil::parse_unit_no_type(unit_path)?); 318f206f17aSGnoCiYeH } 319f206f17aSGnoCiYeH } 320f206f17aSGnoCiYeH BaseUnitAttr::Conflicts => { 321f206f17aSGnoCiYeH let units = val.split_whitespace().collect::<Vec<&str>>(); 322f206f17aSGnoCiYeH //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题 323f206f17aSGnoCiYeH for unit_path in units { 324f206f17aSGnoCiYeH let unit = UnitParseUtil::parse_unit_no_type(unit_path)?; 325f206f17aSGnoCiYeH self.conflicts.push(unit); 326f206f17aSGnoCiYeH } 327f206f17aSGnoCiYeH } 328f206f17aSGnoCiYeH } 329f206f17aSGnoCiYeH return Ok(()); 330f206f17aSGnoCiYeH } 331f206f17aSGnoCiYeH 332f206f17aSGnoCiYeH pub fn description(&self) -> &str { 333f206f17aSGnoCiYeH &self.description 334f206f17aSGnoCiYeH } 335f206f17aSGnoCiYeH 336f206f17aSGnoCiYeH pub fn documentation(&self) -> &[Url] { 337f206f17aSGnoCiYeH &self.documentation 338f206f17aSGnoCiYeH } 339f206f17aSGnoCiYeH 340*e5a8055aSGnoCiYeH pub fn requires(&self) -> &[Arc<dyn Unit>] { 341f206f17aSGnoCiYeH &self.requires 342f206f17aSGnoCiYeH } 343f206f17aSGnoCiYeH 344*e5a8055aSGnoCiYeH pub fn wants(&self) -> &[Arc<dyn Unit>] { 345f206f17aSGnoCiYeH &self.wants 346f206f17aSGnoCiYeH } 347f206f17aSGnoCiYeH 348*e5a8055aSGnoCiYeH pub fn after(&self) -> &[Arc<dyn Unit>] { 349f206f17aSGnoCiYeH &self.after 350f206f17aSGnoCiYeH } 351f206f17aSGnoCiYeH 352*e5a8055aSGnoCiYeH pub fn before(&self) -> &[Arc<dyn Unit>] { 353f206f17aSGnoCiYeH &self.before 354f206f17aSGnoCiYeH } 355f206f17aSGnoCiYeH 356*e5a8055aSGnoCiYeH pub fn binds_to(&self) -> &[Arc<dyn Unit>] { 357f206f17aSGnoCiYeH &self.binds_to 358f206f17aSGnoCiYeH } 359f206f17aSGnoCiYeH 360*e5a8055aSGnoCiYeH pub fn part_of(&self) -> &[Arc<dyn Unit>] { 361f206f17aSGnoCiYeH &self.part_of 362f206f17aSGnoCiYeH } 363f206f17aSGnoCiYeH 364*e5a8055aSGnoCiYeH pub fn on_failure(&self) -> &[Arc<dyn Unit>] { 365f206f17aSGnoCiYeH &self.on_failure 366f206f17aSGnoCiYeH } 367f206f17aSGnoCiYeH 368*e5a8055aSGnoCiYeH pub fn conflicts(&self) -> &[Arc<dyn Unit>] { 369f206f17aSGnoCiYeH &self.conflicts 370f206f17aSGnoCiYeH } 371f206f17aSGnoCiYeH } 372f206f17aSGnoCiYeH 3734e851715SGnoCiYeH //对应Unit文件的Install段 374*e5a8055aSGnoCiYeH #[derive(Debug, Clone)] 3754e851715SGnoCiYeH pub struct InstallPart { 376*e5a8055aSGnoCiYeH wanted_by: Vec<Arc<TargetUnit>>, 377*e5a8055aSGnoCiYeH requires_by: Vec<Arc<TargetUnit>>, 378*e5a8055aSGnoCiYeH also: Vec<Arc<dyn Unit>>, 379f206f17aSGnoCiYeH alias: String, 38021fc724cSGnoCiYeH } 38121fc724cSGnoCiYeH 38221fc724cSGnoCiYeH impl Default for InstallPart { 38321fc724cSGnoCiYeH fn default() -> Self { 38421fc724cSGnoCiYeH InstallPart { 38521fc724cSGnoCiYeH wanted_by: Vec::new(), 38621fc724cSGnoCiYeH requires_by: Vec::new(), 38721fc724cSGnoCiYeH also: Vec::new(), 3884e851715SGnoCiYeH alias: String::new(), 38921fc724cSGnoCiYeH } 39021fc724cSGnoCiYeH } 39121fc724cSGnoCiYeH } 39221fc724cSGnoCiYeH 393f206f17aSGnoCiYeH impl InstallPart { 394f206f17aSGnoCiYeH pub fn set_attr(&mut self, attr: &InstallUnitAttr, val: &str) -> Result<(), ParseError> { 395f206f17aSGnoCiYeH match attr { 396f206f17aSGnoCiYeH InstallUnitAttr::RequiredBy => { 397f206f17aSGnoCiYeH let units = val.split_whitespace().collect::<Vec<&str>>(); 398f206f17aSGnoCiYeH //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题 399f206f17aSGnoCiYeH for unit_path in units { 400f206f17aSGnoCiYeH let unit = UnitParseUtil::parse_unit::<TargetUnit>(unit_path)?; 401f206f17aSGnoCiYeH self.requires_by.push(unit); 402f206f17aSGnoCiYeH } 403f206f17aSGnoCiYeH } 404f206f17aSGnoCiYeH InstallUnitAttr::Also => { 405f206f17aSGnoCiYeH let units = val.split_whitespace().collect::<Vec<&str>>(); 406f206f17aSGnoCiYeH //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题 407f206f17aSGnoCiYeH for unit_path in units { 408f206f17aSGnoCiYeH let unit = UnitParseUtil::parse_unit_no_type(unit_path)?; 409f206f17aSGnoCiYeH self.also.push(unit); 410f206f17aSGnoCiYeH } 411f206f17aSGnoCiYeH } 412f206f17aSGnoCiYeH InstallUnitAttr::WantedBy => { 413f206f17aSGnoCiYeH let units = val.split_whitespace().collect::<Vec<&str>>(); 414f206f17aSGnoCiYeH //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题 415f206f17aSGnoCiYeH for unit_path in units { 416f206f17aSGnoCiYeH let unit = UnitParseUtil::parse_unit::<TargetUnit>(unit_path)?; 417f206f17aSGnoCiYeH self.wanted_by.push(unit); 418f206f17aSGnoCiYeH } 419f206f17aSGnoCiYeH } 420f206f17aSGnoCiYeH InstallUnitAttr::Alias => { 421f206f17aSGnoCiYeH self.alias = String::from(val); 422f206f17aSGnoCiYeH } 423f206f17aSGnoCiYeH InstallUnitAttr::None => { 424f206f17aSGnoCiYeH return Err(ParseError::new(ParseErrorType::EINVAL, String::new(), 0)); 425f206f17aSGnoCiYeH } 426f206f17aSGnoCiYeH } 427f206f17aSGnoCiYeH return Ok(()); 428f206f17aSGnoCiYeH } 429f206f17aSGnoCiYeH 430*e5a8055aSGnoCiYeH pub fn wanted_by(&self) -> &[Arc<TargetUnit>] { 431f206f17aSGnoCiYeH &self.wanted_by 432f206f17aSGnoCiYeH } 433f206f17aSGnoCiYeH 434*e5a8055aSGnoCiYeH pub fn requires_by(&self) -> &[Arc<TargetUnit>] { 435f206f17aSGnoCiYeH &self.requires_by 436f206f17aSGnoCiYeH } 437f206f17aSGnoCiYeH 438*e5a8055aSGnoCiYeH pub fn also(&self) -> &[Arc<dyn Unit>] { 439f206f17aSGnoCiYeH &self.also 440f206f17aSGnoCiYeH } 441f206f17aSGnoCiYeH 442f206f17aSGnoCiYeH pub fn alias(&self) -> &str { 443f206f17aSGnoCiYeH &self.alias 444f206f17aSGnoCiYeH } 445f206f17aSGnoCiYeH } 4464e851715SGnoCiYeH //对应Unit文件的各种属性 4474e851715SGnoCiYeH pub enum BaseUnitAttr { 4484e851715SGnoCiYeH None, 4494e851715SGnoCiYeH 4504e851715SGnoCiYeH //Unit段 4514e851715SGnoCiYeH //描述该Unit文件的信息 4524e851715SGnoCiYeH Description, 4534e851715SGnoCiYeH //指定服务文档 4544e851715SGnoCiYeH Documentation, 4554e851715SGnoCiYeH //依赖的其它 Unit 列表 4564e851715SGnoCiYeH Requires, 4574e851715SGnoCiYeH //这个 Unit 启动时,触发启动列出的每个 Unit 模块,而不去考虑这些模板启动是否成功 4584e851715SGnoCiYeH Wants, 4594e851715SGnoCiYeH //后面列出的所有模块全部启动完成以后,才会启动当前的服务 4604e851715SGnoCiYeH After, 4614e851715SGnoCiYeH //在启动指定的任务一个模块之间,都会首先确证当前服务已经运行 4624e851715SGnoCiYeH Before, 4634e851715SGnoCiYeH //这些Unit启动失败时该任务失败,都成功时该任务成功,在这些模板中有任意一个出现意外结束或重启时,这个服务也会跟着终止或重启 4644e851715SGnoCiYeH BindsTo, 4654e851715SGnoCiYeH //仅在列出的任务模块失败或重启时,终止或重启当前服务,而不会随列出模板的启动而启动 4664e851715SGnoCiYeH PartOf, 4674e851715SGnoCiYeH //当这个模板启动失败时,就会自动启动列出的每个模块 4684e851715SGnoCiYeH OnFailure, 4694e851715SGnoCiYeH //与这个模块有冲突的模块,如果列出的模块中有已经在运行的,这个服务就不能启动,反之亦然 4704e851715SGnoCiYeH Conflicts, 4714e851715SGnoCiYeH } 4724e851715SGnoCiYeH 4734e851715SGnoCiYeH pub enum InstallUnitAttr { 4744e851715SGnoCiYeH None, 4754e851715SGnoCiYeH //Install段 4764e851715SGnoCiYeH //依赖当前服务的模块。当前 Unit 激活时(enable)符号链接会放入 /etc/systemd/system 目录下面以 <Target 名> + .wants 后缀构成的子目录中 4774e851715SGnoCiYeH WantedBy, 4784e851715SGnoCiYeH //依赖当前服务的模块。当前 Unit 激活时(enable)符号链接会放入 /etc/systemd/system 目录下面以 <Target 名> + .required 后缀构成的子目录中 4794e851715SGnoCiYeH RequiredBy, 4804e851715SGnoCiYeH //当前 Unit enable/disable 时,同时 enable/disable 的其他 Unit 4814e851715SGnoCiYeH Also, 4824e851715SGnoCiYeH //别名 4834e851715SGnoCiYeH Alias, 48421fc724cSGnoCiYeH } 485