Debian Codegraph @

libstdc++-v3/testsuite/experimental/filesystem/iterators/error_reporting.cc

raw
1 // Copyright (C) 2020-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 <cerrno>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <dirent.h>
27 #include <testsuite_hooks.h>
28 #include <testsuite_fs.h>
29
30 int choice;
31
32 extern "C" struct dirent* readdir(DIR*)
33 {
34 // On some targets dirent::d_name is very small, but the OS allocates
35 // a trailing char array after the dirent struct. Emulate that here.
36 union State
37 {
38 struct dirent d;
39 char buf[sizeof(struct dirent) + 16] = {};
40 };
41
42 static State state;
43 char* d_name = state.buf + offsetof(struct dirent, d_name);
44
45 switch (choice)
46 {
47 case 1:
48 state.d.d_ino = 999;
49 #if defined _GLIBCXX_HAVE_STRUCT_DIRENT_D_TYPE && defined DT_REG
50 state.d.d_type = DT_REG;
51 #endif
52 state.d.d_reclen = 0;
53 std::char_traits<char>::copy(d_name, "file", 5);
54 choice = 0;
55 return &state.d;
56 case 2:
57 state.d.d_ino = 111;
58 #if defined _GLIBCXX_HAVE_STRUCT_DIRENT_D_TYPE && defined DT_DIR
59 state.d.d_type = DT_DIR;
60 #endif
61 state.d.d_reclen = 60;
62 std::char_traits<char>::copy(d_name, "subdir", 7);
63 choice = 1;
64 return &state.d;
65 default:
66 errno = EIO;
67 return nullptr;
68 }
69 return &state.d;
70 }
71
72 void
73 test01()
74 {
75 namespace fs = std::experimental::filesystem;
76 std::error_code ec;
77 choice = 1;
78 fs::recursive_directory_iterator it(".", ec);
79 if (choice == 0) // custom readdir was called
80 {
81 it.increment(ec);
82 VERIFY( ec.value() == EIO );
83 VERIFY( it == end(it) );
84 }
85 else
86 {
87 puts("Custom readdir not used, cannot test error handling");
88 exit(0);
89 }
90
91 #if __cpp_exceptions
92 choice = 1;
93 fs::recursive_directory_iterator it2(".", ec);
94 if (choice == 0) // custom readdir was called
95 {
96 try {
97 ++it2;
98 VERIFY( false );
99 } catch (const fs::filesystem_error& e) {
100 VERIFY( e.code().value() == EIO );
101 VERIFY( it2 == end(it2) );
102 }
103 }
104 #endif
105 }
106
107 void
108 test02()
109 {
110 namespace fs = std::experimental::filesystem;
111 const auto dir = __gnu_test::nonexistent_path();
112 fs::create_directories(dir/"subdir");
113
114 std::error_code ec;
115 choice = 2;
116 fs::recursive_directory_iterator it(dir, ec);
117 if (choice == 1)
118 {
119 ++it;
120 it.pop(ec);
121 VERIFY( ec.value() == EIO );
122 VERIFY( it == end(it) );
123 }
124
125 #if __cpp_exceptions
126 choice = 2;
127 fs::recursive_directory_iterator it2(dir, ec);
128 if (choice == 1)
129 {
130 ++it2;
131 try {
132 it2.pop();
133 VERIFY( false );
134 } catch (const fs::filesystem_error& e) {
135 VERIFY( e.code().value() == EIO );
136 VERIFY( it2 == end(it2) );
137 }
138 }
139 #endif
140
141 // Cannot use fs::remove_all here because that depends on
142 // recursive_directory_iterator which would use the fake readdir above.
143 #ifndef _GLIBCXX_FILESYSTEM_IS_WINDOWS
144 ::rmdir((dir/"subdir").c_str());
145 ::rmdir(dir.c_str());
146 #endif
147 }
148
149 int
150 main()
151 {
152 test01();
153 test02();
154 }