1 /* Tests for opening a file without destroying an old file with the same name.
3 Copyright (C) 2020-2025 Free Software Foundation, Inc.
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.
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.
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/>. */
18 /* Written by Bruno Haible, 2020. */
21 test_fopen_supersede (bool supersede_if_exists
, bool supersede_if_does_not_exist
)
23 char xtemplate
[] = "gnulibtestXXXXXX";
24 char *dir
= mkdtemp (xtemplate
);
25 char *filename
= file_name_concat (dir
, "test.mo", NULL
);
28 /* Test the case that the file does not yet exist. */
30 ASSERT (stat (filename
, &statbuf
) < 0);
32 struct supersede_final_action action
;
34 fopen_supersede (filename
, "wb",
35 supersede_if_exists
, supersede_if_does_not_exist
,
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);
42 ASSERT (stat (filename
, &statbuf
) == 0);
43 ASSERT (fclose_supersede (fp
, &action
) == 0);
45 ASSERT (stat (filename
, &statbuf
) == 0);
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);
53 /* Test the case that the file exists and is a regular file. */
55 ASSERT (stat (filename
, &statbuf
) == 0);
56 dev_t orig_dev
= statbuf
.st_dev
;
57 ino_t orig_ino
= statbuf
.st_ino
;
59 struct supersede_final_action action
;
61 fopen_supersede (filename
, "wb",
62 supersede_if_exists
, supersede_if_does_not_exist
,
65 ASSERT (fwrite ("Foobar\n", 1, 7, fp
) == 7 && fflush (fp
) == 0);
66 ASSERT (stat (filename
, &statbuf
) == 0);
69 char *file_contents
= read_file (filename
, RF_BINARY
, &file_size
);
70 if (supersede_if_exists
)
72 ASSERT (file_size
== 12);
73 ASSERT (memcmp (file_contents
, "Hello world\n", 12) == 0);
77 ASSERT (file_size
== 7);
78 ASSERT (memcmp (file_contents
, "Foobar\n", 7) == 0);
81 ASSERT (fclose_supersede (fp
, &action
) == 0);
83 ASSERT (stat (filename
, &statbuf
) == 0);
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);
90 if (supersede_if_exists
)
92 /* Verify that the file now has a different inode number, on the same
94 #if !(defined _WIN32 && !defined __CYGWIN__)
95 /* Note: On Linux/mips, statbuf.st_dev is smaller than a dev_t! */
96 dev_t new_dev
= statbuf
.st_dev
;
97 ASSERT (memcmp (&orig_dev
, &new_dev
, sizeof (dev_t
)) == 0);
98 ASSERT (memcmp (&orig_ino
, &statbuf
.st_ino
, sizeof (ino_t
)) != 0);
103 /* Test the case that the file exists and is a character device. */
105 ASSERT (stat (DEV_NULL
, &statbuf
) == 0);
107 struct supersede_final_action action
;
109 fopen_supersede (DEV_NULL
, "wb",
110 supersede_if_exists
, supersede_if_does_not_exist
,
113 ASSERT (fwrite ("Foobar\n", 1, 7, fp
) == 7 && fflush (fp
) == 0);
114 ASSERT (stat (DEV_NULL
, &statbuf
) == 0);
115 ASSERT (fclose_supersede (fp
, &action
) == 0);
117 ASSERT (stat (DEV_NULL
, &statbuf
) == 0);
120 /* Test the case that the file is a symbolic link to an existing regular
123 const char *linkname
= "link1";
125 if (symlink (filename
, linkname
) >= 0)
127 ASSERT (stat (linkname
, &statbuf
) == 0);
128 dev_t orig_dev
= statbuf
.st_dev
;
129 ino_t orig_ino
= statbuf
.st_ino
;
131 struct supersede_final_action action
;
133 fopen_supersede (linkname
, "wb",
134 supersede_if_exists
, supersede_if_does_not_exist
,
137 ASSERT (fwrite ("New\n", 1, 4, fp
) == 4 && fflush (fp
) == 0);
138 ASSERT (stat (linkname
, &statbuf
) == 0);
141 char *file_contents
= read_file (linkname
, RF_BINARY
, &file_size
);
142 if (supersede_if_exists
)
144 ASSERT (file_size
== 7);
145 ASSERT (memcmp (file_contents
, "Foobar\n", 7) == 0);
149 ASSERT (file_size
== 4);
150 ASSERT (memcmp (file_contents
, "New\n", 4) == 0);
153 ASSERT (fclose_supersede (fp
, &action
) == 0);
155 ASSERT (stat (linkname
, &statbuf
) == 0);
158 char *file_contents
= read_file (linkname
, RF_BINARY
, &file_size
);
159 ASSERT (file_size
== 4);
160 ASSERT (memcmp (file_contents
, "New\n", 4) == 0);
162 if (supersede_if_exists
)
164 /* Verify that the file now has a different inode number, on the
166 #if !(defined _WIN32 && !defined __CYGWIN__)
167 /* Note: On Linux/mips, statbuf.st_dev is smaller than a dev_t! */
168 dev_t new_dev
= statbuf
.st_dev
;
169 ASSERT (memcmp (&orig_dev
, &new_dev
, sizeof (dev_t
)) == 0);
170 ASSERT (memcmp (&orig_ino
, &statbuf
.st_ino
, sizeof (ino_t
)) != 0);
179 /* Test the case that the file is a symbolic link to an existing character
182 const char *linkname
= "link2";
184 if (symlink (DEV_NULL
, linkname
) >= 0)
186 ASSERT (stat (linkname
, &statbuf
) == 0);
188 struct supersede_final_action action
;
190 fopen_supersede (linkname
, "wb",
191 supersede_if_exists
, supersede_if_does_not_exist
,
194 ASSERT (fwrite ("New\n", 1, 4, fp
) == 4 && fflush (fp
) == 0);
195 ASSERT (stat (linkname
, &statbuf
) == 0);
196 ASSERT (fclose_supersede (fp
, &action
) == 0);
198 ASSERT (stat (linkname
, &statbuf
) == 0);
208 /* Test the case that the file is a symbolic link to a nonexistent file in an
209 existing directory. */
211 const char *linkname
= "link3";
213 if (symlink (filename
, linkname
) >= 0)
215 ASSERT (stat (linkname
, &statbuf
) < 0);
217 struct supersede_final_action action
;
219 fopen_supersede (linkname
, "wb",
220 supersede_if_exists
, supersede_if_does_not_exist
,
223 ASSERT (fwrite ("Hello world\n", 1, 12, fp
) == 12 && fflush (fp
) == 0);
224 if (supersede_if_does_not_exist
)
225 ASSERT (stat (linkname
, &statbuf
) < 0);
227 ASSERT (stat (linkname
, &statbuf
) == 0);
228 ASSERT (fclose_supersede (fp
, &action
) == 0);
230 ASSERT (stat (linkname
, &statbuf
) == 0);
233 char *file_contents
= read_file (linkname
, RF_BINARY
, &file_size
);
234 ASSERT (file_size
== 12);
235 ASSERT (memcmp (file_contents
, "Hello world\n", 12) == 0);
242 /* Test the case that the file is a symbolic link to a nonexistent file in a
243 nonexistent directory. */
245 const char *linkname
= "link4";
247 if (symlink ("/nonexistent/gnulibtest8237/24715863701440", linkname
) >= 0)
249 ASSERT (stat (linkname
, &statbuf
) < 0);
251 struct supersede_final_action action
;
253 fopen_supersede (linkname
, "wb",
254 supersede_if_exists
, supersede_if_does_not_exist
,
257 ASSERT (errno
== ENOENT
);
259 ASSERT (stat (linkname
, &statbuf
) < 0);