implemented push and pop commands, push command saves the wrong path though

This commit is contained in:
2024-11-27 23:44:47 +01:00
parent 51ea69194d
commit ce096d2430
4 changed files with 113 additions and 38 deletions
+55 -13
View File
@@ -1,9 +1,9 @@
use std::io::{Result};
use std::fs;
use std::fs::File;
use std::path::{PathBuf};
use std::str::FromStr;
use std::io::{Result, Error, ErrorKind};
use sysinfo::{System, Pid};
@@ -11,6 +11,7 @@ use sysinfo::{System, Pid};
pub struct Stack
{
pid: u32,
path: PathBuf,
stack: Vec<PathBuf>,
}
@@ -20,29 +21,57 @@ impl Stack
pub fn new(process_id: u32) -> Result<Self> {
let mut stack: Stack = Stack{
pid : process_id,
path : PathBuf::new(),
stack : Vec::<PathBuf>::new(),
};
stack.stack = stack.build_stack()?;
_ = stack.build_stack()?;
return Ok(stack);
}
/**
* return stack
*/
pub fn get_stack(&mut self) -> Result<&Vec<PathBuf>> {
return Ok(&self.stack);
}
/**
* push entry to stack
* returns updated stack
*/
pub fn push_entry(&mut self, path: PathBuf) -> Vec<PathBuf> {
self.stack.push(path);
return self.stack.clone();
pub fn push_entry(&mut self, path: &PathBuf) -> Result<&Vec<PathBuf>> {
let abs_path = path.canonicalize()?;
self.stack.push(abs_path);
self.write_stack_file()?;
return Ok(&self.stack);
}
/**
* pop entry from stack
* return poppe entry
*/
pub fn pop_entry(&mut self) -> PathBuf {
return self.stack.pop().unwrap();
pub fn pop_entry(&mut self) -> Result<PathBuf> {
let entry = self.stack.pop();
self.write_stack_file()?;
return match entry {
Some(entry) => Ok(entry),
None => Err(Error::new(ErrorKind::Other, "pop failed to retrieve item from stack")),
}
}
/**
* get entry by number
* return nth last entry
*/
pub fn get_entry_by_number(&mut self, entry_number: u32) -> Result<&PathBuf> {
if entry_number > (self.stack.len() as u32 - 1) {
return Err(Error::new(ErrorKind::Other, "requested entry number is out of bounds"));
}
// index from the end of the vector as new entries are appended at the end of the list
return Ok(&self.stack[self.stack.len() - (entry_number as usize)]);
}
/**
@@ -69,14 +98,14 @@ impl Stack
let _ = fs::create_dir(stack_dir.clone())?;
}
let mut stack_file_path = stack_dir.clone();
stack_file_path.push(PathBuf::from(&self.pid.to_string()));
if stack_file_path.is_file() {
self.path = stack_dir.clone();
self.path.push(PathBuf::from(&self.pid.to_string()));
if self.path.is_file() {
// read and parse stack file
let _ = self.parse_stack_file(stack_file_path);
_ = self.read_stack_file(&self.path.clone())?;
} else {
// create stack file and store current path
let _ = File::create(stack_file_path.clone())?;
_ = File::create(self.path.clone())?;
}
return Ok(vec![stack_dir]);
@@ -85,7 +114,7 @@ impl Stack
/**
* parse stack file
*/
fn parse_stack_file(&mut self, stack_file_path: PathBuf) -> Result<()> {
fn read_stack_file(&mut self, stack_file_path: &PathBuf) -> Result<()> {
let stack = fs::read_to_string(stack_file_path)?;
let stack = stack.split("\n");
for entry in stack {
@@ -95,5 +124,18 @@ impl Stack
return Ok(());
}
/**
* write stack current stack to file to save it for next execution
*/
fn write_stack_file(&mut self) -> Result<()> {
let mut output = Vec::<&str>::new();
for entry in &self.stack {
output.push(entry.to_str().expect("failed to convert stack entry to string"));
}
fs::write(self.path.clone(), output.join("\n"))?;
return Ok(())
}
} // end `impl database`