(root)/
glibc-2.38/
io/
tst-stat.c
       1  /* Basic tests for stat, lstat, fstat, and fstatat.
       2     Copyright (C) 2021-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 <array_length.h>
      20  #include <errno.h>
      21  #include <fcntl.h>
      22  #include <support/check.h>
      23  #include <support/support.h>
      24  #include <support/temp_file.h>
      25  #include <support/xunistd.h>
      26  #include <sys/stat.h>
      27  #include <sys/sysmacros.h>
      28  #include <stdio.h>
      29  #include <unistd.h>
      30  
      31  static void
      32  stat_check (int fd, const char *path, struct stat *st)
      33  {
      34    TEST_COMPARE (stat (path, st), 0);
      35  }
      36  
      37  static void
      38  lstat_check (int fd, const char *path, struct stat *st)
      39  {
      40    TEST_COMPARE (lstat (path, st), 0);
      41  }
      42  
      43  static void
      44  fstat_check (int fd, const char *path, struct stat *st)
      45  {
      46    /* Test for invalid fstat input (BZ #27559).  */
      47    TEST_COMPARE (fstat (AT_FDCWD, st), -1);
      48    TEST_COMPARE (errno, EBADF);
      49  
      50    TEST_COMPARE (fstat (fd, st), 0);
      51  }
      52  
      53  static void
      54  fstatat_check (int fd, const char *path, struct stat *st)
      55  {
      56    TEST_COMPARE (fstatat (fd, "", st, 0), -1);
      57    TEST_COMPARE (errno, ENOENT);
      58  
      59    TEST_COMPARE (fstatat (fd, path, st, 0), 0);
      60  }
      61  
      62  typedef void (*test_t)(int, const char *path, struct stat *);
      63  
      64  static int
      65  do_test (void)
      66  {
      67    char *path;
      68    int fd = create_temp_file ("tst-fstat.", &path);
      69    TEST_VERIFY_EXIT (fd >= 0);
      70    support_write_file_string (path, "abc");
      71  
      72    /* This should help to prevent delayed allocation, which may result
      73       in a spurious stx_blocks/st_blocks difference.  */
      74    fsync (fd);
      75  
      76    bool check_ns = support_stat_nanoseconds (path);
      77    if (!check_ns)
      78      printf ("warning: timestamp with nanoseconds not supported\n");
      79  
      80    struct statx stx;
      81    TEST_COMPARE (statx (fd, path, 0, STATX_BASIC_STATS, &stx), 0);
      82  
      83    test_t tests[] = { stat_check, lstat_check, fstat_check, fstatat_check };
      84  
      85    for (int i = 0; i < array_length (tests); i++)
      86      {
      87        struct stat st;
      88        tests[i](fd, path, &st);
      89  
      90        TEST_COMPARE (stx.stx_dev_major, major (st.st_dev));
      91        TEST_COMPARE (stx.stx_dev_minor, minor (st.st_dev));
      92        TEST_COMPARE (stx.stx_ino, st.st_ino);
      93        TEST_COMPARE (stx.stx_mode, st.st_mode);
      94        TEST_COMPARE (stx.stx_nlink, st.st_nlink);
      95        TEST_COMPARE (stx.stx_uid, st.st_uid);
      96        TEST_COMPARE (stx.stx_gid, st.st_gid);
      97        TEST_COMPARE (stx.stx_rdev_major, major (st.st_rdev));
      98        TEST_COMPARE (stx.stx_rdev_minor, minor (st.st_rdev));
      99        TEST_COMPARE (stx.stx_blksize, st.st_blksize);
     100        TEST_COMPARE (stx.stx_blocks, st.st_blocks);
     101  
     102        TEST_COMPARE (stx.stx_ctime.tv_sec, st.st_ctim.tv_sec);
     103        TEST_COMPARE (stx.stx_mtime.tv_sec, st.st_mtim.tv_sec);
     104        if (check_ns)
     105  	{
     106  	  TEST_COMPARE (stx.stx_ctime.tv_nsec, st.st_ctim.tv_nsec);
     107  	  TEST_COMPARE (stx.stx_mtime.tv_nsec, st.st_mtim.tv_nsec);
     108  	}
     109      }
     110  
     111    return 0;
     112  }
     113  
     114  #include <support/test-driver.c>