| 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 |
using __gnu_test::compare_paths; |
| 27 |
|
| 28 |
void |
| 29 |
test01() |
| 30 |
{ |
| 31 |
const std::error_code bad_ec = make_error_code(std::errc::invalid_argument); |
| 32 |
std::error_code ec; |
| 33 |
auto p = __gnu_test::nonexistent_path(); |
| 34 |
(void) canonical( p, ec ); |
| 35 |
VERIFY( ec ); |
| 36 |
|
| 37 |
create_directory(p); |
| 38 |
__gnu_test::scoped_file l(p, __gnu_test::scoped_file::adopt_file); |
| 39 |
auto p2 = canonical( p, ec ); |
| 40 |
compare_paths( p2, fs::current_path()/p ); |
| 41 |
VERIFY( !ec ); |
| 42 |
|
| 43 |
ec = bad_ec; |
| 44 |
p2 = canonical( fs::current_path() / "." / (p.string() + "////././."), ec ); |
| 45 |
compare_paths( p2, fs::current_path()/p ); |
| 46 |
VERIFY( !ec ); |
| 47 |
|
| 48 |
ec = bad_ec; |
| 49 |
p = fs::current_path(); |
| 50 |
p2 = canonical( p, ec ); |
| 51 |
compare_paths( p2, p ); |
| 52 |
VERIFY( !ec ); |
| 53 |
|
| 54 |
const auto root = fs::absolute("/"); |
| 55 |
|
| 56 |
ec = bad_ec; |
| 57 |
p = "/"; |
| 58 |
p = canonical( p, ec ); |
| 59 |
compare_paths( p, root ); |
| 60 |
VERIFY( !ec ); |
| 61 |
|
| 62 |
ec = bad_ec; |
| 63 |
p = "/."; |
| 64 |
p = canonical( p, ec ); |
| 65 |
compare_paths( p, root ); |
| 66 |
VERIFY( !ec ); |
| 67 |
|
| 68 |
ec = bad_ec; |
| 69 |
p = "/.."; |
| 70 |
p = canonical( p, ec ); |
| 71 |
compare_paths( p, root ); |
| 72 |
VERIFY( !ec ); |
| 73 |
|
| 74 |
ec = bad_ec; |
| 75 |
p = "/../.././."; |
| 76 |
p = canonical( p, ec ); |
| 77 |
compare_paths( p, root ); |
| 78 |
VERIFY( !ec ); |
| 79 |
} |
| 80 |
|
| 81 |
void |
| 82 |
test02() |
| 83 |
{ |
| 84 |
const fs::path p = __gnu_test::nonexistent_path(); |
| 85 |
std::error_code ec, ec2; |
| 86 |
const fs::path res = canonical(p, ec); |
| 87 |
VERIFY( ec ); |
| 88 |
VERIFY( res.empty() ); |
| 89 |
|
| 90 |
#if __cpp_exceptions |
| 91 |
fs::path e1, e2; |
| 92 |
try { |
| 93 |
(void) canonical(p); |
| 94 |
} catch (const fs::filesystem_error& e) { |
| 95 |
e1 = e.path1(); |
| 96 |
e2 = e.path2(); |
| 97 |
ec2 = e.code(); |
| 98 |
} |
| 99 |
VERIFY( e1 == p ); |
| 100 |
VERIFY( e2.empty() ); |
| 101 |
VERIFY( ec == ec2 ); |
| 102 |
#endif |
| 103 |
} |
| 104 |
|
| 105 |
void |
| 106 |
test03() |
| 107 |
{ |
| 108 |
std::error_code ec; |
| 109 |
auto dir = __gnu_test::nonexistent_path(); |
| 110 |
fs::create_directory(dir); |
| 111 |
fs::path foo = dir/"foo", bar = dir/"bar"; |
| 112 |
fs::create_directory(foo); |
| 113 |
fs::create_directory(bar); |
| 114 |
#ifdef NO_SYMLINKS |
| 115 |
#if defined(__MINGW32__) || defined(__MINGW64__) |
| 116 |
const fs::path baz = dir/"foo\\\\..\\bar///"; |
| 117 |
#else |
| 118 |
const fs::path baz = dir/"foo//../bar///"; |
| 119 |
#endif |
| 120 |
#else |
| 121 |
fs::create_symlink("../bar", foo/"baz"); |
| 122 |
const fs::path baz = dir/"foo//./baz///"; |
| 123 |
#endif |
| 124 |
|
| 125 |
auto dirc = canonical(dir); |
| 126 |
auto barc = canonical(bar); |
| 127 |
|
| 128 |
auto p1 = fs::canonical(dir/"foo//.///..//./"); |
| 129 |
compare_paths( p1, dirc ); |
| 130 |
auto p2 = fs::canonical(baz/"..//./"); |
| 131 |
compare_paths( p2, dirc ); |
| 132 |
auto p3 = fs::canonical(baz/"./"); |
| 133 |
compare_paths( p3, barc ); |
| 134 |
auto p4 = fs::canonical(baz/"..//./bar"); |
| 135 |
compare_paths( p4, barc ); |
| 136 |
auto p5 = fs::canonical(baz/"..//./bar/"); |
| 137 |
compare_paths( p5, p4 ); |
| 138 |
auto p6 = fs::canonical(baz/"..//./bar/."); |
| 139 |
compare_paths( p6, p4 ); |
| 140 |
|
| 141 |
remove_all(dir); |
| 142 |
} |
| 143 |
|
| 144 |
int |
| 145 |
main() |
| 146 |
{ |
| 147 |
test01(); |
| 148 |
test02(); |
| 149 |
test03(); |
| 150 |
} |