| 1 |
// Copyright (C) 2016-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-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 |
#ifndef NO_SYMLINKS |
| 46 |
auto link = __gnu_test::nonexistent_path(); |
| 47 |
create_symlink(p, link); // dangling symlink |
| 48 |
ec = bad_ec; |
| 49 |
n = remove_all(link, ec); |
| 50 |
VERIFY( !ec ); |
| 51 |
VERIFY( n == 1 ); |
| 52 |
VERIFY( !exists(symlink_status(link)) ); // DR 2721 |
| 53 |
|
| 54 |
__gnu_test::scoped_file f(p); |
| 55 |
create_symlink(p, link); |
| 56 |
ec = bad_ec; |
| 57 |
n = remove_all(link, ec); |
| 58 |
VERIFY( !ec ); |
| 59 |
VERIFY( n == 1 ); |
| 60 |
VERIFY( !exists(symlink_status(link)) ); // The symlink is removed, but |
| 61 |
VERIFY( exists(p) ); // its target is not. |
| 62 |
#endif |
| 63 |
|
| 64 |
const auto dir = __gnu_test::nonexistent_path(); |
| 65 |
create_directories(dir/"a/b/c"); |
| 66 |
ec = bad_ec; |
| 67 |
n = remove_all(dir/"a", ec); |
| 68 |
VERIFY( !ec ); |
| 69 |
VERIFY( n == 3 ); |
| 70 |
VERIFY( exists(dir) ); |
| 71 |
VERIFY( !exists(dir/"a") ); |
| 72 |
|
| 73 |
create_directories(dir/"a/b/c"); |
| 74 |
__gnu_test::scoped_file a1(dir/"a/1"); |
| 75 |
__gnu_test::scoped_file a2(dir/"a/2"); |
| 76 |
__gnu_test::scoped_file b1(dir/"a/b/1"); |
| 77 |
__gnu_test::scoped_file b2(dir/"a/b/2"); |
| 78 |
ec = bad_ec; |
| 79 |
n = remove_all(dir, ec); |
| 80 |
VERIFY( !ec ); |
| 81 |
VERIFY( n == 8 ); |
| 82 |
VERIFY( !exists(dir) ); |
| 83 |
|
| 84 |
a1.path.clear(); |
| 85 |
a2.path.clear(); |
| 86 |
b1.path.clear(); |
| 87 |
b2.path.clear(); |
| 88 |
} |
| 89 |
|
| 90 |
void |
| 91 |
test02() |
| 92 |
{ |
| 93 |
const auto dir = __gnu_test::nonexistent_path(); |
| 94 |
create_directories(dir/"a/b/c"); |
| 95 |
std::uintmax_t n = remove_all(dir/"a"); |
| 96 |
VERIFY( n == 3 ); |
| 97 |
VERIFY( exists(dir) ); |
| 98 |
VERIFY( !exists(dir/"a") ); |
| 99 |
|
| 100 |
n = remove_all(dir/"a"); |
| 101 |
VERIFY( n == 0 ); |
| 102 |
VERIFY( exists(dir) ); |
| 103 |
|
| 104 |
n = remove_all(dir); |
| 105 |
VERIFY( n == 1 ); |
| 106 |
VERIFY( !exists(dir) ); |
| 107 |
} |
| 108 |
|
| 109 |
void |
| 110 |
test04() |
| 111 |
{ |
| 112 |
if (!__gnu_test::permissions_are_testable()) |
| 113 |
return; |
| 114 |
|
| 115 |
// PR libstdc++/93201 |
| 116 |
std::error_code ec; |
| 117 |
std::uintmax_t n; |
| 118 |
|
| 119 |
auto dir = __gnu_test::nonexistent_path(); |
| 120 |
fs::create_directory(dir); |
| 121 |
__gnu_test::scoped_file f(dir/"file"); |
| 122 |
// remove write permission on the directory: |
| 123 |
fs::permissions(dir, fs::perms::owner_read|fs::perms::owner_exec); |
| 124 |
n = fs::remove_all(dir, ec); |
| 125 |
VERIFY( n == std::uintmax_t(-1) ); |
| 126 |
VERIFY( ec == std::errc::permission_denied ); // not ENOTEMPTY |
| 127 |
|
| 128 |
try { |
| 129 |
fs::remove_all(dir); |
| 130 |
VERIFY( false ); |
| 131 |
} catch (const fs::filesystem_error& e) { |
| 132 |
VERIFY( e.code() == std::errc::permission_denied ); |
| 133 |
// First path is the argument to remove_all |
| 134 |
VERIFY( e.path1() == dir ); |
| 135 |
} |
| 136 |
|
| 137 |
fs::permissions(dir, fs::perms::owner_write|fs::perms::add_perms); |
| 138 |
fs::remove_all(dir, ec); |
| 139 |
f.path.clear(); |
| 140 |
} |
| 141 |
|
| 142 |
int |
| 143 |
main() |
| 144 |
{ |
| 145 |
test01(); |
| 146 |
test02(); |
| 147 |
test04(); |
| 148 |
} |