xref: /aoo42x/main/sal/osl/w32/file_error.c (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #define UNICODE
29*cdf0e10cSrcweir #define _UNICODE
30*cdf0e10cSrcweir #define _WIN32_WINNT_0x0500
31*cdf0e10cSrcweir #include "systools/win32/uwinapi.h"
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include "file_error.h"
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include "osl/diagnose.h"
36*cdf0e10cSrcweir #include "osl/thread.h"
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir /* OS error to oslFileError values mapping table */
39*cdf0e10cSrcweir struct osl_file_error_entry
40*cdf0e10cSrcweir {
41*cdf0e10cSrcweir 	unsigned long oscode; /* OS return value */
42*cdf0e10cSrcweir 	int errnocode;        /* oslFileError code */
43*cdf0e10cSrcweir };
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir static const struct osl_file_error_entry errtable[] = {
46*cdf0e10cSrcweir   {  ERROR_SUCCESS,				   osl_File_E_None     },  /* 0 */
47*cdf0e10cSrcweir   {  ERROR_INVALID_FUNCTION,       osl_File_E_INVAL    },  /* 1 */
48*cdf0e10cSrcweir   {  ERROR_FILE_NOT_FOUND,         osl_File_E_NOENT    },  /* 2 */
49*cdf0e10cSrcweir   {  ERROR_PATH_NOT_FOUND,         osl_File_E_NOENT    },  /* 3 */
50*cdf0e10cSrcweir   {  ERROR_TOO_MANY_OPEN_FILES,    osl_File_E_MFILE    },  /* 4 */
51*cdf0e10cSrcweir   {  ERROR_ACCESS_DENIED,          osl_File_E_ACCES    },  /* 5 */
52*cdf0e10cSrcweir   {  ERROR_INVALID_HANDLE,         osl_File_E_BADF     },  /* 6 */
53*cdf0e10cSrcweir   {  ERROR_ARENA_TRASHED,          osl_File_E_NOMEM    },  /* 7 */
54*cdf0e10cSrcweir   {  ERROR_NOT_ENOUGH_MEMORY,      osl_File_E_NOMEM    },  /* 8 */
55*cdf0e10cSrcweir   {  ERROR_INVALID_BLOCK,          osl_File_E_NOMEM    },  /* 9 */
56*cdf0e10cSrcweir   {  ERROR_BAD_ENVIRONMENT,        osl_File_E_2BIG     },  /* 10 */
57*cdf0e10cSrcweir   {  ERROR_BAD_FORMAT,             osl_File_E_NOEXEC   },  /* 11 */
58*cdf0e10cSrcweir   {  ERROR_INVALID_ACCESS,         osl_File_E_INVAL    },  /* 12 */
59*cdf0e10cSrcweir   {  ERROR_INVALID_DATA,           osl_File_E_INVAL    },  /* 13 */
60*cdf0e10cSrcweir   {  ERROR_INVALID_DRIVE,          osl_File_E_NOENT    },  /* 15 */
61*cdf0e10cSrcweir   {  ERROR_CURRENT_DIRECTORY,      osl_File_E_ACCES    },  /* 16 */
62*cdf0e10cSrcweir   {  ERROR_NOT_SAME_DEVICE,        osl_File_E_XDEV     },  /* 17 */
63*cdf0e10cSrcweir   {  ERROR_NO_MORE_FILES,          osl_File_E_NOENT    },  /* 18 */
64*cdf0e10cSrcweir   {  ERROR_NOT_READY,              osl_File_E_NOTREADY },  /* 21 */
65*cdf0e10cSrcweir   {  ERROR_LOCK_VIOLATION,         osl_File_E_ACCES    },  /* 33 */
66*cdf0e10cSrcweir   {  ERROR_BAD_NETPATH,            osl_File_E_NOENT    },  /* 53 */
67*cdf0e10cSrcweir   {  ERROR_NETWORK_ACCESS_DENIED,  osl_File_E_ACCES    },  /* 65 */
68*cdf0e10cSrcweir   {  ERROR_BAD_NET_NAME,           osl_File_E_NOENT    },  /* 67 */
69*cdf0e10cSrcweir   {  ERROR_FILE_EXISTS,            osl_File_E_EXIST    },  /* 80 */
70*cdf0e10cSrcweir   {  ERROR_CANNOT_MAKE,            osl_File_E_ACCES    },  /* 82 */
71*cdf0e10cSrcweir   {  ERROR_FAIL_I24,               osl_File_E_ACCES    },  /* 83 */
72*cdf0e10cSrcweir   {  ERROR_INVALID_PARAMETER,      osl_File_E_INVAL    },  /* 87 */
73*cdf0e10cSrcweir   {  ERROR_NO_PROC_SLOTS,          osl_File_E_AGAIN    },  /* 89 */
74*cdf0e10cSrcweir   {  ERROR_DRIVE_LOCKED,           osl_File_E_ACCES    },  /* 108 */
75*cdf0e10cSrcweir   {  ERROR_BROKEN_PIPE,            osl_File_E_PIPE     },  /* 109 */
76*cdf0e10cSrcweir   {  ERROR_DISK_FULL,              osl_File_E_NOSPC    },  /* 112 */
77*cdf0e10cSrcweir   {  ERROR_INVALID_TARGET_HANDLE,  osl_File_E_BADF     },  /* 114 */
78*cdf0e10cSrcweir   {  ERROR_INVALID_HANDLE,         osl_File_E_INVAL    },  /* 124 */
79*cdf0e10cSrcweir   {  ERROR_WAIT_NO_CHILDREN,       osl_File_E_CHILD    },  /* 128 */
80*cdf0e10cSrcweir   {  ERROR_CHILD_NOT_COMPLETE,     osl_File_E_CHILD    },  /* 129 */
81*cdf0e10cSrcweir   {  ERROR_DIRECT_ACCESS_HANDLE,   osl_File_E_BADF     },  /* 130 */
82*cdf0e10cSrcweir   {  ERROR_NEGATIVE_SEEK,          osl_File_E_INVAL    },  /* 131 */
83*cdf0e10cSrcweir   {  ERROR_SEEK_ON_DEVICE,         osl_File_E_ACCES    },  /* 132 */
84*cdf0e10cSrcweir   {  ERROR_DIR_NOT_EMPTY,          osl_File_E_NOTEMPTY },  /* 145 */
85*cdf0e10cSrcweir   {  ERROR_NOT_LOCKED,             osl_File_E_ACCES    },  /* 158 */
86*cdf0e10cSrcweir   {  ERROR_BAD_PATHNAME,           osl_File_E_NOENT    },  /* 161 */
87*cdf0e10cSrcweir   {  ERROR_MAX_THRDS_REACHED,      osl_File_E_AGAIN    },  /* 164 */
88*cdf0e10cSrcweir   {  ERROR_LOCK_FAILED,            osl_File_E_ACCES    },  /* 167 */
89*cdf0e10cSrcweir   {  ERROR_ALREADY_EXISTS,         osl_File_E_EXIST    },  /* 183 */
90*cdf0e10cSrcweir   {  ERROR_FILENAME_EXCED_RANGE,   osl_File_E_NOENT    },  /* 206 */
91*cdf0e10cSrcweir   {  ERROR_NESTING_NOT_ALLOWED,    osl_File_E_AGAIN    },  /* 215 */
92*cdf0e10cSrcweir   {  ERROR_DIRECTORY,              osl_File_E_NOENT    },  /* 267 */
93*cdf0e10cSrcweir   {  ERROR_NOT_ENOUGH_QUOTA,       osl_File_E_NOMEM    },  /* 1816 */
94*cdf0e10cSrcweir   {  ERROR_UNEXP_NET_ERR,          osl_File_E_NETWORK  }   /* 59 */
95*cdf0e10cSrcweir };
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir /* The following two constants must be the minimum and maximum
98*cdf0e10cSrcweir    values in the (contiguous) range of osl_File_E_xec Failure errors.
99*cdf0e10cSrcweir */
100*cdf0e10cSrcweir #define MIN_EXEC_ERROR ERROR_INVALID_STARTING_CODESEG
101*cdf0e10cSrcweir #define MAX_EXEC_ERROR ERROR_INFLOOP_IN_RELOC_CHAIN
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir /* These are the low and high value in the range of errors that are
104*cdf0e10cSrcweir    access violations
105*cdf0e10cSrcweir */
106*cdf0e10cSrcweir #define MIN_EACCES_RANGE ERROR_WRITE_PROTECT
107*cdf0e10cSrcweir #define MAX_EACCES_RANGE ERROR_SHARING_BUFFER_EXCEEDED
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir oslFileError oslTranslateFileError (/*DWORD*/ unsigned long dwError)
110*cdf0e10cSrcweir {
111*cdf0e10cSrcweir 	static const int n = sizeof(errtable)/sizeof(errtable[0]);
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir 	int i;
114*cdf0e10cSrcweir 	for (i = 0; i < n; ++i )
115*cdf0e10cSrcweir 	{
116*cdf0e10cSrcweir 		if (dwError == errtable[i].oscode)
117*cdf0e10cSrcweir 			return (oslFileError)(errtable[i].errnocode);
118*cdf0e10cSrcweir 	}
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir 	/* The error code wasn't in the table.  We check for a range of
121*cdf0e10cSrcweir 	   osl_File_E_ACCES errors or exec failure errors (ENOEXEC).
122*cdf0e10cSrcweir 	   Otherwise osl_File_E_INVAL is returned.
123*cdf0e10cSrcweir 	*/
124*cdf0e10cSrcweir 	if ( (dwError >= MIN_EACCES_RANGE) && (dwError <= MAX_EACCES_RANGE) )
125*cdf0e10cSrcweir 		return osl_File_E_ACCES;
126*cdf0e10cSrcweir 	else if ( (dwError >= MIN_EXEC_ERROR) && (dwError <= MAX_EXEC_ERROR) )
127*cdf0e10cSrcweir 		return osl_File_E_NOEXEC;
128*cdf0e10cSrcweir 	else
129*cdf0e10cSrcweir 		return osl_File_E_INVAL;
130*cdf0e10cSrcweir }
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir //#####################################################
133*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
134*cdf0e10cSrcweir void _osl_warnFile( const char *message, rtl_uString *ustrFile )
135*cdf0e10cSrcweir {
136*cdf0e10cSrcweir 	char szBuffer[2048];
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir 	if (ustrFile)
139*cdf0e10cSrcweir 	{
140*cdf0e10cSrcweir 		rtl_String	*strFile = NULL;
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir 		rtl_uString2String( &strFile, rtl_uString_getStr( ustrFile ), rtl_uString_getLength( ustrFile ),
143*cdf0e10cSrcweir 							osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS );
144*cdf0e10cSrcweir 		snprintf( szBuffer, sizeof(szBuffer), message, strFile->buffer );
145*cdf0e10cSrcweir 		rtl_string_release( strFile );
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir 		message = szBuffer;
148*cdf0e10cSrcweir 	}
149*cdf0e10cSrcweir 	OSL_ENSURE( 0, message );
150*cdf0e10cSrcweir }
151*cdf0e10cSrcweir #endif /* OSL_DEBUG_LEVEL */
152