dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.47k stars 4.76k forks source link

No way to handle UnauthorizedAccessException #36412

Open TylerLeonhardt opened 5 years ago

TylerLeonhardt commented 5 years ago

Describe the bug

I'm receiving a System.UnauthorizedAccessException while using the Microsoft.Extensions.FileSystemGlobbing. I need some way to catch this exception and continue the File search.

To Reproduce

Steps to reproduce the behavior:

  1. Using version '2.2.0' of package 'Microsoft.Extensions.FileSystemGlobbing'
  2. Run this code:
using System;
using System.IO;
using System.Linq;
using Microsoft.Extensions.FileSystemGlobbing;
using Microsoft.Extensions.FileSystemGlobbing.Abstractions;

namespace testProj
{
    class Program
    {
        public static void Main(string[] args)
        {
            Matcher matcher = new Matcher().AddInclude(@"**/*.*");

            PatternMatchingResult result = matcher.Execute(new DirectoryInfoWrapper(new DirectoryInfo("/")));

            foreach (var item in result.Files)
            {
                Console.WriteLine(item);
                System.Threading.Thread.Sleep(100);
            }
        }
    }
}
  1. See error:
Exception has occurred: CLR/System.UnauthorizedAccessException
An unhandled exception of type 'System.UnauthorizedAccessException' occurred in System.IO.FileSystem.dll: 'Access to the path '/usr/sbin/authserver' is denied.'
 Inner exceptions found, see $exception in variables window for more details.
 Innermost exception     System.IO.IOException : Permission denied

Expected behavior

I would expect some way to catch these errors and continue searching.

Additional context

Include the output of dotnet --info:

dotnet --info
.NET Core SDK (reflecting any global.json):
 Version:   2.1.403
 Commit:    04e15494b6

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  10.14
 OS Platform: Darwin
 RID:         osx-x64
 Base Path:   /usr/local/share/dotnet/sdk/2.1.403/

Host (useful for support):
  Version: 2.1.5
  Commit:  290303f510

.NET Core SDKs installed:
  2.1.403 [/usr/local/share/dotnet/sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.5 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.5 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.5 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download
jozkee commented 4 years ago

I think that if you do a recursive search, you should not be blocked by the files that you don't have access to, they should be only ignored.

The problem with the current code is here, EnumerateFileSystemInfos(string, SearchOption) does not support ignoring inaccessible files: https://github.com/dotnet/runtime/blob/3cf8d43713c6ee188486459d988db0af0d7596c6/src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Abstractions/DirectoryInfoWrapper.cs#L38

The scenario could be easily enable if we could use the EnumerateFileSystemInfos overload that takes EnumerableOptions and then you pass options with IgnoreInaccessible set to true. The concern is that such API is not available for NS2.0 which is the version that FileSystemGlobbing targets.

cc @carlossanlop @maryamariyan

TylerLeonhardt commented 4 years ago

Yeah my product is a NS2.0 lib as well so I would appreciate the support there :)

dhhoang commented 3 years ago

+1 on this issue