xref: /aoo4110/main/sal/osl/w32/file_error.c (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #define UNICODE
25 #define _UNICODE
26 #define _WIN32_WINNT_0x0500
27 #include "systools/win32/uwinapi.h"
28 
29 #include "file_error.h"
30 
31 #include "osl/diagnose.h"
32 #include "osl/thread.h"
33 
34 /* OS error to oslFileError values mapping table */
35 struct osl_file_error_entry
36 {
37 	unsigned long oscode; /* OS return value */
38 	int errnocode;        /* oslFileError code */
39 };
40 
41 static const struct osl_file_error_entry errtable[] = {
42   {  ERROR_SUCCESS,				   osl_File_E_None     },  /* 0 */
43   {  ERROR_INVALID_FUNCTION,       osl_File_E_INVAL    },  /* 1 */
44   {  ERROR_FILE_NOT_FOUND,         osl_File_E_NOENT    },  /* 2 */
45   {  ERROR_PATH_NOT_FOUND,         osl_File_E_NOENT    },  /* 3 */
46   {  ERROR_TOO_MANY_OPEN_FILES,    osl_File_E_MFILE    },  /* 4 */
47   {  ERROR_ACCESS_DENIED,          osl_File_E_ACCES    },  /* 5 */
48   {  ERROR_INVALID_HANDLE,         osl_File_E_BADF     },  /* 6 */
49   {  ERROR_ARENA_TRASHED,          osl_File_E_NOMEM    },  /* 7 */
50   {  ERROR_NOT_ENOUGH_MEMORY,      osl_File_E_NOMEM    },  /* 8 */
51   {  ERROR_INVALID_BLOCK,          osl_File_E_NOMEM    },  /* 9 */
52   {  ERROR_BAD_ENVIRONMENT,        osl_File_E_2BIG     },  /* 10 */
53   {  ERROR_BAD_FORMAT,             osl_File_E_NOEXEC   },  /* 11 */
54   {  ERROR_INVALID_ACCESS,         osl_File_E_INVAL    },  /* 12 */
55   {  ERROR_INVALID_DATA,           osl_File_E_INVAL    },  /* 13 */
56   {  ERROR_INVALID_DRIVE,          osl_File_E_NOENT    },  /* 15 */
57   {  ERROR_CURRENT_DIRECTORY,      osl_File_E_ACCES    },  /* 16 */
58   {  ERROR_NOT_SAME_DEVICE,        osl_File_E_XDEV     },  /* 17 */
59   {  ERROR_NO_MORE_FILES,          osl_File_E_NOENT    },  /* 18 */
60   {  ERROR_NOT_READY,              osl_File_E_NOTREADY },  /* 21 */
61   {  ERROR_LOCK_VIOLATION,         osl_File_E_ACCES    },  /* 33 */
62   {  ERROR_BAD_NETPATH,            osl_File_E_NOENT    },  /* 53 */
63   {  ERROR_NETWORK_ACCESS_DENIED,  osl_File_E_ACCES    },  /* 65 */
64   {  ERROR_BAD_NET_NAME,           osl_File_E_NOENT    },  /* 67 */
65   {  ERROR_FILE_EXISTS,            osl_File_E_EXIST    },  /* 80 */
66   {  ERROR_CANNOT_MAKE,            osl_File_E_ACCES    },  /* 82 */
67   {  ERROR_FAIL_I24,               osl_File_E_ACCES    },  /* 83 */
68   {  ERROR_INVALID_PARAMETER,      osl_File_E_INVAL    },  /* 87 */
69   {  ERROR_NO_PROC_SLOTS,          osl_File_E_AGAIN    },  /* 89 */
70   {  ERROR_DRIVE_LOCKED,           osl_File_E_ACCES    },  /* 108 */
71   {  ERROR_BROKEN_PIPE,            osl_File_E_PIPE     },  /* 109 */
72   {  ERROR_DISK_FULL,              osl_File_E_NOSPC    },  /* 112 */
73   {  ERROR_INVALID_TARGET_HANDLE,  osl_File_E_BADF     },  /* 114 */
74   {  ERROR_INVALID_HANDLE,         osl_File_E_INVAL    },  /* 124 */
75   {  ERROR_WAIT_NO_CHILDREN,       osl_File_E_CHILD    },  /* 128 */
76   {  ERROR_CHILD_NOT_COMPLETE,     osl_File_E_CHILD    },  /* 129 */
77   {  ERROR_DIRECT_ACCESS_HANDLE,   osl_File_E_BADF     },  /* 130 */
78   {  ERROR_NEGATIVE_SEEK,          osl_File_E_INVAL    },  /* 131 */
79   {  ERROR_SEEK_ON_DEVICE,         osl_File_E_ACCES    },  /* 132 */
80   {  ERROR_DIR_NOT_EMPTY,          osl_File_E_NOTEMPTY },  /* 145 */
81   {  ERROR_NOT_LOCKED,             osl_File_E_ACCES    },  /* 158 */
82   {  ERROR_BAD_PATHNAME,           osl_File_E_NOENT    },  /* 161 */
83   {  ERROR_MAX_THRDS_REACHED,      osl_File_E_AGAIN    },  /* 164 */
84   {  ERROR_LOCK_FAILED,            osl_File_E_ACCES    },  /* 167 */
85   {  ERROR_ALREADY_EXISTS,         osl_File_E_EXIST    },  /* 183 */
86   {  ERROR_FILENAME_EXCED_RANGE,   osl_File_E_NOENT    },  /* 206 */
87   {  ERROR_NESTING_NOT_ALLOWED,    osl_File_E_AGAIN    },  /* 215 */
88   {  ERROR_DIRECTORY,              osl_File_E_NOENT    },  /* 267 */
89   {  ERROR_NOT_ENOUGH_QUOTA,       osl_File_E_NOMEM    },  /* 1816 */
90   {  ERROR_CANT_ACCESS_FILE,       osl_File_E_ACCES    },  /* 1920 */
91   {  ERROR_UNEXP_NET_ERR,          osl_File_E_NETWORK  },   /* 59 */
92   {  ERROR_FILE_CHECKED_OUT,       osl_File_E_ACCES    },   /* 220 The file is locked or checked out by another user. */
93   {  ERROR_INVALID_NAME,           osl_File_E_NOENT    }   /*  123 One or more of the names composing the file path has a wrong syntax. */
94 };
95 
96 /* The following two constants must be the minimum and maximum
97    values in the (contiguous) range of osl_File_E_xec Failure errors.
98 */
99 #define MIN_EXEC_ERROR ERROR_INVALID_STARTING_CODESEG
100 #define MAX_EXEC_ERROR ERROR_INFLOOP_IN_RELOC_CHAIN
101 
102 /* These are the low and high value in the range of errors that are
103    access violations
104 */
105 #define MIN_EACCES_RANGE ERROR_WRITE_PROTECT
106 #define MAX_EACCES_RANGE ERROR_SHARING_BUFFER_EXCEEDED
107 
oslTranslateFileError(unsigned long dwError)108 oslFileError oslTranslateFileError (/*DWORD*/ unsigned long dwError)
109 {
110 	static const int n = sizeof(errtable)/sizeof(errtable[0]);
111 
112 	int i;
113 	for (i = 0; i < n; ++i )
114 	{
115 		if (dwError == errtable[i].oscode)
116 			return (oslFileError)(errtable[i].errnocode);
117 	}
118 
119 	/* The error code wasn't in the table.  We check for a range of
120 	   osl_File_E_ACCES errors or exec failure errors (ENOEXEC).
121 	   Otherwise osl_File_E_INVAL is returned.
122 	*/
123 	if ( (dwError >= MIN_EACCES_RANGE) && (dwError <= MAX_EACCES_RANGE) )
124 		return osl_File_E_ACCES;
125 	else if ( (dwError >= MIN_EXEC_ERROR) && (dwError <= MAX_EXEC_ERROR) )
126 		return osl_File_E_NOEXEC;
127 	else
128 		return osl_File_E_INVAL;
129 }
130 
131 //#####################################################
132 #if OSL_DEBUG_LEVEL > 0
_osl_warnFile(const char * message,rtl_uString * ustrFile)133 void _osl_warnFile( const char *message, rtl_uString *ustrFile )
134 {
135 	char szBuffer[2048];
136 
137 	if (ustrFile)
138 	{
139 		rtl_String	*strFile = NULL;
140 
141 		rtl_uString2String( &strFile, rtl_uString_getStr( ustrFile ), rtl_uString_getLength( ustrFile ),
142 							osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS );
143 		snprintf( szBuffer, sizeof(szBuffer), message, strFile->buffer );
144 		rtl_string_release( strFile );
145 
146 		message = szBuffer;
147 	}
148 	OSL_ENSURE( 0, message );
149 }
150 #endif /* OSL_DEBUG_LEVEL */
151