]>
Commit | Line | Data |
---|---|---|
87686aee | 1 | /* Common f{truncate} tests definitions. |
04277e02 | 2 | Copyright (C) 2016-2019 Free Software Foundation, Inc. |
87686aee AZ |
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 <unistd.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 | #include <test-skeleton.c> | |
30 | ||
31 | static char *temp_filename; | |
32 | static int temp_fd; | |
33 | ||
34 | static void | |
35 | do_prepare (void) | |
36 | { | |
37 | temp_fd = create_temp_file ("tst-trucate.", &temp_filename); | |
38 | if (temp_fd == -1) | |
39 | { | |
40 | printf ("cannot create temporary file: %m\n"); | |
41 | exit (1); | |
42 | } | |
43 | } | |
44 | ||
45 | #define FAIL(str) \ | |
46 | do { printf ("error: %s (line %d)\n", str, __LINE__); return 1; } while (0) | |
47 | ||
48 | static int | |
49 | do_test_with_offset (off_t offset) | |
50 | { | |
51 | struct stat st; | |
52 | char buf[1000]; | |
53 | ||
54 | memset (buf, 0xcf, sizeof (buf)); | |
55 | ||
56 | if (pwrite (temp_fd, buf, sizeof (buf), offset) != sizeof (buf)) | |
57 | FAIL ("write failed"); | |
58 | if (fstat (temp_fd, &st) < 0 || st.st_size != (offset + sizeof (buf))) | |
59 | FAIL ("initial size wrong"); | |
60 | ||
61 | if (ftruncate (temp_fd, offset + 800) < 0) | |
62 | FAIL ("size reduction with ftruncate failed"); | |
63 | if (fstat (temp_fd, &st) < 0 || st.st_size != (offset + 800)) | |
64 | FAIL ("size after reduction with ftruncate is incorrect"); | |
65 | ||
66 | /* The following test covers more than POSIX. POSIX does not require | |
67 | that ftruncate() can increase the file size. But we are testing | |
68 | Unix systems. */ | |
69 | if (ftruncate (temp_fd, offset + 1200) < 0) | |
70 | FAIL ("size increate with ftruncate failed"); | |
71 | if (fstat (temp_fd, &st) < 0 || st.st_size != (offset + 1200)) | |
72 | FAIL ("size after increase is incorrect"); | |
73 | ||
74 | if (truncate (temp_filename, offset + 800) < 0) | |
75 | FAIL ("size reduction with truncate failed"); | |
76 | if (fstat (temp_fd, &st) < 0 || st.st_size != (offset + 800)) | |
77 | FAIL ("size after reduction with truncate incorrect"); | |
78 | ||
79 | /* The following test covers more than POSIX. POSIX does not require | |
80 | that truncate() can increase the file size. But we are testing | |
81 | Unix systems. */ | |
82 | if (truncate (temp_filename, (offset + 1200)) < 0) | |
83 | FAIL ("size increase with truncate failed"); | |
84 | if (fstat (temp_fd, &st) < 0 || st.st_size != (offset + 1200)) | |
85 | FAIL ("size increase with truncate is incorrect"); | |
86 | ||
87 | return 0; | |
88 | } |