aboutsummaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/experimental/filesystem/operations/rename.cc
blob: 84ffce1e97b81eed40c0d3a8171ce096f6442eb8 (plain)
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
// Copyright (C) 2021-2025 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library 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, or (at your option)
// any later version.

// This library 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 library; see the file COPYING3.  If not see
// <http://www.gnu.org/licenses/>.

// { dg-options "-DUSE_FILESYSTEM_TS -lstdc++fs" }
// { dg-do run { target c++11 } }
// { dg-require-filesystem-ts "" }
// { dg-xfail-run-if "rename is not POSIX-compliant" { *-*-rtems* } }

#include <experimental/filesystem>
#include <testsuite_hooks.h>
#include <testsuite_fs.h>

namespace fs = std::experimental::filesystem;

void
test01()
{
  std::error_code ec;
  const std::error_code bad_ec = make_error_code(std::errc::invalid_argument);

  auto p1 = __gnu_test::nonexistent_path();
  auto p2 = __gnu_test::nonexistent_path();

  fs::rename(p1, p2, ec);
  VERIFY( ec );

  ec.clear();
  fs::rename(p1, "", ec);
  VERIFY( ec );

  ec.clear();
  fs::rename("", p1, ec);
  VERIFY( ec );

  ec = bad_ec;
  std::ofstream{p1}; // create file
  fs::rename(p1, p1, ec); // no-op
  VERIFY( !ec );
  VERIFY( is_regular_file(p1) );

  ec.clear();
  rename(p2, p1, ec);
  VERIFY( ec );
  VERIFY( is_regular_file(p1) );

  ec = bad_ec;
  fs::rename(p1, p2, ec);
  VERIFY( !ec );
  VERIFY( !exists(p1) );
  VERIFY( is_regular_file(p2) );

  ec = bad_ec;
  std::ofstream{p1}; // create file
  fs::rename(p1, p2, ec);
  VERIFY( !ec );
  VERIFY( !exists(p1) );
  VERIFY( is_regular_file(p2) );

  fs::remove(p2, ec);
}

void
test_symlinks()
{
#ifndef NO_SYMLINKS
  std::error_code ec;
  const std::error_code bad_ec = make_error_code(std::errc::invalid_argument);

  const auto dir = __gnu_test::nonexistent_path();
  fs::create_directory(dir);

  create_symlink(dir/"nonesuch", dir/"link");  // dangling symlink
  ec = bad_ec;
  fs::rename(dir/"link", dir/"newlink", ec);
  VERIFY( !ec );
  VERIFY( !exists(symlink_status(dir/"link")) );
  VERIFY( is_symlink(dir/"newlink") );

  __gnu_test::scoped_file f(dir/"file");
  create_symlink(dir/"file", dir/"link");
  ec = bad_ec;
  fs::rename(dir/"link", dir/"newerlink", ec);
  VERIFY( !ec );
  VERIFY( !exists(symlink_status(dir/"link")) );
  VERIFY( is_symlink(dir/"newerlink") );
  VERIFY( is_regular_file(dir/"file") );

  fs::remove_all(dir, ec);
  f.path.clear();
#endif
}

void
test_directories()
{
  std::error_code ec;
  const std::error_code bad_ec = make_error_code(std::errc::invalid_argument);

  const auto dir = __gnu_test::nonexistent_path();
  fs::create_directory(dir);
  __gnu_test::scoped_file f(dir/"file");
  fs::create_directory(dir/"subdir");

  // Rename directory.
  ec = bad_ec;
  fs::rename(dir/"subdir", dir/"subdir2", ec);
  VERIFY( !ec );
  VERIFY( is_directory(dir/"subdir2") );
  VERIFY( !exists(dir/"subdir") );

  // Cannot rename a directory to a sub-directory of itself.
  fs::rename(dir/"subdir2", dir/"subdir2/subsubdir", ec);
  VERIFY( ec );
  VERIFY( is_directory(dir/"subdir2") );
  VERIFY( !exists(dir/"subdir2"/"subsubdir") );

  // Cannot rename a file to the name of an existing directory.
  ec.clear();
  fs::rename(dir/"file", dir/"subdir2", ec);
  VERIFY( ec );
  VERIFY( is_directory(dir/"subdir2") );
  VERIFY( is_regular_file(dir/"file") );

#if defined(__MINGW32__) || defined(__MINGW64__)
  // XXX broken on Windows, see PR 98985
#else
  // Cannot rename a directory to the name of an existing non-directory
  ec.clear();
  fs::rename(dir/"subdir2", dir/"file", ec);
  VERIFY( ec );
  VERIFY( is_regular_file(dir/"file") );
  VERIFY( is_directory(dir/"subdir2") );

  // Cannot rename directory to the name of a non-empty directory.
  ec.clear();
  __gnu_test::scoped_file f2(dir/"subdir2/file");
  fs::create_directory(dir/"subdir");
  fs::rename(dir/"subdir", dir/"subdir2", ec);
  VERIFY( ec );
  VERIFY( is_directory(dir/"subdir") );
  VERIFY( is_directory(dir/"subdir2") );
  VERIFY( is_regular_file(dir/"subdir2/file") );

  // Can rename a non-empty directory to the name of an empty directory.
  ec = bad_ec;
  fs::rename(dir/"subdir2", dir/"subdir", ec);
  VERIFY( !ec );
  VERIFY( is_directory(dir/"subdir") );
  VERIFY( !exists(dir/"subdir2") );
  VERIFY( is_regular_file(dir/"subdir/file") );
  f2.path.clear();

  f.path.clear();
#endif

  fs::remove_all(dir, ec);
}

int
main()
{
  test01();
  test_symlinks();
  test_directories();
}