From a56ec333ae5a46b32d255ac5536ca6629165e1b5 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Mon, 9 Jan 2017 15:23:30 -0700 Subject: [PATCH] Add sysroot config --- .gitignore | 1 + config/default.toml | 9 +++++++++ src/config/general.rs | 3 ++- src/config/package.rs | 6 +++--- src/install.rs | 36 ++++++++++++++++++++++++++++++++++-- 5 files changed, 49 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index fa8d85ac52..7661fb4fad 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ Cargo.lock +sysroot target diff --git a/config/default.toml b/config/default.toml index f8bef798ba..48b87a5362 100644 --- a/config/default.toml +++ b/config/default.toml @@ -7,7 +7,16 @@ prompt = false # Package settings [packages] +acid = {} +coreutils = {} +extrautils = {} +games = {} +ion = {} +netutils = {} +pkgutils = {} orbutils = {} +smith = {} +userutils = {} # User settings [users.root] diff --git a/src/config/general.rs b/src/config/general.rs index f247293663..0fcdb03015 100644 --- a/src/config/general.rs +++ b/src/config/general.rs @@ -1,4 +1,5 @@ #[derive(Debug, Default, Deserialize)] pub struct GeneralConfig { - pub prompt: bool + pub prompt: bool, + pub sysroot: Option } diff --git a/src/config/package.rs b/src/config/package.rs index 2a01d77cff..005ca3acf0 100644 --- a/src/config/package.rs +++ b/src/config/package.rs @@ -1,6 +1,6 @@ #[derive(Debug, Default, Deserialize)] pub struct PackageConfig { - pub version: String, - pub git: String, - pub path: String, + pub version: Option, + pub git: Option, + pub path: Option, } diff --git a/src/install.rs b/src/install.rs index f1fbd2b24f..4bd0df0c66 100644 --- a/src/install.rs +++ b/src/install.rs @@ -6,7 +6,9 @@ extern crate userutils; use self::rand::Rng; use self::termion::input::TermRead; +use std::{env, fs}; use std::io::{self, Write}; +use std::path::Path; use std::str::FromStr; use config::Config; @@ -57,6 +59,19 @@ fn prompt_password(prompt: &str, confirm_prompt: &str) -> Result } } +pub fn dir(path: &Path) -> Result<(), String> { + println!("Create directory {}", path.display()); + fs::create_dir(path).map_err(|err| format!("failed to create {}: {}", path.display(), err))?; + Ok(()) +} + +pub fn file(path: &Path, data: &[u8]) -> Result<(), String> { + println!("Create file {}", path.display()); + let mut file = fs::File::create(path).map_err(|err| format!("failed to create {}: {}", path.display(), err))?; + file.write_all(data).map_err(|err| format!("failed to write {}: {}", path.display(), err))?; + Ok(()) +} + pub fn install(config: Config) -> Result<(), String> { println!("Install {:#?}", config); @@ -77,6 +92,15 @@ pub fn install(config: Config) -> Result<(), String> { }) } + let sysroot = { + let mut wd = env::current_dir().map_err(|err| format!("failed to get current dir: {}", err))?; + let path = prompt!(config.general.sysroot, "sysroot".to_string(), "sysroot [sysroot]: ")?; + wd.push(path); + wd + }; + + println!("Using sysroot: {}", sysroot.display()); + let mut passwd = String::new(); let mut next_uid = 1000; @@ -101,7 +125,7 @@ pub fn install(config: Config) -> Result<(), String> { let home = prompt!(user.home, format!("/home/{}", username), "{}: home [/home/{}]: ", username, username)?; let shell = prompt!(user.shell, "/bin/ion".to_string(), "{}: shell [/bin/ion]: ", username)?; - println!("Creating user {}:", username); + println!("Adding user {}:", username); println!("\tPassword: {}", password); println!("\tUID: {}", uid); println!("\tGID: {}", gid); @@ -112,7 +136,15 @@ pub fn install(config: Config) -> Result<(), String> { passwd.push_str(&format!("{};{};{};{};{};{};{}\n", username, password, uid, gid, name, home, shell)); } - print!("/etc/passwd:\n{}", passwd); + dir(&sysroot)?; + + let mut etc = sysroot.clone(); + etc.push("etc"); + dir(&etc)?; + + let mut etc_passwd = etc.clone(); + etc_passwd.push("passwd"); + file(&etc_passwd, passwd.as_bytes())?; Ok(()) }