2 * Copyright 2017 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <folly/portability/Dirent.h>
23 #include <folly/portability/Windows.h>
27 HANDLE searchHandle{INVALID_HANDLE_VALUE};
29 char currentName[MAX_PATH * 3];
33 return FindClose(searchHandle) ? 0 : -1;
37 wchar_t patternBuf[MAX_PATH + 3];
40 if (pattern.empty()) {
44 if (mbstowcs_s(&len, patternBuf, MAX_PATH, pattern.c_str(), MAX_PATH - 2)) {
48 // `len` includes the trailing NUL
52 if (len && patternBuf[len - 1] != '/' && patternBuf[len - 1] != '\\') {
53 patternBuf[len++] = '\\';
55 patternBuf[len++] = '*';
58 WIN32_FIND_DATAW fdata;
59 HANDLE h = FindFirstFileW(patternBuf, &fdata);
60 if (h == INVALID_HANDLE_VALUE) {
65 dir.d_name = currentName;
66 if (wcstombs(currentName, fdata.cFileName, MAX_PATH * 3) == (size_t)-1) {
70 setEntryType(fdata.dwFileAttributes);
76 WIN32_FIND_DATAW fdata;
77 if (!FindNextFileW(searchHandle, &fdata)) {
81 if (wcstombs(currentName, fdata.cFileName, MAX_PATH * 3) == (size_t)-1) {
85 setEntryType(fdata.dwFileAttributes);
93 void setEntryType(DWORD attr) {
94 if (attr & FILE_ATTRIBUTE_DIRECTORY) {
103 int closedir(DIR* dir) {
104 auto ret = dir->close();
109 DIR* opendir(const char* name) {
110 auto dir = new DIR();
119 dirent* readdir(DIR* dir) {
120 return dir->nextDir();
123 int readdir_r(DIR* dir, dirent* buf, dirent** ent) {
124 if (!dir || !buf || !ent) {
127 *ent = dir->nextDir();
128 // Our normal readdir implementation is actually
129 // already reentrant, but we need to do this copy
130 // in case the caller expects buf to have the value.
137 void rewinddir(DIR* dir) {