Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for 'posix_fallocate' #779

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -19,6 +19,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
([#825](https://github.com/nix-rust/nix/pull/825))
- Exposed `MSG_CMSG_CLOEXEC` on *BSD.
([#825](https://github.com/nix-rust/nix/pull/825))
- Added `nix::sys::posix_fallocate` for Android, FreeBSD and Linux.
([#779](https:://github.com/nix-rust/nix/pull/779))

### Changed
- Display and Debug for SysControlAddr now includes all fields.
Expand Down
17 changes: 17 additions & 0 deletions src/fcntl.rs
Expand Up @@ -383,3 +383,20 @@ pub fn fallocate(fd: RawFd, mode: FallocateFlags, offset: libc::off_t, len: libc
let res = unsafe { libc::fallocate(fd, mode.bits(), offset, len) };
Errno::result(res)
}

/// Allocate file space.
///
/// Ensure disk space is allocated for the file referred to by the file
/// descriptor `fd` for the bytes in the range starting at `offset` and
/// continuing for `len` bytes.
///
/// # References
///
/// [`posix_fallocate`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_fallocate.html)
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux"))]
#[inline]
pub fn posix_fallocate(fd: RawFd, offset: libc::off_t,
len: libc::off_t) -> Result<c_int> {
let res = unsafe { libc::posix_fallocate(fd, offset, len) };
Errno::result(res)
}
21 changes: 21 additions & 0 deletions test/test_fcntl.rs
Expand Up @@ -5,6 +5,7 @@ use tempdir::TempDir;
use tempfile::NamedTempFile;
use std::io::prelude::*;
use std::os::unix::fs;
use std::os::unix::io::AsRawFd;

#[test]
fn test_openat() {
Expand Down Expand Up @@ -143,3 +144,23 @@ mod linux_android {
assert_eq!(100, read(fd, &mut buf).unwrap());
}
}

#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux"))]
#[test]
fn test_posix_fallocate() {
use nix::fcntl::posix_fallocate;
let tmp = NamedTempFile::new().unwrap();

let fd = tmp.as_raw_fd();
let ret = posix_fallocate(fd, 0, 100);
if ret.is_ok() {
// The test will fail if /tmp's filesystem doesn't support
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't ignore all errors, just EINVAL.

// posix_fallocate. ZFS for example does not. POSIX requires
// that posix_fallocate return EINVAL in that case. Skip the
// test in this scenario.
//
// Check if we read exactly 100 bytes
let mut buf = [0u8; 200];
assert_eq!(100, read(fd, &mut buf).unwrap());
}
}