Skip to content

Add constructor of std::fs::Metadata from struct statx #761

@Darksonn

Description

@Darksonn

Proposal

Problem statement

The only way to construct std::fs::Metadata is to use the std::fs::metadata() method, and possibly other similar stdlib methods. It's impossible to invoke the statx syscall manually and create a stdlib std::fs::Metadata struct from the result.

Motivating examples or use cases

Tokio currently implements tokio::fs::metadata() by calling std::fs::metadata() from a thread-pool. However, we would like to provide an optional implementation that looks up the metadata via io_uring instead. Unfortunately, Tokio has made the design mistake of using std::fs::Metadata as the return value of tokio::fs::metadata(), instead of using our own custom Metadata struct.

However, since there is no way to convert the output of the io_uring statx operation into a std::fs::Metadata value, it's currently impossible for Tokio to provide an io-uring backed tokio::fs::metadata() method without a breaking change.

Solution sketch

Add a method to the Linux MetadataExt trait that takes a *const c_void to a struct populated by statx syscall. This avoids defining the statx struct in the standard library.

There are many variations of this. I want to avoid spending a lot of time on the precise API design until we've discussed whether the stdlib is willing in principle to add such a constructor, assuming a good design can be found.

I acknowledge that you may not want to make stdlib changes to fix what is essentially just a design mistake in a third-party library.

Downsides

If a statx2 syscall or similar is added in the future with additional fields, then Metadata cannot be evolved to unconditionally provide those fields (even on Linux only via MetadataExt), as it may be constructed from a struct statx where that information is not present. On the other hand, we probably need to do that anyway since older Linux versions will not have this new syscall available, so stdlib will have to use the syscall of today on some Linux systems.

Alternatives

Live with the design mistake in Tokio forever and never implement tokio::fs::metadata() in terms of io_uring.

Links and related work

tokio-rs/tokio#7616

What happens now?

This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

  • We think this problem seems worth solving, and the standard library might be the right place to solve it.
  • We think that this probably doesn't belong in the standard library.

Second, if there's a concrete solution:

  • We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
  • We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ACP-acceptedAPI Change Proposal is accepted (seconded with no objections)T-libs-apiapi-change-proposalA proposal to add or alter unstable APIs in the standard libraries

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions