Debian Codegraph @

libstdc++-v3/testsuite/27_io/filesystem/operations/rename.cc

raw
1 // Copyright (C) 2021-2025 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 // { dg-do run { target c++17 } }
19 // { dg-require-filesystem-ts "" }
20 // { dg-xfail-run-if "rename is not POSIX-compliant" { *-*-rtems* } }
21
22 #include <filesystem>
23 #include <testsuite_hooks.h>
24 #include <testsuite_fs.h>
25
26 namespace fs = std::filesystem;
27
28 void
29 test01()
30 {
31 std::error_code ec;
32 const std::error_code bad_ec = make_error_code(std::errc::invalid_argument);
33
34 auto p1 = __gnu_test::nonexistent_path();
35 auto p2 = __gnu_test::nonexistent_path();
36
37 fs::rename(p1, p2, ec);
38 VERIFY( ec );
39
40 ec.clear();
41 fs::rename(p1, "", ec);
42 VERIFY( ec );
43
44 ec.clear();
45 fs::rename("", p1, ec);
46 VERIFY( ec );
47
48 ec = bad_ec;
49 std::ofstream{p1}; // create file
50 fs::rename(p1, p1, ec); // no-op
51 VERIFY( !ec );
52 VERIFY( is_regular_file(p1) );
53
54 ec.clear();
55 rename(p2, p1, ec);
56 VERIFY( ec );
57 VERIFY( ec.value() == ENOENT );
58 VERIFY( is_regular_file(p1) );
59
60 ec = bad_ec;
61 fs::rename(p1, p2, ec);
62 VERIFY( !ec );
63 VERIFY( !exists(p1) );
64 VERIFY( is_regular_file(p2) );
65
66 ec = bad_ec;
67 std::ofstream{p1}; // create file
68 fs::rename(p1, p2, ec);
69 VERIFY( !ec );
70 VERIFY( !exists(p1) );
71 VERIFY( is_regular_file(p2) );
72
73 fs::remove(p2, ec);
74 }
75
76 void
77 test_symlinks()
78 {
79 #ifndef NO_SYMLINKS
80 std::error_code ec;
81 const std::error_code bad_ec = make_error_code(std::errc::invalid_argument);
82
83 const auto dir = __gnu_test::nonexistent_path();
84 fs::create_directory(dir);
85
86 create_symlink(dir/"nonesuch", dir/"link"); // dangling symlink
87 ec = bad_ec;
88 fs::rename(dir/"link", dir/"newlink", ec);
89 VERIFY( !ec );
90 VERIFY( !exists(symlink_status(dir/"link")) );
91 VERIFY( is_symlink(dir/"newlink") );
92
93 __gnu_test::scoped_file f(dir/"file");
94 create_symlink(dir/"file", dir/"link");
95 ec = bad_ec;
96 fs::rename(dir/"link", dir/"newerlink", ec);
97 VERIFY( !ec );
98 VERIFY( !exists(symlink_status(dir/"link")) );
99 VERIFY( is_symlink(dir/"newerlink") );
100 VERIFY( is_regular_file(dir/"file") );
101
102 fs::remove_all(dir, ec);
103 f.path.clear();
104 #endif
105 }
106
107 void
108 test_directories()
109 {
110 std::error_code ec;
111 const std::error_code bad_ec = make_error_code(std::errc::invalid_argument);
112
113 const auto dir = __gnu_test::nonexistent_path();
114 fs::create_directory(dir);
115 __gnu_test::scoped_file f(dir/"file");
116 fs::create_directory(dir/"subdir");
117
118 // Rename directory.
119 ec = bad_ec;
120 fs::rename(dir/"subdir", dir/"subdir2", ec);
121 VERIFY( !ec );
122 VERIFY( is_directory(dir/"subdir2") );
123 VERIFY( !exists(dir/"subdir") );
124
125 // Cannot rename a directory to a sub-directory of itself.
126 fs::rename(dir/"subdir2", dir/"subdir2/subsubdir", ec);
127 VERIFY( ec );
128 VERIFY( is_directory(dir/"subdir2") );
129 VERIFY( !exists(dir/"subdir2"/"subsubdir") );
130
131 // Cannot rename a file to the name of an existing directory.
132 ec.clear();
133 fs::rename(dir/"file", dir/"subdir2", ec);
134 VERIFY( ec );
135 VERIFY( is_directory(dir/"subdir2") );
136 VERIFY( is_regular_file(dir/"file") );
137
138 // Cannot rename a directory to the name of an existing non-directory
139 ec.clear();
140 fs::rename(dir/"subdir2", dir/"file", ec);
141 VERIFY( ec );
142 VERIFY( is_regular_file(dir/"file") );
143 VERIFY( is_directory(dir/"subdir2") );
144
145 // Cannot rename directory to the name of a non-empty directory.
146 ec.clear();
147 __gnu_test::scoped_file f2(dir/"subdir2/file");
148 fs::create_directory(dir/"subdir");
149 fs::rename(dir/"subdir", dir/"subdir2", ec);
150 VERIFY( ec );
151 VERIFY( is_directory(dir/"subdir") );
152 VERIFY( is_directory(dir/"subdir2") );
153 VERIFY( is_regular_file(dir/"subdir2/file") );
154
155 #if defined(__MINGW32__) || defined(__MINGW64__)
156 // Cannot rename a directory to an existing directory
157 #else
158 // Can rename a non-empty directory to the name of an empty directory.
159 ec = bad_ec;
160 fs::rename(dir/"subdir2", dir/"subdir", ec);
161 VERIFY( !ec );
162 VERIFY( is_directory(dir/"subdir") );
163 VERIFY( !exists(dir/"subdir2") );
164 VERIFY( is_regular_file(dir/"subdir/file") );
165 #endif
166
167 f2.path.clear();
168 f.path.clear();
169
170 fs::remove_all(dir, ec);
171 }
172
173 void
174 test_pr122726()
175 {
176 std::error_code ec;
177 const auto nonesuch = __gnu_test::nonexistent_path();
178 fs::rename(nonesuch, "new-name", ec);
179 VERIFY( ec == std::make_error_code(std::errc::no_such_file_or_directory) );
180 }
181
182 int
183 main()
184 {
185 test01();
186 test_symlinks();
187 test_directories();
188 test_pr122726();
189 }