(root)/
glibc-2.38/
io/
statx_generic.c
       1  /* Generic implementation of statx based on fstatat64.
       2     Copyright (C) 2018-2023 Free Software Foundation, Inc.
       3     This file is part of the GNU C Library.
       4  
       5     The GNU C Library is free software; you can redistribute it and/or
       6     modify it under the terms of the GNU Lesser General Public
       7     License as published by the Free Software Foundation; either
       8     version 2.1 of the License, or (at your option) any later version.
       9  
      10     The GNU C Library is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13     Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public
      16     License along with the GNU C Library; if not, see
      17     <https://www.gnu.org/licenses/>.  */
      18  
      19  #include <errno.h>
      20  #include <fcntl.h>
      21  #include <string.h>
      22  #include <sys/stat.h>
      23  #include <sys/sysmacros.h>
      24  
      25  /* Obtain the original definition of struct statx.  */
      26  #undef __statx_defined
      27  #define statx original_statx
      28  #include <bits/types/struct_statx.h>
      29  #undef statx
      30  
      31  static inline struct statx_timestamp
      32  statx_convert_timestamp (struct timespec tv)
      33  {
      34    return (struct statx_timestamp) { tv.tv_sec, tv.tv_nsec };
      35  }
      36  
      37  /* Approximate emulation of statx.  This will always fill in
      38     POSIX-mandated attributes even if the underlying file system does
      39     not actually support it (for example, GID and UID on file systems
      40     without UNIX-style permissions).  */
      41  static __attribute__ ((unused)) int
      42  statx_generic (int fd, const char *path, int flags,
      43                 unsigned int mask, struct statx *buf)
      44  {
      45    /* Flags which need to be cleared before passing them to
      46       fstatat64.  */
      47    static const int clear_flags = AT_STATX_SYNC_AS_STAT;
      48  
      49      /* Flags supported by our emulation.  */
      50    static const int supported_flags
      51      = AT_EMPTY_PATH | AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW | clear_flags;
      52  
      53    if (__glibc_unlikely ((flags & ~supported_flags) != 0))
      54      {
      55        __set_errno (EINVAL);
      56        return -1;
      57      }
      58  
      59    struct stat64 st;
      60    int ret = __fstatat64 (fd, path, &st, flags & ~clear_flags);
      61    if (ret != 0)
      62      return ret;
      63  
      64    /* The interface is defined in such a way that unused (padding)
      65       fields have to be cleared.  STATX_BASIC_STATS corresponds to the
      66       data which is available via fstatat64.  */
      67    struct original_statx obuf =
      68      {
      69        .stx_mask = STATX_BASIC_STATS,
      70        .stx_blksize = st.st_blksize,
      71        .stx_nlink = st.st_nlink,
      72        .stx_uid = st.st_uid,
      73        .stx_gid = st.st_gid,
      74        .stx_mode = st.st_mode,
      75        .stx_ino = st.st_ino,
      76        .stx_size = st.st_size,
      77        .stx_blocks = st.st_blocks,
      78        .stx_atime = statx_convert_timestamp (st.st_atim),
      79        .stx_ctime = statx_convert_timestamp (st.st_ctim),
      80        .stx_mtime = statx_convert_timestamp (st.st_mtim),
      81        .stx_rdev_major = major (st.st_rdev),
      82        .stx_rdev_minor = minor (st.st_rdev),
      83        .stx_dev_major = major (st.st_dev),
      84        .stx_dev_minor = minor (st.st_dev),
      85      };
      86    _Static_assert (sizeof (*buf) >= sizeof (obuf), "struct statx size");
      87    memcpy (buf, &obuf, sizeof (obuf));
      88  
      89    return 0;
      90  }