Skip to content

Commit c2e6f81

Browse files
committed
Implement os.statx
1 parent ac5c5d4 commit c2e6f81

17 files changed

+918
-64
lines changed

Doc/library/os.rst

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

33853385

3386+
.. function:: statx(path, mask, *, dir_fd=None, follow_symlinks=True, sync=None)
3387+
3388+
Get the status of a file or file descriptor by performing a :c:func:`!statx`
3389+
system call on the given path. *path* may be specified as either a string or
3390+
bytes -- directly or indirectly through the :class:`PathLike` interface --
3391+
or as an open file descriptor. *mask* is a combination of the module-level
3392+
:const:`STATX_* <STATX_TYPE>` constants specifying the information to
3393+
retrieve. Returns a :class:`statx_result` object whose
3394+
:attr:`~os.statx_result.stx_mask` attribute specifies the information
3395+
actually retrieved (which may differ from *mask*).
3396+
3397+
The optional parameter *sync* controls the freshness of the returned
3398+
information. ``sync=True`` requests that the kernel return up-to-date
3399+
information, even when doing so is expensive (for example, requiring a
3400+
round trip to the server for a file on a network filesystem).
3401+
``sync=False`` requests that the kernel return cached information if
3402+
available. ``sync=None`` expresses no preference, in which case the kernel
3403+
will return information as fresh as :func:`~os.stat` does.
3404+
3405+
This function supports :ref:`specifying a file descriptor <path_fd>`,
3406+
:ref:`paths relative to directory descriptors <dir_fd>`, and
3407+
:ref:`not following symlinks <follow_symlinks>`.
3408+
3409+
.. seealso:: The :manpage:`statx(2)` man page.
3410+
3411+
.. availability:: Linux >= 4.11 with glibc >= 2.28.
3412+
3413+
.. versionadded:: next
3414+
3415+
3416+
.. class:: statx_result
3417+
3418+
Object whose attributes correspond roughly to the members of the
3419+
:c:struct:`!statx` structure. It is used for the result of :func:`os.statx`.
3420+
:class:`!statx_result` has all of the attributes of :class:`stat_result`
3421+
available on Linux, but is not a subclass of :class:`stat_result` nor a
3422+
tuple. :class:`!statx_result` has the following additional attributes:
3423+
3424+
.. attribute:: stx_mask
3425+
3426+
Bitmask of :const:`STATX_* <STATX_TYPE>` constants specifying the
3427+
information retrieved, which may differ from what was requested depending
3428+
on the filesystem, filesystem type, and kernel version. All attributes
3429+
of this class are accessible regardless of the value of
3430+
:attr:`!stx_mask`, and they may have useful fictitious values. For
3431+
example, for a file on a network filesystem, :const:`STATX_UID` and
3432+
:const:`STATX_GID` may be unset because file ownership on the server is
3433+
based on an external user database, but :attr:`!st_uid` and
3434+
:attr:`!st_gid` may contain the IDs of the local user who controls the
3435+
mount.
3436+
3437+
.. attribute:: stx_attributes_mask
3438+
3439+
Bitmask of :const:`!STATX_ATTR_* <stat.STATX_ATTR_COMPRESSED>` constants
3440+
specifying the attributes bits supported for this file.
3441+
3442+
.. attribute:: stx_attributes
3443+
3444+
Bitmask of :const:`!STATX_ATTR_* <stat.STATX_ATTR_COMPRESSED>` constants
3445+
specifying the attributes of this file.
3446+
3447+
.. attribute:: stx_mnt_id
3448+
3449+
Mount ID.
3450+
3451+
.. attribute:: stx_dio_mem_align
3452+
3453+
Direct I/O memory buffer alignment requirement.
3454+
3455+
.. attribute:: stx_dio_offset_align
3456+
3457+
Direct I/O file offset alignment requirement.
3458+
3459+
.. attribute:: stx_subvol
3460+
3461+
Subvolume ID.
3462+
3463+
.. attribute:: stx_atomic_write_unit_min
3464+
3465+
Minimum size for direct I/O with torn-write protection.
3466+
3467+
.. attribute:: stx_atomic_write_unit_max
3468+
3469+
Maximum size for direct I/O with torn-write protection.
3470+
3471+
.. attribute:: stx_atomic_write_segments_max
3472+
3473+
Maximum iovecs for direct I/O with torn-write protection.
3474+
3475+
.. attribute:: stx_dio_read_offset_align
3476+
3477+
Direct I/O file offset alignment requirement for reads.
3478+
3479+
.. attribute:: stx_atomic_write_unit_max_opt
3480+
3481+
Maximum optimized size for direct I/O with torn-write protection.
3482+
3483+
.. seealso:: The :manpage:`statx(2)` man page.
3484+
3485+
.. availability:: Linux >= 4.11 with glibc >= 2.28.
3486+
3487+
.. versionadded:: next
3488+
3489+
3490+
.. data:: STATX_TYPE
3491+
STATX_MODE
3492+
STATX_NLINK
3493+
STATX_UID
3494+
STATX_GID
3495+
STATX_ATIME
3496+
STATX_MTIME
3497+
STATX_CTIME
3498+
STATX_INO
3499+
STATX_SIZE
3500+
STATX_BLOCKS
3501+
STATX_BASIC_STATS
3502+
STATX_BTIME
3503+
STATX_MNT_ID
3504+
STATX_DIOALIGN
3505+
STATX_MNT_ID_UNIQUE
3506+
STATX_SUBVOL
3507+
STATX_WRITE_ATOMIC
3508+
STATX_DIO_READ_ALIGN
3509+
3510+
Bitflags for use as the *mask* parameter to :func:`os.statx`.
3511+
3512+
.. availability:: Linux >= 4.11 with glibc >= 2.28.
3513+
3514+
.. versionadded:: next
3515+
3516+
33863517
.. function:: statvfs(path)
33873518

