]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/experimental/filesystem/operations/rename.cc
1 // Copyright (C) 2021-2024 Free Software Foundation, Inc.
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)
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.
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/>.
18 // { dg-options "-DUSE_FILESYSTEM_TS -lstdc++fs" }
19 // { dg-do run { target c++11 } }
20 // { dg-require-filesystem-ts "" }
21 // { dg-xfail-run-if "rename is not POSIX-compliant" { *-*-rtems* } }
23 #include <experimental/filesystem>
24 #include <testsuite_hooks.h>
25 #include <testsuite_fs.h>
27 namespace fs
= std::experimental::filesystem
;
33 const std::error_code bad_ec
= make_error_code(std::errc::invalid_argument
);
35 auto p1
= __gnu_test::nonexistent_path();
36 auto p2
= __gnu_test::nonexistent_path();
38 fs::rename(p1
, p2
, ec
);
42 fs::rename(p1
, "", ec
);
46 fs::rename("", p1
, ec
);
50 std::ofstream
{p1
}; // create file
51 fs::rename(p1
, p1
, ec
); // no-op
53 VERIFY( is_regular_file(p1
) );
58 VERIFY( is_regular_file(p1
) );
61 fs::rename(p1
, p2
, ec
);
63 VERIFY( !exists(p1
) );
64 VERIFY( is_regular_file(p2
) );
67 std::ofstream
{p1
}; // create file
68 fs::rename(p1
, p2
, ec
);
70 VERIFY( !exists(p1
) );
71 VERIFY( is_regular_file(p2
) );
81 const std::error_code bad_ec
= make_error_code(std::errc::invalid_argument
);
83 const auto dir
= __gnu_test::nonexistent_path();
84 fs::create_directory(dir
);
86 create_symlink(dir
/"nonesuch", dir
/"link"); // dangling symlink
88 fs::rename(dir
/"link", dir
/"newlink", ec
);
90 VERIFY( !exists(symlink_status(dir
/"link")) );
91 VERIFY( is_symlink(dir
/"newlink") );
93 __gnu_test::scoped_file
f(dir
/"file");
94 create_symlink(dir
/"file", dir
/"link");
96 fs::rename(dir
/"link", dir
/"newerlink", ec
);
98 VERIFY( !exists(symlink_status(dir
/"link")) );
99 VERIFY( is_symlink(dir
/"newerlink") );
100 VERIFY( is_regular_file(dir
/"file") );
102 fs::remove_all(dir
, ec
);
111 const std::error_code bad_ec
= make_error_code(std::errc::invalid_argument
);
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");
120 fs::rename(dir
/"subdir", dir
/"subdir2", ec
);
122 VERIFY( is_directory(dir
/"subdir2") );
123 VERIFY( !exists(dir
/"subdir") );
125 // Cannot rename a directory to a sub-directory of itself.
126 fs::rename(dir
/"subdir2", dir
/"subdir2/subsubdir", ec
);
128 VERIFY( is_directory(dir
/"subdir2") );
129 VERIFY( !exists(dir
/"subdir2"/"subsubdir") );
131 // Cannot rename a file to the name of an existing directory.
133 fs::rename(dir
/"file", dir
/"subdir2", ec
);
135 VERIFY( is_directory(dir
/"subdir2") );
136 VERIFY( is_regular_file(dir
/"file") );
138 #if defined(__MINGW32__) || defined(__MINGW64__)
139 // XXX broken on Windows, see PR 98985
141 // Cannot rename a directory to the name of an existing non-directory
143 fs::rename(dir
/"subdir2", dir
/"file", ec
);
145 VERIFY( is_regular_file(dir
/"file") );
146 VERIFY( is_directory(dir
/"subdir2") );
148 // Cannot rename directory to the name of a non-empty directory.
150 __gnu_test::scoped_file
f2(dir
/"subdir2/file");
151 fs::create_directory(dir
/"subdir");
152 fs::rename(dir
/"subdir", dir
/"subdir2", ec
);
154 VERIFY( is_directory(dir
/"subdir") );
155 VERIFY( is_directory(dir
/"subdir2") );
156 VERIFY( is_regular_file(dir
/"subdir2/file") );
158 // Can rename a non-empty directory to the name of an empty directory.
160 fs::rename(dir
/"subdir2", dir
/"subdir", ec
);
162 VERIFY( is_directory(dir
/"subdir") );
163 VERIFY( !exists(dir
/"subdir2") );
164 VERIFY( is_regular_file(dir
/"subdir/file") );
170 fs::remove_all(dir
, ec
);