jetstream_libc 14.1.0

Platform-specific libc bindings for jetstream
Documentation
#![no_std]
#![doc(
    html_logo_url = "https://raw.githubusercontent.com/sevki/jetstream/main/logo/JetStream.png"
)]
#![doc(
    html_favicon_url = "https://raw.githubusercontent.com/sevki/jetstream/main/logo/JetStream.png"
)]
#![cfg_attr(docsrs, feature(doc_cfg))]
//! Platform-specific libc bindings for jetstream
//!
//! This crate provides a unified interface to libc constants across different platforms,
//! including wasm32 targets where some constants may not be available.

extern crate libc;

// Types - Linux uses libc, others use core::ffi
#[cfg(target_os = "linux")]
pub use libc::{c_int, gid_t, mode_t, pid_t, ucred, uid_t};

#[cfg(not(target_os = "linux"))]
pub use core::ffi::c_int;

#[cfg(not(target_os = "linux"))]
#[allow(non_camel_case_types)]
pub type mode_t = u32;

#[cfg(not(target_os = "linux"))]
#[allow(non_camel_case_types)]
pub type pid_t = i32;

#[cfg(not(target_os = "linux"))]
#[allow(non_camel_case_types)]
pub type uid_t = u32;

#[cfg(not(target_os = "linux"))]
#[allow(non_camel_case_types)]
pub type gid_t = u32;

// Error constants - Unix platforms use libc, others define them
#[cfg(any(target_os = "linux", target_os = "macos"))]
pub use libc::{
    EADDRINUSE, EADDRNOTAVAIL, ECONNABORTED, ECONNREFUSED, ECONNRESET, EEXIST,
    EINTR, EINVAL, EIO, ENOENT, ENOTCONN, EPERM, EPIPE, ETIMEDOUT, EWOULDBLOCK,
};

#[cfg(any(target_arch = "wasm32", target_os = "windows"))]
mod error_constants {
    use super::c_int;

    pub const EPERM: c_int = 1;
    pub const ENOENT: c_int = 2;
    pub const EIO: c_int = 5;
    pub const EEXIST: c_int = 17;
    pub const EINVAL: c_int = 22;
    pub const EPIPE: c_int = 32;
    pub const EWOULDBLOCK: c_int = 35;
    pub const EADDRINUSE: c_int = 98;
    pub const EADDRNOTAVAIL: c_int = 99;
    pub const ECONNABORTED: c_int = 103;
    pub const ECONNRESET: c_int = 104;
    pub const ENOTCONN: c_int = 107;
    pub const ETIMEDOUT: c_int = 110;
    pub const ECONNREFUSED: c_int = 111;
    pub const EINTR: c_int = 4;
}

#[cfg(any(target_arch = "wasm32", target_os = "windows"))]
pub use error_constants::*;

// Open flags - Unix platforms use libc, others define them
#[cfg(any(target_os = "linux", target_os = "macos"))]
pub use libc::{
    O_APPEND, O_CREAT, O_DSYNC, O_EXCL, O_NOCTTY, O_NONBLOCK, O_RDONLY, O_RDWR,
    O_SYNC, O_TRUNC, O_WRONLY,
};

#[cfg(any(target_arch = "wasm32", target_os = "windows"))]
mod open_flags {
    use super::c_int;

    pub const O_RDONLY: c_int = 0;
    pub const O_WRONLY: c_int = 1;
    pub const O_RDWR: c_int = 2;
    pub const O_CREAT: c_int = 0o100;
    pub const O_EXCL: c_int = 0o200;
    pub const O_NOCTTY: c_int = 0o400;
    pub const O_TRUNC: c_int = 0o1000;
    pub const O_APPEND: c_int = 0o2000;
    pub const O_NONBLOCK: c_int = 0o4000;
    pub const O_DSYNC: c_int = 0o10000;
    pub const O_SYNC: c_int = 0o4010000;
}

#[cfg(any(target_arch = "wasm32", target_os = "windows"))]
pub use open_flags::*;

// Linux-specific flags
#[cfg(target_os = "linux")]
pub use libc::{O_DIRECT, O_DIRECTORY, O_LARGEFILE, O_NOATIME, O_NOFOLLOW};

#[cfg(not(target_os = "linux"))]
mod linux_compat {
    use super::c_int;

    pub const O_DIRECT: c_int = 0o40000;
    pub const O_LARGEFILE: c_int = 0o100000;
    pub const O_DIRECTORY: c_int = 0o200000;
    pub const O_NOFOLLOW: c_int = 0o400000;
    pub const O_NOATIME: c_int = 0o1000000;
}

#[cfg(not(target_os = "linux"))]
pub use linux_compat::*;

// File mode constants
#[cfg(any(target_os = "linux", target_os = "macos"))]
pub use libc::{S_IFDIR, S_IFLNK, S_IFMT, S_IFREG};

#[cfg(any(target_arch = "wasm32", target_os = "windows"))]
mod file_modes {
    pub const S_IFMT: u32 = 0o170000;
    pub const S_IFDIR: u32 = 0o040000;
    pub const S_IFREG: u32 = 0o100000;
    pub const S_IFLNK: u32 = 0o120000;
}

#[cfg(any(target_arch = "wasm32", target_os = "windows"))]
pub use file_modes::*;

// Stat structure
#[cfg(target_os = "linux")]
pub use libc::stat64;

#[cfg(target_os = "macos")]
pub use libc::stat as stat64;

#[cfg(any(target_arch = "wasm32", target_os = "windows"))]
#[repr(C)]
pub struct stat64 {
    pub st_dev: u64,
    pub st_ino: u64,
    pub st_mode: u32,
    pub st_nlink: u32,
    pub st_uid: u32,
    pub st_gid: u32,
    pub st_rdev: u64,
    pub st_size: i64,
    pub st_blksize: i64,
    pub st_blocks: i64,
    pub st_atime: i64,
    pub st_atime_nsec: i64,
    pub st_mtime: i64,
    pub st_mtime_nsec: i64,
    pub st_ctime: i64,
    pub st_ctime_nsec: i64,
}

// ucred structure - credentials passed over Unix sockets
// On Linux, ucred is already exported from libc at the top
#[cfg(not(target_os = "linux"))]
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ucred {
    /// Process ID of the sending process
    pub pid: pid_t,
    /// User ID of the sending process
    pub uid: uid_t,
    /// Group ID of the sending process
    pub gid: gid_t,
}