Skip to content

Painful [i8] in rustix::fs::listxattr() #1218

@allisonkarlitskaya

Description

@allisonkarlitskaya

listxattr() is defined:

pub fn listxattr<P>(path: P, list: &mut [c::c_char]) -> io::Result<usize>
where
    P: path::Arg,

which seems reasonable enough. It's c_char under there, of course. Unfortunately, at least on my platform (x86_64) this type is equivalent to &mut [i8] which is a bit more of an oddity.

The issue here is that there are an awful lot of APIs for converting between string types in Rust, and pretty much none of them consider [i8] as a string representation.

In particular: the name field of rustix::fs::getxattr() wants a &CStr. I'm not sure of a good way to get from one to the other.

The core slice type has split_inclusive() which is a very nice way to iterate the returned values. You get the string plus the nul terminator, which you can then pass to CStr:from_bytes_with_nul.... if it were a [u8] slice.

But since it's [i8] you end up having to do an unsafe transmute or an extremely explicit character-by-character conversion.

Of course, because of the embedded nuls, this API can't return CString. But how about some variant of [u8] instead?

Metadata

Metadata

Assignees

No one assigned

    Labels

    semver bumpIssues that will require a semver-incompatible fix

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions