From 66b24d11167b1e1f24f71c9ee376a3095a45a9d1 Mon Sep 17 00:00:00 2001 From: MggMuggins Date: Sat, 28 Jul 2018 10:41:39 -0500 Subject: [PATCH] Support Directories --- config/default.toml | 7 +++++++ src/config/file.rs | 19 ++++++++++++++----- src/lib.rs | 38 ++++++++++++++++---------------------- 3 files changed, 37 insertions(+), 27 deletions(-) diff --git a/config/default.toml b/config/default.toml index ce7f54d13f..328ea9ec94 100644 --- a/config/default.toml +++ b/config/default.toml @@ -171,3 +171,10 @@ Welcome to Redox OS! path = "/usr" data = "/" symlink = true + +[[files]] +path = "/tmp" +data = "" +directory = true +# 0o1777 +mode = 1023 diff --git a/src/config/file.rs b/src/config/file.rs index 2104fdfe97..ade1e92cdc 100644 --- a/src/config/file.rs +++ b/src/config/file.rs @@ -17,12 +17,14 @@ pub struct FileConfig { pub data: String, #[serde(default)] pub symlink: bool, + #[serde(default)] + pub directory: bool, pub mode: Option, pub uid: Option, pub gid: Option } -// TODO: Rewrite +// TODO: Rewrite impls impl FileConfig { pub(crate) fn create>(self, prefix: P) -> Result<()> { @@ -30,9 +32,12 @@ impl FileConfig { let target_file = prefix.as_ref() .join(path); - println!("target file: {:?}", target_file); - - if let Some(parent) = target_file.parent() { + if self.directory { + println!("Create directory {}", target_file.display()); + fs::create_dir_all(&target_file)?; + self.apply_perms(&target_file)?; + return Ok(()); + } else if let Some(parent) = target_file.parent() { println!("Create file parent {}", parent.display()); fs::create_dir_all(parent)?; } @@ -52,7 +57,11 @@ impl FileConfig { fn apply_perms>(&self, target: P) -> Result<()> { let path = target.as_ref(); - let mode = self.mode.unwrap_or(0o0755); + let mode = self.mode.unwrap_or_else(|| if self.directory { + 0o0755 + } else { + 0o0644 + }); let uid = self.uid.unwrap_or(0); let gid = self.gid.unwrap_or(0); diff --git a/src/lib.rs b/src/lib.rs index ed122a87f0..bda37cea54 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,7 +22,7 @@ use rand::{OsRng, Rng}; use termion::input::TermRead; use pkgutils::{Repo, Package}; -use std::{env, fs}; +use std::env; use std::io::{self, stderr, Write}; use std::path::Path; use std::process::{self, Command}; @@ -32,7 +32,6 @@ pub(crate) type Result = std::result::Result; const REMOTE: &'static str = "https://static.redox-os.org/pkg"; const TARGET: &'static str = "x86_64-unknown-redox"; -const SYSROOT: &'static str = "SYSROOT"; /// Converts a password to a serialized argon2rs hash, understandable /// by redox_users. If the password is blank, the hash is blank. @@ -121,17 +120,6 @@ fn install_packages>(config: &Config, dest: &str, cookbook: Option } pub fn install, S: AsRef>(config: Config, output_dir: P, cookbook: Option) -> Result<()> { - - /// Creates a directory relative to the output directory - fn create_dir_relative>(path: P) -> Result<()> { - let root = env::var(SYSROOT)?; - let target_dir = Path::new(&root) - .join(path.as_ref()); - println!("Create directory {}", target_dir.display()); - fs::create_dir_all(&target_dir)?; - Ok(()) - } - let mut context = liner::Context::new(); macro_rules! prompt { @@ -151,10 +139,6 @@ pub fn install, S: AsRef>(config: Config, output_dir: P, coo let output_dir = output_dir.as_ref(); - // Using an env var here to communicate the root dir to the functions - // instead of passing it as a param - env::set_var(SYSROOT, output_dir.as_os_str()); - println!("Install {:#?} to {}", config, output_dir.display()); // TODO: Mount disk if output is a file @@ -201,7 +185,15 @@ pub fn install, S: AsRef>(config: Config, output_dir: P, coo println!("\tHome: {}", home); println!("\tShell: {}", shell); - create_dir_relative(home.trim_matches('/'))?; + FileConfig { + path: home.clone(), + data: String::new(), + symlink: false, + directory: true, + mode: Some(0o0700), + uid: Some(uid), + gid: Some(gid) + }.create(&output_dir)?; let password = hash_password(&password)?; @@ -213,10 +205,12 @@ pub fn install, S: AsRef>(config: Config, output_dir: P, coo path: "/etc/passwd".to_string(), data: passwd, symlink: false, - mode: Some(0o755), - uid: Some(0), - gid: Some(0) - }.create(output_dir)?; + directory: false, + // Take defaults + mode: None, + uid: None, + gid: None + }.create(&output_dir)?; } Ok(())