1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
/* Test that fflush (FILE) and fflush (NULL) are semantically equivalent.
Copyright (C) 2024 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
/* A success on this test doesn't imply the effectiveness of fflush as
we can't ensure that the file wasn't already in the expected state
before the call of the function. It only ensures that, if the test
fails, fflush is broken. */
#include <assert.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <support/check.h>
#include <support/support.h>
#include <support/temp_file.h>
#include <support/test-driver.h>
#include <support/xstdio.h>
#include <support/xunistd.h>
#define CONTENT_SZ_MAX 32
#define TEST_FILE_COUNT 10
struct file_tracking
{
FILE *file;
char *name;
int fd;
char *mfile;
} files[TEST_FILE_COUNT];
static void
file_init (int file)
{
int fd = -1;
assert (file < TEST_FILE_COUNT);
files[file] = (struct file_tracking) { .fd = -1, };
xclose (create_temp_file ("tst-fflush", &files[file].name));
fd = xopen (files[file].name, O_RDONLY, 0);
files[file].mfile = xmmap (NULL, CONTENT_SZ_MAX, PROT_READ, MAP_SHARED, fd);
xclose (fd);
}
static void
file_cleanup (int file)
{
free (files[file].name);
xmunmap (files[file].mfile, CONTENT_SZ_MAX);
files[file] = (struct file_tracking) { .fd = -1, };
}
static void
file_changed (int to_check, const char *mode)
{
struct stat stats = { };
char expected[CONTENT_SZ_MAX] = { };
verbose_printf ("Check that %s (%d) exactly contains the data we put in\n",
files[to_check].name, to_check);
/* File should contain "N:M" where both N and M are one digit exactly. */
snprintf (expected, sizeof (expected), "%d:%d", FILE_FLUSH_TYPE, to_check);
TEST_COMPARE_BLOB (files[to_check].mfile, sizeof (expected),
expected, sizeof (expected));
TEST_VERIFY (fstat (files[to_check].fd, &stats) >= 0);
TEST_VERIFY (stats.st_size == 3);
/* In read mode we expect to be at position 1, in write mode at position 3 */
TEST_COMPARE (lseek (files[to_check].fd, 0, SEEK_CUR),
mode[0] == 'r' ? 1 : 3);
if (support_record_failure_is_failed ())
FAIL_EXIT1 ("exiting due to previous failure");
/* Not reached if the data doesn't match. */
}
static void
file_flush (const char *mode)
{
for (int i = 0; i < TEST_FILE_COUNT; i++)
{
files[i].file = xfopen (files[i].name, mode);
files[i].fd = fileno (files[i].file);
}
/* Print a unique identifier in each file, that is not too long nor contain
new line to not trigger _IO_OVERFLOW/_IO_SYNC. */
for (int i = 0; i < TEST_FILE_COUNT; i++)
{
if (mode[0] == 'r')
fgetc (files[i].file);
else
fprintf (files[i].file, "%d:%d", FILE_FLUSH_TYPE, i);
}
if (!FILE_FLUSH_TYPE)
TEST_VERIFY (fflush (NULL) == 0);
else
for (int i = 0; i < TEST_FILE_COUNT; i++)
TEST_VERIFY (fflush (files[i].file) == 0);
for (int i = 0; i < TEST_FILE_COUNT; i++)
{
verbose_printf ("Check that file %s has been modified after fflush\n",
files[i].name);
file_changed (i, mode);
}
for (int i = 0; i < TEST_FILE_COUNT; i++)
xfclose (files[i].file);
}
static int
do_test (void)
{
for (int i = 0; i < TEST_FILE_COUNT; i++)
file_init (i);
verbose_printf ("Checking fflush(" S_FLUSH_TYPE "), WRITE mode\n");
file_flush ("w");
verbose_printf ("Checking fflush(" S_FLUSH_TYPE "), READWRITE mode\n");
file_flush ("r+");
for (int i = 0; i < TEST_FILE_COUNT; i++)
file_cleanup (i);
return 0;
}
#include <support/test-driver.c>
|