]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/experimental/filesystem/operations/remove_all.cc
libstdc++: Avoid warnings in tests
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / experimental / filesystem / operations / remove_all.cc
1 // Copyright (C) 2016-2020 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-options "-DUSE_FILESYSTEM_TS -lstdc++fs" }
19 // { dg-do run { target c++11 } }
20 // { dg-require-filesystem-ts "" }
21
22 #include <experimental/filesystem>
23 #include <testsuite_hooks.h>
24 #include <testsuite_fs.h>
25
26 namespace fs = std::experimental::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 std::uintmax_t n;
34
35 n = fs::remove_all("", ec);
36 VERIFY( !ec ); // This seems odd, but is what the TS requires.
37 VERIFY( n == 0 );
38
39 auto p = __gnu_test::nonexistent_path();
40 ec = bad_ec;
41 n = remove_all(p, ec);
42 VERIFY( !ec );
43 VERIFY( n == 0 );
44
45 #if defined(__MINGW32__) || defined(__MINGW64__)
46 // No symlink support
47 #else
48 auto link = __gnu_test::nonexistent_path();
49 create_symlink(p, link); // dangling symlink
50 ec = bad_ec;
51 n = remove_all(link, ec);
52 VERIFY( !ec );
53 VERIFY( n == 1 );
54 VERIFY( !exists(symlink_status(link)) ); // DR 2721
55
56 __gnu_test::scoped_file f(p);
57 create_symlink(p, link);
58 ec = bad_ec;
59 n = remove_all(link, ec);
60 VERIFY( !ec );
61 VERIFY( n == 1 );
62 VERIFY( !exists(symlink_status(link)) ); // The symlink is removed, but
63 VERIFY( exists(p) ); // its target is not.
64 #endif
65
66 const auto dir = __gnu_test::nonexistent_path();
67 create_directories(dir/"a/b/c");
68 ec = bad_ec;
69 n = remove_all(dir/"a", ec);
70 VERIFY( !ec );
71 VERIFY( n == 3 );
72 VERIFY( exists(dir) );
73 VERIFY( !exists(dir/"a") );
74
75 create_directories(dir/"a/b/c");
76 __gnu_test::scoped_file a1(dir/"a/1");
77 __gnu_test::scoped_file a2(dir/"a/2");
78 __gnu_test::scoped_file b1(dir/"a/b/1");
79 __gnu_test::scoped_file b2(dir/"a/b/2");
80 ec = bad_ec;
81 n = remove_all(dir, ec);
82 VERIFY( !ec );
83 VERIFY( n == 8 );
84 VERIFY( !exists(dir) );
85
86 a1.path.clear();
87 a2.path.clear();
88 b1.path.clear();
89 b2.path.clear();
90 }
91
92 void
93 test02()
94 {
95 const auto dir = __gnu_test::nonexistent_path();
96 create_directories(dir/"a/b/c");
97 std::uintmax_t n = remove_all(dir/"a");
98 VERIFY( n == 3 );
99 VERIFY( exists(dir) );
100 VERIFY( !exists(dir/"a") );
101
102 n = remove_all(dir/"a");
103 VERIFY( n == 0 );
104 VERIFY( exists(dir) );
105
106 n = remove_all(dir);
107 VERIFY( n == 1 );
108 VERIFY( !exists(dir) );
109 }
110
111 void
112 test04()
113 {
114 #if defined(__MINGW32__) || defined(__MINGW64__)
115 // no permissions
116 #else
117 // PR libstdc++/93201
118 std::error_code ec;
119 std::uintmax_t n;
120
121 auto dir = __gnu_test::nonexistent_path();
122 fs::create_directory(dir);
123 __gnu_test::scoped_file f(dir/"file");
124 // remove write permission on the directory:
125 fs::permissions(dir, fs::perms::owner_read|fs::perms::owner_exec);
126 n = fs::remove_all(dir, ec);
127 VERIFY( n == std::uintmax_t(-1) );
128 VERIFY( ec == std::errc::permission_denied ); // not ENOTEMPTY
129
130 try {
131 fs::remove_all(dir);
132 VERIFY( false );
133 } catch (const fs::filesystem_error& e) {
134 VERIFY( e.code() == std::errc::permission_denied );
135 // First path is the argument to remove_all
136 VERIFY( e.path1() == dir );
137 }
138
139 fs::permissions(dir, fs::perms::owner_write|fs::perms::add_perms);
140 #endif
141 }
142
143 int
144 main()
145 {
146 test01();
147 test02();
148 test04();
149 }