Closed cbjadwani closed 1 year ago
@sylvestre GitHub closed this automatically, but it hasn't been implemented yet
Is the only issue with this the major device number? Type and minor seem to be working fine. I will create a quick PR for that fix now.
Actually, I'm not sure. I think I might have put the reopen request on the wrong issue😄 Your fix is interesting though, because shifting by 8 works for me, so is it dependent on platform?
Ah that is interesting. It must be, tried to find some info but couldn't find anything concrete. I'm on aarch64 macOS, the stat command is also giving me some strange results for device files.
I think I found the solution based on:
We need the major
function in libc
: https://docs.rs/libc/0.2.94/libc/fn.major.html. This works on my machine:
let major = unsafe { uucore::libc::major(dev) } as u8;
let minor = unsafe { uucore::libc::minor(dev) } as u8;
Could you try that on mac as well?
Ah good find. For some reason I can't use the major and minor funcs from libc? Maybe being feature-gated out for some reason? Although looking at the source:
pub fn major(dev: ::dev_t) -> ::c_uint {
let mut major = 0;
major |= (dev & 0x00000000000fff00) >> 8;
major |= (dev & 0xfffff00000000000) >> 32;
major as ::c_uint
}
pub fn minor(dev: ::dev_t) -> ::c_uint {
let mut minor = 0;
minor |= (dev & 0x00000000000000ff) >> 0;
minor |= (dev & 0x00000ffffff00000) >> 12;
minor as ::c_uint
}
I don't see any reason why we couldn't use this logic without calling the unsafe libc func? Unless I'm missing something?
For some reason I can't use the major and minor funcs from libc?
Interesting, it looks like those functions aren't defined for Mac. That's too bad.
I don't see any reason why we couldn't use this logic without calling the unsafe libc func? Unless I'm missing something?
I'm guessing that the logic should be different on different systems so this might not solve it, but it's worth a shot!
I did some more digging and this is what exa
does (they just ignore the issue):
// In C-land, getting the major and minor device IDs is done with
// preprocessor macros called `major` and `minor` that depend on
// the size of `dev_t`, but we just take the second-to-last and
// last bytes.
f::Size::DeviceIDs(f::DeviceIDs {
major: device_ids[6],
minor: device_ids[7],
})
Also here's a StackOverflow answer that we might be able to translate: https://stackoverflow.com/questions/35392291/how-to-convert-between-a-dev-t-and-major-minor-device-numbers. Honestly, I think it's okay to just implement a version that works for Linux and Mac and add fixes for other systems later.
Edit: That SO answer mentions that everything is the same for all systems (except the dev_t
size, I guess) apart from Windows, so maybe your solution does work.
Ah good find. For some reason I can't use the major and minor funcs from libc? Maybe being feature-gated out for some reason?
These two functions for Darwin have been added in libc
@SteveLauC Thanks for contributing that to libc
!
So to summarize, we now have this for:
And the way we can enable this for more platforms is by contributing to libc
?
That's exactly what I am doing right now, see rust-lang/libc#2999 😃
It seems that redox does not support this. I searched their libc impl and didn't find those functions
Awesome!
Add support to show device major and minor numbers and type (block/char).
Example:
The 'c' in
crw-rw----
is for the type of device and13, 76
are major and minor numbers of the device.