2018-10-09 Richard Biener <rguenther@suse.de>
[official-gcc.git] / libstdc++-v3 / src / filesystem / dir-common.h
blob03875819d04fb205e0057a366439cadfc9cefd09
1 // Filesystem directory iterator utilities -*- C++ -*-
3 // Copyright (C) 2014-2018 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 #ifndef _GLIBCXX_DIR_COMMON_H
26 #define _GLIBCXX_DIR_COMMON_H 1
28 #include <string.h> // strcmp
29 #if _GLIBCXX_FILESYSTEM_IS_WINDOWS
30 #include <wchar.h> // wcscmp
31 #endif
32 #ifdef _GLIBCXX_HAVE_DIRENT_H
33 # ifdef _GLIBCXX_HAVE_SYS_TYPES_H
34 # include <sys/types.h>
35 # endif
36 # include <dirent.h>
37 #else
38 # error "the <dirent.h> header is needed to build the Filesystem TS"
39 #endif
41 namespace std _GLIBCXX_VISIBILITY(default)
43 _GLIBCXX_BEGIN_NAMESPACE_VERSION
44 namespace filesystem
46 namespace __gnu_posix
48 #if _GLIBCXX_FILESYSTEM_IS_WINDOWS
49 // Adapt the Windows _wxxx functions to look like POSIX xxx, but for wchar_t*.
50 using char_type = wchar_t;
51 using DIR = ::_WDIR;
52 using dirent = _wdirent;
53 inline DIR* opendir(const wchar_t* path) { return ::_wopendir(path); }
54 inline dirent* readdir(DIR* dir) { return ::_wreaddir(dir); }
55 inline int closedir(DIR* dir) { return ::_wclosedir(dir); }
56 #else
57 using char_type = char;
58 using DIR = ::DIR;
59 typedef struct ::dirent dirent;
60 using ::opendir;
61 using ::readdir;
62 using ::closedir;
63 #endif
64 } // namespace __gnu_posix
66 namespace posix = __gnu_posix;
68 struct _Dir_base
70 _Dir_base(posix::DIR* dirp = nullptr) : dirp(dirp) { }
72 // If no error occurs then dirp is non-null,
73 // otherwise null (whether error ignored or not).
74 _Dir_base(const posix::char_type* pathname, bool skip_permission_denied,
75 error_code& ec) noexcept
76 : dirp(posix::opendir(pathname))
78 if (dirp)
79 ec.clear();
80 else
82 const int err = errno;
83 if (err == EACCES && skip_permission_denied)
84 ec.clear();
85 else
86 ec.assign(err, std::generic_category());
90 _Dir_base(_Dir_base&& d) : dirp(std::exchange(d.dirp, nullptr)) { }
92 _Dir_base& operator=(_Dir_base&&) = delete;
94 ~_Dir_base() { if (dirp) posix::closedir(dirp); }
96 const posix::dirent*
97 advance(bool skip_permission_denied, error_code& ec) noexcept
99 ec.clear();
101 int err = std::exchange(errno, 0);
102 const posix::dirent* entp = posix::readdir(dirp);
103 // std::swap cannot be used with Bionic's errno
104 err = std::exchange(errno, err);
106 if (entp)
108 // skip past dot and dot-dot
109 if (is_dot_or_dotdot(entp->d_name))
110 return advance(skip_permission_denied, ec);
111 return entp;
113 else if (err)
115 if (err == EACCES && skip_permission_denied)
116 return nullptr;
117 ec.assign(err, std::generic_category());
118 return nullptr;
120 else
122 // reached the end
123 return nullptr;
127 static bool is_dot_or_dotdot(const char* s) noexcept
128 { return !strcmp(s, ".") || !strcmp(s, ".."); }
130 #if _GLIBCXX_FILESYSTEM_IS_WINDOWS
131 static bool is_dot_or_dotdot(const wchar_t* s) noexcept
132 { return !wcscmp(s, L".") || !wcscmp(s, L".."); }
133 #endif
135 posix::DIR* dirp;
138 } // namespace filesystem
140 // BEGIN/END macros must be defined before including this file.
141 _GLIBCXX_BEGIN_NAMESPACE_FILESYSTEM
143 inline file_type
144 get_file_type(const std::filesystem::__gnu_posix::dirent& d [[gnu::unused]])
146 #ifdef _GLIBCXX_HAVE_STRUCT_DIRENT_D_TYPE
147 switch (d.d_type)
149 case DT_BLK:
150 return file_type::block;
151 case DT_CHR:
152 return file_type::character;
153 case DT_DIR:
154 return file_type::directory;
155 case DT_FIFO:
156 return file_type::fifo;
157 case DT_LNK:
158 return file_type::symlink;
159 case DT_REG:
160 return file_type::regular;
161 case DT_SOCK:
162 return file_type::socket;
163 case DT_UNKNOWN:
164 return file_type::unknown;
165 default:
166 return file_type::none;
168 #else
169 return file_type::none;
170 #endif
172 _GLIBCXX_END_NAMESPACE_FILESYSTEM
174 _GLIBCXX_END_NAMESPACE_VERSION
175 } // namespace std
177 #endif // _GLIBCXX_DIR_COMMON_H