🧹 drop unsafe at pages.rs

This commit is contained in:
Vladislav Nepogodin 2023-02-18 01:12:51 +04:00
parent 33cf5d7fa4
commit c40aee852a
No known key found for this signature in database
GPG Key ID: B62C3D10C54D5DA9
1 changed files with 46 additions and 54 deletions

View File

@ -13,9 +13,8 @@ use gtk::prelude::*;
use std::str; use std::str;
use subprocess::{Exec, Redirection}; use subprocess::{Exec, Redirection};
static mut g_local_units: Lazy<Mutex<SystemdUnits>> = Lazy::new(|| Mutex::new(SystemdUnits::new())); static G_LOCAL_UNITS: Lazy<Mutex<SystemdUnits>> = Lazy::new(|| Mutex::new(SystemdUnits::new()));
static mut g_global_units: Lazy<Mutex<SystemdUnits>> = static G_GLOBAL_UNITS: Lazy<Mutex<SystemdUnits>> = Lazy::new(|| Mutex::new(SystemdUnits::new()));
Lazy::new(|| Mutex::new(SystemdUnits::new()));
fn create_fixes_section() -> gtk::Box { fn create_fixes_section() -> gtk::Box {
let topbox = gtk::Box::new(gtk::Orientation::Vertical, 2); let topbox = gtk::Box::new(gtk::Orientation::Vertical, 2);
@ -128,8 +127,8 @@ fn create_options_section() -> gtk::Box {
for btn in &[&psd_btn, &systemd_oomd_btn, &apparmor_btn, &bluetooth_btn, &ananicy_cpp_btn] { for btn in &[&psd_btn, &systemd_oomd_btn, &apparmor_btn, &bluetooth_btn, &ananicy_cpp_btn] {
unsafe { unsafe {
let data: &str = *btn.data("actionData").unwrap().as_ptr(); let data: &str = *btn.data("actionData").unwrap().as_ptr();
if g_local_units.lock().unwrap().enabled_units.contains(&String::from(data)) if G_LOCAL_UNITS.lock().unwrap().enabled_units.contains(&String::from(data))
|| g_global_units.lock().unwrap().enabled_units.contains(&String::from(data)) || G_GLOBAL_UNITS.lock().unwrap().enabled_units.contains(&String::from(data))
{ {
btn.set_active(true); btn.set_active(true);
} }
@ -144,7 +143,7 @@ fn create_options_section() -> gtk::Box {
matches!(alpm.localdb().pkg(pkg_name.as_bytes()), Ok(_)) matches!(alpm.localdb().pkg(pkg_name.as_bytes()), Ok(_))
}; };
let is_local_service_enabled = |service_unit_name: &str| -> bool { let is_local_service_enabled = |service_unit_name: &str| -> bool {
let local_units = unsafe { &g_local_units.lock().unwrap().enabled_units }; let local_units = &G_LOCAL_UNITS.lock().unwrap().enabled_units;
local_units.contains(&String::from(service_unit_name)) local_units.contains(&String::from(service_unit_name))
}; };
@ -231,49 +230,45 @@ fn create_apps_section() -> Option<gtk::Box> {
} }
fn load_enabled_units() { fn load_enabled_units() {
unsafe { G_LOCAL_UNITS.lock().unwrap().loaded_units.clear();
g_local_units.lock().unwrap().loaded_units.clear(); G_LOCAL_UNITS.lock().unwrap().enabled_units.clear();
g_local_units.lock().unwrap().enabled_units.clear();
let mut exec_out = Exec::shell("systemctl list-unit-files -q --no-pager | tr -s \" \"") let mut exec_out = Exec::shell("systemctl list-unit-files -q --no-pager | tr -s \" \"")
.stdout(Redirection::Pipe) .stdout(Redirection::Pipe)
.capture() .capture()
.unwrap() .unwrap()
.stdout_str(); .stdout_str();
exec_out.pop(); exec_out.pop();
let service_list = exec_out.split('\n'); let service_list = exec_out.split('\n');
for service in service_list { for service in service_list {
let out: Vec<&str> = service.split(' ').collect(); let out: Vec<&str> = service.split(' ').collect();
g_local_units.lock().unwrap().loaded_units.push(String::from(out[0])); G_LOCAL_UNITS.lock().unwrap().loaded_units.push(String::from(out[0]));
if out[1] == "enabled" { if out[1] == "enabled" {
g_local_units.lock().unwrap().enabled_units.push(String::from(out[0])); G_LOCAL_UNITS.lock().unwrap().enabled_units.push(String::from(out[0]));
}
} }
} }
} }
fn load_global_enabled_units() { fn load_global_enabled_units() {
unsafe { G_GLOBAL_UNITS.lock().unwrap().loaded_units.clear();
g_global_units.lock().unwrap().loaded_units.clear(); G_GLOBAL_UNITS.lock().unwrap().enabled_units.clear();
g_global_units.lock().unwrap().enabled_units.clear();
let mut exec_out = let mut exec_out =
Exec::shell("systemctl --global list-unit-files -q --no-pager | tr -s \" \"") Exec::shell("systemctl --global list-unit-files -q --no-pager | tr -s \" \"")
.stdout(Redirection::Pipe) .stdout(Redirection::Pipe)
.capture() .capture()
.unwrap() .unwrap()
.stdout_str(); .stdout_str();
exec_out.pop(); exec_out.pop();
let service_list = exec_out.split('\n'); let service_list = exec_out.split('\n');
for service in service_list { for service in service_list {
let out: Vec<&str> = service.split(' ').collect(); let out: Vec<&str> = service.split(' ').collect();
g_global_units.lock().unwrap().loaded_units.push(String::from(out[0])); G_GLOBAL_UNITS.lock().unwrap().loaded_units.push(String::from(out[0]));
if out[1] == "enabled" { if out[1] == "enabled" {
g_global_units.lock().unwrap().enabled_units.push(String::from(out[0])); G_GLOBAL_UNITS.lock().unwrap().enabled_units.push(String::from(out[0]));
}
} }
} }
} }
@ -378,21 +373,18 @@ fn on_servbtn_clicked(button: &gtk::CheckButton) {
let (user_only, pkexec_only) = let (user_only, pkexec_only) =
if action_type == "user_service" { ("--user", "--user $(logname)") } else { ("", "") }; if action_type == "user_service" { ("--user", "--user $(logname)") } else { ("", "") };
let cmd: String; let local_units = &G_LOCAL_UNITS.lock().unwrap().enabled_units;
unsafe { let cmd = if !local_units.contains(&String::from(action_data)) {
let local_units = &g_local_units.lock().unwrap().enabled_units; format!(
cmd = if !local_units.contains(&String::from(action_data)) { "/sbin/pkexec {pkexec_only} bash -c \"systemctl {user_only} enable --now --force \
format!( {action_data}\""
"/sbin/pkexec {pkexec_only} bash -c \"systemctl {user_only} enable --now --force \ )
{action_data}\"" } else {
) format!(
} else { "/sbin/pkexec {pkexec_only} bash -c \"systemctl {user_only} disable --now \
format!( {action_data}\""
"/sbin/pkexec {pkexec_only} bash -c \"systemctl {user_only} disable --now \ )
{action_data}\"" };
)
};
}
// Spawn child process in separate thread. // Spawn child process in separate thread.
std::thread::spawn(move || { std::thread::spawn(move || {