linux_stat/raw/
riscv64.rs

1#![allow(non_camel_case_types, non_upper_case_globals)]
2
3use linux_syscalls::Sysno;
4
5use crate::{Dev, Dev64, Timestamp};
6
7pub const SYS_FSTATAT: Sysno = Sysno::fstatat;
8
9/// `fstatat()` file informations representation.
10#[repr(C)]
11#[derive(Copy, Clone)]
12pub struct stat {
13    st_dev: Dev64,
14    st_ino: u64,
15    st_mode: u32,
16    st_nlink: u32,
17    st_uid: u32,
18    st_gid: u32,
19    st_rdev: Dev64,
20    __pad0: u64,
21    st_size: i64,
22    st_blksize: i32,
23    __pad1: i32,
24    st_blocks: i64,
25    st_atime: i64,
26    st_atime_nsec: u64,
27    st_mtime: i64,
28    st_mtime_nsec: u64,
29    st_ctime: i64,
30    st_ctime_nsec: u64,
31    __pad2: [u32; 2],
32}
33
34impl stat {
35    /// Returns the device on which this file (inode) resides.
36    #[inline]
37    pub const fn dev(&self) -> Dev {
38        Dev::B64(self.st_dev)
39    }
40
41    /// Returns the inode number of the file.
42    #[inline]
43    pub const fn inode(&self) -> u64 {
44        self.st_ino
45    }
46
47    /// Returns the number of hard links on a file.
48    #[inline]
49    pub const fn nlink(&self) -> u32 {
50        self.st_nlink
51    }
52
53    #[inline]
54    pub(crate) const fn raw_mode(&self) -> u16 {
55        self.st_mode as u16
56    }
57
58    /// Returns the user ID of the owner of the file.
59    #[inline]
60    pub const fn uid(&self) -> u32 {
61        self.st_uid
62    }
63
64    /// Returns the ID of the group owner of the file.
65    #[inline]
66    pub const fn gid(&self) -> u32 {
67        self.st_gid
68    }
69
70    /// Returns the device that this file (inode) represents if the file is of
71    /// block or character device type
72    #[inline]
73    pub const fn rdev(&self) -> Dev {
74        Dev::B64(self.st_rdev)
75    }
76
77    /// Returns the size of the file (if it is a regular file or a symbolic
78    /// link) in bytes. The size of a symbolic link is the length of
79    /// the pathname it contains, without a terminating null byte.
80    #[inline]
81    pub const fn size(&self) -> i64 {
82        self.st_size
83    }
84
85    /// Returns the "preferred" block size for efficient filesystem I/O.
86    /// (Writing to a file in smaller chunks may cause an inefficient
87    /// read-modify-rewrite.)
88    #[inline]
89    pub const fn block_size(&self) -> i32 {
90        self.st_blksize
91    }
92
93    /// Returns the number of blocks allocated to the file on the medium, in
94    /// 512-byte units. (This may be smaller than stx_size/512 when the
95    /// file has holes.)
96    #[inline]
97    pub const fn blocks(&self) -> i64 {
98        self.st_blocks
99    }
100
101    /// Returns the file's last access timestamp.
102    #[inline]
103    pub const fn atime(&self) -> Timestamp {
104        Timestamp {
105            secs: self.st_atime,
106            nsecs: self.st_atime_nsec as u32,
107        }
108    }
109
110    /// Returns the file's last modification timestamp.
111    #[inline]
112    pub const fn mtime(&self) -> Timestamp {
113        Timestamp {
114            secs: self.st_mtime,
115            nsecs: self.st_mtime_nsec as u32,
116        }
117    }
118
119    /// Returns the file's last status change timestamp.
120    #[inline]
121    pub const fn ctime(&self) -> Timestamp {
122        Timestamp {
123            secs: self.st_ctime,
124            nsecs: self.st_ctime_nsec as u32,
125        }
126    }
127
128    #[doc(hidden)]
129    pub fn uninit() -> core::mem::MaybeUninit<Self> {
130        let mut res = core::mem::MaybeUninit::uninit();
131        unsafe {
132            let buf: &mut Self = &mut *res.as_mut_ptr();
133            buf.__pad0 = 0;
134            buf.__pad1 = 0;
135            core::ptr::write_bytes(
136                &mut buf.__pad2[0] as *mut u32 as *mut u8,
137                0,
138                core::mem::size_of_val(&buf.__pad2),
139            );
140        }
141        res
142    }
143}