diff --git a/src/config/file.rs b/src/config/file.rs index d56758bcba..f1da3c723c 100644 --- a/src/config/file.rs +++ b/src/config/file.rs @@ -41,10 +41,42 @@ pub struct FileConfig { pub gid: Option, #[serde(default)] pub recursive_chown: bool, + #[serde(default)] + pub postinstall: bool, } // TODO: Rewrite impls impl FileConfig { + pub fn new_file(path: String, data: String) -> FileConfig { + FileConfig { + path, + data, + ..Default::default() + } + } + + pub fn new_directory(path: String) -> FileConfig { + FileConfig { + path, + data: String::new(), + directory: true, + ..Default::default() + } + } + + pub fn with_mod(&mut self, mode: u32, uid: u32, gid: u32) -> &mut FileConfig { + self.mode = Some(mode); + self.uid = Some(uid); + self.gid = Some(gid); + self + } + + pub fn with_recursive_mod(&mut self, mode: u32, uid: u32, gid: u32) -> &mut FileConfig { + self.with_mod(mode, uid, gid); + self.recursive_chown = true; + self + } + pub(crate) fn create>(&self, prefix: P) -> Result<()> { let path = self.path.trim_start_matches('/'); let target_file = prefix.as_ref().join(path); diff --git a/src/lib.rs b/src/lib.rs index f7f18c7193..7c09808e44 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -205,11 +205,19 @@ pub fn install_dir( let output_dir = output_dir.to_owned(); for file in &config.files { - file.create(&output_dir)?; + if !file.postinstall { + file.create(&output_dir)?; + } } install_packages(&config, output_dir.to_str().unwrap(), cookbook); + for file in &config.files { + if file.postinstall { + file.create(&output_dir)?; + } + } + let mut passwd = String::new(); let mut shadow = String::new(); let mut next_uid = 1000; @@ -271,17 +279,9 @@ pub fn install_dir( println!("\tHome: {home}"); println!("\tShell: {shell}"); - FileConfig { - path: home.clone(), - data: String::new(), - symlink: false, - directory: true, - mode: Some(0o0700), - uid: Some(uid), - gid: Some(gid), - recursive_chown: true, - } - .create(&output_dir)?; + FileConfig::new_directory(home.clone()) + .with_recursive_mod(0o777, uid, gid) + .create(&output_dir)?; if uid >= 1000 { // Create XDG user dirs @@ -301,21 +301,14 @@ pub fn install_dir( ".local/share/Trash", ".local/share/Trash/info", ] { - FileConfig { - path: format!("{}/{}", home, xdg_folder), - data: String::new(), - symlink: false, - directory: true, - mode: Some(0o0700), - uid: Some(uid), - gid: Some(gid), - recursive_chown: false, - } - .create(&output_dir)?; + FileConfig::new_directory(format!("{}/{}", home, xdg_folder)) + .with_mod(0o0700, uid, gid) + .create(&output_dir)?; } - FileConfig { - path: format!("{}/.config/user-dirs.dirs", home), - data: r#"# Produced by redox installer + + FileConfig::new_file( + format!("{}/.config/user-dirs.dirs", home), + r#"# Produced by redox installer XDG_DESKTOP_DIR="$HOME/Desktop" XDG_DOCUMENTS_DIR="$HOME/Documents" XDG_DOWNLOAD_DIR="$HOME/Downloads" @@ -326,13 +319,8 @@ XDG_TEMPLATES_DIR="$HOME/Templates" XDG_VIDEOS_DIR="$HOME/Videos" "# .to_string(), - symlink: false, - directory: false, - mode: Some(0o0600), - uid: Some(uid), - gid: Some(gid), - recursive_chown: false, - } + ) + .with_mod(0o0600, uid, gid) .create(&output_dir)?; } @@ -356,32 +344,13 @@ XDG_VIDEOS_DIR="$HOME/Videos" } if !passwd.is_empty() { - FileConfig { - path: "/etc/passwd".to_string(), - data: passwd, - symlink: false, - directory: false, - // Take defaults - mode: None, - uid: None, - gid: None, - recursive_chown: false, - } - .create(&output_dir)?; + FileConfig::new_file("/etc/passwd".to_string(), passwd).create(&output_dir)?; } if !shadow.is_empty() { - FileConfig { - path: "/etc/shadow".to_string(), - data: shadow, - symlink: false, - directory: false, - mode: Some(0o0600), - uid: Some(0), - gid: Some(0), - recursive_chown: false, - } - .create(&output_dir)?; + FileConfig::new_file("/etc/shadow".to_string(), shadow) + .with_mod(0o0600, 0, 0) + .create(&output_dir)?; } if !groups.is_empty() { @@ -396,18 +365,9 @@ XDG_VIDEOS_DIR="$HOME/Videos" println!("\tMembers: {}", members.join(", ")); } - FileConfig { - path: "/etc/group".to_string(), - data: groups_data, - symlink: false, - directory: false, - // Take defaults - mode: None, - uid: None, - gid: None, - recursive_chown: false, - } - .create(&output_dir)?; + FileConfig::new_file("/etc/group".to_string(), groups_data) + .with_mod(0o0600, 0, 0) + .create(&output_dir)?; } Ok(())