@@ -318,7 +318,7 @@ public override bool DirectoryExists(string fullPath)
318
318
return DirectoryExists ( fullPath , out errno ) ;
319
319
}
320
320
321
- private bool DirectoryExists ( string fullPath , out int errno )
321
+ private static bool DirectoryExists ( string fullPath , out int errno )
322
322
{
323
323
return FileExists ( fullPath , Interop . libcoreclr . FileTypes . S_IFDIR , out errno ) ;
324
324
}
@@ -329,7 +329,7 @@ public override bool FileExists(string fullPath)
329
329
return FileExists ( fullPath , Interop . libcoreclr . FileTypes . S_IFREG , out errno ) ;
330
330
}
331
331
332
- private bool FileExists ( string fullPath , int fileType , out int errno )
332
+ private static bool FileExists ( string fullPath , int fileType , out int errno )
333
333
{
334
334
Interop . libcoreclr . fileinfo fileinfo ;
335
335
while ( true )
@@ -475,48 +475,53 @@ private IEnumerator<T> Enumerate(Interop.libc.SafeDirHandle dirHandle)
475
475
string name = Interop . libc . GetDirEntName ( curEntry ) ;
476
476
477
477
// Get from the dir entry whether the entry is a file or directory.
478
- // If we're not sure from the dir entry itself, stat to the entry.
479
- bool isDir = false , isFile = false ;
478
+ // We classify everything as a file unless we know it to be a directory,
479
+ // e.g. a FIFO will be classified as a file.
480
+ bool isDir ;
480
481
switch ( Interop . libc . GetDirEntType ( curEntry ) )
481
482
{
482
483
case Interop . libc . DType . DT_DIR :
484
+ // We know it's a directory.
483
485
isDir = true ;
484
486
break ;
485
- case Interop . libc . DType . DT_REG :
486
- isFile = true ;
487
- break ;
488
487
case Interop . libc . DType . DT_LNK :
489
488
case Interop . libc . DType . DT_UNKNOWN :
490
- string fullPath = Path . Combine ( dirPath . FullPath , name ) ;
491
- Interop . libcoreclr . fileinfo fileinfo ;
492
- while ( Interop . CheckIo ( Interop . libcoreclr . GetFileInformationFromPath ( fullPath , out fileinfo ) , fullPath ) ) ;
493
- isDir = ( fileinfo . mode & Interop . libcoreclr . FileTypes . S_IFMT ) == Interop . libcoreclr . FileTypes . S_IFDIR ;
494
- isFile = ( fileinfo . mode & Interop . libcoreclr . FileTypes . S_IFMT ) == Interop . libcoreclr . FileTypes . S_IFREG ;
489
+ // It's a symlink or unknown: stat to it to see if we can resolve it to a directory.
490
+ // If we can't (e.g.symlink to a file, broken symlink, etc.), we'll just treat it as a file.
491
+ int errnoIgnored ;
492
+ isDir = DirectoryExists ( Path . Combine ( dirPath . FullPath , name ) , out errnoIgnored ) ;
493
+ break ;
494
+ default :
495
+ // Otherwise, treat it as a file. This includes regular files,
496
+ // FIFOs, etc.
497
+ isDir = false ;
495
498
break ;
496
499
}
497
- bool matchesSearchPattern =
498
- ( isFile || isDir ) &&
499
- Interop . libc . fnmatch ( _searchPattern , name , Interop . libc . FnmatchFlags . None ) == 0 ;
500
500
501
501
// Yield the result if the user has asked for it. In the case of directories,
502
502
// always explore it by pushing it onto the stack, regardless of whether
503
503
// we're returning directories.
504
- if ( isDir && ! ShouldIgnoreDirectory ( name ) )
504
+ if ( isDir )
505
505
{
506
- if ( _includeDirectories && matchesSearchPattern )
507
- {
508
- yield return _translateResult ( Path . Combine ( dirPath . UserPath , name ) , /*isDirectory*/ true ) ;
509
- }
510
- if ( _searchOption == SearchOption . AllDirectories )
506
+ if ( ! ShouldIgnoreDirectory ( name ) )
511
507
{
512
- if ( toExplore == null )
508
+ if ( _includeDirectories &&
509
+ Interop . libc . fnmatch ( _searchPattern , name , Interop . libc . FnmatchFlags . None ) == 0 )
510
+ {
511
+ yield return _translateResult ( Path . Combine ( dirPath . UserPath , name ) , /*isDirectory*/ true ) ;
512
+ }
513
+ if ( _searchOption == SearchOption . AllDirectories )
513
514
{
514
- toExplore = new Stack < PathPair > ( ) ;
515
+ if ( toExplore == null )
516
+ {
517
+ toExplore = new Stack < PathPair > ( ) ;
518
+ }
519
+ toExplore . Push ( new PathPair ( Path . Combine ( dirPath . UserPath , name ) , Path . Combine ( dirPath . FullPath , name ) ) ) ;
515
520
}
516
- toExplore . Push ( new PathPair ( Path . Combine ( dirPath . UserPath , name ) , Path . Combine ( dirPath . FullPath , name ) ) ) ;
517
521
}
518
522
}
519
- else if ( isFile && _includeFiles && matchesSearchPattern )
523
+ else if ( _includeFiles &&
524
+ Interop . libc . fnmatch ( _searchPattern , name , Interop . libc . FnmatchFlags . None ) == 0 )
520
525
{
521
526
yield return _translateResult ( Path . Combine ( dirPath . UserPath , name ) , /*isDirectory*/ false ) ;
522
527
}
0 commit comments