Add native errno to ErrorCategory mapper

This commit is contained in:
Andrew Schwartzmeyer
2016-10-14 15:30:19 -07:00
parent e1aeeaa9c2
commit 196a37b118
5 changed files with 86 additions and 0 deletions
@@ -461,6 +461,12 @@ namespace System.Management.Automation
// to a PAL value and calls strerror_r underneath to generate the message.
internal static class Unix
{
// This is a helper that attempts to map errno into a PowerShell ErrorCategory
internal static ErrorCategory GetErrorCategory(int errno)
{
return (ErrorCategory)Unix.NativeMethods.GetErrorCategory(errno);
}
private static string s_userName;
public static string UserName
{
@@ -574,6 +580,9 @@ namespace System.Management.Automation
// C bools are 1 byte and so must be marshaled as I1
[DllImport(psLib, CharSet = CharSet.Ansi)]
internal static extern int GetErrorCategory(int errno);
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
[return: MarshalAs(UnmanagedType.LPStr)]
internal static extern string GetUserName();
@@ -2236,7 +2236,11 @@ namespace Microsoft.PowerShell.Commands
Win32Exception w32Exception = new Win32Exception((int)errorCode);
#if UNIX
if (Platform.Unix.GetErrorCategory(errorCode) == ErrorCategory.PermissionDenied)
#else
if (errorCode == 1314) //ERROR_PRIVILEGE_NOT_HELD
#endif
{
string message = FileSystemProviderStrings.ElevationRequired;
WriteError(new ErrorRecord(new UnauthorizedAccessException(message, w32Exception), "NewItemSymbolicLinkElevationRequired", ErrorCategory.PermissionDenied, value.ToString()));
+1
View File
@@ -9,6 +9,7 @@ add_library(psl-native SHARED
getcomputername.cpp
getlinkcount.cpp
getfullyqualifiedname.cpp
geterrorcategory.cpp
isfile.cpp
isdirectory.cpp
issymlink.cpp
@@ -0,0 +1,63 @@
#include "geterrorcategory.h"
#include <assert.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
// Copy of PowerShell ErrorCategory enum from ErrorPackage.cs
enum ErrorCategory {
NotSpecified = 0,
OpenError = 1,
CloseError = 2,
DeviceError = 3,
DeadlockDetected = 4,
InvalidArgument = 5,
InvalidData = 6,
InvalidOperation = 7,
InvalidResult = 8,
InvalidType = 9,
MetadataError = 10,
NotImplemented = 11,
NotInstalled = 12,
ObjectNotFound = 13,
OperationStopped = 14,
OperationTimeout = 15,
SyntaxError = 16,
ParserError = 17,
PermissionDenied = 18,
ResourceBusy = 19,
ResourceExists = 20,
ResourceUnavailable = 21,
ReadError = 22,
WriteError = 23,
FromStdErr = 24,
SecurityError = 25,
ProtocolError = 26,
ConnectionError = 27,
AuthenticationError = 28,
LimitsExceeded = 29,
QuotaExceeded = 30,
NotEnabled = 31,
};
//! @brief Maps Linux errno to PowerShell ErrorCategory
int32_t GetErrorCategory(int32_t errnum)
{
switch (errnum)
{
case EINVAL:
return InvalidArgument;
case ENOENT:
case ESRCH:
return ObjectNotFound;
case EINTR:
return OperationStopped;
case EACCES:
case EPERM:
return PermissionDenied;
default:
return NotSpecified;
}
}
+9
View File
@@ -0,0 +1,9 @@
#pragma once
#include "pal.h"
PAL_BEGIN_EXTERNC
int32_t GetErrorCategory(int32_t);
PAL_END_EXTERNC