From fe70669cd3406b0f964d6b025d55652dc24fc47e Mon Sep 17 00:00:00 2001
From: wanglujun <wanglujun@uniontech.com>
Date: Wed, 6 Sep 2023 20:10:17 +0800
Subject: [PATCH] change-struct-stat-for-arm-and-loongarch

---
 lib/util/src/secure_path.rs      | 64 ++++++++++++++++++++++----------
 lib/util/src/sudo_conf.rs        | 29 +++++++++++++++
 lib/util/src/sudo_debug_macro.rs |  4 --
 lib/util/src/ttyname_dev.rs      | 59 +++++++++++++++++++++++++++++
 4 files changed, 133 insertions(+), 23 deletions(-)

diff --git a/lib/util/src/secure_path.rs b/lib/util/src/secure_path.rs
index 428c79d..b09b57f 100644
--- a/lib/util/src/secure_path.rs
+++ b/lib/util/src/secure_path.rs
@@ -28,10 +28,16 @@ pub type __uid_t = libc::c_uint;
 pub type __gid_t = libc::c_uint;
 pub type __ino_t = libc::c_ulong;
 pub type __mode_t = libc::c_uint;
+#[cfg(target_arch = "x86_64")]
 pub type __nlink_t = libc::c_ulong;
+#[cfg(not(target_arch = "x86_64"))]
+pub type __nlink_t = libc::c_uint;
 pub type __off_t = libc::c_long;
 pub type __time_t = libc::c_long;
+#[cfg(target_arch = "x86_64")]
 pub type __blksize_t = libc::c_long;
+#[cfg(not(target_arch = "x86_64"))]
+pub type __blksize_t = libc::c_int;
 pub type __blkcnt_t = libc::c_long;
 pub type __syscall_slong_t = libc::c_long;
 pub type gid_t = __gid_t;
