Skip to content

Commit b85d501

Browse files
Add support for 'fallocate'
1 parent 2166e71 commit b85d501

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
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+
}

0 commit comments

Comments
 (0)