System Calls & File I/O in Rust

VerifiedSafe

Guide to using bun_sys for system calls and file I/O in Rust, with rich error handling and cross-platform support.

Sby Skills Guide Bot
DevelopmentAdvanced
8/29/2026
Claude Code
#rust#syscalls#file-io#error-handling#bun-sys

Recommended for

Automated summary

Guides use of the internal bun_sys crate for system calls and file I/O in Rust instead of std::fs or libc.

Strengths

  • Provides complete, copyable examples (File::openat, read_all, write_all) with Fd and O flags
  • Explains Maybe<T> and bun_sys::Error handling — errno, syscall tag, path — including branching on ENOENT
  • Explicitly lists common mistakes: risky unwrap, std::fs::File, stack-allocated PathBuffer, double-closing the fd
  • Covers cross-platform specifics such as the Windows libuv fallback and the path buffer pool

Limitations

  • Tightly coupled to Bun's internal crates (bun_sys, bun_core, bun_paths, bun_sys_jsc), so it does not transfer to other projects
  • Documents only a subset of calls (open, read, write, stat, mkdir, unlink…) and ignores async or concurrency concerns
  • Assumes prior familiarity with POSIX file descriptors and Rust ownership
When to use it

Use it when writing or reviewing Rust code inside the Bun repository that opens descriptors, reads or writes files, or replaces std::fs and libc calls.

When not to use it

Avoid it for standalone Rust projects where bun_sys is unavailable, or for tasks unrelated to files and system calls.

Security analysis

Safe
Quality score85/100

This is purely a reference/documentation skill describing how to use the internal bun_sys Rust crate for file I/O and syscalls. It contains no executable commands, no network or shell invocations, no credential handling, and no destructive operations. The code samples are illustrative and scoped to legitimate file operations.

No concerns found

How this analysis is made

Examples

Migrate std::fs to bun_sys
Here is a function in my Bun Rust module that uses std::fs::File and unwrap(). Rewrite it with bun_sys::File and File::openat, propagating errors as Maybe<T> instead of panicking.
Handle ENOENT properly
My bun_sys::unlink call fails intermittently because the file may already be gone. Show me how to match on bun_sys::Error and check e.errno() == bun_c::ENOENT while letting other errors propagate.
Safe path joining for openat
Write a helper that joins a directory and a file name using bun_paths path_buffer_pool and resolve_path, then opens the result with File::openat(Fd::cwd(), …) without allocating a PathBuffer on the stack.

name: rust-system-calls description: Guides using bun_sys for system calls and file I/O in Rust. Use when implementing file operations, opening fds, or any syscall path instead of std::fs or libc.

System Calls & File I/O in Rust

Use bun_sys instead of std::fs or raw libc for cross-platform syscalls with proper error handling.

bun_sys::File (Preferred)

For most file operations, use the bun_sys::File wrapper. It owns the fd and closes on Drop.

use bun_sys::{File, Fd, O};

let file = File::openat(Fd::cwd(), b"path/to/file", O::RDONLY, 0)?;
let mut buf = vec![0u8; 4096];
let n = file.read_all(&mut buf)?;     // loops until EOF or full
// `file` closes on Drop.

Complete Example

use bun_sys::{File, Fd, O};

pub fn write_file(path: &[u8], data: &[u8]) -> Result<(), bun_sys::Error> {
    let file = File::openat(Fd::cwd(), path, O::WRONLY | O::CREAT | O::TRUNC, 0o664)?;
    file.write_all(data)?;
    Ok(())
}

Why bun_sys?

| Aspect | bun_sys | std::fs / libc | | ----------- | ---------------------------------- | ---------------------- | | Return Type | Maybe<T> with rich Error | io::Error (lossy) | | Windows | Full support with libuv fallback | Incomplete/POSIX-ish | | Error Info | errno, syscall tag, path, fd | errno only | | EINTR | Automatic retry | Manual handling | | Paths | &[u8] (WTF-8 safe) | &Path (UTF-8 lossy) |

Error Handling with Maybe<T>

bun_sys functions return Maybe<T> = Result<T, bun_sys::Error>. Propagate with ?; convert to a JS exception via bun_sys_jsc::ErrorJsc::to_js:

use bun_sys_jsc::ErrorJsc;
use bun_sys::{File, Fd, O};

let file = match File::openat(Fd::cwd(), path, O::RDONLY, 0) {
    Ok(f) => f,
    Err(err) => return Ok(err.to_js(global)?),
};

bun_sys::Error carries errno, syscall: Tag, and path: Box<[u8]>. To branch on errno:

match bun_sys::unlink(path) {
    Ok(()) => {}
    Err(e) if e.errno() == bun_c::ENOENT => {} // already gone
    Err(e) => return Err(e),
}

