| 1 | // Copyright (C) 2015-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 | |
| 21 | #include <filesystem> |
| 22 | #include <testsuite_hooks.h> |
| 23 | #include <testsuite_fs.h> |
| 24 | |
| 25 | namespace fs = std::filesystem; |
| 26 | |
| 27 | void |
| 28 | test01() |
| 29 | { |
| 30 | const std::error_code bad_ec = make_error_code(std::errc::invalid_argument); |
| 31 | std::error_code ec; |
| 32 | |
| 33 | // Test empty path. |
| 34 | bool b = fs::create_directories( "", ec ); |
| 35 | VERIFY( ec ); |
| 36 | VERIFY( !b ); |
| 37 | |
| 38 | // Test existing path. |
| 39 | ec = bad_ec; |
| 40 | b = fs::create_directories( fs::current_path(), ec ); |
| 41 | VERIFY( !ec ); |
| 42 | VERIFY( !b ); |
| 43 | |
| 44 | // Test non-existent path. |
| 45 | const auto p = __gnu_test::nonexistent_path(); |
| 46 | ec = bad_ec; |
| 47 | b = fs::create_directories( p, ec ); |
| 48 | VERIFY( !ec ); |
| 49 | VERIFY( b ); |
| 50 | VERIFY( is_directory(p) ); |
| 51 | |
| 52 | ec = bad_ec; |
| 53 | b = fs::create_directories( p/".", ec ); |
| 54 | VERIFY( !ec ); |
| 55 | VERIFY( !b ); |
| 56 | |
| 57 | ec = bad_ec; |
| 58 | b = fs::create_directories( p/"..", ec ); |
| 59 | VERIFY( !ec ); |
| 60 | VERIFY( !b ); |
| 61 | |
| 62 | ec = bad_ec; |
| 63 | b = fs::create_directories( p/"d1/d2/d3", ec ); |
| 64 | VERIFY( !ec ); |
| 65 | VERIFY( b ); |
| 66 | VERIFY( is_directory(p/"d1/d2/d3") ); |
| 67 | |
| 68 | ec = bad_ec; |
| 69 | b = fs::create_directories( p/"./d4/../d5", ec ); |
| 70 | VERIFY( !ec ); |
| 71 | VERIFY( b ); |
| 72 | #if defined(__MINGW32__) || defined(__MINGW64__) |
| 73 | // create_directories("./d4/..") is a no-op, does not create "d4" |
| 74 | #else |
| 75 | VERIFY( is_directory(p/"d4") ); |
| 76 | #endif |
| 77 | VERIFY( is_directory(p/"d5") ); |
| 78 | VERIFY( is_directory(p/"./d4/../d5") ); |
| 79 | |
| 80 | std::uintmax_t count = remove_all(p, ec); |
| 81 | #if defined(__MINGW32__) || defined(__MINGW64__) |
| 82 | VERIFY( count == 5 ); |
| 83 | #else |
| 84 | VERIFY( count == 6 ); |
| 85 | #endif |
| 86 | } |
| 87 | |
| 88 | void |
| 89 | test02() |
| 90 | { |
| 91 | // PR libstdc++/86910 |
| 92 | const auto p = __gnu_test::nonexistent_path(); |
| 93 | std::error_code ec; |
| 94 | bool result; |
| 95 | |
| 96 | { |
| 97 | __gnu_test::scoped_file file; |
| 98 | |
| 99 | result = create_directories(file.path, ec); |
| 100 | VERIFY( !result ); |
| 101 | VERIFY( ec == std::errc::not_a_directory ); |
| 102 | ec.clear(); |
| 103 | result = create_directories(file.path / "foo", ec); |
| 104 | VERIFY( !result ); |
| 105 | VERIFY( ec == std::errc::not_a_directory ); |
| 106 | ec.clear(); |
| 107 | } |
| 108 | |
| 109 | create_directories(p); |
| 110 | { |
| 111 | __gnu_test::scoped_file dir(p, __gnu_test::scoped_file::adopt_file); |
| 112 | __gnu_test::scoped_file file(dir.path/"file"); |
| 113 | |
| 114 | result = create_directories(file.path, ec); |
| 115 | VERIFY( !result ); |
| 116 | VERIFY( ec == std::errc::not_a_directory ); |
| 117 | ec.clear(); |
| 118 | result = create_directories(file.path/"../bar", ec); |
| 119 | #if defined(__MINGW32__) || defined(__MINGW64__) |
| 120 | VERIFY( result ); |
| 121 | VERIFY( !ec ); |
| 122 | VERIFY( is_directory(dir.path/"bar") ); |
| 123 | remove(dir.path/"bar"); |
| 124 | #else |
| 125 | VERIFY( !result ); |
| 126 | VERIFY( ec == std::errc::not_a_directory ); |
| 127 | VERIFY( !is_directory(dir.path/"bar") ); |
| 128 | #endif |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | void |
| 133 | test03() |
| 134 | { |
| 135 | // PR libstdc++/87846 |
| 136 | const auto p = __gnu_test::nonexistent_path() / ""; |
| 137 | bool result = create_directories(p); |
| 138 | VERIFY( result ); |
| 139 | VERIFY( exists(p) ); |
| 140 | remove(p); |
| 141 | result = create_directories(p/"foo/"); |
| 142 | VERIFY( result ); |
| 143 | VERIFY( exists(p) ); |
| 144 | VERIFY( exists(p/"foo") ); |
| 145 | remove_all(p); |
| 146 | } |
| 147 | |
| 148 | void |
| 149 | test04() |
| 150 | { |
| 151 | #ifndef NO_SYMLINKS |
| 152 | // PR libstdc++/101510 |
| 153 | // create_directories reports an error if the path is a symlink to a dir |
| 154 | std::error_code ec = make_error_code(std::errc::invalid_argument); |
| 155 | const auto p = __gnu_test::nonexistent_path() / ""; |
| 156 | fs::create_directories(p/"dir"); |
| 157 | auto link = p/"link"; |
| 158 | fs::create_directory_symlink("dir", link); |
| 159 | bool created = fs::create_directories(link, ec); |
| 160 | VERIFY( !created ); |
| 161 | VERIFY( !ec ); |
| 162 | created = fs::create_directories(link); |
| 163 | VERIFY( !created ); |
| 164 | remove_all(p); |
| 165 | #endif |
| 166 | } |
| 167 | |
| 168 | int |
| 169 | main() |
| 170 | { |
| 171 | test01(); |
| 172 | test02(); |
| 173 | test03(); |
| 174 | test04(); |
| 175 | } |