1 /* Tests for opening a file without destroying an old file with the same name.
2
3 Copyright (C) 2020 Free Software Foundation, Inc.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
9
10 This program 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
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17
18 /* Written by Bruno Haible, 2020. */
19
20 static void
test_fopen_supersede(bool supersede_if_exists,bool supersede_if_does_not_exist)21 test_fopen_supersede (bool supersede_if_exists, bool supersede_if_does_not_exist)
22 {
23 char xtemplate[] = "gnulibtestXXXXXX";
24 char *dir = mkdtemp (xtemplate);
25 char *filename = file_name_concat (dir, "test.mo", NULL);
26 struct stat statbuf;
27
28 /* Test the case that the file does not yet exist. */
29 {
30 ASSERT (stat (filename, &statbuf) < 0);
31
32 struct supersede_final_action action;
33 FILE *fp =
34 fopen_supersede (filename, "wb",
35 supersede_if_exists, supersede_if_does_not_exist,
36 &action);
37 ASSERT (fp != NULL);
38 ASSERT (fwrite ("Hello world\n", 1, 12, fp) == 12 && fflush (fp) == 0);
39 if (supersede_if_does_not_exist)
40 ASSERT (stat (filename, &statbuf) < 0);
41 else
42 ASSERT (stat (filename, &statbuf) == 0);
43 ASSERT (fclose_supersede (fp, &action) == 0);
44
45 ASSERT (stat (filename, &statbuf) == 0);
46
47 size_t file_size;
48 char *file_contents = read_file (filename, RF_BINARY, &file_size);
49 ASSERT (file_size == 12);
50 ASSERT (memcmp (file_contents, "Hello world\n", 12) == 0);
51 }
52
53 /* Test the case that the file exists and is a regular file. */
54 {
55 ASSERT (stat (filename, &statbuf) == 0);
56 dev_t orig_dev = statbuf.st_dev;
57 ino_t orig_ino = statbuf.st_ino;
58
59 struct supersede_final_action action;
60 FILE *fp =
61 fopen_supersede (filename, "wb",
62 supersede_if_exists, supersede_if_does_not_exist,
63 &action);
64 ASSERT (fp != NULL);
65 ASSERT (fwrite ("Foobar\n", 1, 7, fp) == 7 && fflush (fp) == 0);
66 ASSERT (stat (filename, &statbuf) == 0);
67 {
68 size_t file_size;
69 char *file_contents = read_file (filename, RF_BINARY, &file_size);
70 if (supersede_if_exists)
71 {
72 ASSERT (file_size == 12);
73 ASSERT (memcmp (file_contents, "Hello world\n", 12) == 0);
74 }
75 else
76 {
77 ASSERT (file_size == 7);
78 ASSERT (memcmp (file_contents, "Foobar\n", 7) == 0);
79 }
80 }
81 ASSERT (fclose_supersede (fp, &action) == 0);
82
83 ASSERT (stat (filename, &statbuf) == 0);
84
85 size_t file_size;
86 char *file_contents = read_file (filename, RF_BINARY, &file_size);
87 ASSERT (file_size == 7);
88 ASSERT (memcmp (file_contents, "Foobar\n", 7) == 0);
89
90 if (supersede_if_exists)
91 {
92 /* Verify that the file now has a different inode number, on the same
93 device. */
94 #if !(defined _WIN32 && !defined __CYGWIN__)
95 ASSERT (memcmp (&orig_dev, &statbuf.st_dev, sizeof (dev_t)) == 0);
96 ASSERT (memcmp (&orig_ino, &statbuf.st_ino, sizeof (ino_t)) != 0);
97 #endif
98 }
99 }
100
101 /* Test the case that the file exists and is a character device. */
102 {
103 ASSERT (stat (DEV_NULL, &statbuf) == 0);
104
105 struct supersede_final_action action;
106 FILE *fp =
107 fopen_supersede (DEV_NULL, "wb",
108 supersede_if_exists, supersede_if_does_not_exist,
109 &action);
110 ASSERT (fp != NULL);
111 ASSERT (fwrite ("Foobar\n", 1, 7, fp) == 7 && fflush (fp) == 0);
112 ASSERT (stat (DEV_NULL, &statbuf) == 0);
113 ASSERT (fclose_supersede (fp, &action) == 0);
114
115 ASSERT (stat (DEV_NULL, &statbuf) == 0);
116 }
117
118 /* Test the case that the file is a symbolic link to an existing regular
119 file. */
120 {
121 const char *linkname = "link1";
122 unlink (linkname);
123 if (symlink (filename, linkname) >= 0)
124 {
125 ASSERT (stat (linkname, &statbuf) == 0);
126 dev_t orig_dev = statbuf.st_dev;
127 ino_t orig_ino = statbuf.st_ino;
128
129 struct supersede_final_action action;
130 FILE *fp =
131 fopen_supersede (linkname, "wb",
132 supersede_if_exists, supersede_if_does_not_exist,
133 &action);
134 ASSERT (fp != NULL);
135 ASSERT (fwrite ("New\n", 1, 4, fp) == 4 && fflush (fp) == 0);
136 ASSERT (stat (linkname, &statbuf) == 0);
137 {
138 size_t file_size;
139 char *file_contents = read_file (linkname, RF_BINARY, &file_size);
140 if (supersede_if_exists)
141 {
142 ASSERT (file_size == 7);
143 ASSERT (memcmp (file_contents, "Foobar\n", 7) == 0);
144 }
145 else
146 {
147 ASSERT (file_size == 4);
148 ASSERT (memcmp (file_contents, "New\n", 4) == 0);
149 }
150 }
151 ASSERT (fclose_supersede (fp, &action) == 0);
152
153 ASSERT (stat (linkname, &statbuf) == 0);
154
155 size_t file_size;
156 char *file_contents = read_file (linkname, RF_BINARY, &file_size);
157 ASSERT (file_size == 4);
158 ASSERT (memcmp (file_contents, "New\n", 4) == 0);
159
160 if (supersede_if_exists)
161 {
162 /* Verify that the file now has a different inode number, on the
163 same device. */
164 #if !(defined _WIN32 && !defined __CYGWIN__)
165 ASSERT (memcmp (&orig_dev, &statbuf.st_dev, sizeof (dev_t)) == 0);
166 ASSERT (memcmp (&orig_ino, &statbuf.st_ino, sizeof (ino_t)) != 0);
167 #endif
168 }
169
170 /* Clean up. */
171 unlink (linkname);
172 }
173 }
174
175 /* Test the case that the file is a symbolic link to an existing character
176 device. */
177 {
178 const char *linkname = "link2";
179 unlink (linkname);
180 if (symlink (DEV_NULL, linkname) >= 0)
181 {
182 ASSERT (stat (linkname, &statbuf) == 0);
183
184 struct supersede_final_action action;
185 FILE *fp =
186 fopen_supersede (linkname, "wb",
187 supersede_if_exists, supersede_if_does_not_exist,
188 &action);
189 ASSERT (fp != NULL);
190 ASSERT (fwrite ("New\n", 1, 4, fp) == 4 && fflush (fp) == 0);
191 ASSERT (stat (linkname, &statbuf) == 0);
192 ASSERT (fclose_supersede (fp, &action) == 0);
193
194 ASSERT (stat (linkname, &statbuf) == 0);
195
196 /* Clean up. */
197 unlink (linkname);
198 }
199 }
200
201 /* Clean up. */
202 unlink (filename);
203
204 /* Test the case that the file is a symbolic link to a nonexistent file in an
205 existing directory. */
206 {
207 const char *linkname = "link3";
208 unlink (linkname);
209 if (symlink (filename, linkname) >= 0)
210 {
211 ASSERT (stat (linkname, &statbuf) < 0);
212
213 struct supersede_final_action action;
214 FILE *fp =
215 fopen_supersede (linkname, "wb",
216 supersede_if_exists, supersede_if_does_not_exist,
217 &action);
218 ASSERT (fp != NULL);
219 ASSERT (fwrite ("Hello world\n", 1, 12, fp) == 12 && fflush (fp) == 0);
220 if (supersede_if_does_not_exist)
221 ASSERT (stat (linkname, &statbuf) < 0);
222 else
223 ASSERT (stat (linkname, &statbuf) == 0);
224 ASSERT (fclose_supersede (fp, &action) == 0);
225
226 ASSERT (stat (linkname, &statbuf) == 0);
227
228 size_t file_size;
229 char *file_contents = read_file (linkname, RF_BINARY, &file_size);
230 ASSERT (file_size == 12);
231 ASSERT (memcmp (file_contents, "Hello world\n", 12) == 0);
232
233 /* Clean up. */
234 unlink (linkname);
235 }
236 }
237
238 /* Test the case that the file is a symbolic link to a nonexistent file in a
239 nonexistent directory. */
240 {
241 const char *linkname = "link4";
242 unlink (linkname);
243 if (symlink ("/nonexistent/gnulibtest8237/24715863701440", linkname) >= 0)
244 {
245 ASSERT (stat (linkname, &statbuf) < 0);
246
247 struct supersede_final_action action;
248 FILE *fp =
249 fopen_supersede (linkname, "wb",
250 supersede_if_exists, supersede_if_does_not_exist,
251 &action);
252 ASSERT (fp == NULL);
253 ASSERT (errno == ENOENT);
254
255 ASSERT (stat (linkname, &statbuf) < 0);
256
257 /* Clean up. */
258 unlink (linkname);
259 }
260 }
261
262 /* Clean up. */
263 unlink (filename);
264 rmdir (dir);
265 }
266