From c75a4ed5113846b23bc28a45bab81c5e289af277 Mon Sep 17 00:00:00 2001 From: Ana Hobden Date: Wed, 19 Oct 2022 15:12:50 -0700 Subject: [PATCH] Hammer out a bunch of Mac support --- Cargo.lock | 36 +++++++++++++++++++++ Cargo.toml | 1 + src/actions/base/create_group.rs | 26 ++++++++++++--- src/actions/base/create_user.rs | 10 +++--- src/actions/base/darwin/enable_ownership.rs | 14 +++----- src/lib.rs | 1 + src/os/darwin.rs | 6 ++++ src/os/mod.rs | 1 + src/planner/darwin/multi_user.rs | 18 ++++------- src/settings.rs | 6 ++-- 10 files changed, 86 insertions(+), 33 deletions(-) create mode 100644 src/os/darwin.rs create mode 100644 src/os/mod.rs diff --git a/Cargo.lock b/Cargo.lock index f70f0d4..edde270 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -754,6 +754,7 @@ dependencies = [ "glob", "nix", "owo-colors", + "plist", "reqwest", "serde", "serde_json", @@ -1007,6 +1008,15 @@ version = "0.2.132" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8371e4e5341c3a96db127eb2465ac681ced4c433e01dd0e938adbef26ba93ba5" +[[package]] +name = "line-wrap" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f30344350a2a51da54c1d53be93fade8a237e545dbcc4bdbe635413f2117cab9" +dependencies = [ + "safemem", +] + [[package]] name = "link-cplusplus" version = "1.0.7" @@ -1248,6 +1258,20 @@ version = "0.3.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae" +[[package]] +name = "plist" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd39bc6cdc9355ad1dc5eeedefee696bb35c34caf21768741e81826c0bbd7225" +dependencies = [ + "base64", + "indexmap", + "line-wrap", + "serde", + "time", + "xml-rs", +] + [[package]] name = "polling" version = "2.3.0" @@ -1477,6 +1501,12 @@ version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09" +[[package]] +name = "safemem" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef703b7cb59335eae2eb93ceb664c0eb7ea6bf567079d843e09420219668e072" + [[package]] name = "same-file" version = "1.0.6" @@ -2306,6 +2336,12 @@ dependencies = [ "libc", ] +[[package]] +name = "xml-rs" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2d7d3948613f75c98fd9328cfdcc45acc4d360655289d0a7d4ec931392200a3" + [[package]] name = "xz2" version = "0.1.7" diff --git a/Cargo.toml b/Cargo.toml index 7843ba7..6f6f0e0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,3 +38,4 @@ walkdir = "2.3.2" sxd-xpath = "0.4.2" xz2 = { version = "0.1.7", features = ["static", "tokio"] } sxd-document = "0.3.2" +plist = "1.3.1" diff --git a/src/actions/base/create_group.rs b/src/actions/base/create_group.rs index 6a7a108..28db7ee 100644 --- a/src/actions/base/create_group.rs +++ b/src/actions/base/create_group.rs @@ -62,12 +62,13 @@ impl Actionable for CreateGroup { tracing::debug!("Creating group"); use target_lexicon::OperatingSystem; - match target_lexicon::HOST.operating_system { + match target_lexicon::OperatingSystem::host() { OperatingSystem::MacOSX { major: _, minor: _, patch: _, - } => { + } + | OperatingSystem::Darwin => { execute_command(Command::new("/usr/sbin/dseditgroup").args([ "-o", "create", @@ -131,9 +132,24 @@ impl Actionable for CreateGroup { } tracing::debug!("Deleting group"); - execute_command(Command::new("groupdel").arg(&name)) - .await - .map_err(CreateGroupError::Command)?; + use target_lexicon::OperatingSystem; + match target_lexicon::OperatingSystem::host() { + OperatingSystem::MacOSX { + major: _, + minor: _, + patch: _, + } + | OperatingSystem::Darwin => { + execute_command(Command::new("groupdel").arg(&name)) + .await + .map_err(CreateGroupError::Command)?; + }, + _ => { + execute_command(Command::new("userdel").args([&name.to_string()])) + .await + .map_err(Self::Error::Command)?; + }, + }; tracing::trace!("Deleted group"); *action_state = ActionState::Uncompleted; diff --git a/src/actions/base/create_user.rs b/src/actions/base/create_user.rs index d6654d9..cb52909 100644 --- a/src/actions/base/create_user.rs +++ b/src/actions/base/create_user.rs @@ -68,12 +68,13 @@ impl Actionable for CreateUser { tracing::debug!("Creating user"); use target_lexicon::OperatingSystem; - match target_lexicon::HOST.operating_system { + match target_lexicon::OperatingSystem::host() { OperatingSystem::MacOSX { major: _, minor: _, patch: _, - } => { + } + | OperatingSystem::Darwin => { execute_command(Command::new("/usr/bin/dscl").args([ ".", "create", @@ -155,12 +156,13 @@ impl Actionable for CreateUser { tracing::debug!("Deleting user"); use target_lexicon::OperatingSystem; - match target_lexicon::HOST.operating_system { + match target_lexicon::OperatingSystem::host() { OperatingSystem::MacOSX { major: _, minor: _, patch: _, - } => { + } + | OperatingSystem::Darwin => { todo!() }, _ => { diff --git a/src/actions/base/darwin/enable_ownership.rs b/src/actions/base/darwin/enable_ownership.rs index 3f80eef..b33a56a 100644 --- a/src/actions/base/darwin/enable_ownership.rs +++ b/src/actions/base/darwin/enable_ownership.rs @@ -1,3 +1,4 @@ +use std::io::Cursor; use std::path::{Path, PathBuf}; use serde::Serialize; @@ -6,6 +7,7 @@ use tokio::process::Command; use crate::execute_command; use crate::actions::{Action, ActionDescription, ActionState, Actionable}; +use crate::os::darwin::DiskUtilOutput; #[derive(Debug, serde::Deserialize, serde::Serialize, Clone)] pub struct EnableOwnership { @@ -58,17 +60,9 @@ impl Actionable for EnableOwnership { .await .unwrap() .stdout; - let package = sxd_document::parser::parse(&String::from_utf8(buf).unwrap()).unwrap(); + let the_plist: DiskUtilOutput = plist::from_reader(Cursor::new(buf)).unwrap(); - match sxd_xpath::evaluate_xpath( - &package.as_document(), - "(/plist/dict/key[text()='GlobalPermissionsEnabled'])/following-sibling::*[1]", - ) - .unwrap() - { - sxd_xpath::Value::Boolean(bool) => bool, - _ => panic!("At the other disk i/o!!!"), - } + the_plist.global_permissions_enabled }; if should_enable_ownership { diff --git a/src/lib.rs b/src/lib.rs index 13a6ce9..2751e84 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ mod actions; mod error; +mod os; mod plan; mod planner; mod settings; diff --git a/src/os/darwin.rs b/src/os/darwin.rs new file mode 100644 index 0000000..0ccda9e --- /dev/null +++ b/src/os/darwin.rs @@ -0,0 +1,6 @@ +#[derive(serde::Deserialize)] +#[serde(rename_all = "PascalCase")] +pub struct DiskUtilOutput { + pub parent_whole_disk: String, + pub global_permissions_enabled: bool, +} diff --git a/src/os/mod.rs b/src/os/mod.rs new file mode 100644 index 0000000..942a254 --- /dev/null +++ b/src/os/mod.rs @@ -0,0 +1 @@ +pub mod darwin; diff --git a/src/planner/darwin/multi_user.rs b/src/planner/darwin/multi_user.rs index 22f1902..9af3892 100644 --- a/src/planner/darwin/multi_user.rs +++ b/src/planner/darwin/multi_user.rs @@ -1,3 +1,5 @@ +use std::io::Cursor; + use tokio::process::Command; use crate::{ @@ -6,6 +8,7 @@ use crate::{ Action, ActionError, }, execute_command, + os::darwin::DiskUtilOutput, planner::{Plannable, PlannerError}, InstallPlan, Planner, }; @@ -22,23 +25,14 @@ impl Plannable for DarwinMultiUser { settings: crate::InstallSettings, ) -> Result { let root_disk = { - let root_disk_buf = + let buf = execute_command(Command::new("/usr/sbin/diskutil").args(["info", "-plist", "/"])) .await .unwrap() .stdout; - let package = - sxd_document::parser::parse(&String::from_utf8(root_disk_buf).unwrap()).unwrap(); + let the_plist: DiskUtilOutput = plist::from_reader(Cursor::new(buf)).unwrap(); - match sxd_xpath::evaluate_xpath( - &package.as_document(), - "/plist/dict/key[text()='ParentWholeDisk']/following-sibling::string[1]/text()", - ) - .unwrap() - { - sxd_xpath::Value::String(s) => s, - _ => panic!("At the disk i/o!!!"), - } + the_plist.parent_whole_disk }; let volume_label = "Nix Store".into(); diff --git a/src/settings.rs b/src/settings.rs index e14e6d8..5fca261 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -31,10 +31,12 @@ impl InstallSettings { (Architecture::Aarch64(_), OperatingSystem::Linux) => { url = "https://releases.nixos.org/nix/nix-2.11.0/nix-2.11.0-aarch64-linux.tar.xz"; }, - (Architecture::X86_64, OperatingSystem::MacOSX { .. }) => { + (Architecture::X86_64, OperatingSystem::MacOSX { .. }) + | (Architecture::X86_64, OperatingSystem::Darwin) => { url = "https://releases.nixos.org/nix/nix-2.11.0/nix-2.11.0-x86_64-darwin.tar.xz"; }, - (Architecture::Aarch64(_), OperatingSystem::MacOSX { .. }) => { + (Architecture::Aarch64(_), OperatingSystem::MacOSX { .. }) + | (Architecture::Aarch64(_), OperatingSystem::Darwin) => { url = "https://releases.nixos.org/nix/nix-2.11.0/nix-2.11.0-aarch64-darwin.tar.xz"; }, _ => {