| Crates.io | filemode |
| lib.rs | filemode |
| version | 0.1.0 |
| created_at | 2025-11-10 19:17:15.838251+00 |
| updated_at | 2025-11-10 19:17:15.838251+00 |
| description | Type-safe, zero-cost conversions between Unix mode_t and Go os.FileMode formats |
| homepage | |
| repository | https://github.com/AprilNEA/buildkit-client |
| max_upload_size | |
| id | 1925968 |
| size | 16,486 |
A Rust library for converting between Unix mode_t and Go os.FileMode formats.
This crate provides type-safe, zero-cost abstractions for converting file permission and type information between Unix (POSIX) systems and Go's file mode representation. It's particularly useful when working with systems that bridge Rust and Go ecosystems, such as BuildKit, Docker, or other container build systems.
From/Into traits for ergonomic conversionsAdd this to your Cargo.toml:
[dependencies]
filemode = "0.1"
use filemode::{UnixMode, GoFileMode};
// Convert Unix directory mode to Go FileMode
let unix_mode = UnixMode::from(0o040755);
let go_mode: GoFileMode = unix_mode.into();
assert_eq!(go_mode.as_u32(), 0x800001ed); // ModeDir | 0o755
// Convert Unix regular file mode
let unix_mode = UnixMode::from(0o100644);
let go_mode = GoFileMode::from(unix_mode);
assert_eq!(go_mode.as_u32(), 0o644);
use filemode::unix_mode_to_go_filemode;
// Directory with 0o755 permissions
let go_mode = unix_mode_to_go_filemode(0o040755);
assert_eq!(go_mode, 0x800001ed);
// Regular file with 0o644 permissions
let go_mode = unix_mode_to_go_filemode(0o100644);
assert_eq!(go_mode, 0o644);
use std::fs;
use std::os::unix::fs::PermissionsExt;
use filemode::{UnixMode, GoFileMode};
// Get file metadata from filesystem
let metadata = fs::metadata("some_file.txt")?;
let unix_mode = metadata.permissions().mode();
// Convert to Go FileMode for use with BuildKit or other Go-based tools
let go_mode = GoFileMode::from(UnixMode::from(unix_mode));
// Send go_mode.as_u32() to your gRPC/protocol buffer message
Unix uses bits 14-12 for file type:
S_IFDIR (0o040000) - DirectoryS_IFREG (0o100000) - Regular fileS_IFLNK (0o120000) - Symbolic linkPermissions are stored in the lower 12 bits (0o7777).
Go uses high bits for file type flags:
Regular files have no special type bit set.
| Unix Type | Octal | Go Mode | Hex |
|---|---|---|---|
| Regular file | 0o100000 | (none) | 0x00000000 |
| Directory | 0o040000 | ModeDir | 0x80000000 |
| Symlink | 0o120000 | ModeSymlink | 0x08000000 |
| Named pipe | 0o010000 | ModeNamedPipe | 0x02000000 |
| Socket | 0o140000 | ModeSocket | 0x01000000 |
Special permission bits (setuid, setgid, sticky) are also converted appropriately.
When building systems that interact with both Rust and Go code (like container build systems), you often need to serialize file metadata in a way that Go can understand. Direct conversion of Unix mode_t values to Go's FileMode format requires careful bit manipulation. This crate handles all the edge cases correctly.
All conversion functions are marked #[inline] and compile down to simple bit operations. There is zero runtime overhead compared to manual bit manipulation.
This project is licensed under either of
at your option.
Contributions are welcome! Please feel free to submit a Pull Request.