| 160 | } |
| 161 | |
| 162 | void IterateDirRecursive(const std::string &Dir, |
| 163 | void (*DirPreCallback)(const std::string &Dir), |
| 164 | void (*DirPostCallback)(const std::string &Dir), |
| 165 | void (*FileCallback)(const std::string &Dir)) { |
| 166 | // TODO(metzman): Implement ListFilesInDirRecursive via this function. |
| 167 | DirPreCallback(Dir); |
| 168 | |
| 169 | DWORD DirAttrs = GetFileAttributesA(Dir.c_str()); |
| 170 | if (!IsDir(DirAttrs)) return; |
| 171 | |
| 172 | std::string TargetDir(Dir); |
| 173 | assert(!TargetDir.empty()); |
| 174 | if (TargetDir.back() != '\\') TargetDir.push_back('\\'); |
| 175 | TargetDir.push_back('*'); |
| 176 | |
| 177 | WIN32_FIND_DATAA FindInfo; |
| 178 | // Find the directory's first file. |
| 179 | HANDLE FindHandle = FindFirstFileA(TargetDir.c_str(), &FindInfo); |
| 180 | if (FindHandle == INVALID_HANDLE_VALUE) { |
| 181 | DWORD LastError = GetLastError(); |
| 182 | if (LastError != ERROR_FILE_NOT_FOUND) { |
| 183 | // If the directory isn't empty, then something abnormal is going on. |
| 184 | Printf("FindFirstFileA failed for %s (Error code: %lu).\n", Dir.c_str(), |
| 185 | LastError); |
| 186 | } |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | do { |
| 191 | std::string Path = DirPlusFile(Dir, FindInfo.cFileName); |
| 192 | DWORD PathAttrs = FindInfo.dwFileAttributes; |
| 193 | if (IsDir(PathAttrs)) { |
| 194 | // Is Path the current directory (".") or the parent ("..")? |
| 195 | if (strcmp(FindInfo.cFileName, ".") == 0 || |
| 196 | strcmp(FindInfo.cFileName, "..") == 0) |
| 197 | continue; |
| 198 | IterateDirRecursive(Path, DirPreCallback, DirPostCallback, FileCallback); |
| 199 | } else if (PathAttrs != INVALID_FILE_ATTRIBUTES) { |
| 200 | FileCallback(Path); |
| 201 | } |
| 202 | } while (FindNextFileA(FindHandle, &FindInfo)); |
| 203 | |
| 204 | DWORD LastError = GetLastError(); |
| 205 | if (LastError != ERROR_NO_MORE_FILES) |
| 206 | Printf("FindNextFileA failed for %s (Error code: %lu).\n", Dir.c_str(), |
| 207 | LastError); |
| 208 | |
| 209 | FindClose(FindHandle); |
| 210 | DirPostCallback(Dir); |
| 211 | } |
| 212 | |
| 213 | char GetSeparator() { |
| 214 | return '\\'; |