-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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), | ||
| ); | ||
| } | ||
| 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 } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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), | ||
| ); | ||
| } | ||
| 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 } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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), | ||
| ); | ||
| } | ||
| 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 } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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), | ||
| ); | ||
| } | ||
| 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 } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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