Skip to content

Commit

Permalink
posix_fallocate
Browse files Browse the repository at this point in the history
  • Loading branch information
dingxiangfei2009 committed Aug 12, 2019
1 parent 414cc86 commit 50417ff
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/fcntl.rs
Expand Up @@ -504,3 +504,16 @@ mod posix_fadvise {
Errno::result(res)
}
}

#[cfg(any(
target_os = "linux",
target_os = "android",
target_os = "emscripten",
target_os = "fuchsia",
any(target_os = "wasi", target_env = "wasi"),
target_env = "freebsd"
))]
pub fn posix_fallocate(fd: RawFd, offset: libc::off_t, len: libc::off_t) -> Result<libc::c_int> {
let res = unsafe { libc::posix_fallocate(fd, offset, len) };
Errno::result(res)
}
43 changes: 43 additions & 0 deletions test/test_fcntl.rs
Expand Up @@ -232,3 +232,46 @@ mod test_posix_fadvise {
assert_eq!(errno, Errno::ESPIPE as i32);
}
}

#[cfg(any(target_os = "linux",
target_os = "android",
target_os = "emscripten",
target_os = "fuchsia",
any(target_os = "wasi", target_env = "wasi"),
target_env = "freebsd"))]
mod test_posix_fallocate {

use tempfile::NamedTempFile;
use std::{io::Read, os::unix::io::{RawFd, AsRawFd}};
use nix::errno::Errno;
use nix::fcntl::*;
use nix::unistd::pipe;

#[test]
fn test_success() {
const len: usize = 100;
let mut tmp = NamedTempFile::new().unwrap();
let fd = tmp.as_raw_fd();
let res = posix_fallocate(fd, 0, len as i64).unwrap();
let mut data = [1u8; len];
assert_eq!(tmp.read(&mut data).expect("read failure"), len);
assert_eq!(&data as &[u8], &[0u8; len] as &[u8]);
assert_eq!(res, 0);
}

#[test]
fn test_errno() {
let (rd, _wr) = pipe().unwrap();
let errno = posix_fallocate(rd as RawFd, 0, 100).unwrap();
match Errno::from_i32(errno) {
Errno::EBADF
| Errno::EFBIG
| Errno::EINTR
| Errno::EINVAL
| Errno::ENODEV
| Errno::ENOSPC
| Errno::ESPIPE => (),
_ => panic!("errno does not match posix_fallocate spec"),
}
}
}

0 comments on commit 50417ff

Please sign in to comment.