Merge recent update from ch7 && cargo clippy

This commit is contained in:
Yifan Wu 2022-01-22 12:32:36 -08:00
parent 737340b0a0
commit c9583b0f53
20 changed files with 236 additions and 163 deletions

View file

@ -61,7 +61,7 @@ fn easy_fs_pack() -> std::io::Result<()> {
}))); })));
// 16MiB, at most 4095 files // 16MiB, at most 4095 files
let efs = EasyFileSystem::create( let efs = EasyFileSystem::create(
block_file.clone(), block_file,
16 * 2048, 16 * 2048,
1, 1,
); );

View file

@ -17,7 +17,7 @@ pub struct Bitmap {
/// Return (block_pos, bits64_pos, inner_pos) /// Return (block_pos, bits64_pos, inner_pos)
fn decomposition(mut bit: usize) -> (usize, usize, usize) { fn decomposition(mut bit: usize) -> (usize, usize, usize) {
let block_pos = bit / BLOCK_BITS; let block_pos = bit / BLOCK_BITS;
bit = bit % BLOCK_BITS; bit %= BLOCK_BITS;
(block_pos, bit / 64, bit % 64) (block_pos, bit / 64, bit % 64)
} }
@ -70,4 +70,4 @@ impl Bitmap {
pub fn maximum(&self) -> usize { pub fn maximum(&self) -> usize {
self.blocks * BLOCK_BITS self.blocks * BLOCK_BITS
} }
} }

View file

@ -289,35 +289,26 @@ impl DiskInode {
.lock() .lock()
.modify(0, |indirect2: &mut IndirectBlock| { .modify(0, |indirect2: &mut IndirectBlock| {
// full indirect1 blocks // full indirect1 blocks
for i in 0..a1 { for entry in indirect2.iter_mut().take(a1) {
v.push(indirect2[i]); v.push(*entry);
get_block_cache( get_block_cache(*entry as usize, Arc::clone(block_device))
indirect2[i] as usize, .lock()
Arc::clone(block_device), .modify(0, |indirect1: &mut IndirectBlock| {
) for entry in indirect1.iter() {
.lock() v.push(*entry);
.modify(0, |indirect1: &mut IndirectBlock| { }
for j in 0..INODE_INDIRECT1_COUNT { });
v.push(indirect1[j]);
//indirect1[j] = 0;
}
});
//indirect2[i] = 0;
} }
// last indirect1 block // last indirect1 block
if b1 > 0 { if b1 > 0 {
v.push(indirect2[a1]); v.push(indirect2[a1]);
get_block_cache( get_block_cache(indirect2[a1] as usize, Arc::clone(block_device))
indirect2[a1] as usize, .lock()
Arc::clone(block_device), .modify(0, |indirect1: &mut IndirectBlock| {
) for entry in indirect1.iter().take(b1) {
.lock() v.push(*entry);
.modify(0, |indirect1: &mut IndirectBlock| { }
for j in 0..b1 { });
v.push(indirect1[j]);
//indirect1[j] = 0;
}
});
//indirect2[a1] = 0; //indirect2[a1] = 0;
} }
}); });
@ -445,4 +436,4 @@ impl DirEntry {
pub fn inode_number(&self) -> u32 { pub fn inode_number(&self) -> u32 {
self.inode_number self.inode_number
} }
} }

View file

@ -110,12 +110,13 @@ impl Inode {
pub fn create(&self, name: &str) -> Option<Arc<Inode>> { pub fn create(&self, name: &str) -> Option<Arc<Inode>> {
let mut fs = self.fs.lock(); 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 it is a directory
assert!(root_inode.is_dir()); assert!(root_inode.is_dir());
// has the file been created? // has the file been created?
self.find_inode_id(name, root_inode) self.find_inode_id(name, root_inode)
}).is_some() { };
if self.modify_disk_inode(op).is_some() {
return None; return None;
} }
// create a new file // create a new file

View file

@ -78,12 +78,10 @@ impl PipeRingBuffer {
pub fn available_read(&self) -> usize { pub fn available_read(&self) -> usize {
if self.status == RingBufferStatus::EMPTY { if self.status == RingBufferStatus::EMPTY {
0 0
} else if self.tail > self.head {
self.tail - self.head
} else { } else {
if self.tail > self.head { self.tail + RING_BUFFER_SIZE - self.head
self.tail - self.head
} else {
self.tail + RING_BUFFER_SIZE - self.head
}
} }
} }
pub fn available_write(&self) -> usize { pub fn available_write(&self) -> usize {
@ -165,4 +163,4 @@ impl File for Pipe {
} }
} }
} }
} }

View file

