Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a truncate(2) wrapper #956

Merged
merged 1 commit into from Oct 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
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
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer map(|| ()) for stylistic reasons. drop, in this context, is basically being called for its side effect of returning nothing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not exactly sure what you'd like to see here, but given that this is a copy/paste from the many, many other functions that follow this style, I think keeping this for consistency is good.

}

/// Truncate a file to a specified length
///
/// See also
Expand Down
38 changes: 37 additions & 1 deletion test/test_unistd.rs
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