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

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

Open
allisonkarlitskaya opened this issue Nov 18, 2024 · 7 comments
Open

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

allisonkarlitskaya opened this issue Nov 18, 2024 · 7 comments

Comments

@allisonkarlitskaya
Copy link

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?

@allisonkarlitskaya
Copy link
Author

Another wish item if we're going to break/add API here: readlink() in rustix has a very nice auto-allocating wrapper which automatically returns the correct-sized result. It would be lovely to see something like that here.

@allisonkarlitskaya
Copy link
Author

getxattr() could also use the "automatically return correct-sized object" treatment as well.

@allisonkarlitskaya
Copy link
Author

Fixed in 0.38.41, thanks!

@Tuupertunut
Copy link

How was it fixed? I can't find any mention of it being fixed and for me it's still taking in &mut [i8].

@allisonkarlitskaya
Copy link
Author

I think it wasn't actually fixed indeed. It started working at some point for me, but I think something else must have changed because a colleague was still running into the problem.

@allisonkarlitskaya
Copy link
Author

@Tuupertunut
Copy link

#945 is relevant. It seems that the definition of c_char changes based on what dependencies you have and if they are turning the "std" feature of linux-raw-sys on or off.

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

No branches or pull requests

2 participants