peteraritchie / LongPath

drop-in library to support long paths in .NET
GNU Lesser General Public License v3.0
112 stars 43 forks source link

System.UnauthorizedAccessException: Empty path #52

Closed thomaslevesque closed 7 years ago

thomaslevesque commented 7 years ago

While investigating an issue I found this in my log file:

2016-10-26 09:36:41,141 - Scan - ERROR - MyApp.Business.SyncService - Error analyzing local changes
System.UnauthorizedAccessException: Empty path
   à Pri.LongPath.Common.ThrowIOError(Int32 errorCode, String maybeFullPath)
   à Pri.LongPath.FileSystemInfo.Refresh()
   à Pri.LongPath.DirectoryInfo.get_Exists()
   ...

I first tried to understand how I could possibly end-up with a DirectoryInfo with an empty path, since the path is validated at construction time, but looking at the code, I think the message is incorrect and misleading. The message "Empty path" comes from here:

            case NativeMethods.ERROR_ACCESS_DENIED:
                if (str.Length == 0)
                    throw new UnauthorizedAccessException("Empty path");
                else
                    throw new UnauthorizedAccessException(String.Format("Access denied accessing {0}", str));

because the maybeFullPath parameter is empty.

Common.ThrowIOError is called from here:

        catch (Exception)
        {
            if (state != State.Error)
                Common.ThrowIOError(Marshal.GetLastWin32Error(), string.Empty);
                    }

Is there any reason not to pass FullPath instead of string.Empty?

peteraritchie commented 7 years ago

I agree, this doesn't seem right. I can't think of any reason why that should be empty (otherwise there would be a unit test for it :) ), probably just a bug. Thanks for detailing it and submitting a PR.