summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2018-10-19 22:22:25 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2018-10-19 22:22:25 +0000
commitdc6b2992f5e794cdc67a49a23b239241b0ebafd5 (patch)
tree84d9e7f1d5c499b346925be7a8b9feb284706bb9 /src
parentcfbfa1287dc80147b48205fbd2c18903781745e5 (diff)
parent1f817c7b8e97b828dce4648c2d8fa311666806f4 (diff)
downloadnix-dc6b2992f5e794cdc67a49a23b239241b0ebafd5.zip
Merge #956
956: Add a truncate(2) wrapper r=asomers a=jmmv This also adds a test for truncate (obviously) but, while doing so, also adds a test for the already-existing ftruncate. Co-authored-by: Julio Merino <julio@meroh.net>
Diffstat (limited to 'src')
-rw-r--r--src/unistd.rs14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/unistd.rs b/src/unistd.rs
index ad06a3c0..70882604 100644
--- a/src/unistd.rs
+++ b/src/unistd.rs
@@ -989,6 +989,20 @@ fn pipe2_setflags(fd1: RawFd, fd2: RawFd, flags: OFlag) -> Result<()> {
/// Truncate a file to a specified length
///
/// See also
+/// [truncate(2)](http://pubs.opengroup.org/onlinepubs/9699919799/functions/truncate.html)
+pub fn truncate<P: ?Sized + NixPath>(path: &P, len: off_t) -> Result<()> {
+ let res = try!(path.with_nix_path(|cstr| {
+ unsafe {
+ libc::truncate(cstr.as_ptr(), len)
+ }
+ }));
+
+ Errno::result(res).map(drop)
+}
+
+/// Truncate a file to a specified length
+///
+/// See also
/// [ftruncate(2)](http://pubs.opengroup.org/onlinepubs/9699919799/functions/ftruncate.html)
pub fn ftruncate(fd: RawFd, len: off_t) -> Result<()> {
Errno::result(unsafe { libc::ftruncate(fd, len) }).map(drop)