Skip to content

Commit

Permalink
using once_cell
Browse files Browse the repository at this point in the history
  • Loading branch information
soohoonc committed May 14, 2024
1 parent 84871cc commit 4034cd5
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 53 deletions.
2 changes: 1 addition & 1 deletion src/wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ wasm-bindgen = "0.2.83"
serde = { version = "1.0.193", features = ["derive"] }
serde_json = { version = "1.0.108" }
web-sys = { version = "0.3.66", features=["console"]}
include_dir = "0.7.3"
once_cell = "1.19.0"
36 changes: 19 additions & 17 deletions src/wasm/src/filesystem/create.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
use std::cell::RefCell;
use std::rc::Rc;


// use std::cell::RefCell;
// use std::rc::Rc;
use std::path::Path;
use std::sync::LazyLock;
use once_cell::sync::Lazy;
use std::sync::{Arc, Mutex};

use crate::filesystem::node::Node;
use crate::filesystem::node::NodeType;

use web_sys::console;

static ROOT_NODE: LazyLock<Rc<RefCell<Node>>> = LazyLock::new(|| {
let root_path = Path::new("../../assets/fs");
create_fs(root_path)
static ROOT_NODE: Lazy<Arc<Mutex<Node>>> = Lazy::new(|| {
return create_fs(&Path::new("../../assets/fs"));
});

pub fn get_root_node() -> Rc<RefCell<Node>> {
ROOT_NODE.lock().unwrap().clone()
pub fn get_root_node() -> Arc<Mutex<Node>> {
ROOT_NODE.clone()
}

fn traverse(path: &Path, node: Rc<RefCell<Node>>) {
fn traverse(path: &Path, node: Arc<Mutex<Node>>) {

console::log_1(&path.display().to_string().into());

Expand All @@ -39,27 +41,27 @@ fn traverse(path: &Path, node: Rc<RefCell<Node>>) {
let child_node;
if child_path.is_dir() {
console::log_1(&"directory".into());
child_node = Rc::new(RefCell::new(Node::new(child_name, NodeType::Directory, Some(node.clone()))));
traverse(&child_path, Rc::clone(&child_node));
child_node = Arc::new(Mutex::new(Node::new(child_name, NodeType::Directory, Some(node.clone()))));
traverse(&child_path, Arc::clone(&child_node));
} else if child_path.is_file() {
console::log_1(&"file".into());
child_node = Rc::new(RefCell::new(Node::new(child_name, NodeType::File, Some(node.clone()))));
child_node = Arc::new(Mutex::new(Node::new(child_name, NodeType::File, Some(node.clone()))));
} else {
continue;
}

match node.borrow().get_node_type() {
match node.lock().unwrap().get_node_type() {
NodeType::File => continue,
NodeType::Directory => {
node.borrow_mut().add_child(Rc::clone(&child_node));
node.lock().unwrap().add_child(Arc::clone(&child_node));
}
}
}
}

fn create_fs (root_path: &Path) -> Rc<RefCell<Node>> {
let root = Rc::new(RefCell::new(Node::new("root".to_string(), NodeType::Directory, None)));
fn create_fs (root_path: &Path) -> Arc<Mutex<Node>> {
let root = Arc::new(Mutex::new(Node::new("root".to_string(), NodeType::Directory, None)));
// read files in root_path and create a filesystem structure, traverse directories
traverse(root_path, Rc::clone(&root));
traverse(root_path, Arc::clone(&root));
root
}
15 changes: 8 additions & 7 deletions src/wasm/src/filesystem/directory.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use std::cell::RefCell;
use std::rc::Rc;
// use std::cell::RefCell;
// use std::rc::Rc;
use std::sync::{Arc, Mutex};
use crate::filesystem::node::Node;

pub struct Directory {
name: String,
node: Rc<RefCell<Node>>,
node: Arc<Mutex<Node>>,
}

impl Directory {
// pub fn new(name: String, node: Rc<RefCell<Node>>) -> Directory {
// pub fn new(name: String, node: Arc<Mutex<Node>>) -> Directory {
// Directory {
// name,
// node,
Expand All @@ -26,18 +27,18 @@ impl Directory {
// }
// }

// pub fn get_parent(&self) -> Option<Rc<RefCell<Node>>> {
// pub fn get_parent(&self) -> Option<Arc<Mutex<Node>>> {
// match &self.node.borrow().get_parent() {
// Some(parent) => Some(Rc::clone(parent)),
// None => None,
// }
// }

// pub fn get_children(&self) -> Vec<Rc<RefCell<Node>>> {
// pub fn get_children(&self) -> Vec<Arc<Mutex<Node>>> {
// self.node.borrow().get_children().clone()
// }

// pub fn add_child(&mut self, child: Rc<RefCell<Node>>) {
// pub fn add_child(&mut self, child: Arc<Mutex<Node>>) {
// self.node.borrow_mut().get_children().push(Rc::clone(&child));
// }
}
7 changes: 4 additions & 3 deletions src/wasm/src/filesystem/file.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use std::{cell::RefCell, rc::Rc};
// use std::{cell::RefCell, rc::Rc};
use std::sync::{Arc, Mutex};

use crate::filesystem::node::Node;


pub struct File {
name: String,
data: String,
node: Rc<RefCell<Node>>
node: Arc<Mutex<Node>>
}

impl File {
pub fn new(name: String, data: String, node: Rc<RefCell<Node>>) -> File {
pub fn new(name: String, data: String, node: Arc<Mutex<Node>>) -> File {
File {
name,
data,
Expand Down
25 changes: 13 additions & 12 deletions src/wasm/src/filesystem/node.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
use std::rc::Rc;
use std::cell::RefCell;
// use std::rc::Rc;
// use std::cell::RefCell;
use std::sync::{Arc, Mutex};

#[derive(Clone, Send)]
#[derive(Clone)]
pub enum NodeType {
Directory,
File,
}

#[derive(Clone, Send)]
#[derive(Clone)]
pub struct Node {
name: String,
node_type: NodeType,
parent: Option<Rc<RefCell<Node>>>,
children: Vec<Rc<RefCell<Node>>>,
parent: Option<Arc<Mutex<Node>>>,
children: Vec<Arc<Mutex<Node>>>,
}

impl Node {
pub fn new(name: String, node_type: NodeType, parent: Option<Rc<RefCell<Node>>>) -> Node {
pub fn new(name: String, node_type: NodeType, parent: Option<Arc<Mutex<Node>>>) -> Node {
Node {
name,
node_type,
parent,
children: Vec::<Rc<RefCell<Node>>>::new(),
children: Vec::<Arc<Mutex<Node>>>::new(),
}
}

Expand All @@ -33,18 +34,18 @@ impl Node {
self.name.clone()
}

pub fn get_parent(&self) -> Option<Rc<RefCell<Node>>> {
pub fn get_parent(&self) -> Option<Arc<Mutex<Node>>> {
match &self.parent {
Some(parent) => Some(Rc::clone(parent)),
Some(parent) => Some(Arc::clone(parent)),
None => None,
}
}

pub fn add_child(&mut self, child: Rc<RefCell<Node>>) {
pub fn add_child(&mut self, child: Arc<Mutex<Node>>) {
self.children.push(child);
}

pub fn get_children(&self) -> Vec<Rc<RefCell<Node>>> {
pub fn get_children(&self) -> Vec<Arc<Mutex<Node>>> {
self.children.clone()
}
}
15 changes: 8 additions & 7 deletions src/wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::rc::Rc;
use std::cell::RefCell;
// use std::cell::RefCell;
use std::sync::{Arc, Mutex};

use wasm_bindgen::prelude::*;
use web_sys::console;
Expand All @@ -11,16 +12,16 @@ mod filesystem;
use shell::lexer::Lexer;
use shell::exec::Exec;
use filesystem::node::Node;
use filesystem::create::create_fs;
use filesystem::create::get_root_node;
// use filesystem::pipe::Pipe;


#[wasm_bindgen]
pub struct Shell {
lexer: Lexer,
exec: Exec,
root: Rc<RefCell<Node>>,
current: Rc<RefCell<Node>>,
root: Arc<Mutex<Node>>,
current: Arc<Mutex<Node>>,
// pipe: Pipe,
user: String,
hostname: String,
Expand All @@ -36,9 +37,9 @@ impl Shell {
// let current = root.borrow().get("/user/guest").unwrap();
Shell {
lexer: Lexer::new(),
exec: Exec::new(Rc::clone(&root), Rc::clone(&root)),
exec: Exec::new(Arc::clone(&root), Arc::clone(&root)),
// current,
current: Rc::clone(&root),
current: Arc::clone(&root),
root,
// pipe: Pipe::new(""),
user,
Expand All @@ -55,7 +56,7 @@ impl Shell {
"result": result,
"user": self.user,
"host": self.hostname,
"path": self.current.borrow().get_name(),
"path": self.current.lock().unwrap().get_name(),
});

let output_str = serde_json::to_string(&output).unwrap();
Expand Down
13 changes: 7 additions & 6 deletions src/wasm/src/shell/exec.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@

use std::rc::Rc;
use std::cell::RefCell;
// use std::rc::Rc;
// use std::cell::RefCell;
use std::sync::{Arc, Mutex};
use wasm_bindgen::prelude::*;
use web_sys::console;

use crate::filesystem::node::Node;
use crate::filesystem::directory::Directory;
// use crate::filesystem::directory::Directory;
use crate::shell::lexer::Statement;

pub struct Exec {
root: Rc<RefCell<Node>>,
current: Rc<RefCell<Node>>,
root: Arc<Mutex<Node>>,
current: Arc<Mutex<Node>>,
}

impl Exec {
pub fn new(root: Rc<RefCell<Node>>, current: Rc<RefCell<Node>>) -> Exec {
pub fn new(root: Arc<Mutex<Node>>, current: Arc<Mutex<Node>>) -> Exec {
Exec {
root,
current,
Expand Down

0 comments on commit 4034cd5

Please sign in to comment.