linux: fix ntp_gettime abi break (BZ# 30156)
[glibc.git] / io / test-lfs.c
blob3eca256935651cb6714abe942ff92259c1274cc9
1 /* Some basic tests for LFS.
2 Copyright (C) 2000-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 #include <unistd.h>
20 #include <stdlib.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <error.h>
25 #include <errno.h>
26 #include <sys/resource.h>
27 #include <support/check.h>
29 /* Prototype for our test function. */
30 extern void do_prepare (int argc, char *argv[]);
31 extern int do_test (int argc, char *argv[]);
33 /* We have a preparation function. */
34 #define PREPARE do_prepare
36 /* This defines the `main' function and some more. */
37 #include <test-skeleton.c>
39 /* These are for the temporary file we generate. */
40 char *name;
41 int fd;
43 /* 2^31 = 2GB. */
44 #define TWO_GB 2147483648LL
46 void
47 do_prepare (int argc, char *argv[])
49 size_t name_len;
50 struct rlimit64 rlim;
52 name_len = strlen (test_dir);
53 name = xmalloc (name_len + sizeof ("/lfsXXXXXX"));
54 mempcpy (mempcpy (name, test_dir, name_len),
55 "/lfsXXXXXX", sizeof ("/lfsXXXXXX"));
57 /* Open our test file. */
58 fd = mkstemp64 (name);
59 if (fd == -1)
61 if (errno == ENOSYS)
63 /* Fail silently. */
64 error (0, 0, "open64 is not supported");
65 exit (EXIT_SUCCESS);
67 else
68 error (EXIT_FAILURE, errno, "cannot create temporary file");
70 if (!support_descriptor_supports_holes (fd))
71 FAIL_UNSUPPORTED ("File %s does not support holes", name);
72 add_temp_file (name);
74 if (getrlimit64 (RLIMIT_FSIZE, &rlim) != 0)
76 error (0, errno, "cannot get resource limit");
77 exit (0);
79 if (rlim.rlim_cur < TWO_GB + 200)
81 rlim.rlim_cur = TWO_GB + 200;
82 if (setrlimit64 (RLIMIT_FSIZE, &rlim) != 0)
84 error (0, errno, "cannot reset file size limits");
85 exit (0);
90 static void
91 test_ftello (void)
93 FILE *f;
94 int ret;
95 off64_t pos;
97 f = fopen64 (name, "w");
99 ret = fseeko64 (f, TWO_GB+100, SEEK_SET);
100 if (ret == -1 && errno == ENOSYS)
102 error (0, 0, "fseeko64 is not supported.");
103 exit (EXIT_SUCCESS);
105 if (ret == -1 && errno == EINVAL)
107 error (0, 0, "LFS seems not to be supported");
108 exit (EXIT_SUCCESS);
110 if (ret == -1)
112 error (0, errno, "fseeko64 failed with error");
113 exit (EXIT_FAILURE);
116 ret = fwrite ("Hello", 1, 5, f);
117 if (ret == -1 && errno == EFBIG)
119 error (0, errno, "LFS seems not to be supported");
120 exit (EXIT_SUCCESS);
123 if (ret == -1 && errno == ENOSPC)
125 error (0, 0, "Not enough space to write file.");
126 exit (EXIT_SUCCESS);
129 if (ret != 5)
130 error (EXIT_FAILURE, errno, "Cannot write test string to large file");
132 pos = ftello64 (f);
134 if (pos != TWO_GB+105)
136 error (0, 0, "ftello64 gives wrong result.");
137 exit (EXIT_FAILURE);
140 fclose (f);
144 do_test (int argc, char *argv[])
146 int ret, fd2;
147 struct stat64 statbuf;
149 ret = lseek64 (fd, TWO_GB+100, SEEK_SET);
150 if (ret == -1 && errno == ENOSYS)
152 error (0, 0, "lseek64 is not supported.");
153 exit (EXIT_SUCCESS);
155 if (ret == -1 && errno == EINVAL)
157 error (0, 0, "LFS seems not to be supported.");
158 exit (EXIT_SUCCESS);
160 if (ret == -1)
162 error (0, errno, "lseek64 failed with error");
163 exit (EXIT_FAILURE);
165 off64_t offset64 = lseek64 (fd, 0, SEEK_CUR);
166 if (offset64 != TWO_GB + 100)
168 error (0, 0, "lseek64 did not return expected offset");
169 exit (EXIT_FAILURE);
171 off_t offset = lseek (fd, 0, SEEK_CUR);
172 if (sizeof (off_t) < sizeof (off64_t))
174 if (offset != -1 || errno != EOVERFLOW)
176 error (0, 0, "lseek did not fail with EOVERFLOW");
177 exit (EXIT_FAILURE);
180 else
181 if (offset != TWO_GB + 100)
183 error (0, 0, "lseek did not return expected offset");
184 exit (EXIT_FAILURE);
187 ret = write (fd, "Hello", 5);
188 if (ret == -1 && errno == EFBIG)
190 error (0, 0, "LFS seems not to be supported.");
191 exit (EXIT_SUCCESS);
194 if (ret == -1 && errno == ENOSPC)
196 error (0, 0, "Not enough space to write file.");
197 exit (EXIT_SUCCESS);
200 if (ret != 5)
201 error (EXIT_FAILURE, errno, "cannot write test string to large file");
203 ret = close (fd);
205 if (ret == -1)
206 error (EXIT_FAILURE, errno, "error closing file");
208 ret = stat64 (name, &statbuf);
210 if (ret == -1 && (errno == ENOSYS || errno == EOVERFLOW))
211 error (0, 0, "stat64 is not supported.");
212 else if (ret == -1)
213 error (EXIT_FAILURE, errno, "cannot stat file `%s'", name);
214 else if (statbuf.st_size != (TWO_GB + 100 + 5))
215 error (EXIT_FAILURE, 0, "stat reported size %lld instead of %lld.",
216 (long long int) statbuf.st_size, (TWO_GB + 100 + 5));
218 fd2 = openat64 (AT_FDCWD, name, O_RDWR);
219 if (fd2 == -1)
221 if (errno == ENOSYS)
223 /* Silently ignore this test. */
224 error (0, 0, "openat64 is not supported");
226 else
227 error (EXIT_FAILURE, errno, "openat64 failed to open big file");
229 else
231 ret = close (fd2);
233 if (ret == -1)
234 error (EXIT_FAILURE, errno, "error closing file");
237 test_ftello ();
239 return 0;