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 support for 'posix_fallocate' #779

Closed
wants to merge 1 commit into from
Closed

Add support for 'posix_fallocate' #779

wants to merge 1 commit into from

Conversation

SanchayanMaity
Copy link
Contributor

No description provided.

src/fcntl.rs Outdated
///
/// Ensure disk space is allocated for the file referred to by the file descriptor
/// fd for the bytes in the range starting at offset and continuing for len bytes.
#[cfg(any(target_os = "linux"))]
Copy link
Member

Choose a reason for hiding this comment

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

This #[cfg()] statement is too narrow. In addition to Linux, libc already defines posix_fallocate for FreeBSD, Dragonfly, and Android. NetBSD has it too, though libc lacks the definition for NetBSD. Please broaden the definition. You will also need a matching #[cfg()] statement on the tests, or else nix's tests won't build on platforms that don't define this function.

@Susurrus
Copy link
Contributor

@SanchayanMaity You still planning on working on this?

@SanchayanMaity
Copy link
Contributor Author

@Susurrus Yes, I plan to work on this. Got busy with a few things last week. Will work on it today.

@SanchayanMaity
Copy link
Contributor Author

How to fix the build for darwin?

@asomers
Copy link
Member

asomers commented Nov 5, 2017

The build failed on Darwin because libc doesn't define posix_fallocate for that platform. Either the definition is missing from libc or Darwin really doesn't have posix_fallocate. You need to figure out which. In the former case, send a PR to libc. In the latter, you just need to amend your #[cfg()] guard in this PR.

@Susurrus
Copy link
Contributor

Susurrus commented Nov 5, 2017

@asomers @SanchayanMaity I just checked on my mac running 10.11.6 and there is no definition of posix_fallocate, so macos and ios targets should definitely be skipped.

Also, this needs a rebase so that it'll run on all the current targets nix supports.

src/fcntl.rs Outdated
///
/// Ensure disk space is allocated for the file referred to by the file descriptor
/// fd for the bytes in the range starting at offset and continuing for len bytes.
#[cfg(any(target_os = "linux", target_os = "macos", target_os = "android", target_os = "freebsd", target_os = "dragonfly", target_os = "openbsd"))]
Copy link
Member

Choose a reason for hiding this comment

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

Please wrap long lines.

src/fcntl.rs Outdated
/// Allocate file space.
///
/// Ensure disk space is allocated for the file referred to by the file descriptor
/// fd for the bytes in the range starting at offset and continuing for len bytes.
Copy link
Member

Choose a reason for hiding this comment

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

let tmp = NamedTempFile::new().unwrap();

let fd = tmp.as_raw_fd();
posix_fallocate(fd, 0, 100).unwrap();
Copy link
Member

Choose a reason for hiding this comment

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

This test will fail if /tmp's filesystem doesn't support posix_fallocate. ZFS, for example, does not. POSIX stupidly requires that posix_fallocate return EINVAL in that case. So you should skip the rest of the test if you get that errno.

@Susurrus
Copy link
Contributor

Susurrus commented Dec 9, 2017

@SanchayanMaity Any time to wrap up this PR? It's pretty close to being able to be merged.

@SanchayanMaity
Copy link
Contributor Author

@Susurrus Will pick this up again. Sorry for the delay. Was in between jobs.

src/fcntl.rs Outdated
/// Allocate file space.
///
/// Ensure disk space is allocated for the file referred to by file
/// descriptor fd for the bytes in the range starting at offset and
Copy link
Member

Choose a reason for hiding this comment

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

parameter names like fd and offset should be in backticks.

src/fcntl.rs Outdated
/// Ensure disk space is allocated for the file referred to by file
/// descriptor fd for the bytes in the range starting at offset and
/// continuing for len bytes.
/// http://pubs.opengroup.org/onlinepubs/9699919799/
Copy link
Member

Choose a reason for hiding this comment

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

The link is wrong. I think you probably linked to the wrong iframe. Also, for consistency with other Nix functions, you should format the link like this:

/// # References
///
/// [`posix_fallocate`](http://whatever)

src/fcntl.rs Outdated
/// descriptor fd for the bytes in the range starting at offset and
/// continuing for len bytes.
/// http://pubs.opengroup.org/onlinepubs/9699919799/
#[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "dragonfly", target_os = "openbsd"))]
Copy link
Member

Choose a reason for hiding this comment

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

You still need to wrap long lines here and elsewhere.

