Skip to content

Commit a4e7357

Browse files
committed
Update both 32 and 64 bit statx
1 parent 94df6e8 commit a4e7357

File tree

1 file changed

+60
-11
lines changed

1 file changed

+60
-11
lines changed

qiling/os/posix/syscall/stat.py

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,14 +1146,54 @@ def ql_syscall_stat(ql: Qiling, path: int, buf_ptr: int):
11461146
def ql_syscall_stat64(ql: Qiling, path: int, buf_ptr: int):
11471147
return statFamily(ql, path, buf_ptr, "stat64", Stat, pack_stat64_struct)
11481148

1149-
class StatxTimestamp(ctypes.Structure):
1149+
class StatxTimestamp32(ctypes.Structure):
1150+
_fields_ = [
1151+
('tv_sec', ctypes.c_int32),
1152+
('tv_nsec', ctypes.c_uint32),
1153+
('__statx_timestamp_pad1', ctypes.c_int32)
1154+
]
1155+
1156+
class Statx32(ctypes.Structure):
1157+
"""
1158+
Reference:
1159+
- https://man7.org/linux/man-pages/man2/statx.2.html
1160+
- https://code.woboq.org/userspace/glibc/sysdeps/unix/sysv/linux/statx.c.html
1161+
1162+
"""
1163+
_fields_ = [
1164+
('stx_mask', ctypes.c_uint32),
1165+
('stx_blksize', ctypes.c_uint32),
1166+
('stx_attributes', ctypes.c_uint32),
1167+
('stx_nlink', ctypes.c_uint32),
1168+
('stx_uid', ctypes.c_uint32),
1169+
('stx_gid', ctypes.c_uint32),
1170+
('stx_mode', ctypes.c_uint16),
1171+
('__statx_pad1', ctypes.c_uint16),
1172+
('stx_ino', ctypes.c_uint32),
1173+
('stx_size', ctypes.c_uint32),
1174+
('stx_blocks', ctypes.c_uint32),
1175+
('stx_attributes_mask', ctypes.c_uint32),
1176+
('stx_atime', StatxTimestamp32),
1177+
('stx_btime', StatxTimestamp32),
1178+
('stx_ctime', StatxTimestamp32),
1179+
('stx_mtime', StatxTimestamp32),
1180+
('stx_rdev_major', ctypes.c_uint32),
1181+
('stx_rdev_minor', ctypes.c_uint32),
1182+
('stx_dev_major', ctypes.c_uint32),
1183+
('stx_dev_minor', ctypes.c_uint32),
1184+
('__statx_pad2', ctypes.c_uint32 * 14),
1185+
]
1186+
1187+
_pack_ = 8
1188+
1189+
class StatxTimestamp64(ctypes.Structure):
11501190
_fields_ = [
11511191
('tv_sec', ctypes.c_int64),
11521192
('tv_nsec', ctypes.c_uint32),
11531193
('__statx_timestamp_pad1', ctypes.c_int32)
11541194
]
11551195

1156-
class Statx(ctypes.Structure):
1196+
class Statx64(ctypes.Structure):
11571197
"""
11581198
Reference:
11591199
- https://man7.org/linux/man-pages/man2/statx.2.html
@@ -1173,27 +1213,31 @@ class Statx(ctypes.Structure):
11731213
('stx_size', ctypes.c_uint64),
11741214
('stx_blocks', ctypes.c_uint64),
11751215
('stx_attributes_mask', ctypes.c_uint64),
1176-
('stx_atime', StatxTimestamp),
1177-
('stx_btime', StatxTimestamp),
1178-
('stx_ctime', StatxTimestamp),
1179-
('stx_mtime', StatxTimestamp),
1216+
('stx_atime', StatxTimestamp64),
1217+
('stx_btime', StatxTimestamp64),
1218+
('stx_ctime', StatxTimestamp64),
1219+
('stx_mtime', StatxTimestamp64),
11801220
('stx_rdev_major', ctypes.c_uint32),
11811221
('stx_rdev_minor', ctypes.c_uint32),
11821222
('stx_dev_major', ctypes.c_uint32),
11831223
('stx_dev_minor', ctypes.c_uint32),
11841224
('__statx_pad2', ctypes.c_uint64 * 14),
11851225
]
11861226

1187-
_pack_ = 8
1227+
_pack_ = 4
11881228

11891229
# int statx(int dirfd, const char *restrict pathname, int flags,
11901230
# unsigned int mask, struct statx *restrict statxbuf);
11911231
def ql_syscall_statx(ql: Qiling, dirfd: int, path: int, flags: int, mask: int, buf_ptr: int):
11921232
def statx_convert_timestamp(tv_sec, tv_nsec):
1193-
return StatxTimestamp(
1194-
tv_sec = struct.unpack('i', struct.pack('f', tv_sec))[0],
1195-
tv_nsec = struct.unpack('i', struct.pack('f', tv_nsec))[0],
1196-
)
1233+
tv_sec = struct.unpack('i', struct.pack('f', tv_sec))[0]
1234+
tv_nsec = struct.unpack('i', struct.pack('f', tv_nsec))[0]
1235+
1236+
if ql.archbit == 32:
1237+
return StatxTimestamp32(tv_sec=tv_sec, tv_nsec=tv_nsec)
1238+
else:
1239+
return StatxTimestamp64(tv_sec=tv_sec, tv_nsec=tv_nsec)
1240+
11971241

11981242
def major(dev):
11991243
return ((dev >> 8) & 0xfff) | ((dev >> 32) & ~0xfff)
@@ -1209,6 +1253,11 @@ def minor(dev):
12091253
else:
12101254
st = Stat(real_path, fd)
12111255

1256+
if ql.archbit == 32:
1257+
Statx = Statx32
1258+
else:
1259+
Statx = Statx64
1260+
12121261
stx = Statx(
12131262
stx_mask = 0x07ff, # STATX_BASIC_STATS
12141263
stx_blksize = st.st_blksize,

0 commit comments

Comments
 (0)