1use std::{fs::File, io::Write as _, path::Path};
2
3use crate::input::Opts;
4use e_utils::{parse::AutoPath, system::cmd::CmdResult};
5
6pub const SYSTEM_WIN32: &'static str = "C:\\windows\\system32";
8pub type CmdRes = CmdResult<Opts>;
10use e_utils::Result;
11
12pub fn write_bytes_file(path: impl AsRef<Path>, data: &[u8]) -> Result<()> {
14 let path = path.as_ref();
15 if !path.exists() {
16 path.auto_create_dir()?;
17 let mut temp_file = File::create(path)?;
19 temp_file.write_all(data)?;
20 }
21 Ok(())
22}
23
24pub fn default_cmd_res() -> CmdRes {
26 CmdRes {
27 content: String::new(),
28 status: false,
29 opts: Opts::default(),
30 }
31}
32
33pub fn cmd_res(opts: Opts) -> CmdRes {
35 CmdRes {
36 content: String::new(),
37 status: false,
38 opts,
39 }
40 }
41#[cfg(target_os = "windows")]
43#[cfg(feature = "shell")]
44pub fn get_powershell_path() -> String {
45 unsafe {
46 let key_path: Vec<u16> = "SOFTWARE\\Microsoft\\PowerShell\\3\\PowerShellEngine"
47 .encode_utf16()
48 .chain(Some(0))
49 .collect();
50 let value_name: Vec<u16> = "ApplicationBase"
51 .encode_utf16()
52 .chain(Some(0))
53 .collect();
54 let mut key: winapi::shared::minwindef::HKEY = std::ptr::null_mut();
55
56 let result: winapi::um::winreg::LSTATUS = winapi::um::winreg::RegOpenKeyExW(
57 winapi::um::winreg::HKEY_LOCAL_MACHINE,
58 key_path.as_ptr(),
59 0,
60 winapi::um::winnt::KEY_READ,
61 &mut key,
62 );
63
64 if result == winapi::shared::winerror::ERROR_SUCCESS as i32 {
65 let mut value_len: winapi::shared::minwindef::DWORD = 0;
66 let query_result = winapi::um::winreg::RegQueryValueExW(
67 key,
68 value_name.as_ptr(),
69 std::ptr::null_mut(),
70 std::ptr::null_mut(),
71 std::ptr::null_mut(),
72 &mut value_len,
73 );
74
75 if query_result == winapi::shared::winerror::ERROR_SUCCESS as i32 {
76 let mut buffer: Vec<u16> = vec![0; (value_len / 2) as usize];
77 let query_result = winapi::um::winreg::RegQueryValueExW(
78 key,
79 value_name.as_ptr(),
80 std::ptr::null_mut(),
81 std::ptr::null_mut(),
82 buffer.as_mut_ptr() as *mut u8,
83 &mut value_len,
84 );
85
86 if query_result == winapi::shared::winerror::ERROR_SUCCESS as i32{
87 winapi::um::winreg::RegCloseKey(key);
88 return String::from_utf16_lossy(&buffer).trim_end_matches('\0').to_string();
89 }
90 }
91
92 winapi::um::winreg::RegCloseKey(key);
93 }
94 }
95
96 let system32_path = std::env::var("SYSTEMROOT").unwrap_or_else(|_| "C:\\Windows\\System32".to_string());
98 let power_path = Path::new(&system32_path).join("WindowsPowerShell").join("v1.0");
99
100 if power_path.exists() {
101 return power_path.to_string_lossy().into_owned();
102 }
103
104 String::new()
105}