This commit is contained in:
Penwing 2024-01-26 08:33:55 +01:00
parent 877beb06f4
commit 2b39a1c695

View file

@ -10,117 +10,122 @@ use std::process::Stdio;
use std::{env, path::Path, path::PathBuf, process::Command}; use std::{env, path::Path, path::PathBuf, process::Command};
pub struct Buffer { pub struct Buffer {
pub output_buffer: BufReader<std::process::ChildStdout>, pub output_buffer: BufReader<std::process::ChildStdout>,
pub error_buffer: BufReader<std::process::ChildStderr>, pub error_buffer: BufReader<std::process::ChildStderr>,
} }
pub struct CommandEntry { pub struct CommandEntry {
pub env: String, pub env: String,
pub command: String, pub command: String,
pub output: String, pub output: String,
pub error: String, pub error: String,
pub buffer: Option<Buffer>, pub buffer: Option<Buffer>,
} }
impl CommandEntry { impl CommandEntry {
pub fn new(command: String) -> Self { pub fn new(env: String, command: String) -> Self {
let (buffer, error) = match execute(command.clone()) { let (buffer, error) = match execute(command.clone()) {
Ok(command_buffer) => (Some(command_buffer), String::new()), Ok(command_buffer) => (Some(command_buffer), String::new()),
Err(err) => (None, format!("failed to get results: {}", err)), Err(err) => (None, format!("failed to get results: {}", err)),
}; };
CommandEntry { CommandEntry {
env: format_path(&env::current_dir().unwrap_or_else(|_| PathBuf::from("/"))), env,
command, command,
output: String::new(), output: String::new(),
error, error,
buffer, buffer,
} }
} }
pub fn update(&mut self) { pub fn update(&mut self) {
if let Some(buffer) = &mut self.buffer { if let Some(buffer) = &mut self.buffer {
for line in buffer.output_buffer.by_ref().lines() { for line in buffer.output_buffer.by_ref().lines() {
match line { match line {
Ok(line) => self.output += &format!("{}\n", line), Ok(line) => self.output += &format!("{}\n", line),
Err(_) => return, Err(_) => return,
} }
} }
for line in buffer.error_buffer.by_ref().lines() { for line in buffer.error_buffer.by_ref().lines() {
match line { match line {
Ok(line) => self.error += &format!("{}\n", line), Ok(line) => self.error += &format!("{}\n", line),
Err(_) => return, Err(_) => return,
} }
} }
} }
} }
} }
pub fn send_command(command: String) -> CommandEntry { pub fn send_command(command: String) -> CommandEntry {
if command.len() < 2 { let env = format_path(&env::current_dir().unwrap_or_else(|_| PathBuf::from("/")));
return CommandEntry::new(command);
}
if &command[..2] != "cd" { if command.len() < 2 {
return CommandEntry::new(command); return CommandEntry::new(env, command);
} }
if command.len() < 4 { if &command[..2] != "cd" {
let mut entry = CommandEntry::new("echo Invalid cd, should provide path >&2".to_string()); return CommandEntry::new(env, command);
entry.command = command; }
return entry;
}
let path_append = command[3..].replace('~', "/home/penwing"); if command.len() < 4 {
let path = Path::new(&path_append); let mut entry =
CommandEntry::new(env, "echo Invalid cd, should provide path >&2".to_string());
entry.command = command;
return entry;
}
if format!("{}", path.display()) == "/" { let path_append = command[3..].replace('~', "/home/penwing");
let mut entry = CommandEntry::new("echo Root access denied >&2".to_string()); let path = Path::new(&path_append);
entry.command = command;
return entry;
}
if env::set_current_dir(path).is_ok() { if format!("{}", path.display()) == "/" {
let mut entry = CommandEntry::new(format!("echo Moved to : {}", path.display())); let mut entry = CommandEntry::new(env, "echo Root access denied >&2".to_string());
entry.command = command; entry.command = command;
entry return entry;
} else { }
let mut entry =
CommandEntry::new(format!("echo Could not find path : {} >&2", path.display())); if env::set_current_dir(path).is_ok() {
entry.command = command; let mut entry = CommandEntry::new(env, format!("echo Moved to : {}", path.display()));
entry entry.command = command;
} entry
} else {
let mut entry = CommandEntry::new(
env,
format!("echo Could not find path : {} >&2", path.display()),
);
entry.command = command;
entry
}
} }
pub fn execute(command: String) -> Result<Buffer, std::io::Error> { pub fn execute(command: String) -> Result<Buffer, std::io::Error> {
let mut child = Command::new("sh") let mut child = Command::new("sh")
.arg("-c") .arg("-c")
.arg(command.clone()) .arg(command.clone())
.stdout(Stdio::piped()) .stdout(Stdio::piped())
.stderr(Stdio::piped()) .stderr(Stdio::piped())
.spawn()?; .spawn()?;
let stdout = child let stdout = child
.stdout .stdout
.take() .take()
.ok_or_else(|| std::io::Error::new(std::io::ErrorKind::Other, "Failed to open stdout"))?; .ok_or_else(|| std::io::Error::new(std::io::ErrorKind::Other, "Failed to open stdout"))?;
let stderr = child let stderr = child
.stderr .stderr
.take() .take()
.ok_or_else(|| std::io::Error::new(std::io::ErrorKind::Other, "Failed to open stderr"))?; .ok_or_else(|| std::io::Error::new(std::io::ErrorKind::Other, "Failed to open stderr"))?;
let stdout_fd = stdout.as_raw_fd(); let stdout_fd = stdout.as_raw_fd();
let stderr_fd = stderr.as_raw_fd(); let stderr_fd = stderr.as_raw_fd();
fcntl(stdout_fd, FcntlArg::F_SETFL(OFlag::O_NONBLOCK))?; fcntl(stdout_fd, FcntlArg::F_SETFL(OFlag::O_NONBLOCK))?;
fcntl(stderr_fd, FcntlArg::F_SETFL(OFlag::O_NONBLOCK))?; fcntl(stderr_fd, FcntlArg::F_SETFL(OFlag::O_NONBLOCK))?;
let output_buffer = BufReader::new(stdout); let output_buffer = BufReader::new(stdout);
let error_buffer = BufReader::new(stderr); let error_buffer = BufReader::new(stderr);
Ok(Buffer { Ok(Buffer {
output_buffer, output_buffer,
error_buffer, error_buffer,
}) })
} }