access_dirent

Macro access_dirent 

Source
macro_rules! access_dirent {
    ($entry_ptr:expr, d_reclen) => { ... };
    ($entry_ptr:expr, d_namlen) => { ... };
    ($entry_ptr:expr, d_off) => { ... };
    ($entry_ptr:expr,d_name) => { ... };
    ($entry_ptr:expr, d_type) => { ... };
    ($entry_ptr:expr, d_ino) => { ... };
}
Expand description

A helper macro to safely access dirent(64 on linux)’s fields of a libc::dirent/libc::dirent64 aka ‘dirent-type’ struct by offset.

§Safety

  • The caller must ensure that the pointer is valid and points to a ‘dirent-type’ struct.
  • The field name must be a valid field of the ‘dirent-type’ struct.

§Field Aliases

  • On BSD systems (FreeBSD, OpenBSD, NetBSD, DragonFly), d_ino is aliased to d_fileno
  • On Linux, d_reclen is used to access the record length directly,
  • On MacOS/BSD, d_namlen is used to access the name length directly, this is a special case, since it is not aligned similarly to d_reclen.
  • The other fields are accessed normally, as raw pointers to the field

§Usage

let entry_ptr: *const libc::dirent = ...; // Assume this is a valid pointer to a dirent struct
let d_name_ptr:*const _ = access_dirent!(entry_ptr, d_name);
let d_reclen:usize = access_dirent!(entry_ptr, d_reclen);

let d_namlen:usize = access_dirent!(entry_ptr, d_namlen); // This is a special case for BSD and MacOS, where d_namlen is available
let d_ino :u64= access_dirent!(entry_ptr, d_ino); // This