Skip to content

Commit 5851bd0

Browse files
committed
Merge #768
768: Add support for 'fallocate' r=asomers a=SanchayanMaity For #596
2 parents 2166e71 + 724f7c7 commit 5851bd0

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
3232
([#663](https://github.com/nix-rust/nix/pull/663))
3333
- Added more accessor methods for `AioCb`
3434
([#773](https://github.com/nix-rust/nix/pull/773))
35+
- Add nix::sys::fallocate
36+
([#768](https:://github.com/nix-rust/nix/pull/768))
3537

3638
### Changed
3739
- Renamed existing `ptrace` wrappers to encourage namespacing ([#692](https://github.com/nix-rust/nix/pull/692))

src/fcntl.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,3 +341,43 @@ pub fn vmsplice(fd: RawFd, iov: &[IoVec<&[u8]>], flags: SpliceFFlags) -> Result<
341341
Errno::result(ret).map(|r| r as usize)
342342
}
343343

344+
#[cfg(any(target_os = "linux"))]
345+
libc_bitflags!(
346+
/// Mode argument flags for fallocate determining operation performed on a given range.
347+
pub struct FallocateFlags: libc::c_int {
348+
/// File size is not changed.
349+
///
350+
/// offset + len can be greater than file size.
351+
FALLOC_FL_KEEP_SIZE;
352+
/// Deallocates space by creating a hole.
353+
///
354+
/// Must be ORed with FALLOC_FL_KEEP_SIZE. Byte range starts at offset and continues for len bytes.
355+
FALLOC_FL_PUNCH_HOLE;
356+
/// Removes byte range from a file without leaving a hole.
357+
///
358+
/// Byte range to collapse starts at offset and continues for len bytes.
359+
FALLOC_FL_COLLAPSE_RANGE;
360+
/// Zeroes space in specified byte range.
361+
///
362+
/// Byte range starts at offset and continues for len bytes.
363+
FALLOC_FL_ZERO_RANGE;
364+
/// Increases file space by inserting a hole within the file size.
365+
///
366+
/// Does not overwrite existing data. Hole starts at offset and continues for len bytes.
367+
FALLOC_FL_INSERT_RANGE;
368+
/// Shared file data extants are made private to the file.
369+
///
370+
/// Gaurantees that a subsequent write will not fail due to lack of space.
371+
FALLOC_FL_UNSHARE_RANGE;
372+
}
373+
);
374+
375+
/// Manipulates file space.
376+
///
377+
/// Allows the caller to directly manipulate the allocated disk space for the
378+
/// file referred to by fd.
379+
#[cfg(any(target_os = "linux"))]
380+
pub fn fallocate(fd: RawFd, mode: FallocateFlags, offset: libc::off_t, len: libc::off_t) -> Result<c_int> {
381+
let res = unsafe { libc::fallocate(fd, mode.bits(), offset, len) };
382+
Errno::result(res)
383+
}

test/test_fcntl.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ mod linux_android {
5454

5555
use libc::loff_t;
5656

57-
use nix::fcntl::{SpliceFFlags, splice, tee, vmsplice};
57+
use nix::fcntl::{SpliceFFlags, FallocateFlags, fallocate, splice, tee, vmsplice};
5858
use nix::sys::uio::IoVec;
5959
use nix::unistd::{close, pipe, read, write};
6060

61-
use tempfile::tempfile;
61+
use tempfile::{tempfile, NamedTempFile};
6262

6363
#[test]
6464
fn test_splice() {
@@ -131,4 +131,15 @@ mod linux_android {
131131
close(wr).unwrap();
132132
}
133133

134+
#[test]
135+
fn test_fallocate() {
136+
let tmp = NamedTempFile::new().unwrap();
137+
138+
let fd = tmp.as_raw_fd();
139+
fallocate(fd, FallocateFlags::empty(), 0, 100).unwrap();
140+
141+
// Check if we read exactly 100 bytes
142+
let mut buf = [0u8; 200];
143+
assert_eq!(100, read(fd, &mut buf).unwrap());
144+
}
134145
}

0 commit comments

Comments
 (0)