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