| 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 | using std::filesystem::path; |
| 26 | |
| 27 | void |
| 28 | test01() |
| 29 | { |
| 30 | const std::error_code bad_ec = make_error_code(std::errc::invalid_argument); |
| 31 | const path root = __gnu_test::root_path(); |
| 32 | |
| 33 | VERIFY( exists(root) ); |
| 34 | VERIFY( exists(root/".") ); |
| 35 | VERIFY( exists(path{"."}) ); |
| 36 | VERIFY( exists(path{".."}) ); |
| 37 | VERIFY( exists(std::filesystem::current_path()) ); |
| 38 | |
| 39 | std::error_code ec; |
| 40 | ec = bad_ec; |
| 41 | VERIFY( exists(root, ec) ); |
| 42 | VERIFY( !ec ); |
| 43 | ec = bad_ec; |
| 44 | VERIFY( exists(root/".", ec) ); |
| 45 | VERIFY( !ec ); |
| 46 | ec = bad_ec; |
| 47 | VERIFY( exists(path{"."}, ec) ); |
| 48 | VERIFY( !ec ); |
| 49 | ec = bad_ec; |
| 50 | VERIFY( exists(path{".."}, ec) ); |
| 51 | VERIFY( !ec ); |
| 52 | ec = bad_ec; |
| 53 | VERIFY( exists(std::filesystem::current_path(), ec) ); |
| 54 | VERIFY( !ec ); |
| 55 | } |
| 56 | |
| 57 | void |
| 58 | test02() |
| 59 | { |
| 60 | path rel = __gnu_test::nonexistent_path(); |
| 61 | VERIFY( !exists(rel) ); |
| 62 | |
| 63 | std::error_code ec = std::make_error_code(std::errc::invalid_argument); |
| 64 | VERIFY( !exists(rel, ec) ); |
| 65 | VERIFY( !ec ); // DR 2725 |
| 66 | } |
| 67 | |
| 68 | void |
| 69 | test03() |
| 70 | { |
| 71 | path abs = absolute(__gnu_test::nonexistent_path()); |
| 72 | VERIFY( !exists(abs) ); |
| 73 | |
| 74 | std::error_code ec = std::make_error_code(std::errc::invalid_argument); |
| 75 | VERIFY( !exists(abs, ec) ); |
| 76 | VERIFY( !ec ); // DR 2725 |
| 77 | } |
| 78 | |
| 79 | void |
| 80 | test04() |
| 81 | { |
| 82 | if (!__gnu_test::permissions_are_testable()) |
| 83 | return; |
| 84 | |
| 85 | using std::filesystem::perms; |
| 86 | using std::filesystem::perm_options; |
| 87 | path p = __gnu_test::nonexistent_path(); |
| 88 | create_directory(p); |
| 89 | permissions(p, perms::all, perm_options::remove); |
| 90 | |
| 91 | auto unr = p / "unreachable"; |
| 92 | std::error_code ec; |
| 93 | VERIFY( !exists(unr, ec) ); |
| 94 | VERIFY( ec == std::errc::permission_denied ); |
| 95 | ec.clear(); |
| 96 | try |
| 97 | { |
| 98 | (void) exists(unr); |
| 99 | } |
| 100 | catch(const std::filesystem::filesystem_error& ex) |
| 101 | { |
| 102 | ec = ex.code(); |
| 103 | VERIFY( ex.path1() == unr ); |
| 104 | } |
| 105 | VERIFY( ec == std::errc::permission_denied ); |
| 106 | |
| 107 | permissions(p, perms::owner_all); |
| 108 | remove(p); |
| 109 | } |
| 110 | |
| 111 | int |
| 112 | main() |
| 113 | { |
| 114 | test01(); |
| 115 | test02(); |
| 116 | test03(); |
| 117 | test04(); |
| 118 | } |