]>
Commit | Line | Data |
---|---|---|
4c524b81 | 1 | /* Some basic tests for LFS. |
04277e02 | 2 | Copyright (C) 2000-2019 Free Software Foundation, Inc. |
41bdb6e2 | 3 | This file is part of the GNU C Library. |
4c524b81 AJ |
4 | Contributed by Andreas Jaeger <aj@suse.de>, 2000. |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
7 | modify it under the terms of the GNU Lesser General Public |
8 | License as published by the Free Software Foundation; either | |
9 | version 2.1 of the License, or (at your option) any later version. | |
4c524b81 AJ |
10 | |
11 | The GNU C Library is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 14 | Lesser General Public License for more details. |
4c524b81 | 15 | |
41bdb6e2 | 16 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
17 | License along with the GNU C Library; if not, see |
18 | <http://www.gnu.org/licenses/>. */ | |
4c524b81 AJ |
19 | |
20 | #include <unistd.h> | |
21 | #include <stdlib.h> | |
22 | #include <sys/types.h> | |
23 | #include <sys/stat.h> | |
24 | #include <fcntl.h> | |
25 | #include <error.h> | |
26 | #include <errno.h> | |
feb27893 | 27 | #include <sys/resource.h> |
aa42b3db | 28 | #include <support/check.h> |
4c524b81 AJ |
29 | |
30 | /* Prototype for our test function. */ | |
31 | extern void do_prepare (int argc, char *argv[]); | |
32 | extern int do_test (int argc, char *argv[]); | |
33 | ||
34 | /* We have a preparation function. */ | |
35 | #define PREPARE do_prepare | |
36 | ||
4c524b81 AJ |
37 | /* This defines the `main' function and some more. */ |
38 | #include <test-skeleton.c> | |
39 | ||
40 | /* These are for the temporary file we generate. */ | |
41 | char *name; | |
42 | int fd; | |
43 | ||
44 | /* 2^31 = 2GB. */ | |
45 | #define TWO_GB 2147483648LL | |
46 | ||
47 | void | |
48 | do_prepare (int argc, char *argv[]) | |
49 | { | |
30aa5785 | 50 | size_t name_len; |
85471284 | 51 | struct rlimit64 rlim; |
4c524b81 AJ |
52 | |
53 | name_len = strlen (test_dir); | |
850c6760 | 54 | name = xmalloc (name_len + sizeof ("/lfsXXXXXX")); |
4c524b81 AJ |
55 | mempcpy (mempcpy (name, test_dir, name_len), |
56 | "/lfsXXXXXX", sizeof ("/lfsXXXXXX")); | |
4c524b81 AJ |
57 | |
58 | /* Open our test file. */ | |
85471284 UD |
59 | fd = mkstemp64 (name); |
60 | if (fd == -1) | |
4c524b81 | 61 | { |
85471284 UD |
62 | if (errno == ENOSYS) |
63 | { | |
64 | /* Fail silently. */ | |
207390f7 | 65 | error (0, 0, "open64 is not supported"); |
85471284 UD |
66 | exit (EXIT_SUCCESS); |
67 | } | |
68 | else | |
69 | error (EXIT_FAILURE, errno, "cannot create temporary file"); | |
4c524b81 | 70 | } |
aa42b3db FW |
71 | if (!support_descriptor_supports_holes (fd)) |
72 | FAIL_UNSUPPORTED ("File %s does not support holes", name); | |
cf145565 | 73 | add_temp_file (name); |
4c524b81 | 74 | |
85471284 UD |
75 | if (getrlimit64 (RLIMIT_FSIZE, &rlim) != 0) |
76 | { | |
77 | error (0, errno, "cannot get resource limit"); | |
78 | exit (0); | |
79 | } | |
80 | if (rlim.rlim_cur < TWO_GB + 200) | |
81 | { | |
82 | rlim.rlim_cur = TWO_GB + 200; | |
83 | if (setrlimit64 (RLIMIT_FSIZE, &rlim) != 0) | |
84 | { | |
85 | error (0, errno, "cannot reset file size limits"); | |
86 | exit (0); | |
87 | } | |
88 | } | |
4c524b81 AJ |
89 | } |
90 | ||
725c76a6 AJ |
91 | static void |
92 | test_ftello (void) | |
93 | { | |
94 | FILE *f; | |
95 | int ret; | |
96 | off64_t pos; | |
97 | ||
98 | f = fopen64 (name, "w"); | |
99 | ||
100 | ret = fseeko64 (f, TWO_GB+100, SEEK_SET); | |
101 | if (ret == -1 && errno == ENOSYS) | |
102 | { | |
207390f7 | 103 | error (0, 0, "fseeko64 is not supported."); |
725c76a6 AJ |
104 | exit (EXIT_SUCCESS); |
105 | } | |
106 | if (ret == -1 && errno == EINVAL) | |
107 | { | |
207390f7 | 108 | error (0, 0, "LFS seems not to be supported"); |
725c76a6 AJ |
109 | exit (EXIT_SUCCESS); |
110 | } | |
111 | if (ret == -1) | |
112 | { | |
207390f7 | 113 | error (0, errno, "fseeko64 failed with error"); |
725c76a6 AJ |
114 | exit (EXIT_FAILURE); |
115 | } | |
116 | ||
117 | ret = fwrite ("Hello", 1, 5, f); | |
207390f7 AJ |
118 | if (ret == -1 && errno == EFBIG) |
119 | { | |
120 | error (0, errno, "LFS seems not to be supported"); | |
121 | exit (EXIT_SUCCESS); | |
122 | } | |
123 | ||
124 | if (ret == -1 && errno == ENOSPC) | |
725c76a6 | 125 | { |
207390f7 | 126 | error (0, 0, "Not enough space to write file."); |
725c76a6 AJ |
127 | exit (EXIT_SUCCESS); |
128 | } | |
129 | ||
130 | if (ret != 5) | |
207390f7 | 131 | error (EXIT_FAILURE, errno, "Cannot write test string to large file"); |
725c76a6 AJ |
132 | |
133 | pos = ftello64 (f); | |
134 | ||
135 | if (pos != TWO_GB+105) | |
136 | { | |
137 | error (0, 0, "ftello64 gives wrong result."); | |
138 | exit (EXIT_FAILURE); | |
139 | } | |
140 | ||
141 | fclose (f); | |
142 | } | |
143 | ||
4c524b81 AJ |
144 | int |
145 | do_test (int argc, char *argv[]) | |
146 | { | |
eb32b0d4 | 147 | int ret, fd2; |
4c524b81 AJ |
148 | struct stat64 statbuf; |
149 | ||
150 | ret = lseek64 (fd, TWO_GB+100, SEEK_SET); | |
151 | if (ret == -1 && errno == ENOSYS) | |
152 | { | |
207390f7 | 153 | error (0, 0, "lseek64 is not supported."); |
4c524b81 AJ |
154 | exit (EXIT_SUCCESS); |
155 | } | |
0b795736 UD |
156 | if (ret == -1 && errno == EINVAL) |
157 | { | |
207390f7 | 158 | error (0, 0, "LFS seems not to be supported."); |
0b795736 UD |
159 | exit (EXIT_SUCCESS); |
160 | } | |
725c76a6 AJ |
161 | if (ret == -1) |
162 | { | |
207390f7 | 163 | error (0, errno, "lseek64 failed with error"); |
725c76a6 AJ |
164 | exit (EXIT_FAILURE); |
165 | } | |
df19fdcf JM |
166 | off64_t offset64 = lseek64 (fd, 0, SEEK_CUR); |
167 | if (offset64 != TWO_GB + 100) | |
168 | { | |
169 | error (0, 0, "lseek64 did not return expected offset"); | |
170 | exit (EXIT_FAILURE); | |
171 | } | |
172 | off_t offset = lseek (fd, 0, SEEK_CUR); | |
173 | if (sizeof (off_t) < sizeof (off64_t)) | |
174 | { | |
175 | if (offset != -1 || errno != EOVERFLOW) | |
176 | { | |
177 | error (0, 0, "lseek did not fail with EOVERFLOW"); | |
178 | exit (EXIT_FAILURE); | |
179 | } | |
180 | } | |
181 | else | |
182 | if (offset != TWO_GB + 100) | |
183 | { | |
184 | error (0, 0, "lseek did not return expected offset"); | |
185 | exit (EXIT_FAILURE); | |
186 | } | |
4c524b81 AJ |
187 | |
188 | ret = write (fd, "Hello", 5); | |
207390f7 AJ |
189 | if (ret == -1 && errno == EFBIG) |
190 | { | |
191 | error (0, 0, "LFS seems not to be supported."); | |
192 | exit (EXIT_SUCCESS); | |
193 | } | |
194 | ||
195 | if (ret == -1 && errno == ENOSPC) | |
4c524b81 | 196 | { |
207390f7 | 197 | error (0, 0, "Not enough space to write file."); |
4c524b81 AJ |
198 | exit (EXIT_SUCCESS); |
199 | } | |
56b2223e | 200 | |
4c524b81 AJ |
201 | if (ret != 5) |
202 | error (EXIT_FAILURE, errno, "cannot write test string to large file"); | |
203 | ||
204 | ret = close (fd); | |
205 | ||
206 | if (ret == -1) | |
207 | error (EXIT_FAILURE, errno, "error closing file"); | |
208 | ||
209 | ret = stat64 (name, &statbuf); | |
210 | ||
56b2223e | 211 | if (ret == -1 && (errno == ENOSYS || errno == EOVERFLOW)) |
207390f7 | 212 | error (0, 0, "stat64 is not supported."); |
4c524b81 AJ |
213 | else if (ret == -1) |
214 | error (EXIT_FAILURE, errno, "cannot stat file `%s'", name); | |
59553897 | 215 | else if (statbuf.st_size != (TWO_GB + 100 + 5)) |
4c524b81 | 216 | error (EXIT_FAILURE, 0, "stat reported size %lld instead of %lld.", |
59553897 | 217 | (long long int) statbuf.st_size, (TWO_GB + 100 + 5)); |
4c524b81 | 218 | |
eb32b0d4 AS |
219 | fd2 = openat64 (AT_FDCWD, name, O_RDWR); |
220 | if (fd2 == -1) | |
221 | { | |
222 | if (errno == ENOSYS) | |
223 | { | |
224 | /* Silently ignore this test. */ | |
225 | error (0, 0, "openat64 is not supported"); | |
226 | } | |
227 | else | |
228 | error (EXIT_FAILURE, errno, "openat64 failed to open big file"); | |
229 | } | |
230 | else | |
231 | { | |
232 | ret = close (fd2); | |
233 | ||
234 | if (ret == -1) | |
235 | error (EXIT_FAILURE, errno, "error closing file"); | |
236 | } | |
237 | ||
725c76a6 AJ |
238 | test_ftello (); |
239 | ||
4c524b81 AJ |
240 | return 0; |
241 | } |