Skip to content

Commit 803eb39

Browse files
committed
Foundation: avoid unnecessary disk IO on windows file enumeration
Prefer to use the `hasDirectoryPath` ivar on the URL which should be properly populated when populating the stack. The one special case is when we first initialise the enumerator, where we can assume that we are given a directory path.
1 parent b70418c commit 803eb39

File tree

1 file changed

+3
-12
lines changed

1 file changed

+3
-12
lines changed

Sources/Foundation/FileManager+Win32.swift

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -930,14 +930,14 @@ extension FileManager {
930930
var _errorHandler : ((URL, Error) -> Bool)?
931931
var _stack: [URL]
932932
var _lastReturned: URL
933-
var _rootDepth : Int
933+
var _rootDepth: Int
934934

935935
init(url: URL, options: FileManager.DirectoryEnumerationOptions, errorHandler: (/* @escaping */ (URL, Error) -> Bool)?) {
936936
_options = options
937937
_errorHandler = errorHandler
938938
_stack = []
939939
_rootDepth = url.pathComponents.count
940-
_lastReturned = url
940+
_lastReturned = URL(fileURLWithPath: url.path, isDirectory: true)
941941
}
942942

943943
override func nextObject() -> Any? {
@@ -959,16 +959,7 @@ extension FileManager {
959959
}
960960

961961
// If we most recently returned a directory, decend into it
962-
guard let attrs = try? FileManager.default.windowsFileAttributes(atPath: _lastReturned.path) else {
963-
guard let handler = _errorHandler,
964-
handler(_lastReturned, _NSErrorWithWindowsError(GetLastError(), reading: true, paths: [_lastReturned.path]))
965-
else { return nil }
966-
return firstValidItem()
967-
}
968-
969-
let isDir = attrs.dwFileAttributes & DWORD(FILE_ATTRIBUTE_DIRECTORY) == DWORD(FILE_ATTRIBUTE_DIRECTORY) &&
970-
attrs.dwFileAttributes & DWORD(FILE_ATTRIBUTE_REPARSE_POINT) == 0
971-
if isDir && (level == 0 || !_options.contains(.skipsSubdirectoryDescendants)) {
962+
if _lastReturned.hasDirectoryPath && (level == 0 || !_options.contains(.skipsSubdirectoryDescendants)) {
972963
var ffd = WIN32_FIND_DATAW()
973964
let capacity = MemoryLayout.size(ofValue: ffd.cFileName)
974965

0 commit comments

Comments
 (0)