Skip to content

Commit c3bc406

Browse files
committed
linux_like: Unify statx definitions
The statx system call and corresponding constants are defined by the Linux kernel and don't depend on the libc or architecture. The only difference is whether a libc exports the statx syscall wrapper or not. We can thus unify the statx definitions for all Linux "like" platforms: GNU (glibc), Android (bionic), and (in a later commit) musl. The statx struct is in a separate file because the style check doesn't allow multiple s! macros in one file, and #[cfg()] doesn't work in s!. Plain u64 (or uint64_t in C) can't be used for the statx fields because bionic defines them as __u64, and provides incompatible definitions of uint64_t and __u64.
1 parent 7f2e466 commit c3bc406

File tree

4 files changed

+83
-111
lines changed

4 files changed

+83
-111
lines changed

src/unix/linux_like/android/mod.rs

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -515,39 +515,6 @@ s! {
515515
pub ifr6_prefixlen: u32,
516516
pub ifr6_ifindex: ::c_int,
517517
}
518-
519-
pub struct statx {
520-
pub stx_mask: ::__u32,
521-
pub stx_blksize: ::__u32,
522-
pub stx_attributes: ::__u64,
523-
pub stx_nlink: ::__u32,
524-
pub stx_uid: ::__u32,
525-
pub stx_gid: ::__u32,
526-
pub stx_mode: ::__u16,
527-
__statx_pad1: [::__u16; 1],
528-
pub stx_ino: ::__u64,
529-
pub stx_size: ::__u64,
530-
pub stx_blocks: ::__u64,
531-
pub stx_attributes_mask: ::__u64,
532-
pub stx_atime: ::statx_timestamp,
533-
pub stx_btime: ::statx_timestamp,
534-
pub stx_ctime: ::statx_timestamp,
535-
pub stx_mtime: ::statx_timestamp,
536-
pub stx_rdev_major: ::__u32,
537-
pub stx_rdev_minor: ::__u32,
538-
pub stx_dev_major: ::__u32,
539-
pub stx_dev_minor: ::__u32,
540-
pub stx_mnt_id: ::__u64,
541-
pub stx_dio_mem_align: ::__u32,
542-
pub stx_dio_offset_align: ::__u32,
543-
__statx_pad3: [::__u64; 12],
544-
}
545-
546-
pub struct statx_timestamp {
547-
pub tv_sec: ::__s64,
548-
pub tv_nsec: ::__u32,
549-
pub __reserved: ::__s32,
550-
}
551518
}
552519

553520
s_no_extra_traits! {
@@ -4158,13 +4125,6 @@ extern "C" {
41584125
newpath: *const ::c_char,
41594126
flags: ::c_uint,
41604127
) -> ::c_int;
4161-
pub fn statx(
4162-
dirfd: ::c_int,
4163-
pathname: *const c_char,
4164-
flags: ::c_int,
4165-
mask: ::c_uint,
4166-
statxbuf: *mut statx,
4167-
) -> ::c_int;
41684128

41694129
pub fn klogctl(syslog_type: ::c_int, bufp: *mut ::c_char, len: ::c_int) -> ::c_int;
41704130
}

src/unix/linux_like/linux/gnu/mod.rs

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,6 @@ cfg_if! {
1616
}
1717

