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

feat: I/O safety for 'sys/statfs' #1919

Merged
merged 1 commit into from
Dec 6, 2022

Conversation

SteveLauC
Copy link
Member

@SteveLauC SteveLauC commented Dec 6, 2022

What this PR does:

  1. Adds I/O safety for module sys/statfs.

This PR is pretty small as all we need to do is to change the interface of fstatfs(2):

from:

pub fn fstatfs<T: AsRawFd>(fd: &T) -> Result<Statfs> 

to:

pub fn fstatfs<Fd: AsFd>(fd: &Fd) -> Result<Statfs>

Besides from the changes in module sys/statfs, there are two extra places where care needs to be taken:

$ cd nix

# Search for the usage of `fstatfs(2)` in `nix`
$ rg "fstatfs\("
test/test_fcntl.rs
386:        let statfs = nix::sys::statfs::fstatfs(&tmp).unwrap();
424:        let statfs = nix::sys::statfs::fstatfs(&tmp).unwrap();

CHANGELOG.md
849:- Now functions `statfs()` and `fstatfs()` return result with `Statfs` wrapper

src/sys/statfs.rs
769:        check_fstatfs("/tmp");
770:        check_fstatfs("/dev");
771:        check_fstatfs("/run");
772:        check_fstatfs("/");
775:    fn check_fstatfs(path: &str) {
781:        let fs = fstatfs(&file).unwrap();
830:        let fs = fstatfs(&file);

As you can see, fstatfs(2) is used in the tests in test/test_fcntl.rs:

// Test code that involves `fstatfs(2)`

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

 let fd = tmp.as_raw_fd();
 let statfs = nix::sys::statfs::fstatfs(&tmp).unwrap();

tmp is of type NamedTempFile, which does not implement AsFd in the current implementation of tempfile, but the implementation should be easy as it contains std::fs::File internally:

pub struct NamedTempFile {
    path: TempPath,
    file: File,
}

So I am thinking about making a PR to tempfile to make NamedTempFile AsFd, any thoughts on this?

@SteveLauC
Copy link
Member Author

Ohh, I am too stupid, we can actually call .as_file() on NamedTempFile to get &File to avoid this...

@SteveLauC SteveLauC mentioned this pull request Dec 6, 2022
29 tasks
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.

bors r+

@bors bors bot merged commit 64b5d33 into nix-rust:master Dec 6, 2022
@SteveLauC SteveLauC deleted the io-safety-sys/statfs branch December 7, 2022 00:20
bors bot added a commit that referenced this pull request Dec 9, 2022
1932: refactor: take `AsFd` by value r=asomers a=SteveLauC

#### What this PR does

1. Changes the `fd` type to take `AsFd` by value for the I/O safety PRs that are merged.
    * #1916 
    * #1919 
    * #1921 
    * #1922 

Co-authored-by: Steve Lau <stevelauc@outlook.com>
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.

2 participants