Copy link
Contributor

Choose a reason for hiding this comment

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

This list should also be alphabetized.

src/fcntl.rs Outdated
/// descriptor fd for the bytes in the range starting at offset and
/// continuing for len bytes.
/// http://pubs.opengroup.org/onlinepubs/9699919799/
#[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "dragonfly", target_os = "openbsd"))]
Copy link
Contributor

Choose a reason for hiding this comment

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

This list should also be alphabetized.

let ret = posix_fallocate(fd, 0, 100).unwrap();

match Errno::result(ret) {
Err(Error::Sys(Errno::EINVAL)) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Clippy will get mad about this, a match where only 1 arm is checked. Please rewrite this using if ret.is_some(), but keep the comment there so it's clear what's going on.

Long-term it'd be better to actually check somehow before the test is run if this will work and skip the test otherwise. Adding a TODO here would be helpful for that.

@@ -143,3 +146,27 @@ mod linux_android {
assert_eq!(100, read(fd, &mut buf).unwrap());
}
}

#[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "dragonfly", target_os = "openbsd"))]
Copy link
Contributor

Choose a reason for hiding this comment

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

Please alphabetize this and line wrap it also.

@@ -1,10 +1,13 @@
use nix::errno::{Errno};
Copy link
Contributor

Choose a reason for hiding this comment

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

Please remove unnecessary braces here and elsewhere.

@Susurrus
Copy link
Contributor

@SanchayanMaity Do you have time to come back and finish up this PR in the near future? It's been a while since you last updated it.

@Susurrus
Copy link
Contributor

You'll need to correct your CHANGELOG entries such that they end up in the UNRELEASED section.

Copy link
Member

@asomers asomers left a comment

Choose a reason for hiding this comment

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

All LGTM now. I'll wait to see if @Susurrus has any comments before I merge it.

Copy link
Contributor

@Susurrus Susurrus left a comment

Choose a reason for hiding this comment

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

A few minor changes then this should be good.

src/fcntl.rs Outdated

/// Allocate file space.
///
/// Ensure disk space is allocated for the file referred to by file
Copy link
Contributor

Choose a reason for hiding this comment

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

"to by the file"

CHANGELOG.md Outdated
@@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
- Add nix::sys::posix_fallocate
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be under the "Added" section and rephrased like "Added nix::sys::posix_fallocate". Please also list the platforms that it was added for since it wasn't all platforms nix supports.

src/fcntl.rs Outdated
/// # References
///
/// [`posix_fallocate`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_fallocate.html)
#[cfg(any(target_os = "android", target_os = "dragonfly", target_os = "freebsd",
Copy link
Contributor

Choose a reason for hiding this comment

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

posix_fallocate is not defined in libc for DragonFlyBSD and OpenBSD and I couldn't find it in the public sources for those OSes either. I think this and the tests should be removed for those platforms.

src/fcntl.rs Outdated
/// [`posix_fallocate`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_fallocate.html)
#[cfg(any(target_os = "android", target_os = "dragonfly", target_os = "freebsd",
target_os = "linux", target_os = "openbsd"))]
pub fn posix_fallocate(fd: RawFd, offset: libc::off_t,
Copy link
Contributor

Choose a reason for hiding this comment

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

Please annotate this as #[inline] since it's a very small wrapper around the bare libc call.

@Susurrus
Copy link
Contributor

@SanchayanMaity Want to take care of these comments so we can get this merged?

@SanchayanMaity
Copy link
Contributor Author

@Susurrus Yes, of course. Been busy lately. I will send out a patch tomorrow addressing your feedback.

@Susurrus
Copy link
Contributor

Please change the CHANGELOG entry to be under the current UNRELEASED section, as this didn't make it into the 0.10 release.

Then it LGTM. @asomers if you want another look-over, now's the time as I'll merge it as soon as this gets updated.

@Susurrus
Copy link
Contributor

Please move your changelog entry under the "Added" section within the "[UNRELEASED]" section and squash these commits into 1. Then we'll merge!

let fd = tmp.as_raw_fd();
let ret = posix_fallocate(fd, 0, 100);
if ret.is_ok() {
// The test will fail if /tmp's filesystem doesn't support
Copy link
Member

Choose a reason for hiding this comment

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

You shouldn't ignore all errors, just EINVAL.

@asomers
Copy link
Member

asomers commented Oct 3, 2020

Superseded by #1105

@asomers asomers closed this Oct 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants