2 * Copyright 2016 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 + 2];
40 if (mbstowcs_s(&len, patternBuf, MAX_PATH, pattern.c_str(), MAX_PATH - 2)) {
44 if (len && patternBuf[len - 1] != '/' && patternBuf[len - 1] != '\\') {
45 patternBuf[len++] = '\\';
47 patternBuf[len++] = '*';
50 WIN32_FIND_DATAW fdata;
51 HANDLE h = FindFirstFileW(patternBuf, &fdata);
52 if (h == INVALID_HANDLE_VALUE) {
57 dir.d_name = currentName;
58 if (wcstombs(currentName, fdata.cFileName, MAX_PATH * 3) == (size_t)-1) {
62 setEntryType(fdata.dwFileAttributes);
68 WIN32_FIND_DATAW fdata;
69 if (!FindNextFileW(searchHandle, &fdata)) {
73 if (wcstombs(currentName, fdata.cFileName, MAX_PATH * 3) == (size_t)-1) {
77 setEntryType(fdata.dwFileAttributes);
85 void setEntryType(DWORD attr) {
86 if (attr & FILE_ATTRIBUTE_DIRECTORY) {
95 int closedir(DIR* dir) {
96 auto ret = dir->close();
101 DIR* opendir(const char* name) {
102 auto dir = new DIR();
111 dirent* readdir(DIR* dir) {
112 return dir->nextDir();
115 int readdir_r(DIR* dir, dirent* buf, dirent** ent) {
116 if (!dir || !buf || !ent) {
119 *ent = dir->nextDir();
120 // Our normal readdir implementation is actually
121 // already reentrant, but we need to do this copy
122 // in case the caller expects buf to have the value.
129 void rewinddir(DIR* dir) {