@ -62,13 +62,11 @@ impl FrameAllocator for StackFrameAllocator {
fn alloc(&mut self) -> Option<PhysPageNum> { fn alloc(&mut self) -> Option<PhysPageNum> {
if let Some(ppn) = self.recycled.pop() { if let Some(ppn) = self.recycled.pop() {
Some(ppn.into()) Some(ppn.into())
} else if self.current == self.end {
None
} else { } else {
if self.current == self.end { self.current += 1;
None Some((self.current - 1).into())
} else {
self.current += 1;
Some((self.current - 1).into())
}
} }
} }
fn dealloc(&mut self, ppn: PhysPageNum) { fn dealloc(&mut self, ppn: PhysPageNum) {
@ -131,4 +129,4 @@ pub fn frame_allocator_test() {
} }
drop(v); drop(v);
println!("frame_allocator_test passed!"); println!("frame_allocator_test passed!");
} }

View file

@ -9,8 +9,8 @@ extern crate user_lib;
#[no_mangle] #[no_mangle]
pub fn main(argc: usize, argv: &[&str]) -> i32 { pub fn main(argc: usize, argv: &[&str]) -> i32 {
println!("argc = {}", argc); println!("argc = {}", argc);
for i in 0..argc { for (i, arg) in argv.iter().enumerate() {
println!("argv[{}] = {}", i, argv[i]); println!("argv[{}] = {}", i, arg);
} }
0 0
} }

View file

