Changeset 1566
- Timestamp:
- 1/18/2010 6:41:13 AM (3 years ago)
- Location:
- branches/eraser6/CodeReview
- Files:
-
- 9 edited
-
Eraser.DefaultPlugins/FileSystems/Windows.cs (modified) (2 diffs)
-
Eraser.Manager/EntropySource.cs (modified) (2 diffs)
-
Eraser.Util/File.cs (modified) (3 diffs)
-
Eraser.Util/KernelApi.cs (modified) (1 diff)
-
Eraser.Util/Security.cs (modified) (2 diffs)
-
Eraser.Util/StreamInfo.cs (modified) (5 diffs)
-
Eraser.Util/VolumeInfo.cs (modified) (8 diffs)
-
Eraser.Util/Win32ErrorCodes.cs (modified) (1 diff)
-
Eraser/Program.cs (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
branches/eraser6/CodeReview/Eraser.DefaultPlugins/FileSystems/Windows.cs
r1551 r1566 93 93 catch (IOException e) 94 94 { 95 switch ( (Win32ErrorCodes)System.Runtime.InteropServices.Marshal.GetLastWin32Error())95 switch (System.Runtime.InteropServices.Marshal.GetLastWin32Error()) 96 96 { 97 case Win32ErrorCode s.AccessDenied:97 case Win32ErrorCode.AccessDenied: 98 98 throw new UnauthorizedAccessException(S._("The file {0} could not " + 99 99 "be erased because the file's permissions prevent access to the file.", 100 100 info.FullName)); 101 101 102 case Win32ErrorCode s.SharingViolation:102 case Win32ErrorCode.SharingViolation: 103 103 //If after FilenameEraseTries the file is still locked, some program is 104 104 //definitely using the file; throw an exception. … … 126 126 catch (IOException e) 127 127 { 128 switch ( (Win32ErrorCodes)System.Runtime.InteropServices.Marshal.GetLastWin32Error())128 switch (System.Runtime.InteropServices.Marshal.GetLastWin32Error()) 129 129 { 130 case Win32ErrorCode s.AccessDenied:130 case Win32ErrorCode.AccessDenied: 131 131 throw new UnauthorizedAccessException(S._("The file {0} could not " + 132 132 "be erased because the file's permissions prevent access to the file.", 133 133 info.FullName), e); 134 134 135 case Win32ErrorCode s.SharingViolation:135 case Win32ErrorCode.SharingViolation: 136 136 //If after FilenameEraseTries the file is still locked, some program is 137 137 //definitely using the file; throw an exception. -
branches/eraser6/CodeReview/Eraser.Manager/EntropySource.cs
r1553 r1566 344 344 catch (System.ComponentModel.Win32Exception e) 345 345 { 346 if (e.NativeErrorCode != (int)Win32ErrorCodes.AccessDenied)346 if (e.NativeErrorCode != Win32ErrorCode.AccessDenied) 347 347 throw; 348 348 } … … 437 437 (uint)infoBuffer.Length, out dataWritten); 438 438 439 if (sysInfo == (int)Win32ErrorCodes.Success && dataWritten > 0)439 if (sysInfo == Win32ErrorCode.Success && dataWritten > 0) 440 440 { 441 441 byte[] entropy = new byte[dataWritten]; -
branches/eraser6/CodeReview/Eraser.Util/File.cs
r1564 r1566 330 330 out modifiedTimeNative)) 331 331 { 332 throw KernelApi.GetExceptionForWin32Error(Marshal.GetLastWin32Error());332 throw Win32ErrorCode.GetExceptionForWin32Error(Marshal.GetLastWin32Error()); 333 333 } 334 334 … … 351 351 out accessedTimeNative, out modifiedTimeNative)) 352 352 { 353 throw KernelApi.GetExceptionForWin32Error(Marshal.GetLastWin32Error());353 throw Win32ErrorCode.GetExceptionForWin32Error(Marshal.GetLastWin32Error()); 354 354 } 355 355 … … 364 364 ref accessedTimeNative, ref modifiedTimeNative)) 365 365 { 366 throw KernelApi.GetExceptionForWin32Error(Marshal.GetLastWin32Error());366 throw Win32ErrorCode.GetExceptionForWin32Error(Marshal.GetLastWin32Error()); 367 367 } 368 368 } -
branches/eraser6/CodeReview/Eraser.Util/KernelApi.cs
r1562 r1566 86 86 { 87 87 return NativeMethods.FreeConsole(); 88 }89 90 /// <summary>91 /// Converts a Win32 Error code to a HRESULT.92 /// </summary>93 /// <param name="errorCode">The error code to convert.</param>94 /// <returns>A HRESULT value representing the error code.</returns>95 internal static int GetHRForWin32Error(int errorCode)96 {97 const uint FACILITY_WIN32 = 7;98 return errorCode <= 0 ? errorCode :99 (int)((((uint)errorCode) & 0x0000FFFF) | (FACILITY_WIN32 << 16) | 0x80000000);100 }101 102 /// <summary>103 /// Gets a Exception for the given Win32 error code.104 /// </summary>105 /// <param name="errorCode">The error code.</param>106 /// <returns>An exception object representing the error code.</returns>107 internal static Exception GetExceptionForWin32Error(int errorCode)108 {109 int HR = GetHRForWin32Error(errorCode);110 return Marshal.GetExceptionForHR(HR);111 88 } 112 89 -
branches/eraser6/CodeReview/Eraser.Util/Security.cs
r1554 r1566 64 64 NativeMethods.TOKEN_QUERY, out hToken); 65 65 if (!result || hToken.IsInvalid) 66 throw KernelApi.GetExceptionForWin32Error(Marshal.GetLastWin32Error());66 throw Win32ErrorCode.GetExceptionForWin32Error(Marshal.GetLastWin32Error()); 67 67 68 68 IntPtr pElevationType = Marshal.AllocHGlobal(Marshal.SizeOf( … … 79 79 //Check the return code 80 80 if (!result) 81 throw KernelApi.GetExceptionForWin32Error(Marshal.GetLastWin32Error());81 throw Win32ErrorCode.GetExceptionForWin32Error(Marshal.GetLastWin32Error()); 82 82 83 83 NativeMethods.TOKEN_ELEVATION_TYPE elevationType = -
branches/eraser6/CodeReview/Eraser.Util/StreamInfo.cs
r1551 r1566 133 133 if (!handle.IsInvalid) 134 134 return true; 135 else if (Marshal.GetLastWin32Error() == (int)Win32ErrorCodes.FileNotFound)135 else if (Marshal.GetLastWin32Error() == Win32ErrorCode.FileNotFound) 136 136 return false; 137 137 138 throw KernelApi.GetExceptionForWin32Error(Marshal.GetLastWin32Error());138 throw Win32ErrorCode.GetExceptionForWin32Error(Marshal.GetLastWin32Error()); 139 139 } 140 140 } … … 239 239 try 240 240 { 241 KernelApi.GetFileTime(handle, out creationTime, out lastAccess, out lastWrite);241 Util.File.GetFileTime(handle, out creationTime, out lastAccess, out lastWrite); 242 242 } 243 243 finally … … 268 268 try 269 269 { 270 KernelApi.SetFileTime(handle, creationTime, lastAccess, lastWrite);270 Util.File.SetFileTime(handle, creationTime, lastAccess, lastWrite); 271 271 } 272 272 finally … … 286 286 else 287 287 if (!NativeMethods.DeleteFile(FullName)) 288 throw KernelApi.GetExceptionForWin32Error(Marshal.GetLastWin32Error());288 throw Win32ErrorCode.GetExceptionForWin32Error(Marshal.GetLastWin32Error()); 289 289 } 290 290 … … 389 389 (uint)share, IntPtr.Zero, (uint)mode, (uint)options, IntPtr.Zero); 390 390 if (result.IsInvalid) 391 throw KernelApi.GetExceptionForWin32Error(Marshal.GetLastWin32Error());391 throw Win32ErrorCode.GetExceptionForWin32Error(Marshal.GetLastWin32Error()); 392 392 393 393 //Cache the handle if we have an exclusive handle - this is used for things like -
branches/eraser6/CodeReview/Eraser.Util/VolumeInfo.cs
r1551 r1566 52 52 pathNamesBuffer, (uint)pathNamesBuffer.Capacity, out returnLength)) 53 53 { 54 if (Marshal.GetLastWin32Error() == (int)Win32ErrorCodes.MoreData)54 if (Marshal.GetLastWin32Error() == Win32ErrorCode.MoreData) 55 55 pathNamesBuffer.EnsureCapacity((int)returnLength); 56 56 else … … 93 93 { 94 94 int lastError = Marshal.GetLastWin32Error(); 95 switch ( (Win32ErrorCodes)lastError)96 { 97 case Win32ErrorCode s.Success:98 case Win32ErrorCode s.NotReady:99 case Win32ErrorCode s.InvalidParameter: //when the volume given is not mounted.100 case Win32ErrorCode s.UnrecognizedVolume:95 switch (lastError) 96 { 97 case Win32ErrorCode.Success: 98 case Win32ErrorCode.NotReady: 99 case Win32ErrorCode.InvalidParameter: //when the volume given is not mounted. 100 case Win32ErrorCode.UnrecognizedVolume: 101 101 break; 102 102 … … 148 148 149 149 //Close the handle 150 if (Marshal.GetLastWin32Error() == (int)Win32ErrorCodes.NoMoreFiles)150 if (Marshal.GetLastWin32Error() == Win32ErrorCode.NoMoreFiles) 151 151 NativeMethods.FindVolumeClose(handle); 152 152 … … 177 177 else 178 178 { 179 switch ( (Win32ErrorCodes)Marshal.GetLastWin32Error())179 switch (Marshal.GetLastWin32Error()) 180 180 { 181 case Win32ErrorCode s.InvalidFunction:182 case Win32ErrorCode s.FileNotFound:183 case Win32ErrorCode s.PathNotFound:184 case Win32ErrorCode s.NotAReparsePoint:181 case Win32ErrorCode.InvalidFunction: 182 case Win32ErrorCode.FileNotFound: 183 case Win32ErrorCode.PathNotFound: 184 case Win32ErrorCode.NotAReparsePoint: 185 185 break; 186 186 default: … … 193 193 while (mountpointDir != null); 194 194 195 throw Marshal.GetExceptionForHR(KernelApi.GetHRForWin32Error( 196 (int)Win32ErrorCodes.NotAReparsePoint)); 195 throw Win32ErrorCode.GetExceptionForWin32Error(Win32ErrorCode.NotAReparsePoint); 197 196 } 198 197 … … 272 271 return totalNumberOfFreeBytes != freeBytesAvailable; 273 272 } 274 else if (Marshal.GetLastWin32Error() == (int)Win32ErrorCodes.NotReady)273 else if (Marshal.GetLastWin32Error() == Win32ErrorCode.NotReady) 275 274 { 276 275 //For the lack of more appropriate responses. … … 362 361 363 362 //Close the handle 364 if (Marshal.GetLastWin32Error() == (int)Win32ErrorCodes.NoMoreFiles)363 if (Marshal.GetLastWin32Error() == Win32ErrorCode.NoMoreFiles) 365 364 NativeMethods.FindVolumeMountPointClose(handle); 366 365 … … 475 474 (uint)share, IntPtr.Zero, (uint)FileMode.Open, (uint)options, IntPtr.Zero); 476 475 if (result.IsInvalid) 477 throw KernelApi.GetExceptionForWin32Error(Marshal.GetLastWin32Error());476 throw Win32ErrorCode.GetExceptionForWin32Error(Marshal.GetLastWin32Error()); 478 477 479 478 return result; -
branches/eraser6/CodeReview/Eraser.Util/Win32ErrorCodes.cs
r1551 r1566 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Linq;25 24 using System.Text; 25 using System.Runtime.InteropServices; 26 26 27 27 namespace Eraser.Util 28 28 { 29 public enum Win32ErrorCodes29 public static class Win32ErrorCode 30 30 { 31 NoError = Success, 32 Success = 0, 33 InvalidFunction = 1, 34 FileNotFound = 2, 35 PathNotFound = 3, 36 AccessDenied = 5, 37 NoMoreFiles = 18, 38 NotReady = 21, 39 SharingViolation = 32, 40 InvalidParameter = 87, 41 MoreData = 234, 42 UnrecognizedVolume = 1005, 43 NotAReparsePoint = 4390 31 /// <summary> 32 /// Converts a Win32 Error code to a HRESULT. 33 /// </summary> 34 /// <param name="errorCode">The error code to convert.</param> 35 /// <returns>A HRESULT value representing the error code.</returns> 36 internal static int GetHRForWin32Error(int errorCode) 37 { 38 const uint FACILITY_WIN32 = 7; 39 return errorCode <= 0 ? errorCode : 40 (int)((((uint)errorCode) & 0x0000FFFF) | (FACILITY_WIN32 << 16) | 0x80000000); 41 } 42 43 /// <summary> 44 /// Gets a Exception for the given Win32 error code. 45 /// </summary> 46 /// <param name="errorCode">The error code.</param> 47 /// <returns>An exception object representing the error code.</returns> 48 internal static Exception GetExceptionForWin32Error(int errorCode) 49 { 50 int HR = GetHRForWin32Error(errorCode); 51 return Marshal.GetExceptionForHR(HR); 52 } 53 54 public const int NoError = Success; 55 public const int Success = 0; 56 public const int InvalidFunction = 1; 57 public const int FileNotFound = 2; 58 public const int PathNotFound = 3; 59 public const int AccessDenied = 5; 60 public const int NoMoreFiles = 18; 61 public const int NotReady = 21; 62 public const int SharingViolation = 32; 63 public const int InvalidParameter = 87; 64 public const int MoreData = 234; 65 public const int UnrecognizedVolume = 1005; 66 public const int NotAReparsePoint = 4390; 44 67 } 45 68 } -
branches/eraser6/CodeReview/Eraser/Program.cs
r1551 r1566 157 157 catch (UnauthorizedAccessException) 158 158 { 159 return (int)Win32ErrorCodes.AccessDenied;159 return Win32ErrorCode.AccessDenied; 160 160 } 161 161 catch (Win32Exception e)
Note: See TracChangeset
for help on using the changeset viewer.
