Skip to content

Commit

Permalink
Rename file handle to handle
Browse files Browse the repository at this point in the history
  • Loading branch information
vinc committed Jul 12, 2023
1 parent 6ff4ef1 commit 4b241ed
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 50 deletions.
8 changes: 4 additions & 4 deletions doc/shell.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ file while the standard error is kept:
> time read foo.txt => /dev/null

The standard output is implied as the source of a redirection, but it is
possible to explicitly redirect a file handle to another (TODO):
possible to explicitly redirect a handle to another (TODO):

> time read foo.txt [1]=>[3]

Or to redirect a file handle to a file:
Or to redirect a handle to a file:

> time read foo.txt [1]=> bar.txt

Or to pipe a file handle to another command:
Or to pipe a handle to another command:

> time read foo.txt [1]-> write bar.txt

Expand All @@ -125,7 +125,7 @@ Redirections should be declared before piping (TODO):

> write <= req.txt => /net/http/moros.cc -> find --line href -> sort

NOTE: The following file handles are available when a process is created:
NOTE: The following handles are available when a process is created:

- `stdin(0)`
- `stdout(1)`
Expand Down
44 changes: 22 additions & 22 deletions src/sys/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use object::{Object, ObjectSegment};
use spin::RwLock;
use x86_64::structures::idt::InterruptStackFrameValue;

const MAX_FILE_HANDLES: usize = 64;
const MAX_HANDLES: usize = 64;
const MAX_PROCS: usize = 2; // TODO: Update this when more than one process can run at once
const MAX_PROC_SIZE: usize = 10 << 20; // 10 MB

Expand All @@ -29,20 +29,20 @@ pub struct ProcessData {
env: BTreeMap<String, String>,
dir: String,
user: Option<String>,
file_handles: [Option<Box<Resource>>; MAX_FILE_HANDLES],
handles: [Option<Box<Resource>>; MAX_HANDLES],
}

impl ProcessData {
pub fn new(dir: &str, user: Option<&str>) -> Self {
let env = BTreeMap::new();
let dir = dir.to_string();
let user = user.map(String::from);
let mut file_handles = [(); MAX_FILE_HANDLES].map(|_| None);
file_handles[0] = Some(Box::new(Resource::Device(Device::Console(Console::new())))); // stdin
file_handles[1] = Some(Box::new(Resource::Device(Device::Console(Console::new())))); // stdout
file_handles[2] = Some(Box::new(Resource::Device(Device::Console(Console::new())))); // stderr
file_handles[3] = Some(Box::new(Resource::Device(Device::Null))); // stdnull
Self { env, dir, user, file_handles }
let mut handles = [(); MAX_HANDLES].map(|_| None);
handles[0] = Some(Box::new(Resource::Device(Device::Console(Console::new())))); // stdin
handles[1] = Some(Box::new(Resource::Device(Device::Console(Console::new())))); // stdout
handles[2] = Some(Box::new(Resource::Device(Device::Console(Console::new())))); // stderr
handles[3] = Some(Box::new(Resource::Device(Device::Null))); // stdnull
Self { env, dir, user, handles }
}
}

Expand Down Expand Up @@ -96,43 +96,43 @@ pub fn set_user(user: &str) {
proc.data.user = Some(user.into())
}