@ -15,8 +15,8 @@ use user_lib::{
#[no_mangle] #[no_mangle]
pub fn main() -> i32 { pub fn main() -> i32 {
let mut buffer = [0u8; 1024]; // 1KiB let mut buffer = [0u8; 1024]; // 1KiB
for i in 0..buffer.len() { for (i, ch) in buffer.iter_mut().enumerate() {
buffer[i] = i as u8; *ch = i as u8;
} }
let f = open("testf\0", OpenFlags::CREATE | OpenFlags::WRONLY); let f = open("testf\0", OpenFlags::CREATE | OpenFlags::WRONLY);
if f < 0 { if f < 0 {

View file

@ -1,7 +1,6 @@
#![no_std] #![no_std]
#![no_main] #![no_main]
#[macro_use]
extern crate user_lib; extern crate user_lib;
use user_lib::{ use user_lib::{
@ -14,7 +13,7 @@ use user_lib::{
#[no_mangle] #[no_mangle]
fn main() -> i32 { fn main() -> i32 {
if fork() == 0 { if fork() == 0 {
exec("user_shell\0", &[0 as *const u8]); exec("user_shell\0", &[core::ptr::null::<u8>()]);
} else { } else {
loop { loop {
let mut exit_code: i32 = 0; let mut exit_code: i32 = 0;
@ -23,11 +22,13 @@ fn main() -> i32 {
yield_(); yield_();
continue; continue;
} }
/*
println!( println!(
"[initproc] Released a zombie process, pid={}, exit_code={}", "[initproc] Released a zombie process, pid={}, exit_code={}",
pid, pid,
exit_code, exit_code,
); );
*/
} }
} }
0 0

View file

@ -1,5 +1,6 @@
#![no_std] #![no_std]
#![no_main] #![no_main]
#![allow(clippy::needless_range_loop)]
#[macro_use] #[macro_use]
extern crate user_lib; extern crate user_lib;

View file

@ -1,5 +1,6 @@
#![no_std] #![no_std]
#![no_main] #![no_main]
#![allow(clippy::println_empty_string)]
#[macro_use] #[macro_use]
extern crate user_lib; extern crate user_lib;

View file

@ -1,5 +1,6 @@
#![no_std] #![no_std]
#![no_main] #![no_main]
#![allow(clippy::println_empty_string)]
#[macro_use] #[macro_use]
extern crate user_lib; extern crate user_lib;
@ -73,17 +74,17 @@ pub fn main() -> i32 {
print!("#{}:", id); print!("#{}:", id);
for j in 0..time_cost/GRAPH_SCALE { for j in 0..time_cost/GRAPH_SCALE {
let current_time = j * GRAPH_SCALE + start; 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 start_thinking = THINK[id][2 * round];
let end_thinking = THINK[id][2 * round + 1]; let end_thinking = THINK[id][2 * round + 1];
start_thinking <= current_time && current_time <= end_thinking start_thinking <= current_time && current_time <= end_thinking
}).is_some() { }) {
print!("-"); print!("-");
} else if (0..ROUND).find(|round| unsafe { } else if (0..ROUND).any(|round| unsafe {
let start_eating = EAT[id][2 * round]; let start_eating = EAT[id][2 * round];
let end_eating = EAT[id][2 * round + 1]; let end_eating = EAT[id][2 * round + 1];
start_eating <= current_time && current_time <= end_eating start_eating <= current_time && current_time <= end_eating
}).is_some() { }) {
print!("x"); print!("x");
} else { } else {
print!(" "); print!(" ");

View file

@ -40,8 +40,8 @@ pub fn main() -> i32 {
// close write end of up pipe // close write end of up pipe
close(up_pipe_fd[1]); close(up_pipe_fd[1]);
// generate a long random string // generate a long random string
for i in 0..LENGTH { for ch in random_str.iter_mut() {
random_str[i] = get_time() as u8; *ch = get_time() as u8;
} }
// send it // send it
assert_eq!(write(down_pipe_fd[1], &random_str) as usize, random_str.len()); 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!"); println!("pipe_large_test passed!");
0 0
} }
} }

View file

@ -10,7 +10,7 @@ use user_lib::{fork, exec, wait};
pub fn main() -> i32 { pub fn main() -> i32 {
for i in 0..1000 { for i in 0..1000 {
if fork() == 0 { if fork() == 0 {
exec("pipe_large_test\0", &[0 as *const u8]); exec("pipe_large_test\0", &[core::ptr::null::<u8>()]);
} else { } else {
let mut _unused: i32 = 0; let mut _unused: i32 = 0;
wait(&mut _unused); wait(&mut _unused);
@ -18,4 +18,4 @@ pub fn main() -> i32 {
} }
} }
0 0
} }

View file

@ -9,7 +9,7 @@ extern crate alloc;
use user_lib::{semaphore_create, semaphore_up, semaphore_down}; use user_lib::{semaphore_create, semaphore_up, semaphore_down};
use user_lib::{thread_create, waittid, sleep}; use user_lib::{thread_create, waittid, sleep};
use user_lib::exit; use user_lib::exit;
use alloc::vec::Vec; use alloc::vec;
const SEM_SYNC: usize = 0; const SEM_SYNC: usize = 0;
@ -33,9 +33,10 @@ pub fn main() -> i32 {
// create semaphores // create semaphores
assert_eq!(semaphore_create(0) as usize, SEM_SYNC); assert_eq!(semaphore_create(0) as usize, SEM_SYNC);
// create threads // create threads
let mut threads = Vec::new(); let threads = vec![
threads.push(thread_create(first as usize, 0)); thread_create(first as usize, 0),
threads.push(thread_create(second as usize, 0)); thread_create(second as usize, 0),
];
// wait for all threads to complete // wait for all threads to complete
for thread in threads.iter() { for thread in threads.iter() {
waittid(*thread as usize); waittid(*thread as usize);

View file

@ -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::{condvar_create, condvar_signal, condvar_wait, mutex_blocking_create, mutex_lock, mutex_unlock};
use user_lib::{thread_create, waittid, sleep}; use user_lib::{thread_create, waittid, sleep};
use user_lib::exit; use user_lib::exit;
use alloc::vec::Vec; use alloc::vec;
static mut A: usize = 0; static mut A: usize = 0;
@ -44,9 +44,10 @@ pub fn main() -> i32 {
assert_eq!(condvar_create() as usize, CONDVAR_ID); assert_eq!(condvar_create() as usize, CONDVAR_ID);
assert_eq!(mutex_blocking_create() as usize, MUTEX_ID); assert_eq!(mutex_blocking_create() as usize, MUTEX_ID);
// create threads // create threads
let mut threads = Vec::new(); let threads = vec![
threads.push(thread_create(first as usize, 0)); thread_create(first as usize, 0),
threads.push(thread_create(second as usize, 0)); thread_create(second as usize, 0),
];
// wait for all threads to complete // wait for all threads to complete
for thread in threads.iter() { for thread in threads.iter() {
waittid(*thread as usize); waittid(*thread as usize);

View file

@ -6,7 +6,7 @@ extern crate user_lib;
extern crate alloc; extern crate alloc;
use user_lib::{thread_create, waittid, exit}; use user_lib::{thread_create, waittid, exit};
use alloc::vec::Vec; use alloc::vec;
pub fn thread_a() -> ! { pub fn thread_a() -> ! {
for _ in 0..1000 { print!("a"); } for _ in 0..1000 { print!("a"); }
@ -25,10 +25,11 @@ pub fn thread_c() -> ! {
#[no_mangle] #[no_mangle]
pub fn main() -> i32 { pub fn main() -> i32 {
let mut v = Vec::new(); let v = vec![
v.push(thread_create(thread_a as usize, 0)); thread_create(thread_a as usize, 0),
v.push(thread_create(thread_b as usize, 0)); thread_create(thread_b as usize, 0),
v.push(thread_create(thread_c as usize, 0)); thread_create(thread_c as usize, 0),
];
for tid in v.iter() { for tid in v.iter() {
let exit_code = waittid(*tid as usize); let exit_code = waittid(*tid as usize);
println!("thread#{} exited with code {}", tid, exit_code); println!("thread#{} exited with code {}", tid, exit_code);

View file

@ -27,8 +27,8 @@ pub fn main() -> i32 {
Argument { ch: 'b', rc: 2, }, Argument { ch: 'b', rc: 2, },
Argument { ch: 'c', rc: 3, }, Argument { ch: 'c', rc: 3, },
]; ];
for i in 0..3 { for arg in args.iter() {
v.push(thread_create(thread_print as usize, &args[i] as *const _ as usize)); v.push(thread_create(thread_print as usize, arg as *const _ as usize));
} }
for tid in v.iter() { for tid in v.iter() {
let exit_code = waittid(*tid as usize); let exit_code = waittid(*tid as usize);

View file

@ -1,5 +1,7 @@
#![no_std] #![no_std]
#![no_main] #![no_main]
#![allow(clippy::println_empty_string)]
extern crate alloc; extern crate alloc;
@ -10,116 +12,191 @@ const LF: u8 = 0x0au8;
const CR: u8 = 0x0du8; const CR: u8 = 0x0du8;
const DL: u8 = 0x7fu8; const DL: u8 = 0x7fu8;
const BS: u8 = 0x08u8; const BS: u8 = 0x08u8;
const LINE_START: &str = ">> ";
use alloc::string::String; use alloc::string::String;
use alloc::vec::Vec; use alloc::vec::Vec;
use user_lib::{
fork,
exec,
waitpid,
open,
OpenFlags,
close,
dup,
};
use user_lib::console::getchar; 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<String>,
args_addr: Vec<*const u8>,
}
impl ProcessArguments {
pub fn new(command: &str) -> Self {
let args: Vec<_> = command.split(' ').collect();
let mut args_copy: Vec<String> = 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::<u8>());
Self {
input,
output,
args_copy,
args_addr,
}
}
}
#[no_mangle] #[no_mangle]
pub fn main() -> i32 { pub fn main() -> i32 {
println!("Rust user shell"); println!("Rust user shell");
let mut line: String = String::new(); let mut line: String = String::new();
print!(">> "); print!("{}", LINE_START);
loop { loop {
let c = getchar(); let c = getchar();
match c { match c {
LF | CR => { LF | CR => {
println!(""); println!("");
if !line.is_empty() { if !line.is_empty() {
let args: Vec<_> = line.as_str().split(' ').collect(); let splited: Vec<_> = line.as_str().split('|').collect();
let mut args_copy: Vec<String> = args let process_arguments_list: Vec<_> = splited
.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
.iter() .iter()
.map(|arg| arg.as_ptr()) .map(|&cmd| ProcessArguments::new(cmd))
.collect(); .collect();
args_addr.push(0 as *const u8); let mut valid = true;
let pid = fork(); for (i, process_args) in process_arguments_list.iter().enumerate() {
if pid == 0 { if i == 0 {
// input redirection if !process_args.output.is_empty() {
if !input.is_empty() { valid = false;
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; } else if i == process_arguments_list.len() - 1 {
close(0); if !process_args.input.is_empty() {
assert_eq!(dup(input_fd), 0); valid = false;
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;
} }
let output_fd = output_fd as usize; } else if !process_args.output.is_empty() || !process_args.input.is_empty()
close(1); {
assert_eq!(dup(output_fd), 1); valid = false;
close(output_fd);
} }
// child process }
if exec(args_copy[0].as_str(), args_addr.as_slice()) == -1 { if process_arguments_list.len() == 1 {
println!("Error when executing!"); valid = true;
return -4; }
} if !valid {
unreachable!(); println!("Invalid command: Inputs/Outputs cannot be correctly binded!");
} else { } 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 mut exit_code: i32 = 0;
let exit_pid = waitpid(pid as usize, &mut exit_code); for pid in children.into_iter() {
assert_eq!(pid, exit_pid); let exit_pid = waitpid(pid as usize, &mut exit_code);
println!("Shell: Process {} exited with code {}", pid, exit_code); assert_eq!(pid, exit_pid);
//println!("Shell: Process {} exited with code {}", pid, exit_code);
}
} }
line.clear(); line.clear();
} }
print!(">> "); print!("{}", LINE_START);
} }
BS | DL => { BS | DL => {
if !line.is_empty() { if !line.is_empty() {
@ -135,4 +212,5 @@ pub fn main() -> i32 {
} }
} }
} }
} }

View file

@ -26,7 +26,7 @@ pub fn main() -> i32 {
println!("Usertests: Running {}", test); println!("Usertests: Running {}", test);
let pid = fork(); let pid = fork();
if pid == 0 { if pid == 0 {
exec(*test, &[0 as *const u8]); exec(*test, &[core::ptr::null::<u8>()]);
panic!("unreachable!"); panic!("unreachable!");
} else { } else {
let mut exit_code: i32 = Default::default(); let mut exit_code: i32 = Default::default();
@ -37,4 +37,4 @@ pub fn main() -> i32 {
} }
println!("Usertests passed!"); println!("Usertests passed!");
0 0
} }