jetstream_libc/
lib.rs

1#![no_std]
2#![doc(
3    html_logo_url = "https://raw.githubusercontent.com/sevki/jetstream/main/logo/JetStream.png"
4)]
5#![doc(
6    html_favicon_url = "https://raw.githubusercontent.com/sevki/jetstream/main/logo/JetStream.png"
7)]
8#![cfg_attr(docsrs, feature(doc_cfg))]
9//! Platform-specific libc bindings for jetstream
10//!
11//! This crate provides a unified interface to libc constants across different platforms,
12//! including wasm32 targets where some constants may not be available.
13
14extern crate libc;
15
16// Types - Linux uses libc, others use core::ffi
17#[cfg(target_os = "linux")]
18pub use libc::{c_int, gid_t, mode_t, pid_t, ucred, uid_t};
19
20#[cfg(not(target_os = "linux"))]
21pub use core::ffi::c_int;
22
23#[cfg(not(target_os = "linux"))]
24#[allow(non_camel_case_types)]
25pub type mode_t = u32;
26
27#[cfg(not(target_os = "linux"))]
28#[allow(non_camel_case_types)]
29pub type pid_t = i32;
30
31#[cfg(not(target_os = "linux"))]
32#[allow(non_camel_case_types)]
33pub type uid_t = u32;
34
35#[cfg(not(target_os = "linux"))]
36#[allow(non_camel_case_types)]
37pub type gid_t = u32;
38
39// Error constants - Unix platforms use libc, others define them
40#[cfg(any(target_os = "linux", target_os = "macos"))]
41pub use libc::{
42    EADDRINUSE, EADDRNOTAVAIL, ECONNABORTED, ECONNREFUSED, ECONNRESET, EEXIST,
43    EINTR, EINVAL, EIO, ENOENT, ENOTCONN, EPERM, EPIPE, ETIMEDOUT, EWOULDBLOCK,
44};
45
46#[cfg(any(target_arch = "wasm32", target_os = "windows"))]
47mod error_constants {
48    use super::c_int;
49
50    pub const EPERM: c_int = 1;
51    pub const ENOENT: c_int = 2;
52    pub const EIO: c_int = 5;
53    pub const EEXIST: c_int = 17;
54    pub const EINVAL: c_int = 22;
55    pub const EPIPE: c_int = 32;
56    pub const EWOULDBLOCK: c_int = 35;
57    pub const EADDRINUSE: c_int = 98;
58    pub const EADDRNOTAVAIL: c_int = 99;
59    pub const ECONNABORTED: c_int = 103;
60    pub const ECONNRESET: c_int = 104;
61    pub const ENOTCONN: c_int = 107;
62    pub const ETIMEDOUT: c_int = 110;
63    pub const ECONNREFUSED: c_int = 111;
64    pub const EINTR: c_int = 4;
65}
66
67#[cfg(any(target_arch = "wasm32", target_os = "windows"))]
68pub use error_constants::*;
69
70// Open flags - Unix platforms use libc, others define them
71#[cfg(any(target_os = "linux", target_os = "macos"))]
72pub use libc::{
73    O_APPEND, O_CREAT, O_DSYNC, O_EXCL, O_NOCTTY, O_NONBLOCK, O_RDONLY, O_RDWR,
74    O_SYNC, O_TRUNC, O_WRONLY,
75};
76
77#[cfg(any(target_arch = "wasm32", target_os = "windows"))]
78mod open_flags {
79    use super::c_int;
80
81    pub const O_RDONLY: c_int = 0;
82    pub const O_WRONLY: c_int = 1;
83    pub const O_RDWR: c_int = 2;
84    pub const O_CREAT: c_int = 0o100;
85    pub const O_EXCL: c_int = 0o200;
86    pub const O_NOCTTY: c_int = 0o400;
87    pub const O_TRUNC: c_int = 0o1000;
88    pub const O_APPEND: c_int = 0o2000;
89    pub const O_NONBLOCK: c_int = 0o4000;
90    pub const O_DSYNC: c_int = 0o10000;
91    pub const O_SYNC: c_int = 0o4010000;
92}
93
94#[cfg(any(target_arch = "wasm32", target_os = "windows"))]
95pub use open_flags::*;
96
97// Linux-specific flags
98#[cfg(target_os = "linux")]
99pub use libc::{O_DIRECT, O_DIRECTORY, O_LARGEFILE, O_NOATIME, O_NOFOLLOW};
100
101#[cfg(not(target_os = "linux"))]
102mod linux_compat {
103    use super::c_int;
104
105    pub const O_DIRECT: c_int = 0o40000;
106    pub const O_LARGEFILE: c_int = 0o100000;
107    pub const O_DIRECTORY: c_int = 0o200000;
108    pub const O_NOFOLLOW: c_int = 0o400000;
109    pub const O_NOATIME: c_int = 0o1000000;
110}
111
112#[cfg(not(target_os = "linux"))]
113pub use linux_compat::*;
114
115// File mode constants
116#[cfg(any(target_os = "linux", target_os = "macos"))]
117pub use libc::{S_IFDIR, S_IFLNK, S_IFMT, S_IFREG};
118
119#[cfg(any(target_arch = "wasm32", target_os = "windows"))]
120mod file_modes {
121    pub const S_IFMT: u32 = 0o170000;
122    pub const S_IFDIR: u32 = 0o040000;
123    pub const S_IFREG: u32 = 0o100000;
124    pub const S_IFLNK: u32 = 0o120000;
125}
126
127#[cfg(any(target_arch = "wasm32", target_os = "windows"))]
128pub use file_modes::*;
129
130// Stat structure
131#[cfg(target_os = "linux")]
132pub use libc::stat64;
133
134#[cfg(target_os = "macos")]
135pub use libc::stat as stat64;
136
137#[cfg(any(target_arch = "wasm32", target_os = "windows"))]
138#[repr(C)]
139pub struct stat64 {
140    pub st_dev: u64,
141    pub st_ino: u64,
142    pub st_mode: u32,
143    pub st_nlink: u32,
144    pub st_uid: u32,
145    pub st_gid: u32,
146    pub st_rdev: u64,
147    pub st_size: i64,
148    pub st_blksize: i64,
149    pub st_blocks: i64,
150    pub st_atime: i64,
151    pub st_atime_nsec: i64,
152    pub st_mtime: i64,
153    pub st_mtime_nsec: i64,
154    pub st_ctime: i64,
155    pub st_ctime_nsec: i64,
156}
157
158// ucred structure - credentials passed over Unix sockets
159// On Linux, ucred is already exported from libc at the top
160#[cfg(not(target_os = "linux"))]
161#[repr(C)]
162#[derive(Debug, Clone, Copy, PartialEq, Eq)]
163pub struct ucred {
164    /// Process ID of the sending process
165    pub pid: pid_t,
166    /// User ID of the sending process
167    pub uid: uid_t,
168    /// Group ID of the sending process
169    pub gid: gid_t,
170}