]>
Commit | Line | Data |
---|---|---|
45ff2bfc AZ |
1 | /* Common posix_fallocate tests definitions. |
2 | Copyright (C) 2016 Free Software Foundation, Inc. | |
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 <fcntl.h> | |
20 | #include <sys/types.h> | |
21 | #include <sys/stat.h> | |
22 | #include <unistd.h> | |
23 | ||
24 | static void do_prepare (void); | |
25 | #define PREPARE(argc, argv) do_prepare () | |
26 | static int do_test (void); | |
27 | #define TEST_FUNCTION do_test () | |
28 | ||
29 | #define TIMEOUT 20 /* sec. */ | |
30 | ||
31 | #include <test-skeleton.c> | |
32 | ||
33 | static char *temp_filename; | |
34 | static int temp_fd; | |
35 | ||
36 | static void | |
37 | do_prepare (void) | |
38 | { | |
39 | temp_fd = create_temp_file ("tst-posix_fallocate.", &temp_filename); | |
40 | if (temp_fd == -1) | |
41 | FAIL_EXIT1 ("cannot create temporary file: %m\n"); | |
42 | } | |
43 | ||
44 | #define FAIL(str) \ | |
45 | do { printf ("error: %s (line %d)\n", str, __LINE__); return 1; } while (0) | |
46 | ||
47 | static int | |
48 | do_test_with_offset (off_t offset) | |
49 | { | |
50 | struct stat st; | |
51 | ||
52 | if (posix_fallocate (temp_fd, offset, 768) != 0) | |
53 | FAIL_EXIT1 ("1st posix_fallocate call failed"); | |
54 | ||
55 | if (fstat (temp_fd, &st) != 0) | |
56 | FAIL_EXIT1 ("2nd fstat failed"); | |
57 | ||
58 | if (st.st_size != (offset + 768)) | |
59 | FAIL_EXIT1 ("file size after first posix_fallocate call is %lu, " | |
60 | "expected %lu", | |
61 | (unsigned long int) st.st_size, 512lu + 768lu); | |
62 | ||
63 | if (posix_fallocate (temp_fd, 0, 1024) != 0) | |
64 | FAIL_EXIT1 ("2nd posix_fallocate call failed"); | |
65 | ||
66 | if (fstat (temp_fd, &st) != 0) | |
67 | FAIL_EXIT1 ("3rd fstat failed"); | |
68 | ||
69 | if (st.st_size != (offset) + 768) | |
70 | FAIL_EXIT1 ("file size changed in second posix_fallocate"); | |
71 | ||
72 | offset += 2048; | |
73 | if (posix_fallocate (temp_fd, offset, 64) != 0) | |
74 | FAIL_EXIT1 ("3rd posix_fallocate call failed"); | |
75 | ||
76 | if (fstat (temp_fd, &st) != 0) | |
77 | FAIL_EXIT1 ("4th fstat failed"); | |
78 | ||
79 | if (st.st_size != (offset + 64)) | |
80 | FAIL_EXIT1 ("file size after first posix_fallocate call is %llu, " | |
81 | "expected %u", | |
82 | (unsigned long long int) st.st_size, 2048u + 64u); | |
83 | ||
84 | return 0; | |
85 | } |