| 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-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 |
fs::path p; |
| 35 |
bool b = create_directory( p, ec ); |
| 36 |
VERIFY( ec ); |
| 37 |
VERIFY( !b ); |
| 38 |
|
| 39 |
// Test non-existent path |
| 40 |
p = __gnu_test::nonexistent_path(); |
| 41 |
VERIFY( !exists(p) ); |
| 42 |
|
| 43 |
ec = bad_ec; |
| 44 |
b = create_directory(p, ec); // create the directory once |
| 45 |
VERIFY( !ec ); |
| 46 |
VERIFY( b ); |
| 47 |
VERIFY( exists(p) ); |
| 48 |
|
| 49 |
// Test existing path (libstdc++/71036). |
| 50 |
ec = bad_ec; |
| 51 |
b = create_directory(p, ec); |
| 52 |
VERIFY( !ec ); |
| 53 |
VERIFY( !b ); |
| 54 |
b = create_directory(p); |
| 55 |
VERIFY( !b ); |
| 56 |
|
| 57 |
auto f = p/"file"; |
| 58 |
std::ofstream{f} << "create file"; |
| 59 |
b = create_directory(f, ec); |
| 60 |
VERIFY( ec == std::errc::file_exists ); |
| 61 |
VERIFY( !b ); |
| 62 |
try |
| 63 |
{ |
| 64 |
create_directory(f); |
| 65 |
VERIFY( false ); |
| 66 |
} |
| 67 |
catch (const fs::filesystem_error& e) |
| 68 |
{ |
| 69 |
VERIFY( e.code() == std::errc::file_exists ); |
| 70 |
VERIFY( e.path1() == f ); |
| 71 |
} |
| 72 |
|
| 73 |
#ifndef NO_SYMLINKS |
| 74 |
// PR libstdc++/101510 create_directory on an existing symlink to a directory |
| 75 |
fs::create_directory(p/"dir"); |
| 76 |
auto link = p/"link"; |
| 77 |
fs::create_directory_symlink("dir", link); |
| 78 |
ec = bad_ec; |
| 79 |
b = fs::create_directory(link, ec); |
| 80 |
VERIFY( !b ); |
| 81 |
VERIFY( !ec ); |
| 82 |
b = fs::create_directory(link); |
| 83 |
VERIFY( !b ); |
| 84 |
#endif |
| 85 |
|
| 86 |
remove_all(p, ec); |
| 87 |
} |
| 88 |
|
| 89 |
int |
| 90 |
main() |
| 91 |
{ |
| 92 |
test01(); |
| 93 |
} |