aboutsummaryrefslogtreecommitdiff
path: root/io/tst-statx.c
blob: 356a069762fa5273659363e934616613d1e9367c (plain)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* Basic test of statx system call.
   Copyright (C) 2018-2019 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
   <http://www.gnu.org/licenses/>.  */

#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <support/check.h>
#include <support/support.h>
#include <support/temp_file.h>
#include <support/xunistd.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/sysmacros.h>
#include <unistd.h>

/* Ensure that the types have the kernel-expected layout.  */
_Static_assert (sizeof (struct statx_timestamp) == 16, "statx_timestamp size");
_Static_assert (sizeof (struct statx) == 256, "statx size");
_Static_assert (offsetof (struct statx, stx_nlink) == 16, "statx nlink");
_Static_assert (offsetof (struct statx, stx_ino) == 32, "statx ino");
_Static_assert (offsetof (struct statx, stx_atime) == 64, "statx atime");
_Static_assert (offsetof (struct statx, stx_rdev_major) == 128, "statx rdev");
_Static_assert (offsetof (struct statx, __statx_pad2) == 144, "statx pad2");

#include "statx_generic.c"

typedef int (*statx_function) (int, const char *, int, unsigned int,
                               struct statx *);

/* Return true if we have a real implementation of statx.  */
static bool
kernel_supports_statx (void)
{
#ifdef __NR_statx
  struct statx buf;
  return syscall (__NR_statx, 0, "", AT_EMPTY_PATH, 0, &buf) == 0
    || errno != ENOSYS;
#else
  return false;
#endif
}

/* Tests which apply to both implementations.  */
static void
both_implementations_tests (statx_function impl, const char *path, int fd)
{
  uint64_t ino;
  {
    struct statx buf = { 0, };
    TEST_COMPARE (statx (fd, "", AT_EMPTY_PATH, STATX_BASIC_STATS, &buf), 0);
    TEST_COMPARE (buf.stx_size, 3);
    ino = buf.stx_ino;
  }
  {
    struct statx buf = { 0, };
    TEST_COMPARE (statx (AT_FDCWD, path, 0, STATX_BASIC_STATS, &buf), 0);
    TEST_COMPARE (buf.stx_size, 3);
    TEST_COMPARE (buf.stx_ino, ino);
  }
  {
    struct statx stx = { 0, };
    TEST_COMPARE (statx (fd, "", AT_EMPTY_PATH, STATX_BASIC_STATS, &stx), 0);
    struct stat64 st;
    xfstat (fd, &st);
    TEST_COMPARE (stx.stx_mode, st.st_mode);
    TEST_COMPARE (stx.stx_dev_major, major (st.st_dev));
    TEST_COMPARE (stx.stx_dev_minor, minor (st.st_dev));
  }
  {
    struct statx stx = { 0, };
    TEST_COMPARE (statx (AT_FDCWD, "/dev/null", 0, STATX_BASIC_STATS, &stx),
                  0);
    struct stat64 st;
    xstat ("/dev/null", &st);
    TEST_COMPARE (stx.stx_mode, st.st_mode);
    TEST_COMPARE (stx.stx_dev_major, major (st.st_dev));
    TEST_COMPARE (stx.stx_dev_minor, minor (st.st_dev));
    TEST_COMPARE (stx.stx_rdev_major, major (st.st_rdev));
    TEST_COMPARE (stx.stx_rdev_minor, minor (st.st_rdev));
  }
}

/* Tests which apply only to the non-kernel (generic)
   implementation.  */
static void
non_kernel_tests (statx_function impl, int fd)
{
  /* The non-kernel implementation must always fail for explicit sync
     flags.  */
  struct statx buf;
  errno = 0;
  TEST_COMPARE (impl (fd, "", AT_EMPTY_PATH | AT_STATX_FORCE_SYNC,
                      STATX_BASIC_STATS, &buf), -1);
  TEST_COMPARE (errno, EINVAL);
  errno = 0;
  TEST_COMPARE (impl (fd, "", AT_EMPTY_PATH | AT_STATX_DONT_SYNC,
                      STATX_BASIC_STATS, &buf), -1);
  TEST_COMPARE (errno, EINVAL);
}

static int
do_test (void)
{
  char *path;
  int fd = create_temp_file ("tst-statx-", &path);
  TEST_VERIFY_EXIT (fd >= 0);
  support_write_file_string (path, "abc");

  both_implementations_tests (&statx, path, fd);
  both_implementations_tests (&statx_generic, path, fd);

  if (kernel_supports_statx ())
    {
      puts ("info: kernel supports statx");
      struct statx buf;
      buf.stx_size = 0;
      TEST_COMPARE (statx (fd, "", AT_EMPTY_PATH | AT_STATX_FORCE_SYNC,
                           STATX_BASIC_STATS, &buf),
                    0);
      TEST_COMPARE (buf.stx_size, 3);
      buf.stx_size = 0;
      TEST_COMPARE (statx (fd, "", AT_EMPTY_PATH | AT_STATX_DONT_SYNC,
                           STATX_BASIC_STATS, &buf),
                    0);
      TEST_COMPARE (buf.stx_size, 3);
    }
  else
    {
      puts ("info: kernel does not support statx");
      non_kernel_tests (&statx, fd);
    }
  non_kernel_tests (&statx_generic, fd);

  xclose (fd);
  free (path);

  return 0;
}

#include <support/test-driver.c>