private static CommonSecurityDescriptor CreateInternal(ResourceType resourceType, bool isContainer, string name, SafeHandle handle, AccessControlSections includeSections, bool createByName, ExceptionFromErrorCode exceptionFromErrorCode, object exceptionContext)
{
int error;
RawSecurityDescriptor rawSD;
if (createByName && name == null)
{
throw new ArgumentNullException(nameof(name));
}
else if (!createByName && handle == null)
{
throw new ArgumentNullException(nameof(handle));
}
error = Win32.GetSecurityInfo(resourceType, name, handle, includeSections, out rawSD);
if (error != Interop.Errors.ERROR_SUCCESS)
{
System.Exception exception = null;
if (exceptionFromErrorCode != null)
{
exception = exceptionFromErrorCode(error, name, handle, exceptionContext);
}
if (exception == null)
{
if (error == Interop.Errors.ERROR_ACCESS_DENIED)
{
exception = new UnauthorizedAccessException();
}
else if (error == Interop.Errors.ERROR_INVALID_OWNER)
{
exception = new InvalidOperationException(SR.AccessControl_InvalidOwner);
}
else if (error == Interop.Errors.ERROR_INVALID_PRIMARY_GROUP)
{
exception = new InvalidOperationException(SR.AccessControl_InvalidGroup);
}
else if (error == Interop.Errors.ERROR_INVALID_PARAMETER)
{
exception = new InvalidOperationException(SR.Format(SR.AccessControl_UnexpectedError, error));
}
else if (error == Interop.Errors.ERROR_INVALID_NAME)
{
exception = new ArgumentException(
SR.Argument_InvalidName,
nameof(name));
}
else if (error == Interop.Errors.ERROR_FILE_NOT_FOUND)
{
exception = (name == null ? new FileNotFoundException() : new FileNotFoundException(name));
}
else if (error == Interop.Errors.ERROR_NO_SECURITY_ON_OBJECT)
{
exception = new NotSupportedException(SR.AccessControl_NoAssociatedSecurity);
}
else
{
Debug.Assert(false, string.Format(CultureInfo.InvariantCulture, "Win32GetSecurityInfo() failed with unexpected error code {0}", error));
exception = new InvalidOperationException(SR.Format(SR.AccessControl_UnexpectedError, error));
}
}
throw exception;
}
return new CommonSecurityDescriptor(isContainer, false /* isDS */, rawSD, true);
}