pub fn create_file_handle(file: Resource) -> Result<usize, ()> {
pub fn create_handle(file: Resource) -> Result<usize, ()> {
let mut table = PROCESS_TABLE.write();
let proc = &mut table[id()];
let min = 4; // The first 4 file handles are reserved
let max = MAX_FILE_HANDLES;
let min = 4; // The first 4 handles are reserved
let max = MAX_HANDLES;
for handle in min..max {
if proc.data.file_handles[handle].is_none() {
proc.data.file_handles[handle] = Some(Box::new(file));
if proc.data.handles[handle].is_none() {
proc.data.handles[handle] = Some(Box::new(file));
return Ok(handle);
}
}
debug!("Could not create file handle");
debug!("Could not create handle");
Err(())
}

pub fn update_file_handle(handle: usize, file: Resource) {
pub fn update_handle(handle: usize, file: Resource) {
let mut table = PROCESS_TABLE.write();
let proc = &mut table[id()];
proc.data.file_handles[handle] = Some(Box::new(file));
proc.data.handles[handle] = Some(Box::new(file));
}

pub fn delete_file_handle(handle: usize) {
pub fn delete_handle(handle: usize) {
let mut table = PROCESS_TABLE.write();
let proc = &mut table[id()];
proc.data.file_handles[handle] = None;
proc.data.handles[handle] = None;
}

pub fn file_handle(handle: usize) -> Option<Box<Resource>> {
pub fn handle(handle: usize) -> Option<Box<Resource>> {
let table = PROCESS_TABLE.read();
let proc = &table[id()];
proc.data.file_handles[handle].clone()
proc.data.handles[handle].clone()
}

pub fn file_handles() -> Vec<Option<Box<Resource>>> {
pub fn handles() -> Vec<Option<Box<Resource>>> {
let table = PROCESS_TABLE.read();
let proc = &table[id()];
proc.data.file_handles.to_vec()
proc.data.handles.to_vec()
}

pub fn code_addr() -> u64 {
Expand Down
26 changes: 13 additions & 13 deletions src/sys/syscall/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,45 +45,45 @@ pub fn open(path: &str, flags: usize) -> isize {
Err(_) => return -1,
};
if let Some(resource) = sys::fs::open(&path, flags) {
if let Ok(handle) = sys::process::create_file_handle(resource) {
if let Ok(handle) = sys::process::create_handle(resource) {
return handle as isize;
}
}
-1
}

pub fn dup(old_handle: usize, new_handle: usize) -> isize {
if let Some(file) = sys::process::file_handle(old_handle) {
sys::process::update_file_handle(new_handle, *file);
if let Some(file) = sys::process::handle(old_handle) {
sys::process::update_handle(new_handle, *file);
return new_handle as isize;
}
-1
}

pub fn read(handle: usize, buf: &mut [u8]) -> isize {
if let Some(mut file) = sys::process::file_handle(handle) {
if let Some(mut file) = sys::process::handle(handle) {
if let Ok(bytes) = file.read(buf) {
sys::process::update_file_handle(handle, *file);
sys::process::update_handle(handle, *file);
return bytes as isize;
}
}
-1
}

pub fn write(handle: usize, buf: &mut [u8]) -> isize {
if let Some(mut file) = sys::process::file_handle(handle) {
if let Some(mut file) = sys::process::handle(handle) {
if let Ok(bytes) = file.write(buf) {
sys::process::update_file_handle(handle, *file);
sys::process::update_handle(handle, *file);
return bytes as isize;
}
}
-1
}

pub fn close(handle: usize) {
if let Some(mut file) = sys::process::file_handle(handle) {
if let Some(mut file) = sys::process::handle(handle) {
file.close();
sys::process::delete_file_handle(handle);
sys::process::delete_handle(handle);
}
}

Expand Down Expand Up @@ -132,7 +132,7 @@ pub fn stop(code: usize) -> usize {

pub fn poll(list: &[(usize, IO)]) -> isize {
for (i, (handle, event)) in list.iter().enumerate() {
if let Some(mut file) = sys::process::file_handle(*handle) {
if let Some(mut file) = sys::process::handle(*handle) {
if file.poll(*event) {
return i as isize;
}
Expand All @@ -142,7 +142,7 @@ pub fn poll(list: &[(usize, IO)]) -> isize {
}

pub fn connect(handle: usize, addr: IpAddress, port: u16) -> isize {
if let Some(file) = sys::process::file_handle(handle) {
if let Some(file) = sys::process::handle(handle) {
if let sys::fs::Resource::Device(Device::TcpSocket(mut dev)) = *file {
if dev.connect(addr, port).is_ok() {
return 0;
Expand All @@ -153,7 +153,7 @@ pub fn connect(handle: usize, addr: IpAddress, port: u16) -> isize {
}

pub fn listen(handle: usize, port: u16) -> isize {
if let Some(file) = sys::process::file_handle(handle) {
if let Some(file) = sys::process::handle(handle) {
if let sys::fs::Resource::Device(Device::TcpSocket(mut dev)) = *file {
if dev.listen(port).is_ok() {
return 0;
Expand All @@ -164,7 +164,7 @@ pub fn listen(handle: usize, port: u16) -> isize {
}

pub fn accept(handle: usize) -> Result<IpAddress, ()> {
if let Some(file) = sys::process::file_handle(handle) {
if let Some(file) = sys::process::handle(handle) {
if let sys::fs::Resource::Device(Device::TcpSocket(mut dev)) = *file {
return dev.accept();
}
Expand Down
12 changes: 6 additions & 6 deletions src/usr/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ fn cmd_proc(args: &[&str]) -> Result<(), ExitCode> {
Ok(())
}
"files" => {
for (i, handle) in sys::process::file_handles().iter().enumerate() {
for (i, handle) in sys::process::handles().iter().enumerate() {
if let Some(resource) = handle {
println!("{}: {:?}", i, resource);
}
Expand Down Expand Up @@ -380,7 +380,7 @@ fn exec_with_config(cmd: &str, config: &mut Config) -> Result<(), ExitCode> {
let mut args: Vec<&str> = args.iter().map(String::as_str).collect();

// Redirections
let mut restore_file_handles = false;
let mut restore_handles = false;
let mut n = args.len();
let mut i = 0;
loop {
Expand Down Expand Up @@ -417,7 +417,7 @@ fn exec_with_config(cmd: &str, config: &mut Config) -> Result<(), ExitCode> {
continue;
}

// Parse file handles
// Parse handles
let mut num = String::new();
for c in args[i].chars() {
match c {
Expand All @@ -438,10 +438,10 @@ fn exec_with_config(cmd: &str, config: &mut Config) -> Result<(), ExitCode> {
}

if is_fat_arrow { // Redirections
restore_file_handles = true;
restore_handles = true;
if !num.is_empty() {
// if let Ok(right_handle) = num.parse() {}
println!("Redirecting to a file handle has not been implemented yet");
println!("Redirecting to a handle has not been implemented yet");
return Err(ExitCode::Failure);
} else {
if i == n - 1 {
Expand Down Expand Up @@ -538,7 +538,7 @@ fn exec_with_config(cmd: &str, config: &mut Config) -> Result<(), ExitCode> {


// TODO: Remove this when redirections are done in spawned process
if restore_file_handles {
if restore_handles {
for i in 0..3 {
api::fs::reopen("/dev/console", i, false).ok();
}
Expand Down
1 change: 0 additions & 1 deletion src/usr/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::api::syscall;
use crate::sys::fs::OpenFlag;

use alloc::format;
use alloc::string::String;
use alloc::vec;
use alloc::vec::Vec;
use core::str::{self, FromStr};
Expand Down
8 changes: 4 additions & 4 deletions www/shell.html
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,17 @@ <h2>Pipes and redirections (WIP)</h2>
</code></pre>

<p>The standard output is implied as the source of a redirection, but it is
possible to explicitly redirect a file handle to another (TODO):</p>
possible to explicitly redirect a handle to another (TODO):</p>

<pre><code>&gt; time read foo.txt [1]=&gt;[3]
</code></pre>

<p>Or to redirect a file handle to a file:</p>
<p>Or to redirect a handle to a file:</p>

<pre><code>&gt; time read foo.txt [1]=&gt; bar.txt
</code></pre>

<p>Or to pipe a file handle to another command:</p>
<p>Or to pipe a handle to another command:</p>

<pre><code>&gt; time read foo.txt [1]-&gt; write bar.txt
</code></pre>
Expand All @@ -150,7 +150,7 @@ <h2>Pipes and redirections (WIP)</h2>
<pre><code>&gt; write &lt;= req.txt =&gt; /net/http/moros.cc -&gt; find --line href -&gt; sort
</code></pre>

<p>NOTE: The following file handles are available when a process is created:</p>
<p>NOTE: The following handles are available when a process is created:</p>

<ul>
<li><code>stdin(0)</code></li>
Expand Down

0 comments on commit 4b241ed

Please sign in to comment.