Skip to content

Commit 0617c24

Browse files
posix_fallocate
1 parent 414cc86 commit 0617c24

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

src/fcntl.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,3 +504,16 @@ mod posix_fadvise {
504504
Errno::result(res)
505505
}
506506
}
507+
508+
#[cfg(any(
509+
target_os = "linux",
510+
target_os = "android",
511+
target_os = "emscripten",
512+
target_os = "fuchsia",
513+
any(target_os = "wasi", target_env = "wasi"),
514+
target_env = "freebsd"
515+
))]
516+
pub fn posix_fallocate(fd: RawFd, offset: libc::off_t, len: libc::off_t) -> Result<libc::c_int> {
517+
let res = unsafe { libc::posix_fallocate(fd, offset, len) };
518+
Errno::result(res)
519+
}

test/test_fcntl.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,46 @@ mod test_posix_fadvise {
232232
assert_eq!(errno, Errno::ESPIPE as i32);
233233
}
234234
}
235+
236+
#[cfg(any(target_os = "linux",
237+
target_os = "android",
238+
target_os = "emscripten",
239+
target_os = "fuchsia",
240+
any(target_os = "wasi", target_env = "wasi"),
241+
target_env = "freebsd"))]
242+
mod test_posix_fallocate {
243+
244+
use tempfile::NamedTempFile;
245+
use std::{io::Read, os::unix::io::{RawFd, AsRawFd}};
246+
use nix::errno::Errno;
247+
use nix::fcntl::*;
248+
use nix::unistd::pipe;
249+
250+
#[test]
251+
fn test_success() {
252+
const LEN: usize = 100;
253+
let mut tmp = NamedTempFile::new().unwrap();
254+
let fd = tmp.as_raw_fd();
255+
let res = posix_fallocate(fd, 0, LEN as libc::off_t).unwrap();
256+
let mut data = [1u8; LEN];
257+
assert_eq!(tmp.read(&mut data).expect("read failure"), LEN);
258+
assert_eq!(&data as &[u8], &[0u8; LEN] as &[u8]);
259+
assert_eq!(res, 0);
260+
}
261+
262+
#[test]
263+
fn test_errno() {
264+
let (rd, _wr) = pipe().unwrap();
265+
let errno = posix_fallocate(rd as RawFd, 0, 100).unwrap();
266+
match Errno::from_i32(errno) {
267+
Errno::EBADF
268+
| Errno::EFBIG
269+
| Errno::EINTR
270+
| Errno::EINVAL
271+
| Errno::ENODEV
272+
| Errno::ENOSPC
273+
| Errno::ESPIPE => (),
274+
_ => panic!("errno does not match posix_fallocate spec"),
275+
}
276+
}
277+
}

0 commit comments

Comments
 (0)