]>
Commit | Line | Data |
---|---|---|
bad7a0c8 | 1 | /* Tests for copy_file_range. |
04277e02 | 2 | Copyright (C) 2017-2019 Free Software Foundation, Inc. |
bad7a0c8 FW |
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 | <http://www.gnu.org/licenses/>. */ | |
18 | ||
19 | #include <array_length.h> | |
20 | #include <errno.h> | |
21 | #include <fcntl.h> | |
22 | #include <inttypes.h> | |
23 | #include <libgen.h> | |
24 | #include <poll.h> | |
25 | #include <sched.h> | |
26 | #include <stdbool.h> | |
27 | #include <stdio.h> | |
28 | #include <stdlib.h> | |
29 | #include <string.h> | |
30 | #include <support/check.h> | |
31 | #include <support/namespace.h> | |
32 | #include <support/support.h> | |
33 | #include <support/temp_file.h> | |
34 | #include <support/test-driver.h> | |
35 | #include <support/xunistd.h> | |
3fc1ecf9 ST |
36 | #ifdef CLONE_NEWNS |
37 | # include <sys/mount.h> | |
38 | #endif | |
bad7a0c8 FW |
39 | |
40 | /* Boolean flags which indicate whether to use pointers with explicit | |
41 | output flags. */ | |
42 | static int do_inoff; | |
43 | static int do_outoff; | |
44 | ||
45 | /* Name and descriptors of the input files. Files are truncated and | |
46 | reopened (with O_RDWR) between tests. */ | |
47 | static char *infile; | |
48 | static int infd; | |
49 | static char *outfile; | |
50 | static int outfd; | |
51 | ||
52 | /* Like the above, but on a different file system. xdevfile can be | |
53 | NULL if no suitable file system has been found. */ | |
54 | static char *xdevfile; | |
55 | ||
56 | /* Input and output offsets. Set according to do_inoff and do_outoff | |
57 | before the test. The offsets themselves are always set to | |
58 | zero. */ | |
59 | static off64_t inoff; | |
60 | static off64_t *pinoff; | |
61 | static off64_t outoff; | |
62 | static off64_t *poutoff; | |
63 | ||
64 | /* These are a collection of copy sizes used in tests. The selection | |
65 | takes into account that the fallback implementation uses an | |
66 | internal buffer of 8192 bytes. */ | |
67 | enum { maximum_size = 99999 }; | |
68 | static const int typical_sizes[] = | |
69 | { 0, 1, 2, 3, 1024, 2048, 4096, 8191, 8192, 8193, 16383, 16384, 16385, | |
70 | maximum_size }; | |
71 | ||
72 | /* The random contents of this array can be used as a pattern to check | |
73 | for correct write operations. */ | |
74 | static unsigned char random_data[maximum_size]; | |
75 | ||
76 | /* The size chosen by the test harness. */ | |
77 | static int current_size; | |
78 | ||
79 | /* Maximum writable file offset. Updated by find_maximum_offset | |
80 | below. */ | |
81 | static off64_t maximum_offset; | |
82 | ||
83 | /* Error code when crossing the offset. */ | |
84 | static int maximum_offset_errno; | |
85 | ||
86 | /* If true: Writes which cross the limit will fail. If false: Writes | |
87 | which cross the limit will result in a partial write. */ | |
88 | static bool maximum_offset_hard_limit; | |
89 | ||
90 | /* Fills maximum_offset etc. above. Truncates outfd as a side | |
91 | effect. */ | |
92 | static void | |
93 | find_maximum_offset (void) | |
94 | { | |
95 | xftruncate (outfd, 0); | |
96 | if (maximum_offset != 0) | |
97 | return; | |
98 | ||
99 | uint64_t upper = -1; | |
100 | upper >>= 1; /* Maximum of off64_t. */ | |
101 | TEST_VERIFY ((off64_t) upper > 0); | |
102 | TEST_VERIFY ((off64_t) (upper + 1) < 0); | |
103 | if (lseek64 (outfd, upper, SEEK_SET) >= 0) | |
104 | { | |
105 | if (write (outfd, "", 1) == 1) | |
106 | FAIL_EXIT1 ("created a file larger than the off64_t range"); | |
107 | } | |
108 | ||
109 | uint64_t lower = 1024 * 1024; /* A reasonable minimum file size. */ | |
110 | /* Loop invariant: writing at lower succeeds, writing at upper fails. */ | |
111 | while (lower + 1 < upper) | |
112 | { | |
113 | uint64_t middle = (lower + upper) / 2; | |
114 | if (test_verbose > 0) | |
115 | printf ("info: %s: remaining test range %" PRIu64 " .. %" PRIu64 | |
116 | ", probe at %" PRIu64 "\n", __func__, lower, upper, middle); | |
117 | xftruncate (outfd, 0); | |
118 | if (lseek64 (outfd, middle, SEEK_SET) >= 0 | |
119 | && write (outfd, "", 1) == 1) | |
120 | lower = middle; | |
121 | else | |
122 | upper = middle; | |
123 | } | |
124 | TEST_VERIFY (lower + 1 == upper); | |
125 | maximum_offset = lower; | |
126 | printf ("info: maximum writable file offset: %" PRIu64 " (%" PRIx64 ")\n", | |
127 | lower, lower); | |
128 | ||
129 | /* Check that writing at the valid offset actually works. */ | |
130 | xftruncate (outfd, 0); | |
131 | xlseek (outfd, lower, SEEK_SET); | |
132 | TEST_COMPARE (write (outfd, "", 1), 1); | |
133 | ||
134 | /* Cross the boundary with a two-byte write. This can either result | |
135 | in a short write, or a failure. */ | |
136 | xlseek (outfd, lower, SEEK_SET); | |
137 | ssize_t ret = write (outfd, " ", 2); | |
138 | if (ret < 0) | |
139 | { | |
140 | maximum_offset_errno = errno; | |
141 | maximum_offset_hard_limit = true; | |
142 | } | |
143 | else | |
144 | maximum_offset_hard_limit = false; | |
145 | ||
146 | /* Check that writing at the next offset actually fails. This also | |
147 | obtains the expected errno value. */ | |
148 | xftruncate (outfd, 0); | |
149 | const char *action; | |
150 | if (lseek64 (outfd, lower + 1, SEEK_SET) != 0) | |
151 | { | |
152 | if (write (outfd, "", 1) != -1) | |
153 | FAIL_EXIT1 ("write to impossible offset %" PRIu64 " succeeded", | |
154 | lower + 1); | |
155 | action = "writing"; | |
156 | int errno_copy = errno; | |
157 | if (maximum_offset_hard_limit) | |
158 | TEST_COMPARE (errno_copy, maximum_offset_errno); | |
159 | else | |
160 | maximum_offset_errno = errno_copy; | |
161 | } | |
162 | else | |
163 | { | |
164 | action = "seeking"; | |
165 | maximum_offset_errno = errno; | |
166 | } | |
167 | printf ("info: %s out of range fails with %m (%d)\n", | |
168 | action, maximum_offset_errno); | |
169 | ||
170 | xftruncate (outfd, 0); | |
171 | xlseek (outfd, 0, SEEK_SET); | |
172 | } | |
173 | ||
174 | /* Perform a copy of a file. */ | |
175 | static void | |
176 | simple_file_copy (void) | |
177 | { | |
178 | xwrite (infd, random_data, current_size); | |
179 | ||
180 | int length; | |
181 | int in_skipped; /* Expected skipped bytes in input. */ | |
182 | if (do_inoff) | |
183 | { | |
184 | xlseek (infd, 1, SEEK_SET); | |
185 | inoff = 2; | |
186 | length = current_size - 3; | |
187 | in_skipped = 2; | |
188 | } | |
189 | else | |
190 | { | |
191 | xlseek (infd, 3, SEEK_SET); | |
192 | length = current_size - 5; | |
193 | in_skipped = 3; | |
194 | } | |
195 | int out_skipped; /* Expected skipped bytes before the written data. */ | |
196 | if (do_outoff) | |
197 | { | |
198 | xlseek (outfd, 4, SEEK_SET); | |
199 | outoff = 5; | |
200 | out_skipped = 5; | |
201 | } | |
202 | else | |
203 | { | |
204 | xlseek (outfd, 6, SEEK_SET); | |
205 | length = current_size - 6; | |
206 | out_skipped = 6; | |
207 | } | |
208 | if (length < 0) | |
209 | length = 0; | |
210 | ||
211 | TEST_COMPARE (copy_file_range (infd, pinoff, outfd, poutoff, | |
212 | length, 0), length); | |
213 | if (do_inoff) | |
214 | { | |
215 | TEST_COMPARE (inoff, 2 + length); | |
216 | TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), 1); | |
217 | } | |
218 | else | |
219 | TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), 3 + length); | |
220 | if (do_outoff) | |
221 | { | |
222 | TEST_COMPARE (outoff, 5 + length); | |
223 | TEST_COMPARE (xlseek (outfd, 0, SEEK_CUR), 4); | |
224 | } | |
225 | else | |
226 | TEST_COMPARE (xlseek (outfd, 0, SEEK_CUR), 6 + length); | |
227 | ||
228 | struct stat64 st; | |
229 | xfstat (outfd, &st); | |
230 | if (length > 0) | |
231 | TEST_COMPARE (st.st_size, out_skipped + length); | |
232 | else | |
233 | { | |
234 | /* If we did not write anything, we also did not add any | |
235 | padding. */ | |
236 | TEST_COMPARE (st.st_size, 0); | |
237 | return; | |
238 | } | |
239 | ||
240 | xlseek (outfd, 0, SEEK_SET); | |
241 | char *bytes = xmalloc (st.st_size); | |
242 | TEST_COMPARE (read (outfd, bytes, st.st_size), st.st_size); | |
243 | for (int i = 0; i < out_skipped; ++i) | |
244 | TEST_COMPARE (bytes[i], 0); | |
245 | TEST_VERIFY (memcmp (bytes + out_skipped, random_data + in_skipped, | |
246 | length) == 0); | |
247 | free (bytes); | |
248 | } | |
249 | ||
250 | /* Test that reading from a pipe willfails. */ | |
251 | static void | |
252 | pipe_as_source (void) | |
253 | { | |
254 | int pipefds[2]; | |
255 | xpipe (pipefds); | |
256 | ||
257 | for (int length = 0; length < 2; ++length) | |
258 | { | |
259 | if (test_verbose > 0) | |
260 | printf ("info: %s: length=%d\n", __func__, length); | |
261 | ||
262 | /* Make sure that there is something to copy in the pipe. */ | |
263 | xwrite (pipefds[1], "@", 1); | |
264 | ||
265 | TEST_COMPARE (copy_file_range (pipefds[0], pinoff, outfd, poutoff, | |
266 | length, 0), -1); | |
267 | /* Linux 4.10 and later return EINVAL. Older kernels return | |
268 | EXDEV. */ | |
269 | TEST_VERIFY (errno == EINVAL || errno == EXDEV); | |
270 | TEST_COMPARE (inoff, 0); | |
271 | TEST_COMPARE (outoff, 0); | |
272 | TEST_COMPARE (xlseek (outfd, 0, SEEK_CUR), 0); | |
273 | ||
274 | /* Make sure that nothing was read. */ | |
275 | char buf = 'A'; | |
276 | TEST_COMPARE (read (pipefds[0], &buf, 1), 1); | |
277 | TEST_COMPARE (buf, '@'); | |
278 | } | |
279 | ||
280 | xclose (pipefds[0]); | |
281 | xclose (pipefds[1]); | |
282 | } | |
283 | ||
284 | /* Test that writing to a pipe fails. */ | |
285 | static void | |
286 | pipe_as_destination (void) | |
287 | { | |
288 | /* Make sure that there is something to read in the input file. */ | |
289 | xwrite (infd, "abc", 3); | |
290 | xlseek (infd, 0, SEEK_SET); | |
291 | ||
292 | int pipefds[2]; | |
293 | xpipe (pipefds); | |
294 | ||
295 | for (int length = 0; length < 2; ++length) | |
296 | { | |
297 | if (test_verbose > 0) | |
298 | printf ("info: %s: length=%d\n", __func__, length); | |
299 | ||
300 | TEST_COMPARE (copy_file_range (infd, pinoff, pipefds[1], poutoff, | |
301 | length, 0), -1); | |
302 | /* Linux 4.10 and later return EINVAL. Older kernels return | |
303 | EXDEV. */ | |
304 | TEST_VERIFY (errno == EINVAL || errno == EXDEV); | |
305 | TEST_COMPARE (inoff, 0); | |
306 | TEST_COMPARE (outoff, 0); | |
307 | TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), 0); | |
308 | ||
309 | /* Make sure that nothing was written. */ | |
310 | struct pollfd pollfd = { .fd = pipefds[0], .events = POLLIN, }; | |
311 | TEST_COMPARE (poll (&pollfd, 1, 0), 0); | |
312 | } | |
313 | ||
314 | xclose (pipefds[0]); | |
315 | xclose (pipefds[1]); | |
316 | } | |
317 | ||
318 | /* Test a write failure after (potentially) writing some bytes. | |
319 | Failure occurs near the start of the buffer. */ | |
320 | static void | |
321 | delayed_write_failure_beginning (void) | |
322 | { | |
323 | /* We need to write something to provoke the error. */ | |
324 | if (current_size == 0) | |
325 | return; | |
326 | xwrite (infd, random_data, sizeof (random_data)); | |
327 | xlseek (infd, 0, SEEK_SET); | |
328 | ||
329 | /* Write failure near the start. The actual error code varies among | |
330 | file systems. */ | |
331 | find_maximum_offset (); | |
332 | off64_t where = maximum_offset; | |
333 | ||
334 | if (current_size == 1) | |
335 | ++where; | |
336 | outoff = where; | |
337 | if (do_outoff) | |
338 | xlseek (outfd, 1, SEEK_SET); | |
339 | else | |
340 | xlseek (outfd, where, SEEK_SET); | |
341 | if (maximum_offset_hard_limit || where > maximum_offset) | |
342 | { | |
343 | TEST_COMPARE (copy_file_range (infd, pinoff, outfd, poutoff, | |
344 | sizeof (random_data), 0), -1); | |
345 | TEST_COMPARE (errno, maximum_offset_errno); | |
346 | TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), 0); | |
347 | TEST_COMPARE (inoff, 0); | |
348 | if (do_outoff) | |
349 | TEST_COMPARE (xlseek (outfd, 0, SEEK_CUR), 1); | |
350 | else | |
351 | TEST_COMPARE (xlseek (outfd, 0, SEEK_CUR), where); | |
352 | TEST_COMPARE (outoff, where); | |
353 | struct stat64 st; | |
354 | xfstat (outfd, &st); | |
355 | TEST_COMPARE (st.st_size, 0); | |
356 | } | |
357 | else | |
358 | { | |
359 | /* The offset is not a hard limit. This means we write one | |
360 | byte. */ | |
361 | TEST_COMPARE (copy_file_range (infd, pinoff, outfd, poutoff, | |
362 | sizeof (random_data), 0), 1); | |
363 | if (do_inoff) | |
364 | { | |
365 | TEST_COMPARE (inoff, 1); | |
366 | TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), 0); | |
367 | } | |
368 | else | |
369 | { | |
370 | TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), 1); | |
371 | TEST_COMPARE (inoff, 0); | |
372 | } | |
373 | if (do_outoff) | |
374 | { | |
375 | TEST_COMPARE (xlseek (outfd, 0, SEEK_CUR), 1); | |
376 | TEST_COMPARE (outoff, where + 1); | |
377 | } | |
378 | else | |
379 | { | |
380 | TEST_COMPARE (xlseek (outfd, 0, SEEK_CUR), where + 1); | |
381 | TEST_COMPARE (outoff, where); | |
382 | } | |
383 | struct stat64 st; | |
384 | xfstat (outfd, &st); | |
385 | TEST_COMPARE (st.st_size, where + 1); | |
386 | } | |
387 | } | |
388 | ||
389 | /* Test a write failure after (potentially) writing some bytes. | |
390 | Failure occurs near the end of the buffer. */ | |
391 | static void | |
392 | delayed_write_failure_end (void) | |
393 | { | |
394 | if (current_size <= 1) | |
395 | /* This would be same as the first test because there is not | |
396 | enough data to write to make a difference. */ | |
397 | return; | |
398 | xwrite (infd, random_data, sizeof (random_data)); | |
399 | xlseek (infd, 0, SEEK_SET); | |
400 | ||
401 | find_maximum_offset (); | |
402 | off64_t where = maximum_offset - current_size + 1; | |
403 | if (current_size == sizeof (random_data)) | |
404 | /* Otherwise we do not reach the non-writable byte. */ | |
405 | ++where; | |
406 | outoff = where; | |
407 | if (do_outoff) | |
408 | xlseek (outfd, 1, SEEK_SET); | |
409 | else | |
410 | xlseek (outfd, where, SEEK_SET); | |
411 | ssize_t ret = copy_file_range (infd, pinoff, outfd, poutoff, | |
412 | sizeof (random_data), 0); | |
413 | if (ret < 0) | |
414 | { | |
415 | TEST_COMPARE (ret, -1); | |
416 | TEST_COMPARE (errno, maximum_offset_errno); | |
417 | struct stat64 st; | |
418 | xfstat (outfd, &st); | |
419 | TEST_COMPARE (st.st_size, 0); | |
420 | } | |
421 | else | |
422 | { | |
423 | /* The first copy succeeded. This happens in the emulation | |
424 | because the internal buffer of limited size does not | |
425 | necessarily cross the off64_t boundary on the first write | |
426 | operation. */ | |
427 | if (test_verbose > 0) | |
428 | printf ("info: copy_file_range (%zu) returned %zd\n", | |
429 | sizeof (random_data), ret); | |
430 | TEST_VERIFY (ret > 0); | |
431 | TEST_VERIFY (ret < maximum_size); | |
432 | struct stat64 st; | |
433 | xfstat (outfd, &st); | |
434 | TEST_COMPARE (st.st_size, where + ret); | |
435 | if (do_inoff) | |
436 | { | |
437 | TEST_COMPARE (inoff, ret); | |
438 | TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), 0); | |
439 | } | |
440 | else | |
441 | TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), ret); | |
442 | ||
443 | char *buffer = xmalloc (ret); | |
444 | TEST_COMPARE (pread64 (outfd, buffer, ret, where), ret); | |
445 | TEST_VERIFY (memcmp (buffer, random_data, ret) == 0); | |
446 | free (buffer); | |
447 | ||
448 | /* The second copy fails. */ | |
449 | TEST_COMPARE (copy_file_range (infd, pinoff, outfd, poutoff, | |
450 | sizeof (random_data), 0), -1); | |
451 | TEST_COMPARE (errno, maximum_offset_errno); | |
452 | } | |
453 | } | |
454 | ||
455 | /* Test a write failure across devices. */ | |
456 | static void | |
457 | cross_device_failure (void) | |
458 | { | |
459 | if (xdevfile == NULL) | |
460 | /* Subtest not supported due to missing cross-device file. */ | |
461 | return; | |
462 | ||
463 | /* We need something to write. */ | |
464 | xwrite (infd, random_data, sizeof (random_data)); | |
465 | xlseek (infd, 0, SEEK_SET); | |
466 | ||
467 | int xdevfd = xopen (xdevfile, O_RDWR | O_LARGEFILE, 0); | |
468 | TEST_COMPARE (copy_file_range (infd, pinoff, xdevfd, poutoff, | |
469 | current_size, 0), -1); | |
470 | TEST_COMPARE (errno, EXDEV); | |
471 | TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), 0); | |
472 | struct stat64 st; | |
473 | xfstat (xdevfd, &st); | |
474 | TEST_COMPARE (st.st_size, 0); | |
475 | ||
476 | xclose (xdevfd); | |
477 | } | |
478 | ||
479 | /* Try to exercise ENOSPC behavior with a tempfs file system (so that | |
480 | we do not have to fill up a regular file system to get the error). | |
481 | This function runs in a subprocess, so that we do not change the | |
482 | mount namespace of the actual test process. */ | |
483 | static void | |
484 | enospc_failure_1 (void *closure) | |
485 | { | |
486 | #ifdef CLONE_NEWNS | |
487 | support_become_root (); | |
488 | ||
489 | /* Make sure that we do not alter the file system mounts of the | |
490 | parents. */ | |
491 | if (! support_enter_mount_namespace ()) | |
492 | { | |
493 | printf ("warning: ENOSPC test skipped\n"); | |
494 | return; | |
495 | } | |
496 | ||
497 | char *mountpoint = closure; | |
498 | if (mount ("none", mountpoint, "tmpfs", MS_NODEV | MS_NOEXEC, | |
499 | "size=500k") != 0) | |
500 | { | |
501 | printf ("warning: could not mount tmpfs at %s: %m\n", mountpoint); | |
502 | return; | |
503 | } | |
504 | ||
505 | /* The source file must reside on the same file system. */ | |
506 | char *intmpfsfile = xasprintf ("%s/%s", mountpoint, "in"); | |
507 | int intmpfsfd = xopen (intmpfsfile, O_RDWR | O_CREAT | O_LARGEFILE, 0600); | |
508 | xwrite (intmpfsfd, random_data, sizeof (random_data)); | |
509 | xlseek (intmpfsfd, 1, SEEK_SET); | |
510 | inoff = 1; | |
511 | ||
512 | char *outtmpfsfile = xasprintf ("%s/%s", mountpoint, "out"); | |
513 | int outtmpfsfd = xopen (outtmpfsfile, O_RDWR | O_CREAT | O_LARGEFILE, 0600); | |
514 | ||
515 | /* Fill the file with data until ENOSPC is reached. */ | |
516 | while (true) | |
517 | { | |
518 | ssize_t ret = write (outtmpfsfd, random_data, sizeof (random_data)); | |
519 | if (ret < 0 && errno != ENOSPC) | |
520 | FAIL_EXIT1 ("write to %s: %m", outtmpfsfile); | |
521 | if (ret < sizeof (random_data)) | |
522 | break; | |
523 | } | |
524 | TEST_COMPARE (write (outtmpfsfd, "", 1), -1); | |
525 | TEST_COMPARE (errno, ENOSPC); | |
526 | off64_t maxsize = xlseek (outtmpfsfd, 0, SEEK_CUR); | |
527 | TEST_VERIFY_EXIT (maxsize > sizeof (random_data)); | |
528 | ||
529 | /* Constructed the expected file contents. */ | |
530 | char *expected = xmalloc (maxsize); | |
531 | TEST_COMPARE (pread64 (outtmpfsfd, expected, maxsize, 0), maxsize); | |
532 | /* Go back a little, so some bytes can be written. */ | |
533 | enum { offset = 20000 }; | |
534 | TEST_VERIFY_EXIT (offset < maxsize); | |
535 | TEST_VERIFY_EXIT (offset < sizeof (random_data)); | |
536 | memcpy (expected + maxsize - offset, random_data + 1, offset); | |
537 | ||
538 | if (do_outoff) | |
539 | { | |
540 | outoff = maxsize - offset; | |
541 | xlseek (outtmpfsfd, 2, SEEK_SET); | |
542 | } | |
543 | else | |
544 | xlseek (outtmpfsfd, -offset, SEEK_CUR); | |
545 | ||
546 | /* First call is expected to succeed because we made room for some | |
547 | bytes. */ | |
548 | TEST_COMPARE (copy_file_range (intmpfsfd, pinoff, outtmpfsfd, poutoff, | |
549 | maximum_size, 0), offset); | |
550 | if (do_inoff) | |
551 | { | |
552 | TEST_COMPARE (inoff, 1 + offset); | |
553 | TEST_COMPARE (xlseek (intmpfsfd, 0, SEEK_CUR), 1); | |
554 | } | |
555 | else | |
556 | TEST_COMPARE (xlseek (intmpfsfd, 0, SEEK_CUR), 1 + offset); | |
557 | if (do_outoff) | |
558 | { | |
559 | TEST_COMPARE (outoff, maxsize); | |
560 | TEST_COMPARE (xlseek (outtmpfsfd, 0, SEEK_CUR), 2); | |
561 | } | |
562 | else | |
563 | TEST_COMPARE (xlseek (outtmpfsfd, 0, SEEK_CUR), maxsize); | |
564 | struct stat64 st; | |
565 | xfstat (outtmpfsfd, &st); | |
566 | TEST_COMPARE (st.st_size, maxsize); | |
567 | char *actual = xmalloc (st.st_size); | |
568 | TEST_COMPARE (pread64 (outtmpfsfd, actual, st.st_size, 0), st.st_size); | |
569 | TEST_VERIFY (memcmp (expected, actual, maxsize) == 0); | |
570 | ||
571 | /* Second call should fail with ENOSPC. */ | |
572 | TEST_COMPARE (copy_file_range (intmpfsfd, pinoff, outtmpfsfd, poutoff, | |
573 | maximum_size, 0), -1); | |
574 | TEST_COMPARE (errno, ENOSPC); | |
575 | ||
576 | /* Offsets should be unchanged. */ | |
577 | if (do_inoff) | |
578 | { | |
579 | TEST_COMPARE (inoff, 1 + offset); | |
580 | TEST_COMPARE (xlseek (intmpfsfd, 0, SEEK_CUR), 1); | |
581 | } | |
582 | else | |
583 | TEST_COMPARE (xlseek (intmpfsfd, 0, SEEK_CUR), 1 + offset); | |
584 | if (do_outoff) | |
585 | { | |
586 | TEST_COMPARE (outoff, maxsize); | |
587 | TEST_COMPARE (xlseek (outtmpfsfd, 0, SEEK_CUR), 2); | |
588 | } | |
589 | else | |
590 | TEST_COMPARE (xlseek (outtmpfsfd, 0, SEEK_CUR), maxsize); | |
591 | TEST_COMPARE (xlseek (outtmpfsfd, 0, SEEK_END), maxsize); | |
592 | TEST_COMPARE (pread64 (outtmpfsfd, actual, maxsize, 0), maxsize); | |
593 | TEST_VERIFY (memcmp (expected, actual, maxsize) == 0); | |
594 | ||
595 | free (actual); | |
596 | free (expected); | |
597 | ||
598 | xclose (intmpfsfd); | |
599 | xclose (outtmpfsfd); | |
600 | free (intmpfsfile); | |
601 | free (outtmpfsfile); | |
602 | ||
603 | #else /* !CLONE_NEWNS */ | |
604 | puts ("warning: ENOSPC test skipped (no mount namespaces)"); | |
605 | #endif | |
606 | } | |
607 | ||
608 | /* Call enospc_failure_1 in a subprocess. */ | |
609 | static void | |
610 | enospc_failure (void) | |
611 | { | |
612 | char *mountpoint | |
613 | = support_create_temp_directory ("tst-copy_file_range-enospc-"); | |
614 | support_isolate_in_subprocess (enospc_failure_1, mountpoint); | |
615 | free (mountpoint); | |
616 | } | |
617 | ||
618 | /* The target file descriptor must have O_APPEND enabled. */ | |
619 | static void | |
620 | oappend_failure (void) | |
621 | { | |
622 | /* Add data, to make sure we do not fail because there is | |
623 | insufficient input data. */ | |
624 | xwrite (infd, random_data, current_size); | |
625 | xlseek (infd, 0, SEEK_SET); | |
626 | ||
627 | xclose (outfd); | |
628 | outfd = xopen (outfile, O_RDWR | O_APPEND, 0); | |
629 | TEST_COMPARE (copy_file_range (infd, pinoff, outfd, poutoff, | |
630 | current_size, 0), -1); | |
631 | TEST_COMPARE (errno, EBADF); | |
632 | } | |
633 | ||
634 | /* Test that a short input file results in a shortened copy. */ | |
635 | static void | |
636 | short_copy (void) | |
637 | { | |
638 | if (current_size == 0) | |
639 | /* Nothing to shorten. */ | |
640 | return; | |
641 | ||
642 | /* Two subtests, one with offset 0 and current_size - 1 bytes, and | |
643 | another one with current_size bytes, but offset 1. */ | |
644 | for (int shift = 0; shift < 2; ++shift) | |
645 | { | |
646 | if (test_verbose > 0) | |
647 | printf ("info: shift=%d\n", shift); | |
648 | xftruncate (infd, 0); | |
649 | xlseek (infd, 0, SEEK_SET); | |
650 | xwrite (infd, random_data, current_size - !shift); | |
651 | ||
652 | if (do_inoff) | |
653 | { | |
654 | inoff = shift; | |
655 | xlseek (infd, 2, SEEK_SET); | |
656 | } | |
657 | else | |
658 | { | |
659 | inoff = 3; | |
660 | xlseek (infd, shift, SEEK_SET); | |
661 | } | |
662 | ftruncate (outfd, 0); | |
663 | xlseek (outfd, 0, SEEK_SET); | |
664 | outoff = 0; | |
665 | ||
666 | /* First call copies current_size - 1 bytes. */ | |
667 | TEST_COMPARE (copy_file_range (infd, pinoff, outfd, poutoff, | |
668 | current_size, 0), current_size - 1); | |
669 | char *buffer = xmalloc (current_size); | |
670 | TEST_COMPARE (pread64 (outfd, buffer, current_size, 0), | |
671 | current_size - 1); | |
672 | TEST_VERIFY (memcmp (buffer, random_data + shift, current_size - 1) | |
673 | == 0); | |
674 | free (buffer); | |
675 | ||
676 | if (do_inoff) | |
677 | { | |
678 | TEST_COMPARE (inoff, current_size - 1 + shift); | |
679 | TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), 2); | |
680 | } | |
681 | else | |
682 | TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), current_size - 1 + shift); | |
683 | if (do_outoff) | |
684 | { | |
685 | TEST_COMPARE (outoff, current_size - 1); | |
686 | TEST_COMPARE (xlseek (outfd, 0, SEEK_CUR), 0); | |
687 | } | |
688 | else | |
689 | TEST_COMPARE (xlseek (outfd, 0, SEEK_CUR), current_size - 1); | |
690 | ||
691 | /* First call copies zero bytes. */ | |
692 | TEST_COMPARE (copy_file_range (infd, pinoff, outfd, poutoff, | |
693 | current_size, 0), 0); | |
694 | /* And the offsets are unchanged. */ | |
695 | if (do_inoff) | |
696 | { | |
697 | TEST_COMPARE (inoff, current_size - 1 + shift); | |
698 | TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), 2); | |
699 | } | |
700 | else | |
701 | TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), current_size - 1 + shift); | |
702 | if (do_outoff) | |
703 | { | |
704 | TEST_COMPARE (outoff, current_size - 1); | |
705 | TEST_COMPARE (xlseek (outfd, 0, SEEK_CUR), 0); | |
706 | } | |
707 | else | |
708 | TEST_COMPARE (xlseek (outfd, 0, SEEK_CUR), current_size - 1); | |
709 | } | |
710 | } | |
711 | ||
712 | /* A named test function. */ | |
713 | struct test_case | |
714 | { | |
715 | const char *name; | |
716 | void (*func) (void); | |
717 | bool sizes; /* If true, call the test with different current_size values. */ | |
718 | }; | |
719 | ||
720 | /* The available test cases. */ | |
721 | static struct test_case tests[] = | |
722 | { | |
723 | { "simple_file_copy", simple_file_copy, .sizes = true }, | |
724 | { "pipe_as_source", pipe_as_source, }, | |
725 | { "pipe_as_destination", pipe_as_destination, }, | |
726 | { "delayed_write_failure_beginning", delayed_write_failure_beginning, | |
727 | .sizes = true }, | |
728 | { "delayed_write_failure_end", delayed_write_failure_end, .sizes = true }, | |
729 | { "cross_device_failure", cross_device_failure, .sizes = true }, | |
730 | { "enospc_failure", enospc_failure, }, | |
731 | { "oappend_failure", oappend_failure, .sizes = true }, | |
732 | { "short_copy", short_copy, .sizes = true }, | |
733 | }; | |
734 | ||
735 | static int | |
736 | do_test (void) | |
737 | { | |
738 | for (unsigned char *p = random_data; p < array_end (random_data); ++p) | |
739 | *p = rand () >> 24; | |
740 | ||
741 | infd = create_temp_file ("tst-copy_file_range-in-", &infile); | |
aa42b3db FW |
742 | { |
743 | int outfd = create_temp_file ("tst-copy_file_range-out-", &outfile); | |
744 | if (!support_descriptor_supports_holes (outfd)) | |
745 | FAIL_UNSUPPORTED ("File %s does not support holes", outfile); | |
746 | xclose (outfd); | |
747 | } | |
bad7a0c8 FW |
748 | |
749 | /* Try to find a different directory from the default input/output | |
750 | file. */ | |
751 | { | |
752 | struct stat64 instat; | |
753 | xfstat (infd, &instat); | |
754 | static const char *const candidates[] = | |
755 | { NULL, "/var/tmp", "/dev/shm" }; | |
756 | for (const char *const *c = candidates; c < array_end (candidates); ++c) | |
757 | { | |
758 | const char *path = *c; | |
759 | char *to_free = NULL; | |
760 | if (path == NULL) | |
761 | { | |
762 | to_free = xreadlink ("/proc/self/exe"); | |
763 | path = dirname (to_free); | |
764 | } | |
765 | ||
766 | struct stat64 cstat; | |
767 | xstat (path, &cstat); | |
768 | if (cstat.st_dev == instat.st_dev) | |
769 | { | |
770 | free (to_free); | |
771 | continue; | |
772 | } | |
773 | ||
774 | printf ("info: using alternate temporary files directory: %s\n", path); | |
775 | xdevfile = xasprintf ("%s/tst-copy_file_range-xdev-XXXXXX", path); | |
776 | free (to_free); | |
777 | break; | |
778 | } | |
779 | if (xdevfile != NULL) | |
780 | { | |
781 | int xdevfd = mkstemp (xdevfile); | |
782 | if (xdevfd < 0) | |
783 | FAIL_EXIT1 ("mkstemp (\"%s\"): %m", xdevfile); | |
784 | struct stat64 xdevst; | |
785 | xfstat (xdevfd, &xdevst); | |
786 | TEST_VERIFY (xdevst.st_dev != instat.st_dev); | |
787 | add_temp_file (xdevfile); | |
788 | xclose (xdevfd); | |
789 | } | |
790 | else | |
791 | puts ("warning: no alternate directory on different file system found"); | |
792 | } | |
793 | xclose (infd); | |
794 | ||
795 | for (do_inoff = 0; do_inoff < 2; ++do_inoff) | |
796 | for (do_outoff = 0; do_outoff < 2; ++do_outoff) | |
797 | for (struct test_case *test = tests; test < array_end (tests); ++test) | |
798 | for (const int *size = typical_sizes; | |
799 | size < array_end (typical_sizes); ++size) | |
800 | { | |
801 | current_size = *size; | |
802 | if (test_verbose > 0) | |
803 | printf ("info: %s do_inoff=%d do_outoff=%d current_size=%d\n", | |
804 | test->name, do_inoff, do_outoff, current_size); | |
805 | ||
806 | inoff = 0; | |
807 | if (do_inoff) | |
808 | pinoff = &inoff; | |
809 | else | |
810 | pinoff = NULL; | |
811 | outoff = 0; | |
812 | if (do_outoff) | |
813 | poutoff = &outoff; | |
814 | else | |
815 | poutoff = NULL; | |
816 | ||
817 | infd = xopen (infile, O_RDWR | O_LARGEFILE, 0); | |
818 | xftruncate (infd, 0); | |
819 | outfd = xopen (outfile, O_RDWR | O_LARGEFILE, 0); | |
820 | xftruncate (outfd, 0); | |
821 | ||
822 | test->func (); | |
823 | ||
824 | xclose (infd); | |
825 | xclose (outfd); | |
826 | ||
827 | if (!test->sizes) | |
828 | /* Skip the other sizes unless they have been | |
829 | requested. */ | |
830 | break; | |
831 | } | |
832 | ||
833 | free (infile); | |
834 | free (outfile); | |
835 | free (xdevfile); | |
836 | ||
837 | return 0; | |
838 | } | |
839 | ||
840 | #include <support/test-driver.c> |