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

Display the right major and minor device IDs when encountering block or char devices #1126

Open
SteveLauC opened this issue Oct 2, 2022 · 2 comments

Comments

@SteveLauC
Copy link

I noticed that the device ID returned by exa is wrong:

$ bat src/main.rs
───────┬──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       │ File: src/main.rs
───────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │ use nix::sys::stat::{major, minor, stat};
   2   │ fn main() {
   3   │     let s = stat("/dev/nvme0n1p3").unwrap();
   4   │
   5   │     println!("major: {}", major(s.st_rdev));
   6   │     println!("minor: {}", minor(s.st_rdev));
   7   │ }
───────┴──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

$ cargo r -q
major: 259
minor: 3

$ exa -l /dev/nvme0n1p3
brw-rw----@ 3,3 root  2 Oct 07:22 /dev/nvme0n1p3

And this behavior is actually documented in the source code:
https://github.com/ogham/exa/blob/master/src/fs/file.rs#L321~L332

Any reason why it is not correctly decomposed and returned?

@SteveLauC
Copy link
Author

Seems that I found the reason, DeviceID is defined as following:

pub struct DeviceIDs {
    pub major: u8,
    pub minor: u8,
}

u8 is apparently not sufficient for those IDs...

@SteveLauC
Copy link
Author

For someone who is interested in implementing this, you need major() and minor() functions from libc. These two functions for apple platforms are added in libc#2937, and included in release 0.2.135.

The impl is basically something like this:

https://github.com/SteveLauC/exa/blob/7bf29e8927c7747aacc963cb0ed273d3cc2241c9/src/fs/file.rs#L325-L328

            f::Size::DeviceIDs(f::DeviceIDs {
                major: unsafe{major(device_ids as dev_t)},
                minor: unsafe{minor(device_ids as dev_t)},
            })

https://github.com/SteveLauC/exa/blob/7bf29e8927c7747aacc963cb0ed273d3cc2241c9/src/fs/fields.rs#L181-L185

#[derive(Copy, Clone)]
pub struct DeviceIDs {
    pub major: u32,
    pub minor: u32,
}

What you need to fight is the difference between Linux's major()/minor()` definitions and the macOS one:

# Linux
pub type dev_t = u64;
pub fn major(dev: ::dev_t) -> ::c_uint;
pub fn minor(dev: ::dev_t) -> ::c_uint;

# macOS
pub type dev_t = i32;
pub fn major(dev: dev_t) -> i32;
pub fn minor(dev: dev_t) -> i32;

cesarpastorini added a commit to cesarpastorini/exa that referenced this issue May 4, 2023
cesarpastorini added a commit to cesarpastorini/exa that referenced this issue May 4, 2023
This is meant as the final step in fixing ogham#1126.

The different build targets of Rust libc's `major()` and `minor()`
functions have conflicting return types like `c_uint`, `c_int` and
`i32`, so an `i64` should be able to represent all of them.
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

1 participant