diff --git a/src-tauri/src/data/auth.rs b/src-tauri/src/data/auth.rs index a64439d..d1801ae 100644 --- a/src-tauri/src/data/auth.rs +++ b/src-tauri/src/data/auth.rs @@ -97,8 +97,8 @@ pub fn load_user(name: &str, password: &str) -> Result<(), String>{ "fs" => { let file_content = read(&path).unwrap(); let original = aes_decrypt(name, password, &file_content); - unsafe{} - + let original = serde_json::from_slice(&original).unwrap(); + unsafe{FS = original}; } _ => {} diff --git a/src-tauri/src/fs/commands.rs b/src-tauri/src/fs/commands.rs index b41b024..78f84dd 100644 --- a/src-tauri/src/fs/commands.rs +++ b/src-tauri/src/fs/commands.rs @@ -1,6 +1,7 @@ -use std::path::PathBuf; +use std::{fs, path::PathBuf}; use serde::{Deserialize, Serialize}; -use std::fs::read; +use serde_json::Error; +use std::fs::{write,read}; pub static mut FS: Home = Home::new(); @@ -14,13 +15,15 @@ pub fn pwd() -> String{return unsafe { #[tauri::command] pub fn cd(new: String) { - for item in unsafe {FS.current_dir.files.iter()}{ + unsafe{ + for item in FS.current_dir.files.iter(){ let dir = item.get_directory(); if dir.is_none(){continue} // if you use an else block then dir will need to be mutable let dir = dir.unwrap(); - if dir.name == new{FS.cd(dir);} + if dir.name == new{unsafe{FS.cd(dir);}} } + } } #[tauri::command] @@ -31,12 +34,21 @@ pub fn ls() -> Vec { DiretoryItems::File(file) => file.name.clone() }).collect::>()} } +#[tauri::command] +pub fn create_file(file_name: String, location: PathBuf) { + unsafe { + let new_file = File::new(file_name, location); + FS.current_dir.files.push(DiretoryItems::File(new_file)) + + } +} #[derive(Clone, Debug, PartialEq, PartialOrd, Serialize, Deserialize)] pub struct Home{ path: Vec, current_dir: Directory, + } impl Home{ @@ -47,12 +59,17 @@ impl Home{ let bin_dir = Directory::new(String::from("bin")); home_dir.files.push(DiretoryItems::Directory(bin_dir)); self.path.push(home_dir); - println!("path:{:?}", self.path); } + pub fn cd_back(&mut self) {if self.path.len() > 1 {self.path.pop();self.current_dir = self.path.last().unwrap().clone();}} pub fn cd(&mut self, dir: Directory) {self.current_dir = dir.clone();self.path.push(dir);} + pub fn to_bytes(&self) -> Result, Error>{return serde_json::to_vec(self);} } +#[tauri::command] +pub fn mkdir(name: String) {unsafe{FS.current_dir.files.push(DiretoryItems::Directory(Directory::new(name)))};} + + #[derive(Clone, Debug, PartialEq, PartialOrd, Serialize, Deserialize)] pub struct Directory{ files: Vec, @@ -68,6 +85,7 @@ pub enum DiretoryItems{File(File),Directory(Directory)} impl DiretoryItems{ pub fn get_directory(&self) -> Option{match self{Self::Directory(dir)=>{Some(dir.clone())} _=>{None}}} + pub fn get_file(&self) -> Option{match self{Self::File(file)=>{Some(file.clone())} _=>{None}}} } #[derive(Clone, Debug, PartialEq, PartialOrd, Serialize, Deserialize)] @@ -76,17 +94,30 @@ pub struct File{ location: PathBuf, } impl File{ + pub fn new(name: String,location: PathBuf) -> Self {return File {name, location}} + pub fn add_data(&self, data: Vec) -> Option<()>{ + let content = data.join("\n"); + let data = fs::write(self.location.as_path(), content); + if data.is_err(){return None;} + // Ori will work on it later + None + } pub fn open(&self) -> Option> { let data = read(self.location.as_path()); if data.is_err(){return None;} // i'l keep on working on it later None } + } #[test] -fn test_fs(){ +fn test_fs() { let mut home = Home::new(); - home.init_fs(); - + let mut dir = Directory::new(String::from("man")); + home.cd(dir.clone()); + let location = PathBuf::from("Home/man/"); + let new_file = File::new(String::from("salves"), location.clone()); + dir.files.push(DiretoryItems::File(new_file)); + println!("dir files: {:?}", home.path); } \ No newline at end of file diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index b4142c6..f970cc6 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -12,7 +12,7 @@ use base64::{decode_config, encode_config, URL_SAFE}; use crate::fs::encryption::aes_encrypt; use crate::data::auth::{init_dir, save_user, authenticate_user, load_user, user_exists, create_user}; use crate::data::auth::Encodable; -use crate::fs::commands::{pwd, ls, FS, cd}; +use crate::fs::commands::{pwd, ls, FS, cd, mkdir}; pub fn dir() -> PathBuf {data_dir().expect("failed to enter data directory").join("d_vault_data")} pub fn get_user_dir(name: &str, password: &str) -> PathBuf{ @@ -80,6 +80,6 @@ pwd - shows your current path fn main() { tauri::Builder::default().invoke_handler(tauri::generate_handler![ first_init, list_commands, console, user_get, authenticate_user, save_user, user_exists, load_user, ls, pwd, cd, create_user, - create_value, + create_value, mkdir ]).run(tauri::generate_context!()).expect("failed to run the code"); }