diff --git a/easy-fs-fuse/src/main.rs b/easy-fs-fuse/src/main.rs index cf07b618..09e93456 100644 --- a/easy-fs-fuse/src/main.rs +++ b/easy-fs-fuse/src/main.rs @@ -61,7 +61,7 @@ fn easy_fs_pack() -> std::io::Result<()> { }))); // 16MiB, at most 4095 files let efs = EasyFileSystem::create( - block_file.clone(), + block_file, 16 * 2048, 1, ); diff --git a/easy-fs/src/bitmap.rs b/easy-fs/src/bitmap.rs index 4feaa9cf..5474e749 100644 --- a/easy-fs/src/bitmap.rs +++ b/easy-fs/src/bitmap.rs @@ -17,7 +17,7 @@ pub struct Bitmap { /// Return (block_pos, bits64_pos, inner_pos) fn decomposition(mut bit: usize) -> (usize, usize, usize) { let block_pos = bit / BLOCK_BITS; - bit = bit % BLOCK_BITS; + bit %= BLOCK_BITS; (block_pos, bit / 64, bit % 64) } @@ -70,4 +70,4 @@ impl Bitmap { pub fn maximum(&self) -> usize { self.blocks * BLOCK_BITS } -} \ No newline at end of file +} diff --git a/easy-fs/src/layout.rs b/easy-fs/src/layout.rs index c6f7b315..b5295de1 100644 --- a/easy-fs/src/layout.rs +++ b/easy-fs/src/layout.rs @@ -289,35 +289,26 @@ impl DiskInode { .lock() .modify(0, |indirect2: &mut IndirectBlock| { // full indirect1 blocks - for i in 0..a1 { - v.push(indirect2[i]); - get_block_cache( - indirect2[i] as usize, - Arc::clone(block_device), - ) - .lock() - .modify(0, |indirect1: &mut IndirectBlock| { - for j in 0..INODE_INDIRECT1_COUNT { - v.push(indirect1[j]); - //indirect1[j] = 0; - } - }); - //indirect2[i] = 0; + for entry in indirect2.iter_mut().take(a1) { + v.push(*entry); + get_block_cache(*entry as usize, Arc::clone(block_device)) + .lock() + .modify(0, |indirect1: &mut IndirectBlock| { + for entry in indirect1.iter() { + v.push(*entry); + } + }); } // last indirect1 block if b1 > 0 { v.push(indirect2[a1]); - get_block_cache( - indirect2[a1] as usize, - Arc::clone(block_device), - ) - .lock() - .modify(0, |indirect1: &mut IndirectBlock| { - for j in 0..b1 { - v.push(indirect1[j]); - //indirect1[j] = 0; - } - }); + get_block_cache(indirect2[a1] as usize, Arc::clone(block_device)) + .lock() + .modify(0, |indirect1: &mut IndirectBlock| { + for entry in indirect1.iter().take(b1) { + v.push(*entry); + } + }); //indirect2[a1] = 0; } }); @@ -445,4 +436,4 @@ impl DirEntry { pub fn inode_number(&self) -> u32 { self.inode_number } -} \ No newline at end of file +} diff --git a/easy-fs/src/vfs.rs b/easy-fs/src/vfs.rs index 9534c39a..4dfd31b0 100644 --- a/easy-fs/src/vfs.rs +++ b/easy-fs/src/vfs.rs @@ -110,12 +110,13 @@ impl Inode { pub fn create(&self, name: &str) -> Option> { let mut fs = self.fs.lock(); - if self.modify_disk_inode(|root_inode| { + let op = |root_inode: &mut DiskInode| { // assert it is a directory assert!(root_inode.is_dir()); // has the file been created? self.find_inode_id(name, root_inode) - }).is_some() { + }; + if self.modify_disk_inode(op).is_some() { return None; } // create a new file diff --git a/os/src/fs/pipe.rs b/os/src/fs/pipe.rs index ed2dde15..08d51daf 100644 --- a/os/src/fs/pipe.rs +++ b/os/src/fs/pipe.rs @@ -78,12 +78,10 @@ impl PipeRingBuffer { pub fn available_read(&self) -> usize { if self.status == RingBufferStatus::EMPTY { 0 + } else if self.tail > self.head { + self.tail - self.head } else { - if self.tail > self.head { - self.tail - self.head - } else { - self.tail + RING_BUFFER_SIZE - self.head - } + self.tail + RING_BUFFER_SIZE - self.head } } pub fn available_write(&self) -> usize { @@ -165,4 +163,4 @@ impl File for Pipe { } } } -} \ No newline at end of file +} diff --git a/os/src/mm/frame_allocator.rs b/os/src/mm/frame_allocator.rs index 357db707..3afe336b 100644 --- a/os/src/mm/frame_allocator.rs +++ b/os/src/mm/frame_allocator.rs @@ -62,13 +62,11 @@ impl FrameAllocator for StackFrameAllocator { fn alloc(&mut self) -> Option { if let Some(ppn) = self.recycled.pop() { Some(ppn.into()) + } else if self.current == self.end { + None } else { - if self.current == self.end { - None - } else { - self.current += 1; - Some((self.current - 1).into()) - } + self.current += 1; + Some((self.current - 1).into()) } } fn dealloc(&mut self, ppn: PhysPageNum) { @@ -131,4 +129,4 @@ pub fn frame_allocator_test() { } drop(v); println!("frame_allocator_test passed!"); -} \ No newline at end of file +} diff --git a/user/src/bin/cmdline_args.rs b/user/src/bin/cmdline_args.rs index b49ec332..9a227456 100644 --- a/user/src/bin/cmdline_args.rs +++ b/user/src/bin/cmdline_args.rs @@ -9,8 +9,8 @@ extern crate user_lib; #[no_mangle] pub fn main(argc: usize, argv: &[&str]) -> i32 { println!("argc = {}", argc); - for i in 0..argc { - println!("argv[{}] = {}", i, argv[i]); + for (i, arg) in argv.iter().enumerate() { + println!("argv[{}] = {}", i, arg); } 0 -} \ No newline at end of file +} diff --git a/user/src/bin/huge_write.rs b/user/src/bin/huge_write.rs index b00ca17b..4b0daaf8 100644 --- a/user/src/bin/huge_write.rs +++ b/user/src/bin/huge_write.rs @@ -15,8 +15,8 @@ use user_lib::{ #[no_mangle] pub fn main() -> i32 { let mut buffer = [0u8; 1024]; // 1KiB - for i in 0..buffer.len() { - buffer[i] = i as u8; + for (i, ch) in buffer.iter_mut().enumerate() { + *ch = i as u8; } let f = open("testf\0", OpenFlags::CREATE | OpenFlags::WRONLY); if f < 0 { diff --git a/user/src/bin/initproc.rs b/user/src/bin/initproc.rs index cf8840f7..fba348cb 100644 --- a/user/src/bin/initproc.rs +++ b/user/src/bin/initproc.rs @@ -1,7 +1,6 @@ #![no_std] #![no_main] -#[macro_use] extern crate user_lib; use user_lib::{ @@ -14,7 +13,7 @@ use user_lib::{ #[no_mangle] fn main() -> i32 { if fork() == 0 { - exec("user_shell\0", &[0 as *const u8]); + exec("user_shell\0", &[core::ptr::null::()]); } else { loop { let mut exit_code: i32 = 0; @@ -23,11 +22,13 @@ fn main() -> i32 { yield_(); continue; } + /* println!( "[initproc] Released a zombie process, pid={}, exit_code={}", pid, exit_code, ); + */ } } 0 diff --git a/user/src/bin/matrix.rs b/user/src/bin/matrix.rs index 8f1357ea..c2461cf2 100644 --- a/user/src/bin/matrix.rs +++ b/user/src/bin/matrix.rs @@ -1,5 +1,6 @@ #![no_std] #![no_main] +#![allow(clippy::needless_range_loop)] #[macro_use] extern crate user_lib; diff --git a/user/src/bin/mpsc_sem.rs b/user/src/bin/mpsc_sem.rs index 71141e41..a60c14b6 100644 --- a/user/src/bin/mpsc_sem.rs +++ b/user/src/bin/mpsc_sem.rs @@ -1,5 +1,6 @@ #![no_std] #![no_main] +#![allow(clippy::println_empty_string)] #[macro_use] extern crate user_lib; diff --git a/user/src/bin/phil_din_mutex.rs b/user/src/bin/phil_din_mutex.rs index 97a4a8c1..62baab60 100644 --- a/user/src/bin/phil_din_mutex.rs +++ b/user/src/bin/phil_din_mutex.rs @@ -1,5 +1,6 @@ #![no_std] #![no_main] +#![allow(clippy::println_empty_string)] #[macro_use] extern crate user_lib; @@ -73,17 +74,17 @@ pub fn main() -> i32 { print!("#{}:", id); for j in 0..time_cost/GRAPH_SCALE { let current_time = j * GRAPH_SCALE + start; - if (0..ROUND).find(|round| unsafe { + if (0..ROUND).any(|round| unsafe { let start_thinking = THINK[id][2 * round]; let end_thinking = THINK[id][2 * round + 1]; start_thinking <= current_time && current_time <= end_thinking - }).is_some() { + }) { print!("-"); - } else if (0..ROUND).find(|round| unsafe { + } else if (0..ROUND).any(|round| unsafe { let start_eating = EAT[id][2 * round]; let end_eating = EAT[id][2 * round + 1]; start_eating <= current_time && current_time <= end_eating - }).is_some() { + }) { print!("x"); } else { print!(" "); diff --git a/user/src/bin/pipe_large_test.rs b/user/src/bin/pipe_large_test.rs index 121987be..6f4f5000 100644 --- a/user/src/bin/pipe_large_test.rs +++ b/user/src/bin/pipe_large_test.rs @@ -40,8 +40,8 @@ pub fn main() -> i32 { // close write end of up pipe close(up_pipe_fd[1]); // generate a long random string - for i in 0..LENGTH { - random_str[i] = get_time() as u8; + for ch in random_str.iter_mut() { + *ch = get_time() as u8; } // send it assert_eq!(write(down_pipe_fd[1], &random_str) as usize, random_str.len()); @@ -66,4 +66,4 @@ pub fn main() -> i32 { println!("pipe_large_test passed!"); 0 } -} \ No newline at end of file +} diff --git a/user/src/bin/run_pipe_test.rs b/user/src/bin/run_pipe_test.rs index 000b82d5..f0f95b03 100644 --- a/user/src/bin/run_pipe_test.rs +++ b/user/src/bin/run_pipe_test.rs @@ -10,7 +10,7 @@ use user_lib::{fork, exec, wait}; pub fn main() -> i32 { for i in 0..1000 { if fork() == 0 { - exec("pipe_large_test\0", &[0 as *const u8]); + exec("pipe_large_test\0", &[core::ptr::null::()]); } else { let mut _unused: i32 = 0; wait(&mut _unused); @@ -18,4 +18,4 @@ pub fn main() -> i32 { } } 0 -} \ No newline at end of file +} diff --git a/user/src/bin/sync_sem.rs b/user/src/bin/sync_sem.rs index d72d520d..66b25fb7 100644 --- a/user/src/bin/sync_sem.rs +++ b/user/src/bin/sync_sem.rs @@ -9,7 +9,7 @@ extern crate alloc; use user_lib::{semaphore_create, semaphore_up, semaphore_down}; use user_lib::{thread_create, waittid, sleep}; use user_lib::exit; -use alloc::vec::Vec; +use alloc::vec; const SEM_SYNC: usize = 0; @@ -33,9 +33,10 @@ pub fn main() -> i32 { // create semaphores assert_eq!(semaphore_create(0) as usize, SEM_SYNC); // create threads - let mut threads = Vec::new(); - threads.push(thread_create(first as usize, 0)); - threads.push(thread_create(second as usize, 0)); + let threads = vec![ + thread_create(first as usize, 0), + thread_create(second as usize, 0), + ]; // wait for all threads to complete for thread in threads.iter() { waittid(*thread as usize); diff --git a/user/src/bin/test_condvar.rs b/user/src/bin/test_condvar.rs index 6aa49eff..6caa53e7 100644 --- a/user/src/bin/test_condvar.rs +++ b/user/src/bin/test_condvar.rs @@ -9,7 +9,7 @@ extern crate alloc; use user_lib::{condvar_create, condvar_signal, condvar_wait, mutex_blocking_create, mutex_lock, mutex_unlock}; use user_lib::{thread_create, waittid, sleep}; use user_lib::exit; -use alloc::vec::Vec; +use alloc::vec; static mut A: usize = 0; @@ -44,9 +44,10 @@ pub fn main() -> i32 { assert_eq!(condvar_create() as usize, CONDVAR_ID); assert_eq!(mutex_blocking_create() as usize, MUTEX_ID); // create threads - let mut threads = Vec::new(); - threads.push(thread_create(first as usize, 0)); - threads.push(thread_create(second as usize, 0)); + let threads = vec![ + thread_create(first as usize, 0), + thread_create(second as usize, 0), + ]; // wait for all threads to complete for thread in threads.iter() { waittid(*thread as usize); diff --git a/user/src/bin/threads.rs b/user/src/bin/threads.rs index 97ce43f0..851766d4 100644 --- a/user/src/bin/threads.rs +++ b/user/src/bin/threads.rs @@ -6,7 +6,7 @@ extern crate user_lib; extern crate alloc; use user_lib::{thread_create, waittid, exit}; -use alloc::vec::Vec; +use alloc::vec; pub fn thread_a() -> ! { for _ in 0..1000 { print!("a"); } @@ -25,10 +25,11 @@ pub fn thread_c() -> ! { #[no_mangle] pub fn main() -> i32 { - let mut v = Vec::new(); - v.push(thread_create(thread_a as usize, 0)); - v.push(thread_create(thread_b as usize, 0)); - v.push(thread_create(thread_c as usize, 0)); + let v = vec![ + thread_create(thread_a as usize, 0), + thread_create(thread_b as usize, 0), + thread_create(thread_c as usize, 0), + ]; for tid in v.iter() { let exit_code = waittid(*tid as usize); println!("thread#{} exited with code {}", tid, exit_code); diff --git a/user/src/bin/threads_arg.rs b/user/src/bin/threads_arg.rs index a29b23dd..c5b3b305 100644 --- a/user/src/bin/threads_arg.rs +++ b/user/src/bin/threads_arg.rs @@ -27,8 +27,8 @@ pub fn main() -> i32 { Argument { ch: 'b', rc: 2, }, Argument { ch: 'c', rc: 3, }, ]; - for i in 0..3 { - v.push(thread_create(thread_print as usize, &args[i] as *const _ as usize)); + for arg in args.iter() { + v.push(thread_create(thread_print as usize, arg as *const _ as usize)); } for tid in v.iter() { let exit_code = waittid(*tid as usize); diff --git a/user/src/bin/user_shell.rs b/user/src/bin/user_shell.rs index b2c33f2d..5da878c6 100644 --- a/user/src/bin/user_shell.rs +++ b/user/src/bin/user_shell.rs @@ -1,5 +1,7 @@ + #![no_std] #![no_main] +#![allow(clippy::println_empty_string)] extern crate alloc; @@ -10,116 +12,191 @@ const LF: u8 = 0x0au8; const CR: u8 = 0x0du8; const DL: u8 = 0x7fu8; const BS: u8 = 0x08u8; +const LINE_START: &str = ">> "; use alloc::string::String; use alloc::vec::Vec; -use user_lib::{ - fork, - exec, - waitpid, - open, - OpenFlags, - close, - dup, -}; use user_lib::console::getchar; +use user_lib::{close, dup, exec, fork, open, pipe, waitpid, OpenFlags}; + +#[derive(Debug)] +struct ProcessArguments { + input: String, + output: String, + args_copy: Vec, + args_addr: Vec<*const u8>, +} + +impl ProcessArguments { + pub fn new(command: &str) -> Self { + let args: Vec<_> = command.split(' ').collect(); + let mut args_copy: Vec = args + .iter() + .filter(|&arg| !arg.is_empty()) + .map(|&arg| { + let mut string = String::new(); + string.push_str(arg); + string.push('\0'); + string + }) + .collect(); + + // redirect input + let mut input = String::new(); + if let Some((idx, _)) = args_copy + .iter() + .enumerate() + .find(|(_, arg)| arg.as_str() == "<\0") + { + input = args_copy[idx + 1].clone(); + args_copy.drain(idx..=idx + 1); + } + + // redirect output + let mut output = String::new(); + if let Some((idx, _)) = args_copy + .iter() + .enumerate() + .find(|(_, arg)| arg.as_str() == ">\0") + { + output = args_copy[idx + 1].clone(); + args_copy.drain(idx..=idx + 1); + } + + let mut args_addr: Vec<*const u8> = args_copy.iter().map(|arg| arg.as_ptr()).collect(); + args_addr.push(core::ptr::null::()); + + Self { + input, + output, + args_copy, + args_addr, + } + } +} #[no_mangle] pub fn main() -> i32 { println!("Rust user shell"); let mut line: String = String::new(); - print!(">> "); + print!("{}", LINE_START); loop { let c = getchar(); match c { LF | CR => { println!(""); if !line.is_empty() { - let args: Vec<_> = line.as_str().split(' ').collect(); - let mut args_copy: Vec = args - .iter() - .map(|&arg| { - let mut string = String::new(); - string.push_str(arg); - string - }) - .collect(); - - args_copy - .iter_mut() - .for_each(|string| { - string.push('\0'); - }); - - // redirect input - let mut input = String::new(); - if let Some((idx, _)) = args_copy - .iter() - .enumerate() - .find(|(_, arg)| arg.as_str() == "<\0") { - input = args_copy[idx + 1].clone(); - args_copy.drain(idx..=idx + 1); - } - - // redirect output - let mut output = String::new(); - if let Some((idx, _)) = args_copy - .iter() - .enumerate() - .find(|(_, arg)| arg.as_str() == ">\0") { - output = args_copy[idx + 1].clone(); - args_copy.drain(idx..=idx + 1); - } - - let mut args_addr: Vec<*const u8> = args_copy + let splited: Vec<_> = line.as_str().split('|').collect(); + let process_arguments_list: Vec<_> = splited .iter() - .map(|arg| arg.as_ptr()) + .map(|&cmd| ProcessArguments::new(cmd)) .collect(); - args_addr.push(0 as *const u8); - let pid = fork(); - if pid == 0 { - // input redirection - if !input.is_empty() { - let input_fd = open(input.as_str(), OpenFlags::RDONLY); - if input_fd == -1 { - println!("Error when opening file {}", input); - return -4; + let mut valid = true; + for (i, process_args) in process_arguments_list.iter().enumerate() { + if i == 0 { + if !process_args.output.is_empty() { + valid = false; } - let input_fd = input_fd as usize; - close(0); - assert_eq!(dup(input_fd), 0); - close(input_fd); - } - // output redirection - if !output.is_empty() { - let output_fd = open( - output.as_str(), - OpenFlags::CREATE | OpenFlags::WRONLY - ); - if output_fd == -1 { - println!("Error when opening file {}", output); - return -4; + } else if i == process_arguments_list.len() - 1 { + if !process_args.input.is_empty() { + valid = false; } - let output_fd = output_fd as usize; - close(1); - assert_eq!(dup(output_fd), 1); - close(output_fd); + } else if !process_args.output.is_empty() || !process_args.input.is_empty() + { + valid = false; } - // child process - if exec(args_copy[0].as_str(), args_addr.as_slice()) == -1 { - println!("Error when executing!"); - return -4; - } - unreachable!(); + } + if process_arguments_list.len() == 1 { + valid = true; + } + if !valid { + println!("Invalid command: Inputs/Outputs cannot be correctly binded!"); } else { + // create pipes + let mut pipes_fd: Vec<[usize; 2]> = Vec::new(); + if !process_arguments_list.is_empty() { + for _ in 0..process_arguments_list.len() - 1 { + let mut pipe_fd = [0usize; 2]; + pipe(&mut pipe_fd); + pipes_fd.push(pipe_fd); + } + } + let mut children: Vec<_> = Vec::new(); + for (i, process_argument) in process_arguments_list.iter().enumerate() { + let pid = fork(); + if pid == 0 { + let input = &process_argument.input; + let output = &process_argument.output; + let args_copy = &process_argument.args_copy; + let args_addr = &process_argument.args_addr; + // redirect input + if !input.is_empty() { + let input_fd = open(input.as_str(), OpenFlags::RDONLY); + if input_fd == -1 { + println!("Error when opening file {}", input); + return -4; + } + let input_fd = input_fd as usize; + close(0); + assert_eq!(dup(input_fd), 0); + close(input_fd); + } + // redirect output + if !output.is_empty() { + let output_fd = open( + output.as_str(), + OpenFlags::CREATE | OpenFlags::WRONLY, + ); + if output_fd == -1 { + println!("Error when opening file {}", output); + return -4; + } + let output_fd = output_fd as usize; + close(1); + assert_eq!(dup(output_fd), 1); + close(output_fd); + } + // receive input from the previous process + if i > 0 { + close(0); + let read_end = pipes_fd.get(i - 1).unwrap()[0]; + assert_eq!(dup(read_end), 0); + } + // send output to the next process + if i < process_arguments_list.len() - 1 { + close(1); + let write_end = pipes_fd.get(i).unwrap()[1]; + assert_eq!(dup(write_end), 1); + } + // close all pipe ends inherited from the parent process + for pipe_fd in pipes_fd.iter() { + close(pipe_fd[0]); + close(pipe_fd[1]); + } + // execute new application + if exec(args_copy[0].as_str(), args_addr.as_slice()) == -1 { + println!("Error when executing!"); + return -4; + } + unreachable!(); + } else { + children.push(pid); + } + } + for pipe_fd in pipes_fd.iter() { + close(pipe_fd[0]); + close(pipe_fd[1]); + } let mut exit_code: i32 = 0; - let exit_pid = waitpid(pid as usize, &mut exit_code); - assert_eq!(pid, exit_pid); - println!("Shell: Process {} exited with code {}", pid, exit_code); + for pid in children.into_iter() { + let exit_pid = waitpid(pid as usize, &mut exit_code); + assert_eq!(pid, exit_pid); + //println!("Shell: Process {} exited with code {}", pid, exit_code); + } } line.clear(); } - print!(">> "); + print!("{}", LINE_START); } BS | DL => { if !line.is_empty() { @@ -135,4 +212,5 @@ pub fn main() -> i32 { } } } -} \ No newline at end of file +} + diff --git a/user/src/bin/usertests.rs b/user/src/bin/usertests.rs index e8be6c45..83da4d59 100644 --- a/user/src/bin/usertests.rs +++ b/user/src/bin/usertests.rs @@ -26,7 +26,7 @@ pub fn main() -> i32 { println!("Usertests: Running {}", test); let pid = fork(); if pid == 0 { - exec(*test, &[0 as *const u8]); + exec(*test, &[core::ptr::null::()]); panic!("unreachable!"); } else { let mut exit_code: i32 = Default::default(); @@ -37,4 +37,4 @@ pub fn main() -> i32 { } println!("Usertests passed!"); 0 -} \ No newline at end of file +}