Skip to content

Commit e44e7ce

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 11b0dbc commit e44e7ce

File tree

1 file changed

+3
-13
lines changed

1 file changed

+3
-13
lines changed

Sources/Foundation/FileManager+Win32.swift

Lines changed: 3 additions & 13 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? {
@@ -958,17 +958,7 @@ extension FileManager {
958958
return nil
959959
}
960960

961-
// 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 & FILE_ATTRIBUTE_DIRECTORY == FILE_ATTRIBUTE_DIRECTORY &&
970-
attrs.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT == 0
971-
if isDir && (level == 0 || !_options.contains(.skipsSubdirectoryDescendants)) {
961+
if _lastReturned.hasDirectoryPath && (level == 0 || !_options.contains(.skipsSubdirectoryDescendants)) {
972962
var ffd = WIN32_FIND_DATAW()
973963
let capacity = MemoryLayout.size(ofValue: ffd.cFileName)
974964

0 commit comments

Comments
 (0)