Key Types and Functions

  • Fd (bun_core::Fd) — cross-platform file descriptor. Fd::cwd(), Fd::stdin()/stdout()/stderr(), fd.close().
  • File::open(path: &ZStr, flags, mode) / File::openat(dir: Fd, path: &[u8], flags, mode) / File::make_open(...) (creates parent dirs) / File::create(dir, path, truncate)
  • file.read(buf) / read_all(buf) / read_to_end() / read_to_end_small() / write(buf) / write_all(buf)
  • bun_sys::open, read, write, pread, pwrite, stat, fstat, lstat, mkdir, unlink, rename, symlink, chmod — free fns over Fd
  • Open flags: bun_sys::O::RDONLY, O::WRONLY | O::CREAT | O::TRUNC, etc.

Path Buffers

Use bun_paths for joining/normalization and the path-buffer pool to avoid 64 KB stack allocations on Windows:

use bun_paths::{path_buffer_pool, resolve_path::{self, platform}};

let mut buf = path_buffer_pool::get();
let joined = resolve_path::join_string_buf::<platform::Auto>(&mut *buf, &[dir, name]);
let file = File::openat(Fd::cwd(), joined, O::RDONLY, 0)?;

Common Mistakes

  • Don't .unwrap() a bun_sys result that user input or the OS can cause to fail at runtime — return the error.
  • Don't use std::fs::File — it loses the syscall tag and path needed for Node-compatible error objects.
  • Don't allocate PathBuffer on the stack in hot paths — use path_buffer_pool::get().
  • Don't forget Drop alone closes a File — never file.fd().close() while the File is still live (double close).

See src/CLAUDE.md for the full bun_core/bun_sys reference.

From the file to a first result

The download contains exactly the content displayed on this page. This copy may differ from the latest version in the source repository.

View the source and supporting files

This download contains SKILL.md only. Any scripts, references, templates and dependencies are not included. Get the required files from the source, preserving their directory structure, before continuing.

1. Download the file

Download SKILL.md

From the root of a test project, save the file as .skill-downloads/system-calls-file-i-o-in-rust-mte1lfw0/SKILL.md, or use the command below. This folder is for inspecting the skill before installation.

Command for macOS/Linux or WSL: requires a POSIX shell and curl. On Windows without WSL, use the download button and create the folder manually.

mkdir -p '.skill-downloads/system-calls-file-i-o-in-rust-mte1lfw0' && curl --fail --show-error --location 'https://skills-guides.com/api/skills/system-calls-file-i-o-in-rust-mte1lfw0/download' --output '.skill-downloads/system-calls-file-i-o-in-rust-mte1lfw0/SKILL.md.part' && mv '.skill-downloads/system-calls-file-i-o-in-rust-mte1lfw0/SKILL.md.part' '.skill-downloads/system-calls-file-i-o-in-rust-mte1lfw0/SKILL.md'

2. Check prerequisites

Read SKILL.md before using it. If it references scripts or external services, inspect them and prepare the required access in your test environment.

No recognized prerequisites section in this file. This does not mean it is self-contained: libraries, tools and access requirements may be mentioned elsewhere in the content or source repository.

The platforms on this page are catalog indications. The paths below follow tool documentation; they do not prove this skill has been run in those tools. No minimum tool version is confirmed for this listing.

3. Load the skill

After inspection, copy the complete folder into your project using one of the commands below, then open a new tool session. The command stops if a folder with the same name already exists: compare the files before replacing it.

Claude Code
test -f '.skill-downloads/system-calls-file-i-o-in-rust-mte1lfw0/SKILL.md' && test -s '.skill-downloads/system-calls-file-i-o-in-rust-mte1lfw0/SKILL.md' && mkdir -p '.claude/skills' && test ! -e '.claude/skills/rust-system-calls' && cp -R '.skill-downloads/system-calls-file-i-o-in-rust-mte1lfw0' '.claude/skills/rust-system-calls'

OpenSkills

OpenSkills must already be installed. The local install/read procedure is checked with OpenSkills 1.5.0, which requires Node.js ≥ 20.6.0. Catalog slugs are not resolved automatically.

Official OpenSkills installation
openskills install './.skill-downloads/system-calls-file-i-o-in-rust-mte1lfw0'

openskills read 'system-calls-file-i-o-in-rust-mte1lfw0'

Reading should display the installed file content and its base directory. It does not run the skill or install its dependencies.

For an agent that reads AGENTS.md, then sync the index. This command changes AGENTS.md: review the diff before using it.

openskills sync

Manual method or another tool: attach SKILL.md to a conversation, or open it in an agent that can read files. Include any required supporting resources. No universal folder is recognized by every tool.

4. Try a first task

Use a short, synthetic input: an API endpoint, a small document or an interface brief. In a new conversation, attach the file or give the agent its path, then adapt this request:

Read .skill-downloads/system-calls-file-i-o-in-rust-mte1lfw0/SKILL.md and apply its instructions to this example: [describe a test input]. Identify any missing files, dependencies or access. Produce the result requested by the skill without inventing data or running unreviewed scripts.

Expected result: output matching the skill instructions, or a precise list of missing prerequisites. A read confirmation alone does not validate a first use. If an external resource is unavailable, that path remains untested.

Related skills