1
2pub mod kernel{
4
5 use colorful::{Color, Colorful};
7 use std::process::{Command, Child, Stdio};
8
9 #[derive(Debug, PartialEq)]
11 pub struct Process{
12
13 pub command : String,
14 pub args : String,
15 }
16
17
18 #[derive(Debug, PartialEq)]
20 pub enum Kernellevel{
21
22 Write,
23 Read,
24 None,
25 Root,
26 }
27
28
29 pub fn new(command : String , args : String) -> Process{
31
32 Process{
33 command,
34 args,
35 }
36 }
37
38 impl Process{
41
42
43 pub fn kernel_permission(&self) -> Kernellevel {
45
46
47 let mut klevel : Kernellevel = Kernellevel::None;
48 if self.command.contains("ls "){
49
50 klevel = Kernellevel::Read
51 }
52
53 if self.command.contains("cat "){
54
55 klevel = Kernellevel::Read;
56 }
57
58 if self.command.contains("cd ") {
59
60 klevel = Kernellevel::None;
61 }
62
63 if self.command.contains("mkdir ") {
64
65 klevel = Kernellevel::None;
66 }
67
68 if self.command.contains("cp ") {
69
70 klevel = Kernellevel::None;
71 }
72
73 if self.command.contains("touch ") {
74
75 klevel = Kernellevel::Write;
76 }
77
78 if self.command.contains("rm ") {
79
80 klevel = Kernellevel::Write;
81 }
82
83 if self.command.contains("mv "){
84
85 klevel = Kernellevel::None;
86 }
87
88 if self.command.contains("sudo "){
89
90 klevel = Kernellevel::Root;
91 }
92
93 if self.command.contains("top ") {
94
95 klevel = Kernellevel::None;
96 }
97
98 if self.command.contains("q") || self.command.contains("--q "){
99
100 klevel = Kernellevel::None;
101 }
102
103 if self.command.contains("? ") || self.command.contains("help "){
104
105 klevel = Kernellevel::None;
106 }
107
108 if self.command.contains("-v") || self.command.contains("--v "){
109
110 klevel = Kernellevel::None;
111 }
112
113 if self.command.contains("whatis") {
114
115 klevel = Kernellevel::None;
116 }
117
118 if self.command.contains("man "){
119
120 klevel = Kernellevel::None;
121 }
122
123 if self.command.contains("exit"){
124
125 klevel = Kernellevel::None;
126 }
127
128 klevel
129 }
130
131 pub fn listing(&self, level : Kernellevel) -> std::io::Result<Child> {
136
137
138 if level == Kernellevel::None {
139
140 println!("{}", " File Dispatch is not found".color(Color::Red).bold());
141
142 panic!("file dispatch is not found");
143
144 }
145
146 if !(self.args.contains("-a")){
147
148 let child = Command::new(self.command.clone()).spawn().expect("process created");
149
150 println!("command = {:?}", child);
151 return Ok(child);
152
153 }else{
154
155 let child = Command::new(self.command.clone()).arg(self.args.clone()).spawn().expect("process created");
156 return Ok(child);
157 }
158
159 }
160
161 pub fn cat_operation(&self, level : Kernellevel) -> std::io::Result<Child>{
165
166 if level == Kernellevel::None{
167
168 println!("{}", "File Dispatch is not found".color(Color::Red).bold());
169 panic!("file dispatch is not found");
170
171 }
172
173 let child = Command::new(self.command.clone()).arg(self.args.clone()).spawn().expect("cat command!");
174
175 Ok(child)
176
177 }
178
179 pub fn change_direc(&self) -> std::io::Result<Child>{
182
183 let child = Command::new(self.command.clone()).arg(self.args.clone()).stdout(Stdio::piped()).spawn().expect("change your directory");
184
185 Ok(child)
186 }
187 }
188}
189
190
191#[cfg(test)]
192mod tests {
193 use crate::kernel;
194
195 #[test]
196 fn instance() {
197
198 let object : _ = kernel::new("ls ".to_string(), "-a ".to_string());
199
200 assert_ne!(object, kernel::Process{command : "".to_string(), args : "".to_string()});
201 }
202
203 #[test]
204 fn kernel_operation() {
205
206 let object : _ = kernel::new("ls ".to_string(), "-a ".to_string());
207
208 let pem : _ = object.kernel_permission();
209
210 assert_ne!(pem, kernel::Kernellevel::None);
211 }
212
213 #[test]
214 fn kernel_listing_command() {
215
216 let object : _ = kernel::new("ls ".to_string(), "".to_string());
217
218 let pem : _ = object.kernel_permission();
219
220 let list : _ = object.listing(pem);
221
222 let output : _ = list.expect("process created").wait_with_output().expect("child process running");
223
224 assert_eq!(output.stdout, Vec::<u8>::new());
225 }
226
227 #[test]
228 fn kernel_cat_operation() {
229
230 let object : _ = kernel::new("cat ".to_string(), "file.txt".to_string());
231
232 let pem : _ = object.kernel_permission();
233
234 let cat_op : _ = object.cat_operation(pem);
235
236 let allow :_ = cat_op.expect("child process created!").id();
237
238 assert_ne!(allow, 0);
239 }
240
241 #[test]
242 fn kernel_change_direc(){
243
244 let object : _ = kernel::new("cd ".to_string(), "lib/".to_string());
245
246 let cd_op : _ = object.change_direc();
249
250 let allow : _ = cd_op.expect("process created ...").wait_with_output().expect("child process running");
251
252 assert!(allow.status.success());
253
254 }
255}