33883519
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

Include/internal/pycore_global_objects_fini_generated.h

Lines changed: 2 additions & 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: 2 additions & 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)
@@ -784,6 +785,7 @@ struct _Py_global_strings {
784785
STRUCT_FOR_ID(sub_key)
785786
STRUCT_FOR_ID(subcalls)
786787
STRUCT_FOR_ID(symmetric_difference_update)
788+
STRUCT_FOR_ID(sync)
787789
STRUCT_FOR_ID(tabsize)
788790
STRUCT_FOR_ID(tag)
789791
STRUCT_FOR_ID(target)

Include/internal/pycore_runtime_init_generated.h

Lines changed: 2 additions & 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: 8 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
@@ -131,6 +131,8 @@ def _add(str, fn):
131131
_add("HAVE_UNLINKAT", "unlink")
132132
_add("HAVE_UNLINKAT", "rmdir")
133133
_add("HAVE_UTIMENSAT", "utime")
134+
if _exists("statx"):
135+
_set.add(statx)
134136
supports_dir_fd = _set
135137

136138
_set = set()
@@ -152,6 +154,8 @@ def _add(str, fn):
152154
_add("HAVE_FPATHCONF", "pathconf")
153155
if _exists("statvfs") and _exists("fstatvfs"): # mac os x10.3
154156
_add("HAVE_FSTATVFS", "statvfs")
157+
if _exists("statx"):
158+
_set.add(statx)
155159
supports_fd = _set
156160

157161
_set = set()
@@ -190,6 +194,8 @@ def _add(str, fn):
190194
_add("HAVE_FSTATAT", "stat")
191195
_add("HAVE_UTIMENSAT", "utime")
192196
_add("MS_WINDOWS", "stat")
197+
if _exists("statx"):
198+
_set.add(statx)
193199
supports_follow_symlinks = _set
194200

195201
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)