File: tst-stat.c

package info (click to toggle)
glibc 2.42-13
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 312,844 kB
  • sloc: ansic: 1,061,027; asm: 238,416; makefile: 20,970; python: 13,509; sh: 11,828; cpp: 5,188; awk: 1,794; perl: 317; yacc: 292; pascal: 182; sed: 19
file content (137 lines) | stat: -rw-r--r-- 4,124 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* Basic tests for stat, lstat, fstat, and fstatat.
   Copyright (C) 2021-2025 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

#include <array_length.h>
#include <errno.h>
#include <fcntl.h>
#include <support/check.h>
#include <support/support.h>
#include <support/temp_file.h>
#include <support/xunistd.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

static void
stat_check (int fd, const char *path, struct stat *st)
{
  TEST_COMPARE (stat (path, st), 0);
}

static void
lstat_check (int fd, const char *path, struct stat *st)
{
  TEST_COMPARE (lstat (path, st), 0);
}

static void
fstat_check (int fd, const char *path, struct stat *st)
{
  /* Test for invalid fstat input (BZ #27559).  */
  TEST_COMPARE (fstat (AT_FDCWD, st), -1);
  TEST_COMPARE (errno, EBADF);

  TEST_COMPARE (fstat (fd, st), 0);
}

static void
fstatat_check (int fd, const char *path, struct stat *st)
{
  TEST_COMPARE (fstatat (fd, "", st, 0), -1);
  TEST_COMPARE (errno, ENOENT);

  TEST_COMPARE (fstatat (AT_FDCWD, "_non_existing_file", st, 0), -1);
  TEST_COMPARE (errno, ENOENT);

  TEST_COMPARE (fstatat (fd, path, st, 0), 0);
}

static void
fstatat_link (const char *path, struct stat *st)
{
  TEST_COMPARE (fstatat (AT_FDCWD, path, st, 0), -1);
  TEST_COMPARE (errno, ENOENT);

  TEST_COMPARE (fstatat (AT_FDCWD, path, st, AT_SYMLINK_NOFOLLOW), 0);
  TEST_COMPARE (!S_ISLNK(st->st_mode), 0);
}

typedef void (*test_t)(int, const char *path, struct stat *);

static int
do_test (void)
{
  char *path;
  char *tempdir = support_create_temp_directory ("tst-stat-");
  char *linkname = xasprintf ("%s/tst-fstat.linkname", tempdir);
  int fd = create_temp_file ("tst-fstat.", &path);
  TEST_VERIFY_EXIT (fd >= 0);
  support_write_file_string (path, "abc");

  /* This should help to prevent delayed allocation, which may result
     in a spurious stx_blocks/st_blocks difference.  */
  fsync (fd);

  bool check_ns = support_stat_nanoseconds (path);
  if (!check_ns)
    printf ("warning: timestamp with nanoseconds not supported\n");

  struct statx stx;
  struct stat st;
  TEST_COMPARE (statx (fd, path, 0, STATX_BASIC_STATS, &stx), 0);

  test_t tests[] = { stat_check, lstat_check, fstat_check, fstatat_check };

  for (int i = 0; i < array_length (tests); i++)
    {
      tests[i](fd, path, &st);

      TEST_COMPARE (stx.stx_dev_major, major (st.st_dev));
      TEST_COMPARE (stx.stx_dev_minor, minor (st.st_dev));
      TEST_COMPARE (stx.stx_ino, st.st_ino);
      TEST_COMPARE (stx.stx_mode, st.st_mode);
      TEST_COMPARE (stx.stx_nlink, st.st_nlink);
      TEST_COMPARE (stx.stx_uid, st.st_uid);
      TEST_COMPARE (stx.stx_gid, st.st_gid);
      TEST_COMPARE (stx.stx_rdev_major, major (st.st_rdev));
      TEST_COMPARE (stx.stx_rdev_minor, minor (st.st_rdev));
      TEST_COMPARE (stx.stx_blksize, st.st_blksize);
      TEST_COMPARE (stx.stx_blocks, st.st_blocks);

      TEST_COMPARE (stx.stx_ctime.tv_sec, st.st_ctim.tv_sec);
      TEST_COMPARE (stx.stx_mtime.tv_sec, st.st_mtim.tv_sec);
      if (check_ns)
	{
	  TEST_COMPARE (stx.stx_ctime.tv_nsec, st.st_ctim.tv_nsec);
	  TEST_COMPARE (stx.stx_mtime.tv_nsec, st.st_mtim.tv_nsec);
	}
    }

  TEST_COMPARE (symlink ("tst-fstat.target", linkname), 0);
  add_temp_file (linkname);
  fstatat_link (linkname, &st);

  free (linkname);
  free (tempdir);

  return 0;
}

#include <support/test-driver.c>