statx(2) | System Calls Manual | statx(2) |
statx - get file status (extended)
Standard C library (libc, -lc)
#define _GNU_SOURCE /* See feature_test_macros(7) */ #include <fcntl.h> /* Definition of AT_* constants */ #include <sys/stat.h>
int statx(int dirfd, const char *restrict pathname, int flags, unsigned int mask, struct statx *restrict statxbuf);
This function returns information about a file, storing it in the buffer pointed to by statxbuf. The returned buffer is a structure of the following type:
struct statx { __u32 stx_mask; /* Mask of bits indicating filled fields */ __u32 stx_blksize; /* Block size for filesystem I/O */ __u64 stx_attributes; /* Extra file attribute indicators */ __u32 stx_nlink; /* Number of hard links */ __u32 stx_uid; /* User ID of owner */ __u32 stx_gid; /* Group ID of owner */ __u16 stx_mode; /* File type and mode */ __u64 stx_ino; /* Inode number */ __u64 stx_size; /* Total size in bytes */ __u64 stx_blocks; /* Number of 512B blocks allocated */ __u64 stx_attributes_mask; /* Mask to show what's supported in stx_attributes */ /* The following fields are file timestamps */ struct statx_timestamp stx_atime; /* Last access */ struct statx_timestamp stx_btime; /* Creation */ struct statx_timestamp stx_ctime; /* Last status change */ struct statx_timestamp stx_mtime; /* Last modification */ /* If this file represents a device, then the next two fields contain the ID of the device */ __u32 stx_rdev_major; /* Major ID */ __u32 stx_rdev_minor; /* Minor ID */ /* The next two fields contain the ID of the device containing the filesystem where the file resides */ __u32 stx_dev_major; /* Major ID */ __u32 stx_dev_minor; /* Minor ID */ __u64 stx_mnt_id; /* Mount ID */ /* Direct I/O alignment restrictions */ __u32 stx_dio_mem_align; __u32 stx_dio_offset_align; };
The file timestamps are structures of the following type:
struct statx_timestamp { __s64 tv_sec; /* Seconds since the Epoch (UNIX time) */ __u32 tv_nsec; /* Nanoseconds since tv_sec */ };
(Note that reserved space and padding is omitted.)
To access a file's status, no permissions are required on the file itself, but in the case of statx() with a pathname, execute (search) permission is required on all of the directories in pathname that lead to the file.
statx() uses pathname, dirfd, and flags to identify the target file in one of the following ways:
flags can be used to influence a pathname-based lookup. A value for flags is constructed by ORing together zero or more of the following constants:
flags can also be used to control what sort of synchronization the kernel will do when querying a file on a remote filesystem. This is done by ORing in one of the following values:
The mask argument to statx() is used to tell the kernel which fields the caller is interested in. mask is an ORed combination of the following constants:
STATX_TYPE | Want stx_mode & S_IFMT |
STATX_MODE | Want stx_mode & ~S_IFMT |
STATX_NLINK | Want stx_nlink |
STATX_UID | Want stx_uid |
STATX_GID | Want stx_gid |
STATX_ATIME | Want stx_atime |
STATX_MTIME | Want stx_mtime |
STATX_CTIME | Want stx_ctime |
STATX_INO | Want stx_ino |
STATX_SIZE | Want stx_size |
STATX_BLOCKS | Want stx_blocks |
STATX_BASIC_STATS | [All of the above] |
STATX_BTIME | Want stx_btime |
STATX_ALL | The same as STATX_BASIC_STATS | STATX_BTIME. |
It is deprecated and should not be used. | |
STATX_MNT_ID | Want stx_mnt_id (since Linux 5.8) |
STATX_DIOALIGN | Want stx_dio_mem_align and stx_dio_offset_align |
(since Linux 6.1; support varies by filesystem) |
Note that, in general, the kernel does not reject values in mask other than the above. (For an exception, see EINVAL in errors.) Instead, it simply informs the caller which values are supported by this kernel and filesystem via the statx.stx_mask field. Therefore, do not simply set mask to UINT_MAX (all bits set), as one or more bits may, in the future, be used to specify an extension to the buffer.
The status information for the target file is returned in the statx structure pointed to by statxbuf. Included in this is stx_mask which indicates what other information has been returned. stx_mask has the same format as the mask argument and bits are set in it to indicate which fields have been filled in.
It should be noted that the kernel may return fields that weren't requested and may fail to return fields that were requested, depending on what the backing filesystem supports. (Fields that are given values despite being unrequested can just be ignored.) In either case, stx_mask will not be equal mask.
If a filesystem does not support a field or if it has an unrepresentable value (for instance, a file with an exotic type), then the mask bit corresponding to that field will be cleared in stx_mask even if the user asked for it and a dummy value will be filled in for compatibility purposes if one is available (e.g., a dummy UID and GID may be specified to mount under some circumstances).
A filesystem may also fill in fields that the caller didn't ask for if it has values for them available and the information is available at no extra cost. If this happens, the corresponding bits will be set in stx_mask.
Note: for performance and simplicity reasons, different fields in the statx structure may contain state information from different moments during the execution of the system call. For example, if stx_mode or stx_uid is changed by another process by calling chmod(2) or chown(2), stat() might return the old stx_mode together with the new stx_uid, or the old stx_uid together with the new stx_mode.
Apart from stx_mask (which is described above), the fields in the statx structure are:
For further information on the above fields, see inode(7).
The stx_attributes field contains a set of ORed flags that indicate additional attributes of the file. Note that any attribute that is not indicated as supported by stx_attributes_mask has no usable value here. The bits in stx_attributes_mask correspond bit-by-bit to stx_attributes.
The flags are as follows:
On success, zero is returned. On error, -1 is returned, and errno is set to indicate the error.
Linux.
Linux 4.11, glibc 2.28.
ls(1), stat(1), access(2), chmod(2), chown(2), name_to_handle_at(2), readlink(2), stat(2), utime(2), proc(5), capabilities(7), inode(7), symlink(7)
2023-10-31 | Linux man-pages 6.7 |