@@ -929,15 +929,14 @@ extension FileManager {
929
929
var _options : FileManager . DirectoryEnumerationOptions
930
930
var _errorHandler : ( ( URL , Error ) -> Bool ) ?
931
931
var _stack : [ URL ]
932
- var _lastReturned : URL
933
- var _rootDepth : Int
932
+ var _lastReturned : URL ?
933
+ var _root : URL
934
934
935
935
init ( url: URL , options: FileManager . DirectoryEnumerationOptions , errorHandler: ( /* @escaping */ ( URL , Error ) -> Bool ) ? ) {
936
936
_options = options
937
937
_errorHandler = errorHandler
938
938
_stack = [ ]
939
- _rootDepth = url. pathComponents. count
940
- _lastReturned = url
939
+ _root = url
941
940
}
942
941
943
942
override func nextObject( ) -> Any ? {
@@ -955,17 +954,22 @@ extension FileManager {
955
954
return nil
956
955
}
957
956
958
- // If we most recently returned a directory, decend into it
959
- guard let attrs = try ? FileManager . default. windowsFileAttributes ( atPath: _lastReturned. path) else {
960
- guard let handler = _errorHandler,
961
- handler ( _lastReturned, _NSErrorWithWindowsError ( GetLastError ( ) , reading: true , paths: [ _lastReturned. path] ) )
962
- else { return nil }
963
- return firstValidItem ( )
957
+ if _lastReturned == nil {
958
+ guard let attrs = try ? FileManager . default. windowsFileAttributes ( atPath: _root. path) else {
959
+ guard let handler = _errorHandler else { return nil }
960
+ if !handler( _root, _NSErrorWithWindowsError ( GetLastError ( ) , reading: true , paths: [ _root. path] ) ) {
961
+ return nil
962
+ }
963
+ return firstValidItem ( )
964
+ }
965
+
966
+ let isDirectory = attrs. dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY == FILE_ATTRIBUTE_DIRECTORY && attrs. dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT != FILE_ATTRIBUTE_REPARSE_POINT
967
+ _lastReturned = URL ( fileURLWithPath: _root. path, isDirectory: isDirectory)
964
968
}
965
969
966
- let isDir = attrs . dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY == FILE_ATTRIBUTE_DIRECTORY &&
967
- attrs . dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT == 0
968
- if isDir && ( level == 0 || !_options. contains ( . skipsSubdirectoryDescendants) ) {
970
+ guard let _lastReturned else { return firstValidItem ( ) }
971
+
972
+ if _lastReturned . hasDirectoryPath && ( level == 0 || !_options. contains ( . skipsSubdirectoryDescendants) ) {
969
973
var ffd = WIN32_FIND_DATAW ( )
970
974
let capacity = MemoryLayout . size ( ofValue: ffd. cFileName)
971
975
@@ -991,11 +995,13 @@ extension FileManager {
991
995
_stack. append ( _lastReturned. appendingPathComponent ( file, isDirectory: isDirectory) )
992
996
} while FindNextFileW ( handle, & ffd)
993
997
}
998
+
994
999
return firstValidItem ( )
995
1000
}
996
1001
997
1002
override var level : Int {
998
- return _lastReturned. pathComponents. count - _rootDepth
1003
+ guard let _lastReturned else { return 0 }
1004
+ return _lastReturned. pathComponents. count - _root. pathComponents. count
999
1005
}
1000
1006
1001
1007
override func skipDescendants( ) {
0 commit comments