Debian Codegraph @

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