Use std for dir reads, impl cd, and print errors right

This commit is contained in:
pjht 2024-10-04 12:39:41 -05:00
parent 645af427ca
commit 7eaf3b933c
Signed by: pjht
GPG Key ID: CA239FC6934E6F3A
3 changed files with 85 additions and 252 deletions

244
Cargo.lock generated
View File

@ -1,6 +1,6 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
version = 4
[[package]]
name = "anstream"
@ -51,44 +51,11 @@ dependencies = [
"windows-sys",
]
[[package]]
name = "atomic-polyfill"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8cf2bce30dfe09ef0bfaef228b9d414faaf7e563035494d7fe092dba54b300f4"
dependencies = [
"critical-section",
]
[[package]]
name = "autocfg"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0"
[[package]]
name = "bitflags"
version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
[[package]]
name = "byteorder"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "4.5.18"
version = "4.5.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0956a43b323ac1afaffc053ed5c4b7c1f1800bacd1683c353aabbb752515dd3"
checksum = "7be5744db7978a28d9df86a214130d106a89ce49644cbc4e3f0c22c3fba30615"
dependencies = [
"clap_builder",
"clap_derive",
@ -96,9 +63,9 @@ dependencies = [
[[package]]
name = "clap_builder"
version = "4.5.18"
version = "4.5.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d72166dd41634086d5803a47eb71ae740e61d84709c36f3c34110173db3961b"
checksum = "a5fbc17d3ef8278f55b282b2a2e75ae6f6c7d4bb70ed3d0382375104bfafdb4b"
dependencies = [
"anstream",
"anstyle",
@ -124,67 +91,12 @@ version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97"
[[package]]
name = "cobs"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67ba02a97a2bd10f4b59b25c7973101c79642302776489e030cd13cdab09ed15"
[[package]]
name = "colorchoice"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0"
[[package]]
name = "critical-section"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f64009896348fc5af4222e9cf7d7d82a95a256c634ebcf61c53e4ea461422242"
[[package]]
name = "dir_rpc"
version = "0.1.0"
dependencies = [
"parking_lot",
"postcard",
]
[[package]]
name = "embedded-io"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef1a6892d9eef45c8fa6b9e0086428a2cca8491aca8f787c534a3d6d0bcb3ced"
[[package]]
name = "embedded-io"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "edd0f118536f44f5ccd48bcb8b111bdc3de888b58c74639dfb034a357d0f206d"
[[package]]
name = "hash32"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0c35f58762feb77d74ebe43bdbc3210f09be9fe6742234d573bacc26ed92b67"
dependencies = [
"byteorder",
]
[[package]]
name = "heapless"
version = "0.7.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdc6457c0eb62c71aac4bc17216026d8410337c4126773b9c5daba343f17964f"
dependencies = [
"atomic-polyfill",
"hash32",
"rustc_version",
"serde",
"spin",
"stable_deref_trait",
]
[[package]]
name = "heck"
version = "0.5.0"
@ -197,58 +109,6 @@ version = "1.70.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
[[package]]
name = "libc"
version = "0.2.159"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "561d97a539a36e26a9a5fad1ea11a3039a67714694aaa379433e580854bc3dc5"
[[package]]
name = "lock_api"
version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17"
dependencies = [
"autocfg",
"scopeguard",
]
[[package]]
name = "parking_lot"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27"
dependencies = [
"lock_api",
"parking_lot_core",
]
[[package]]
name = "parking_lot_core"
version = "0.9.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8"
dependencies = [
"cfg-if",
"libc",
"redox_syscall",
"smallvec",
"windows-targets",
]
[[package]]
name = "postcard"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f7f0a8d620d71c457dd1d47df76bb18960378da56af4527aaa10f515eee732e"
dependencies = [
"cobs",
"embedded-io 0.4.0",
"embedded-io 0.6.1",
"heapless",
"serde",
]
[[package]]
name = "proc-macro2"
version = "1.0.86"
@ -263,9 +123,7 @@ name = "pts_tester"
version = "0.1.0"
dependencies = [
"clap",
"dir_rpc",
"shlex",
"vfs_rpc",
]
[[package]]
@ -277,83 +135,12 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "redox_syscall"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "355ae415ccd3a04315d3f8246e86d67689ea74d88d915576e1589a351062a13b"
dependencies = [
"bitflags",
]
[[package]]
name = "rustc_version"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92"
dependencies = [
"semver",
]
[[package]]
name = "scopeguard"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "semver"
version = "1.0.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b"
[[package]]
name = "serde"
version = "1.0.210"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c8e3592472072e6e22e0a54d5904d9febf8508f65fb8552499a1abc7d1078c3a"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.210"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "smallvec"
version = "1.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
[[package]]
name = "spin"
version = "0.9.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67"
dependencies = [
"lock_api",
]
[[package]]
name = "stable_deref_trait"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3"
[[package]]
name = "strsim"
version = "0.11.1"
@ -362,9 +149,9 @@ checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]]
name = "syn"
version = "2.0.77"
version = "2.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f35bcdf61fd8e7be6caf75f429fdca8beb3ed76584befb503b1569faee373ed"
checksum = "89132cd0bf050864e1d38dc3bbc07a0eb8e7530af26344d3d2bbbef83499f590"
dependencies = [
"proc-macro2",
"quote",
@ -383,15 +170,6 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "vfs_rpc"
version = "0.1.0"
dependencies = [
"parking_lot",
"postcard",
"serde",
]
[[package]]
name = "windows-sys"
version = "0.52.0"
@ -464,3 +242,11 @@ name = "windows_x86_64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[patch.unused]]
name = "serde"
version = "1.0.210"
[[patch.unused]]
name = "serde_derive"
version = "1.0.210"

View File

@ -5,6 +5,8 @@ edition = "2021"
[dependencies]
clap = { version = "4.5.18", features = ["derive"] }
dir_rpc = { version = "0.1.0", path = "../dir_rpc" }
shlex = "1.3.0"
vfs_rpc = { version = "0.1.0", path = "../vfs/vfs_rpc" }
[patch.crates-io]
serde = { path = "../serde/serde" }
serde_derive = { path = "../serde/serde_derive" }

View File

@ -1,7 +1,6 @@
use std::{
io::{self, Write},
os::mikros::syscalls,
path::{Path, PathBuf},
path::{Component, Path, PathBuf},
};
use clap::Parser;
@ -16,17 +15,31 @@ struct Cat {
path: PathBuf,
}
fn main() {
let vfs_pid = loop {
if let Some(pid) = syscalls::try_get_registered(0) {
break pid;
}
};
let vfs_client = vfs_rpc::Client::new(vfs_pid);
#[derive(Parser, Debug)]
struct Cd {
path: PathBuf,
}
fn canonicalize_path(path: &Path) -> PathBuf {
path.components()
.fold(PathBuf::from("/"), |mut buf, component| match component {
Component::Prefix(_) => unreachable!(),
Component::RootDir | Component::Normal(_) => {
buf.push(component);
buf
}
Component::CurDir => buf,
Component::ParentDir => {
buf.pop();
buf
}
})
}
fn main() {
let mut line = String::new();
let cwd = Path::new("/").to_owned();
let mut cwd = Path::new("/").to_owned();
loop {
print!(
@ -62,17 +75,20 @@ fn main() {
let abs_path = if cmd_path.is_relative() {
let mut abs_path = cwd.clone();
abs_path.push(cmd_path.clone());
abs_path
canonicalize_path(&abs_path)
} else {
cmd_path.clone()
};
let Ok((fs_pid, dir_fd)) = vfs_client.open_dir(&abs_path) else {
println!("ls: {}: failed to open", cmd_path.display());
continue;
let dir = match std::fs::read_dir(&abs_path) {
Ok(val) => val,
Err(e) => {
println!("ls: {}: {}", cmd_path.display(), e);
continue;
}
};
let fs_client = dir_rpc::Client::new(fs_pid);
while let Some(entry) = fs_client.next_entry(dir_fd).unwrap() {
println!("{}", entry);
for entry in dir {
let entry = entry.unwrap();
println!("{}", entry.file_name().to_string_lossy());
}
}
"cat" => {
@ -86,13 +102,16 @@ fn main() {
let abs_path = if cmd.path.is_relative() {
let mut abs_path = cwd.clone();
abs_path.push(cmd.path.clone());
abs_path
canonicalize_path(&abs_path)
} else {
cmd.path.clone()
};
let Ok(contents) = std::fs::read(&abs_path) else {
println!("cat: {}: failed to read", cmd.path.display());
continue;
let contents = match std::fs::read(&abs_path) {
Ok(val) => val,
Err(e) => {
println!("cat: {}: {}", cmd.path.display(), e);
continue;
}
};
io::stdout().lock().write_all(&contents).unwrap();
if let Some(&last_byte) = contents.last() {
@ -101,6 +120,32 @@ fn main() {
}
}
}
"cd" => {
let cmd = match Cd::try_parse_from(&cmd) {
Ok(cmd) => cmd,
Err(err) => {
err.print().unwrap();
continue;
}
};
let abs_path = if cmd.path.is_relative() {
let mut abs_path = cwd.clone();
abs_path.push(cmd.path.clone());
canonicalize_path(&abs_path)
} else {
cmd.path.clone()
};
// Try to open the given directory, if it fails it's not a valid directory.
// Use Metadata instead once it's implemented.
match std::fs::read_dir(&abs_path) {
Ok(_) => (),
Err(e) => {
println!("cd: {}: {}", cmd.path.display(), e);
continue;
}
};
cwd = abs_path;
}
"exit" => {
break;
}