Skip to content

Commit

Permalink
Implement fallocate and clock.
Browse files Browse the repository at this point in the history
  • Loading branch information
sunfishcode committed Oct 3, 2023
1 parent 34bd364 commit 6643c46
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
16 changes: 16 additions & 0 deletions c-scape/src/fs/fallocate.rs
Original file line number Diff line number Diff line change
@@ -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,
}
}
1 change: 1 addition & 0 deletions c-scape/src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod access;
mod chmod;
mod dir;
mod fadvise;
mod fallocate;
mod fcntl;
mod flock;
mod inotify;
Expand Down
14 changes: 14 additions & 0 deletions c-scape/src/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,17 @@ 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)
.try_into()
.unwrap_or(-1)
}

0 comments on commit 6643c46

Please sign in to comment.