1818
s! {
19-
pub struct statx {
20-
pub stx_mask: u32,
21-
pub stx_blksize: u32,
22-
pub stx_attributes: u64,
23-
pub stx_nlink: u32,
24-
pub stx_uid: u32,
25-
pub stx_gid: u32,
26-
pub stx_mode: u16,
27-
__statx_pad1: [u16; 1],
28-
pub stx_ino: u64,
29-
pub stx_size: u64,
30-
pub stx_blocks: u64,
31-
pub stx_attributes_mask: u64,
32-
pub stx_atime: ::statx_timestamp,
33-
pub stx_btime: ::statx_timestamp,
34-
pub stx_ctime: ::statx_timestamp,
35-
pub stx_mtime: ::statx_timestamp,
36-
pub stx_rdev_major: u32,
37-
pub stx_rdev_minor: u32,
38-
pub stx_dev_major: u32,
39-
pub stx_dev_minor: u32,
40-
pub stx_mnt_id: u64,
41-
pub stx_dio_mem_align: u32,
42-
pub stx_dio_offset_align: u32,
43-
__statx_pad3: [u64; 12],
44-
}
45-
46-
pub struct statx_timestamp {
47-
pub tv_sec: i64,
48-
pub tv_nsec: u32,
49-
pub __statx_timestamp_pad1: [i32; 1],
50-
}
51-
5219
pub struct aiocb {
5320
pub aio_fildes: ::c_int,
5421
pub aio_lio_opcode: ::c_int,
@@ -1148,37 +1115,6 @@ pub const M_PERTURB: ::c_int = -6;
11481115
pub const M_ARENA_TEST: ::c_int = -7;
11491116
pub const M_ARENA_MAX: ::c_int = -8;
11501117

1151-
pub const AT_STATX_SYNC_TYPE: ::c_int = 0x6000;
1152-
pub const AT_STATX_SYNC_AS_STAT: ::c_int = 0x0000;
1153-
pub const AT_STATX_FORCE_SYNC: ::c_int = 0x2000;
1154-
pub const AT_STATX_DONT_SYNC: ::c_int = 0x4000;
1155-
pub const STATX_TYPE: ::c_uint = 0x0001;
1156-
pub const STATX_MODE: ::c_uint = 0x0002;
1157-
pub const STATX_NLINK: ::c_uint = 0x0004;
1158-
pub const STATX_UID: ::c_uint = 0x0008;
1159-
pub const STATX_GID: ::c_uint = 0x0010;
1160-
pub const STATX_ATIME: ::c_uint = 0x0020;
1161-
pub const STATX_MTIME: ::c_uint = 0x0040;
1162-
pub const STATX_CTIME: ::c_uint = 0x0080;
1163-
pub const STATX_INO: ::c_uint = 0x0100;
1164-
pub const STATX_SIZE: ::c_uint = 0x0200;
1165-
pub const STATX_BLOCKS: ::c_uint = 0x0400;
1166-
pub const STATX_BASIC_STATS: ::c_uint = 0x07ff;
1167-
pub const STATX_BTIME: ::c_uint = 0x0800;
1168-
pub const STATX_MNT_ID: ::c_uint = 0x1000;
1169-
pub const STATX_DIOALIGN: ::c_uint = 0x2000;
1170-
pub const STATX_ALL: ::c_uint = 0x0fff;
1171-
pub const STATX__RESERVED: ::c_int = 0x80000000;
1172-
pub const STATX_ATTR_COMPRESSED: ::c_int = 0x0004;
1173-
pub const STATX_ATTR_IMMUTABLE: ::c_int = 0x0010;
1174-
pub const STATX_ATTR_APPEND: ::c_int = 0x0020;
1175-
pub const STATX_ATTR_NODUMP: ::c_int = 0x0040;
1176-
pub const STATX_ATTR_ENCRYPTED: ::c_int = 0x0800;
1177-
pub const STATX_ATTR_AUTOMOUNT: ::c_int = 0x1000;
1178-
pub const STATX_ATTR_MOUNT_ROOT: ::c_int = 0x2000;
1179-
pub const STATX_ATTR_VERITY: ::c_int = 0x00100000;
1180-
pub const STATX_ATTR_DAX: ::c_int = 0x00200000;
1181-
11821118
pub const SOMAXCONN: ::c_int = 4096;
11831119

11841120
// linux/mount.h
@@ -1360,13 +1296,6 @@ extern "C" {
13601296
pub fn getpt() -> ::c_int;
13611297
pub fn mallopt(param: ::c_int, value: ::c_int) -> ::c_int;
13621298
pub fn gettimeofday(tp: *mut ::timeval, tz: *mut ::timezone) -> ::c_int;
1363-
pub fn statx(
1364-
dirfd: ::c_int,
1365-
pathname: *const c_char,
1366-
flags: ::c_int,
1367-
mask: ::c_uint,
1368-
statxbuf: *mut statx,
1369-
) -> ::c_int;
13701299
pub fn getentropy(buf: *mut ::c_void, buflen: ::size_t) -> ::c_int;
13711300
pub fn getrandom(buf: *mut ::c_void, buflen: ::size_t, flags: ::c_uint) -> ::ssize_t;
13721301
pub fn getauxval(type_: ::c_ulong) -> ::c_ulong;

src/unix/linux_like/linux_statx.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
s! {
2+
pub struct statx {
3+
pub stx_mask: ::__u32,
4+
pub stx_blksize: ::__u32,
5+
pub stx_attributes: ::__u64,
6+
pub stx_nlink: ::__u32,
7+
pub stx_uid: ::__u32,
8+
pub stx_gid: ::__u32,
9+
pub stx_mode: ::__u16,
10+
__statx_pad1: [::__u16; 1],
11+
pub stx_ino: ::__u64,
12+
pub stx_size: ::__u64,
13+
pub stx_blocks: ::__u64,
14+
pub stx_attributes_mask: ::__u64,
15+
pub stx_atime: statx_timestamp,
16+
pub stx_btime: statx_timestamp,
17+
pub stx_ctime: statx_timestamp,
18+
pub stx_mtime: statx_timestamp,
19+
pub stx_rdev_major: ::__u32,
20+
pub stx_rdev_minor: ::__u32,
21+
pub stx_dev_major: ::__u32,
22+
pub stx_dev_minor: ::__u32,
23+
pub stx_mnt_id: ::__u64,
24+
pub stx_dio_mem_align: ::__u32,
25+
pub stx_dio_offset_align: ::__u32,
26+
__statx_pad3: [::__u64; 12],
27+
}
28+
29+
pub struct statx_timestamp {
30+
pub tv_sec: ::__s64,
31+
pub tv_nsec: ::__u32,
32+
__statx_timestamp_pad1: [::__s32; 1],
33+
}
34+
}
35+
36+
pub const AT_STATX_SYNC_TYPE: ::c_int = 0x6000;
37+
pub const AT_STATX_SYNC_AS_STAT: ::c_int = 0x0000;
38+
pub const AT_STATX_FORCE_SYNC: ::c_int = 0x2000;
39+
pub const AT_STATX_DONT_SYNC: ::c_int = 0x4000;
40+
pub const STATX_TYPE: ::c_uint = 0x0001;
41+
pub const STATX_MODE: ::c_uint = 0x0002;
42+
pub const STATX_NLINK: ::c_uint = 0x0004;
43+
pub const STATX_UID: ::c_uint = 0x0008;
44+
pub const STATX_GID: ::c_uint = 0x0010;
45+
pub const STATX_ATIME: ::c_uint = 0x0020;
46+
pub const STATX_MTIME: ::c_uint = 0x0040;
47+
pub const STATX_CTIME: ::c_uint = 0x0080;
48+
pub const STATX_INO: ::c_uint = 0x0100;
49+
pub const STATX_SIZE: ::c_uint = 0x0200;
50+
pub const STATX_BLOCKS: ::c_uint = 0x0400;
51+
pub const STATX_BASIC_STATS: ::c_uint = 0x07ff;
52+
pub const STATX_BTIME: ::c_uint = 0x0800;
53+
pub const STATX_ALL: ::c_uint = 0x0fff;
54+
pub const STATX_MNT_ID: ::c_uint = 0x1000;
55+
pub const STATX_DIOALIGN: ::c_uint = 0x2000;
56+
pub const STATX__RESERVED: ::c_int = 0x80000000;
57+
pub const STATX_ATTR_COMPRESSED: ::c_int = 0x0004;
58+
pub const STATX_ATTR_IMMUTABLE: ::c_int = 0x0010;
59+
pub const STATX_ATTR_APPEND: ::c_int = 0x0020;
60+
pub const STATX_ATTR_NODUMP: ::c_int = 0x0040;
61+
pub const STATX_ATTR_ENCRYPTED: ::c_int = 0x0800;
62+
pub const STATX_ATTR_AUTOMOUNT: ::c_int = 0x1000;
63+
pub const STATX_ATTR_MOUNT_ROOT: ::c_int = 0x2000;
64+
pub const STATX_ATTR_VERITY: ::c_int = 0x100000;
65+
pub const STATX_ATTR_DAX: ::c_int = 0x200000;
66+
67+
extern "C" {
68+
pub fn statx(
69+
dirfd: ::c_int,
70+
pathname: *const ::c_char,
71+
flags: ::c_int,
72+
mask: ::c_uint,
73+
statxbuf: *mut statx,
74+
) -> ::c_int;
75+
}

src/unix/linux_like/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1886,6 +1886,14 @@ cfg_if! {
18861886
}
18871887
}
18881888

1889+
// The statx syscall, available on some libcs.
1890+
cfg_if! {
1891+
if #[cfg(any(target_env = "gnu", target_os = "android"))] {
1892+
mod linux_statx;
1893+
pub use self::linux_statx::*;
1894+
}
1895+
}
1896+
18891897
cfg_if! {
18901898
if #[cfg(target_os = "emscripten")] {
18911899
mod emscripten;

0 commit comments

Comments
 (0)