-
Notifications
You must be signed in to change notification settings - Fork 0
Add userspace file utilities (stat, cp, mv, rm, mkdir, rmdir, touch) and /bin/uname #913
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| [package] | ||
| name = "cp" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
| authors = ["vibix hackers"] | ||
| license = "MIT OR Apache-2.0" | ||
|
|
||
| [[bin]] | ||
| name = "cp" | ||
| path = "src/main.rs" | ||
|
|
||
| # Standalone package — not part of the main workspace. Built with | ||
| # `-Z build-std` against the in-repo std fork (see xtask build). | ||
| [workspace] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| #![feature(restricted_std)] | ||
|
|
||
| #[cfg(not(test))] | ||
| mod syscalls; | ||
|
|
||
| use std::env; | ||
| use std::fs; | ||
| use std::process::ExitCode; | ||
|
|
||
| fn main() -> ExitCode { | ||
| let args: Vec<String> = env::args().collect(); | ||
|
|
||
| if args.len() != 3 { | ||
| eprintln!("usage: cp <src> <dst>"); | ||
| return ExitCode::from(1); | ||
| } | ||
|
|
||
| let src = &args[1]; | ||
| let dst = &args[2]; | ||
|
|
||
| if let Err(e) = fs::copy(src, dst) { | ||
| eprintln!("cp: {src} -> {dst}: {e}"); | ||
| return ExitCode::from(1); | ||
| } | ||
|
|
||
| ExitCode::from(0) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| //! C-ABI syscall shims required by the vibix std fork. | ||
| //! | ||
| //! The in-repo std fork links against POSIX symbols (`close`, etc.) that are | ||
| //! not provided by a system libc on vibix. We supply them here via raw | ||
| //! syscall instructions, mirroring the approach in `base/sh/src/syscalls.rs`. | ||
|
|
||
| use core::arch::asm; | ||
|
|
||
| const SYS_CLOSE: u64 = 3; | ||
|
|
||
| extern "C" { | ||
| fn __errno_location() -> *mut i32; | ||
| } | ||
|
|
||
| #[inline(always)] | ||
| unsafe fn raw1(nr: u64, a0: u64) -> i64 { | ||
| let ret: i64; | ||
| unsafe { | ||
| asm!( | ||
| "syscall", | ||
| inlateout("rax") nr => ret, | ||
| inlateout("rdi") a0 => _, | ||
| lateout("rcx") _, | ||
| lateout("r11") _, | ||
| lateout("rdx") _, | ||
| lateout("rsi") _, | ||
| lateout("r8") _, | ||
| lateout("r9") _, | ||
| lateout("r10") _, | ||
| options(nostack, preserves_flags), | ||
| ); | ||
| } | ||
| ret | ||
| } | ||
|
|
||
| /// Convert raw syscall return to C convention: on error set errno, return -1. | ||
| #[inline] | ||
| unsafe fn cvt(r: i64) -> i64 { | ||
| if r < 0 { | ||
| unsafe { *__errno_location() = (-r) as i32 }; | ||
| -1 | ||
| } else { | ||
| r | ||
| } | ||
| } | ||
|
|
||
| #[no_mangle] | ||
| pub unsafe extern "C" fn close(fd: i32) -> i32 { | ||
| unsafe { cvt(raw1(SYS_CLOSE, fd as u64)) as i32 } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| [package] | ||
| name = "mkdir" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
| authors = ["vibix hackers"] | ||
| license = "MIT OR Apache-2.0" | ||
|
|
||
| [[bin]] | ||
| name = "mkdir" | ||
| path = "src/main.rs" | ||
|
|
||
| # Standalone package — not part of the main workspace. Built with | ||
| # `-Z build-std` against the in-repo std fork (see xtask build). | ||
| [workspace] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| #![feature(restricted_std)] | ||
|
|
||
| #[cfg(not(test))] | ||
| mod syscalls; | ||
|
|
||
| use std::env; | ||
| use std::fs; | ||
| use std::process::ExitCode; | ||
|
|
||
| fn main() -> ExitCode { | ||
| let args: Vec<String> = env::args().collect(); | ||
|
|
||
| if args.len() <= 1 { | ||
| eprintln!("mkdir: missing operand"); | ||
| return ExitCode::from(1); | ||
| } | ||
|
|
||
| let mut status: u8 = 0; | ||
| for path in &args[1..] { | ||
| if let Err(e) = fs::create_dir(path) { | ||
| eprintln!("mkdir: {path}: {e}"); | ||
| status = 1; | ||
| } | ||
| } | ||
|
|
||
| ExitCode::from(status) | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,50 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| //! C-ABI syscall shims required by the vibix std fork. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| //! | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| //! The in-repo std fork links against POSIX symbols (`close`, etc.) that are | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| //! not provided by a system libc on vibix. We supply them here via raw | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| //! syscall instructions, mirroring the approach in `base/sh/src/syscalls.rs`. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| use core::arch::asm; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| const SYS_CLOSE: u64 = 3; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| extern "C" { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn __errno_location() -> *mut i32; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[inline(always)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| unsafe fn raw1(nr: u64, a0: u64) -> i64 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| let ret: i64; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| unsafe { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| asm!( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| "syscall", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| inlateout("rax") nr => ret, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| inlateout("rdi") a0 => _, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| lateout("rcx") _, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| lateout("r11") _, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| lateout("rdx") _, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| lateout("rsi") _, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| lateout("r8") _, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| lateout("r9") _, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| lateout("r10") _, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| options(nostack, preserves_flags), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🌐 Web query:
💡 Result: No, the Citations:
🏁 Script executed: #!/bin/bash
# Find all syscalls.rs files and check for preserves_flags usage
find . -name "syscalls.rs" -type f | head -20Repository: dburkart/vibix Length of output: 389 🏁 Script executed: #!/bin/bash
# Check the specific file and location mentioned in the review
if [ -f "base/mkdir/src/syscalls.rs" ]; then
echo "=== base/mkdir/src/syscalls.rs lines 19-30 ==="
sed -n '19,30p' "base/mkdir/src/syscalls.rs"
fiRepository: dburkart/vibix Length of output: 475 🏁 Script executed: #!/bin/bash
# Search for all occurrences of preserves_flags in syscall blocks
rg "preserves_flags" --type rust -B 3 -A 1Repository: dburkart/vibix Length of output: 50370 🏁 Script executed: #!/bin/bash
# Find all base/*/src/syscalls.rs files that contain preserves_flags with syscall
for file in base/*/src/syscalls.rs; do
if grep -q "preserves_flags" "$file"; then
echo "=== $file ==="
grep -c "preserves_flags" "$file"
fi
doneRepository: dburkart/vibix Length of output: 447 🏁 Script executed: #!/bin/bash
# Show one full syscall block from a sibling to confirm same pattern
echo "=== Example from base/sh/src/syscalls.rs ==="
head -35 base/sh/src/syscalls.rs | tail -15Repository: dburkart/vibix Length of output: 512 🏁 Script executed: #!/bin/bash
# Verify one of the sibling files has the same issue
echo "=== base/cat/src/syscalls.rs syscall block with preserves_flags ==="
grep -B 10 -A 2 "preserves_flags" base/cat/src/syscalls.rs | head -15Repository: dburkart/vibix Length of output: 499 🏁 Script executed: #!/bin/bash
# Count total syscall blocks with preserves_flags across all base utils
total=0
for file in base/*/src/syscalls.rs; do
count=$(grep -c "preserves_flags" "$file")
if [ "$count" -gt 0 ]; then
total=$((total + count))
fi
done
echo "Total preserves_flags occurrences in base/*/src/syscalls.rs: $total"Repository: dburkart/vibix Length of output: 122 Drop The Example fix for base/mkdir/src/syscalls.rs asm!(
"syscall",
inlateout("rax") nr => ret,
inlateout("rdi") a0 => _,
lateout("rcx") _,
lateout("r11") _,
lateout("rdx") _,
lateout("rsi") _,
lateout("r8") _,
lateout("r9") _,
lateout("r10") _,
- options(nostack, preserves_flags),
+ options(nostack),
);📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ret | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// Convert raw syscall return to C convention: on error set errno, return -1. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[inline] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| unsafe fn cvt(r: i64) -> i64 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if r < 0 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| unsafe { *__errno_location() = (-r) as i32 }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| -1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| r | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[no_mangle] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub unsafe extern "C" fn close(fd: i32) -> i32 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| unsafe { cvt(raw1(SYS_CLOSE, fd as u64)) as i32 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| [package] | ||
| name = "mv" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
| authors = ["vibix hackers"] | ||
| license = "MIT OR Apache-2.0" | ||
|
|
||
| [[bin]] | ||
| name = "mv" | ||
| path = "src/main.rs" | ||
|
|
||
| # Standalone package — not part of the main workspace. Built with | ||
| # `-Z build-std` against the in-repo std fork (see xtask build). | ||
| [workspace] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| #![feature(restricted_std)] | ||
|
|
||
| #[cfg(not(test))] | ||
| mod syscalls; | ||
|
|
||
| use std::env; | ||
| use std::fs; | ||
| use std::process::ExitCode; | ||
|
|
||
| fn main() -> ExitCode { | ||
| let args: Vec<String> = env::args().collect(); | ||
|
|
||
| if args.len() != 3 { | ||
| eprintln!("usage: mv <src> <dst>"); | ||
| return ExitCode::from(1); | ||
| } | ||
|
|
||
| let src = &args[1]; | ||
| let dst = &args[2]; | ||
|
|
||
| if let Err(e) = fs::rename(src, dst) { | ||
| eprintln!("mv: {src} -> {dst}: {e}"); | ||
| return ExitCode::from(1); | ||
| } | ||
|
|
||
| ExitCode::from(0) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| //! C-ABI syscall shims required by the vibix std fork. | ||
| //! | ||
| //! The in-repo std fork links against POSIX symbols (`close`, etc.) that are | ||
| //! not provided by a system libc on vibix. We supply them here via raw | ||
| //! syscall instructions, mirroring the approach in `base/sh/src/syscalls.rs`. | ||
|
|
||
| use core::arch::asm; | ||
|
|
||
| const SYS_CLOSE: u64 = 3; | ||
|
|
||
| extern "C" { | ||
| fn __errno_location() -> *mut i32; | ||
| } | ||
|
|
||
| #[inline(always)] | ||
| unsafe fn raw1(nr: u64, a0: u64) -> i64 { | ||
| let ret: i64; | ||
| unsafe { | ||
| asm!( | ||
| "syscall", | ||
| inlateout("rax") nr => ret, | ||
| inlateout("rdi") a0 => _, | ||
| lateout("rcx") _, | ||
| lateout("r11") _, | ||
| lateout("rdx") _, | ||
| lateout("rsi") _, | ||
| lateout("r8") _, | ||
| lateout("r9") _, | ||
| lateout("r10") _, | ||
| options(nostack, preserves_flags), | ||
| ); | ||
| } | ||
| ret | ||
| } | ||
|
|
||
| /// Convert raw syscall return to C convention: on error set errno, return -1. | ||
| #[inline] | ||
| unsafe fn cvt(r: i64) -> i64 { | ||
| if r < 0 { | ||
| unsafe { *__errno_location() = (-r) as i32 }; | ||
| -1 | ||
| } else { | ||
| r | ||
| } | ||
| } | ||
|
|
||
| #[no_mangle] | ||
| pub unsafe extern "C" fn close(fd: i32) -> i32 { | ||
| unsafe { cvt(raw1(SYS_CLOSE, fd as u64)) as i32 } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| [package] | ||
| name = "rm" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
| authors = ["vibix hackers"] | ||
| license = "MIT OR Apache-2.0" | ||
|
|
||
| [[bin]] | ||
| name = "rm" | ||
| path = "src/main.rs" | ||
|
|
||
| # Standalone package — not part of the main workspace. Built with | ||
| # `-Z build-std` against the in-repo std fork (see xtask build). | ||
| [workspace] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| #![feature(restricted_std)] | ||
|
|
||
| #[cfg(not(test))] | ||
| mod syscalls; | ||
|
|
||
| use std::env; | ||
| use std::fs; | ||
| use std::process::ExitCode; | ||
|
|
||
| fn main() -> ExitCode { | ||
| let args: Vec<String> = env::args().collect(); | ||
|
|
||
| if args.len() <= 1 { | ||
| eprintln!("rm: missing operand"); | ||
| return ExitCode::from(1); | ||
| } | ||
|
|
||
| let mut status: u8 = 0; | ||
| for path in &args[1..] { | ||
| if let Err(e) = fs::remove_file(path) { | ||
| eprintln!("rm: {path}: {e}"); | ||
| status = 1; | ||
| } | ||
| } | ||
|
|
||
| ExitCode::from(status) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| //! C-ABI syscall shims required by the vibix std fork. | ||
| //! | ||
| //! The in-repo std fork links against POSIX symbols (`close`, etc.) that are | ||
| //! not provided by a system libc on vibix. We supply them here via raw | ||
| //! syscall instructions, mirroring the approach in `base/sh/src/syscalls.rs`. | ||
|
|
||
| use core::arch::asm; | ||
|
|
||
| const SYS_CLOSE: u64 = 3; | ||
|
|
||
| extern "C" { | ||
| fn __errno_location() -> *mut i32; | ||
| } | ||
|
|
||
| #[inline(always)] | ||
| unsafe fn raw1(nr: u64, a0: u64) -> i64 { | ||
| let ret: i64; | ||
| unsafe { | ||
| asm!( | ||
| "syscall", | ||
| inlateout("rax") nr => ret, | ||
| inlateout("rdi") a0 => _, | ||
| lateout("rcx") _, | ||
| lateout("r11") _, | ||
| lateout("rdx") _, | ||
| lateout("rsi") _, | ||
| lateout("r8") _, | ||
| lateout("r9") _, | ||
| lateout("r10") _, | ||
| options(nostack, preserves_flags), | ||
| ); | ||
| } | ||
| ret | ||
| } | ||
|
|
||
| /// Convert raw syscall return to C convention: on error set errno, return -1. | ||
| #[inline] | ||
| unsafe fn cvt(r: i64) -> i64 { | ||
| if r < 0 { | ||
| unsafe { *__errno_location() = (-r) as i32 }; | ||
| -1 | ||
| } else { | ||
| r | ||
| } | ||
| } | ||
|
|
||
| #[no_mangle] | ||
| pub unsafe extern "C" fn close(fd: i32) -> i32 { | ||
| unsafe { cvt(raw1(SYS_CLOSE, fd as u64)) as i32 } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| [package] | ||
| name = "rmdir" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
| authors = ["vibix hackers"] | ||
| license = "MIT OR Apache-2.0" | ||
|
|
||
| [[bin]] | ||
| name = "rmdir" | ||
| path = "src/main.rs" | ||
|
|
||
| # Standalone package — not part of the main workspace. Built with | ||
| # `-Z build-std` against the in-repo std fork (see xtask build). | ||
| [workspace] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major | 🏗️ Heavy lift
🧩 Analysis chain
🏁 Script executed:
Repository: dburkart/vibix
Length of output: 1178
🏁 Script executed:
Repository: dburkart/vibix
Length of output: 3858
🏁 Script executed:
Repository: dburkart/vibix
Length of output: 872
🏁 Script executed:
Repository: dburkart/vibix
Length of output: 4536
🏁 Script executed:
Repository: dburkart/vibix
Length of output: 964
Consolidate duplicated syscall shims—inconsistencies have already emerged across copies.
The
raw1/cvt/closeshim is now duplicated across 10 crates. More critically, these copies have already diverged:catandlsuseoptions(nostack, preserves_flags)in their asm! blocks, whilecp,mkdir,mv,rm,rmdir,stat,touch, andunameuse onlyoptions(nostack). This inconsistency is exactly the maintainability risk this structure creates—fixes and updates land unevenly and go unnoticed.Move this shim into a single shared module or helper crate, or have the build system inject it consistently, so ABI-sensitive code like syscall assembly stays synchronized across all utilities.
🤖 Prompt for AI Agents