Branch
Hash :
7b089321
Author :
Date :
2025-01-01T09:24:36
maint: run 'make update-copyright'
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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
/* Tests for opening a file without destroying an old file with the same name.
Copyright (C) 2020-2025 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
/* Written by Bruno Haible, 2020. */
static void
test_open_supersede (bool supersede_if_exists, bool supersede_if_does_not_exist)
{
char xtemplate[] = "gnulibtestXXXXXX";
char *dir = mkdtemp (xtemplate);
char *filename = file_name_concat (dir, "test.mo", NULL);
struct stat statbuf;
/* Test the case that the file does not yet exist. */
{
ASSERT (stat (filename, &statbuf) < 0);
struct supersede_final_action action;
int fd = open_supersede (filename, O_RDWR | O_BINARY | O_TRUNC, 0666,
supersede_if_exists, supersede_if_does_not_exist,
&action);
ASSERT (fd >= 0);
ASSERT (write (fd, "Hello world\n", 12) == 12);
if (supersede_if_does_not_exist)
ASSERT (stat (filename, &statbuf) < 0);
else
ASSERT (stat (filename, &statbuf) == 0);
ASSERT (close_supersede (fd, &action) == 0);
ASSERT (stat (filename, &statbuf) == 0);
size_t file_size;
char *file_contents = read_file (filename, RF_BINARY, &file_size);
ASSERT (file_size == 12);
ASSERT (memcmp (file_contents, "Hello world\n", 12) == 0);
}
/* Test the case that the file exists and is a regular file. */
{
ASSERT (stat (filename, &statbuf) == 0);
dev_t orig_dev = statbuf.st_dev;
ino_t orig_ino = statbuf.st_ino;
struct supersede_final_action action;
int fd = open_supersede (filename, O_RDWR | O_BINARY | O_TRUNC, 0666,
supersede_if_exists, supersede_if_does_not_exist,
&action);
ASSERT (fd >= 0);
ASSERT (write (fd, "Foobar\n", 7) == 7);
ASSERT (stat (filename, &statbuf) == 0);
{
size_t file_size;
char *file_contents = read_file (filename, RF_BINARY, &file_size);
if (supersede_if_exists)
{
ASSERT (file_size == 12);
ASSERT (memcmp (file_contents, "Hello world\n", 12) == 0);
}
else
{
ASSERT (file_size == 7);
ASSERT (memcmp (file_contents, "Foobar\n", 7) == 0);
}
}
ASSERT (close_supersede (fd, &action) == 0);
ASSERT (stat (filename, &statbuf) == 0);
size_t file_size;
char *file_contents = read_file (filename, RF_BINARY, &file_size);
ASSERT (file_size == 7);
ASSERT (memcmp (file_contents, "Foobar\n", 7) == 0);
if (supersede_if_exists)
{
/* Verify that the file now has a different inode number, on the same
device. */
#if !(defined _WIN32 && !defined __CYGWIN__)
/* Note: On Linux/mips, statbuf.st_dev is smaller than a dev_t! */
dev_t new_dev = statbuf.st_dev;
ASSERT (memcmp (&orig_dev, &new_dev, sizeof (dev_t)) == 0);
ASSERT (memcmp (&orig_ino, &statbuf.st_ino, sizeof (ino_t)) != 0);
#endif
}
}
/* Test the case that the file exists and is a character device. */
{
ASSERT (stat (DEV_NULL, &statbuf) == 0);
struct supersede_final_action action;
int fd = open_supersede (DEV_NULL, O_RDWR | O_BINARY | O_TRUNC, 0666,
supersede_if_exists, supersede_if_does_not_exist,
&action);
ASSERT (fd >= 0);
ASSERT (write (fd, "Foobar\n", 7) == 7);
ASSERT (stat (DEV_NULL, &statbuf) == 0);
ASSERT (close_supersede (fd, &action) == 0);
ASSERT (stat (DEV_NULL, &statbuf) == 0);
}
/* Test the case that the file is a symbolic link to an existing regular
file. */
{
const char *linkname = "link1";
unlink (linkname);
if (symlink (filename, linkname) >= 0)
{
ASSERT (stat (linkname, &statbuf) == 0);
dev_t orig_dev = statbuf.st_dev;
ino_t orig_ino = statbuf.st_ino;
struct supersede_final_action action;
int fd =
open_supersede (linkname, O_RDWR | O_BINARY | O_TRUNC, 0666,
supersede_if_exists, supersede_if_does_not_exist,
&action);
ASSERT (fd >= 0);
ASSERT (write (fd, "New\n", 4) == 4);
ASSERT (stat (linkname, &statbuf) == 0);
{
size_t file_size;
char *file_contents = read_file (linkname, RF_BINARY, &file_size);
if (supersede_if_exists)
{
ASSERT (file_size == 7);
ASSERT (memcmp (file_contents, "Foobar\n", 7) == 0);
}
else
{
ASSERT (file_size == 4);
ASSERT (memcmp (file_contents, "New\n", 4) == 0);
}
}
ASSERT (close_supersede (fd, &action) == 0);
ASSERT (stat (linkname, &statbuf) == 0);
size_t file_size;
char *file_contents = read_file (linkname, RF_BINARY, &file_size);
ASSERT (file_size == 4);
ASSERT (memcmp (file_contents, "New\n", 4) == 0);
if (supersede_if_exists)
{
/* Verify that the file now has a different inode number, on the
same device. */
#if !(defined _WIN32 && !defined __CYGWIN__)
/* Note: On Linux/mips, statbuf.st_dev is smaller than a dev_t! */
dev_t new_dev = statbuf.st_dev;
ASSERT (memcmp (&orig_dev, &new_dev, sizeof (dev_t)) == 0);
ASSERT (memcmp (&orig_ino, &statbuf.st_ino, sizeof (ino_t)) != 0);
#endif
}
/* Clean up. */
unlink (linkname);
}
}
/* Test the case that the file is a symbolic link to an existing character
device. */
{
const char *linkname = "link2";
unlink (linkname);
if (symlink (DEV_NULL, linkname) >= 0)
{
ASSERT (stat (linkname, &statbuf) == 0);
struct supersede_final_action action;
int fd =
open_supersede (linkname, O_RDWR | O_BINARY | O_TRUNC, 0666,
supersede_if_exists, supersede_if_does_not_exist,
&action);
ASSERT (fd >= 0);
ASSERT (write (fd, "New\n", 4) == 4);
ASSERT (stat (linkname, &statbuf) == 0);
ASSERT (close_supersede (fd, &action) == 0);
ASSERT (stat (linkname, &statbuf) == 0);
/* Clean up. */
unlink (linkname);
}
}
/* Clean up. */
unlink (filename);
/* Test the case that the file is a symbolic link to a nonexistent file in an
existing directory. */
{
const char *linkname = "link3";
unlink (linkname);
if (symlink (filename, linkname) >= 0)
{
ASSERT (stat (linkname, &statbuf) < 0);
struct supersede_final_action action;
int fd =
open_supersede (linkname, O_RDWR | O_BINARY | O_TRUNC, 0666,
supersede_if_exists, supersede_if_does_not_exist,
&action);
ASSERT (fd >= 0);
ASSERT (write (fd, "Hello world\n", 12) == 12);
if (supersede_if_does_not_exist)
ASSERT (stat (linkname, &statbuf) < 0);
else
ASSERT (stat (linkname, &statbuf) == 0);
ASSERT (close_supersede (fd, &action) == 0);
ASSERT (stat (linkname, &statbuf) == 0);
size_t file_size;
char *file_contents = read_file (linkname, RF_BINARY, &file_size);
ASSERT (file_size == 12);
ASSERT (memcmp (file_contents, "Hello world\n", 12) == 0);
/* Clean up. */
unlink (linkname);
}
}
/* Test the case that the file is a symbolic link to a nonexistent file in a
nonexistent directory. */
{
const char *linkname = "link4";
unlink (linkname);
if (symlink ("/nonexistent/gnulibtest8237/24715863701440", linkname) >= 0)
{
ASSERT (stat (linkname, &statbuf) < 0);
struct supersede_final_action action;
int fd =
open_supersede (linkname, O_RDWR | O_BINARY | O_TRUNC, 0666,
supersede_if_exists, supersede_if_does_not_exist,
&action);
ASSERT (fd < 0);
ASSERT (errno == ENOENT);
ASSERT (stat (linkname, &statbuf) < 0);
/* Clean up. */
unlink (linkname);
}
}
/* Clean up. */
unlink (filename);
rmdir (dir);
}