@@ -155,27 +161,32 @@ pub struct timespec {
 #[derive(Copy, Clone)]
 #[repr(C)]
 pub struct stat {
-    pub st_dev: __dev_t,     /* Device.  */
-    pub st_ino: __ino_t,     /* File serial number.	*/
-    pub st_nlink: __nlink_t, /* Link count.  */
-    pub st_mode: __mode_t,   /* File mode.  */
-    pub st_uid: __uid_t,     /* User ID of the file's owner.	*/
-    pub st_gid: __gid_t,     /* Group ID of the file's group.*/
+    pub st_dev: __dev_t,
+    pub st_ino: __ino_t,
+    #[cfg(target_arch = "x86_64")]
+    pub st_nlink: __nlink_t,
+    pub st_mode: __mode_t,
+    #[cfg(not(target_arch = "x86_64"))]
+    pub st_nlink: __nlink_t,
+    pub st_uid: __uid_t,
+    pub st_gid: __gid_t,
+    #[cfg(target_arch = "x86_64")]
     pub __pad0: libc::c_int,
-    pub st_rdev: __dev_t,        /* Device number, if device.  */
-    pub st_size: __off_t,        /* Size of file, in bytes.  */
-    pub st_blksize: __blksize_t, /* Optimal block size for I/O.  */
-    pub st_blocks: __blkcnt_t,   /* Number 512-byte blocks allocated. */
-    /* Nanosecond resolution timestamps are stored in a format
-    equivalent to 'struct timespec'.  This is the type used
-    whenever possible but the Unix namespace rules do not allow the
-    identifier 'timespec' to appear in the <sys/stat.h> header.
-    Therefore we have to handle the use of this header in strictly
-    standard-compliant sources special.  */
-    pub st_atim: timespec, /* Time of last access.  */
-    pub st_mtim: timespec, /* Time of last modification.  */
-    pub st_ctim: timespec, /* Time of last status change.  */
+    pub st_rdev: __dev_t,
+    #[cfg(not(target_arch = "x86_64"))]
+    pub __pad1: __dev_t,
+    pub st_size: __off_t,
+    pub st_blksize: __blksize_t,
+    #[cfg(not(target_arch = "x86_64"))]
+    pub __pad2: libc::c_int,
+    pub st_blocks: __blkcnt_t,
+    pub st_atim: timespec,
+    pub st_mtim: timespec,
+    pub st_ctim: timespec,
+    #[cfg(target_arch = "x86_64")]
     pub __glibc_reserved: [__syscall_slong_t; 3],
+    #[cfg(not(target_arch = "x86_64"))]
+    pub __glibc_reserved: [libc::c_int; 2],
 }
 
 #[inline]
@@ -183,7 +194,10 @@ unsafe extern "C" fn stat(
     mut __path: *const libc::c_char,
     mut __statbuf: *mut stat,
 ) -> libc::c_int {
+    #[cfg(target_arch = "x86_64")]
     return __xstat(1 as libc::c_int, __path, __statbuf);
+    #[cfg(not(target_arch = "x86_64"))]
+    return __xstat(0 as libc::c_int, __path, __statbuf);
 }
 
 /*
@@ -200,14 +214,22 @@ pub unsafe extern "C" fn sudo_secure_path(
     let mut sb: stat = stat {
         st_dev: 0,
         st_ino: 0,
+        #[cfg(target_arch = "x86_64")]
         st_nlink: 0,
         st_mode: 0,
+        #[cfg(not(target_arch = "x86_64"))]
+        st_nlink: 0,
         st_uid: 0,
         st_gid: 0,
+        #[cfg(target_arch = "x86_64")]
         __pad0: 0,
         st_rdev: 0,
+        #[cfg(not(target_arch = "x86_64"))]
+        __pad1: 0,
         st_size: 0,
         st_blksize: 0,
+        #[cfg(not(target_arch = "x86_64"))]
+        __pad2: 0,
         st_blocks: 0,
         st_atim: timespec {
             tv_sec: 0,
@@ -221,8 +243,12 @@ pub unsafe extern "C" fn sudo_secure_path(
             tv_sec: 0,
             tv_nsec: 0,
         },
+        #[cfg(target_arch = "x86_64")]
         __glibc_reserved: [0; 3],
+        #[cfg(not(target_arch = "x86_64"))]
+        __glibc_reserved: [0; 2],
     };
+
     let mut ret: libc::c_int = SUDO_PATH_MISSING!();
     debug_decl!(stdext::function_name!().as_ptr(), SUDO_DEBUG_UTIL);
 
diff --git a/lib/util/src/sudo_conf.rs b/lib/util/src/sudo_conf.rs
index 31b8374..5fead52 100644
--- a/lib/util/src/sudo_conf.rs
+++ b/lib/util/src/sudo_conf.rs
@@ -32,11 +32,17 @@ pub type __uid_t = libc::c_uint;
 pub type __gid_t = libc::c_uint;
 pub type __ino_t = libc::c_ulong;
 pub type __mode_t = libc::c_uint;
+#[cfg(target_arch = "x86_64")]
 pub type __nlink_t = libc::c_ulong;
+#[cfg(not(target_arch = "x86_64"))]
+pub type __nlink_t = libc::c_uint;
 pub type __off_t = libc::c_long;
 pub type __off64_t = libc::c_long;
 pub type __time_t = libc::c_long;
+#[cfg(target_arch = "x86_64")]
 pub type __blksize_t = libc::c_long;
+#[cfg(not(target_arch = "x86_64"))]
+pub type __blksize_t = libc::c_int;
 pub type __blkcnt_t = libc::c_long;
 pub type __ssize_t = libc::c_long;
 pub type __syscall_slong_t = libc::c_long;
@@ -57,19 +63,30 @@ pub struct timespec {
 pub struct stat {
     pub st_dev: __dev_t,
     pub st_ino: __ino_t,
+    #[cfg(target_arch = "x86_64")]
     pub st_nlink: __nlink_t,
     pub st_mode: __mode_t,
+    #[cfg(not(target_arch = "x86_64"))]
+    pub st_nlink: __nlink_t,
     pub st_uid: __uid_t,
     pub st_gid: __gid_t,
+    #[cfg(target_arch = "x86_64")]
     pub __pad0: libc::c_int,
     pub st_rdev: __dev_t,
+    #[cfg(not(target_arch = "x86_64"))]
+    pub __pad1: __dev_t,
     pub st_size: __off_t,
     pub st_blksize: __blksize_t,
+    #[cfg(not(target_arch = "x86_64"))]
+    pub __pad2: libc::c_int,
     pub st_blocks: __blkcnt_t,
     pub st_atim: timespec,
     pub st_mtim: timespec,
     pub st_ctim: timespec,
+    #[cfg(target_arch = "x86_64")]
     pub __glibc_reserved: [__syscall_slong_t; 3],
+    #[cfg(not(target_arch = "x86_64"))]
+    pub __glibc_reserved: [libc::c_int; 2],
 }
 
 #[derive(Copy, Clone)]
@@ -1111,14 +1128,22 @@ pub unsafe extern "C" fn sudo_conf_read_v1(
     let mut sb: stat = stat {
         st_dev: 0,
         st_ino: 0,
+        #[cfg(target_arch = "x86_64")]
         st_nlink: 0,
         st_mode: 0,
+        #[cfg(not(target_arch = "x86_64"))]
+        st_nlink: 0,
         st_uid: 0,
         st_gid: 0,
+        #[cfg(target_arch = "x86_64")]
         __pad0: 0,
         st_rdev: 0,
+        #[cfg(not(target_arch = "x86_64"))]
+        __pad1: 0,
         st_size: 0,
         st_blksize: 0,
+        #[cfg(not(target_arch = "x86_64"))]
+        __pad2: 0,
         st_blocks: 0,
         st_atim: timespec {
             tv_sec: 0,
@@ -1132,8 +1157,12 @@ pub unsafe extern "C" fn sudo_conf_read_v1(
             tv_sec: 0,
             tv_nsec: 0,
         },
+        #[cfg(target_arch = "x86_64")]
         __glibc_reserved: [0; 3],
+        #[cfg(not(target_arch = "x86_64"))]
+        __glibc_reserved: [0; 2],
     };
+
     let mut fp: *mut FILE = 0 as *mut FILE;
     let mut ret: libc::c_int = false as libc::c_int;
 
diff --git a/lib/util/src/sudo_debug_macro.rs b/lib/util/src/sudo_debug_macro.rs
index 4822b7a..bb448c9 100644
--- a/lib/util/src/sudo_debug_macro.rs
+++ b/lib/util/src/sudo_debug_macro.rs
@@ -22,7 +22,6 @@ use crate::sudo_debug::*;
 
 pub static mut sudo_debug_subsys: libc::c_int = 0 as libc::c_int;
 
-
 // extern "C" {
 //     fn sudo_debug_printf2_v1(
 //         func: *const libc::c_char,
@@ -134,7 +133,6 @@ macro_rules! debug_decl {
     };
 }
 
-
 macro_rules! debug_return_int {
     ($ret:expr) => {{
         sudo_debug_exit_int_v1(
@@ -235,7 +233,6 @@ macro_rules! debug_return_bool {
     }};
 }
 
-
 macro_rules! debug_return_str {
     ($ret:expr) => {{
         sudo_debug_exit_str_v1(
@@ -249,7 +246,6 @@ macro_rules! debug_return_str {
     }};
 }
 
-
 macro_rules! debug_return_const_str {
     ($ret:expr) => {
         let mut sudo_debug_ret: *mut libc::c_char = ($ret);
diff --git a/lib/util/src/ttyname_dev.rs b/lib/util/src/ttyname_dev.rs
index 0351c56..da85da5 100644
--- a/lib/util/src/ttyname_dev.rs
+++ b/lib/util/src/ttyname_dev.rs
@@ -31,10 +31,16 @@ pub type __uid_t = libc::c_uint;
 pub type __gid_t = libc::c_uint;
 pub type __ino_t = libc::c_ulong;
 pub type __mode_t = libc::c_uint;
+#[cfg(target_arch = "x86_64")]
 pub type __nlink_t = libc::c_ulong;
+#[cfg(not(target_arch = "x86_64"))]
+pub type __nlink_t = libc::c_uint;
 pub type __off_t = libc::c_long;
 pub type __time_t = libc::c_long;
+#[cfg(target_arch = "x86_64")]
 pub type __blksize_t = libc::c_long;
+#[cfg(not(target_arch = "x86_64"))]
+pub type __blksize_t = libc::c_int;
 pub type __blkcnt_t = libc::c_long;
 pub type __syscall_slong_t = libc::c_long;
 pub type dev_t = __dev_t;
@@ -212,19 +218,30 @@ pub struct timespec {
 pub struct stat {
     pub st_dev: __dev_t,
     pub st_ino: __ino_t,
+    #[cfg(target_arch = "x86_64")]
     pub st_nlink: __nlink_t,
     pub st_mode: __mode_t,
+    #[cfg(not(target_arch = "x86_64"))]
+    pub st_nlink: __nlink_t,
     pub st_uid: __uid_t,
     pub st_gid: __gid_t,
+    #[cfg(target_arch = "x86_64")]
     pub __pad0: libc::c_int,
     pub st_rdev: __dev_t,
+    #[cfg(not(target_arch = "x86_64"))]
+    pub __pad1: __dev_t,
     pub st_size: __off_t,
     pub st_blksize: __blksize_t,
+    #[cfg(not(target_arch = "x86_64"))]
+    pub __pad2: libc::c_int,
     pub st_blocks: __blkcnt_t,
     pub st_atim: timespec,
     pub st_mtim: timespec,
     pub st_ctim: timespec,
+    #[cfg(target_arch = "x86_64")]
     pub __glibc_reserved: [__syscall_slong_t; 3],
+    #[cfg(not(target_arch = "x86_64"))]
+    pub __glibc_reserved: [libc::c_int; 2],
 }
 
 #[derive(Copy, Clone)]
@@ -251,14 +268,21 @@ static mut ignore_devs: [*const libc::c_char; 4] = [
 
 #[inline]
 unsafe extern "C" fn fstat(mut __fd: libc::c_int, mut __statbuf: *mut stat) -> libc::c_int {
+    #[cfg(target_arch = "x86_64")]
     return __fxstat(1 as libc::c_int, __fd, __statbuf);
+    #[cfg(not(target_arch = "x86_64"))]
+    return __fxstat(0 as libc::c_int, __fd, __statbuf);
 }
+
 #[inline]
 unsafe extern "C" fn stat(
     mut __path: *const libc::c_char,
     mut __statbuf: *mut stat,
 ) -> libc::c_int {
+    #[cfg(target_arch = "x86_64")]
     return __xstat(1 as libc::c_int, __path, __statbuf);
+    #[cfg(not(target_arch = "x86_64"))]
+    return __xstat(0 as libc::c_int, __path, __statbuf);
 }
 
 #[inline]
@@ -289,17 +313,26 @@ unsafe extern "C" fn sudo_ttyname_scan(
     let mut pathbuf: [libc::c_char; PATH_MAX] = [0; PATH_MAX];
     let mut ret: *mut libc::c_char = 0 as *mut libc::c_char;
     let mut dp: *mut dirent = 0 as *mut dirent;
+
     let mut sb: stat = stat {
         st_dev: 0,
         st_ino: 0,
+        #[cfg(target_arch = "x86_64")]
         st_nlink: 0,
         st_mode: 0,
+        #[cfg(not(target_arch = "x86_64"))]
+        st_nlink: 0,
         st_uid: 0,
         st_gid: 0,
+        #[cfg(target_arch = "x86_64")]
         __pad0: 0,
         st_rdev: 0,
+        #[cfg(not(target_arch = "x86_64"))]
+        __pad1: 0,
         st_size: 0,
         st_blksize: 0,
+        #[cfg(not(target_arch = "x86_64"))]
+        __pad2: 0,
         st_blocks: 0,
         st_atim: timespec {
             tv_sec: 0,
@@ -313,7 +346,10 @@ unsafe extern "C" fn sudo_ttyname_scan(
             tv_sec: 0,
             tv_nsec: 0,
         },
+        #[cfg(target_arch = "x86_64")]
         __glibc_reserved: [0; 3],
+        #[cfg(not(target_arch = "x86_64"))]
+        __glibc_reserved: [0; 2],
     };
 
     let mut i: libc::c_int = 0 as libc::c_int;
@@ -372,14 +408,22 @@ unsafe extern "C" fn sudo_ttyname_scan(
             let mut sb: stat = stat {
                 st_dev: 0,
                 st_ino: 0,
+                #[cfg(target_arch = "x86_64")]
                 st_nlink: 0,
                 st_mode: 0,
+                #[cfg(not(target_arch = "x86_64"))]
+                st_nlink: 0,
                 st_uid: 0,
                 st_gid: 0,
+                #[cfg(target_arch = "x86_64")]
                 __pad0: 0,
                 st_rdev: 0,
+                #[cfg(not(target_arch = "x86_64"))]
+                __pad1: 0,
                 st_size: 0,
                 st_blksize: 0,
+                #[cfg(not(target_arch = "x86_64"))]
+                __pad2: 0,
                 st_blocks: 0,
                 st_atim: timespec {
                     tv_sec: 0,
@@ -393,8 +437,12 @@ unsafe extern "C" fn sudo_ttyname_scan(
                     tv_sec: 0,
                     tv_nsec: 0,
                 },
+                #[cfg(target_arch = "x86_64")]
                 __glibc_reserved: [0; 3],
+                #[cfg(not(target_arch = "x86_64"))]
+                __glibc_reserved: [0; 2],
             };
+
             /* Skip anything starting with "." */
             if (*dp).d_name[0 as usize] as libc::c_int == '.' as i32 {
                 continue;
@@ -495,14 +543,22 @@ unsafe extern "C" fn sudo_dev_check(
     let mut sb: stat = stat {
         st_dev: 0,
         st_ino: 0,
+        #[cfg(target_arch = "x86_64")]
         st_nlink: 0,
         st_mode: 0,
+        #[cfg(not(target_arch = "x86_64"))]
+        st_nlink: 0,
         st_uid: 0,
         st_gid: 0,
+        #[cfg(target_arch = "x86_64")]
         __pad0: 0,
         st_rdev: 0,
+        #[cfg(not(target_arch = "x86_64"))]
+        __pad1: 0,
         st_size: 0,
         st_blksize: 0,
+        #[cfg(not(target_arch = "x86_64"))]
+        __pad2: 0,
         st_blocks: 0,
         st_atim: timespec {
             tv_sec: 0,
@@ -516,7 +572,10 @@ unsafe extern "C" fn sudo_dev_check(
             tv_sec: 0,
             tv_nsec: 0,
         },
+        #[cfg(target_arch = "x86_64")]
         __glibc_reserved: [0; 3],
+        #[cfg(not(target_arch = "x86_64"))]
+        __glibc_reserved: [0; 2],
     };
     debug_decl!(stdext::function_name!().as_ptr(), SUDO_DEBUG_UTIL);
     if stat(devname, &mut sb) == 0 {
-- 
2.41.0

