Skip to content

Commit fe9ac7f

Browse files
jbosboomcmaloneyvstinner
authored
gh-83714: Implement os.statx() function (#139178)
Co-authored-by: Cody Maloney <[email protected]> Co-authored-by: Victor Stinner <[email protected]>
1 parent 27acaf1 commit fe9ac7f

File tree

18 files changed

+1018
-71
lines changed

18 files changed

+1018
-71
lines changed

Doc/library/os.rst

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3383,6 +3383,214 @@ features:
33833383
Added the :attr:`st_birthtime` member on Windows.
33843384

33853385

3386+
.. function:: statx(path, mask, *, flags=0, dir_fd=None, follow_symlinks=True)
3387+
3388+
Get the status of a file or file descriptor by performing a :c:func:`!statx`
3389+
system call on the given path.
3390+
3391+
*path* is a :term:`path-like object` or an open file descriptor. *mask* is a
3392+
combination of the module-level :const:`STATX_* <STATX_TYPE>` constants
3393+
specifying the information to retrieve. *flags* is a combination of the
3394+
module-level :const:`AT_STATX_* <AT_STATX_FORCE_SYNC>` constants and/or
3395+
:const:`AT_NO_AUTOMOUNT`. Returns a :class:`statx_result` object whose
3396+
:attr:`~os.statx_result.stx_mask` attribute specifies the information
3397+
actually retrieved (which may differ from *mask*).
3398+
3399+
This function supports :ref:`specifying a file descriptor <path_fd>`,
3400+
:ref:`paths relative to directory descriptors <dir_fd>`, and
3401+
:ref:`not following symlinks <follow_symlinks>`.
3402+
3403+
.. seealso:: The :manpage:`statx(2)` man page.
3404+
3405+
.. availability:: Linux >= 4.11 with glibc >= 2.28.
3406+
3407+
.. versionadded:: next
3408+
3409+
3410+
.. class:: statx_result
3411+
3412+
Information about a file returned by :func:`os.statx`.
3413+
3414+
:class:`!statx_result` has all the attributes that :class:`~stat_result` has
3415+
on Linux, making it :term:`duck-typing` compatible, but
3416+
:class:`!statx_result` is not a subclass of :class:`~stat_result` and cannot
3417+
be used as a tuple.
3418+
3419+
:class:`!statx_result` has the following additional attributes:
3420+
3421+
.. attribute:: stx_mask
3422+
3423+
Bitmask of :const:`STATX_* <STATX_TYPE>` constants specifying the
3424+
information retrieved, which may differ from what was requested.
3425+
3426+
.. attribute:: stx_attributes_mask
3427+
3428+
Bitmask of :const:`STATX_ATTR_* <stat.STATX_ATTR_COMPRESSED>` constants
3429+
specifying the attributes bits supported for this file.
3430+
3431+
.. attribute:: stx_attributes
3432+
3433+
Bitmask of :const:`STATX_ATTR_* <stat.STATX_ATTR_COMPRESSED>` constants
3434+
specifying the attributes of this file.
3435+
3436+
.. attribute:: stx_dev_major
3437+
3438+
Major number of the device on which this file resides.
3439+
3440+
.. attribute:: stx_dev_minor
3441+
3442+
Minor number of the device on which this file resides.
3443+
3444+
.. attribute:: stx_rdev_major
3445+
3446+
Major number of the device this file represents.
3447+
3448+
.. attribute:: stx_rdev_minor
3449+
3450+
Minor number of the device this file represents.
3451+
3452+
.. attribute:: stx_mnt_id
3453+
3454+
Mount ID.
3455+
3456+
.. availability:: Linux >= 4.11 with glibc >= 2.28 and build-time kernel
3457+
userspace API headers >= 5.8.
3458+
3459+
.. attribute:: stx_dio_mem_align
3460+
3461+
Direct I/O memory buffer alignment requirement.
3462+
3463+
.. availability:: Linux >= 4.11 with glibc >= 2.28 and build-time kernel
3464+
userspace API headers >= 6.1.
3465+
3466+
.. attribute:: stx_dio_offset_align
3467+
3468+
Direct I/O file offset alignment requirement.
3469+
3470+
.. availability:: Linux >= 4.11 with glibc >= 2.28 and build-time kernel
3471+
userspace API headers >= 6.1.
3472+
3473+
.. attribute:: stx_subvol
3474+
3475+
Subvolume ID.
3476+
3477+
.. availability:: Linux >= 4.11 with glibc >= 2.28 and build-time kernel
3478+
userspace API headers >= 6.10.
3479+
3480+
.. attribute:: stx_atomic_write_unit_min
3481+
3482+
Minimum size for direct I/O with torn-write protection.
3483+
3484+
.. availability:: Linux >= 4.11 with glibc >= 2.28 and build-time kernel
3485+
userspace API headers >= 6.11.
3486+
3487+
.. attribute:: stx_atomic_write_unit_max
3488+
3489+
Maximum size for direct I/O with torn-write protection.
3490+
3491+
.. availability:: Linux >= 4.11 with glibc >= 2.28 and build-time kernel
3492+
userspace API headers >= 6.11.
3493+
3494+
.. attribute:: stx_atomic_write_unit_max_opt
3495+
3496+
Maximum optimized size for direct I/O with torn-write protection.
3497+
3498+
.. availability:: Linux >= 4.11 with glibc >= 2.28 and build-time kernel
3499+
userspace API headers >= 6.11.
3500+
3501+
.. attribute:: stx_atomic_write_segments_max
3502+
3503+
Maximum iovecs for direct I/O with torn-write protection.
3504+
3505+
.. availability:: Linux >= 4.11 with glibc >= 2.28 and build-time kernel
3506+
userspace API headers >= 6.11.
3507+
3508+
.. attribute:: stx_dio_read_offset_align
3509+
3510+
Direct I/O file offset alignment requirement for reads.
3511+
3512+
.. availability:: Linux >= 4.11 with glibc >= 2.28 and build-time kernel
3513+
userspace API headers >= 6.14.
3514+
3515+
.. seealso:: The :manpage:`statx(2)` man page.
3516+
3517+
.. availability:: Linux >= 4.11 with glibc >= 2.28.
3518+
3519+
.. versionadded:: next
3520+
3521+
3522+
.. data:: STATX_TYPE
3523+
STATX_MODE
3524+
STATX_NLINK
3525+
STATX_UID
3526+
STATX_GID
3527+
STATX_ATIME
3528+
STATX_MTIME
3529+
STATX_CTIME
3530+
STATX_INO
3531+
STATX_SIZE
3532+
STATX_BLOCKS
3533+
STATX_BASIC_STATS
3534+
STATX_BTIME
3535+
STATX_MNT_ID
3536+
STATX_DIOALIGN
3537+
STATX_MNT_ID_UNIQUE
3538+
STATX_SUBVOL
3539+
STATX_WRITE_ATOMIC
3540+
STATX_DIO_READ_ALIGN
3541+
3542+
Bitflags for use in the *mask* parameter to :func:`os.statx`. Flags
3543+
including and after :const:`!STATX_MNT_ID` are only available when their
3544+
corresponding members in :class:`statx_result` are available.
3545+
3546+
.. availability:: Linux >= 4.11 with glibc >= 2.28.
3547+
3548+
.. versionadded:: next
3549+
3550+
.. data:: AT_STATX_FORCE_SYNC
3551+
3552+
A flag for the :func:`os.statx` function. Requests that the kernel return
3553+
up-to-date information even when doing so is expensive (for example,
3554+
requiring a round trip to the server for a file on a network filesystem).
3555+
3556+
.. availability:: Linux >= 4.11 with glibc >= 2.28.
3557+
3558+
.. versionadded:: next
3559+
3560+
.. data:: AT_STATX_DONT_SYNC
3561+
3562+
A flag for the :func:`os.statx` function. Requests that the kernel return
3563+
cached information if possible.
3564+
3565+
.. availability:: Linux >= 4.11 with glibc >= 2.28.
3566+
3567+
.. versionadded:: next
3568+
3569+
.. data:: AT_STATX_SYNC_AS_STAT
3570+
3571+
A flag for the :func:`os.statx` function. This flag is defined as ``0``, so
3572+
it has no effect, but it can be used to explicitly indicate neither
3573+
:data:`AT_STATX_FORCE_SYNC` nor :data:`AT_STATX_DONT_SYNC` is being passed.
3574+
In the absence of the other two flags, the kernel will generally return
3575+
information as fresh as :func:`os.stat` would return.
3576+
3577+
.. availability:: Linux >= 4.11 with glibc >= 2.28.
3578+
3579+
.. versionadded:: next
3580+
3581+
3582+
.. data:: AT_NO_AUTOMOUNT
3583+
3584+
If the final component of a path is an automount point, operate on the
3585+
automount point instead of performing the automount. On Linux,
3586+
:func:`os.stat`, :func:`os.fstat` and :func:`os.lstat` always behave this
3587+
way.
3588+
3589+
.. availability:: Linux.
3590+
3591+
.. versionadded:: next
3592+
3593+
33863594
.. function:: statvfs(path)
33873595

33883596
Perform a :c:func:`!statvfs` system call on the given path. The return value is

Doc/library/stat.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,3 +493,22 @@ constants, but are not an exhaustive list.
493493
IO_REPARSE_TAG_APPEXECLINK
494494

495495
.. versionadded:: 3.8
496+
497+
On Linux, the following file attribute constants are available for use when
498+
testing bits in the :attr:`~os.statx_result.stx_attributes` and
499+
:attr:`~os.statx_result.stx_attributes_mask` members returned by
500+
:func:`os.statx`. See the :manpage:`statx(2)` man page for more detail on the
501+
meaning of these constants.
502+
503+
.. data:: STATX_ATTR_COMPRESSED
504+
STATX_ATTR_IMMUTABLE
505+
STATX_ATTR_APPEND
506+
STATX_ATTR_NODUMP
507+
STATX_ATTR_ENCRYPTED
508+
STATX_ATTR_AUTOMOUNT
509+
STATX_ATTR_MOUNT_ROOT
510+
STATX_ATTR_VERITY
511+
STATX_ATTR_DAX
512+
STATX_ATTR_WRITE_ATOMIC
513+
514+
.. versionadded:: next

Doc/whatsnew/3.15.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,14 @@ mmap
433433
(Contributed by Serhiy Storchaka in :gh:`78502`.)
434434

435435

436+
os
437+
--
438+
439+
* Add :func:`os.statx` on Linux kernel versions 4.11 and later with
440+
glibc versions 2.28 and later.
441+
(Contributed by Jeffrey Bosboom in :gh:`83714`.)
442+
443+
436444
os.path
437445
-------
438446

Include/internal/pycore_global_objects_fini_generated.h

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_global_strings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,7 @@ struct _Py_global_strings {
590590
STRUCT_FOR_ID(loop)
591591
STRUCT_FOR_ID(manual_reset)
592592
STRUCT_FOR_ID(mapping)
593+
STRUCT_FOR_ID(mask)
593594
STRUCT_FOR_ID(match)
594595
STRUCT_FOR_ID(max_length)
595596
STRUCT_FOR_ID(maxdigits)

Include/internal/pycore_runtime_init_generated.h

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_unicodeobject_generated.h

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/os.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ def _add(str, fn):
136136
_add("HAVE_UNLINKAT", "unlink")
137137
_add("HAVE_UNLINKAT", "rmdir")
138138
_add("HAVE_UTIMENSAT", "utime")
139+
if _exists("statx"):
140+
_set.add(statx)
139141
supports_dir_fd = _set
140142

141143
_set = set()
@@ -157,6 +159,8 @@ def _add(str, fn):
157159
_add("HAVE_FPATHCONF", "pathconf")
158160
if _exists("statvfs") and _exists("fstatvfs"): # mac os x10.3
159161
_add("HAVE_FSTATVFS", "statvfs")
162+
if _exists("statx"):
163+
_set.add(statx)
160164
supports_fd = _set
161165

162166
_set = set()
@@ -195,6 +199,8 @@ def _add(str, fn):
195199
_add("HAVE_FSTATAT", "stat")
196200
_add("HAVE_UTIMENSAT", "utime")
197201
_add("MS_WINDOWS", "stat")
202+
if _exists("statx"):
203+
_set.add(statx)
198204
supports_follow_symlinks = _set
199205

200206
del _set

Lib/stat.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,21 @@ def filemode(mode):
200200
FILE_ATTRIBUTE_VIRTUAL = 65536
201201

202202

203+
# Linux STATX_ATTR constants for interpreting os.statx()'s
204+
# "stx_attributes" and "stx_attributes_mask" members
205+
206+
STATX_ATTR_COMPRESSED = 0x00000004
207+
STATX_ATTR_IMMUTABLE = 0x00000010
208+
STATX_ATTR_APPEND = 0x00000020
209+
STATX_ATTR_NODUMP = 0x00000040
210+
STATX_ATTR_ENCRYPTED = 0x00000800
211+
STATX_ATTR_AUTOMOUNT = 0x00001000
212+
STATX_ATTR_MOUNT_ROOT = 0x00002000
213+
STATX_ATTR_VERITY = 0x00100000
214+
STATX_ATTR_DAX = 0x00200000
215+
STATX_ATTR_WRITE_ATOMIC = 0x00400000
216+
217+
203218
# If available, use C implementation
204219
try:
205220
from _stat import *

0 commit comments

Comments
 (0)