4 ms·
> Why am I using my own ffi version of chown instead of the libc crate? The libc crate prototypes chown with unsigned uid_t and gid_t types, and I want to pass
by thaumasiotes 6y ago
> Why am I using my own ffi version of chown instead of the libc crate? The libc crate prototypes chown with unsigned uid_t and gid_t types, and I want to pass -1 because I'm not interested in changing the group. I'll spare you the long rant about how one should never redeclare system interfaces if you can't take the time to do so properly, because it turns out if you dig into it, uid_t boils down to uint32_t, but even so, the manual for chown says -1 should work and I want it to work.
First thing I'd try would be to pass "~0" instead of "-1". Would that work? (Looks like in Rust that would be "!0". I don't know anything about Rust.)
- adwn 6y agoYes, !0 would work. Or, to make the intent even more clear: -1_i32 as u32 Definitely better than declaring your own function prototype.
- ChrisSD 6y agoIn Rust the simplest way would be to use the `u32::MAX` constant. Create a fun alias for it if you want to express intent.
- thaumasiotes 6y agoI actually find ~0 more intuitive. u32::MAX makes me think the relevant concept is "a really big number"; ~0 makes me think the relevant concept is "a bunch of bits that are all 1s".
- ChrisSD 6y agoFair enough. But tbh I'm not sure it really matters in this context. It's just a constant that means "ignore this parameter". Whether it's "a really big number" or "a bunch of bits that are all 1s" is incidental. Btw, you can directly express "all ones" in Rust as: 0b_1111_1111_1111_1111_1111_1111_1111_1111 Though it's somewhat less succinct. ;) EDIT: Added more ones, thanks adwn!
- boardwaalk 6y agoIf your reaction to Rust actually following the prototype of chmod but not accepting conversions signed/unsigned silently is to redefine chmod so you can do it like you’d do in C… that seems a little bullheaded to me. Wrapping a C function is dangerous in a literal and real sense, doing “-1 as u32” is not at all.
- thaumasiotes 6y ago> If your reaction to Rust actually following the prototype of chmod but not accepting conversions Eh... as the article points out, the chown documentation specifically says that you are expected to provide negative numbers. The spec is obviously bugged. This limits the amount of righteousness you can claim over "but we followed the spec (that we knew was wrong)!"
- NovemberWhiskey 6y agoIf you read the POSIX specification for chown you'll see that the "leave unchanged" sentinel value is, for UIDs, defined to be (uid_t) -1. I can't be bothered to try it myself but I'm fairly sure the author could just have written (-1i32) as uid_t and gotten some of their life back.
- cryptonector 6y agoYes. Specifically, POSIX does not specify whether id_t, uid_t, gid_t, or pid_t are signed or unsigned, so whenever it refers to the "default"/"leave alone"/"unspecified" value, it always does it as a cast of -1.
- cryptonector 6y agoAlthough `~0` assumes twos-complement signed numbers. Which, well, is probably a safe assumption, but still.
- IshKebab 6y agoAll modern computers use twos-complement and modern languages like Rust require it. Not assuming twos-complement is a theoretical nitpick along the lines of not assuming a byte is 8 bits. Nobody does that.