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 statx #1660

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft

Add statx #1660

wants to merge 7 commits into from

Conversation

vi
Copy link
Contributor

@vi vi commented Feb 9, 2022

Resolves #1649.

In current state this breaks the build for Musl targets.

src/fcntl.rs Show resolved Hide resolved
);
*/

/// Attempt to retrieve stats of `pathname`. If `pathname` is relative, `dirfd` is used as starting directory for lookups. If `dirfs` is None, current directory is used.
Copy link
Member

Choose a reason for hiding this comment

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

Wrap to 80 cols, please.

src/fcntl/statx.rs Outdated Show resolved Hide resolved

impl Statx {
/// Retrieve file type, if it has been returned by kernel
pub fn r#type(&self) -> Option<SFlag> {
Copy link
Member

Choose a reason for hiding this comment

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

This method name will be hard for users to type. I suggest filetype.

src/fcntl/statx.rs Outdated Show resolved Hide resolved
src/fcntl/statx.rs Outdated Show resolved Hide resolved
}

/// Retrieve file access time, if it has been returned by kernel
pub fn atime(&self) -> Option<libc::statx_timestamp> {
Copy link
Member

Choose a reason for hiding this comment

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

Returning the raw libc type isn't very useful. Is it identical to a struct timespec? If so, I suggest you convert it into a nix::sys::time::TimeSpec.

Copy link
Contributor Author

@vi vi Mar 7, 2022

Choose a reason for hiding this comment

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

Done, also added the appropriate From TimeSpec implementation within the statx module. Or should it be moved to the place where other TimeSpec From implementations live with a duplicated #[cfg] line?

}

/// Retrieve file size as a number of blocks, if it has been returned by kernel
pub fn blksize(&self) -> Option<u64> {
Copy link
Member

Choose a reason for hiding this comment

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

Is this identical to blocks?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, probably I just forgot to fill in new doccomment and implementation after copying neighbouring method.

Copy link
Member

Choose a reason for hiding this comment

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

What is it supposed to do then? Because it's just returning the same stx_blocks variable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed to return blocksize, fixed the doccomment.

src/fcntl/statx.rs Show resolved Hide resolved
Co-authored-by: Alan Somers <asomers@gmail.com>
src/fcntl/statx.rs Outdated Show resolved Hide resolved
}

/// Retrieve file size as a number of blocks, if it has been returned by kernel
pub fn blksize(&self) -> Option<u64> {
Copy link
Member

Choose a reason for hiding this comment

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

What is it supposed to do then? Because it's just returning the same stx_blocks variable.

src/fcntl/statx.rs Show resolved Hide resolved
);

/// Attempt to retrieve stats of `pathname`. If `pathname` is relative, `dirfd`
/// is used as starting directory for lookups. If `dirfs` is None, current
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// is used as starting directory for lookups. If `dirfs` is None, current
/// is used as starting directory for lookups. If `dirfd` is None, current

/// is used as starting directory for lookups. If `dirfs` is None, current
/// directory is used.
/// `pathname` may be empty string. But instead of specifying empty string
/// literal, you are adviced to use zero-terminated `CStr` to avoid extra allocation
Copy link
Member

Choose a reason for hiding this comment

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

How about adding some examples to show usage? That would be especially helpful for the "empty pathname" case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea. Shall doc examples be marked as testable code (and assert something about typical external files e.g. /dev/null)?

Comment on lines 107 to 109
pub struct Statx {
pub inner: libc::statx,
}
Copy link
Member

Choose a reason for hiding this comment

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

Since it has only one member, it can be a NewType.

Suggested change
pub struct Statx {
pub inner: libc::statx,
}
#[repr(transparent)]
pub struct Statx(libc::statx);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done (inner tuple field is still pub, unlike in the suggested change).

}
}

/*
Copy link
Member

Choose a reason for hiding this comment

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

Why are these two methods commented out?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because of current version of libc crate does not expose e.g. STATX_ATTR_VERITY. See my question at #1649 (comment).
Android target's libc lacks even more consts in libc.

Copy link
Member

Choose a reason for hiding this comment

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

I submitted a PR to add the missing constants on GNU/Linux

@SteveLauC
Copy link
Member

SteveLauC commented Oct 4, 2022

Hey @vi :) What is the status of this PR? I really hope this can be merged

I just tried it myself, sadly, there is a lot of missing stuff in libc.

@@ -889,3 +889,6 @@ pub fn posix_fallocate(fd: RawFd, offset: libc::off_t, len: libc::off_t) -> Resu
}
}
}

#[cfg(all(feature="fs", any(target_os = "android", target_os = "linux")))]
Copy link
Member

@SteveLauC SteveLauC Oct 5, 2022

Choose a reason for hiding this comment

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

#[cfg(all(
    any(
        target_os = "android",
        all(target_os = "linux", not(target_env = "musl")),
    ),
    feature = "fs"
))]

Since this is not available on musl, this configuration should work


let mut dst = mem::MaybeUninit::uninit();
let res = pathname.with_nix_path(|cstr| unsafe {
libc::statx(
Copy link
Member

Choose a reason for hiding this comment

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

This wrapper function is only exposed on GNU/Linux as glibc added it.

What about making this statx module temporarily exclusive to GNU/Linux? When the missing stuff is added, we can easily add support for other platforms by changing a few lines of code.

@vi
Copy link
Contributor Author

vi commented Oct 5, 2022

What is the status of this PR?

It is waiting for answers for questions:

  • What to do with the missing constants? Based on your pull request to libc, supposed answer is "wait for libc support", then "wait when nix starts depending on new libc".
  • Shall we get ahead of musl upstream and provide statx even on musl by using hacks?
  • Is overall design of the feature is OK and in line with nix? If code review comments e.g. about statx_without_path are resolved, and points above are resolved, will it be OK for merge or there will be just another round of problems?

@SteveLauC
Copy link
Member

Shall we get ahead of musl upstream and provide statx even on musl by using hacks?

IMHO, this is not a good idea as musl hasn't provided a wrapper in their library. As I said in this comment, I would like to temporarily just support GNU/Linux.

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.

Feature request: wrap Linux's statx
3 participants