Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / mscorlib / system / io / __error.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 /*============================================================
7 **
8 ** Class:  __Error
9 ** 
10 ** <OWNER>Microsoft</OWNER>
11 **
12 **
13 ** Purpose: Centralized error methods for the IO package.  
14 ** Mostly useful for translating Win32 HRESULTs into meaningful
15 ** error strings & exceptions.
16 **
17 **
18 ===========================================================*/
19
20 using System;
21 using System.Runtime.InteropServices;
22 using Win32Native = Microsoft.Win32.Win32Native;
23 using System.Text;
24 using System.Globalization;
25 using System.Security;
26 using System.Security.Permissions;
27 using System.Diagnostics.Contracts;
28
29 namespace System.IO {
30     [Pure]
31     internal static class __Error
32     {
33         internal static void EndOfFile() {
34             throw new EndOfStreamException(Environment.GetResourceString("IO.EOF_ReadBeyondEOF"));
35         }
36
37         internal static void FileNotOpen() {
38             throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_FileClosed"));
39         }
40         
41         internal static void StreamIsClosed() {
42             throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_StreamClosed"));
43         }
44     
45         internal static void MemoryStreamNotExpandable() {
46             throw new NotSupportedException(Environment.GetResourceString("NotSupported_MemStreamNotExpandable"));
47         }
48     
49         internal static void ReaderClosed() {
50             throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_ReaderClosed"));
51         }
52
53         internal static void ReadNotSupported() {
54             throw new NotSupportedException(Environment.GetResourceString("NotSupported_UnreadableStream"));
55         }
56     
57         internal static void SeekNotSupported() {
58             throw new NotSupportedException(Environment.GetResourceString("NotSupported_UnseekableStream"));
59         }
60
61         internal static void WrongAsyncResult() {
62             throw new ArgumentException(Environment.GetResourceString("Arg_WrongAsyncResult"));
63         }
64
65         internal static void EndReadCalledTwice() {
66             // Should ideally be InvalidOperationExc but we can't maitain parity with Stream and FileStream without some work
67             throw new ArgumentException(Environment.GetResourceString("InvalidOperation_EndReadCalledMultiple"));
68         }
69
70         internal static void EndWriteCalledTwice() {
71             // Should ideally be InvalidOperationExc but we can't maintain parity with Stream and FileStream without some work
72             throw new ArgumentException(Environment.GetResourceString("InvalidOperation_EndWriteCalledMultiple"));
73         }
74
75         // Given a possible fully qualified path, ensure that we have path
76         // discovery permission to that path.  If we do not, return just the 
77         // file name.  If we know it is a directory, then don't return the 
78         // directory name.
79         [System.Security.SecurityCritical]  // auto-generated
80         internal static String GetDisplayablePath(String path, bool isInvalidPath)
81         {
82             if (String.IsNullOrEmpty(path))
83                 return String.Empty;
84
85             if (path.Length < 2)
86                 return path;
87
88             // Return the path as is if we're relative (not fully qualified) and not a bad path
89             if (PathInternal.IsPartiallyQualified(path) && !isInvalidPath)
90                 return path;
91
92             bool safeToReturn = false;
93             try {
94                 if (!isInvalidPath) {
95 #if !FEATURE_CORECLR && MONO_FEATURE_CAS
96                     FileIOPermission.QuickDemand(FileIOPermissionAccess.PathDiscovery, path, false, false);
97 #endif
98                     safeToReturn = true;
99                 }
100             }
101             catch (SecurityException) {
102             }
103             catch (ArgumentException) {
104                 // ? and * characters cause ArgumentException to be thrown from HasIllegalCharacters
105                 // inside FileIOPermission.AddPathList
106             }
107             catch (NotSupportedException) {
108                 // paths like "!Bogus\\dir:with/junk_.in it" can cause NotSupportedException to be thrown
109                 // from Security.Util.StringExpressionSet.CanonicalizePath when ':' is found in the path
110                 // beyond string index position 1.  
111             }
112             
113             if (!safeToReturn) {
114                 if (Path.IsDirectorySeparator(path[path.Length - 1]))
115                     path = Environment.GetResourceString("IO.IO_NoPermissionToDirectoryName");
116                 else
117                     path = Path.GetFileName(path);
118             }
119
120             return path;
121         }
122 #if !MONO
123         [System.Security.SecuritySafeCritical]  // auto-generated
124         internal static void WinIOError() {
125             int errorCode = Marshal.GetLastWin32Error();
126             WinIOError(errorCode, String.Empty);
127         }
128 #endif
129         // After calling GetLastWin32Error(), it clears the last error field,
130         // so you must save the HResult and pass it to this method.  This method
131         // will determine the appropriate exception to throw dependent on your 
132         // error, and depending on the error, insert a string into the message 
133         // gotten from the ResourceManager.
134         [System.Security.SecurityCritical]  // auto-generated
135         internal static void WinIOError(int errorCode, String maybeFullPath) {
136             // This doesn't have to be perfect, but is a perf optimization.
137             bool isInvalidPath = errorCode == Win32Native.ERROR_INVALID_NAME || errorCode == Win32Native.ERROR_BAD_PATHNAME;
138             String str = GetDisplayablePath(maybeFullPath, isInvalidPath);
139
140             switch (errorCode) {
141             case Win32Native.ERROR_FILE_NOT_FOUND:
142                 if (str.Length == 0)
143                     throw new FileNotFoundException(Environment.GetResourceString("IO.FileNotFound"));
144                 else
145                     throw new FileNotFoundException(Environment.GetResourceString("IO.FileNotFound_FileName", str), str);
146                 
147             case Win32Native.ERROR_PATH_NOT_FOUND:
148                 if (str.Length == 0)
149                     throw new DirectoryNotFoundException(Environment.GetResourceString("IO.PathNotFound_NoPathName"));
150                 else
151                     throw new DirectoryNotFoundException(Environment.GetResourceString("IO.PathNotFound_Path", str));
152
153             case Win32Native.ERROR_ACCESS_DENIED:
154                 if (str.Length == 0)
155                     throw new UnauthorizedAccessException(Environment.GetResourceString("UnauthorizedAccess_IODenied_NoPathName"));
156                 else
157                     throw new UnauthorizedAccessException(Environment.GetResourceString("UnauthorizedAccess_IODenied_Path", str));
158
159             case Win32Native.ERROR_ALREADY_EXISTS:
160                 if (str.Length == 0)
161                     goto default;
162                 throw new IOException(Environment.GetResourceString("IO.IO_AlreadyExists_Name", str), Win32Native.MakeHRFromErrorCode(errorCode), maybeFullPath);
163
164             case Win32Native.ERROR_FILENAME_EXCED_RANGE:
165                 throw new PathTooLongException(Environment.GetResourceString("IO.PathTooLong"));
166
167             case Win32Native.ERROR_INVALID_DRIVE:
168                 throw new DriveNotFoundException(Environment.GetResourceString("IO.DriveNotFound_Drive", str));
169
170             case Win32Native.ERROR_INVALID_PARAMETER:
171                 throw new IOException(Win32Native.GetMessage(errorCode), Win32Native.MakeHRFromErrorCode(errorCode), maybeFullPath);
172
173             case Win32Native.ERROR_SHARING_VIOLATION:
174                 if (str.Length == 0)
175                     throw new IOException(Environment.GetResourceString("IO.IO_SharingViolation_NoFileName"), Win32Native.MakeHRFromErrorCode(errorCode), maybeFullPath);
176                 else
177                     throw new IOException(Environment.GetResourceString("IO.IO_SharingViolation_File", str), Win32Native.MakeHRFromErrorCode(errorCode), maybeFullPath);
178
179             case Win32Native.ERROR_FILE_EXISTS:
180                 if (str.Length == 0)
181                     goto default;
182                 throw new IOException(Environment.GetResourceString("IO.IO_FileExists_Name", str), Win32Native.MakeHRFromErrorCode(errorCode), maybeFullPath);
183
184             case Win32Native.ERROR_OPERATION_ABORTED:
185                 throw new OperationCanceledException();
186
187             default:
188                 throw new IOException(Win32Native.GetMessage(errorCode), Win32Native.MakeHRFromErrorCode(errorCode), maybeFullPath);
189             }
190         }
191 #if !MONO
192         // An alternative to WinIOError with friendlier messages for drives
193         [System.Security.SecuritySafeCritical]  // auto-generated
194         internal static void WinIODriveError(String driveName) {
195             int errorCode = Marshal.GetLastWin32Error();
196             WinIODriveError(driveName, errorCode);
197         }
198
199         [System.Security.SecurityCritical]  // auto-generated
200         internal static void WinIODriveError(String driveName, int errorCode)
201         {
202             switch (errorCode) {
203             case Win32Native.ERROR_PATH_NOT_FOUND:
204             case Win32Native.ERROR_INVALID_DRIVE:
205                 throw new DriveNotFoundException(Environment.GetResourceString("IO.DriveNotFound_Drive", driveName));
206
207             default: 
208                 WinIOError(errorCode, driveName);                
209                 break;
210             }
211         }
212 #endif
213         internal static void WriteNotSupported() {
214             throw new NotSupportedException(Environment.GetResourceString("NotSupported_UnwritableStream"));
215         }
216
217         internal static void WriterClosed() {
218             throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_WriterClosed"));
219         }
220 #if !MONO
221         // From WinError.h
222         internal const int ERROR_FILE_NOT_FOUND = Win32Native.ERROR_FILE_NOT_FOUND;
223         internal const int ERROR_PATH_NOT_FOUND = Win32Native.ERROR_PATH_NOT_FOUND;
224         internal const int ERROR_ACCESS_DENIED  = Win32Native.ERROR_ACCESS_DENIED;
225         internal const int ERROR_INVALID_PARAMETER = Win32Native.ERROR_INVALID_PARAMETER;
226 #endif
227     }
228 }