@@ -33,9 +33,9 @@ await Parallel.ForEachAsync(
3333 {
3434 ComputeSizeRecursively ( path , token ) ;
3535 } ,
36- token ,
37- TaskCreationOptions . LongRunning ,
38- TaskScheduler . Default ) ) ;
36+ token ,
37+ TaskCreationOptions . LongRunning ,
38+ TaskScheduler . Default ) ) ;
3939
4040 unsafe void ComputeSizeRecursively ( string path , CancellationToken token )
4141 {
@@ -64,47 +64,39 @@ unsafe void ComputeSizeRecursively(string path, CancellationToken token)
6464
6565 if ( ! hFile . IsNull )
6666 {
67- try
67+ do
6868 {
69- do
69+ FILE_FLAGS_AND_ATTRIBUTES attributes = ( FILE_FLAGS_AND_ATTRIBUTES ) findData . dwFileAttributes ;
70+
71+ if ( attributes . HasFlag ( FILE_FLAGS_AND_ATTRIBUTES . FILE_ATTRIBUTE_REPARSE_POINT ) )
72+ // Skip symbolic links and junctions
73+ continue ;
74+
75+ var itemPath = Path . Combine ( directory , findData . cFileName . ToString ( ) ) ;
76+
77+ // Skip current and parent directory entries
78+ var fileName = findData . cFileName . ToString ( ) ;
79+ if ( fileName . Equals ( "." , StringComparison . OrdinalIgnoreCase ) ||
80+ fileName . Equals ( ".." , StringComparison . OrdinalIgnoreCase ) )
7081 {
71- FILE_FLAGS_AND_ATTRIBUTES attributes = ( FILE_FLAGS_AND_ATTRIBUTES ) findData . dwFileAttributes ;
72-
73- if ( attributes . HasFlag ( FILE_FLAGS_AND_ATTRIBUTES . FILE_ATTRIBUTE_REPARSE_POINT ) )
74- // Skip symbolic links and junctions
75- continue ;
76-
77- var itemPath = Path . Combine ( directory , findData . cFileName . ToString ( ) ) ;
78-
79- // Skip current and parent directory entries
80- var fileName = findData . cFileName . ToString ( ) ;
81- if ( fileName . Equals ( "." , StringComparison . OrdinalIgnoreCase ) ||
82- fileName . Equals ( ".." , StringComparison . OrdinalIgnoreCase ) )
83- {
84- continue ;
85- }
86-
87- if ( attributes . HasFlag ( FILE_FLAGS_AND_ATTRIBUTES . FILE_ATTRIBUTE_DIRECTORY ) )
88- {
89- queue . Enqueue ( itemPath ) ;
90- }
91- else
92- {
93- ComputeFileSize ( itemPath ) ;
94- }
95-
96- if ( token . IsCancellationRequested )
97- break ;
82+ continue ;
9883 }
99- while ( PInvoke . FindNextFile ( hFile , & findData ) ) ;
100- }
101- finally
102- {
103- if ( ! PInvoke . FindClose ( hFile ) )
84+
85+ if ( attributes . HasFlag ( FILE_FLAGS_AND_ATTRIBUTES . FILE_ATTRIBUTE_DIRECTORY ) )
86+ {
87+ queue . Enqueue ( itemPath ) ;
88+ }
89+ else
10490 {
105- var error = Marshal . GetLastWin32Error ( ) ;
91+ ComputeFileSize ( itemPath ) ;
10692 }
93+
94+ if ( token . IsCancellationRequested )
95+ break ;
10796 }
97+ while ( PInvoke . FindNextFile ( hFile , & findData ) ) ;
98+
99+ PInvoke . FindClose ( hFile ) ;
108100 }
109101 }
110102 }
0 commit comments