From 5cd0bed79d99cd12dc29b698d1237e5333c49aa3 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Tue, 3 Oct 2023 08:05:19 -0700 Subject: [PATCH] Implement `fallocate` and `clock`. --- c-scape/src/fs/fallocate.rs | 16 ++++++++++++++++ c-scape/src/fs/mod.rs | 1 + c-scape/src/time/mod.rs | 12 ++++++++++++ 3 files changed, 29 insertions(+) create mode 100644 c-scape/src/fs/fallocate.rs diff --git a/c-scape/src/fs/fallocate.rs b/c-scape/src/fs/fallocate.rs new file mode 100644 index 0000000..2c2d47d --- /dev/null +++ b/c-scape/src/fs/fallocate.rs @@ -0,0 +1,16 @@ +use crate::convert_res; +use libc::{c_int, off_t}; +use rustix::fd::BorrowedFd; +use rustix::fs::FallocateFlags; + +#[no_mangle] +unsafe extern "C" fn fallocate(fd: c_int, mode: c_int, offset: off_t, len: off_t) -> c_int { + libc!(libc::fallocate(fd, mode, offset, len)); + + let fd = BorrowedFd::borrow_raw(fd); + let mode = FallocateFlags::from_bits_retain(mode as _); + match convert_res(rustix::fs::fallocate(fd, mode, offset as _, len as _)) { + Some(()) => 0, + None => -1, + } +} diff --git a/c-scape/src/fs/mod.rs b/c-scape/src/fs/mod.rs index a690b3d..f8871b8 100644 --- a/c-scape/src/fs/mod.rs +++ b/c-scape/src/fs/mod.rs @@ -3,6 +3,7 @@ mod access; mod chmod; mod dir; mod fadvise; +mod fallocate; mod fcntl; mod flock; mod inotify; diff --git a/c-scape/src/time/mod.rs b/c-scape/src/time/mod.rs index 5d680be..afd6ae6 100644 --- a/c-scape/src/time/mod.rs +++ b/c-scape/src/time/mod.rs @@ -126,3 +126,15 @@ unsafe extern "C" fn difftime(time1: libc::time_t, time0: libc::time_t) -> f64 { (time1 as i128 - time0 as i128) as f64 } + +#[no_mangle] +unsafe extern "C" fn clock() -> libc::clock_t { + //libc!(libc::clock()); + + let time = rustix::time::clock_gettime(rustix::time::ClockId::ProcessCPUTime); + + time.tv_sec + .checked_mul(1_000_000) + .map(|usec| usec + time.tv_nsec / 1000) + .unwrap_or(-1) +}