Skip to content

Commit

Permalink
Merge #956
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
bors[bot] and jmmv committed Oct 19, 2018
2 parents cfbfa12 + 1f817c7 commit dc6b299
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Added `AF_UNSPEC` wrapper to `AddressFamily` ([#948](https://github.com/nix-rust/nix/pull/948))
- Added the `mode_t` public alias within `sys::stat`.
([#954](https://github.com/nix-rust/nix/pull/954))
- Added a `truncate` wrapper.
([#956](https://github.com/nix-rust/nix/pull/956))

### Changed
- Increased required Rust version to 1.22.1/
Expand Down
14 changes: 14 additions & 0 deletions src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,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
Expand Down
38 changes: 37 additions & 1 deletion test/test_unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use nix::sys::wait::*;
use nix::sys::stat::{self, Mode, SFlag};
use std::{env, iter};
use std::ffi::CString;
use std::fs::File;
use std::fs::{self, File};
use std::io::Write;
use std::os::unix::prelude::*;
use tempfile::{self, tempfile};
Expand Down Expand Up @@ -390,6 +390,42 @@ fn test_pipe2() {
assert!(f1.contains(FdFlag::FD_CLOEXEC));
}

#[test]
fn test_truncate() {
let tempdir = tempfile::tempdir().unwrap();
let path = tempdir.path().join("file");

{
let mut tmp = File::create(&path).unwrap();
const CONTENTS: &[u8] = b"12345678";
tmp.write_all(CONTENTS).unwrap();
}

truncate(&path, 4).unwrap();

let metadata = fs::metadata(&path).unwrap();
assert_eq!(4, metadata.len());
}

#[test]
fn test_ftruncate() {
let tempdir = tempfile::tempdir().unwrap();
let path = tempdir.path().join("file");

let tmpfd = {
let mut tmp = File::create(&path).unwrap();
const CONTENTS: &[u8] = b"12345678";
tmp.write_all(CONTENTS).unwrap();
tmp.into_raw_fd()
};

ftruncate(tmpfd, 2).unwrap();
close(tmpfd).unwrap();

let metadata = fs::metadata(&path).unwrap();
assert_eq!(2, metadata.len());
}

// Used in `test_alarm`.
static mut ALARM_CALLED: bool = false;

Expand Down

0 comments on commit dc6b299

Please sign in to comment.