From b85d50147f1f503597d8f54e6be821dfd2379193 Mon Sep 17 00:00:00 2001 From: Sanchayan Maity Date: Sat, 7 Oct 2017 19:13:02 +0530 Subject: Add support for 'fallocate' --- CHANGELOG.md | 2 ++ src/fcntl.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index beacb282..664fdaff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). ([#663](https://github.com/nix-rust/nix/pull/663)) - Added more accessor methods for `AioCb` ([#773](https://github.com/nix-rust/nix/pull/773)) +- Add nix::sys::fallocate + ([#768](https:://github.com/nix-rust/nix/pull/768)) ### Changed - Renamed existing `ptrace` wrappers to encourage namespacing ([#692](https://github.com/nix-rust/nix/pull/692)) diff --git a/src/fcntl.rs b/src/fcntl.rs index 003c316c..f99036d6 100644 --- a/src/fcntl.rs +++ b/src/fcntl.rs @@ -341,3 +341,43 @@ pub fn vmsplice(fd: RawFd, iov: &[IoVec<&[u8]>], flags: SpliceFFlags) -> Result< Errno::result(ret).map(|r| r as usize) } +#[cfg(any(target_os = "linux"))] +libc_bitflags!( + /// Mode argument flags for fallocate determining operation performed on a given range. + pub struct FallocateFlags: libc::c_int { + /// File size is not changed. + /// + /// offset + len can be greater than file size. + FALLOC_FL_KEEP_SIZE; + /// Deallocates space by creating a hole. + /// + /// Must be ORed with FALLOC_FL_KEEP_SIZE. Byte range starts at offset and continues for len bytes. + FALLOC_FL_PUNCH_HOLE; + /// Removes byte range from a file without leaving a hole. + /// + /// Byte range to collapse starts at offset and continues for len bytes. + FALLOC_FL_COLLAPSE_RANGE; + /// Zeroes space in specified byte range. + /// + /// Byte range starts at offset and continues for len bytes. + FALLOC_FL_ZERO_RANGE; + /// Increases file space by inserting a hole within the file size. + /// + /// Does not overwrite existing data. Hole starts at offset and continues for len bytes. + FALLOC_FL_INSERT_RANGE; + /// Shared file data extants are made private to the file. + /// + /// Gaurantees that a subsequent write will not fail due to lack of space. + FALLOC_FL_UNSHARE_RANGE; + } +); + +/// Manipulates file space. +/// +/// Allows the caller to directly manipulate the allocated disk space for the +/// file referred to by fd. +#[cfg(any(target_os = "linux"))] +pub fn fallocate(fd: RawFd, mode: FallocateFlags, offset: libc::off_t, len: libc::off_t) -> Result { + let res = unsafe { libc::fallocate(fd, mode.bits(), offset, len) }; + Errno::result(res) +} -- cgit v1.2.3 From 724f7c734b5484116aefb9f055d6303204c7be8b Mon Sep 17 00:00:00 2001 From: Sanchayan Maity Date: Sat, 7 Oct 2017 19:13:22 +0530 Subject: Add test for 'fallocate' --- test/test_fcntl.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/test/test_fcntl.rs b/test/test_fcntl.rs index 43bfc091..d171b91d 100644 --- a/test/test_fcntl.rs +++ b/test/test_fcntl.rs @@ -54,11 +54,11 @@ mod linux_android { use libc::loff_t; - use nix::fcntl::{SpliceFFlags, splice, tee, vmsplice}; + use nix::fcntl::{SpliceFFlags, FallocateFlags, fallocate, splice, tee, vmsplice}; use nix::sys::uio::IoVec; use nix::unistd::{close, pipe, read, write}; - use tempfile::tempfile; + use tempfile::{tempfile, NamedTempFile}; #[test] fn test_splice() { @@ -131,4 +131,15 @@ mod linux_android { close(wr).unwrap(); } + #[test] + fn test_fallocate() { + let tmp = NamedTempFile::new().unwrap(); + + let fd = tmp.as_raw_fd(); + fallocate(fd, FallocateFlags::empty(), 0, 100).unwrap(); + + // Check if we read exactly 100 bytes + let mut buf = [0u8; 200]; + assert_eq!(100, read(fd, &mut buf).unwrap()); + } } -- cgit v1.2.3