| 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 | std::error_code ec = make_error_code(std::errc::invalid_argument); |
| 31 | fs::path dot = "."; |
| 32 | |
| 33 | fs::file_status st1 = fs::status(dot, ec); |
| 34 | VERIFY( !ec ); |
| 35 | VERIFY( st1.type() == fs::file_type::directory ); |
| 36 | |
| 37 | fs::file_status st2 = fs::status(dot); |
| 38 | VERIFY( st2.type() == fs::file_type::directory ); |
| 39 | } |
| 40 | |
| 41 | void |
| 42 | test02() |
| 43 | { |
| 44 | fs::path p = __gnu_test::nonexistent_path(); |
| 45 | |
| 46 | std::error_code ec; |
| 47 | fs::file_status st1 = fs::status(p, ec); |
| 48 | VERIFY( ec ); |
| 49 | VERIFY( st1.type() == fs::file_type::not_found ); |
| 50 | |
| 51 | fs::file_status st2 = fs::status(p); |
| 52 | VERIFY( st2.type() == fs::file_type::not_found ); |
| 53 | } |
| 54 | |
| 55 | void |
| 56 | test03() |
| 57 | { |
| 58 | if (!__gnu_test::permissions_are_testable()) |
| 59 | return; |
| 60 | |
| 61 | fs::path dir = __gnu_test::nonexistent_path(); |
| 62 | fs::create_directory(dir); |
| 63 | __gnu_test::scoped_file d(dir, __gnu_test::scoped_file::adopt_file); |
| 64 | __gnu_test::scoped_file f(dir / "file"); |
| 65 | fs::permissions(dir, fs::perms::none); |
| 66 | |
| 67 | std::error_code ec; |
| 68 | fs::file_status st = fs::status(f.path, ec); |
| 69 | VERIFY( ec.value() == (int)std::errc::permission_denied ); |
| 70 | VERIFY( st.type() == fs::file_type::none ); |
| 71 | |
| 72 | #if __cpp_exceptions |
| 73 | bool caught = false; |
| 74 | std::error_code ec2; |
| 75 | fs::path p, p2; |
| 76 | try { |
| 77 | (void) fs::symlink_status(f.path); |
| 78 | } catch (const fs::filesystem_error& e) { |
| 79 | caught = true; |
| 80 | p = e.path1(); |
| 81 | p2 = e.path2(); |
| 82 | ec2 = e.code(); |
| 83 | } |
| 84 | VERIFY( caught ); |
| 85 | VERIFY( ec2 == ec ); |
| 86 | VERIFY( p == f.path ); |
| 87 | VERIFY( p2.empty() ); |
| 88 | #endif |
| 89 | |
| 90 | fs::permissions(dir, fs::perms::owner_all, ec); |
| 91 | } |
| 92 | |
| 93 | void |
| 94 | test04() |
| 95 | { |
| 96 | // PR libstdc++/88881 |
| 97 | fs::path p = "./"; |
| 98 | auto st = status(p); |
| 99 | VERIFY( is_directory(st) ); |
| 100 | } |
| 101 | |
| 102 | int |
| 103 | main() |
| 104 | { |
| 105 | test01(); |
| 106 | test02(); |
| 107 | test03(); |
| 108 | test04(); |
| 109 | } |