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_open_supersede(bool supersede_if_exists,bool supersede_if_does_not_exist)21 test_open_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     int fd = open_supersede (filename, O_RDWR | O_TRUNC, 0666,
34                              supersede_if_exists, supersede_if_does_not_exist,
35                              &action);
36     ASSERT (fd >= 0);
37     ASSERT (write (fd, "Hello world\n", 12) == 12);
38     if (supersede_if_does_not_exist)
39       ASSERT (stat (filename, &statbuf) < 0);
40     else
41       ASSERT (stat (filename, &statbuf) == 0);
42     ASSERT (close_supersede (fd, &action) == 0);
43 
44     ASSERT (stat (filename, &statbuf) == 0);
45 
46     size_t file_size;
47     char *file_contents = read_file (filename, RF_BINARY, &file_size);
48     ASSERT (file_size == 12);
49     ASSERT (memcmp (file_contents, "Hello world\n", 12) == 0);
50   }
51 
52   /* Test the case that the file exists and is a regular file.  */
53   {
54     ASSERT (stat (filename, &statbuf) == 0);
55     dev_t orig_dev = statbuf.st_dev;
56     ino_t orig_ino = statbuf.st_ino;
57 
58     struct supersede_final_action action;
59     int fd = open_supersede (filename, O_RDWR | O_TRUNC, 0666,
60                              supersede_if_exists, supersede_if_does_not_exist,
61                              &action);
62     ASSERT (fd >= 0);
63     ASSERT (write (fd, "Foobar\n", 7) == 7);
64     ASSERT (stat (filename, &statbuf) == 0);
65     {
66       size_t file_size;
67       char *file_contents = read_file (filename, RF_BINARY, &file_size);
68       if (supersede_if_exists)
69         {
70           ASSERT (file_size == 12);
71           ASSERT (memcmp (file_contents, "Hello world\n", 12) == 0);
72         }
73       else
74         {
75           ASSERT (file_size == 7);
76           ASSERT (memcmp (file_contents, "Foobar\n", 7) == 0);
77         }
78     }
79     ASSERT (close_supersede (fd, &action) == 0);
80 
81     ASSERT (stat (filename, &statbuf) == 0);
82 
83     size_t file_size;
84     char *file_contents = read_file (filename, RF_BINARY, &file_size);
85     ASSERT (file_size == 7);
86     ASSERT (memcmp (file_contents, "Foobar\n", 7) == 0);
87 
88     if (supersede_if_exists)
89       {
90         /* Verify that the file now has a different inode number, on the same
91            device.  */
92 #if !(defined _WIN32 && !defined __CYGWIN__)
93         ASSERT (memcmp (&orig_dev, &statbuf.st_dev, sizeof (dev_t)) == 0);
94         ASSERT (memcmp (&orig_ino, &statbuf.st_ino, sizeof (ino_t)) != 0);
95 #endif
96       }
97   }
98 
99   /* Test the case that the file exists and is a character device.  */
100   {
101     ASSERT (stat (DEV_NULL, &statbuf) == 0);
102 
103     struct supersede_final_action action;
104     int fd = open_supersede (DEV_NULL, O_RDWR | O_TRUNC, 0666,
105                              supersede_if_exists, supersede_if_does_not_exist,
106                              &action);
107     ASSERT (fd >= 0);
108     ASSERT (write (fd, "Foobar\n", 7) == 7);
109     ASSERT (stat (DEV_NULL, &statbuf) == 0);
110     ASSERT (close_supersede (fd, &action) == 0);
111 
112     ASSERT (stat (DEV_NULL, &statbuf) == 0);
113   }
114 
115   /* Test the case that the file is a symbolic link to an existing regular
116      file.  */
117   {
118     const char *linkname = "link1";
119     unlink (linkname);
120     if (symlink (filename, linkname) >= 0)
121       {
122         ASSERT (stat (linkname, &statbuf) == 0);
123         dev_t orig_dev = statbuf.st_dev;
124         ino_t orig_ino = statbuf.st_ino;
125 
126         struct supersede_final_action action;
127         int fd =
128           open_supersede (linkname, O_RDWR | O_TRUNC, 0666,
129                           supersede_if_exists, supersede_if_does_not_exist,
130                           &action);
131         ASSERT (fd >= 0);
132         ASSERT (write (fd, "New\n", 4) == 4);
133         ASSERT (stat (linkname, &statbuf) == 0);
134         {
135           size_t file_size;
136           char *file_contents = read_file (linkname, RF_BINARY, &file_size);
137           if (supersede_if_exists)
138             {
139               ASSERT (file_size == 7);
140               ASSERT (memcmp (file_contents, "Foobar\n", 7) == 0);
141             }
142           else
143             {
144               ASSERT (file_size == 4);
145               ASSERT (memcmp (file_contents, "New\n", 4) == 0);
146             }
147         }
148         ASSERT (close_supersede (fd, &action) == 0);
149 
150         ASSERT (stat (linkname, &statbuf) == 0);
151 
152         size_t file_size;
153         char *file_contents = read_file (linkname, RF_BINARY, &file_size);
154         ASSERT (file_size == 4);
155         ASSERT (memcmp (file_contents, "New\n", 4) == 0);
156 
157         if (supersede_if_exists)
158           {
159             /* Verify that the file now has a different inode number, on the
160                same device.  */
161 #if !(defined _WIN32 && !defined __CYGWIN__)
162             ASSERT (memcmp (&orig_dev, &statbuf.st_dev, sizeof (dev_t)) == 0);
163             ASSERT (memcmp (&orig_ino, &statbuf.st_ino, sizeof (ino_t)) != 0);
164 #endif
165           }
166 
167         /* Clean up.  */
168         unlink (linkname);
169       }
170   }
171 
172   /* Test the case that the file is a symbolic link to an existing character
173      device.  */
174   {
175     const char *linkname = "link2";
176     unlink (linkname);
177     if (symlink (DEV_NULL, linkname) >= 0)
178       {
179         ASSERT (stat (linkname, &statbuf) == 0);
180 
181         struct supersede_final_action action;
182         int fd =
183           open_supersede (linkname, O_RDWR | O_TRUNC, 0666,
184                           supersede_if_exists, supersede_if_does_not_exist,
185                           &action);
186         ASSERT (fd >= 0);
187         ASSERT (write (fd, "New\n", 4) == 4);
188         ASSERT (stat (linkname, &statbuf) == 0);
189         ASSERT (close_supersede (fd, &action) == 0);
190 
191         ASSERT (stat (linkname, &statbuf) == 0);
192 
193         /* Clean up.  */
194         unlink (linkname);
195       }
196   }
197 
198   /* Clean up.  */
199   unlink (filename);
200 
201   /* Test the case that the file is a symbolic link to a nonexistent file in an
202      existing directory.  */
203   {
204     const char *linkname = "link3";
205     unlink (linkname);
206     if (symlink (filename, linkname) >= 0)
207       {
208         ASSERT (stat (linkname, &statbuf) < 0);
209 
210         struct supersede_final_action action;
211         int fd =
212           open_supersede (linkname, O_RDWR | O_TRUNC, 0666,
213                           supersede_if_exists, supersede_if_does_not_exist,
214                           &action);
215         ASSERT (fd >= 0);
216         ASSERT (write (fd, "Hello world\n", 12) == 12);
217         if (supersede_if_does_not_exist)
218           ASSERT (stat (linkname, &statbuf) < 0);
219         else
220           ASSERT (stat (linkname, &statbuf) == 0);
221         ASSERT (close_supersede (fd, &action) == 0);
222 
223         ASSERT (stat (linkname, &statbuf) == 0);
224 
225         size_t file_size;
226         char *file_contents = read_file (linkname, RF_BINARY, &file_size);
227         ASSERT (file_size == 12);
228         ASSERT (memcmp (file_contents, "Hello world\n", 12) == 0);
229 
230         /* Clean up.  */
231         unlink (linkname);
232       }
233   }
234 
235   /* Test the case that the file is a symbolic link to a nonexistent file in a
236      nonexistent directory.  */
237   {
238     const char *linkname = "link4";
239     unlink (linkname);
240     if (symlink ("/nonexistent/gnulibtest8237/24715863701440", linkname) >= 0)
241       {
242         ASSERT (stat (linkname, &statbuf) < 0);
243 
244         struct supersede_final_action action;
245         int fd =
246           open_supersede (linkname, O_RDWR | O_TRUNC, 0666,
247                           supersede_if_exists, supersede_if_does_not_exist,
248                           &action);
249         ASSERT (fd < 0);
250         ASSERT (errno == ENOENT);
251 
252         ASSERT (stat (linkname, &statbuf) < 0);
253 
254         /* Clean up.  */
255         unlink (linkname);
256       }
257   }
258 
259   /* Clean up.  */
260   unlink (filename);
261   rmdir (dir);
262 }
263